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

# Max. eigth sensors
# .1.3.6.1.4.1.5528.100.4.2.10.1.4.399845582 Wasserstand_FG1
# .1.3.6.1.4.1.5528.100.4.2.10.1.4.3502248167 Ethernet Link Status
# .1.3.6.1.4.1.5528.100.4.2.10.1.4.3823829717 A-Link Bus Power
# .1.3.6.1.4.1.5528.100.4.2.10.1.3.399845582 0
# .1.3.6.1.4.1.5528.100.4.2.10.1.3.3502248167 0
# .1.3.6.1.4.1.5528.100.4.2.10.1.3.3823829717 0
# .1.3.6.1.4.1.5528.100.4.2.10.1.7.399845582 No Leak
# .1.3.6.1.4.1.5528.100.4.2.10.1.7.3502248167 Up
# .1.3.6.1.4.1.5528.100.4.2.10.1.7.3823829717 OK


# MIB: The sensor reading shown as a string (or empty string
# if it is not plugged into a port).
def inventory_apc_netbotz_other_sensors(info):
    for _sensor_label, _error_state, state_readable in info:
        if state_readable != "":
            return [(None, None)]


def check_apc_netbotz_other_sensors(_no_item, _no_params, info):
    count_ok_sensors = 0
    for sensor_label, error_state, state_readable in info:
        if state_readable != "":
            if state_readable != "OK":
                state_readable = state_readable.lower()

            if error_state == "0":
                count_ok_sensors += 1
            else:
                yield 2, "%s: %s" % (sensor_label, state_readable)

    if count_ok_sensors > 0:
        yield 0, "%d sensors are OK" % count_ok_sensors


check_info["apc_netbotz_other_sensors"] = {
    "inventory_function": inventory_apc_netbotz_other_sensors,
    "check_function": check_apc_netbotz_other_sensors,
    "service_description": "Numeric sensors summary",
    "snmp_info": (
        ".1.3.6.1.4.1.5528.100.4.2.10.1",
        [
            "4",  # NETBOTZV2-MIB::otherNumericSensorLabel
            "3",  # NETBOTZV2-MIB::otherNumericSensorErrorStatus
            "7",  # NETBOTZV2-MIB::otherNumericSensorValueStr
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(
        ".1.3.6.1.4.1.5528.100.20.10"
    ),
}
