#!/bin/sh
#
# Copyright (C) 2006 Jens Schanz
#
# Plugin to monitor the consumed CPU cycles of an uncapped LPAR
# on a power5 system for linux
#
# Parameters:
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#
# $Log: lop5-consumed_cpu_cycles.sh,v $
# Revision 1.1  2006/12/08 10:08:33  schanz
# *** empty log message ***
#
#
# Revision 1.0  20.11.2006 - first build
#
# If you have any suggestions, questions, or enhancements, feel free to email
# me at mail@jensschanz.de
#
# Let's go ...
#
# Magick markers (optional - used by munin-config and some installation
# scripts):
#%# family=auto
#%# capabilities=autoconf

LPARCFG=/proc/ppc64/lparcfg

STATEFILE=$MUNIN_PLUGSTATE/power5-consumed_cpu_cycles.state
# check input parameters
if [ "$1" = "autoconf" ]; then
	echo yes
	exit 0
fi

if [ "$1" = "config" ]; then
	echo 'graph_title Consumed CPU cycles'
	echo 'graph_args -l 0'
	echo 'graph_category cpu'
	echo 'graph_vlabel CPU cycles'
	echo 'graph_info This graph shows the CPU cycles on an uncapped LPAR'
	
	echo 'cpuCycles.label used CPU cycles'
	
	exit 0
fi

# let's do the main job

# get cpus in partition
unset LANG

# get actual time
ACTUALTIME=`date +%s`;

# get old time
if [ -f $STATEFILE ]; then
	OLDTIME=`cat $STATEFILE | grep OLDTIME | awk -F = '{print $2}'`;
else
	echo "OLDTIME = $ACTUALTIME" > $STATEFILE;
	OLDTIME=$ACTUALTIME;
fi

# get timebase
TIMEBASE=`cat /proc/cpuinfo | grep timebase | awk -F : '{print $2}'`;

# get actual purr
ACTUALPURR=`cat $LPARCFG| grep purr | awk -F = '{print $2}'`;

# get old purr
if [ -f $STATEFILE ]; then
	OLDPURR=`cat $STATEFILE | grep OLDPURR | awk -F = '{print $2}'`;
else
	echo "OLDPURR = $ACTUALPURR" >> $STATEFILE;
	OLDPURR=$ACTUALPURR;
fi

# calculate CPU cycles
CPUCYCLES=$(echo $OLDPURR $ACTUALPURR $TIMEBASE $OLDTIME $ACTUALTIME | awk '{printf ("%.1f", (($1-$2)/$3)/($4-$5));}');

# write data to state file
echo "OLDTIME = $ACTUALTIME" > $STATEFILE;
echo "OLDPURR = $ACTUALPURR" >> $STATEFILE;

echo "cpuCycles.value $CPUCYCLES"
