#!/bin/bash
#
# Plugin to monitor memory usage, for a selected set of users or applications
#
# Usage: Place in /etc/munin/plugins/ (or link it there  using ln -s)
#        For applications, add this to your /etc/munin/plugin-conf.d/munin-node:
#		[acc_rss_app]
#		env.list apache2 spamd clamd
#	 And for users
#		[acc_rss_user]
#		env.list root csa
#	 ...
#
#	 You need to also make sure that awk is installed
#
#  2009-04-01 v. 0.1 Suren A. Chilingaryan
#	Derived from cpubyuser 1.3.1 by Hanisch Elián
#
# Parameters understood:
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#

type=${0##*acc_}

DIVIDER=1
if [ $type = "rss_user" ]; then
    TITLE="Memory usage, by user"
    INFO="This graph shows memory usage, for monitored users."
    LIST="rss,user"
    UNITS="MB"
    DIVIDER=1024
elif [ $type = "rss_app" ]; then
    TITLE="Memory usage, by application"
    INFO="This graph shows memory usage, for monitored applications."
    LIST="rss,comm"
    UNITS="MB"
    DIVIDER=1024
elif [ $type = "cpu_user" ]; then
    TITLE="CPU usage, by user"
    INFO="This graph shows CPU usage, for monitored users."
    LIST="pcpu,user"
    UNITS="%"
elif [ $type = "cpu_app" ]; then
    TITLE="CPU usage, by application"
    INFO="This graph shows CPU usage, for monitored applications."
    LIST="pcpu,comm"
    UNITS="%"
elif [ $type = "proc_user" ]; then
    TITLE="Number of processes owned by user"
    INFO="This graph shows number of processes, for monitored users."
    LIST="user"
    UNITS="processes"
else
    exit
fi

USERS=$list
if [ -z "$omit_others" -o  "$omit_others" = "0" ]; then
    OMIT_OTHERS=0
else
    OMIT_OTHERS=1
fi

if [ -z "$chart_type" ]; then
    CHART_TYPE="LINE2"
else
    CHART_TYPE=$chart_type
fi

#USERS="csa arvur redrum root apache tomcat jabber clamav named mysql squid p2p"
#USERS="clamd spamd apache2 python java jabberd squid mysqld named tcs-binary"

if [ "$1" = "autoconf" ]; then
	if [ -n $USERS ]; then
		echo "yes"
	else
		echo "env.users not defined."
	fi
	exit
fi

if [ "$1" = "config" ]; then
	echo "graph_args --base 1000 -r --lower-limit 0"
	echo "graph_title $TITLE"
	echo "graph_category system"
	echo "graph_info $INFO"
	echo "graph_vlabel $UNITS"
	echo "graph_scale no"
	echo "graph_period second"
	_USERS=${USERS//[-.]/_}
	FIRSTUSER=1;
	if [ $OMIT_OTHERS = 1 ]; then
	    USERS_LIST="$USERS"
	    echo "graph_order $_USERS"
	else
	    USERS_LIST="$USERS others"
	    echo "graph_order $_USERS others"
	fi
	
	for USER in $USERS_LIST; do
		_USER=${USER//[-.]/_}
		echo "${_USER}.label $USER"
		echo "${_USER}.info Resources used by $USER"
		echo "${_USER}.type GAUGE"
		
		if [ $FIRSTUSER -eq 1 -a $CHART_TYPE = "AREA" ]; then
			echo "${_USER}.draw AREA"
			CHART_TYPE="STACK"
			FIRSTUSER=0
		else
			echo "${_USER}.draw $CHART_TYPE"
		fi
	done
	exit
fi

ps xa -e -o "$LIST" | \
	awk -v USERS="$USERS" -v OMIT_OTHERS="$OMIT_OTHERS" -v DIVIDER="$DIVIDER" '
		{
		    if ($2) { 
			if ($2 != "USER") CPU_USER[$2]+=$1 
		    } else {
			if ($1 != "USER") CPU_USER[$1]+=1 
		    }
		}
		END {
			others_sum = 0
			for (user in CPU_USER) {
				m = match(USERS,user)
				if (m != 0) {
					_user=user
					gsub(/[-.]/,"_", _user);
					print _user".value", CPU_USER[user] / DIVIDER
				} else
					others_sum += CPU_USER[user]
			}
			if (OMIT_OTHERS!=1) {
			    print "others.value", others_sum / DIVIDER;
			}
	    }'
