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

def dell_poweredge_temp_makeitem(chassisIndex, Index, LocationName):
    if LocationName:
        item = LocationName
    else:
        item = chassisIndex + "-" + Index
    if item.endswith(" Temp"):
        item = item[:-5]
    return item


def inventory_dell_poweredge_temp(info):
    for line in info:
        if line[2] != '1':  # StateSettings not 'unknown'
            item = dell_poweredge_temp_makeitem(line[0], line[1], line[5])
            yield item, None


def check_dell_poweredge_temp(item, _no_params, info):
    for chassisIndex, Index, StateSettings, Status, Reading, LocationName, \
        UpperCritical, UpperNonCritical in info:

        if item == dell_poweredge_temp_makeitem(chassisIndex, Index, LocationName):
            temp = int(Reading) / 10.0
            warn = int(UpperNonCritical) / 10.0
            crit = int(UpperCritical) / 10.0
            state_table = {
                 "1" : ("other",               1),
                 "2" : ("unknown",             1),
                 "3" : ("",                    0),
                 "4" : ("nonCriticalUpper",    1),
                 "5" : ("CriticalUpper",       2),
                 "6" : ("NonRecoverableUpper", 2),
                 "7" : ("nonCriticalLower",    1),
                 "8" : ("CriticalLower",       2),
                 "9" : ("NonRecoverableLower", 2),
                "10" : ("failed",              2),
            }
            state_txt, state = state_table.get(Status, ("unknown state", 3))
            if state:
                yield state, state_txt
            yield check_temperature(temp, (warn, crit))



check_info["dell_poweredge_temp"] = {
    "check_function"        : check_dell_poweredge_temp,
    "inventory_function"    : inventory_dell_poweredge_temp,
    "service_description"   : "Temperature %s",
    "has_perfdata"          : True,
    "snmp_info"             : ( ".1.3.6.1.4.1.674.10892.5.4.700.20.1", [
                                      "1", # temperatureProbechassisIndex
                                      "2", # temperatureProbeIndex
                                      "4", # temperatureProbeStateSettings
                                      "5", # temperatureProbeStatus
                                      "6", # temperatureProbeReading
                                      #"7", # temperatureProbeType
                                      "8", # temperatureProbeLocationName
                                      #"9", # temperatureProbeUpperNonRecoverableThreshold
                                      "10", # temperatureProbeUpperCriticalThreshold
                                      "11", # temperatureProbeUpperNonCriticalThreshold
                                      #"12", # temperatureProbeLowerNonCriticalThreshold
                                      #"13", # temperatureProbeLowerCriticalThreshold
                                      #"14", # temperatureProbeLowerNonRecoverableThreshold
                                      #"15", # temperatureProbeCapabilities
                                      #"16", # temperatureProbeDiscreteReading
                              ]),
    "snmp_scan_function"    : lambda oid: oid('.1.3.6.1.2.1.1.2.0') == ".1.3.6.1.4.1.674.10892.5",
    "includes"              : [ "temperature.include" ],
}

