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

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.acme import *  # pylint: disable=wildcard-import,unused-wildcard-import

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.temperature import *  # pylint: disable=wildcard-import,unused-wildcard-import

# .1.3.6.1.4.1.9148.3.3.1.3.1.1.2.1 0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusType.1
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.2.2 0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusType.2
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.2.3 0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusType.3
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.2.4 0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusType.4
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.2.5 0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusType.5
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.2.6 0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusType.6
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.3.1 CPU TEMP0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusDescr.1
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.3.2 MAIN TEMP0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusDescr.2
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.3.3 MAIN TEMP1 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusDescr.3
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.3.4 PHY TEMP0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusDescr.4
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.3.5 PHY TCM5 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusDescr.5
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.3.6 PHY FPGA TEMP0 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusDescr.6
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.4.1 57 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusValue.1
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.4.2 33 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusValue.2
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.4.3 30 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusValue.3
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.4.4 53 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusValue.4
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.4.5 44 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusValue.5
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.4.6 53 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusValue.6
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.5.1 2 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureState.1
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.5.2 2 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureState.2
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.5.3 2 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureState.3
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.5.4 2 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureState.4
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.5.5 2 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureState.5
# .1.3.6.1.4.1.9148.3.3.1.3.1.1.5.6 2 --> ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureState.6


def inventory_acme_temp(info):
    return [(descr, {}) for descr, _value_str, state in info if state != "7"]


def check_acme_temp(item, params, info):
    for descr, value_str, state in info:
        if item == descr:
            dev_state, dev_state_readable = acme_environment_states[state]
            return check_temperature(
                float(value_str),
                params,
                "acme_temp.%s" % item,
                dev_status=dev_state,
                dev_status_name=dev_state_readable,
            )


check_info["acme_temp"] = {
    "inventory_function": inventory_acme_temp,
    "check_function": check_acme_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.9148.3.3.1.3.1.1",
        [
            "3",  # ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusDescr
            "4",  # ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusValue
            "5",  # ACMEPACKET-ENVMON-MIB::apEnvMonTemperatureStatusState
        ],
    ),
    "snmp_scan_function": scan_acme,
    "group": "temperature",
}
