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


# snmp_scan_function
#.1.3.6.1.2.1.1.4.0 = STRING: x.name@green-cooling.de < green-cooling match
#.1.3.6.1.2.1.1.5.0 = STRING: pCOWeb                    < pcoweb match
#.1.3.6.1.4.1.9839.1                                    < exists

# snmp_info
#.1.3.6.1.4.1.9839.2.1.1.31.0 = INTEGER: 0   < Waterloss
#.1.3.6.1.4.1.9839.2.1.1.51.0 = INTEGER: 1   < Global
#.1.3.6.1.4.1.9839.2.1.1.67.0 = INTEGER: 0   < Unit in Emergeny operation
#.1.3.6.1.4.1.9839.2.1.2.6.0 = INTEGER:  246 < Humidifier: Relative Humidity


def inventory_carel_uniflair_cooling(info):
    return [ (None, None) ]


def check_carel_uniflair_cooling(item, _no_params, info):
    waterloss, global_status, ermergency_op, humidity = info[0]

    err_waterloss      = waterloss != "0"
    err_global_status  = global_status != "1"
    err_emergency_op   = ermergency_op != "0"
    humidity = float(humidity)/10

    output = ""
    output = output + ( "Global Status:%s" % (err_global_status and "(!!)Error, " or "OK, " ))
    output = output + ( "Emergency Operation:%s" % (err_emergency_op and "(!!)Active, " or "Inactive, "))
    output = output + ( "Humidifier:%s" % (err_waterloss and "(!!)Water Loss, " or "No Water Loss, " ))
    output = output + "Humidity: %3.1f%%" % humidity


    perfdata = [ ("humidity", humidity) ]
    if err_waterloss or err_global_status or err_emergency_op:
        return (2, output, perfdata)
    else:
        return (0, output, perfdata)

    return (3, "Unknown data from agent", perfdata)


check_info["carel_uniflair_cooling"]  = {
    "check_function"       : check_carel_uniflair_cooling,
    "inventory_function"   : inventory_carel_uniflair_cooling,
    "service_description"  : "Carel uniflair cooling",
    "has_perfdata"         : True,
    "snmp_scan_function"   : lambda oid: "green-cooling" in oid(".1.3.6.1.2.1.1.4.0").lower() \
                             and "pcoweb" in oid(".1.3.6.1.2.1.1.5.0").lower() \
                             and oid(".1.3.6.1.4.1.9839.1.2.0"),
    "snmp_info"            : (".1.3.6.1.4.1.9839.2.1",  [
                               "1.31.0",    # Waterloss
                               "1.51.0",    # Global
                               "1.67.0",    # Unit in Emergency operation
                               "2.6.0",     # Relative Humidity
                               ]),
}
