#!/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_dell_chassis_powersupplies(info):
    inventory = []
    for line in info:
        item = re.sub(r"\.", "-", line[0])
        inventory.append((item, None))
    return inventory


def check_dell_chassis_powersupplies(item, _no_params, info):
    for oid_end, voltage, current, maxpower in info:
        if item == re.sub(r"\.", "-", oid_end):
            power = savefloat(voltage) * savefloat(current)
            state = 0
            infotext = ""
            infotext += "current/max Power: %.2f / %s, Current: %s, Voltage: %s" % (
                power,
                maxpower,
                current,
                voltage,
            )
            perfdata = [("power", str(power) + "Watt", 0, maxpower, "", maxpower)]

            if savefloat(current) == 0:
                infotext = infotext + " - device in standby"

            return state, infotext, perfdata

    return 3, "unknown power supply"


check_info["dell_chassis_powersupplies"] = {
    "check_function": check_dell_chassis_powersupplies,
    "inventory_function": inventory_dell_chassis_powersupplies,
    "service_description": "Power Supply %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.674.10892.2.4.2.1",
        [
            OID_END,
            "5",  # drsPSUVoltsReading  0
            "6",  # drsPSUAmpsReading   1
            "7",  # drsPSUWattsReading  2
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.674.10892.2",
}
