#!/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.arbor import *  # pylint: disable=wildcard-import,unused-wildcard-import
from cmk.base.plugins.agent_based.utils.df import FILESYSTEM_DEFAULT_LEVELS

# .1.3.6.1.4.1.9694.1.5.2.1.0 No Fault --> PEAKFLOW-TMS-MIB::tmsHostFault.0
# .1.3.6.1.4.1.9694.1.5.2.2.0 101420100 --> PEAKFLOW-TMS-MIB::tmsHostUpTime.0
# .1.3.6.1.4.1.9694.1.5.2.3.0 46 --> PEAKFLOW-TMS-MIB::deviceCpuLoadAvg1min.0
# .1.3.6.1.4.1.9694.1.5.2.4.0 64 --> PEAKFLOW-TMS-MIB::deviceCpuLoadAvg5min.0
# .1.3.6.1.4.1.9694.1.5.2.5.0 67 --> PEAKFLOW-TMS-MIB::deviceCpuLoadAvg15min.0
# .1.3.6.1.4.1.9694.1.5.2.6.0 6 --> PEAKFLOW-TMS-MIB::deviceDiskUsage.0
# .1.3.6.1.4.1.9694.1.5.2.7.0 4 --> PEAKFLOW-TMS-MIB::devicePhysicalMemoryUsage.0
# .1.3.6.1.4.1.9694.1.5.2.8.0 0 --> PEAKFLOW-TMS-MIB::deviceSwapSpaceUsage.0

factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


factory_settings["arbor_memory_default_levels"] = ARBOR_MEMORY_CHECK_DEFAULT_PARAMETERS


def parse_peakflow_tms(info):
    health = info[0][0]
    updates = info[1][0]
    return {
        "disk": health[0],
        "memory": health[1:3],
        "host_fault": health[3],
        "update": {"Device": updates[0], "Mitigation": updates[1]},
    }


check_info["arbor_peakflow_tms"] = {
    "check_function": check_arbor_memory,
    "inventory_function": inventory_arbor_memory,
    "parse_function": parse_peakflow_tms,
    "service_description": "Memory",
    "has_perfdata": True,
    "group": "memory_arbor",
    "default_levels_variable": "arbor_memory_default_levels",
    "snmp_info": [
        (
            ".1.3.6.1.4.1.9694.1.5.2",
            [
                "6.0",  # deviceDiskUsage
                "7.0",  # devicePhysicalMemoryUsage
                "8.0",  # deviceSwapSpaceUsage
                "1.0",  # tmsHostFault
            ],
        ),
        (
            ".1.3.6.1.4.1.9694.1.5.5",
            [
                "1.2.0",  # tmsLastUpdate
                "2.1.0",  # tmsMitigationLastUpdate
            ],
        ),
    ],
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("Peakflow"),
}

check_info["arbor_peakflow_tms.disk_usage"] = {
    "check_function": check_arbor_disk_usage,
    "inventory_function": inventory_arbor_disk_usage,
    "service_description": "Disk Usage %s",
    "has_perfdata": True,
    "group": "filesystem",
    "default_levels_variable": "filesystem_default_levels",
}

check_info["arbor_peakflow_tms.host_fault"] = {
    "check_function": check_arbor_host_fault,
    "inventory_function": inventory_arbor_host_fault,
    "service_description": "Host Fault",
}


def inventory_peakflow_tms_updates(parsed):
    for name in parsed["update"]:
        yield name, None


def check_peakflow_tms_updates(item, no_params, parsed):
    if item in parsed["update"]:
        return 0, parsed["update"][item]


check_info["arbor_peakflow_tms.updates"] = {
    "check_function": check_peakflow_tms_updates,
    "inventory_function": inventory_peakflow_tms_updates,
    "service_description": "Config Update %s",
}
