1 |
#!/usr/bin/perl -w |
2 |
|
3 |
#----------------------------------------------------------------- |
4 |
# Machine statistics grabber |
5 |
# $Author: $ |
6 |
# $Id: $ |
7 |
# |
8 |
# A Perl script to return various information about a host machine |
9 |
# by examining the output of some common Unix/Linux commands. |
10 |
# This is a stopgap to act as a generic way of collecting the |
11 |
# data. It is perhaps more reliable than the current Java host |
12 |
# at doing this and it can obviously be used by a C++ program as |
13 |
# well until the C++ host is ready to find the information out |
14 |
# itself. |
15 |
#----------------------------------------------------------------- |
16 |
|
17 |
|
18 |
$| = 1; |
19 |
|
20 |
|
21 |
# You'd be silly not to use this ;) |
22 |
use strict; |
23 |
|
24 |
&include_users(); |
25 |
&include_top(); |
26 |
|
27 |
exit(0); |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
# sub to print pairs of data, separated by a single space character. |
33 |
sub print_pair($$) { |
34 |
my($name, $value) = @_; |
35 |
|
36 |
if (!defined $value) { |
37 |
$value = "unknown"; |
38 |
} |
39 |
|
40 |
# Remove the trailing linefeed if we've not already done so. |
41 |
chomp($value); |
42 |
|
43 |
# print the pair of data with a space inbetween. |
44 |
print "$name $value\n"; |
45 |
} |
46 |
|
47 |
|
48 |
sub include_users() { |
49 |
|
50 |
# Find out all users on this machine. |
51 |
my($users) = `users`; |
52 |
my(@users) = split(/\s+/, $users); |
53 |
|
54 |
my($users_count) = $#users + 1; |
55 |
my($users_list) = $users; |
56 |
|
57 |
&print_pair("packet.users.count", $users_count); |
58 |
&print_pair("packet.users.list", $users_list); |
59 |
} |
60 |
|
61 |
|
62 |
sub include_top() { |
63 |
|
64 |
# Find out some numbers from top. |
65 |
my(@top) = `top -d2 -s1 0`; |
66 |
my($top) = join(" ", @top); |
67 |
$top =~ s/\n//g; |
68 |
|
69 |
&print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
70 |
&print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
71 |
&print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
72 |
&print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/); |
73 |
&print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
74 |
&print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
75 |
&print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
76 |
&print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
77 |
&print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
78 |
&print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/); |
79 |
&print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
80 |
&print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
81 |
&print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
82 |
&print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/); |
83 |
&print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/); |
84 |
&print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); |
85 |
&print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); |
86 |
|
87 |
} |