#!/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.4.2.1.1.0 796 --> PEAKFLOW-SP-MIB::deviceCpuLoadAvg1min.0
# .1.3.6.1.4.1.9694.1.4.2.1.2.0 742 --> PEAKFLOW-SP-MIB::deviceCpuLoadAvg5min.0
# .1.3.6.1.4.1.9694.1.4.2.1.3.0 742 --> PEAKFLOW-SP-MIB::deviceCpuLoadAvg15min.0
# .1.3.6.1.4.1.9694.1.4.2.1.4.0 0 --> PEAKFLOW-SP-MIB::deviceDiskUsage.0
# .1.3.6.1.4.1.9694.1.4.2.1.5.0 32864948 --> PEAKFLOW-SP-MIB::devicePhysicalMemory.0
# .1.3.6.1.4.1.9694.1.4.2.1.6.0 4793660 --> PEAKFLOW-SP-MIB::devicePhysicalMemoryInUse.0
# .1.3.6.1.4.1.9694.1.4.2.1.7.0 15 --> PEAKFLOW-SP-MIB::devicePhysicalMemoryUsage.0
# .1.3.6.1.4.1.9694.1.4.2.1.8.0 4892156 --> PEAKFLOW-SP-MIB::deviceSwapSpace.0
# .1.3.6.1.4.1.9694.1.4.2.1.9.0 0 --> PEAKFLOW-SP-MIB::deviceSwapSpaceInUse.0
# .1.3.6.1.4.1.9694.1.4.2.1.10.0 0 --> PEAKFLOW-SP-MIB::deviceSwapSpaceUsage.0
# .1.3.6.1.4.1.9694.1.4.2.1.11.0 0 --> PEAKFLOW-SP-MIB::deviceTotalFlows.0
# .1.3.6.1.4.1.9694.1.4.2.1.12.0 0 --> PEAKFLOW-SP-MIB::deviceTotalFlowsHC.0

factory_settings["filesystem_default_levels"] = FILESYSTEM_DEFAULT_LEVELS


factory_settings["arbor_memory_default_levels"] = ARBOR_MEMORY_CHECK_DEFAULT_PARAMETERS


def parse_peakflow_sp(info):
    valid = info[0]
    res = {"disk": valid[0], "memory": valid[1:3]}
    if valid[3]:
        # this value appears to be optional
        res["flows"] = valid[3]

    return res


check_info["arbor_peakflow_sp"] = {
    "check_function": check_arbor_memory,
    "inventory_function": inventory_arbor_memory,
    "parse_function": parse_peakflow_sp,
    "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.4.2.1",
        [
            "4.0",  # deviceDiskUsage
            "7.0",  # devicePhysicalMemoryUsage
            "10.0",  # deviceSwapSpaceUsage
            "12.0",  # deviceTotalFlowsHC
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("Peakflow SP"),
}

check_info["arbor_peakflow_sp.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",
}


def inventory_arbor_peakflow_sp_flows(parsed):
    if "flows" in parsed:
        return [(None, None)]


def check_arbor_peakflow_sp_flows(no_item, params, parsed):
    flows = int(parsed["flows"])
    return 0, "%d flows" % flows, [("flows", flows)]


check_info["arbor_peakflow_sp.flows"] = {
    "check_function": check_arbor_peakflow_sp_flows,
    "inventory_function": inventory_arbor_peakflow_sp_flows,
    "service_description": "Flow Count",
    "has_perfdata": True,
}
