#!/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-
# tails. 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" :        ( u" °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, high, low, warn, 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):
    map_sensor_state = {
        "1" : (3, "not available"),
        "2" : (2, "lost"),
        "3" : (1, "changed"),
        "4" : (0, "ok"),
        "5" : (2, "off"),
        "6" : (0, "on"),
        "7" : (1, "warning"),
        "8" : (2, "too low"),
        "9" : (2, "too high"),
    }
    itemindex = item.split(" - ")[-1]
    for index, typeid, statuscode, value, high, low, warn, description in info:
        if itemindex == index:
            unit = cmctc_lcp_sensortypes[sensortype][0]
            value = int(value)
            infotext = ""
            if description:
                infotext += "[%s] " % description
            state, extra_info = map_sensor_state[statuscode]
            yield state, "%s%d%s" % (infotext, value, unit)

            extra_state = 0
            if params:
                warn, crit = params
                perfdata = [(sensortype, value, warn, crit)]
                if value >= crit:
                    extra_state = 2
                elif value >= warn:
                    extra_state = 1

                if extra_state:
                    extra_info += " (warn/crit at %d/%d%s)" % (warn, crit, unit)
            else:
                perfdata = [(sensortype, value)]
                # Assumption: if high and low are both 0
                # then there are no device levels
                if not (int(high) == 0 and int(low) == 0) and int(high) > int(low):
                    if value >= int(high) or value <= int(low):
                        extra_state = 2
                        extra_info += " (device lower/upper crit at %s/%s%s)" % (low, high, unit)

            yield extra_state, extra_info, perfdata


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

def check_cmctc_lcp_temp(item, params, info):
    itemindex = item.split(" - ")[-1]
    for index, typeid, statuscode, value, high, low, warn, description in info:
        if itemindex == index:
            status = int(statuscode)

            if high == "0" and low == "0" and warn == "0":
                levels = None
            else:
                levels = float(warn), float(high)

            if high == "0" and low == "0" and warn == "0":
                levels_low = None
            else:
                levels_low = float(low), float("-inf") # no lower critical level specified

            return check_temperature(float(value), params, "cmctc_lcp_temp_%s" % item,
                    dev_levels=levels,
                    dev_levels_lower=levels_low,
                    dev_status=cmctc_translate_status(status),
                    dev_status_name="Unit: %s" % cmctc_translate_status_text(status))
    return 3, "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
     "5.2.1.6", # High
     "5.2.1.7", # Low
     "5.2.1.8", # Warn
     "7.2.1.2", # Description
   ]
)


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

# temperature check is standardised
check_info['cmctc_lcp.temp'] = {
    "check_function"      : check_cmctc_lcp_temp,
    "inventory_function"  : inventory_cmctc_lcp_temp,
    "has_perfdata"        : True,
    "service_description" : "Temperature %s",
    "group"               : "temperature",
    "includes"            : [ "temperature.include", "cmctc.include" ]
    }
