rendler has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; sub cpu_stat { my($cpu_user, $cpu_nice, $cpu_total, $cpu_idle); open STAT_FILE, "/proc/stat" or warn "Couldn't open /proc/stat: $! +\n"; while (<STAT_FILE>) { ($cpu_user, $cpu_nice, $cpu_total, $cpu_idle) = ($1,$2,$3,$4) +if /^cpu\s+(\d+)\ (\d+) (\d+) (\d+)/; last; } close STAT_FILE; return($cpu_user, $cpu_nice, $cpu_total, $cpu_idle); } my($cpu_user_old, $cpu_nice_old, $cpu_total_old, $cpu_idle_old) = cpu_ +stat(); sleep(1); my($cpu_user, $cpu_nice, $cpu_total, $cpu_idle) = cpu_stat(); my $ticks_past = ($cpu_user + $cpu_nice + $cpu_total + $cpu_idle) - ($ +cpu_user_old + $cpu_nice_old + $cpu_total_old + $cpu_idle_old); my $scale = 100.0 / $ticks_past; $cpu_user = sprintf "%.0f", ($cpu_user - $cpu_user_old) * $scale; $cpu_nice = sprintf "%.0f", ($cpu_nice - $cpu_nice_old) * $scale; $cpu_total = sprintf "%.0f", ($cpu_total - $cpu_total_old) * $scale; $cpu_idle = sprintf "%.0f", ($cpu_idle - $cpu_idle_old) * $scale; print "user: $cpu_user%, nice: $cpu_nice%, total: $cpu_total%, idle: +$cpu_idle%\n";
Edit Masem 2001-11-04 - CODE tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: suggestions for improvement
by blakem (Monsignor) on Nov 04, 2001 at 15:55 UTC | |
|
Re: suggestions for improvement
by Fastolfe (Vicar) on Nov 04, 2001 at 21:29 UTC | |
|
Re: suggestions for improvement
by rendler (Pilgrim) on Nov 04, 2001 at 15:59 UTC |