#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# # +-----------------------------------------------------------------+
# # |                                                                 |
# # |        (  ___ \     | \    /\|\     /||\     /|( (    /|        |
# # |        | (   ) )    |  \  / /| )   ( || )   ( ||  \  ( |        |
# # |        | (__/ /     |  (_/ / | |   | || (___) ||   \ | |        |
# # |        |  __ (      |   _ (  | |   | ||  ___  || (\ \) |        |
# # |        | (  \ \     |  ( \ \ | |   | || (   ) || | \   |        |
# # |        | )___) )_   |  /  \ \| (___) || )   ( || )  \  |        |
# # |        |/ \___/(_)  |_/    \/(_______)|/     \||/    )_)        |
# # |                                                                 |
# # | Copyright Bastian Kuhn 2011                mail@bastian-kuhn.de | 
# # +-----------------------------------------------------------------+
# # cmc_psm_m
# # Version 1.0 (07.04.2011)
# #
# # This file is a check Script for check_mk
# # Information about me can be found at http://bastian-kuhn.de
# # Information about check_mk at http://mathias-kettner.de/check_mk.
# #
# # This 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.
# #
# # Table columns:
# # 0: index
# # 1: sensor type (30 = Power PSM,)
# # 2: sensor state (4 = ok)
# # 3: current value (Ampere)
# # 4: critical level 
# # 5: warn low level 
# # 6: warn level 
# # 7: description 
# 
# # Sensor Type : [
# # service_desc,
# # extended desc, or None, 
# # output text, (first %f for current val, then %s for extendet_output)
# # extended output, or None, (=device Desc) 
# # perfunit
# # Current Value Division
# # ]
# 
# 
# # Idee hier: Wir machen Art von Sensor einen
# # parameter in einem dict-basierten Parameter.
# # cmc_lcp_default_levels = {
# #     "server_in" : (25, 30),
# #     "server_out" : (25, 30),
# #     "water_in" : (20, 25),
# #     "water_out" : (20, 25),
# # }
# # Wir ordnen dann jedem Sensortyp einen Parameter zu,
# # oder keinen, wenn man den von außen nicht einstellen kann.
# # Man kann dann mit nur einer Regel alles schön einstellen.
# # Dann noch die Perfometer reparieren. Dazu wir der Name
# # der Perfvariable genutzt. "rpm", "degree", "liters",
# # "percent" und ein passendes Perf-O-Meter gemalt.
# # PNP-Schablonen: Noch besser wären hier subchecks.
# # cmc_lcp.degree
# # cmc_lcp.liters
# # Die könnte man ja evtl. auch automatisch generieren. Das
# # mit den Levels könnte man auch machn, aber dann nur für
# # die Temperatur.
# 

# 
# def inventory_cmc_lcp(checkname, info):
#    inventory = []
#    for line in info:
#      sensor_type  = saveint(line[1])
#      if check_cmc_lcp_template.get(sensor_type):
#        item = ""
#        if check_cmc_lcp_template[sensor_type][0] != None:
# 	 item = check_cmc_lcp_template[sensor_type][0]
#        if check_cmc_lcp_template[sensor_type][1] != None:
# 	 sensor_index = saveint(line[0].split(".")[1])
# 	 item += " " + check_cmc_lcp_template[sensor_type][1][sensor_index] 
#        inventory.append((item+" - "+line[0],None))
#    return inventory
# 
# 
# def check_cmc_lcp(item, no_params, info):
#   
#    item = item.split(" - ")
#    item = item[1]
#    perf = []
#    output = "ERROR"
#    
#    for line in info: 
#      if line[0] != item:
#          continue
#    
#      service_desc,extended_desc,output_txt,extended_output, perf_var, devision = check_cmc_lcp_template[saveint(line[1])]
#      
#      view_val = float(line[3]) / devision
#      if perf_var != None:
#        perf = [(perf_var,view_val,"","","","")]
#        
#      if extended_output == None:
#        extended_output = line[4]
#      elif extended_output != False:
#        sensor_index = saveint(line[0].split(".")[1])
#        extended_output = extended_output[sensor_index] 
#      else:
#        extended_output = ""
#      
#      output = output_txt % (view_val,extended_output)
#      
#      status = saveint(line[2])
#      if status == 4:
#        return(0, "OK - "+output,perf)
#      elif status == 7:
#        return(1, "WARNING - "+output,perf)
#      else:
#        return(2, "CRIT - "+output,perf)
#    return (3, "Sensor not found")

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"
