#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# <<<hp_msa_fan>>>
# fan 1 durable-id fan_1.1
# fan 1 name Fan Loc:left-PSU 1
# fan 1 location Enclosure 1 - Left
# fan 1 status Up
# fan 1 status-numeric 0
# fan 1 speed 3760
# fan 1 position Left
# fan 1 position-numeric 0
# fan 1 serial-number
# fan 1 fw-revision
# fan 1 hw-revision
# fan 1 health OK
# fan 1 health-numeric 0
# fan 1 health-reason
# fan 1 health-recommendation
# fan 2 durable-id fan_1.2
# fan 2 name Fan Loc:right-PSU 2
# fan 2 location Enclosure 1 - Right
# fan 2 status Up
# fan 2 status-numeric 0
# fan 2 speed 3880
# fan 2 position Right
# fan 2 position-numeric 1
# fan 2 serial-number
# fan 2 fw-revision
# fan 2 hw-revision
# fan 2 health OK
# fan 2 health-numeric 0
# fan 2 health-reason
# fan 2 health-recommendation

def inventory_hp_msa_fan(parsed):
    for item in parsed.keys():
        yield item, None


def check_hp_msa_fan(item, params, parsed):
    if item in parsed:
        fan_speed = int(parsed[item]["speed"])
        fan_state, fan_state_readable = \
            hp_msa_state_map[parsed[item]["status"]]
        fan_health_state, fan_health_state_readable = \
            hp_msa_state_map[parsed[item]["health"]]
        fan_health_reason = parsed[item].get("health-reason", "")

        yield fan_state, "Status: %s, speed: %s RPM" % (fan_state_readable, fan_speed)

        if fan_health_state > 0 and fan_health_reason:
            yield fan_health_state, "health: %s (%s)" % \
                  (fan_health_state_readable, fan_health_reason)


check_info['hp_msa_fan'] = {
    'parse_function'        : parse_hp_msa,
    'inventory_function'    : inventory_hp_msa_fan,
    'check_function'        : check_hp_msa_fan,
    'service_description'   : 'Fan %s',
    'includes'              : [ "hp_msa.include" ],
}
