#!/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.1718.3.2.3.1.2.1.1.1 AA1
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.2 AA2
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.3 AA3
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.4 AA4
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.5 AA5
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.6 AA6
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.7 AA7
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.8 AA8
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.1 TowerA_InfeedA_Outlet1
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.2 PRZ_FAB01_SW1_li
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.3 TowerA_InfeedA_Outlet3
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.4 TowerA_InfeedA_Outlet4
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.5 TowerA_InfeedA_Outlet5
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.6 1643_AMS_Telekom_Umsetz.
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.7 TowerA_InfeedA_Outlet7
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.8 S2M_Optokoppler
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.1 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.2 1
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.3 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.4 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.5 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.6 1
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.7 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.8 1

# parsed = {
#  'AA1 TowerA_InfeedA_1'        : 0,
#  'AA2 PRZ_FAB01_SW1_li'        : 1,
#  'AA3 TowerA_InfeedA_3'        : 0,
#  'AA4 TowerA_InfeedA_4'        : 0,
#  'AA5 TowerA_InfeedA_5'        : 0,
#  'AA6 1643_AMS_Telekom_Umsetz.': 1,
#  'AA7 TowerA_InfeedA_7'        : 0,
#  'AA8 S2M_Optokoppler'         : 1,
# }


def parse_sentry_pdu_outlets(info):
    parsed = {}
    for outlet_id, outlet_name, outlet_state_str in info:
        outlet_name = outlet_name.replace("Outlet", "")
        outlet_id_name = "%s %s" % (outlet_id, outlet_name)
        parsed[outlet_id_name] = int(outlet_state_str)
    return parsed


def inventory_sentry_pdu_outlets(parsed):
    return [(key, None) for key in parsed]


def check_sentry_pdu_outlets(item, _no_params, parsed):
    outlet_states = {
        0: (0, "off"),
        1: (0, "on"),
        2: (1, "off wait"),
        3: (1, "on wait"),
        4: (2, "off error"),
        5: (2, "on error"),
        6: (2, "no comm"),
        7: (2, "reading"),
        8: (2, "off fuse"),
        9: (2, "on fuse"),
    }

    if item in parsed:
        outlet_state = parsed[item]

        if outlet_state in outlet_states:
            return outlet_states[outlet_state][0], "Status: %s" % outlet_states[outlet_state][1]
        return 3, "Unhandled state: %d" % outlet_state


check_info["sentry_pdu_outlets"] = {
    "parse_function": parse_sentry_pdu_outlets,
    "inventory_function": inventory_sentry_pdu_outlets,
    "check_function": check_sentry_pdu_outlets,
    "service_description": "Outlet %s",
    "snmp_info": (".1.3.6.1.4.1.1718.3.2.3.1", ["2", "3", "5"]),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.1718.3",
}
