#!/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.


factory_settings["dell_om_sensors_default_levels"] = {
    "levels" : (50, 60)
}


def dell_om_sensors_item(name):
    return name.replace("Temp", "").strip()


def inventory_dell_om_sensors(info):
    for line in info:
        if line[3]:
            yield dell_om_sensors_item(line[3]), {}


def check_dell_om_sensors(item, params, info):
    #Probetypes found in check_openmanage3.pl
    probe_types = {
         1  : 'Other',      # type is other than following values
         2  : 'Unknown',    # type is unknown
         3  : 'AmbientESM', # type is Ambient Embedded Systems Management temperature probe
         16 : 'Discrete',   # type is temperature probe with discrete reading
    }
    sensor_states = {
        1  : "other",
        2  : "unknown",
        10 : "failed",
    }
    for idx, sensor_state, reading, location_name, dev_crit, dev_warn, dev_warn_lower, dev_crit_lower in info:
        if item == idx or dell_om_sensors_item(location_name) == item:
            sensor_state = int(sensor_state)
            if sensor_state in [ 1, 2, 10 ]:
                return 2, "Sensor is: " + sensor_states[sensor_state]

            temp = int(reading) / 10.0

            dev_warn, dev_crit, dev_warn_lower, dev_crit_lower = \
               map(lambda v: v and float(v)/10 or None, [ dev_warn, dev_crit, dev_warn_lower, dev_crit_lower ])

            return check_temperature(temp, params, "dell_om_sensors_%s" % item,
                                     dev_levels = (dev_warn, dev_crit),
                                     dev_levels_lower = (dev_warn_lower, dev_crit_lower))


check_info["dell_om_sensors"] = {
    "check_function"          : check_dell_om_sensors,
    "inventory_function"      : inventory_dell_om_sensors,
    "service_description"     : "Temperature %s",
    "has_perfdata"            : True,
    "group"                   : "temperature",
    # There is no other way to find out that openmanage is present.
    "snmp_scan_function"      : scan_dell_om,
    "snmp_info"               : ( ".1.3.6.1.4.1.674.10892.1.700.20.1", [
                                        '2', # ProbeIndex
                                        '5', # ProbeStatus
                                        '6', # ProbeReading
                                        #'7', # ProbeType
                                        '8', # ProbeLocationName
                                        '10', # ProbeUpperCriticalThreshold',
                                        '11', # ProbeUpperNonCriticalThreshold',
                                        '12', # ProbeLowerNonCriticalThreshold',
                                        '13', # ProbeLowerCriticalThreshold',
                                        #'16.1', # ProbeDiscreteReading',
                              ]),
    "includes"                : [ "dell_om.include", "temperature.include" ],
    "default_levels_variable" : "dell_om_sensors_default_levels"
}

