#!/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.ups_generic import *  # pylint: disable=wildcard-import,unused-wildcard-import
ups_out_load_default_levels = (85, 90)  # warning / critical


def check_ups_out_load(item, params, info):
    warn, crit = params
    for idx, _raw_voltage, raw_load in info:
        if idx == item:
            power = int(raw_load) if raw_load else 0
            perfdata = [("out_load", power, warn, crit, 100)]
            infotext = "load: %d (warn/crit at %d/%d) " % (power, warn, crit)

            if power >= crit:
                return (2, infotext, perfdata)
            if power >= warn:
                return (1, infotext, perfdata)
            return (0, infotext, perfdata)

    return (3, "Phase %s not found in SNMP output" % item)


check_info['ups_out_load'] = {
    "inventory_function": lambda info: discovery_ups_generic(info, "ups_out_load_default_levels"),
    "check_function": check_ups_out_load,
    "service_description": "OUT load phase %s",
    "has_perfdata": True,
    "snmp_info": (".1.3.6.1.2.1.33.1.4.4.1", [0, 2, 5]),
    "snmp_scan_function": ups_generic_scan_function,
}
