#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# .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_scan_function,
    'includes'                  : [ "ucd.include" ],
}
