#!/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.cpu_util import *  # pylint: disable=wildcard-import,unused-wildcard-import
# 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
factory_settings['huawei_switch_cpu_default_levels'] = {
    'levels': (80.0, 90.0),
}


def parse_huawei_switch_cpu(info):
    return parse_huawei_physical_entity_values(info)


@get_parsed_item_data
def check_huawei_switch_cpu(item, params, item_data):
    try:
        util = float(item_data.value)
    except TypeError:
        return
    return check_cpu_util(util, params, cores=[('core1', util)])


check_info['huawei_switch_cpu'] = {
    'parse_function': parse_huawei_switch_cpu,
    'inventory_function': discover(),
    'check_function': check_huawei_switch_cpu,
    'service_description': 'CPU utilization %s',
    'snmp_info': huawei_entity_specific_snmp_info((
        '.1.3.6.1.4.1.2011.5.25.31.1.1.1.1',
        [OID_END, "5"],
    )),
    'snmp_scan_function': huawei_switch_scan_function,
    'has_perfdata': True,
    'group': "cpu_utilization_multiitem",
    'default_levels_variable': 'huawei_switch_cpu_default_levels',
}
