#!/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_ibm_imm_voltage(info):
    for line in info:
        yield line[0], None


def check_ibm_imm_voltage(item, _no_params, info):
    for line in info:
        if line[0] == item:
            volt, crit, warn, crit_low, warn_low = [float(v) / 1000 for v in line[1:]]
            infotext = "%.2f Volt" % volt

            perfdata = [
                ("volt", volt, str(warn_low) + ":" + str(warn), str(crit_low) + ":" + str(crit))
            ]
            levelstext = " (levels warn/crit lower: %.1f/%.1f upper: %.1f/%.1f)" % (
                warn_low,
                crit_low,
                warn,
                crit,
            )

            if (crit_low and volt <= crit_low) or (crit and volt >= crit):
                state = 2
                infotext += levelstext
            elif (warn_low and volt <= warn_low) or (warn and volt >= warn):
                state = 1
                infotext += levelstext
            else:
                state = 0

            return state, infotext, perfdata


check_info["ibm_imm_voltage"] = {
    "check_function": check_ibm_imm_voltage,
    "inventory_function": inventory_ibm_imm_voltage,
    "service_description": "Voltage %s",
    "has_perfdata": True,
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").lower().endswith(" mips")
    or oid(".1.3.6.1.2.1.1.1.0").lower().endswith(" sh4a"),
    "snmp_info": (
        ".1.3.6.1.4.1.2.3.51.3.1.2.2.1",
        [  # voltTable.voltEntry
            # 1, # voltIndex
            2,  # voltDescr
            3,  # voltReading
            # 4, # voltNominalReading
            # 5, # voltNonRecovLimitHigh
            6,  # voltCritLimitHigh
            7,  # voltNonCritLimitHigh
            # 8, # voltNonRecovLimitLow
            9,  # voltCritLimitLow
            10,  # voltNonCritLimitLow
        ],
    ),
}
