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

def bintec_sensors_scan(oid):
    return oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.272.4")

bintec_sensors_info = ( ".1.3.6.1.4.1.272.4.17.7.1.1.1", [
                             2, # sensorNumber
                             3, # sensorDescr
                             4, # sensorType
                             5, # sensorValue
                             7, # sensorMeasurementUnit
                      ])

#   .--fans----------------------------------------------------------------.
#   |                          __                                          |
#   |                         / _| __ _ _ __  ___                          |
#   |                        | |_ / _` | '_ \/ __|                         |
#   |                        |  _| (_| | | | \__ \                         |
#   |                        |_|  \__,_|_| |_|___/                         |
#   |                                                                      |
#   '----------------------------------------------------------------------'

factory_settings["bintec_sensors_fan_default_levels"] = {
    "lower" : (2000, 1000),
}

def inventory_bintec_sensors_fan(info):
    inventory = []
    for sensor_id, sensor_descr, sensor_type, sensor_value, sensor_unit in info:
        if sensor_type == "2":
            inventory.append( (sensor_descr, {}) )
    return inventory

def check_bintec_sensors_fan(item, params, info):
    for sensor_id, sensor_descr, sensor_type, sensor_value, sensor_unit in info:
        if sensor_descr == item:
            return check_fan(int(sensor_value), params)

check_info["bintec_sensors.fan"] = {
    "check_function"             : check_bintec_sensors_fan,
    "inventory_function"         : inventory_bintec_sensors_fan,
    "service_description"        : "%s",
    "snmp_info"                  : bintec_sensors_info,
    "snmp_scan_function"         : bintec_sensors_scan,
    "default_levels_variable"    : "bintec_sensors_fan_default_levels",
    "group"                      : "hw_fans",
    "includes"                   : [ "fan.include" ],
}

#.
#   .--temp----------------------------------------------------------------.
#   |                       _                                              |
#   |                      | |_ ___ _ __ ___  _ __                         |
#   |                      | __/ _ \ '_ ` _ \| '_ \                        |
#   |                      | ||  __/ | | | | | |_) |                       |
#   |                       \__\___|_| |_| |_| .__/                        |
#   |                                        |_|                           |
#   '----------------------------------------------------------------------'

factory_settings["bintec_sensors_temp_default_levels"] = {
    "levels" : (35, 40)
}


def inventory_bintec_sensors_temp(info):
    for sensor_id, sensor_descr, sensor_type, sensor_value, sensor_unit in info:
        if sensor_type == "1":
            yield sensor_descr, {}


def check_bintec_sensors_temp(item, params, info):
    for sensor_id, sensor_descr, sensor_type, sensor_value, sensor_unit in info:
        if sensor_descr == item:
            return check_temperature(int(sensor_value), params, "bintec_sensors_%s" % item)

    return 3, "Sensor not found in SNMP data"


check_info["bintec_sensors.temp"] = {
    "check_function"          : check_bintec_sensors_temp,
    "inventory_function"      : inventory_bintec_sensors_temp,
    "service_description"     : "Temperature %s",
    "group"                   : "temperature",
    "has_perfdata"            : True,
    "snmp_info"               : bintec_sensors_info,
    "snmp_scan_function"      : bintec_sensors_scan,
    "includes"                : [ "temperature.include" ],
    "default_levels_variable" : "bintec_sensors_temp_default_levels"
}

#.
#   .--voltage-------------------------------------------------------------.
#   |                             _ _                                      |
#   |                 __   _____ | | |_ __ _  __ _  ___                    |
#   |                 \ \ / / _ \| | __/ _` |/ _` |/ _ \                   |
#   |                  \ V / (_) | | || (_| | (_| |  __/                   |
#   |                   \_/ \___/|_|\__\__,_|\__, |\___|                   |
#   |                                        |___/                         |
#   '----------------------------------------------------------------------'

def inventory_bintec_sensors_voltage(info):
    inventory = []
    for sensor_id, sensor_descr, sensor_type, sensor_value, sensor_unit in info:
        if sensor_type == "3":
            inventory.append( (sensor_descr, None ) )
    return inventory

def check_bintec_sensors_voltage(item, _no_params, info):
    for sensor_id, sensor_descr, sensor_type, sensor_value, sensor_unit in info:
        if sensor_descr == item:
            sensor_value = int(sensor_value) / 1000.0

            message = "%s is at %s V" % (sensor_descr, sensor_value)
            perfdata = [ ("voltage", str(sensor_value)+"V") ]

            return 0, message, perfdata

    return 3, "Sensor %s not found" % item

check_info["bintec_sensors.voltage"] = {
    "check_function"        : check_bintec_sensors_voltage,
    "inventory_function"    : inventory_bintec_sensors_voltage,
    "service_description"   : "Voltage %s",
    "has_perfdata"          : True,
    "snmp_info"             : bintec_sensors_info,
    "snmp_scan_function"    : bintec_sensors_scan,
}

#.
