ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/generic/statgrab.pl
Revision: 1.38
Committed: Thu Mar 15 10:45:15 2001 UTC (23 years, 8 months ago) by pjm2
Content type: text/plain
Branch: MAIN
Changes since 1.37: +13 -10 lines
Log Message:
Fixed the regexps to match K on memory details from top

File Contents

# User Rev Content
1 pjm2 1.1 #!/usr/bin/perl -w
2    
3     #-----------------------------------------------------------------
4     # Machine statistics grabber
5 pjm2 1.38 # $Author: pjm2 $
6     # $Id: statgrab.pl,v 1.37 2001/03/11 12:51:52 pjm2 Exp $
7 pjm2 1.1 #
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 tdb 1.30 # 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 tdb 1.31 $topbin = "/usr/local/sbin/top -d2 -s1 0";
33 tdb 1.30 $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 tdb 1.31 $topbin = "/usr/bin/top -d1 -n0 -b";
41 tdb 1.30 $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 tdb 1.31 $topbin = "/usr/bin/top -d2 -s1 0";
49 tdb 1.30 $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 tdb 1.13
61 pjm2 1.6 # Run the following components: -
62 tdb 1.3 &print_ident();
63 tdb 1.2 &include_osver();
64 tdb 1.17 &include_uptime();
65 pjm2 1.1 &include_users();
66     &include_top();
67 pjm2 1.5 &include_disk();
68 pjm2 1.1
69 pjm2 1.6 # End the program normally.
70 pjm2 1.1 exit(0);
71    
72    
73    
74 pjm2 1.6
75 tdb 1.3 # 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.
78     sub print_ident() {
79 pjm2 1.38 print 'version statgrab.pl $Revision: 1.37 $';
80 tdb 1.3 print "\n";
81     }
82 pjm2 1.1
83     # sub to print pairs of data, separated by a single space character.
84 pjm2 1.5 # If the second argument is undefined, then the pair is still printed,
85 pjm2 1.25 # however, the value shall be displayed as the the 'default' value
86     # if the passed value was undefined.
87 pjm2 1.8 sub print_pair($$$) {
88 pjm2 1.25 my($default, $name, $value) = @_;
89 pjm2 1.1
90     if (!defined $value) {
91 pjm2 1.25 $value = $default;
92 pjm2 1.1 }
93    
94     # Remove the trailing linefeed if we've not already done so.
95     chomp($value);
96    
97     # print the pair of data with a space inbetween.
98     print "$name $value\n";
99     }
100    
101    
102 pjm2 1.5 # sub to find out disk partition information, if it exists.
103     sub include_disk() {
104    
105     # Run the df program.
106 tdb 1.13 my(@df) = `$dfbin -ak`;
107 pjm2 1.5
108     # Go through each line of the program, looking for each thing we want.
109     my($partition_no) = 0;
110     for (my($i) = 0; $i < $#df; $i++) {
111     my($line) = $df[$i];
112 tdb 1.35 $line =~ /^([^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/;
113 pjm2 1.5 # $4 will not match unless everything before it does...
114 pjm2 1.7 if (defined $5) {
115     my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5);
116 pjm2 1.25 &print_pair("unknown", "packet.disk.p$partition_no.attributes.name", $filesystem);
117     &print_pair(0, "packet.disk.p$partition_no.attributes.kbytes", $kbytes);
118     &print_pair(0, "packet.disk.p$partition_no.attributes.used", $used);
119     &print_pair(0, "packet.disk.p$partition_no.attributes.avail", $avail);
120     &print_pair("unknown", "packet.disk.p$partition_no.attributes.mount", $mount);
121 pjm2 1.5 ++$partition_no;
122     }
123     }
124    
125     }
126    
127     # sub to find out the list of all (non-unique) usernames logged
128     # in to the machine and how many their are. (not
129 pjm2 1.1 sub include_users() {
130    
131     # Find out all users on this machine.
132 tdb 1.13 my($users) = `$usersbin`;
133 pjm2 1.16 $users = "\n" unless defined $users;
134     chop $users;
135 pjm2 1.14 my($users_count) = 0;
136     $users_count++ while $users =~ /\w+/g;
137 pjm2 1.18 my($users_list) = $users." ";
138 pjm2 1.1
139 pjm2 1.25 &print_pair(0, "packet.users.count", $users_count);
140     &print_pair("unknown", "packet.users.list", $users_list);
141 pjm2 1.1 }
142    
143    
144 pjm2 1.5 # sub to run a series of regexps on the output of 'top' to
145     # gather various machine statistics.
146 pjm2 1.1 sub include_top() {
147    
148     # Find out some numbers from top.
149 tdb 1.31 my(@top) = `$topbin`;
150 pjm2 1.1 my($top) = join(" ", @top);
151     $top =~ s/\n//g;
152    
153 tdb 1.32 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 pjm2 1.38 $top =~ /([^\s]+?)([KMG]) real/;
169 tdb 1.32 my($real) = $1;
170     $real*=1024 if $2 eq "G";
171 pjm2 1.37 $real/=1024 if $2 eq "K";
172 tdb 1.32 &print_pair(0, "packet.memory.total", $real);
173    
174 pjm2 1.38 $top =~ /([^\s]+?)([KMG]) free/;
175 tdb 1.32 my($free) = $1;
176     $free*=1024 if $2 eq "G";
177 pjm2 1.37 $free/=1024 if $2 eq "K";
178 tdb 1.32 &print_pair(0, "packet.memory.free", $free);
179    
180 pjm2 1.38 $top =~ /([^\s]+?)([KMG]) swap in use/;
181 tdb 1.32 my($swap_in_use) = $1;
182     $swap_in_use*=1024 if $2 eq "G";
183 pjm2 1.37 $swap_in_use/=1024 if $2 eq "K";
184 tdb 1.32 # DO NOT print this one out... save it for in a moment...
185    
186 pjm2 1.38 $top =~ /([^\s]+?)([KMG]) swap free/;
187 tdb 1.32 my($swap_free) = $1;
188     $swap_free*=1024 if $2 eq "G";
189 pjm2 1.37 $swap_free/=1024 if $2 eq "K";
190 tdb 1.32 &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 tdb 1.36
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 tdb 1.32
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 pjm2 1.38 $top =~ /([^\s]+?)([KMG]) Free/;
223 tdb 1.32 my($free) = $1;
224     $free*=1024 if $2 eq "G";
225 pjm2 1.38 $free/=1024 if $2 eq "K";
226 tdb 1.32 &print_pair(0, "packet.memory.free", $free);
227    
228 pjm2 1.38 $top =~ /Swap: ([^\s]+?)([KMG]) Total/;
229 tdb 1.32 my($swap_total) = $1;
230     $swap_total*=1024 if $2 eq "G";
231 pjm2 1.38 $swap_total/=1024 if $2 eq "K";
232 tdb 1.32 &print_pair(0, "packet.swap.total", $swap_total);
233    
234 pjm2 1.38 $top =~ /Swap:.*, ([^\s]+?)([KMG]) Free/;
235 tdb 1.32 my($swap_free) = $1;
236     $swap_free*=1024 if $2 eq "G";
237 pjm2 1.38 $swap_free/=1024 if $2 eq "K";
238 tdb 1.32 &print_pair(0, "packet.swap.free", $swap_free);
239     }
240 tdb 1.33 elsif ($ostype eq "Linux") {
241     &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
242     &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
243     &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
244     &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
245     &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/);
246     &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
247     &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
248     &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
249     &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
250     &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
251    
252     # The following need to be specified in megabytes.
253     # If they are preceeded by a G, then multiply by 1024.
254    
255     $top =~ / ([^\s]+?)([KMG]) av/;
256     my($real) = $1;
257     $real*=1024 if $2 eq "G";
258     $real/=1024 if $2 eq "K";
259     &print_pair(0, "packet.memory.total", int($real));
260    
261     $top =~ / ([^\s]+?)([KMG]) free/;
262     my($free) = $1;
263     $free*=1024 if $2 eq "G";
264     $free/=1024 if $2 eq "K";
265     &print_pair(0, "packet.memory.free", int($free));
266    
267     $top =~ /Swap:\s+([^\s]+?)([KMG]) av/;
268     my($swap_total) = $1;
269     $swap_total*=1024 if $2 eq "G";
270     $swap_total/=1024 if $2 eq "K";
271     &print_pair(0, "packet.swap.total", int($swap_total));
272    
273     $top =~ /Swap:.*, ([^\s]+?)([KMG]) free/;
274     my($swap_free) = $1;
275     $swap_free*=1024 if $2 eq "G";
276     $swap_free/=1024 if $2 eq "K";
277     &print_pair(0, "packet.swap.free", int($swap_free));
278     }
279 tdb 1.32 else {
280     # we could have some catchall here
281 tdb 1.33 # but as it stands this means we'll just skip top stuff
282     # for unknown systems
283 tdb 1.32 }
284 tdb 1.2 }
285    
286 pjm2 1.5 # sub to get details of the machine's operating system.
287 tdb 1.2 sub include_osver() {
288    
289     # Find out details about the operating system
290 pjm2 1.5 # If these values remain undefined, then the print_pair
291     # function shall show the value to be the string "unknown".
292 tdb 1.13 my($os_name) = `$unamebin -s`;
293     my($os_release) = `$unamebin -r`;
294     my($os_platform) = `$unamebin -m`;
295     my($os_sysname) = `$unamebin -n`;
296     my($os_version) = `$unamebin -v`;
297 tdb 1.2
298 pjm2 1.25 &print_pair("unknown", "packet.os.name", $os_name);
299     &print_pair("unknown", "packet.os.release", $os_release);
300     &print_pair("unknown", "packet.os.platform", $os_platform);
301     &print_pair("unknown", "packet.os.sysname", $os_sysname);
302     &print_pair("unknown", "packet.os.version", $os_version);
303 tdb 1.2
304 pjm2 1.1 }
305 tdb 1.17
306 tdb 1.28 # sub to get system uptime in seconds.
307 tdb 1.17 sub include_uptime() {
308    
309 tdb 1.27 # debug stuff, all the different cases
310    
311     # normal
312     #my($uptime) = " 4:48pm up 49 day(s), 6:30, 201 users, load average: 0.33, 0.35, 0.38\n";
313     # 0 days
314     #my($uptime) = " 4:48pm up 6:30, 201 users, load average: 0.33, 0.35, 0.38\n";
315     # 0 hours
316     #my($uptime) = " 4:48pm up 49 day(s), 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n";
317     # 0 mins
318     #my($uptime) = " 4:48pm up 49 day(s), 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n";
319     # 0 days and 0 mins
320     #my($uptime) = " 4:48pm up 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n";
321     # 0 days and 0 hours
322     #my($uptime) = " 4:48pm up 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n";
323     # 0 hours and 0 mins
324     #my($uptime) = " 4:48pm up 49 day(s), 201 users, load average: 0.33, 0.35, 0.38\n";
325    
326 tdb 1.21 # grab the uptime
327 tdb 1.17 my($uptime) = `$uptimebin`;
328 pjm2 1.29
329     &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
330     &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
331     &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
332 tdb 1.21
333     # work out the days, hours, and minutes
334 tdb 1.28
335     if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
336     # normal
337 tdb 1.34 $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
338 tdb 1.28 $uptime = "$1:$2:$3";
339     }
340     else {
341     if ($uptime =~ /day/) {
342     if ($uptime =~ /hr/) {
343     # 0 minutes
344 tdb 1.34 $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
345 tdb 1.28 $uptime = "$1:$2:0";
346     }
347     elsif ($uptime =~ /min/) {
348     # 0 hours
349 tdb 1.34 $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
350 tdb 1.28 $uptime = "$1:0:$2";
351     }
352     else {
353     # 0 hours and 0 mins
354     $uptime =~ /up\s+([0-9]+)/;
355     $uptime = "$1:0:0";
356     }
357 tdb 1.27 }
358 tdb 1.28 elsif ($uptime =~ /hr/) {
359 tdb 1.27 # 0 days and 0 minutes
360     $uptime =~ /up\s+([0-9]+)\s+/;
361     $uptime = "0:$1:0";
362     }
363 tdb 1.28 elsif ($uptime =~ /min/) {
364 tdb 1.27 # 0 days and 0 hours
365     $uptime =~ /up\s+([0-9]+)\s+/;
366     $uptime = "0:0:$1";
367     }
368     else {
369 tdb 1.28 # 0 days
370     $uptime =~ /up\s+([0-9]+):([0-9]+)/;
371     $uptime = "0:$1:$2";
372 tdb 1.27 }
373 tdb 1.21 }
374    
375 tdb 1.28 # turn into seconds
376 tdb 1.21 $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
377 tdb 1.28 $uptime = ($3+($2+($1*24))*60)*60;
378    
379     # print the value out
380 pjm2 1.25 &print_pair("unknown", "packet.os.uptime", $uptime);
381 tdb 1.17
382 pjm2 1.18 }