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

# example output
# .1.3.6.1.4.1.232.167.2.4.5.2.1.2.1 4
# .1.3.6.1.4.1.232.167.2.4.5.2.1.2.2 8
# .1.3.6.1.4.1.232.167.2.4.5.2.1.2.3 7
# .1.3.6.1.4.1.232.167.2.4.5.2.1.3.1 Temperature In
# .1.3.6.1.4.1.232.167.2.4.5.2.1.3.2 Warning message
# .1.3.6.1.4.1.232.167.2.4.5.2.1.3.3 Alarm message
# .1.3.6.1.4.1.232.167.2.4.5.2.1.4.1 4
# .1.3.6.1.4.1.232.167.2.4.5.2.1.4.2 4
# .1.3.6.1.4.1.232.167.2.4.5.2.1.4.3 4
# .1.3.6.1.4.1.232.167.2.4.5.2.1.5.1 20
# .1.3.6.1.4.1.232.167.2.4.5.2.1.5.2 0
# .1.3.6.1.4.1.232.167.2.4.5.2.1.5.3 0
# .1.3.6.1.4.1.232.167.2.4.5.2.1.6.1 35
# .1.3.6.1.4.1.232.167.2.4.5.2.1.6.2 0
# .1.3.6.1.4.1.232.167.2.4.5.2.1.6.3 0
# .1.3.6.1.4.1.232.167.2.4.5.2.1.7.1 10
# .1.3.6.1.4.1.232.167.2.4.5.2.1.7.2 0
# .1.3.6.1.4.1.232.167.2.4.5.2.1.7.3 0


def parse_hp_mcs_sensors(info):
    parsed = {}

    for line in info:
        parsed[line[0]] = {
            "type": int(line[1]),
            "name": line[2],
            "status": int(line[3]),
            "value": float(line[4]),
            "high": float(line[5]),
            "low": float(line[6]),
        }

    return parsed


def inventory_hp_mcs_sensors(parsed):
    for entry in parsed.values():
        if int(entry["type"]) in [4, 5, 13, 14, 15, 16, 17, 18, 19, 20]:
            yield (entry["name"], {})


def check_hp_mcs_sensors(item, params, parsed):
    for key, entry in parsed.items():
        if entry["name"] == item:
            return check_temperature(entry["value"], params, "hp_mcs_sensors_%s" % key)


check_info["hp_mcs_sensors"] = {
    "parse_function": parse_hp_mcs_sensors,
    "inventory_function": inventory_hp_mcs_sensors,
    "check_function": check_hp_mcs_sensors,
    "service_description": "Sensor %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.232.167.2.4.5.2.1",
        [
            "1",  # CPQWCRM-MIB::waterCoolUnitSensorIndex
            "2",  # CPQWCRM-MIB::waterCoolUnitSensorType
            "3",  # CPQWCRM-MIB::waterCoolUnitSensorText
            "4",  # CPQWCRM-MIB::waterCoolUnitSensorStatus
            "5",  # CPQWCRM-MIB::waterCoolUnitSensorValue
            "6",  # CPQWCRM-MIB::waterCoolUnitSensorSetHigh
            "7",  # CPQWCRM-MIB::waterCoolUnitSensorSetLow
        ],
    ),
    "snmp_scan_function": hp_mcs_scan_function,
    "group": "temperature",
}

factory_settings["hp_mcs_sensors_fan_default_levels"] = {
    "lower": (1000, 500),
}


def inventory_hp_mcs_sensors_fan(parsed):
    for entry in parsed.values():
        if entry["type"] in [9, 10, 11, 26, 27, 28]:
            yield (entry["name"], {})


def check_hp_mcs_sensors_fan(item, params, parsed):
    for entry in parsed.values():
        if entry["name"] == item:
            return check_fan(entry["value"], params)


check_info["hp_mcs_sensors.fan"] = {
    "inventory_function": inventory_hp_mcs_sensors_fan,
    "check_function": check_hp_mcs_sensors_fan,
    "service_description": "Sensor %s",
    "has_perfdata": True,
    "group": "hw_fans",
    "default_levels_variable": "hp_mcs_sensors_fan_default_levels",
}
