#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.elphase import *  # pylint: disable=wildcard-import,unused-wildcard-import
# .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',
}
