#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             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.

factory_settings["clever_pdu_default_levels"] ={
    "voltage": (240, 250),
    "current": (32, 33),
    "energy": (35000, 36000),
    }

lines = {"Line 1", "Line 2", "Line 3"}
_UNIT_MAP = {
    "voltage": "V" ,
    "current": "A" ,
    "energy": "W",
}

def parse_clever_pdu_120(info):
    data=info[0]
    parsed = {}
    parsed = {
        "Line 1" : {
            "voltage": float(data[0]),
            "current": float(data[3])/10,
            "energy": float(data[6]),
        },
        "Line 2" : {
            "voltage": float(data[1]),
            "current": float(data[4])/10,
            "energy": float(data[7]),
        },        
        "Line 3" : {
            "voltage": float(data[2]),
            "current": float(data[5])/10,
            "energy": float(data[8]),
        },
        "Total Energy" : {
            "energy" : float((float(data[0])*float(data[3])/10)) + float((float(data[1])*float(data[4])/10)) + float((float(data[2])*float(data[5])/10)),
        },
                   
    }
    return parsed

def inventory_clever_pdu_120(parsed):
    for line in parsed:
        yield line, {}


def check_clever_pdu_120(item, params, parsed):
    if "Total" not in item:
        for param in params:
            levels_lower = levels_upper = None
            warn, crit = params.get(param)
            if warn > crit:
                levels_lower = warn, crit
            else:
                levels_upper = warn, crit

            yield check_levels(
                parsed.get(item).get(param),
                param,
                (warn, crit),
                unit = _UNIT_MAP.get(param),
                infoname = param
            )
    else:
        for param in params:
            if "energy" in param:
                levels_lower = levels_upper = None
                warn, crit = params.get(param)
                if warn > crit:
                    levels_lower = warn, crit
                else:
                    levels_upper = warn, crit
                yield check_levels(
                    parsed.get(item).get(param),
                    param,
                    (warn, crit),
                    unit = _UNIT_MAP.get(param),
                    infoname = param
            )



check_info['clever_pdu_120'] = {
    'parse_function'      : parse_clever_pdu_120,
    'inventory_function'  : inventory_clever_pdu_120,
    'check_function'      : check_clever_pdu_120,
    'service_description' : '%s',
    'has_perfdata'        : True,
    'group'               : "clever_pdu",
    'snmp_info'           : ('.1.3.6.1.4.1.30966.10.3.2', 
			      [  
           			'1',
				'2',
				'3',
				'4',
				'5',
				'6',
				'7',
				'8',
				'9',
			      ],
			    ),
    'snmp_scan_function'  : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.30966") and oid(".1.3.6.1.4.1.30966.10.3.2.70.0"),
    'default_levels_variable' : 'clever_pdu_default_levels',
}
