ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/generic/statgrab.pl
(Generate patch)

Comparing projects/cms/source/host/generic/statgrab.pl (file contents):
Revision 1.3 by tdb, Sat Jan 20 20:04:46 2001 UTC vs.
Revision 1.36 by tdb, Sat Mar 3 21:26:55 2001 UTC

# Line 21 | Line 21 | $| = 1;
21   # You'd be silly not to use this ;)
22   use strict;
23  
24 + # 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 +    $topbin = "/usr/local/sbin/top -d2 -s1 0";
33 +    $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 +    $topbin = "/usr/bin/top -d1 -n0 -b";
41 +    $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 +    $topbin = "/usr/bin/top -d2 -s1 0";
49 +    $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 +
61 + # Run the following components: -
62   &print_ident();
63   &include_osver();
64 + &include_uptime();
65   &include_users();
66   &include_top();
67 + &include_disk();
68  
69 + # End the program normally.
70   exit(0);
71  
72  
73  
74 +
75   # 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 <    print '# statgrab.pl $Revision$';
79 >    print 'version statgrab.pl $Revision$';
80      print "\n";
81   }
82  
83   # sub to print pairs of data, separated by a single space character.
84 < sub print_pair($$) {
85 <    my($name, $value) = @_;
84 > # If the second argument is undefined, then the pair is still printed,
85 > # however, the value shall be displayed as the the 'default' value
86 > # if the passed value was undefined.
87 > sub print_pair($$$) {
88 >    my($default, $name, $value) = @_;
89  
90      if (!defined $value) {
91 <        $value = "unknown";
91 >        $value = $default;
92      }
93  
94      # Remove the trailing linefeed if we've not already done so.
# Line 54 | Line 99 | sub print_pair($$) {
99   }
100  
101  
102 + # sub to find out disk partition information, if it exists.
103 + sub include_disk() {
104 +    
105 +    # Run the df program.
106 +    my(@df) = `$dfbin -ak`;
107 +
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 +        $line =~ /^([^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/;
113 +        # $4 will not match unless everything before it does...
114 +        if (defined $5) {
115 +            my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5);
116 +            &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 +            ++$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   sub include_users() {
130  
131      # Find out all users on this machine.
132 <    my($users) = `users`;
133 <    my(@users) = split(/\s+/, $users);
132 >    my($users) = `$usersbin`;
133 >    $users = "\n" unless defined $users;
134 >    chop $users;
135 >    my($users_count) = 0;
136 >    $users_count++ while $users =~ /\w+/g;
137 >    my($users_list) = $users." ";
138  
139 <    my($users_count) = $#users + 1;
140 <    my($users_list) = $users;
65 <
66 <    &print_pair("packet.users.count", $users_count);
67 <    &print_pair("packet.users.list", $users_list);
139 >    &print_pair(0, "packet.users.count", $users_count);
140 >    &print_pair("unknown", "packet.users.list", $users_list);
141   }
142  
143  
144 + # sub to run a series of regexps on the output of 'top' to
145 + # gather various machine statistics.
146   sub include_top() {
147  
148      # Find out some numbers from top.
149 <    my(@top) = `top -d2 -s1 0`;
149 >    my(@top) = `$topbin`;
150      my($top) = join(" ", @top);
151      $top =~ s/\n//g;
152  
153 <    &print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/);
154 <    &print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/);
155 <    &print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/);
156 <    &print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/);
157 <    &print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
158 <    &print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
159 <    &print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
160 <    &print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/);
161 <    &print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
162 <    &print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/);
163 <    &print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
164 <    &print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
165 <    &print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
166 <    &print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/);
167 <    &print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/);
168 <    &print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/);
169 <    &print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/);
170 <    
153 >    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 >        $top =~ /([^\s]+?)([MG]) real/;
169 >        my($real) = $1;
170 >        $real*=1024 if $2 eq "G";
171 >        &print_pair(0, "packet.memory.total", $real);
172 >        
173 >        $top =~ /([^\s]+?)([MG]) free/;
174 >        my($free) = $1;
175 >        $free*=1024 if $2 eq "G";
176 >        &print_pair(0, "packet.memory.free", $free);
177 >        
178 >        $top =~ /([^\s]+?)([MG]) swap in use/;
179 >        my($swap_in_use) = $1;
180 >        $swap_in_use*=1024 if $2 eq "G";
181 >        # DO NOT print this one out... save it for in a moment...
182 >        
183 >        $top =~ /([^\s]+?)([MG]) swap free/;
184 >        my($swap_free) = $1;
185 >        $swap_free*=1024 if $2 eq "G";
186 >        &print_pair(0, "packet.swap.free", $swap_free);
187 >        
188 >        # AJ requested total swap instead of swap_in_use, so here we go!
189 >        &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use);
190 >    }
191 >    elsif ($ostype eq "FreeBSD") {
192 >        &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
193 >        &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
194 >        &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
195 >        &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
196 >        &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/);
197 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
198 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
199 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
200 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
201 >
202 >        # FreeBSD is a bit different, we need to get user and nice.
203 >        my($user) = 0;
204 >        if($top =~ /([^\s]+?)% user/) { $user += $1; }
205 >        if($top =~ /([^\s]+?)% nice/) { $user += $1; }
206 >        &print_pair(0, "packet.cpu.user", $user);
207 >        
208 >        # The following need to be specified in megabytes.
209 >        # If they are preceeded by a G, then multiply by 1024.  
210 >        
211 >        # !! Can't get the total memory !!
212 >        #$top =~ /([^\s]+?)([MG]) real/;
213 >        #my($real) = $1;
214 >        #$real*=1024 if $2 eq "G";
215 >        #&print_pair(0, "packet.memory.total", $real);
216 >        &print_pair(0, "packet.memory.total", 0);
217 >        
218 >        $top =~ /([^\s]+?)([MG]) Free/;
219 >        my($free) = $1;
220 >        $free*=1024 if $2 eq "G";
221 >        &print_pair(0, "packet.memory.free", $free);
222 >        
223 >        $top =~ /Swap: ([^\s]+?)([MG]) Total/;
224 >        my($swap_total) = $1;
225 >        $swap_total*=1024 if $2 eq "G";
226 >        &print_pair(0, "packet.swap.total", $swap_total);
227 >        
228 >        $top =~ /Swap:.*, ([^\s]+?)([MG]) Free/;
229 >        my($swap_free) = $1;
230 >        $swap_free*=1024 if $2 eq "G";
231 >        &print_pair(0, "packet.swap.free", $swap_free);
232 >    }
233 >    elsif ($ostype eq "Linux") {
234 >        &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
235 >        &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
236 >        &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
237 >        &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
238 >        &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/);
239 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
240 >        &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
241 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
242 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
243 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
244 >        
245 >        # The following need to be specified in megabytes.
246 >        # If they are preceeded by a G, then multiply by 1024.  
247 >        
248 >        $top =~ / ([^\s]+?)([KMG]) av/;
249 >        my($real) = $1;
250 >        $real*=1024 if $2 eq "G";
251 >        $real/=1024 if $2 eq "K";
252 >        &print_pair(0, "packet.memory.total", int($real));
253 >        
254 >        $top =~ / ([^\s]+?)([KMG]) free/;
255 >        my($free) = $1;
256 >        $free*=1024 if $2 eq "G";
257 >        $free/=1024 if $2 eq "K";
258 >        &print_pair(0, "packet.memory.free", int($free));
259 >        
260 >        $top =~ /Swap:\s+([^\s]+?)([KMG]) av/;
261 >        my($swap_total) = $1;
262 >        $swap_total*=1024 if $2 eq "G";
263 >        $swap_total/=1024 if $2 eq "K";
264 >        &print_pair(0, "packet.swap.total", int($swap_total));
265 >        
266 >        $top =~ /Swap:.*, ([^\s]+?)([KMG]) free/;
267 >        my($swap_free) = $1;
268 >        $swap_free*=1024 if $2 eq "G";
269 >        $swap_free/=1024 if $2 eq "K";
270 >        &print_pair(0, "packet.swap.free", int($swap_free));
271 >    }
272 >    else {
273 >        # we could have some catchall here
274 >        # but as it stands this means we'll just skip top stuff
275 >        # for unknown systems
276 >    }
277   }
278  
279 + # sub to get details of the machine's operating system.
280   sub include_osver() {
281  
282      # Find out details about the operating system
283 <    my($os_name) = `uname -s`;
284 <    my($os_release) = `uname -r`;
285 <    my($os_platform) = `uname -m`;
286 <    my($os_sysname) = `uname -n`;
287 <    my($os_version) = `uname -v`;
283 >    # If these values remain undefined, then the print_pair
284 >    # function shall show the value to be the string "unknown".
285 >    my($os_name) = `$unamebin -s`;
286 >    my($os_release) = `$unamebin -r`;
287 >    my($os_platform) = `$unamebin -m`;
288 >    my($os_sysname) = `$unamebin -n`;
289 >    my($os_version) = `$unamebin -v`;
290  
291 <    &print_pair("packet.os.name", $os_name);
292 <    &print_pair("packet.os.release", $os_release);
293 <    &print_pair("packet.os.platform", $os_platform);
294 <    &print_pair("packet.os.sysname", $os_sysname);
295 <    &print_pair("packet.os.version", $os_version);
291 >    &print_pair("unknown", "packet.os.name", $os_name);
292 >    &print_pair("unknown", "packet.os.release", $os_release);
293 >    &print_pair("unknown", "packet.os.platform", $os_platform);
294 >    &print_pair("unknown", "packet.os.sysname", $os_sysname);
295 >    &print_pair("unknown", "packet.os.version", $os_version);
296 >
297 > }
298 >
299 > # sub to get system uptime in seconds.
300 > sub include_uptime() {
301 >
302 >    # debug stuff, all the different cases
303 >
304 >    # normal
305 >    #my($uptime) = "  4:48pm  up 49 day(s),  6:30,  201 users,  load average: 0.33, 0.35, 0.38\n";
306 >    # 0 days
307 >    #my($uptime) = "  4:48pm  up 6:30,  201 users,  load average: 0.33, 0.35, 0.38\n";
308 >    # 0 hours
309 >    #my($uptime) = "  4:48pm  up 49 day(s),  30 min(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
310 >    # 0 mins
311 >    #my($uptime) = "  4:48pm  up 49 day(s),  6 hr(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
312 >    # 0 days and 0 mins
313 >    #my($uptime) = "  4:48pm  up 6 hr(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
314 >    # 0 days and 0 hours
315 >    #my($uptime) = "  4:48pm  up 30 min(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
316 >    # 0 hours and 0 mins
317 >    #my($uptime) = "  4:48pm  up 49 day(s), 201 users,  load average: 0.33, 0.35, 0.38\n";
318 >
319 >    # grab the uptime
320 >    my($uptime) = `$uptimebin`;
321 >    
322 >    &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
323 >    &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
324 >    &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
325 >
326 >    # work out the days, hours, and minutes
327 >
328 >    if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
329 >      # normal
330 >        $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
331 >        $uptime = "$1:$2:$3";
332 >    }
333 >    else {
334 >        if ($uptime =~ /day/) {
335 >            if ($uptime =~ /hr/) {
336 >              # 0 minutes
337 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
338 >                $uptime = "$1:$2:0";
339 >            }
340 >            elsif ($uptime =~ /min/) {
341 >              # 0 hours
342 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
343 >                $uptime = "$1:0:$2";
344 >            }
345 >            else {
346 >              # 0 hours and 0 mins
347 >                $uptime =~ /up\s+([0-9]+)/;
348 >                $uptime = "$1:0:0";
349 >            }
350 >        }
351 >        elsif ($uptime =~ /hr/) {
352 >          # 0 days and 0 minutes
353 >            $uptime =~ /up\s+([0-9]+)\s+/;
354 >            $uptime = "0:$1:0";
355 >        }
356 >        elsif ($uptime =~ /min/) {
357 >          # 0 days and 0 hours
358 >            $uptime =~ /up\s+([0-9]+)\s+/;
359 >            $uptime = "0:0:$1";
360 >        }
361 >        else {
362 >          # 0 days
363 >            $uptime =~ /up\s+([0-9]+):([0-9]+)/;
364 >            $uptime = "0:$1:$2";
365 >        }
366 >    }
367 >
368 >    # turn into seconds
369 >    $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
370 >    $uptime = ($3+($2+($1*24))*60)*60;
371 >    
372 >    # print the value out
373 >    &print_pair("unknown", "packet.os.uptime", $uptime);
374  
375   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines