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


cmctc_lcp_sensors = {
       # Blower
       40 : ("1", "blower", ),
       41 : ("2", "blower", ),
       42 : ("3", "blower", ),
       43 : ("4", "blower", ),
       44 : ("5", "blower", ),
       45 : ("6", "blower", ),

       # Server in/out
       48 : ("Server in 1",           "temp", ),
       49 : ("Server out 1",          "temp", ),
       50 : ("Server in 2",           "temp", ),
       51 : ("Server out 2",          "temp", ),
       52 : ("Server in 3",           "temp", ),
       53 : ("Server out 3",          "temp", ),

       # Overview Server
       56 : ("Overview Server in",    "temp", ),
       57 : ("Overview Server out",   "temp", ),

       # Water
       58 : ("Water in",     "temp", ),
       59 : ("Water out",    "temp", ),
       60 : (None, "flow"),

       # Other stuff
       61 : (None, "blowergrade" ),
       62 : (None, "regulator" ),
}

cmctc_lcp_sensortypes = {
    "temp" :        ( "°C",    "Temperature" ),
    "blower" :      ( " RPM",   "Blower" ),
    "blowergrade" : ( "",      "Blower Grade" ),
    "flow" :        ( " l/min", "Waterflow" ),
    "regulator" :   ( "%",     "Regulator" ),
}

def inventory_cmctc_lcp(info, sensortype):
    inventory = []
    for index, typeid, status, value, description in info:
        typeid = saveint(typeid)
        if typeid in cmctc_lcp_sensors:
            item, st = cmctc_lcp_sensors[typeid]
            if st == sensortype:
                if item:
                    item = item + " - " + index
                else:
                    item = index
                inventory.append((item, None))
    return inventory

def check_cmctc_lcp(item, params, info, sensortype):
    itemindex = item.split(" - ")[-1]
    for index, typeid, statuscode, value, description in info:
        if itemindex == index:
            unit = cmctc_lcp_sensortypes[sensortype][0]
            value = int(value)
            infotext = " - "
            if description:
                infotext += description + ", "
            infotext += "%d%s" % (value, unit)
            status = 0
            if params:
                warn, crit = params
                perfdata = [(sensortype, value, warn, crit)]
                if value >= crit:
                    status = 2
                elif value >= warn:
                    status = 1
                if status:
                    infotext += " (levels at %d%s/%d%s)" % (warn, unit, crit, unit)
            else:
                perfdata = [(sensortype, value)]
                if statuscode == "4":
                    status = 0
                elif statuscode == "7":
                    status = 1
                else:
                    status = 2
            return (status, nagios_state_names[status] + infotext, perfdata)
    return 3, "UNKNOWN - Sensor not found in SNMP output"

snmp_scan_functions["cmctc_lcp"] = \
    lambda oid: "CMC-TC" in oid(".1.3.6.1.2.1.1.1.0")

snmp_info["cmctc_lcp"] = (
   ".1.3.6.1.4.1.2606.4.2",
   ["3", "4", "5", "6"],
   [
     "5.2.1.1", # Index
     "5.2.1.2", # Sensor Type
     "5.2.1.4", # Status
     "5.2.1.5", # Value
     "7.2.1.2", # Description
   ]
)


for s, i in cmctc_lcp_sensortypes.items():
    check_info['cmctc_lcp.' + s] = {
        "check_function" :      (lambda st: lambda item,params,info: check_cmctc_lcp(item, params, info, st))(s),
        "inventory_function" :  (lambda st: lambda info: inventory_cmctc_lcp(info, st))(s),
        "has_perfdata" :        True,
        "service_description" : i[1],
    }

# Set WATO check group for temperature checks
check_info['cmctc_lcp.temp']['group'] = "room_temperature"
