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

# .1.3.6.1.4.1.37954.1.2.7.1.0 RZ1SE-KLIMA-NEU  sensor name
# .1.3.6.1.4.1.37954.1.2.7.2.0 159              temperature     INTEGER (0..1000)
# .1.3.6.1.4.1.37954.1.2.7.3.0 474              humidity        INTEGER (0..1000)
# .1.3.6.1.4.1.37954.1.2.7.4.0 48               dew point       INTEGER (0..1000)
# .1.3.6.1.4.1.37954.1.2.7.5.0 0                carbon monoxide INTEGER (-100..100) # in percent
# .1.3.6.1.4.1.37954.1.2.7.6.0 0                motion          INTEGER (0..100)
# .1.3.6.1.4.1.37954.1.2.7.7.0 0                digital in 1    INTEGER (0..1)      # leakage sensor: 0 (no alarm, connected)
                                                                                    #                 1 (alarm or disconnected)
# .1.3.6.1.4.1.37954.1.2.7.8.0 0                digital in 2    INTEGER (0..1)
# .1.3.6.1.4.1.37954.1.2.7.9.0 0                digital out     INTEGER (0..1)
# .1.3.6.1.4.1.37954.1.2.7.10.0 0               comError        INTEGER (0..1)

# parsed:
# {'AlarmManager'   : { 'smoke': 0, 'humidity': 0.0,  'temp': 0.0 , 'leakage':0 },
#  'RZ1SE-INNENRAUM': { 'smoke': 0, 'humidity': 35.9, 'temp': 21.8, 'leakage':1 },
#  'RZ1SE-KLIMA-ALT': { 'smoke': 0, 'humidity': 34.4, 'temp': 22.5, 'leakage':0 },
#  'RZ1SE-KLIMA-NEU': { 'smoke': 0, 'humidity': 47.4, 'temp': 15.9, 'leakage':0 },
#  'RZ1SELI1'       : { 'smoke': 0, 'humidity': 35.6, 'temp': 21.6, 'leakage':0 },
#  'RZ1SERE1'       : { 'smoke': 0, 'humidity': 47.3, 'temp': 16.7, 'leakage':0 },
#  'RZ2AMR001'      : { 'smoke': 0, 'humidity': 36.7, 'temp': 16.6, 'leakage':0 },
#  'RZ2SE-INNENRAUM': { 'smoke': 0, 'humidity': 34.9, 'temp': 18.3, 'leakage':0 },
#  'RZ2SELI1'       : { 'smoke': 0, 'humidity': 41.9, 'temp': 15.1, 'leakage':0 }
# }

def parse_kentix_amp_sensors(info):
    info_flattened = []

    for i in xrange(0, len(info[0]), 10):
        info_flattened.append([ a[0] for a in info[0][i:i+10] ])

    parsed = {}
    for line in info_flattened:
        if line[0] != '':
            parsed[line[0]] = {
                'temp'      : float(line[1])/10,
                'humidity'  : float(line[2])/10,
                'smoke'     : int(line[4]),
            }
            if line[6] != '':
                parsed[line[0]]['leakage'] = int(line[6])

    return parsed


def inventory_kentix_amp_sensors(parsed, params):
    return [ (key, params) for key in parsed ]

#   .--temperature---------------------------------------------------------.
#   |      _                                      _                        |
#   |     | |_ ___ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |     | __/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |     | ||  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      \__\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   +----------------------------------------------------------------------+
#   |                            main check                                |
#   '----------------------------------------------------------------------'

def check_kentix_amp_sensors_temperature(item, params, parsed):
    if item in parsed:
        return check_temperature(parsed[item]['temp'], params, "kentix_amp_sensors_%s" % item)


