ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/generic/statgrab.pl
Revision: 1.27
Committed: Mon Feb 5 17:21:04 2001 UTC (23 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.26: +53 -9 lines
Log Message:
The uptimes section now works, and accounts for every permutation. The only one
we have been unable to verify is if the uptime is < 1min.
There is some debugging code at the top, this allows each permutation to easily
be checked.
This is not efficient, it needs to be reordered to make the most common case
come first.

File Contents

# User Rev Content
1 pjm2 1.1 #!/usr/bin/perl -w
2    
3     #-----------------------------------------------------------------
4     # Machine statistics grabber
5 pjm2 1.24 # $Author: pjm2 $
6 tdb 1.27 # $Id: statgrab.pl,v 1.26 2001/02/05 17:13:26 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 pjm2 1.18 # Paths
25 tdb 1.13 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 tdb 1.17 my($uptimebin) = "/usr/bin/uptime";
30 tdb 1.13
31 pjm2 1.6 # Run the following components: -
32 tdb 1.3 &print_ident();
33 tdb 1.2 &include_osver();
34 tdb 1.17 &include_uptime();
35 pjm2 1.1 &include_users();
36     &include_top();
37 pjm2 1.5 &include_disk();
38 pjm2 1.1
39 pjm2 1.6 # End the program normally.
40 pjm2 1.1 exit(0);
41    
42    
43    
44 pjm2 1.6
45    
46    
47    
48    
49 tdb 1.3 # prints out an identifier for this version of statgrab.pl
50     # the host should check this when reading data
51     # means the host must be checked and updated to work with newer versions.
52     sub print_ident() {
53 tdb 1.27 print 'version statgrab.pl $Revision: 1.26 $';
54 tdb 1.3 print "\n";
55     }
56 pjm2 1.1
57     # sub to print pairs of data, separated by a single space character.
58 pjm2 1.5 # If the second argument is undefined, then the pair is still printed,
59 pjm2 1.25 # however, the value shall be displayed as the the 'default' value
60     # if the passed value was undefined.
61 pjm2 1.8 sub print_pair($$$) {
62 pjm2 1.25 my($default, $name, $value) = @_;
63 pjm2 1.1
64     if (!defined $value) {
65 pjm2 1.25 $value = $default;
66 pjm2 1.1 }
67    
68     # Remove the trailing linefeed if we've not already done so.
69     chomp($value);
70    
71     # print the pair of data with a space inbetween.
72     print "$name $value\n";
73     }
74    
75    
76 pjm2 1.5 # sub to find out disk partition information, if it exists.
77     sub include_disk() {
78    
79     # Run the df program.
80 tdb 1.13 my(@df) = `$dfbin -ak`;
81 pjm2 1.5
82     # Go through each line of the program, looking for each thing we want.
83     my($partition_no) = 0;
84     for (my($i) = 0; $i < $#df; $i++) {
85     my($line) = $df[$i];
86 pjm2 1.7 $line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/;
87 pjm2 1.5 # $4 will not match unless everything before it does...
88 pjm2 1.7 if (defined $5) {
89     my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5);
90 pjm2 1.25 &print_pair("unknown", "packet.disk.p$partition_no.attributes.name", $filesystem);
91     &print_pair(0, "packet.disk.p$partition_no.attributes.kbytes", $kbytes);
92     &print_pair(0, "packet.disk.p$partition_no.attributes.used", $used);
93     &print_pair(0, "packet.disk.p$partition_no.attributes.avail", $avail);
94     &print_pair("unknown", "packet.disk.p$partition_no.attributes.mount", $mount);
95 pjm2 1.5 ++$partition_no;
96     }
97     }
98    
99     }
100    
101     # sub to find out the list of all (non-unique) usernames logged
102     # in to the machine and how many their are. (not
103 pjm2 1.1 sub include_users() {
104    
105     # Find out all users on this machine.
106 tdb 1.13 my($users) = `$usersbin`;
107 pjm2 1.16 $users = "\n" unless defined $users;
108     chop $users;
109 pjm2 1.14 my($users_count) = 0;
110     $users_count++ while $users =~ /\w+/g;
111 pjm2 1.18 my($users_list) = $users." ";
112 pjm2 1.1
113 pjm2 1.25 &print_pair(0, "packet.users.count", $users_count);
114     &print_pair("unknown", "packet.users.list", $users_list);
115 pjm2 1.1 }
116    
117    
118 pjm2 1.5 # sub to run a series of regexps on the output of 'top' to
119     # gather various machine statistics.
120 pjm2 1.1 sub include_top() {
121    
122     # Find out some numbers from top.
123 tdb 1.13 my(@top) = `$topbin -d2 -s1 0`;
124 pjm2 1.1 my($top) = join(" ", @top);
125     $top =~ s/\n//g;
126    
127 pjm2 1.25 &print_pair(0, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/);
128     &print_pair(0, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/);
129     &print_pair(0, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s/);
130     &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
131     &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
132     &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
133     &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
134     &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/);
135     &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
136     &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
137     &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
138     &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
139     &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
140 pjm2 1.10
141     # The following need to be specified in megabytes.
142     # If they are preceeded by a G, then multiply by 1024.
143    
144     $top =~ /([^\s]+?)([MG]) real/;
145 pjm2 1.11 my($real) = $1;
146     $real*=1024 if $2 eq "G";
147 pjm2 1.26 &print_pair(0, "packet.memory.total", $real);
148 pjm2 1.10
149     $top =~ /([^\s]+?)([MG]) free/;
150 pjm2 1.11 my($free) = $1;
151     $free*=1024 if $2 eq "G";
152 pjm2 1.25 &print_pair(0, "packet.memory.free", $free);
153 pjm2 1.10
154     $top =~ /([^\s]+?)([MG]) swap in use/;
155 pjm2 1.11 my($swap_in_use) = $1;
156     $swap_in_use*=1024 if $2 eq "G";
157 pjm2 1.12 # DO NOT print this one out... save it for in a moment...
158 pjm2 1.10
159     $top =~ /([^\s]+?)([MG]) swap free/;
160 pjm2 1.11 my($swap_free) = $1;
161     $swap_free*=1024 if $2 eq "G";
162 pjm2 1.26 &print_pair(0, "packet.swap.free", $swap_free);
163 pjm2 1.12
164     # AJ requested total swap instead of swap_in_use, so here we go!
165 pjm2 1.26 &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use);
166 tdb 1.2 }
167    
168 pjm2 1.5 # sub to get details of the machine's operating system.
169 tdb 1.2 sub include_osver() {
170    
171     # Find out details about the operating system
172 pjm2 1.5 # If these values remain undefined, then the print_pair
173     # function shall show the value to be the string "unknown".
174 tdb 1.13 my($os_name) = `$unamebin -s`;
175     my($os_release) = `$unamebin -r`;
176     my($os_platform) = `$unamebin -m`;
177     my($os_sysname) = `$unamebin -n`;
178     my($os_version) = `$unamebin -v`;
179 tdb 1.2
180 pjm2 1.25 &print_pair("unknown", "packet.os.name", $os_name);
181     &print_pair("unknown", "packet.os.release", $os_release);
182     &print_pair("unknown", "packet.os.platform", $os_platform);
183     &print_pair("unknown", "packet.os.sysname", $os_sysname);
184     &print_pair("unknown", "packet.os.version", $os_version);
185 tdb 1.2
186 pjm2 1.1 }
187 tdb 1.17
188     # sub to get system uptime.
189     sub include_uptime() {
190    
191 tdb 1.27 # debug stuff, all the different cases
192    
193     # normal
194     #my($uptime) = " 4:48pm up 49 day(s), 6:30, 201 users, load average: 0.33, 0.35, 0.38\n";
195     # 0 days
196     #my($uptime) = " 4:48pm up 6:30, 201 users, load average: 0.33, 0.35, 0.38\n";
197     # 0 hours
198     #my($uptime) = " 4:48pm up 49 day(s), 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n";
199     # 0 mins
200     #my($uptime) = " 4:48pm up 49 day(s), 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n";
201     # 0 days and 0 mins
202     #my($uptime) = " 4:48pm up 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n";
203     # 0 days and 0 hours
204     #my($uptime) = " 4:48pm up 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n";
205     # 0 hours and 0 mins
206     #my($uptime) = " 4:48pm up 49 day(s), 201 users, load average: 0.33, 0.35, 0.38\n";
207    
208 tdb 1.21 # grab the uptime
209 tdb 1.17 my($uptime) = `$uptimebin`;
210 tdb 1.21
211     # work out the days, hours, and minutes
212     if ($uptime =~ /hr/) {
213 tdb 1.27 # two possible cases here
214     if($uptime =~ /day/) {
215     # 0 minutes
216     $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/;
217     $uptime = "$1:$2:0";
218     }
219     else {
220     # 0 days and 0 minutes
221     $uptime =~ /up\s+([0-9]+)\s+/;
222     $uptime = "0:$1:0";
223     }
224 tdb 1.21 }
225     elsif ($uptime =~ /min/) {
226 tdb 1.27 # two possible cases here
227     if($uptime =~ /day/) {
228     # 0 hours
229     $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/;
230     $uptime = "$1:0:$2";
231     }
232     else {
233     # 0 days and 0 hours
234     $uptime =~ /up\s+([0-9]+)\s+/;
235     $uptime = "0:0:$1";
236     }
237 tdb 1.21 }
238     elsif ($uptime =~ /day/) {
239 tdb 1.27 if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
240     # normal
241     $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+):([0-9]+)/;
242     $uptime = "$1:$2:$3";
243     }
244     else {
245     # 0 hours and 0 mins
246     $uptime =~ /up\s+([0-9]+)/;
247     $uptime = "$1:0:0";
248     }
249 tdb 1.21 }
250     else {
251     # 0 days
252 tdb 1.27 $uptime =~ /up\s+([0-9]+):([0-9]+)/;
253     $uptime = "0:$1:$2";
254 tdb 1.21 }
255    
256     # turn into minutes
257    
258     $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
259     $uptime = $3 + ($2 + $1*24)*60;
260 tdb 1.17
261 pjm2 1.25 &print_pair("unknown", "packet.os.uptime", $uptime);
262 tdb 1.17
263 pjm2 1.18 }