#!/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.9.9.117.1.1.1.1.2.16  "centiAmpsAt12V"
#  some more examples (but we dont know all):
#       milliAmps12v
#       centiAmpsAt12V
#       Amps @ 12V
#       CentiAmps @ 12V
#       Amps @ 50
#   => calculate power = factor * amps * volt

# .1.3.6.1.4.1.9.9.117.1.1.4.1.1.16 11333
# .1.3.6.1.4.1.9.9.117.1.1.4.1.2.16 9666
# .1.3.6.1.4.1.9.9.117.1.1.4.1.3.16 6000
# .1.3.6.1.4.1.9.9.117.1.1.4.1.4.16 122

# .1.3.6.1.4.1.9.9.117.1.1.4.1.1.13 11333
# .1.3.6.1.4.1.9.9.117.1.1.4.1.2.13 5583
# .1.3.6.1.4.1.9.9.117.1.1.4.1.3.13 6980
# .1.3.6.1.4.1.9.9.117.1.1.4.1.4.13 0       <= exclude


def parse_cisco_fru_powerusage(info):
    parsed = {}
    powerunit, powervals = info
    if powerunit and powervals:
        oidend, powerunit_str = powerunit[0]
        factor_str, voltage_str = powerunit_str.lower().split("amps")

        if "milli" in factor_str.lower():
            factor = 0.001
        elif "centi" in factor_str.lower():
            factor = 0.01
        else:
            factor = 1.0

        voltage = float(voltage_str.lower().replace("at", "").\
                        replace("@", "").replace("v", "").strip())

        if oidend == powervals[0][0]:
            system_total, system_drawn, inline_total, inline_drawn = map(float, powervals[0][1:])
            for what, val in [
                ("system total", system_total),  # Gesamtstrom
                ("system drawn", system_drawn),  # aufgenommene Gesamtstromstaerke
                ("inline total", inline_total),
                ("inline drawn", inline_drawn)
            ]:
                parsed.setdefault(what, {
                    "power": factor * val * voltage,
                    "current": factor * val,
                    "voltage": voltage,
                })

    return parsed


def inventory_cisco_fru_powerusage(parsed):
    for what, data in parsed.items():
        if data["current"] > 0:
            yield what, {}


check_info['cisco_fru_powerusage'] = {
    'parse_function': parse_cisco_fru_powerusage,
    'inventory_function': inventory_cisco_fru_powerusage,
    'check_function': check_elphase,
    'service_description': 'FRU power usage %s',
    'has_perfdata': True,
    'snmp_info': [
        (
            ".1.3.6.1.4.1.9.9.117.1.1.1.1.2",
            [
                OID_END,
                "",  # CISCO-ENTITY-FRU-CONTROL-MIB::cefcPowerUnits
            ]),
        (
            ".1.3.6.1.4.1.9.9.117.1.1.4.1",
            [
                OID_END,
                "1",  # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUTotalSystemCurrent
                "2",  # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUDrawnSystemCurrent
                "3",  # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUTotalInlineCurrent
                "4",  # CISCO-ENTITY-FRU-CONTROL-MIB::cefcFRUDrawnInlineCurrent
            ])
    ],
    'snmp_scan_function': lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    'group': 'el_inphase'
}
