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

# .1.3.6.1.4.1.2544.1.11.2.4.2.2.1.1.101318912  8110
# .1.3.6.1.4.1.2544.1.11.2.4.2.2.1.2.101318912  65600
# .1.3.6.1.4.1.2544.1.11.2.4.2.2.1.3.101318912  9
# .1.3.6.1.4.1.2544.2.5.5.1.1.1.101318912  "PSU/7HU-AC-800"
# .1.3.6.1.4.1.2544.2.5.5.1.1.5.101318912  "MOD-1-1"


def inventory_adva_fsp_current(info):
    for _current_str, _upper_threshold_str, power_str, _unit_name, index_aid in info:
        # Ignore non-connected sensors
        if index_aid != "" and power_str != "":
            yield index_aid, None


def check_adva_fsp_current(item, _no_params, info):
    for current_str, upper_threshold_str, _power_str, unit_name, index_aid in info:
        if index_aid == item:
            current = float(current_str) / 1000.0
            upper_threshold = float(upper_threshold_str) / 1000

            infotext = "[%s] %.3f A (crit at %.3f A)" % (unit_name, current, upper_threshold)
            perfdata = [
                (
                    "current",
                    current,
                    None,
                    upper_threshold,
                )
            ]

            if current <= 0:
                return 3, "Invalid sensor data"
            elif current >= upper_threshold:
                return 2, infotext, perfdata
            return 0, infotext, perfdata


check_info["adva_fsp_current"] = {
    "inventory_function": inventory_adva_fsp_current,
    "check_function": check_adva_fsp_current,
    "service_description": "Power Supply %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.2544",
        [
            "1.11.2.4.2.2.1.1",  # currentDiagnosticsAmpere
            "1.11.2.4.2.2.1.2",  # currentDiagnosticsUpperThres
            "1.11.2.4.2.2.1.3",  # currentDiagnosticsPsuOutputPower
            "2.5.5.1.1.1",  # inventoryUnitName
            "2.5.5.2.1.5",  # entityIndexAid
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0") == "Fiber Service Platform F7",
}
