#!/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.temperature import *  # pylint: disable=wildcard-import,unused-wildcard-import

factory_settings["ups_modulys_battery_default_levels"] = {"capacity": (95, 90), "battime": (0, 0)}


def inventory_ups_modulys_battery(info):
    return [(None, {})]


def check_ups_modulys_battery(item, params, info):
    warn_left, crit_left = params["battime"]
    warn_perc, crit_perc = params["capacity"]
    for condition, elapsed_sec, remaining_min, capacity, _temperature in info:
        condition, elapsed_sec, capacity = [int(x) for x in (condition, elapsed_sec, capacity)]
        if elapsed_sec == 0:
            # not on battery so the it will not run out
            remaining_min = sys.maxsize
        elif not remaining_min:
            # The "remaining_min" value isn't always reported and we don't know why.
            # One theory is that it will be reported if on battery but we have no data to verify.
            #
            # If the theory holds true, this branch is never taken. If it doesn't then its likely
            # the information is only available on some variants of the device or in some
            # configurations. We can still report useful data based on "capacity"
            remaining_min = sys.maxsize
        else:
            remaining_min = int(remaining_min)

        # test 1: on mains or on battery?
        if elapsed_sec == 0:
            yield 0, "on mains"
        else:
            # we already have configurable levels on remaining capacity which
            # would be superfluous if we reported the discharging state itself
            # as a problem. Thus this reports "OK"
            yield 0, "discharging for %d minutes" % (elapsed_sec // 60)

        # test 2: battery health
        if condition == 1:
            yield 1, "battery health weak"
        elif condition == 2:
            yield 2, "battery needs to be replaced"

        # test 3: remaining capacity
        if remaining_min < crit_left or capacity < crit_perc:
            status = 2
        elif remaining_min < warn_left or capacity < warn_perc:
            status = 1
        else:
            status = 0

        infotext = ""

        if remaining_min < warn_left:
            infotext = "%d minutes remaining (warn/crit at %d/%d min)" % (
                remaining_min,
                warn_left,
                crit_left,
            )

        if capacity < warn_perc:
            infotext = "%d percent charged (warn/crit at %d/%d perc)" % (
                capacity,
                warn_perc,
                crit_perc,
            )

        yield status, infotext


check_info["ups_modulys_battery"] = {
    "inventory_function": inventory_ups_modulys_battery,
    "check_function": check_ups_modulys_battery,
    "service_description": "Battery Charge",
    "default_levels_variable": "ups_modulys_battery_default_levels",
    "group": "ups_capacity",
    "snmp_info": (
        ".1.3.6.1.4.1.2254.2.4.7",
        [
            "1",  # dupsBatteryCondition
            "4",  # dupsSecondsOnBattery
            "5",  # dupsBatteryEstimatedTime
            "8",  # dupsBatteryCapacity
            "9",  # dupsBatteryTemperature
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.2254.2.4",
}


def inventory_ups_modulys_battery_temp(info):
    for _condition, _elapsed_sec, _remaining_min, _capacity, temperature in info:
        if temperature:
            yield "Battery", {}


def check_ups_modulys_battery_temp(item, params, info):
    for _condition, _elapsed_sec, _remaining_min, _capacity, temperature in info:
        return check_temperature(int(temperature), params, "ups_modulys_battery_temp_%s" % item)


check_info["ups_modulys_battery.temp"] = {
    "inventory_function": inventory_ups_modulys_battery_temp,
    "check_function": check_ups_modulys_battery_temp,
    "has_perfdata": True,
    "service_description": "Temperature %s",
    "group": "temperature",
    "default_levels_variable": "ups_modulys_battery_temp_default_levels",
}
