#!/bin/sh
#
# Wildcard-script to monitor number of processes running as a given user.
# To monitor a user, link psu_<program> to this file. E.g.
#
#    ln -s /usr/share/munin/node/plugins-auto/psu_ /etc/munin/node.d/psu_munin
#
# ...will monitor number of processes owned by 'munin'.
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
# 	suggest  (optional - used by munin-config)
#
# Magic markers (optional):
#%# family=manual
#%# capabilities=autoconf suggest



name=`basename $0 | sed 's/^psu_//g'`

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 processes owned by $name
	echo 'graph_args --base 1000 --vertical-label processes -l 0'
	echo 'graph_category processes'
	echo "count.label $name"
	echo 'processes.draw LINE2'
	exit 0
fi

printf "count.value "
(pgrep -u "$name"; pgrep -U "$name") | sort -u | wc -l

