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

# we use raritan.include but here we have no infomation
# about availability of the sensors
# .1.3.6.1.4.1.13742.4.3.3.1.1.4 4 --> PDU-MIB::sensorID.4
# .1.3.6.1.4.1.13742.4.3.3.1.1.5 5 --> PDU-MIB::sensorID.5
# .1.3.6.1.4.1.13742.4.3.3.1.1.13 13 --> PDU-MIB::sensorID.13
# .1.3.6.1.4.1.13742.4.3.3.1.4.4 Temperature AEA1160298 --> PDU-MIB::externalSensorName.4
# .1.3.6.1.4.1.13742.4.3.3.1.4.5 itbb-pwr15humi5 --> PDU-MIB::externalSensorName.5
# .1.3.6.1.4.1.13742.4.3.3.1.4.13 Temperature AEH0850504 --> PDU-MIB::externalSensorName.13
# .1.3.6.1.4.1.13742.4.3.3.1.2.4 10 --> PDU-MIB::externalSensorType.4
# .1.3.6.1.4.1.13742.4.3.3.1.2.5 11 --> PDU-MIB::externalSensorType.5
# .1.3.6.1.4.1.13742.4.3.3.1.2.13 10 --> PDU-MIB::externalSensorType.13
# .1.3.6.1.4.1.13742.4.3.3.1.40.4 -1 --> PDU-MIB::externalSensorState.4
# .1.3.6.1.4.1.13742.4.3.3.1.40.5 -1 --> PDU-MIB::externalSensorState.5
# .1.3.6.1.4.1.13742.4.3.3.1.40.13 4 --> PDU-MIB::externalSensorState.13
# .1.3.6.1.4.1.13742.4.3.3.1.16.4 -1 --> PDU-MIB::externalSensorUnits.4
# .1.3.6.1.4.1.13742.4.3.3.1.16.5 -1 --> PDU-MIB::externalSensorUnits.5
# .1.3.6.1.4.1.13742.4.3.3.1.16.13 7 --> PDU-MIB::externalSensorUnits.13
# .1.3.6.1.4.1.13742.4.3.3.1.17.4 1 --> PDU-MIB::externalSensorDecimalDigits.4
# .1.3.6.1.4.1.13742.4.3.3.1.17.5 0 --> PDU-MIB::externalSensorDecimalDigits.5
# .1.3.6.1.4.1.13742.4.3.3.1.17.13 1 --> PDU-MIB::externalSensorDecimalDigits.13
# .1.3.6.1.4.1.13742.4.3.3.1.41.4 0 --> PDU-MIB::externalSensorValue.4
# .1.3.6.1.4.1.13742.4.3.3.1.41.5 0 --> PDU-MIB::externalSensorValue.5
# .1.3.6.1.4.1.13742.4.3.3.1.41.13 215 --> PDU-MIB::externalSensorValue.13
# .1.3.6.1.4.1.13742.4.3.3.1.31.4 -1 --> PDU-MIB::externalSensorLowerCriticalThreshold.4
# .1.3.6.1.4.1.13742.4.3.3.1.31.5 -1 --> PDU-MIB::externalSensorLowerCriticalThreshold.5
# .1.3.6.1.4.1.13742.4.3.3.1.31.13 180 --> PDU-MIB::externalSensorLowerCriticalThreshold.13
# .1.3.6.1.4.1.13742.4.3.3.1.32.4 -1 --> PDU-MIB::externalSensorLowerWarningThreshold.4
# .1.3.6.1.4.1.13742.4.3.3.1.32.5 -1 --> PDU-MIB::externalSensorLowerWarningThreshold.5
# .1.3.6.1.4.1.13742.4.3.3.1.32.13 200 --> PDU-MIB::externalSensorLowerWarningThreshold.13
# .1.3.6.1.4.1.13742.4.3.3.1.33.4 -1 --> PDU-MIB::externalSensorUpperCriticalThreshold.4
# .1.3.6.1.4.1.13742.4.3.3.1.33.5 -1 --> PDU-MIB::externalSensorUpperCriticalThreshold.5
# .1.3.6.1.4.1.13742.4.3.3.1.33.13 600 --> PDU-MIB::externalSensorUpperCriticalThreshold.13
# .1.3.6.1.4.1.13742.4.3.3.1.34.4 -1 --> PDU-MIB::externalSensorUpperWarningThreshold.4
# .1.3.6.1.4.1.13742.4.3.3.1.34.5 -1 --> PDU-MIB::externalSensorUpperWarningThreshold.5
# .1.3.6.1.4.1.13742.4.3.3.1.34.13 550 --> PDU-MIB::externalSensorUpperWarningThreshold.13


def parse_raritan_px_sensors(info):
    pre_parsed = []
    for sensor_id, sensor_name, sensor_type, sensor_state, sensor_unit, \
        sensor_factor, sensor_value_str, sensor_lower_crit_str, \
        sensor_lower_warn_str, sensor_upper_crit_str, sensor_upper_warn_str in info:

        sensor_name =  sensor_name.replace("Temperature", "") \
                                  .replace("Humidity", "") \
                                  .replace("On/Off", "").strip()

        # These raritan devices use the PDU-MIB and have no 'isAvailable' information as used
        # in the 'raritan.include' parse function, thus we take the sensor state instead
        if sensor_state == "-1":
            availability = "0"
        else:
            availability = "1"

        pre_parsed.append(
            [availability, sensor_id, sensor_name, sensor_type, sensor_state, sensor_unit,
             sensor_factor, sensor_value_str, sensor_lower_crit_str, sensor_lower_warn_str,
             sensor_upper_crit_str, sensor_upper_warn_str]
        )

    return parse_raritan_sensors(pre_parsed)


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


check_info['raritan_px_sensors'] = {
    'parse_function'            : parse_raritan_px_sensors,
    'inventory_function'        : lambda parsed: inventory_raritan_sensors_temp(parsed, "temp"),
    'check_function'            : check_raritan_sensors_temp,
    'service_description'       : 'Temperature %s',
    'has_perfdata'              : True,
    'snmp_info'                 : (".1.3.6.1.4.1.13742.4.3.3.1", [
                                        "1",    # sensorID
                                        "4",    # sensorName
                                        "2",    # sensorType
                                        "40",   # sensorState
                                        "16",   # sensorUnit
                                        "17",   # sensorDecimalDigits
                                        "41",   # sensorValue
                                        "31",   # sensorLowerCrit
                                        "32",   # sensorLowerWarn
                                        "33",   # sensorUpperCrit
                                        "34",   # sensorUpperWarn
                                  ]),
    'snmp_scan_function'        : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.13742.4",
    'includes'                  : [ "raritan.include" ],
}


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


check_info["raritan_px_sensors.humidity"] = {
    'inventory_function'        : lambda parsed: inventory_raritan_sensors(parsed, "humidity"),
    'check_function'            : check_raritan_sensors,
    'service_description'       : 'Humidity %s',
    'has_perfdata'              : True,
}


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


check_info["raritan_px_sensors.binary"] = {
    'inventory_function'        : lambda parsed: inventory_raritan_sensors(parsed, "binary"),
    'check_function'            : check_raritan_sensors_binary,
    'service_description'       : 'Contact %s',
}

