#!/bin/sh
#
# Script to monitor number of processes. Programs are configured
# in client-conf.d
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by lrrd-config)
#
# Configuration example
#
# [multips]
# env.names pop3d imapd sslwrap
# env.regex_imapd ^[0-9]* imapd:
# env.regex_pop3d ^[0-9]* pop3d:
# 
# Magic markers (optional):
#%# family=manual
#%# capabilities=autoconf

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

if [ -z "$names" ]; then
  echo "Configuration required"
  exit 1
fi

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

	echo graph_title Number of selected processes
	echo 'graph_args --base 1000 --vertical-label processes -l 0'
	for name in $names; do
		echo "$name.label $name"
		echo "$name.draw LINE2"
	done
	exit 0
fi

for name in $names; do
	printf "$name.value "

	eval REGEX='"${regex_'$name'-\<'$name'\>}"'
	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
done
