1 |
#!/usr/bin/perl -w |
2 |
|
3 |
#----------------------------------------------------------------- |
4 |
# Machine statistics grabber |
5 |
# $Author: pjm2 $ |
6 |
# $Id: statgrab.pl,v 1.1 2001/01/19 19:37:59 pjm2 Exp $ |
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_osver(); |
25 |
&include_users(); |
26 |
&include_top(); |
27 |
|
28 |
exit(0); |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
# sub to print pairs of data, separated by a single space character. |
34 |
sub print_pair($$) { |
35 |
my($name, $value) = @_; |
36 |
|
37 |
if (!defined $value) { |
38 |
$value = "unknown"; |
39 |
} |
40 |
|
41 |
# Remove the trailing linefeed if we've not already done so. |
42 |
chomp($value); |
43 |
|
44 |
# print the pair of data with a space inbetween. |
45 |
print "$name $value\n"; |
46 |
} |
47 |
|
48 |
|
49 |
sub include_users() { |
50 |
|
51 |
# Find out all users on this machine. |
52 |
my($users) = `users`; |
53 |
my(@users) = split(/\s+/, $users); |
54 |
|
55 |
my($users_count) = $#users + 1; |
56 |
my($users_list) = $users; |
57 |
|
58 |
&print_pair("packet.users.count", $users_count); |
59 |
&print_pair("packet.users.list", $users_list); |
60 |
} |
61 |
|
62 |
|
63 |
sub include_top() { |
64 |
|
65 |
# Find out some numbers from top. |
66 |
my(@top) = `top -d2 -s1 0`; |
67 |
my($top) = join(" ", @top); |
68 |
$top =~ s/\n//g; |
69 |
|
70 |
&print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
71 |
&print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
72 |
&print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
73 |
&print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/); |
74 |
&print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
75 |
&print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
76 |
&print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
77 |
&print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
78 |
&print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
79 |
&print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/); |
80 |
&print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
81 |
&print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
82 |
&print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
83 |
&print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/); |
84 |
&print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/); |
85 |
&print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); |
86 |
&print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); |
87 |
|
88 |
} |
89 |
|
90 |
sub include_osver() { |
91 |
|
92 |
# Find out details about the operating system |
93 |
my($os_name) = `uname -s`; |
94 |
my($os_release) = `uname -r`; |
95 |
my($os_platform) = `uname -m`; |
96 |
my($os_sysname) = `uname -n`; |
97 |
my($os_version) = `uname -v`; |
98 |
|
99 |
&print_pair("packet.os.name", $os_name); |
100 |
&print_pair("packet.os.release", $os_release); |
101 |
&print_pair("packet.os.platform", $os_platform); |
102 |
&print_pair("packet.os.sysname", $os_sysname); |
103 |
&print_pair("packet.os.version", $os_version); |
104 |
|
105 |
} |