#!/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.temperature import *  # pylint: disable=wildcard-import,unused-wildcard-import
# .1.3.6.1.4.1.2636.3.1.13.1.5.7.1.0.0 FPC: EX3300 48-Port @ 0/*/* --> SNMPv2-SMI::enterprises.2636.3.1.13.1.5.7.1.0.0
# .1.3.6.1.4.1.2636.3.1.13.1.5.7.2.0.0 FPC: EX3300 48-Port @ 1/*/* --> SNMPv2-SMI::enterprises.2636.3.1.13.1.5.7.2.0.0
# .1.3.6.1.4.1.2636.3.1.13.1.7.7.1.0.0 45 --> SNMPv2-SMI::enterprises.2636.3.1.13.1.7.7.1.0.0
# .1.3.6.1.4.1.2636.3.1.13.1.7.7.2.0.0 43 --> SNMPv2-SMI::enterprises.2636.3.1.13.1.7.7.2.0.0

factory_settings["juniper_temp_default_levels"] = {
    "levels": (55, 60),  # Just an assumption based on observed real temperatures
}


def parse_juniper_temp(info):
    parsed = {}
    for description, reading_str in info:
        temperature = float(reading_str)
        if temperature > 0:
            description = description.replace(":", "") \
                                     .replace("/*", "") \
                                     .replace("@ ", "") \
                                     .strip()
            parsed[description] = temperature
    return parsed


def inventory_juniper_temp(parsed):
    return [(description, {}) for description in parsed]


def check_juniper_temp(item, params, parsed):
    if item in parsed:
        return check_temperature(parsed[item], params, "juniper_temp_%s" % item)


check_info['juniper_temp'] = {
    'parse_function': parse_juniper_temp,
    'inventory_function': inventory_juniper_temp,
    'check_function': check_juniper_temp,
    'service_description': 'Temperature %s',
    'has_perfdata': True,
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(
        ".1.3.6.1.4.1.2636.1.1.1.2") or oid(".1.3.6.1.2.1.1.2.0").startswith(
            ".1.3.6.1.4.1.2636.1.1.1.4"),
    'snmp_info': (
        ".1.3.6.1.4.1.2636.3.1.13.1",
        [
            "5.7",  # jnxOperatingDescr
            "7.7",  # jnxOperatingTemp
        ]),
    'group': 'temperature',
    'default_levels_variable': "juniper_temp_default_levels",
}
