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

# .1.3.6.1.4.1.28507.38.1.3.1.2.1.2.1 TWTA 2 --> GUDEADS-EPC822X-MIB::epc822XPortName.1
# .1.3.6.1.4.1.28507.38.1.3.1.2.1.3.1 0 --> GUDEADS-EPC822X-MIB::epc822XPortState.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.4.1 0 --> GUDEADS-EPC822X-MIB::epc822XPowerActive.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.5.1 0 --> GUDEADS-EPC822X-MIB::epc822XCurrent.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.6.1 228 --> GUDEADS-EPC822X-MIB::epc822XVoltage.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.7.1 4995 --> GUDEADS-EPC822X-MIB::epc822XFrequency.1
# .1.3.6.1.4.1.28507.38.1.5.1.2.1.10.1 0 --> GUDEADS-EPC822X-MIB::epc822XPowerApparent.1

factory_settings["gude_relayport_default_levels"] = {
    "voltage": (220, 210),
    "current": (15, 16),
}


def parse_gude_relayport(info):
    parsed = {}
    for portname, portstate, active_power_str, \
        current_str, volt_str, freq_str, appower_str in info:

        parsed.setdefault(portname, {
            "device_state": {
                "0": (2, "off"),
                "1": (0, "on")
            }[portstate],
        })

        for what, key, factor in [
            (active_power_str, "power", 1.0),
            (current_str, "current", 0.001),
            (volt_str, "voltage", 1.0),
            (freq_str, "frequency", 0.01),
            (appower_str, "appower", 1.0),
        ]:
            parsed[portname][key] = float(what) * factor

    return parsed


check_info['gude_relayport'] = {
    'parse_function': parse_gude_relayport,
    'inventory_function': inventory_elphase,
    'check_function': check_elphase,
    'service_description': 'Relay port %s',
    'has_perfdata': True,
    'snmp_info': (
        '.1.3.6.1.4.1.28507.38',
        [
            "1.3.1.2.1.2",  # GUDEADS-EPC822X-MIB::epc822XPortName
            "1.3.1.2.1.3",  # GUDEADS-EPC822X-MIB::epc822XPortState
            "1.5.5.2.1.4",  # GUDEADS-EPC822X-MIB::epc822XPowerActive.1
            "1.5.5.2.1.5",  # GUDEADS-EPC822X-MIB::epc822XCurrent.1
            "1.5.5.2.1.6",  # GUDEADS-EPC822X-MIB::epc822XVoltage.1
            "1.5.5.2.1.7",  # GUDEADS-EPC822X-MIB::epc822XFrequency.1
            "1.5.5.2.1.10",  # GUDEADS-EPC822X-MIB::epc822XPowerApparent.1
        ]),
    'snmp_scan_function': lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.28507.38"),
    'default_levels_variable': 'gude_relayport_default_levels',
    'group': 'el_inphase',
    'includes': ['elphase.include'],
}
