#!/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.


def inventory_apc_mod_pdu_modules(info):
    return [(x[0], None) for x in info if x[0] != ""]


def check_apc_mod_pdu_modules(item, _no_params, info):
    apc_states = {
        1: "normal",
        2: "warning",
        3: "notPresent",
        6: "unknown",
    }
    for name, status, current_power in info:
        if name == item:
            status = saveint(status)
            # As per the device's MIB, the values are measured in tenths of KW
            current_power = savefloat(current_power) / 10
            message = "Status %s, current: %.2fkw " % (apc_states.get(status, 6), current_power)

            perf = [("current_power", current_power)]
            if status == 2:
                return 1, message, perf
            if status in [3, 6]:
                return 2, message, perf
            if status == 1:
                return 0, message, perf
            return 3, message
    return 3, "Module not found"


check_info["apc_mod_pdu_modules"] = {
    "check_function": check_apc_mod_pdu_modules,
    "inventory_function": inventory_apc_mod_pdu_modules,
    "service_description": "Module %s",
    "snmp_info": (
        ".1.3.6.1.4.1.318.1.1.22.2.6.1",
        [
            4,  # isxModularDistModuleOutputName
            6,  # isxModularDistModuleOutputStatus
            20,  # isxModularDistModuleOutputTotalPower
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.318.1.3.24.1",
    "has_perfdata": True,
}
