#!/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.fsc 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
# We fetch the following columns from SNMP:
# 13: name of the temperature sensor (used as item)
# 11: current temperature in C
# 6:  warning level
# 8:  critical level


def inventory_fsc_temp(info):
    for line in info:
        # Ignore non-connected sensors
        if int(line[1]) < 500:
            yield (line[0], None)


def check_fsc_temp(item, params, info):
    for name, rawtemp, warn, crit in info:
        if name == item:
            temp = int(rawtemp)
            if temp == -1 or temp == 4294967295:
                return 3, "Sensor or component missing"

            return check_temperature(temp,
                                     params,
                                     "fsc_temp_%s" % item,
                                     dev_levels=(int(warn), int(crit)))


check_info['fsc_temp'] = {
    'inventory_function': inventory_fsc_temp,
    'check_function': check_fsc_temp,
    'service_description': 'Temperature %s',
    'has_perfdata': True,
    'snmp_info': (".1.3.6.1.4.1.231.2.10.2.2.5.2.1.1", [13, 11, 6, 8]),
    'snmp_scan_function': is_fsc_temp_prefer_sc2,
    'group': "temperature",
}
