#!/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.infoblox 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.7779.3.1.1.2.1.10.1.2.39 1 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.40 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.41 1 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.sys-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.39 CPU_TEMP: +36.00 C --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.40 No temperature information available. --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.41 SYS_TEMP: +34.00 C --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.sys-temp

# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.39 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.40 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.2.41 5 --> IB-PLATFORMONE-MIB::ibNodeServiceStatus.sys-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.39 --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu1-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.40 --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.cpu2-temp
# .1.3.6.1.4.1.7779.3.1.1.2.1.10.1.3.41 --> IB-PLATFORMONE-MIB::ibNodeServiceDesc.sys-temp

# Suggested by customer
factory_settings["infoblox_temp_default_levels"] = {
    "levels": (40, 50),
}


def parse_infoblox_temp(info):
    map_states = {
        "1": (0, "working"),
        "2": (1, "warning"),
        "3": (2, "failed"),
        "4": (1, "inactive"),
        "5": (3, "unknown"),
    }

    parsed = {}
    # Just for a better handling
    for index, state, descr in list(zip(["", "1", "2", ""], info[0], info[1]))[1:]:
        if ":" in descr:
            name, val_str = descr.split(":", 1)
            val, unit = val_str.split()
            val = float(val)

        else:
            name = descr
            val = None
            unit = None

        what_name = "%s %s" % (name, index)
        parsed.setdefault(
            what_name.strip(),
            {
                "state": map_states[state],
                "reading": val,
                "unit": unit,
            },
        )

    return parsed


def inventory_infoblox_temp(parsed):
    for name, infos in parsed.items():
        if infos["reading"] is not None and infos["unit"] is not None:
            yield name, {}


def check_infoblox_temp(item, params, parsed):
    if item in parsed:
        reading = parsed[item]["reading"]
        devunit = parsed[item]["unit"].lower()
        devstate, devstatename = parsed[item]["state"]
        return check_temperature(
            reading,
            params,
            "infoblox_cpu_temp_%s" % item,
            dev_status=devstate,
            dev_status_name=devstatename,
            dev_unit=devunit,
        )


check_info["infoblox_temp"] = {
    "parse_function": parse_infoblox_temp,
    "inventory_function": inventory_infoblox_temp,
    "check_function": check_infoblox_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.7779.3.1.1.2.1.10.1",
        ["2", "3"],
        [
            OID_END,
            "39",  # IB-PLATFORMONE-MIB::ibNodeService[Desc/Status].cpu1-temp
            "40",  # IB-PLATFORMONE-MIB::ibNodeService[Desc/Status].cpu2-temp
            "41",  # IB-PLATFORMONE-MIB::ibNodeService[Desc/Status].sys-temp
        ],
    ),
    "snmp_scan_function": scan_infoblox,
    "group": "temperature",
    "default_levels_variable": "infoblox_temp_default_levels",
}
