#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             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.

# Note: Sometimes the esx_vsphere_sensors check reports incorrect sensor data.
# The reason is that the data is cached on the esx system. In the worst case some sensors
# might get stuck in an unhealthy state. You can find more information under the following link:
# http://kb.vmware.com/selfservice/microsites/search.do?cmd=displayKC&externalId=1037330

# <<<esx_vsphere_sensors:sep(59)>>>
# VMware Rollup Health State;;0;system;0;;red;Red;Sensor is operating under critical conditions
# Power Domain 1 Power Unit 0 - Redundancy lost;;0;power;0;;yellow;Yellow;Sensor is operating under conditions that are non-critical
# Power Supply 2 Power Supply 2 0: Power Supply AC lost - Assert;;0;power;0;;red;Red;Sensor is operating under critical conditions


def inventory_esx_vsphere_sensors(info):
    yield None, []


def check_esx_vsphere_sensors(_no_item, params, info):

    mulitline = ["All sensors are in normal state", "Sensors operating normal are:"]
    mod_msg = " (Alert state has been modified by Check_MK rule)"

    for name, _base_units, _current_reading, _sensor_type, _unit_modifier, \
        _rate_units, health_key, health_label, health_summary in info:

        sensor_state = {"green": 0, "yellow": 1, "red": 2, "unknown": 3}.get(health_key.lower(), 2)
        txt = "%s: %s (%s)" % (name, health_label, health_summary)

        for entry in params:
            if name.startswith(entry.get("name", "")):
                new_state = entry.get("states", {}).get(str(sensor_state))
                if new_state is not None:
                    sensor_state = new_state
                    txt += mod_msg
                    break
        if sensor_state > 0 or txt.endswith(mod_msg):
            yield sensor_state, txt
            mulitline[:2] = "", "At least one sensor reported. Sensors readings are:"
        mulitline.append(txt)

    yield 0, '\n'.join(mulitline)


check_info['esx_vsphere_sensors'] = {
    "inventory_function": inventory_esx_vsphere_sensors,
    "check_function": check_esx_vsphere_sensors,
    "service_description": "Hardware Sensors",
    "group": "hostsystem_sensors"
}
