#!/bin/sh
#
# Wildcard-script to monitor number of processes. To monitor a
# program, link ps_<program> to this file. E.g.
#
#    ln -s /usr/local/libexec/munin/plugins/ps_ /etc/munin/plugins/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
#
# Magic markers (optional):
#%# family=auto
#%# capabilities=autoconf

. $MUNIN_LIBDIR/plugins/plugin.sh

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"
	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
