#!/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.f5_bigip 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.fan import *  # pylint: disable=wildcard-import,unused-wildcard-import

# Agent / MIB output
# see 1.3.6.1.4.1.3375.2.1.3.2.1.1.0
# F5-BIGIP-SYSTEM-MIB::sysChassisFanIndex.1   .1.3.6.1.4.1.3375.2.1.3.2.1.1.1 = 1
# F5-BIGIP-SYSTEM-MIB::sysChassisFanIndex.2   .1.3.6.1.4.1.3375.2.1.3.2.1.1.2 = 2
# F5-BIGIP-SYSTEM-MIB::sysChassisFanIndex.3   .1.3.6.1.4.1.3375.2.1.3.2.1.1.3 = 3
# F5-BIGIP-SYSTEM-MIB::sysChassisFanIndex.4   .1.3.6.1.4.1.3375.2.1.3.2.1.1.4 = 4
# F5-BIGIP-SYSTEM-MIB::sysChassisFanSpeed.1   .1.3.6.1.4.1.3375.2.1.3.2.1.3.1 = 2915
# F5-BIGIP-SYSTEM-MIB::sysChassisFanSpeed.2   .1.3.6.1.4.1.3375.2.1.3.2.1.3.2 = 2930
# F5-BIGIP-SYSTEM-MIB::sysChassisFanSpeed.3   .1.3.6.1.4.1.3375.2.1.3.2.1.3.3 = 2945
# F5-BIGIP-SYSTEM-MIB::sysChassisFanSpeed.4   .1.3.6.1.4.1.3375.2.1.3.2.1.3.4 = 2960
# see 1.3.6.1.4.1.3375.2.1.3.6.1.0
# F5-BIGIP-SYSTEM-MIB::sysCpuSensorFanSpeed.1.1.   1.3.6.1.4.1.3375.2.1.3.6.2.1.3.1.1 = 4715
# F5-BIGIP-SYSTEM-MIB::sysCpuSensorFanSpeed.2.1.   1.3.6.1.4.1.3375.2.1.3.6.2.1.3.2.1 = 4730
# F5-BIGIP-SYSTEM-MIB::sysCpuSensorName.1.1.       1.3.6.1.4.1.3375.2.1.3.6.2.1.4.1.1 = 1/cpu1
# F5-BIGIP-SYSTEM-MIB::sysCpuSensorName.2.1.       1.3.6.1.4.1.3375.2.1.3.6.2.1.4.2.1 = 2/cpu1

f5_bigip_fans_default_levels = (2000, 500)


def parse_f5_bigip_fans(info):
    fantyp = ["Chassis", "Processor"]
    fanchoice = 0
    parsed = {}

    for line in info:
        for fanentry in line:
            if fanchoice >= len(fantyp):
                continue
            if fanchoice == 0:
                parsed[("%s %d" % (fantyp[fanchoice], int(fanentry[0])))] = int(fanentry[1])
            else:
                parsed[("%s %s" % (fantyp[fanchoice], fanentry[0]))] = int(fanentry[1])
        fanchoice += 1

    return parsed


def inventory_f5_bigip_fans(parsed):
    for item in parsed.keys():
        yield item, "f5_bigip_fans_default_levels"


def check_f5_bigip_fans(item, params, parsed):
    fanspeed = parsed.get(item)
    if fanspeed is None:
        # do not change this to if not fanspeed! fanspeed could be 0.
        yield 3, "Could not detect speed"
        return

    yield check_fan(fanspeed, params)


# Get ID and Speed from the CPU and chassis fan tables

check_info["f5_bigip_fans"] = {
    "check_function": check_f5_bigip_fans,
    "inventory_function": inventory_f5_bigip_fans,
    "parse_function": parse_f5_bigip_fans,
    "service_description": "FAN %s",
    "group": "hw_fans",
    "snmp_info": [
        (".1.3.6.1.4.1.3375.2.1.3.2.1.2.1", [1, 3]),
        (".1.3.6.1.4.1.3375.2.1.3.6.2.1", [4, 3]),
    ],
    "snmp_scan_function": scan_f5_bigip,
}
