#!/usr/bin/perl

use Proc::ProcessTable;

$interval=600;	#sleep interval of 5 minutes
$partofhour=0;	#keep track of where in hour we are

$tobj = new Proc::ProcessTable;	# create a new process object 

# forever loop, collecting stats every $interval seconds
# and dump them once an hour
while (1){
	&collectstats;
	&dumpandreset if ($partofhour >= 3600);
	sleep($interval);
}

# Collect the process Statistics
sub collectstats {
	my ($process);
	foreach $process (@{$tobj->table()}){
		
		# we should ignore ourselves
		next if ($process->pid() == $$);
		
		# save this process info for our next run
		push( @last,$process->pid(),$process->fname());
		
		# ignore this process if we saw it last iteration
		next if ($last{$process->pid()} eq $process->fname());
		
		# else, remember it
		$collection{$process->fname()}++;
	}
	# set the last hash using the current table for our next run
	%last = @last;
	$partofhour += $interval;
}

# dump out the results and reset our counters
sub dumpandreset {
	print scalar localtime(time).("-"x50)."\n";
	for (sort reverse_value_sort keys %collection){
		write;
	}
	
	undef %collection;
	$partofhour = 0;
}

# (reverse) sort by values in %collection and by key name
sub reverse_value_sort{
	return $collection{$b}<=>$collection{$a} || $a cmp $b;
}

format STDOUT=
@<<<<<<<<<<<<< @>>>>
$_,		$collection{$_}
.

format STDOUT_TOP =
Name		Count
-------------	--------

.


