#!/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.14848.2.1.7.1.2.1 -0.0008 Volt --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.1
# .1.3.6.1.4.1.14848.2.1.7.1.2.2 -0.0008 Volt --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.2
# .1.3.6.1.4.1.14848.2.1.7.1.2.3 5.0015 Volt  --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.3
# .1.3.6.1.4.1.14848.2.1.7.1.2.4 2.0031 Volt  --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.4
# .1.3.6.1.4.1.14848.2.1.7.1.2.5 -0.0005 Volt --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.5
# .1.3.6.1.4.1.14848.2.1.7.1.2.6 -0.0004 Volt --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.6
# .1.3.6.1.4.1.14848.2.1.7.1.2.7 5.0002 Volt  --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.7
# .1.3.6.1.4.1.14848.2.1.7.1.2.8 2.0010 Volt  --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.7.1.2.8

# .1.3.6.1.4.1.14848.2.1.9.1.2.1 -2472        --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.9.1.2.1
# .1.3.6.1.4.1.14848.2.1.9.1.2.2 252          --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.9.1.2.2
# .1.3.6.1.4.1.14848.2.1.9.1.2.3 0            --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.9.1.2.3
# .1.3.6.1.4.1.14848.2.1.9.1.2.4 248          --> BETTER-NETWORKS-ETHERNETBOX-MIB::ethernetboxObjects.9.1.2.4


# suggested by customer
factory_settings['etherbox2_temp_default_levels'] = {
    'levels'       : (30, 35),
}


def parse_etherbox2_temp(info):
    # We have to use xxx.7.1.2.a to know if a temperature sensor
    # is connected:
    # - if oid(xxx.7.1.2.{a}) == 5.fff and oid(xxx.7.1.2.{a+1}) == 2.fff
    #   then a temperature sensor is connected to oid(xxx.9.1.2.{(a+1)/2})
    #   (a = 1, 3, 5, ...)
    # - otherwise there's no sensor connected.
    # Furthermore we cannot only use xxx.9.1.2.{a} < 0 (or something like that)
    # because the temperature can drop below 0.
    parsed = {}
    sensor_indicators, sensors = info
    for sensor_index in range(0, len(sensors)):
        indicator_index = 2 * sensor_index
        if float((sensor_indicators[indicator_index][0].split("Volt")[0]).strip()) > 4 and \
           float((sensor_indicators[indicator_index + 1][0].split("Volt")[0]).strip()) > 1:
            parsed["Sensor %s" % sensors[sensor_index][0]] = float(sensors[sensor_index][1]) / 10

    return parsed


def inventory_etherbox2_temp(parsed):
    return [ (sensor, {}) for sensor in parsed ]


def check_etherbox2_temp(item, params, parsed):
    if item in parsed:
        return check_temperature(parsed[item], params, "etherbox2_%s" % item)


check_info['etherbox2_temp'] = {
    'parse_function'            : parse_etherbox2_temp,
    'inventory_function'        : inventory_etherbox2_temp,
    'check_function'            : check_etherbox2_temp,
    'service_description'       : 'Temperature %s',
    'has_perfdata'              : True,
    'snmp_info'                 : [(".1.3.6.1.4.1.14848.2.1.7.1.2", [ "" ]),
                                   (".1.3.6.1.4.1.14848.2.1.9.1.2", [ OID_END, "" ])],
                                  # From firmware version 1.21 on the ethernetbox 2 from MessPC
                                  # supports additional temperature sensors. The sensor data are
                                  # located under these OIDs. There are no standard OIDs for
                                  # disk space reasons.
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.2.1.1.1.0") == "" and \
                                              "Version 1.2" in oid(".1.3.6.1.4.1.14848.2.1.1.1.0", ""),
    'default_levels_variable'   : 'etherbox2_temp_default_levels',
    'group'                     : 'temperature',
    'includes'                  : [ "temperature.include" ],
}
