#!/usr/bin/env perl
#
# FILE:
# cputotals
#
# FUNCTION:
# Compute averages of vmstat cpu usage and queue length statistics.
#
# This script will compute summary averages of the some of the
# statistics returned by the unix "vmstat" command.  It will
# read one or more input files, tabulate averages for each input
# file, and then print out a summary for each input file.
#
# This script should work with most popular versions of vmstat, 
# including AIX & Linux. Note that this script may not summarize
# a statistic that you are interested in.  Because this is a 
# pretty simple, basic script, you should be able to modify it 
# to report what you want.  Note that some OS'es e.g Linux, Solaris, 
# do not report the wait time.  Note that some OS'es, e.g. Solaris, 
# put the statistics in different columns than what this script 
# expects.  Therefore, you will want to verify column numbering
# before you get too far.
#
# SAMPLE USAGE:
# cputotals vmstat.*.out
# where the wildcard * is used to specify multiple input files
#
#


# print usage
if (0 > $#ARGV) {
	print "Usage: \n";
	print "$0 <vmstat-out-file> [<vmstat-out-file> [...]]\n";
	print "\tShell wildcards * and ? may be used to specify multiple files.\n";
	print "\n";
	print "\tThe $0 command will compute and display averages from the\n";
	print "\tinput vmstat files. One summary line is printed per input file\n";
	die "\n";
}



# numfiles is the number of input files read
$numfiles = 0;

$nobs = 0;
$sum_kthrun   = 0;
$sum_kthblock = 0;
$sum_syscalls = 0;
$sum_cswitch  = 0;
$sum_user     = 0;
$sum_sys      = 0;
$sum_idle     = 0;
$sum_wait     = 0;

format STDOUT_TOP =
      file            nobs running block syscall cswitch user  sys  idle   wait
--------------------  ---- ------- ----- ------- ------- ---- ---- ------ -----
.

while(<>) {
	if ($_ =~ /^ *[-=a-zA-Z]/) {next;}
	$_ =~ s/^ *//;
	@split = split(/ +/,$_);
#	print $_;
#	print "$split[0] $split[1] $sum_kthblock $sum_kthrun\n";
#	print "$split[11] $split[12] $split[13] $split[14] $split[15] $split[16]\n";
#	print "$sum_syscalls $sum_cswitch $sum_user $sum_sys $sum_idle $sum_wait\n";
	$sum_kthrun   += $split[0];
	$sum_kthblock += $split[1];
	$sum_syscalls += $split[11];
	$sum_cswitch  += $split[12];
	$sum_user     += $split[13];
	$sum_sys      += $split[14];
	$sum_idle     += $split[15];
	$sum_wait     += $split[16];
	$nobs++;

	# end of file was detected
	if (eof) {
		$numfiles ++;

		# compute the averages ...
		$sum_kthrun   /= $nobs;
		$sum_kthblock /= $nobs;
		$sum_syscalls /= $nobs;
		$sum_syscalls /= 10;
		$sum_cswitch  /= $nobs;
		$sum_cswitch  /= 10;
		$sum_user     /= $nobs;
		$sum_sys      /= $nobs;
		$sum_idle     /= $nobs;
		$sum_wait     /= $nobs;

		# print the report
format STDOUT =
@<<<<<<<<<<<<<<<<<<<< @#### @###.# @###.# @##### @##### @##.# @##.# @##.# @##.#
$ARGV, $nobs, $sum_kthrun, $sum_kthblock, $sum_syscalls, $sum_cswitch, $sum_user, $sum_sys, $sum_idle, $sum_wait
.
		write;

		# reinitialize the stats for the next file
		$nobs = 0;
		$sum_kthrun   = 0;
		$sum_kthblock = 0;
		$sum_syscalls = 0;
		$sum_cswitch  = 0;
		$sum_user     = 0;
		$sum_sys      = 0;
		$sum_idle     = 0;
		$sum_wait     = 0;
	}
}

