#!/usr/bin/perl -w

use strict;

my ($sys, $nonidle) = (0,0);
# my ($oldsys, $oldnonidle) = (0,0);
# my $last = '/home/stats/cpu.last';

open (CPU, "</proc/stat");
while(<CPU>) {
# /proc/cpu has the following:
# cpu  20206916 11647914 21172633 432879446
# in 2.6, it is the following:
# user nice system idle iowait irq softirq
# which means user nice system idle
	next unless /cpu\s+/;
	if (/cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*$/) {
		$sys = $3;
		$nonidle = $1+$2+$3;
	} elsif (/cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*\d*\s*$/) {
		$sys = $3+$6+$7;
		$nonidle = $1+$2+$3+$6+$7;
	}
	last;
}
close CPU;

# if (open (LAST, "<$last") ) {
# 	while (<LAST>) {
# 		chomp;
# 		$oldsys = $1 if /sys=(\d+)$/;
# 		$oldnonidle = $1 if /nonidle=(\d+)$/;
# 	}
# 	close LAST;
 	my $h = `hostname`;
 	chomp $h;
# 	my $param1 = $nonidle - $oldnonidle;
# 	my $param2 = $sys - $oldsys;
 	print <<EOF;
$sys
$nonidle

$h
EOF
#}
#
#unlink $last;
#open (LAST, ">$last") or return 0;
#print LAST "sys=$sys\nnonidle=$nonidle\n";
#close LAST;

1;

