#!/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


def inventory_fsc_subsystems(info):
    yield from (
        (line[0], line[1], (int(line[1]) * 0.9, int(line[1]) * 0.8))
        for line in info
        if int(line[1]) > 0
    )


def check_fsc_subsystems(item, params, info):
    for line in info:  # , value1, value2 in info:
        name = line[0]
        if name != item:
            continue
        if line[1] == "":
            return 3, "Status not found in SNMP data"
        status = int(line[1])
        statusname = {1: "ok", 2: "degraded", 3: "error", 4: "failed", 5: "unknown-init"}.get(
            status, "invalid"
        )
        if status in {1, 5}:
            return (0, "%s - no problems" % statusname)
        if 2 <= status <= 4:
            return (2, "%s" % statusname)
        return (3, "unknown status %d" % status)


check_info["fsc_subsystems"] = {
    "check_function": check_fsc_subsystems,
    "inventory_function": inventory_fsc_subsystems,
    "service_description": "FSC %s",
    "snmp_info": (".1.3.6.1.4.1.231.2.10.2.11.3.1.1", [2, 3]),
    "snmp_scan_function": is_fsc,
}
