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

# The etherbox supports the following sensor types on each port
# sensor types
# 0 = no sensor
# 1 = temperature      - implemented
# 2 = brightness
# 3 = humidity         - implemented
# 4 = switch contact   - implemented
# 5 = voltage detector
# 6 = smoke sensor     - implemented

# Note: The short contact config option in the etherbox is of type switch contact
#       The short contact status is set for 15 seconds after a button press

# TODO: insert missing snmp output

def etherbox_convert(info):
    sensor_data = []
    for i in range(0, len(info[1])):
        sensor_data.append((info[1][i][1], info[2][i][1], info[3][i][1], info[4][i][1] ))
    return sensor_data

def inventory_etherbox(info, req_sensor_type):
    sensor_data = etherbox_convert(info)
    for index, name, sensor_type, value in sensor_data:
        # Ignore not connected Temperature Sensors
        if sensor_type == '1' and value == '0':
            continue
        if sensor_type == req_sensor_type:
            yield "%s.%s" % (index, sensor_type), None

def etherbox_get_sensor(item, item_type, info):
    sensor_data = etherbox_convert(info)
    item_index, item_type = item.split(".")
    for index, name, sensor_type, value in sensor_data:
        if index == item_index:
            if sensor_type != item_type:
                raise Exception("Sensor type changed %s" % item)
            return name, value
    raise Exception("Sensor not found")

def etherbox_scan(oid):
    # Older firmware version of Etherbox do not answer on
    # .1.3.6.1.2.1. (sysDescr). Yurks. We need to fetch
    # a vendor specific OID here and wait until all old devices
    # have vanished.
    return oid(".1.3.6.1.4.1.14848.2.1.1.1.0", "").startswith("Version")

etherbox_info = [
                 ( ".1.3.6.1.4.1.14848.2.1.1.3", [ '' ]), # temperature unit
                 ( ".1.3.6.1.4.1.14848.2.1.2.1.1", [ OID_END, '' ]), # index
                 ( ".1.3.6.1.4.1.14848.2.1.2.1.2", [ OID_END, '' ]), # name
                 ( ".1.3.6.1.4.1.14848.2.1.2.1.3", [ OID_END, '' ]), # type
                 ( ".1.3.6.1.4.1.14848.2.1.2.1.5", [ OID_END, '' ]), # value * 10
                ]

def check_etherbox_temp(item, params, info):
    try:
        name, value = etherbox_get_sensor(item, "1", info)
    except Exception, error:
        return 3, error.message

    uom = { "0": "c", "1": "f", "2": "k" }[info[0][0][0]]
    temp = int(value) / 10.0
    return check_temperature(temp, params, "etherbox_temp_%s" % item, uom)


check_info["etherbox.temp"] = {
    "check_function"        : check_etherbox_temp,
    "inventory_function"    : lambda info: inventory_etherbox(info, "1"),
    "service_description"   : "Temperature %s",
    "has_perfdata"          : True,
    "group"                 : "temperature",
    "snmp_scan_function"    : etherbox_scan,
    "snmp_info"             : etherbox_info,
    "includes"              : [ "temperature.include" ],
}

def check_etherbox_humidity(item, params, info):
    try:
        name, value = etherbox_get_sensor(item, "3", info)
    except Exception, error:
        return 3, error.message

    state = 0
    temp = int(value) / 10.0
    levels_info = ""
    if params:
        crit_low, warn_low, warn_high, crit_high = params
        levels_info = " (warn/crit below %.1f/%.1f %%RH / warn/crit at %.1f/%.1f %%RH )" %\
                        (crit_low, warn_low, warn_high, crit_high)
        if temp <= crit_low:
            state = 2
        elif temp >= crit_high:
            state = 2
        elif temp <= warn_low:
            state = 1
        elif temp >= warn_high:
            state = 1
    else:
        crit_low, warn_low, warn_high, crit_high = None, None, None, None

    perfdata = [ ("humidity", temp, crit_low, warn_low, warn_high, crit_high) ]
    infotext = "%s: Humidity %.1f %%RH %s" % (name, temp, levels_info)
    return (state, infotext, perfdata)

check_info["etherbox.humidity"] = {
    "check_function"        : check_etherbox_humidity,
    "inventory_function"    : lambda x: inventory_etherbox(x, "3"),
    "service_description"   : "Sensor %s",
    "has_perfdata"          : True,
    "group"                 : "humidity",
    "snmp_scan_function"    : etherbox_scan,
    "snmp_info"             : etherbox_info,
}


def check_etherbox_switch_contact(item, params, info):
    try:
        name, value = etherbox_get_sensor(item, "4", info)
    except Exception, error:
        return 3, error.message

    state = 0
    perfdata = [ ("switch_contact", value) ]
    switch_state = value == "1000" and "open" or "closed"

    state = 0
    extra_info = ""
    if params and params != "ignore":
        if switch_state != params:
            state = 2
            extra_info = ", should be %s" % params

    infotext = "%s: Switch contact %s%s" % (name, switch_state, extra_info)
    return (state, infotext, perfdata)

check_info["etherbox.switch"] = {
    "check_function"        : check_etherbox_switch_contact,
    "inventory_function"    : lambda x: inventory_etherbox(x, "4"),
    "service_description"   : "Sensor %s",
    "group"                 : "switch_contact",
    "has_perfdata"          : True,
    "snmp_scan_function"    : etherbox_scan,
    "snmp_info"             : etherbox_info,
}


def check_etherbox_smoke(item, no_params, info):
    try:
        name, value = etherbox_get_sensor(item, "6", info)
    except Exception, error:
        return 3, error.message

    state = 0
    perfdata = [ ("smoke", value) ]
    extra_info = ""
    if value != "0":
        extra_info = " - Smoke alarm!"
        state = 2

    return (state, "%s: Smoke sensor%s" % (name, extra_info), perfdata)

check_info["etherbox.smoke"] = {
    "check_function"        : check_etherbox_smoke,
    "inventory_function"    : lambda x: inventory_etherbox(x, "6"),
    "service_description"   : "Sensor %s",
    "has_perfdata"          : True,
    "snmp_scan_function"    : etherbox_scan,
    "snmp_info"             : etherbox_info,
}

