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.7 by pjm2, Mon Jan 22 16:28:57 2001 UTC vs.
Revision 1.39 by tdb, Mon Mar 19 22:38:20 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);
29 + my($unamebin); my($uptimebin); my($sysctlbin);
30 +
31 + if ($ostype eq "SunOS") {
32 +    # covers: Solaris 8
33 +    $topbin = "/usr/local/sbin/top -d2 -s1 0";
34 +    $dfbin = "/usr/bin/df";
35 +    $usersbin = "/usr/ucb/users";
36 +    $unamebin = "/usr/bin/uname";
37 +    $uptimebin = "/usr/bin/uptime";
38 + }
39 + elsif ($ostype eq "Linux") {
40 +    # covers: Debian r2.2
41 +    $topbin = "/usr/bin/top -d1 -n0 -b";
42 +    $dfbin = "/bin/df";
43 +    $usersbin = "/usr/bin/users";
44 +    $unamebin = "/bin/uname";
45 +    $uptimebin = "/usr/bin/uptime";
46 + }
47 + elsif ($ostype eq "FreeBSD") {
48 +    # covers: FreeBSD 4.2-STABLE
49 +    $topbin = "/usr/bin/top -d2 -s1 0";
50 +    $dfbin = "/bin/df";
51 +    $usersbin = "/usr/bin/users";
52 +    $unamebin = "/usr/bin/uname";
53 +    $uptimebin = "/usr/bin/uptime";
54 +    $sysctlbin = "/sbin/sysctl";
55 + }
56 + else {
57 +    print "statgrab.pl Error: Unable to identify system type - \"$ostype\".\n";
58 +    print "\"uname -s\" does not report one of the following known types;\n";
59 +    print "      SunOS, Linux, FreeBSD\n";
60 +    exit(1);
61 + }
62 +
63   # Run the following components: -
64   &print_ident();
65   &include_osver();
66 + &include_uptime();
67   &include_users();
68   &include_top();
69   &include_disk();
# Line 34 | Line 74 | exit(0);
74  
75  
76  
37
38
39
40
77   # prints out an identifier for this version of statgrab.pl
78   # the host should check this when reading data
79   # means the host must be checked and updated to work with newer versions.
# Line 48 | Line 84 | sub print_ident() {
84  
85   # sub to print pairs of data, separated by a single space character.
86   # If the second argument is undefined, then the pair is still printed,
87 < # however, the value shall be displayed as the string "unknown".
88 < sub print_pair($$) {
89 <    my($name, $value) = @_;
87 > # however, the value shall be displayed as the the 'default' value
88 > # if the passed value was undefined.
89 > sub print_pair($$$) {
90 >    my($default, $name, $value) = @_;
91  
92      if (!defined $value) {
93 <        $value = "unknown";
93 >        $value = $default;
94      }
95  
96      # Remove the trailing linefeed if we've not already done so.
# Line 68 | Line 105 | sub print_pair($$) {
105   sub include_disk() {
106      
107      # Run the df program.
108 <    my(@df) = `df -ak`;
108 >    my(@df) = `$dfbin -ak`;
109  
110      # Go through each line of the program, looking for each thing we want.
111      my($partition_no) = 0;
112      for (my($i) = 0; $i < $#df; $i++) {
113          my($line) = $df[$i];
114 <        $line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/;
114 >        $line =~ /^([^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/;
115          # $4 will not match unless everything before it does...
116          if (defined $5) {
117              my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5);
118 <            &print_pair("packet.disk.p$partition_no.attributes.name", $filesystem);
119 <            &print_pair("packet.disk.p$partition_no.attributes.kbytes", $kbytes);
120 <            &print_pair("packet.disk.p$partition_no.attributes.used", $used);
121 <            &print_pair("packet.disk.p$partition_no.attributes.avail", $avail);
122 <            &print_pair("packet.disk.p$partition_no.attributes.mount", $mount);
118 >            &print_pair("unknown", "packet.disk.p$partition_no.attributes.name", $filesystem);
119 >            &print_pair(0, "packet.disk.p$partition_no.attributes.kbytes", $kbytes);
120 >            &print_pair(0, "packet.disk.p$partition_no.attributes.used", $used);
121 >            &print_pair(0, "packet.disk.p$partition_no.attributes.avail", $avail);
122 >            &print_pair("unknown", "packet.disk.p$partition_no.attributes.mount", $mount);
123              ++$partition_no;
124          }
125      }
# Line 94 | Line 131 | sub include_disk() {
131   sub include_users() {
132  
133      # Find out all users on this machine.
134 <    my($users) = `users`;
135 <    my(@users) = split(/\s+/, $users);
134 >    my($users) = `$usersbin`;
135 >    $users = "\n" unless defined $users;
136 >    chop $users;
137 >    my($users_count) = 0;
138 >    $users_count++ while $users =~ /\w+/g;
139 >    my($users_list) = $users." ";
140  
141 <    my($users_count) = $#users + 1;
142 <    my($users_list) = $users;
102 <
103 <    &print_pair("packet.users.count", $users_count);
104 <    &print_pair("packet.users.list", $users_list);
141 >    &print_pair(0, "packet.users.count", $users_count);
142 >    &print_pair("unknown", "packet.users.list", $users_list);
143   }
144  
145  
# Line 110 | Line 148 | sub include_users() {
148   sub include_top() {
149  
150      # Find out some numbers from top.
151 <    my(@top) = `top -d2 -s1 0`;
151 >    my(@top) = `$topbin`;
152      my($top) = join(" ", @top);
153      $top =~ s/\n//g;
154  
155 <    &print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/);
156 <    &print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/);
157 <    &print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/);
158 <    &print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/);
159 <    &print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
160 <    &print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
161 <    &print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
162 <    &print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/);
163 <    &print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
164 <    &print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/);
165 <    &print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
166 <    &print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
167 <    &print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
168 <    &print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/);
169 <    &print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/);
170 <    &print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/);
171 <    &print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/);
172 <    
155 >    if($ostype eq "SunOS") {
156 >        &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
157 >        &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
158 >        &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
159 >        &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
160 >        &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/);
161 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
162 >        &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
163 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
164 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
165 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
166 >        
167 >        # The following need to be specified in megabytes.
168 >        # If they are preceeded by a G, then multiply by 1024.  
169 >        
170 >        $top =~ /([^\s]+?)([KMG]) real/;
171 >        my($real) = $1;
172 >        $real*=1024 if $2 eq "G";
173 >        $real/=1024 if $2 eq "K";
174 >        &print_pair(0, "packet.memory.total", $real);
175 >        
176 >        $top =~ /([^\s]+?)([KMG]) free/;
177 >        my($free) = $1;
178 >        $free*=1024 if $2 eq "G";
179 >        $free/=1024 if $2 eq "K";
180 >        &print_pair(0, "packet.memory.free", $free);
181 >        
182 >        $top =~ /([^\s]+?)([KMG]) swap in use/;
183 >        my($swap_in_use) = $1;
184 >        $swap_in_use*=1024 if $2 eq "G";
185 >        $swap_in_use/=1024 if $2 eq "K";
186 >        # DO NOT print this one out... save it for in a moment...
187 >        
188 >        $top =~ /([^\s]+?)([KMG]) swap free/;
189 >        my($swap_free) = $1;
190 >        $swap_free*=1024 if $2 eq "G";
191 >        $swap_free/=1024 if $2 eq "K";
192 >        &print_pair(0, "packet.swap.free", $swap_free);
193 >        
194 >        # AJ requested total swap instead of swap_in_use, so here we go!
195 >        &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use);
196 >    }
197 >    elsif ($ostype eq "FreeBSD") {
198 >        &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
199 >        &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
200 >        &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
201 >        &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
202 >        &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/);
203 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
204 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
205 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
206 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
207 >
208 >        # FreeBSD is a bit different, we need to get user and nice.
209 >        my($user) = 0;
210 >        if($top =~ /([^\s]+?)% user/) { $user += $1; }
211 >        if($top =~ /([^\s]+?)% nice/) { $user += $1; }
212 >        &print_pair(0, "packet.cpu.user", $user);
213 >        
214 >        # The following need to be specified in megabytes.
215 >        # If they are preceeded by a G, then multiply by 1024.  
216 >        
217 >        # get RAM slightly differently
218 >        my($real) = `$sysctlbin -n hw.physmem`;
219 >        my($free) = $real - `$sysctlbin -n hw.usermem`;
220 >        
221 >        # turn bytes to megabytes
222 >        $real = ($real / 1024) / 1024;
223 >        $free = ($free / 1024) / 1024;
224 >        
225 >        &print_pair(0, "packet.memory.total", $real);
226 >        &print_pair(0, "packet.memory.free", $free);
227 >        
228 >        $top =~ /Swap: ([^\s]+?)([KMG]) Total/;
229 >        my($swap_total) = $1;
230 >        $swap_total*=1024 if $2 eq "G";
231 >        $swap_total/=1024 if $2 eq "K";
232 >        &print_pair(0, "packet.swap.total", $swap_total);
233 >        
234 >        $top =~ /Swap:.*, ([^\s]+?)([KMG]) Free/;
235 >        my($swap_free) = $1;
236 >        $swap_free*=1024 if $2 eq "G";
237 >        $swap_free/=1024 if $2 eq "K";
238 >        &print_pair(0, "packet.swap.free", $swap_free);
239 >        
240 >        my($loads) = `$sysctlbin -n vm.loadavg`;
241 >        $loads =~ /\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+?)\s+/;
242 >        &print_pair(0, "packet.load.load1", $1);
243 >        &print_pair(0, "packet.load.load5", $2);
244 >        &print_pair(0, "packet.load.load15", $3);
245 >    }
246 >    elsif ($ostype eq "Linux") {
247 >        &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
248 >        &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
249 >        &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
250 >        &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
251 >        &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/);
252 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
253 >        &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
254 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
255 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
256 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
257 >        
258 >        # The following need to be specified in megabytes.
259 >        # If they are preceeded by a G, then multiply by 1024.  
260 >        
261 >        $top =~ / ([^\s]+?)([KMG]) av/;
262 >        my($real) = $1;
263 >        $real*=1024 if $2 eq "G";
264 >        $real/=1024 if $2 eq "K";
265 >        &print_pair(0, "packet.memory.total", int($real));
266 >        
267 >        $top =~ / ([^\s]+?)([KMG]) free/;
268 >        my($free) = $1;
269 >        $free*=1024 if $2 eq "G";
270 >        $free/=1024 if $2 eq "K";
271 >        &print_pair(0, "packet.memory.free", int($free));
272 >        
273 >        $top =~ /Swap:\s+([^\s]+?)([KMG]) av/;
274 >        my($swap_total) = $1;
275 >        $swap_total*=1024 if $2 eq "G";
276 >        $swap_total/=1024 if $2 eq "K";
277 >        &print_pair(0, "packet.swap.total", int($swap_total));
278 >        
279 >        $top =~ /Swap:.*, ([^\s]+?)([KMG]) free/;
280 >        my($swap_free) = $1;
281 >        $swap_free*=1024 if $2 eq "G";
282 >        $swap_free/=1024 if $2 eq "K";
283 >        &print_pair(0, "packet.swap.free", int($swap_free));
284 >    }
285 >    else {
286 >        # we could have some catchall here
287 >        # but as it stands this means we'll just skip top stuff
288 >        # for unknown systems
289 >    }
290   }
291  
292   # sub to get details of the machine's operating system.
# Line 140 | Line 295 | sub include_osver() {
295      # Find out details about the operating system
296      # If these values remain undefined, then the print_pair
297      # function shall show the value to be the string "unknown".
298 <    my($os_name) = `uname -s`;
299 <    my($os_release) = `uname -r`;
300 <    my($os_platform) = `uname -m`;
301 <    my($os_sysname) = `uname -n`;
302 <    my($os_version) = `uname -v`;
298 >    my($os_name) = `$unamebin -s`;
299 >    my($os_release) = `$unamebin -r`;
300 >    my($os_platform) = `$unamebin -m`;
301 >    my($os_sysname) = `$unamebin -n`;
302 >    my($os_version) = `$unamebin -v`;
303  
304 <    &print_pair("packet.os.name", $os_name);
305 <    &print_pair("packet.os.release", $os_release);
306 <    &print_pair("packet.os.platform", $os_platform);
307 <    &print_pair("packet.os.sysname", $os_sysname);
308 <    &print_pair("packet.os.version", $os_version);
304 >    &print_pair("unknown", "packet.os.name", $os_name);
305 >    &print_pair("unknown", "packet.os.release", $os_release);
306 >    &print_pair("unknown", "packet.os.platform", $os_platform);
307 >    &print_pair("unknown", "packet.os.sysname", $os_sysname);
308 >    &print_pair("unknown", "packet.os.version", $os_version);
309 >
310 > }
311 >
312 > # sub to get system uptime in seconds.
313 > sub include_uptime() {
314 >
315 >    # debug stuff, all the different cases
316 >
317 >    # normal
318 >    #my($uptime) = "  4:48pm  up 49 day(s),  6:30,  201 users,  load average: 0.33, 0.35, 0.38\n";
319 >    # 0 days
320 >    #my($uptime) = "  4:48pm  up 6:30,  201 users,  load average: 0.33, 0.35, 0.38\n";
321 >    # 0 hours
322 >    #my($uptime) = "  4:48pm  up 49 day(s),  30 min(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
323 >    # 0 mins
324 >    #my($uptime) = "  4:48pm  up 49 day(s),  6 hr(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
325 >    # 0 days and 0 mins
326 >    #my($uptime) = "  4:48pm  up 6 hr(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
327 >    # 0 days and 0 hours
328 >    #my($uptime) = "  4:48pm  up 30 min(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
329 >    # 0 hours and 0 mins
330 >    #my($uptime) = "  4:48pm  up 49 day(s), 201 users,  load average: 0.33, 0.35, 0.38\n";
331 >
332 >    # grab the uptime
333 >    my($uptime) = `$uptimebin`;
334 >    
335 >    if($ostype ne "FreeBSD") {
336 >        &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
337 >        &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
338 >        &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
339 >    }
340 >
341 >    # work out the days, hours, and minutes
342 >
343 >    if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
344 >      # normal
345 >        $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
346 >        $uptime = "$1:$2:$3";
347 >    }
348 >    else {
349 >        if ($uptime =~ /day/) {
350 >            if ($uptime =~ /hr/) {
351 >              # 0 minutes
352 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
353 >                $uptime = "$1:$2:0";
354 >            }
355 >            elsif ($uptime =~ /min/) {
356 >              # 0 hours
357 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
358 >                $uptime = "$1:0:$2";
359 >            }
360 >            else {
361 >              # 0 hours and 0 mins
362 >                $uptime =~ /up\s+([0-9]+)/;
363 >                $uptime = "$1:0:0";
364 >            }
365 >        }
366 >        elsif ($uptime =~ /hr/) {
367 >          # 0 days and 0 minutes
368 >            $uptime =~ /up\s+([0-9]+)\s+/;
369 >            $uptime = "0:$1:0";
370 >        }
371 >        elsif ($uptime =~ /min/) {
372 >          # 0 days and 0 hours
373 >            $uptime =~ /up\s+([0-9]+)\s+/;
374 >            $uptime = "0:0:$1";
375 >        }
376 >        else {
377 >          # 0 days
378 >            $uptime =~ /up\s+([0-9]+):([0-9]+)/;
379 >            $uptime = "0:$1:$2";
380 >        }
381 >    }
382 >
383 >    # turn into seconds
384 >    $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
385 >    $uptime = ($3+($2+($1*24))*60)*60;
386 >    
387 >    # print the value out
388 >    &print_pair("unknown", "packet.os.uptime", $uptime);
389  
390   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines