#!/usr/bin/perl

use MDK::Common;

#- /proc/stat explanation from linux-2.6.11/Documentation/filesystems/proc.txt
#-
#- The very first  "cpu" line aggregates the  numbers in all  of the other "cpuN"
#- lines.  These numbers identify the amount of time the CPU has spent performing
#- different kinds of work.  Time units are in USER_HZ (typically hundredths of a
#- second).  The meanings of the columns are as follows, from left to right:
#- 
#- - user: normal processes executing in user mode
#- - nice: niced processes executing in user mode
#- - system: processes executing in kernel mode
#- - idle: twiddling thumbs
#- - iowait: waiting for I/O to complete
#- - irq: servicing interrupts
#- - softirq: servicing softirqs

my @samples;
while (1) {
	foreach (cat_('/proc/stat')) {
		next if !/^cpu\s/;	
		my (undef, @new, undef) = split /\s+/;
		if (@last) {
			my $idle = $new[3] - $last[3];
			my $occupied = 0;
			foreach my $i (0 .. $#last) {
				if ($i != 3) {
					$occupied += $new[$i] - $last[$i];
				}
			}
                        my $sample = $occupied*100/($occupied+$idle);
			push @samples, $sample;
			printf("proc occupancy: %02.2f%% instantaneous, %02.2f%% average\n", $sample, sum(@samples)/@samples);
		}
		@last = @new;
	}
	sleep 1;
}



