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

# Author: Lars Michelsen <lm@mathias-kettner.de>, 2011-03-21

strem1_temp_defaultlevels     = (28, 32)
strem1_humidity_defaultlevels = (None, None)
strem1_wetness_defaultlevels  = (None, None)

def strem1_sensors_parse_info(info):
    # Change format of output: 1 tuple for each group
    info = zip(*info)

    parsed = []
    for group in info:
        grp = group[0]

        items = group[1:]
        for i in xrange(0, len(items), 3):
            parsed.append([ grp + ' ' + items[i]] + list(items[i:i+3]))
    return parsed


def inventory_strem1_sensors(info):
    inventory = []
    for index, typ, val, intval in strem1_sensors_parse_info(info[1]):
        lvls = 'strem1_temp_defaultlevels'
        if typ == 'Humidity':
            lvls = 'strem1_humidity_defaultlevels'
        elif typ == 'Wetness':
            lvls = 'strem1_wetness_defaultlevels'
        if val != '-999.9':
            inventory.append((index, lvls))
    return inventory

def check_strem1_sensors(item, params, info):
    for index, typ, val, intval in strem1_sensors_parse_info(info[1]):
        if index == item:
            uom = typ == 'Temperature' and info[0][0][0] or '%'
            val = float(val)
            warn, crit = params

            infotext = "%.1f" % val + uom
            perfdata = [ ( typ.lower(), infotext, warn, crit ) ]
            thrtext = []
            if warn:
                thrtext += ["warn at %.1f" % warn + uom]
            if crit:
                thrtext += ["crit at %.1f" % crit + uom]
            if thrtext:
                infotext += ' (%s)' % ', '.join(thrtext)

            if crit and val >= crit:
                return (2, "CRIT - %s is: " % typ + infotext, perfdata)
            elif warn and val >= warn:
                return (1, "WARN - %s is: " % typ + infotext, perfdata)
            else:
                return (0, "OK - %s is: " % typ + infotext, perfdata )
    return (3, "UNKNOWN - Sensor not found")


check_info['strem1_sensors'] = ( check_strem1_sensors, "Sensor - %s", 1, inventory_strem1_sensors)

#1,  # SENSATRONICS-EM1::group1Name
#2,  # SENSATRONICS-EM1::group1TempName
#3,  # SENSATRONICS-EM1::group1TempDataStr
#4,  # SENSATRONICS-EM1::group1TempDataInt
#5,  # SENSATRONICS-EM1::group1HumidName
#6,  # group1HumidDataStr
#7,  # group1HumidDataInt
#8,  # group1WetName
#9,  # group1WetDataStr
#10, # group1WetDataInt
snmp_info['strem1_sensors'] = [
    (".1.3.6.1.4.1.16174.1.1.3.2.3", [1]),
    (".1.3.6.1.4.1.16174.1.1.3.3", [ # SENSATRONICS-EM1::measurementSystem
        1, # First group
        2, # Second group
        3, # Third group
        4, # Fourth group
    ]),
]

snmp_scan_functions['strem1_sensors'] = \
        lambda oid: "Sensatronics EM1" in oid(".1.3.6.1.2.1.1.1.0")
