1 |
#!/usr/bin/perl -w |
2 |
|
3 |
#----------------------------------------------------------------- |
4 |
# Machine statistics grabber |
5 |
# $Author: tdb1 $ |
6 |
# $Id: statgrab.pl,v 1.4 2001/01/22 04:09:41 tdb1 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 |
&print_ident(); |
25 |
&include_osver(); |
26 |
&include_users(); |
27 |
&include_top(); |
28 |
&include_disk(); |
29 |
|
30 |
exit(0); |
31 |
|
32 |
|
33 |
|
34 |
# prints out an identifier for this version of statgrab.pl |
35 |
# the host should check this when reading data |
36 |
# means the host must be checked and updated to work with newer versions. |
37 |
sub print_ident() { |
38 |
print 'version statgrab.pl $Revision: 1.4 $'; |
39 |
print "\n"; |
40 |
} |
41 |
|
42 |
# sub to print pairs of data, separated by a single space character. |
43 |
# If the second argument is undefined, then the pair is still printed, |
44 |
# however, the value shall be displayed as the string "unknown". |
45 |
sub print_pair($$) { |
46 |
my($name, $value) = @_; |
47 |
|
48 |
if (!defined $value) { |
49 |
$value = "unknown"; |
50 |
} |
51 |
|
52 |
# Remove the trailing linefeed if we've not already done so. |
53 |
chomp($value); |
54 |
|
55 |
# print the pair of data with a space inbetween. |
56 |
print "$name $value\n"; |
57 |
} |
58 |
|
59 |
|
60 |
# sub to find out disk partition information, if it exists. |
61 |
sub include_disk() { |
62 |
|
63 |
# Run the df program. |
64 |
my(@df) = `df -a`; |
65 |
|
66 |
# Only look for these partitions at the moment. |
67 |
my(@partition_list) = qw{ / /home /var /tmp }; |
68 |
|
69 |
# Go through each line of the program, looking for each thing we want. |
70 |
my($scan_for) = '('.join('|', @partition_list).')'; |
71 |
my($partition_no) = 0; |
72 |
for (my($i) = 0; $i < $#df; $i++) { |
73 |
my($line) = $df[$i]; |
74 |
$line =~ /^$scan_for\s*\(([^\s]*)\s*\):\s*([0-9]*)\s*blocks\s*([0-9]*)\s*files/; |
75 |
# $4 will not match unless everything before it does... |
76 |
if (defined $4) { |
77 |
my ($partition, $mounted, $blocks, $files) = ($1, $2, $3, $4); |
78 |
&print_pair("packet.disk.p$partition_no.attributes.name", $partition); |
79 |
&print_pair("packet.disk.p$partition_no.mounted", $mounted); |
80 |
&print_pair("packet.disk.p$partition_no.blocks", $blocks); |
81 |
&print_pair("packet.disk.p$partition_no.name", $files); |
82 |
++$partition_no; |
83 |
} |
84 |
} |
85 |
|
86 |
} |
87 |
|
88 |
# sub to find out the list of all (non-unique) usernames logged |
89 |
# in to the machine and how many their are. (not |
90 |
sub include_users() { |
91 |
|
92 |
# Find out all users on this machine. |
93 |
my($users) = `users`; |
94 |
my(@users) = split(/\s+/, $users); |
95 |
|
96 |
my($users_count) = $#users + 1; |
97 |
my($users_list) = $users; |
98 |
|
99 |
&print_pair("packet.users.count", $users_count); |
100 |
&print_pair("packet.users.list", $users_list); |
101 |
} |
102 |
|
103 |
|
104 |
# sub to run a series of regexps on the output of 'top' to |
105 |
# gather various machine statistics. |
106 |
sub include_top() { |
107 |
|
108 |
# Find out some numbers from top. |
109 |
my(@top) = `top -d2 -s1 0`; |
110 |
my($top) = join(" ", @top); |
111 |
$top =~ s/\n//g; |
112 |
|
113 |
&print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
114 |
&print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
115 |
&print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
116 |
&print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/); |
117 |
&print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
118 |
&print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
119 |
&print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
120 |
&print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
121 |
&print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
122 |
&print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/); |
123 |
&print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
124 |
&print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
125 |
&print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
126 |
&print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/); |
127 |
&print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/); |
128 |
&print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); |
129 |
&print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); |
130 |
|
131 |
} |
132 |
|
133 |
# sub to get details of the machine's operating system. |
134 |
sub include_osver() { |
135 |
|
136 |
# Find out details about the operating system |
137 |
# If these values remain undefined, then the print_pair |
138 |
# function shall show the value to be the string "unknown". |
139 |
my($os_name) = `uname -s`; |
140 |
my($os_release) = `uname -r`; |
141 |
my($os_platform) = `uname -m`; |
142 |
my($os_sysname) = `uname -n`; |
143 |
my($os_version) = `uname -v`; |
144 |
|
145 |
&print_pair("packet.os.name", $os_name); |
146 |
&print_pair("packet.os.release", $os_release); |
147 |
&print_pair("packet.os.platform", $os_platform); |
148 |
&print_pair("packet.os.sysname", $os_sysname); |
149 |
&print_pair("packet.os.version", $os_version); |
150 |
|
151 |
} |