#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |          _           _           _       _   __   _______        |
# |       __| |_  ___ __| |__  _ __ | |__   / | /  \ |__ / _ \       |
# |      / _| ' \/ -_) _| / / | '  \| / /   | || () | |_ \_, /       |
# |      \__|_||_\___\__|_\_\_|_|_|_|_\_\   |_(_)__(_)___//_/        |
# |                                            check_mk 1.0.39       |
# |                                                                  |
# | Copyright Mathias Kettner 2009                mk@mathias-kettner |
# +------------------------------------------------------------------+
# 
# This file is part of check_mk 1.0.39.
# 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-
# ails.  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.


def check_procs(item, params, info):
    try:
        procname, warnmin, okmin, okmax, warnmax = params
    except ValueError,e:
        return (3, "UNKNOWN - invalid check parameters: %s" % (params,))
        
    # procname is either:
    # 1. a string beginning with ~. Then it is interpreted as regular expression
    # that must match the *beginning* of the process line. Please check the output of
    # check_mk -d HOSTNAME. Note: groups of whitespaces are reduced to one single
    # whitespace!
    # 2. a string *not* beginning with ~: It must be equal to the first column
    # in the process table (i.e. the process name). No regular expressions are
    # applied. A simple string compare is done.

    count = 0
    for ps in info:                 # loop over all processes found on the host. ps is list of strings
        if procname[0] != '~':
            if ps[0] == procname:
                count += 1
        else:
            pattern = procname[1:]
            reg = compiled_regexes.get(pattern)
            if not reg:
                reg = re.compile(pattern)
                compiled_regexes[pattern] = reg
            if reg.match(" ".join(ps)):
                count += 1

    if count > warnmax or count < warnmin:
        return (2, "CRIT - %d processes (ok from %d to %d)" % (count, okmin, okmax))
    elif count > okmax or count < okmin:
        return (1, "WARN - %d processes (ok from %d to %d)" % (count, okmin, okmax))
    else:
        return (0, "OK - %d processes" % count)


check_info['ps'] = (check_procs, "proc_%s", 0,  no_inventory_possible)
