#!/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):
    return [(None, [])]

def check_esx_vsphere_sensors(_no_item, params, info):
    state = 0

    infos = []
    sensor_state_modified = False

    for name, base_units, current_reading, sensor_type, unit_modifier, rate_units, health_key, health_label, health_summary in info:
        health_key = health_key.lower()
        if health_key == "green":
            continue # usually not output by agent anyway
        infos.append("%s: %s (%s)" % (name, health_label, health_summary))

        sensor_state = 0
        if health_key == "yellow":
            sensor_state = 1
        elif health_key == "unknown":
            sensor_state = 1
        else:
            sensor_state = 2

        extra_info = ""
        if params:
            for entry in params:
                if name.startswith(entry.get("name")):
                    new_state = entry.get("states").get(str(sensor_state))
                    if new_state != None:
                        sensor_state          = new_state
                        extra_info            = "(Alert state has been modified by Check_MK rule)"
                        sensor_state_modified = True

        state = max(state, sensor_state)
        infos[-1] += ["", "(!)", "(!!)", "(!)"][state]
        infos[-1] += extra_info

    if state > 0 or sensor_state_modified:
        return state, ", ".join(infos)
    else:
        return 0, "All sensors are in normal state"

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