#!/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 inventory_dell_poweredge_amperage_power(info):
    inventory = []
    for line in info:
        if line[6] != "" and line[5] in ( "24", "26" ):
            inventory.append( ( line[6], None ) )
    return inventory

def inventory_dell_poweredge_amperage_current(info):
    inventory = []
    for line in info:
        if line[6] != "" and line[5] in ( "23", "25" ):
            inventory.append( ( line[6], None ) )
    return inventory

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

        if item == LocationName:
            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, "2")

            if UpperNonCritical and UpperCritical:
                limittext = " (upper limits %s/%s)" % (UpperNonCritical, UpperCritical)
                maxi = savefloat(UpperCritical) * 1.1
            else:
                limittext = ""
                maxi = ""

            if ProbeType in ( "23", "25" ): # Amps
                current = str(int(Reading)/10.0)
                infotext = "%s Ampere %s" % ( current, state_txt )
                perfdata = [( "current", current+"A", UpperNonCritical, UpperCritical, "", maxi )]
            elif ProbeType in ( "24", "26" ): # Watts
                infotext = "%s Watt %s" % ( Reading, state_txt )
                perfdata = [( "power", Reading+"W", UpperNonCritical, UpperCritical, "", maxi )]
            else:
                infotext = "Unknown Probe Type %s" % ProbeType
                return 3, infotext

            return state, infotext+limittext, perfdata

    return 3, "Amperage Device not found"

check_info["dell_poweredge_amperage.power"] = {
    "check_function"        : check_dell_poweredge_amperage,
    "inventory_function"    : inventory_dell_poweredge_amperage_power,
    "service_description"   : "%s",
    "has_perfdata"          : True,
    "snmp_info"             : ( ".1.3.6.1.4.1.674.10892.5.4.600.30.1", [
                                      "1", # amperageProbechassisIndex
                                      "2", # amperageProbeIndex
                                      # "3", # amperageProbeStateCapabilities
                                      "4", # amperageProbeStateSettings
                                      "5", # amperageProbeStatus
                                      "6", # amperageProbeReading
                                      "7", # amperageProbeType
                                      "8", # amperageProbeLocationName
                                      #"9", # amperageProbeUpperNonRecoverableThreshold
                                      "10", # amperageProbeUpperCriticalThreshold
                                      "11", # amperageProbeUpperNonCriticalThreshold
                                      #"12", # amperageProbeLowerNonCriticalThreshold
                                      #"13", # amperageProbeLowerCriticalThreshold
                                      #"14", # amperageProbeLowerNonRecoverableThreshold
                                      #"15", # amperageProbeCapabilities
                                      #"16", # amperageProbeDiscreteReading
                              ]),
    "snmp_scan_function"    : lambda oid: oid('.1.3.6.1.2.1.1.2.0') == ".1.3.6.1.4.1.674.10892.5",
}

#check_info["dell_poweredge_amperage.current"] = {
#    "check_function"        : check_dell_poweredge_amperage,
#    "inventory_function"    : inventory_dell_poweredge_amperage_current,
#    "service_description"   : "%s",
#    "has_perfdata"          : True,
#    "snmp_info"             : dell_poweredge_amperage_info,
#    "snmp_scan_function"    : dell_poweredge_amperage_scan,
#}
