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

# .1.3.6.1.4.1.2021.2.1.2.1 Web-Processes  --> UCD-SNMP-MIB::prNames.1
# .1.3.6.1.4.1.2021.2.1.2.2 SMTP-Processes --> UCD-SNMP-MIB::prNames.2
# .1.3.6.1.4.1.2021.2.1.2.3 POP3-Processes --> UCD-SNMP-MIB::prNames.3
# .1.3.6.1.4.1.2021.2.1.2.4 LPD-Processes  --> UCD-SNMP-MIB::prNames.4
# .1.3.6.1.4.1.2021.2.1.3.1 5              --> UCD-SNMP-MIB::prMin.1
# .1.3.6.1.4.1.2021.2.1.3.2 1              --> UCD-SNMP-MIB::prMin.2
# .1.3.6.1.4.1.2021.2.1.3.3 1              --> UCD-SNMP-MIB::prMin.3
# .1.3.6.1.4.1.2021.2.1.3.4 1              --> UCD-SNMP-MIB::prMin.4
# .1.3.6.1.4.1.2021.2.1.4.1 50             --> UCD-SNMP-MIB::prMax.1
# .1.3.6.1.4.1.2021.2.1.4.2 800            --> UCD-SNMP-MIB::prMax.2
# .1.3.6.1.4.1.2021.2.1.4.3 800            --> UCD-SNMP-MIB::prMax.3
# .1.3.6.1.4.1.2021.2.1.4.4 800            --> UCD-SNMP-MIB::prMax.4
# .1.3.6.1.4.1.2021.2.1.5.1 11             --> UCD-SNMP-MIB::prCount.1
# .1.3.6.1.4.1.2021.2.1.5.2 1              --> UCD-SNMP-MIB::prCount.2
# .1.3.6.1.4.1.2021.2.1.5.3 1              --> UCD-SNMP-MIB::prCount.3
# .1.3.6.1.4.1.2021.2.1.5.4 1              --> UCD-SNMP-MIB::prCount.4
# .1.3.6.1.4.1.2021.2.1.100.1 0            --> UCD-SNMP-MIB::prErrFlag.1
# .1.3.6.1.4.1.2021.2.1.100.2 0            --> UCD-SNMP-MIB::prErrFlag.2
# .1.3.6.1.4.1.2021.2.1.100.3 0            --> UCD-SNMP-MIB::prErrFlag.3
# .1.3.6.1.4.1.2021.2.1.100.4 0            --> UCD-SNMP-MIB::prErrFlag.4
# .1.3.6.1.4.1.2021.2.1.101.1              --> UCD-SNMP-MIB::prErrMessage.1
# .1.3.6.1.4.1.2021.2.1.101.2              --> UCD-SNMP-MIB::prErrMessage.2
# .1.3.6.1.4.1.2021.2.1.101.3              --> UCD-SNMP-MIB::prErrMessage.3
# .1.3.6.1.4.1.2021.2.1.101.4              --> UCD-SNMP-MIB::prErrMessage.4


def inventory_ucd_processes(info):
    return [(line[0].replace("-Processes", ""), None) for line in info]


def check_ucd_processes(item, _no_params, info):
    for pr_name, pr_min_str, pr_max_str, pr_count_str, pr_err_flag, pr_err_msg in info:
        if pr_name.replace("-Processes", "") == item:
            state = 0
            infotext = "Total: %s" % pr_count_str
            if int(pr_err_flag) == 0:
                state = 0
            else:
                state = 2
                if pr_err_msg:
                    infotext += ", %s" % pr_err_msg
                infotext += " (lower/upper crit at %s/%s)" % (pr_min_str, pr_max_str)

            return state, infotext, [("processes", int(pr_count_str))]


check_info["ucd_processes"] = {
    "inventory_function": inventory_ucd_processes,
    "check_function": check_ucd_processes,
    "service_description": "Processes %s",
    "has_perfdata": True,
    "snmp_info": (
        ".1.3.6.1.4.1.2021.2.1",
        [
            "2",  # prNames
            "3",  # prMin
            "4",  # prMax
            "5",  # prCount
            "100",  # prErrFlag
            "101",  # prErrMessage
        ],
    ),
    "snmp_scan_function": prefer_hr_else_ucd,
}
