1#!/usr/local/bin/perl 2 3# This was taken from a PERL script Chris Behrens wrote to monitor 4# resource usage for his IRC servers and was trimmed down to 5# report only cpu usage. This has been tweaked to work well with 6# MRTG (Multi Router Traffic Grapher) and will work fine with 7# anything that has a pid file (ie: named) 8# 9# Matthew Ramsey <mjr@blackened.com> 10# Last Modified 31 OCT 1997 11 12$DEBUG = 0; 13 14# Which ps do you want to use ? If you use a non-berkeley based ps, 15# you will need to change the args used in the findcpu function. 16# Uncomment the line you want or modify one to suit your needs. 17 18#$ps = "/usr/ucb/ps"; # Solaris with UCB 19$ps = "/bin/ps"; # most systems 20 21# The ps arguments. For a UCB-based (BSD) ps, -aux will probably 22# work just fine for you. For SysV-based ps, -eaf works best for 23# me. 24 25$psargs = "-aux"; # UCB-based 26#$psargs = "-eaf"; # sysV-based 27 28if ($ARGV[0]) { 29 $pidfile = $ARGV[0] ; 30} else { 31 print STDERR "Usage: $0 <pidfile>\n" ; 32 exit 1 ; 33} 34 35open(PID, "< $pidfile"); 36chomp($pid = <PID>); 37close(PID); 38 39$cpu = findcpu($pid); 40 41print "$cpu\n"; 42print "$cpu\n"; 43print "$time\n"; 44print ""; 45 46exit; # We're done! 47 48sub findcpu 49{ 50 local($pid) = @_; 51 52 local($cpu, $psline, @ps); 53 open(PS, "$ps $psargs |") || die "Couldn't run a ps: $!"; 54 chomp(@ps = <PS>); 55 close(PS); 56 foreach $psline (@ps) 57 { 58 @blah = split(' ', $psline); 59 print "$pid $blah[1]\n" if ($DEBUG); 60 return $blah[2] if ($blah[1] == $pid); 61 } 62 return -1; 63} 64