#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             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.

raritan_emx_sensors = {
   0 : "Air Temperature",
   1 : "Water Temperature",
   2 : "Fan Speed",
   3 : "Door Contact",
}

def inventory_raritan_emx(info):
    return [ (x[0] + " " + raritan_emx_sensors[saveint(x[2])] + " " +x[1], None) \
    for x in info if x[1] != "-1"]

def check_raritan_emx(item, params, info):
    units = {
        -1 : "",
        0 : "Other",
        7 : "°C",
        19 : "rpm",
    }
    sensor_states = {
        -1 :  "unavailable",
         0 :  "open",
         1 :  "closed",
         2 :  "belowLowerCritical",
         3 :  "belowLowerWarning",
         4 :  "normal",
         5 :  "aboveUpperWarning",
         6 :  "aboveUpperCritical",
         7 :  "on",
         8 :  "off",
         9 :  "detected",
         10:  "notDetected",
         11:  "alarmed",
    }
    state = 3
    message = "Sensor not found"
    perfdata = []
    for line in info:
        if line[0] + " " + raritan_emx_sensors[saveint(line[2])] + " " + line[1] == item:
            value, unit, sensor_state = map(saveint, line[3:6])
            if sensor_state in [1, 4, 7, 9]:
                state = 0
            elif sensor_state in [0,3,5]:
                state = 1
            elif sensor_state in [-1, 2, 6, 8, 10, 11]:
                state = 2

            #Temperature
            if unit == 7:
                value = value / 10.0

            if units[unit] != "":
                perfdata = [(units[unit], value)]

            message = "%s%s State: %s" % (str(value), units[unit],sensor_states[sensor_state])

    return (state, nagios_state_names[state] + " - " + message, perfdata)

check_info['raritan_emx'] = {
  "inventory_function"  : inventory_raritan_emx,
  "check_function"      : check_raritan_emx,
  "service_description" : "Rack %s",
  "has_perfdata"        : True,
  "snmp_info"           : ( ".1.3.6.1.4.1.13742.9", [   "1.4.1.1.1", #Rack ID
                                                        "1.4.1.1.4", #Name
                                                        "1.4.1.1.2", #Sensor
                                                        "2.1.1.3",  #Value
                                                        "1.4.1.1.5",  #Unit
                                                        "2.1.1.2",  #State
                                                        ] ),
  "snmp_scan_function"  : lambda oid: oid(".1.3.6.1.2.1.1.2.0") in [".1.3.6.1.4.1.13742.8"]

}
