Blame contrib/cpumon/cpumon.pl

Packit 667938
#!/usr/local/bin/perl
Packit 667938
Packit 667938
# This was taken from a PERL script Chris Behrens wrote to monitor
Packit 667938
# resource usage for his IRC servers and was trimmed down to
Packit 667938
# report only cpu usage.  This has been tweaked to work well with
Packit 667938
# MRTG (Multi Router Traffic Grapher) and will work fine with
Packit 667938
# anything that has a pid file (ie: named)
Packit 667938
#
Packit 667938
# Matthew Ramsey <mjr@blackened.com>
Packit 667938
# Last Modified 31 OCT 1997
Packit 667938
Packit 667938
$DEBUG = 0;
Packit 667938
Packit 667938
# Which ps do you want to use ? If you use a non-berkeley based ps,
Packit 667938
# you will need to change the args used in the findcpu function.
Packit 667938
# Uncomment the line you want or modify one to suit your needs.
Packit 667938
Packit 667938
#$ps = "/usr/ucb/ps";	# Solaris with UCB
Packit 667938
$ps = "/bin/ps";	# most systems
Packit 667938
Packit 667938
# The ps arguments.  For a UCB-based (BSD) ps, -aux will probably
Packit 667938
# work just fine for you.  For SysV-based ps, -eaf works best for
Packit 667938
# me.
Packit 667938
Packit 667938
$psargs = "-aux";	# UCB-based
Packit 667938
#$psargs = "-eaf";	# sysV-based
Packit 667938
Packit 667938
if ($ARGV[0]) {
Packit 667938
   $pidfile = $ARGV[0] ;
Packit 667938
} else {
Packit 667938
   print STDERR "Usage: $0 <pidfile>\n" ;
Packit 667938
   exit 1 ;
Packit 667938
}
Packit 667938
Packit 667938
open(PID, "< $pidfile");
Packit 667938
chomp($pid = <PID>);
Packit 667938
close(PID);
Packit 667938
Packit 667938
$cpu = findcpu($pid);
Packit 667938
Packit 667938
print "$cpu\n";
Packit 667938
print "$cpu\n";
Packit 667938
print "$time\n";
Packit 667938
print "";
Packit 667938
Packit 667938
exit; # We're done!
Packit 667938
Packit 667938
sub findcpu
Packit 667938
{
Packit 667938
	local($pid) = @_;
Packit 667938
Packit 667938
	local($cpu, $psline, @ps);
Packit 667938
	open(PS, "$ps $psargs |") || die "Couldn't run a ps: $!";
Packit 667938
	chomp(@ps = <PS>);
Packit 667938
	close(PS);
Packit 667938
	foreach $psline (@ps)
Packit 667938
	{
Packit 667938
		@blah = split(' ', $psline);
Packit 667938
		print "$pid $blah[1]\n" if ($DEBUG);
Packit 667938
		return $blah[2] if ($blah[1] == $pid);
Packit 667938
	}
Packit 667938
	return -1;
Packit 667938
}