check_info['kentix_amp_sensors'] = {
    'parse_function'            : parse_kentix_amp_sensors,
    'inventory_function'        : lambda parsed: inventory_kentix_amp_sensors(parsed, {}),
    'check_function'            : check_kentix_amp_sensors_temperature,
    'service_description'       : 'Temperature %s',
    'has_perfdata'              : True,
    'group'                     : 'temperature',
    'snmp_info'                 : [ ( ".1.3.6.1.4.1.37954.1.2", [ "" ]) ],
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.332.11.6"),
    'includes'                  : [ "temperature.include" ],
}

#.
#   .--humidity------------------------------------------------------------.
#   |              _                     _     _ _ _                       |
#   |             | |__  _   _ _ __ ___ (_) __| (_) |_ _   _               |
#   |             | '_ \| | | | '_ ` _ \| |/ _` | | __| | | |              |
#   |             | | | | |_| | | | | | | | (_| | | |_| |_| |              |
#   |             |_| |_|\__,_|_| |_| |_|_|\__,_|_|\__|\__, |              |
#   |                                                  |___/               |
#   +----------------------------------------------------------------------+


def check_kentix_amp_sensors_humidity(item, params, parsed):
    if item in parsed:
        return check_humidity(parsed[item]['humidity'], params)


check_info['kentix_amp_sensors.humidity'] = {
    'inventory_function'        : lambda parsed: inventory_kentix_amp_sensors(parsed, {}),
    'check_function'            : check_kentix_amp_sensors_humidity,
    'service_description'       : 'Humidity %s',
    'has_perfdata'              : True,
    'group'                     : 'humidity',
    'includes'                  : [ "humidity.include" ],
}

#.
#   .--smoke---------------------------------------------------------------.
#   |                                        _                             |
#   |                    ___ _ __ ___   ___ | | _____                      |
#   |                   / __| '_ ` _ \ / _ \| |/ / _ \                     |
#   |                   \__ \ | | | | | (_) |   <  __/                     |
#   |                   |___/_| |_| |_|\___/|_|\_\___|                     |
#   |                                                                      |
#   +----------------------------------------------------------------------+

kentix_amp_sensors_smoke_default_levels = (1, 5)


def check_kentix_amp_sensors_smoke(item, params, parsed):
    if item in parsed:
        sensor_smoke = parsed[item]['smoke']
        warn, crit = params

        if sensor_smoke >= crit:
            status = 2
        elif sensor_smoke >= warn:
            status = 1
        else:
            status = 0

        infotext = "%.1f%%" % sensor_smoke

        if status > 0:
            infotext += " (warn/crit at %.1f%%/%.1f%%)" % (warn, crit)

        perfdata = [ ('smoke_perc', sensor_smoke, warn, crit, 0, 100) ]

        yield status, infotext, perfdata


check_info['kentix_amp_sensors.smoke'] = {
    'inventory_function'        : lambda parsed: inventory_kentix_amp_sensors(parsed, "kentix_amp_sensors_smoke_default_levels"),
    'check_function'            : check_kentix_amp_sensors_smoke,
    'service_description'       : 'Smoke Detector %s',
    'has_perfdata'              : True,
    'group'                     : 'smoke',
}

#.
#   .--leakage-------------------------------------------------------------.
#   |                  _            _                                      |
#   |                 | | ___  __ _| | ____ _  __ _  ___                   |
#   |                 | |/ _ \/ _` | |/ / _` |/ _` |/ _ \                  |
#   |                 | |  __/ (_| |   < (_| | (_| |  __/                  |
#   |                 |_|\___|\__,_|_|\_\__,_|\__, |\___|                  |
#   |                                         |___/                        |
#   +----------------------------------------------------------------------+

def check_kentix_amp_sensors_leakage(item, params, parsed):
    if item in parsed:
        if parsed[item]['leakage'] > 0:
            return 2, "Alarm or disconnected"
        else:
            return 0, "Connected"


check_info['kentix_amp_sensors.leakage'] = {
    'inventory_function'        : lambda i: inventory_kentix_amp_sensors(i, None),
    'check_function'            : check_kentix_amp_sensors_leakage,
    'service_description'       : 'Leakage %s',
}

#.





