#!/bin/sh
#
# Wildcard-script to monitor number of processes. To monitor a
# program, link ps_<program> to this file. E.g.
#
#    ln -s /usr/share/munin/node/plugins-auto/ps_ /etc/munin/node.d/ps_exim
#
# ...will monitor number of exim-processes.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
# 	suggest  (optional - used by munin-config)
#
# Configuration variables
#
#   regex - regex to use for filtering pgrep/ps output
#
# $Log$
# Revision 1.4  2004/05/20 13:57:12  jimmyo
# Set categories to some of the plugins.
#
# Revision 1.3  2004/01/29 19:39:00  jimmyo
# Generic plugins now use printf instead of echo -n, as this is more portable (SF#885564)
#
# Revision 1.2  2004/01/29 18:43:20  jimmyo
# Changed wildcard plugin ps_ so it can use env.regex in plugin-conf.d/ (SF#882131)
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.2  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
# Magic markers (optional):
#%# family=auto
#%# capabilities=autoconf suggest


myname=`basename $0 | sed 's/^ps_//g'`

name="${name-\<$myname\>}"
REGEX="${regex-\<$name\>}"

if [ "$1" = "autoconf" ]; then
	echo yes
	exit 0
fi

if [ "$1" = "suggest" ]; then
	exit 0
fi

if [ "$1" = "config" ]; then

	echo graph_title Number of $myname processes
	echo 'graph_args --base 1000 --vertical-label processes -l 0'
	echo 'graph_category processes'
	echo "count.label $myname"
	echo 'processes.draw LINE2'
	exit 0
fi

printf "count.value "

PGREP=`which pgrep`
if [ -n "$PGREP" ]; then
	$PGREP -f -l "$name" | grep "$REGEX" | wc -l
elif [ -x /usr/ucb/ps ]; then
	# Solaris without pgrep. How old is that?
	/usr/ucb/ps auxwww | grep "$REGEX" | grep -v grep | wc -l
else
	ps auxwww | grep "$REGEX" | grep -v grep | wc -l
fi
