line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
####################################################################### |
2
|
|
|
|
|
|
|
# # |
3
|
|
|
|
|
|
|
# Linux::stat # |
4
|
|
|
|
|
|
|
# # |
5
|
|
|
|
|
|
|
# Extract data from /proc/stat # |
6
|
|
|
|
|
|
|
# Supported kernels: 2.4.x, 2.2.x (don't know about others) # |
7
|
|
|
|
|
|
|
# # |
8
|
|
|
|
|
|
|
####################################################################### |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
package Linux::stat; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$VERSION = "1.00"; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
require 5.000; |
15
|
1
|
|
|
1
|
|
557
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1149
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $kernel = `uname -r`; |
18
|
|
|
|
|
|
|
$kernel =~ s/^(\d+\.\d+)\..*/$1/; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
0
|
|
|
0
|
0
|
0
|
my $proto = shift; |
22
|
0
|
|
0
|
|
|
0
|
my $class = ref($proto) || $proto; |
23
|
0
|
|
|
|
|
0
|
my $this = { |
24
|
|
|
|
|
|
|
stat => "/proc/stat", |
25
|
|
|
|
|
|
|
@_, |
26
|
|
|
|
|
|
|
}; |
27
|
0
|
0
|
|
|
|
0
|
die "Unable to read ".$this->{stat}.", stopped" unless -r $this->{stat}; |
28
|
0
|
|
|
|
|
0
|
$this = bless $this, $class; |
29
|
0
|
|
|
|
|
0
|
return $this; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub stat { |
33
|
1
|
|
|
1
|
0
|
43
|
my $this = shift; |
34
|
1
|
|
|
|
|
2
|
my $stat; |
35
|
1
|
50
|
|
|
|
5
|
if (defined $this) { |
36
|
0
|
0
|
|
|
|
0
|
if (ref($this)) { |
37
|
0
|
|
|
|
|
0
|
$stat = $this->{stat}; |
38
|
|
|
|
|
|
|
} else { |
39
|
0
|
|
|
|
|
0
|
$stat = $this; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} else { |
42
|
1
|
|
|
|
|
3
|
$stat = "/proc/stat"; |
43
|
|
|
|
|
|
|
} |
44
|
1
|
50
|
|
|
|
36
|
die "Unable to read $stat, stopped" unless -r $stat; |
45
|
1
|
|
|
|
|
32
|
open(STAT, $stat); |
46
|
1
|
|
|
|
|
3
|
my %result = (); |
47
|
1
|
50
|
|
|
|
8
|
if ($kernel < 2.4) { |
48
|
0
|
|
|
|
|
0
|
$result{disks} = []; |
49
|
0
|
|
|
|
|
0
|
foreach (0..3) { |
50
|
0
|
|
|
|
|
0
|
my $tmpHash = { |
51
|
|
|
|
|
|
|
major => 0, |
52
|
|
|
|
|
|
|
disk => $_, |
53
|
|
|
|
|
|
|
io => 0, |
54
|
|
|
|
|
|
|
read_count => 0, |
55
|
|
|
|
|
|
|
read_sectors => 0, |
56
|
|
|
|
|
|
|
write_count => 0, |
57
|
|
|
|
|
|
|
write_sectors => 0, |
58
|
|
|
|
|
|
|
}; |
59
|
0
|
|
|
|
|
0
|
push @{$result{disks}}, $tmpHash; |
|
0
|
|
|
|
|
0
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
1
|
|
|
|
|
141
|
foreach () { |
63
|
24
|
|
|
|
|
30
|
chomp($_); |
64
|
24
|
50
|
|
|
|
89
|
next unless /^([^\s:]+):?\s+(.+)$/; |
65
|
24
|
|
|
|
|
66
|
my ($desc, $data) = ($1, $2); |
66
|
24
|
|
|
|
|
27
|
$desc =~ tr/A-Z/a-z/; |
67
|
24
|
100
|
33
|
|
|
104
|
if ($desc =~ /^cpu(\d*)$/) { |
|
|
50
|
33
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
68
|
17
|
|
|
|
|
61
|
my ($user, $nice, $system, $idle) = split(/ /, $data); |
69
|
17
|
|
|
|
|
87
|
$result{$desc} = { |
70
|
|
|
|
|
|
|
user => $user / 100, |
71
|
|
|
|
|
|
|
nice => $nice / 100, |
72
|
|
|
|
|
|
|
system => $system / 100, |
73
|
|
|
|
|
|
|
idle => $idle / 100, |
74
|
|
|
|
|
|
|
}; |
75
|
17
|
100
|
|
|
|
49
|
$result{uptime} = ($user + $nice + $system + $idle) / 100 if $desc eq "cpu"; |
76
|
|
|
|
|
|
|
} elsif ($desc eq "page") { |
77
|
0
|
|
|
|
|
0
|
my @pgs = split(/\s+/, $data); |
78
|
0
|
|
|
|
|
0
|
($result{pages_in}, $result{pages_out}) = @pgs; |
79
|
|
|
|
|
|
|
} elsif ($desc eq "swap") { |
80
|
0
|
|
|
|
|
0
|
my @swps = split(/\s+/, $data); |
81
|
0
|
|
|
|
|
0
|
($result{swap_pages_in}, $result{swap_pages_out}) = @swps; |
82
|
|
|
|
|
|
|
} elsif ($desc eq "intr") { |
83
|
1
|
|
|
|
|
318
|
my @irqs = split(/\s+/, $data); |
84
|
1
|
|
|
|
|
29
|
$result{interrupts_total} = shift @irqs; |
85
|
1
|
|
|
|
|
279
|
$result{interrupts} = [@irqs]; |
86
|
|
|
|
|
|
|
} elsif (($desc eq "disk_io") && ($kernel >= 2.4)) { |
87
|
0
|
|
|
|
|
0
|
my @disks = split(/\s+/, $data); |
88
|
0
|
|
|
|
|
0
|
my @diskResult; |
89
|
0
|
|
|
|
|
0
|
my $tot = $result{disks_io}; |
90
|
0
|
|
|
|
|
0
|
foreach (@disks) { |
91
|
0
|
0
|
|
|
|
0
|
next unless /^\((\d+),(\d+)\):\((\d+),(\d+),(\d+),(\d+),(\d+)\)$/; |
92
|
0
|
|
|
|
|
0
|
my $currDisk = { |
93
|
|
|
|
|
|
|
major => $1, |
94
|
|
|
|
|
|
|
disk => $2, |
95
|
|
|
|
|
|
|
io => $3, |
96
|
|
|
|
|
|
|
read_count => $4, |
97
|
|
|
|
|
|
|
read_sectors => $5, |
98
|
|
|
|
|
|
|
write_count => $6, |
99
|
|
|
|
|
|
|
write_sectors => $7, |
100
|
|
|
|
|
|
|
}; |
101
|
0
|
|
|
|
|
0
|
push @diskResult, $currDisk; |
102
|
0
|
0
|
|
|
|
0
|
if (defined $tot) { |
103
|
0
|
|
|
|
|
0
|
foreach (keys %$currDisk) { |
104
|
0
|
|
|
|
|
0
|
$tot->{$_} += $currDisk->{$_}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} else { |
107
|
0
|
|
|
|
|
0
|
%$tot = %$currDisk; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
0
|
|
|
|
|
0
|
push @{$result{disks}}, @diskResult; |
|
0
|
|
|
|
|
0
|
|
111
|
0
|
|
|
|
|
0
|
delete $tot->{major}; |
112
|
0
|
|
|
|
|
0
|
delete $tot->{disk}; |
113
|
0
|
|
|
|
|
0
|
$result{disks_io} = $tot; |
114
|
|
|
|
|
|
|
} elsif (($desc =~ /^disk(_(rio|wio|rblk|wblk))?$/) && ($kernel < 2.4)) { |
115
|
0
|
|
|
|
|
0
|
my @diskData = split(/ /, $data); |
116
|
0
|
0
|
|
|
|
0
|
next if @diskData < 4; |
117
|
0
|
|
0
|
|
|
0
|
$desc = { |
118
|
|
|
|
|
|
|
"" => "io", |
119
|
|
|
|
|
|
|
"rio" => "read_count", "wio" => "write_count", |
120
|
|
|
|
|
|
|
"rblk" => "read_sectors", "wblk" => "write_sectors", |
121
|
|
|
|
|
|
|
}->{$2 || ""}; |
122
|
0
|
|
|
|
|
0
|
foreach (0..3) { |
123
|
0
|
|
|
|
|
0
|
$result{disks}->[$_]->{$desc} = $diskData[$_]; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} elsif ($desc eq "ctxt") { |
126
|
1
|
50
|
|
|
|
6
|
next unless $data =~ /^(\d+)/; |
127
|
1
|
|
|
|
|
5
|
$result{context_switch} = $1; |
128
|
|
|
|
|
|
|
} elsif ($desc eq "btime") { |
129
|
1
|
50
|
|
|
|
6
|
next unless $data =~ /^(\d+)/; |
130
|
1
|
|
|
|
|
3
|
$result{boot_timestamp} = $1; |
131
|
|
|
|
|
|
|
} elsif ($desc eq "processes") { |
132
|
1
|
50
|
|
|
|
5
|
next unless $data =~ /^(\d+)/; |
133
|
1
|
|
|
|
|
3
|
$result{total_forks} = $1; |
134
|
|
|
|
|
|
|
} else { |
135
|
3
|
|
|
|
|
7
|
$result{$desc} = $data; |
136
|
3
|
|
|
|
|
5
|
next; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
1
|
|
|
|
|
7
|
return \%result; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__END__ |