21 |
|
# You'd be silly not to use this ;) |
22 |
|
use strict; |
23 |
|
|
24 |
< |
# Paths |
25 |
< |
my($topbin) = "/usr/local/sbin/top"; |
26 |
< |
my($dfbin) = "/usr/bin/df"; |
27 |
< |
my($usersbin) = "/usr/ucb/users"; |
28 |
< |
my($unamebin) = "/usr/bin/uname"; |
29 |
< |
my($uptimebin) = "/usr/bin/uptime"; |
24 |
> |
# Have to hope this will work really. |
25 |
> |
my($ostype) = `uname -s`; chop($ostype); |
26 |
|
|
27 |
+ |
# Decide which paths we should use. |
28 |
+ |
my($topbin); my($dfbin); my($usersbin); my($unamebin); my($uptimebin); |
29 |
+ |
|
30 |
+ |
if ($ostype eq "SunOS") { |
31 |
+ |
# covers: Solaris 8 |
32 |
+ |
$topbin = "/usr/local/sbin/top -d2 -s1 0"; |
33 |
+ |
$dfbin = "/usr/bin/df"; |
34 |
+ |
$usersbin = "/usr/ucb/users"; |
35 |
+ |
$unamebin = "/usr/bin/uname"; |
36 |
+ |
$uptimebin = "/usr/bin/uptime"; |
37 |
+ |
} |
38 |
+ |
elsif ($ostype eq "Linux") { |
39 |
+ |
# covers: Debian r2.2 |
40 |
+ |
$topbin = "/usr/bin/top -d1 -n0 -b"; |
41 |
+ |
$dfbin = "/bin/df"; |
42 |
+ |
$usersbin = "/usr/bin/users"; |
43 |
+ |
$unamebin = "/bin/uname"; |
44 |
+ |
$uptimebin = "/usr/bin/uptime"; |
45 |
+ |
} |
46 |
+ |
elsif ($ostype eq "FreeBSD") { |
47 |
+ |
# covers: FreeBSD 4.2-STABLE |
48 |
+ |
$topbin = "/usr/bin/top -d2 -s1 0"; |
49 |
+ |
$dfbin = "/bin/df"; |
50 |
+ |
$usersbin = "/usr/bin/users"; |
51 |
+ |
$unamebin = "/usr/bin/uname"; |
52 |
+ |
$uptimebin = "/usr/bin/uptime"; |
53 |
+ |
} |
54 |
+ |
else { |
55 |
+ |
print "statgrab.pl Error: Unable to identify system type - \"$ostype\".\n"; |
56 |
+ |
print "\"uname -s\" does not report one of the following known types;\n"; |
57 |
+ |
print " SunOS, Linux, FreeBSD\n"; |
58 |
+ |
exit(1); |
59 |
+ |
} |
60 |
+ |
|
61 |
|
# Run the following components: - |
62 |
|
&print_ident(); |
63 |
|
&include_osver(); |
72 |
|
|
73 |
|
|
74 |
|
|
45 |
– |
|
46 |
– |
|
47 |
– |
|
48 |
– |
|
75 |
|
# prints out an identifier for this version of statgrab.pl |
76 |
|
# the host should check this when reading data |
77 |
|
# means the host must be checked and updated to work with newer versions. |
109 |
|
my($partition_no) = 0; |
110 |
|
for (my($i) = 0; $i < $#df; $i++) { |
111 |
|
my($line) = $df[$i]; |
112 |
< |
$line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; |
112 |
> |
$line =~ /^([^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; |
113 |
|
# $4 will not match unless everything before it does... |
114 |
|
if (defined $5) { |
115 |
|
my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); |
146 |
|
sub include_top() { |
147 |
|
|
148 |
|
# Find out some numbers from top. |
149 |
< |
my(@top) = `$topbin -d2 -s1 0`; |
149 |
> |
my(@top) = `$topbin`; |
150 |
|
my($top) = join(" ", @top); |
151 |
|
$top =~ s/\n//g; |
152 |
|
|
153 |
< |
&print_pair(0, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
154 |
< |
&print_pair(0, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
155 |
< |
&print_pair(0, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s/); |
156 |
< |
&print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
157 |
< |
&print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
158 |
< |
&print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
159 |
< |
&print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
160 |
< |
&print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
161 |
< |
&print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
162 |
< |
&print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/); |
163 |
< |
&print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
164 |
< |
&print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
165 |
< |
&print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
153 |
> |
if($ostype eq "SunOS") { |
154 |
> |
&print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
155 |
> |
&print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
156 |
> |
&print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
157 |
> |
&print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
158 |
> |
&print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
159 |
> |
&print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
160 |
> |
&print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/); |
161 |
> |
&print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
162 |
> |
&print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
163 |
> |
&print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
164 |
> |
|
165 |
> |
# The following need to be specified in megabytes. |
166 |
> |
# If they are preceeded by a G, then multiply by 1024. |
167 |
> |
|
168 |
> |
$top =~ /([^\s]+?)([MG]) real/; |
169 |
> |
my($real) = $1; |
170 |
> |
$real*=1024 if $2 eq "G"; |
171 |
> |
$real/=1024 if $2 eq "K"; |
172 |
> |
&print_pair(0, "packet.memory.total", $real); |
173 |
> |
|
174 |
> |
$top =~ /([^\s]+?)([MG]) free/; |
175 |
> |
my($free) = $1; |
176 |
> |
$free*=1024 if $2 eq "G"; |
177 |
> |
$free/=1024 if $2 eq "K"; |
178 |
> |
&print_pair(0, "packet.memory.free", $free); |
179 |
> |
|
180 |
> |
$top =~ /([^\s]+?)([MG]) swap in use/; |
181 |
> |
my($swap_in_use) = $1; |
182 |
> |
$swap_in_use*=1024 if $2 eq "G"; |
183 |
> |
$swap_in_use/=1024 if $2 eq "K"; |
184 |
> |
# DO NOT print this one out... save it for in a moment... |
185 |
> |
|
186 |
> |
$top =~ /([^\s]+?)([MG]) swap free/; |
187 |
> |
my($swap_free) = $1; |
188 |
> |
$swap_free*=1024 if $2 eq "G"; |
189 |
> |
$swap_free/=1024 if $2 eq "K"; |
190 |
> |
&print_pair(0, "packet.swap.free", $swap_free); |
191 |
> |
|
192 |
> |
# AJ requested total swap instead of swap_in_use, so here we go! |
193 |
> |
&print_pair(0, "packet.swap.total", $swap_free + $swap_in_use); |
194 |
> |
} |
195 |
> |
elsif ($ostype eq "FreeBSD") { |
196 |
> |
&print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
197 |
> |
&print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
198 |
> |
&print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
199 |
> |
&print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
200 |
> |
&print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/); |
201 |
> |
&print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
202 |
> |
&print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/); |
203 |
> |
&print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/); |
204 |
> |
&print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
205 |
|
|
206 |
< |
# The following need to be specified in megabytes. |
207 |
< |
# If they are preceeded by a G, then multiply by 1024. |
208 |
< |
|
209 |
< |
$top =~ /([^\s]+?)([MG]) real/; |
210 |
< |
my($real) = $1; |
211 |
< |
$real*=1024 if $2 eq "G"; |
212 |
< |
&print_pair(0, "packet.memory.total", $real); |
213 |
< |
|
214 |
< |
$top =~ /([^\s]+?)([MG]) free/; |
215 |
< |
my($free) = $1; |
216 |
< |
$free*=1024 if $2 eq "G"; |
217 |
< |
&print_pair(0, "packet.memory.free", $free); |
218 |
< |
|
219 |
< |
$top =~ /([^\s]+?)([MG]) swap in use/; |
220 |
< |
my($swap_in_use) = $1; |
221 |
< |
$swap_in_use*=1024 if $2 eq "G"; |
222 |
< |
# DO NOT print this one out... save it for in a moment... |
223 |
< |
|
224 |
< |
$top =~ /([^\s]+?)([MG]) swap free/; |
225 |
< |
my($swap_free) = $1; |
226 |
< |
$swap_free*=1024 if $2 eq "G"; |
227 |
< |
&print_pair(0, "packet.swap.free", $swap_free); |
228 |
< |
|
229 |
< |
# AJ requested total swap instead of swap_in_use, so here we go! |
230 |
< |
&print_pair(0, "packet.swap.total", $swap_free + $swap_in_use); |
206 |
> |
# FreeBSD is a bit different, we need to get user and nice. |
207 |
> |
my($user) = 0; |
208 |
> |
if($top =~ /([^\s]+?)% user/) { $user += $1; } |
209 |
> |
if($top =~ /([^\s]+?)% nice/) { $user += $1; } |
210 |
> |
&print_pair(0, "packet.cpu.user", $user); |
211 |
> |
|
212 |
> |
# The following need to be specified in megabytes. |
213 |
> |
# If they are preceeded by a G, then multiply by 1024. |
214 |
> |
|
215 |
> |
# !! Can't get the total memory !! |
216 |
> |
#$top =~ /([^\s]+?)([MG]) real/; |
217 |
> |
#my($real) = $1; |
218 |
> |
#$real*=1024 if $2 eq "G"; |
219 |
> |
#&print_pair(0, "packet.memory.total", $real); |
220 |
> |
&print_pair(0, "packet.memory.total", 0); |
221 |
> |
|
222 |
> |
$top =~ /([^\s]+?)([MG]) Free/; |
223 |
> |
my($free) = $1; |
224 |
> |
$free*=1024 if $2 eq "G"; |
225 |
> |
&print_pair(0, "packet.memory.free", $free); |
226 |
> |
|
227 |
> |
$top =~ /Swap: ([^\s]+?)([MG]) Total/; |
228 |
> |
my($swap_total) = $1; |
229 |
> |
$swap_total*=1024 if $2 eq "G"; |
230 |
> |
&print_pair(0, "packet.swap.total", $swap_total); |
231 |
> |
|
232 |
> |
$top =~ /Swap:.*, ([^\s]+?)([MG]) Free/; |
233 |
> |
my($swap_free) = $1; |
234 |
> |
$swap_free*=1024 if $2 eq "G"; |
235 |
> |
&print_pair(0, "packet.swap.free", $swap_free); |
236 |
> |
} |
237 |
> |
elsif ($ostype eq "Linux") { |
238 |
> |
&print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
239 |
> |
&print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
240 |
> |
&print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
241 |
> |
&print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
242 |
> |
&print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/); |
243 |
> |
&print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
244 |
> |
&print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/); |
245 |
> |
&print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/); |
246 |
> |
&print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/); |
247 |
> |
&print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
248 |
> |
|
249 |
> |
# The following need to be specified in megabytes. |
250 |
> |
# If they are preceeded by a G, then multiply by 1024. |
251 |
> |
|
252 |
> |
$top =~ / ([^\s]+?)([KMG]) av/; |
253 |
> |
my($real) = $1; |
254 |
> |
$real*=1024 if $2 eq "G"; |
255 |
> |
$real/=1024 if $2 eq "K"; |
256 |
> |
&print_pair(0, "packet.memory.total", int($real)); |
257 |
> |
|
258 |
> |
$top =~ / ([^\s]+?)([KMG]) free/; |
259 |
> |
my($free) = $1; |
260 |
> |
$free*=1024 if $2 eq "G"; |
261 |
> |
$free/=1024 if $2 eq "K"; |
262 |
> |
&print_pair(0, "packet.memory.free", int($free)); |
263 |
> |
|
264 |
> |
$top =~ /Swap:\s+([^\s]+?)([KMG]) av/; |
265 |
> |
my($swap_total) = $1; |
266 |
> |
$swap_total*=1024 if $2 eq "G"; |
267 |
> |
$swap_total/=1024 if $2 eq "K"; |
268 |
> |
&print_pair(0, "packet.swap.total", int($swap_total)); |
269 |
> |
|
270 |
> |
$top =~ /Swap:.*, ([^\s]+?)([KMG]) free/; |
271 |
> |
my($swap_free) = $1; |
272 |
> |
$swap_free*=1024 if $2 eq "G"; |
273 |
> |
$swap_free/=1024 if $2 eq "K"; |
274 |
> |
&print_pair(0, "packet.swap.free", int($swap_free)); |
275 |
> |
} |
276 |
> |
else { |
277 |
> |
# we could have some catchall here |
278 |
> |
# but as it stands this means we'll just skip top stuff |
279 |
> |
# for unknown systems |
280 |
> |
} |
281 |
|
} |
282 |
|
|
283 |
|
# sub to get details of the machine's operating system. |
300 |
|
|
301 |
|
} |
302 |
|
|
303 |
< |
# sub to get system uptime. |
303 |
> |
# sub to get system uptime in seconds. |
304 |
|
sub include_uptime() { |
305 |
|
|
306 |
+ |
# debug stuff, all the different cases |
307 |
+ |
|
308 |
+ |
# normal |
309 |
+ |
#my($uptime) = " 4:48pm up 49 day(s), 6:30, 201 users, load average: 0.33, 0.35, 0.38\n"; |
310 |
+ |
# 0 days |
311 |
+ |
#my($uptime) = " 4:48pm up 6:30, 201 users, load average: 0.33, 0.35, 0.38\n"; |
312 |
+ |
# 0 hours |
313 |
+ |
#my($uptime) = " 4:48pm up 49 day(s), 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
314 |
+ |
# 0 mins |
315 |
+ |
#my($uptime) = " 4:48pm up 49 day(s), 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
316 |
+ |
# 0 days and 0 mins |
317 |
+ |
#my($uptime) = " 4:48pm up 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
318 |
+ |
# 0 days and 0 hours |
319 |
+ |
#my($uptime) = " 4:48pm up 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
320 |
+ |
# 0 hours and 0 mins |
321 |
+ |
#my($uptime) = " 4:48pm up 49 day(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
322 |
+ |
|
323 |
|
# grab the uptime |
324 |
|
my($uptime) = `$uptimebin`; |
325 |
+ |
|
326 |
+ |
&print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/); |
327 |
+ |
&print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/); |
328 |
+ |
&print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/); |
329 |
|
|
330 |
|
# work out the days, hours, and minutes |
331 |
< |
if ($uptime =~ /hr/) { |
332 |
< |
# 0 minutes |
197 |
< |
$uptime =~ s/up ([0-9]+) .*, ([0-9]+) .*,/$1:$2:0/; |
198 |
< |
} |
199 |
< |
elsif ($uptime =~ /min/) { |
200 |
< |
# 0 hours |
201 |
< |
$uptime =~ s/up ([0-9]+) .*, ([0-9]+) .*,/$1:0:$2/; |
202 |
< |
} |
203 |
< |
elsif ($uptime =~ /day/) { |
331 |
> |
|
332 |
> |
if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) { |
333 |
|
# normal |
334 |
< |
$uptime =~ s/up ([0-9]+) .*, ([0-9]+):([0-9]+)/$1:$2:$3/; |
334 |
> |
$uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/; |
335 |
> |
$uptime = "$1:$2:$3"; |
336 |
|
} |
337 |
|
else { |
338 |
< |
# 0 days |
339 |
< |
$uptime =~ s/up ([0-9]+):([0-9]+)/0:$1:$2/; |
338 |
> |
if ($uptime =~ /day/) { |
339 |
> |
if ($uptime =~ /hr/) { |
340 |
> |
# 0 minutes |
341 |
> |
$uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/; |
342 |
> |
$uptime = "$1:$2:0"; |
343 |
> |
} |
344 |
> |
elsif ($uptime =~ /min/) { |
345 |
> |
# 0 hours |
346 |
> |
$uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/; |
347 |
> |
$uptime = "$1:0:$2"; |
348 |
> |
} |
349 |
> |
else { |
350 |
> |
# 0 hours and 0 mins |
351 |
> |
$uptime =~ /up\s+([0-9]+)/; |
352 |
> |
$uptime = "$1:0:0"; |
353 |
> |
} |
354 |
> |
} |
355 |
> |
elsif ($uptime =~ /hr/) { |
356 |
> |
# 0 days and 0 minutes |
357 |
> |
$uptime =~ /up\s+([0-9]+)\s+/; |
358 |
> |
$uptime = "0:$1:0"; |
359 |
> |
} |
360 |
> |
elsif ($uptime =~ /min/) { |
361 |
> |
# 0 days and 0 hours |
362 |
> |
$uptime =~ /up\s+([0-9]+)\s+/; |
363 |
> |
$uptime = "0:0:$1"; |
364 |
> |
} |
365 |
> |
else { |
366 |
> |
# 0 days |
367 |
> |
$uptime =~ /up\s+([0-9]+):([0-9]+)/; |
368 |
> |
$uptime = "0:$1:$2"; |
369 |
> |
} |
370 |
|
} |
371 |
|
|
372 |
< |
# turn into minutes |
213 |
< |
|
372 |
> |
# turn into seconds |
373 |
|
$uptime =~ /([0-9]+):([0-9]+):([0-9]+)/; |
374 |
< |
$uptime = $3 + ($2 + $1*24)*60; |
375 |
< |
|
374 |
> |
$uptime = ($3+($2+($1*24))*60)*60; |
375 |
> |
|
376 |
> |
# print the value out |
377 |
|
&print_pair("unknown", "packet.os.uptime", $uptime); |
378 |
|
|
379 |
|
} |