#!/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.6.2.3.0 2070 --> PRAVAIL-MIB::deviceCpuLoadAvg1min.0
# .1.3.6.1.4.1.9694.1.6.2.4.0 2059 --> PRAVAIL-MIB::deviceCpuLoadAvg5min.0
# .1.3.6.1.4.1.9694.1.6.2.5.0 2059 --> PRAVAIL-MIB::deviceCpuLoadAvg15min.0
# .1.3.6.1.4.1.9694.1.6.2.6.0 8 --> PRAVAIL-MIB::deviceDiskUsage.0
# .1.3.6.1.4.1.9694.1.6.2.7.0 49 --> PRAVAIL-MIB::devicePhysicalMemoryUsage.0
# .1.3.6.1.4.1.9694.1.6.2.8.0 0 --> PRAVAIL-MIB::deviceSwapSpaceUsage.0
# .1.3.6.1.4.1.9694.1.6.2.39.0 43 --> PRAVAIL-MIB::pravailOverrunDropRatePps.0

factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


factory_settings["arbor_memory_default_levels"] = ARBOR_MEMORY_CHECK_DEFAULT_PARAMETERS


def parse_pravail(info):
    # peakflow SP and TMS have the same info in different oid ranges
    valid = info[0]
    return {
        "disk": valid[0],
        "memory": valid[1:3],
        "host_fault": valid[3],
        "drop_rate": valid[4],
    }


check_info["arbor_pravail"] = {
    "check_function": check_arbor_memory,
    "inventory_function": inventory_arbor_memory,
    "parse_function": parse_pravail,
    "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.6.2",
        [
            "6.0",  # deviceDiskUsage
            "7.0",  # devicePhysicalMemoryUsage
            "8.0",  # deviceSwapSpaceUsage
            "1.0",  # pravailHostFault
            "39.0",  # pravailOverrunDropRatePps
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("Pravail"),
}

check_info["arbor_pravail.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_pravail.host_fault"] = {
    "check_function": check_arbor_host_fault,
    "inventory_function": inventory_arbor_host_fault,
    "service_description": "Host Fault",
}

check_info["arbor_pravail.drop_rate"] = {
    "check_function": check_arbor_drop_rate,
    "inventory_function": inventory_arbor_drop_rate,
    "service_description": "%s drop rate",
    "has_perfdata": True,
    "group": "generic_rate",
}
