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

# Structure of relevant MIB part (all are integers):
# airIRRCUnitStatusOperateMode                     1.3.6.1.4.1.318.1.1.13.3.2.2.2.1.0
# airIRRCUnitStatusCoolOutput                      1.3.6.1.4.1.318.1.1.13.3.2.2.2.2.0
# airIRRCUnitStatusCoolDemand                      1.3.6.1.4.1.318.1.1.13.3.2.2.2.3.0
# airIRRCUnitStatusAirFlowUS                       1.3.6.1.4.1.318.1.1.13.3.2.2.2.4.0
# airIRRCUnitStatusAirFlowMetric                   1.3.6.1.4.1.318.1.1.13.3.2.2.2.5.0
# airIRRCUnitStatusRackInletTempUS                 1.3.6.1.4.1.318.1.1.13.3.2.2.2.6.0
# airIRRCUnitStatusRackInletTempMetric             1.3.6.1.4.1.318.1.1.13.3.2.2.2.7.0
# airIRRCUnitStatusSupplyAirTempUS                 1.3.6.1.4.1.318.1.1.13.3.2.2.2.8.0
# airIRRCUnitStatusSupplyAirTempMetric             1.3.6.1.4.1.318.1.1.13.3.2.2.2.9.0
# airIRRCUnitStatusReturnAirTempUS                 1.3.6.1.4.1.318.1.1.13.3.2.2.2.10.0
# airIRRCUnitStatusReturnAirTempMetric             1.3.6.1.4.1.318.1.1.13.3.2.2.2.11.0
# airIRRCUnitStatusContainmtDPUS                   1.3.6.1.4.1.318.1.1.13.3.2.2.2.12.0
# airIRRCUnitStatusContainmtDPMetric               1.3.6.1.4.1.318.1.1.13.3.2.2.2.13.0
# airIRRCUnitStatusFilterDPUS                      1.3.6.1.4.1.318.1.1.13.3.2.2.2.14.0
# airIRRCUnitStatusFilterDPMetric                  1.3.6.1.4.1.318.1.1.13.3.2.2.2.15.0
# airIRRCUnitStatusFanSpeed                        1.3.6.1.4.1.318.1.1.13.3.2.2.2.16.0
# airIRRCUnitStatusInputState                      1.3.6.1.4.1.318.1.1.13.3.2.2.2.17.0
# airIRRCUnitStatusOutputState                     1.3.6.1.4.1.318.1.1.13.3.2.2.2.18.0
# airIRRCUnitStatusActivePowerSource               1.3.6.1.4.1.318.1.1.13.3.2.2.2.19.0
# airIRRCUnitStatusFluidValvePosition              1.3.6.1.4.1.318.1.1.13.3.2.2.2.20.0
# airIRRCUnitStatusFluidFlowUS                     1.3.6.1.4.1.318.1.1.13.3.2.2.2.21.0
# airIRRCUnitStatusFluidFlowMetric                 1.3.6.1.4.1.318.1.1.13.3.2.2.2.22.0
# airIRRCUnitStatusEnteringFluidTemperatureUS      1.3.6.1.4.1.318.1.1.13.3.2.2.2.23.0
# airIRRCUnitStatusEnteringFluidTemperatureMetric  1.3.6.1.4.1.318.1.1.13.3.2.2.2.24.0
# airIRRCUnitStatusLeavingFluidTemperatureUS       1.3.6.1.4.1.318.1.1.13.3.2.2.2.25.0
# airIRRCUnitStatusLeavingFluidTemperatureMetric   1.3.6.1.4.1.318.1.1.13.3.2.2.2.26.0

apc_inrow_temp_default_levels = ( 30, 35 )

def apc_inrow_temp_convert(info):
    vars = [
            "Rack Inlet",
            "Supply Air",
            "Return Air",
            "Entering Fluid",
            "Leaving Fluid",
            ]
    count = 0
    data = {}
    for name in vars:
        if len(info) > count and info[count] != "-1":
            value = int(info[count][0])
            data[name] = value / 10.0
        count += 1
    return data


def inventory_apc_inrow_temp(info):
    info = apc_inrow_temp_convert(info)
    return [ (x, "apc_inrow_temp_default_levels") for x in info.keys() ]


def check_apc_inrow_temp(item, params, info):
    info = apc_inrow_temp_convert(info)
    for sensor, value in info.items():
        if sensor == item:
            return check_temperature(value, params)


check_info["apc_inrow_temp"] = {
    "check_function"        : check_apc_inrow_temp,
    "inventory_function"    : inventory_apc_inrow_temp,
    "service_description"   : "Temperature %s",
    "group"                 : "hw_temperature",
    "snmp_info"             : (".1.3.6.1.4.1.318.1.1.13.3.2.2.2", [ 7, 9, 11, 24, 26  ]),
    "snmp_scan_function"    : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.318.1.3.14.5",
    "has_perfdata"          : True,
    "includes"              : [ "temperature.include" ],
}

