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

# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1170.1.1.1 220 --> Stulz-WIB8000-MIB::unitAirTemperature.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1170.1.2.1 216 --> Stulz-WIB8000-MIB::unitAirTemperature.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1175.1.1.1 220 --> Stulz-WIB8000-MIB::unitSetpointAirTratureCorrected.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1175.1.2.1 220 --> Stulz-WIB8000-MIB::unitSetpointAirTratureCorrected.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1192.1.1.1 221 --> Stulz-WIB8000-MIB::unitReturnAirTemperature.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1192.1.2.1 216 --> Stulz-WIB8000-MIB::unitReturnAirTemperature.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1193.1.1.1 220 --> Stulz-WIB8000-MIB::unitSupplyAirTemperature.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1193.1.2.1 214 --> Stulz-WIB8000-MIB::unitSupplyAirTemperature.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1.1196.1.2.1 83 --> Stulz-WIB8000-MIB::unitOutsideAirTemperature.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1171.1.1.1 418 --> Stulz-WIB8000-MIB::unitHumidity.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1171.1.2.1 420 --> Stulz-WIB8000-MIB::unitHumidity.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1178.1.1.1 500 --> Stulz-WIB8000-MIB::unitSetpointHumidityCorrected.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1178.1.2.1 500 --> Stulz-WIB8000-MIB::unitSetpointHumidityCorrected.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1194.1.1.1 418 --> Stulz-WIB8000-MIB::unitReturnAirHumidity.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1194.1.2.1 419 --> Stulz-WIB8000-MIB::unitReturnAirHumidity.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1195.1.1.1 421 --> Stulz-WIB8000-MIB::unitSupplyAirHumidity.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.2.1.1.1195.1.2.1 425 --> Stulz-WIB8000-MIB::unitSupplyAirHumidity.1.2.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.3.1.1.1208.1.1.1 11 --> Stulz-WIB8000-MIB::currentRaisedFloorPressure.1.1.1
# .1.3.6.1.4.1.29462.10.2.1.1.1.1.3.1.1.1208.1.2.1 16 --> Stulz-WIB8000-MIB::currentRaisedFloorPressure.1.2.1

factory_settings["stulz_temp_default_levels"] = {"levels": (25, 28)}


def parse_stulz_temp(info):
    map_types = {
        "1170": "unit air",
        "1192": "unit return air",
        "1193": "unit supply air",
        "1196": "unit outside air",
        "1243": "unit supply 3",
        "1244": "unit return air 2",
        "1245": "unit return air 3",
        "1246": "unit return air temrn",
        "1247": "unit return air temly",
        "1248": "unit supply air 2",
        "10210": "condensor",
        "10211": "supply 1",
        "10212": "supply 2",
        "10264": "FCB room air",
        "10266": "supply air comfort unit 1",
        "10267": "supply air comfort unit 2",
        "10268": "FCB outside air",
    }

    parsed = {}
    for oidend, reading_str in info:
        oids = oidend.split(".")
        temp_ty = oids[0]
        index = oids[2]
        if temp_ty in map_types and reading_str != "999":
            itemname = "%s-%s" % (map_types[temp_ty], index)
            parsed.setdefault(itemname, float(reading_str) / 10)

    return parsed


def inventory_stulz_temp(parsed):
    for item in parsed:
        yield item, {}


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


check_info["stulz_temp"] = {
    "parse_function": parse_stulz_temp,
    "inventory_function": inventory_stulz_temp,
    "check_function": check_stulz_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.29462.10",
    "snmp_info": (".1.3.6.1.4.1.29462.10.2.1.1.1.1.1.1.1", [
        OID_END,
        "",
    ]),
    "includes": ["temperature.include"],
    "default_levels_variable": "stulz_temp_default_levels"
}
