#!/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.huawei_switch import *  # pylint: disable=wildcard-import,unused-wildcard-import
huawei_switch_hw_oper_state_map = {
    '1': 'notSupported',
    '2': 'disabled',
    '3': 'enabled',
    '4': 'offline',
}


def parse_huawei_switch_psu(info):
    return parse_huawei_physical_entity_values(info, "power card")


@get_parsed_item_data
def check_huawei_switch_psu(item, params, item_data):
    if item_data.value is None:
        return

    # Only 'enabled' is OK, everything else is considered CRIT
    status = 0 if item_data.value == "3" else 2
    status_text = huawei_switch_hw_oper_state_map.get(item_data.value,
                                                      "unknown (%s)" % item_data.value)
    return status, "State: %s" % status_text


check_info['huawei_switch_psu'] = {
    'parse_function': parse_huawei_switch_psu,
    'inventory_function': discover(),
    'check_function': check_huawei_switch_psu,
    'service_description': 'Powersupply %s',
    'snmp_info': huawei_entity_specific_snmp_info((
        '.1.3.6.1.4.1.2011.5.25.31.1.1.1.1',
        [OID_END, "2"],  # hwEntityOperStatus
    )),
    'snmp_scan_function': huawei_switch_scan_function,
    'has_perfdata': True,
}
