Commit | Line | Data |
---|---|---|
8d6c9e9b | 1 | #!/usr/bin/perl |
4aba78da | 2 | |
8d6c9e9b | 3 | use strict; |
4aba78da | 4 | |
8d6c9e9b TPVT |
5 | my @cpu_stat_t0 = proc_stat(); |
6 | sleep 1; | |
7 | my @cpu_stat_t1 = proc_stat(); | |
8 | my $total = 0; | |
9 | for (my $cpu_i = 0; $cpu_i < (scalar @cpu_stat_t0); $cpu_i++) { | |
10 | my @stat_t0 = split /\s+/, $cpu_stat_t0[$cpu_i]; | |
11 | my @stat_t1 = split /\s+/, $cpu_stat_t1[$cpu_i]; | |
12 | my ($t0_idle, $t1_idle) = ($stat_t0[3], $stat_t1[3]); | |
13 | my ($t0_total, $t1_total) = (0, 0); | |
14 | $t0_total += $_ foreach @stat_t0; | |
15 | $t1_total += $_ foreach @stat_t1; | |
16 | ||
17 | my $load = ($t1_total - $t0_total == 0) ? 100: | |
18 | int((100 * (($t1_total - $t0_total) - ($t1_idle - $t0_idle))/($t1_total - $t0_total)) + 0.5); | |
19 | print " +" unless ($cpu_i == 0); | |
20 | printf "% 5.1f%% ", $load; | |
21 | $total += $load; | |
22 | } | |
23 | printf " = % 6.1f%%\n", $total; | |
24 | ||
25 | sub proc_stat() { | |
26 | return grep {s/^cpu\d+\s//} read_file('/proc/stat'); | |
4aba78da | 27 | } |
8d6c9e9b TPVT |
28 | |
29 | sub read_file($) { | |
30 | my ($file) = @_; | |
31 | open FILE, "<$file"; | |
32 | my @content = <FILE>; | |
33 | close FILE; | |
34 | return @content; | |
35 | } | |
36 |