#!/bin/sh
#
# Copyright (C) 2007 Andrei Morgan
# Copyright (C) 2008 Micah Anderson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Graph Vserver load average
# 
# Configuration variables
#   vservers - specify the vservers to include in the graph (default: all)
#
# NOTE: If no configuration variables are set, the defaults will be used

# Example  /etc/munin/plugin-conf.d/munin-node 
#
# The following monitors the load average for vservers 1 and 3:
#
# [vserver_loadavg]
# user root
# env.vservers vserver1 vserver3

# Changelog
# version 0.1 - 2007 June 26
# Andrei Morgan <asm-debian@fifthhorseman.net>
#  - initial author, based upon vserver_resources by Holger Levsen and
#    Micah Anderson, and upon the examples in the munin wiki.
# version 0.2 - 2008 July 7
# Micah Anderson <micah@riseup.net>
#  - fix cvirt vs. nsproxy issue with newer kernels by adding $NAMELOC which
#    is aware of VCI_SPACES (> 2.6.19) as well as the older version

# If run with the "autoconf"-parameter, give our opinion on whether we
# should be run on this system or not. This is optional, and only used by
# munin-config. In the case of this plugin, we should most probably
# always be included whwn there is a vserver kernel.

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


# if vservers are specified, use them; the default is to use all.
VSERVERS="$vservers"

INFO=(`sed 's/.*:\t//' /proc/virtual/info 2>/dev/null || echo '<none>'`)
KCIN="$[ 16#${INFO[2]} ]";

# If this is 1, then VCI_SPACES is present in the kernel (new in 2.6.19)
if [ $[ (KCIN >> 10) & 1 ] -eq 1 ]
then 
    NAMELOC="nsproxy"
else 
    NAMELOC="cvirt"
fi

if [ -z "$VSERVERS" ] ; then
    XIDS=`find /proc/virtual/* -type d -exec basename {} \;`
else
    # it's really more performant to specify vservers by ids or not at all
    XIDS=""
    for i in $VSERVERS ; do
	if [ -d /proc/virtual/$i ] ; then
	    XIDS="${XIDS}${i} "
	else
	    for j in `find /proc/virtual/* -type d -exec basename {} \;` ; do
		if [ "$i" = "`cat /proc/virtual/$j/$NAMELOC |grep NodeName |cut -f2`" ] ; then
		    XIDS="${XIDS}${j} "
		fi
	    done
	fi
    done
fi
	
# If run with the "config"-parameter, give out information on how the
# graphs should look.  
if [ "$1" = "config" ]; then
        # The title of the graph
	echo 'graph_title loadavg of vserver'
        # Arguments to "rrdtool graph". In this case, tell it that the
        # lower limit of the graph is '0', and that 1k=1000 (not 1024)
	echo 'graph_args --base 1000 -l 0'
        # We want Cur/Min/Avg/Max unscaled (i.e. 0.42 load instead of
        # 420 milliload)
	echo 'graph_scale no'
        # The Y-axis label
	echo 'graph_vlabel loadavg'
	# graph information for the main table
	echo 'graph_info Shows 5-minute load average per vserver.'
        # Graph category. Defaults to 'other'
	echo 'graph_category vserver'
	for xid in $XIDS ; do
	# Specify the vservers
		LABEL=`cat /proc/virtual/$xid/$NAMELOC |grep NodeName |cut -f2`
		NAME=`echo $LABEL | cut -d. -f1 |  tr '-' '_'`
		echo "$NAME.label $LABEL: load average"
		echo "$NAME.info $NAME average load for the past 5 minutes"
	done 
        # Last, if run with the "config"-parameter, quit here (don't
        # display any data)
        exit 0
fi

for xid in $XIDS ; do
	LABEL=`cat /proc/virtual/$xid/$NAMELOC |grep NodeName |cut -f2`
	NAME=`echo $LABEL | cut -d. -f1 |  tr '-' '_'`
	echo -n "$NAME.value ";
	cat /proc/virtual/$xid/cvirt | grep loadavg: | cut -d' ' -f2	
done

