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

# OID branch 3 means the sensor unit type (from SENSOR-MIB):
# other(1)
# truthvalue(2)
# specialEnum(3)
# volts(4)
# celsius(5)
# rpm(6)

def inventory_bluecoat_sensors(info, temp):
    for name, reading, status, scale, unit in info:
        # temperature sensor is unit 5.
        if temp == (unit == '5'):
            yield name.replace(" temperature", ""), {}


def check_bluecoat_sensors(item, params, info, temp):
    for name, reading, status, scale, unit in info:
        # if the service was discovered before the update,
        # the temp-subcheck will not have discovered anything as it didn't
        # exist before 1.2.8. Otoh the non-temperature check will include
        # temperature sensors. For those, the item name will is not manipulated
        if temp:
            name = name.replace(" temperature", "")
        if name == item:
            value = float(reading) * 10 ** float(scale)
            if temp and unit == '5':
                return check_temperature(value, params,
                                         "bluecoat_sensors_%s" % item, status != '1' and 2 or 0)
            elif unit == '4':
                varname = "voltage"
                unitname = " V"
                perfdata = [ ("voltage", value) ]
            else:
                perfdata = None
                unitname = ""

            if status == '1':
                state = 0
            else:
                state = 2

            return state, "%.1f%s" % (value, unitname), perfdata


check_info["bluecoat_sensors"] = {
    'check_function'      : lambda item, params, info: check_bluecoat_sensors(item, params, info, False),
    'inventory_function'  : lambda info: inventory_bluecoat_sensors(info, False),
    'service_description' : '%s',
    'has_perfdata'        : True,
    'snmp_info'           : ('.1.3.6.1.4.1.3417.2.1.1.1.1.1', [9, 5, 7, 4, 3]),
    'snmp_scan_function'  : lambda oid: '1.3.6.1.4.1.3417.1.1' in oid(".1.3.6.1.2.1.1.2.0"),
}


check_info["bluecoat_sensors.temp"] = {
    'check_function'      : lambda item, params, info: check_bluecoat_sensors(item, params, info, True),
    'inventory_function'  : lambda info: inventory_bluecoat_sensors(info, True),
    'service_description' : 'Temperature %s',
    'has_perfdata'        : True,
    'includes'            : [ "temperature.include" ],
    'group'               : "temperature",
}

