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.15 by pjm2, Sun Jan 28 19:53:39 2001 UTC vs.
Revision 1.48 by tdb, Tue May 21 16:47:11 2002 UTC

# Line 1 | Line 1
1   #!/usr/bin/perl -w
2  
3 + #
4 + # i-scream central monitoring system
5 + # http://www.i-scream.org.uk
6 + # Copyright (C) 2000-2002 i-scream
7 + #
8 + # This program is free software; you can redistribute it and/or
9 + # modify it under the terms of the GNU General Public License
10 + # as published by the Free Software Foundation; either version 2
11 + # of the License, or (at your option) any later version.
12 + #
13 + # This program is distributed in the hope that it will be useful,
14 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 + # GNU General Public License for more details.
17 + #
18 + # You should have received a copy of the GNU General Public License
19 + # along with this program; if not, write to the Free Software
20 + # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 + #
22 +
23   #-----------------------------------------------------------------
24   # Machine statistics grabber
25   #     $Author$
# Line 21 | Line 41 | $| = 1;
41   # You'd be silly not to use this ;)
42   use strict;
43  
44 < # Path's
45 < my($topbin) = "/usr/local/sbin/top";
26 < my($dfbin) = "/usr/bin/df";
27 < my($usersbin) = "/usr/ucb/users";
28 < my($unamebin) = "/usr/bin/uname";
44 > # Have to hope this will work really.
45 > my($ostype) = `uname -s`; chop($ostype);
46  
47 + # Decide which paths we should use.
48 + my($topbin); my($dfbin); my($usersbin);
49 + my($unamebin); my($uptimebin); my($sysctlbin);
50 +
51 + if ($ostype eq "SunOS") {
52 +    # covers: Solaris 8
53 +    $topbin = "/usr/local/sbin/top -d2 -s1 0";
54 +    $dfbin = "/usr/bin/df";
55 +    $usersbin = "/usr/ucb/users";
56 +    $unamebin = "/usr/bin/uname";
57 +    $uptimebin = "/usr/bin/uptime";
58 + }
59 + elsif ($ostype eq "Linux") {
60 +    # covers: Debian r2.2
61 +    $topbin = "/usr/bin/top -d1 -n2 -b -p0";
62 +    $dfbin = "/bin/df";
63 +    $usersbin = "/usr/bin/users";
64 +    $unamebin = "/bin/uname";
65 +    $uptimebin = "/usr/bin/uptime";
66 + }
67 + elsif ($ostype eq "FreeBSD") {
68 +    # covers: FreeBSD 4.2-STABLE
69 +    $topbin = "/usr/bin/top -d2 -s1 0";
70 +    $dfbin = "/bin/df";
71 +    $usersbin = "/usr/bin/users";
72 +    $unamebin = "/usr/bin/uname";
73 +    $uptimebin = "/usr/bin/uptime";
74 +    $sysctlbin = "/sbin/sysctl";
75 + }
76 + else {
77 +    print "statgrab.pl Error: Unable to identify system type - \"$ostype\".\n";
78 +    print "\"uname -s\" does not report one of the following known types;\n";
79 +    print "      SunOS, Linux, FreeBSD\n";
80 +    exit(1);
81 + }
82 +
83   # Run the following components: -
84   &print_ident();
85   &include_osver();
86 + &include_uptime();
87   &include_users();
88   &include_top();
89   &include_disk();
# Line 40 | Line 94 | exit(0);
94  
95  
96  
43
44
45
46
97   # prints out an identifier for this version of statgrab.pl
98   # the host should check this when reading data
99   # means the host must be checked and updated to work with newer versions.
100   sub print_ident() {
101 <    print 'version statgrab.pl $Revision$';
101 >    print 'packet.version statgrab.pl $Revision$';
102      print "\n";
103   }
104  
105   # sub to print pairs of data, separated by a single space character.
106   # If the second argument is undefined, then the pair is still printed,
107 < # however, the value shall be displayed as the string "unknown".
108 < # If $type is non-zero, then "0" is printed instead of "unknown".
107 > # however, the value shall be displayed as the the 'default' value
108 > # if the passed value was undefined.
109   sub print_pair($$$) {
110 <    my($type, $name, $value) = @_;
110 >    my($default, $name, $value) = @_;
111  
112      if (!defined $value) {
113 <        if ($type) {
64 <            $value = "0.00";
65 <        }
66 <        else {
67 <            $value = "unknown";
68 <        }
113 >        $value = $default;
114      }
115  
116      # Remove the trailing linefeed if we've not already done so.
# Line 80 | Line 125 | sub print_pair($$$) {
125   sub include_disk() {
126      
127      # Run the df program.
128 <    my(@df) = `$dfbin -ak`;
128 >    my(@df) = `$dfbin -akl`;
129  
130      # Go through each line of the program, looking for each thing we want.
131      my($partition_no) = 0;
132      for (my($i) = 0; $i < $#df; $i++) {
133          my($line) = $df[$i];
134 <        $line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/;
134 >        $line =~ /^([^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/;
135          # $4 will not match unless everything before it does...
136          if (defined $5) {
137              my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5);
138 <            &print_pair(0, "packet.disk.p$partition_no.attributes.name", $filesystem);
139 <            &print_pair(1, "packet.disk.p$partition_no.attributes.kbytes", $kbytes);
140 <            &print_pair(1, "packet.disk.p$partition_no.attributes.used", $used);
141 <            &print_pair(1, "packet.disk.p$partition_no.attributes.avail", $avail);
142 <            &print_pair(0, "packet.disk.p$partition_no.attributes.mount", $mount);
138 >            &print_pair("unknown", "packet.disk.p$partition_no.attributes.name", $filesystem);
139 >            &print_pair(0, "packet.disk.p$partition_no.attributes.kbytes", $kbytes);
140 >            &print_pair(0, "packet.disk.p$partition_no.attributes.used", $used);
141 >            &print_pair(0, "packet.disk.p$partition_no.attributes.avail", $avail);
142 >            &print_pair("unknown", "packet.disk.p$partition_no.attributes.mount", $mount);
143              ++$partition_no;
144          }
145      }
# Line 107 | Line 152 | sub include_users() {
152  
153      # Find out all users on this machine.
154      my($users) = `$usersbin`;
155 <    chop($users);
156 <    $users = "" unless defined $users;
155 >    $users = "\n" unless defined $users;
156 >    chop $users;
157      my($users_count) = 0;
158      $users_count++ while $users =~ /\w+/g;
159 <    my($users_list) = $users;
159 >    my($users_list) = $users." ";
160  
161 <    &print_pair(1, "packet.users.count", $users_count);
162 <    &print_pair(0, "packet.users.list", $users_list);
161 >    &print_pair(0, "packet.users.count", $users_count);
162 >    &print_pair("unknown", "packet.users.list", $users_list);
163   }
164  
165  
# Line 123 | Line 168 | sub include_users() {
168   sub include_top() {
169  
170      # Find out some numbers from top.
171 <    my(@top) = `$topbin -d2 -s1 0`;
171 >    my(@top) = `$topbin`;
172      my($top) = join(" ", @top);
173 <    $top =~ s/\n//g;
173 >    $top =~ s/\n/ /g;
174  
175 <    &print_pair(1, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/);
176 <    &print_pair(1, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/);
177 <    &print_pair(1, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/);
178 <    &print_pair(1, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
179 <    &print_pair(1, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
180 <    &print_pair(1, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
181 <    &print_pair(1, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
182 <    &print_pair(1, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/);
183 <    &print_pair(1, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
184 <    &print_pair(1, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
185 <    &print_pair(1, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
186 <    &print_pair(1, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
187 <    &print_pair(1, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
175 >    if($ostype eq "SunOS") {
176 >        &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
177 >        &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
178 >        &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
179 >        &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
180 >        &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*on cpu/);
181 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
182 >        &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
183 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
184 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
185 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
186 >        
187 >        # The following need to be specified in megabytes.
188 >        # If they are preceeded by a G, then multiply by 1024.  
189 >        
190 >        $top =~ /([0-9]+?)([KMG]) real/;
191 >        my($real) = $1;
192 >        $real*=1024 if $2 eq "G";
193 >        $real/=1024 if $2 eq "K";
194 >        &print_pair(0, "packet.memory.total", $real);
195 >        
196 >        $top =~ /([0-9]+?)([KMG]) free/;
197 >        my($free) = $1;
198 >        $free*=1024 if $2 eq "G";
199 >        $free/=1024 if $2 eq "K";
200 >        &print_pair(0, "packet.memory.free", $free);
201 >        
202 >        $top =~ /([0-9]+?)([KMG]) swap in use/;
203 >        my($swap_in_use) = $1;
204 >        $swap_in_use*=1024 if $2 eq "G";
205 >        $swap_in_use/=1024 if $2 eq "K";
206 >        # DO NOT print this one out... save it for in a moment...
207 >        
208 >        $top =~ /([0-9]+?)([KMG]) swap free/;
209 >        my($swap_free) = $1;
210 >        $swap_free*=1024 if $2 eq "G";
211 >        $swap_free/=1024 if $2 eq "K";
212 >        &print_pair(0, "packet.swap.free", $swap_free);
213 >        
214 >        &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use);
215 >    }
216 >    elsif ($ostype eq "FreeBSD") {
217 >        &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
218 >        &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
219 >        &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
220 >        &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
221 >        &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/);
222 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
223 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
224 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
225 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
226  
227 <    # The following need to be specified in megabytes.
228 <    # If they are preceeded by a G, then multiply by 1024.  
227 >        # FreeBSD is a bit different, we need to get user and nice.
228 >        my($user) = 0;
229 >        if($top =~ /([^\s]+?)% user/) { $user += $1; }
230 >        if($top =~ /([^\s]+?)% nice/) { $user += $1; }
231 >        &print_pair(0, "packet.cpu.user", $user);
232 >        
233 >        # The following need to be specified in megabytes.
234 >        # If they are preceeded by a G, then multiply by 1024.  
235 >        
236 >        # get RAM slightly differently
237 >        my($real) = `$sysctlbin -n hw.physmem`;
238 >        my($free) = $real - `$sysctlbin -n hw.usermem`;
239 >        
240 >        # turn bytes to megabytes
241 >        $real = ($real / 1024) / 1024;
242 >        $free = ($free / 1024) / 1024;
243 >        
244 >        &print_pair(0, "packet.memory.total", $real);
245 >        &print_pair(0, "packet.memory.free", $free);
246 >        
247 >        $top =~ /Swap: ([0-9]+?)([KMG]) Total/;
248 >        my($swap_total) = $1;
249 >        $swap_total*=1024 if $2 eq "G";
250 >        $swap_total/=1024 if $2 eq "K";
251 >        &print_pair(0, "packet.swap.total", $swap_total);
252 >        
253 >        $top =~ /Swap:.*, ([0-9]+?)([KMG]) Free/;
254 >        my($swap_free) = $1;
255 >        $swap_free*=1024 if $2 eq "G";
256 >        $swap_free/=1024 if $2 eq "K";
257 >        &print_pair(0, "packet.swap.free", $swap_free);
258 >        
259 >        my($loads) = `$sysctlbin -n vm.loadavg`;
260 >        $loads =~ /\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+?)\s+/;
261 >        &print_pair(0, "packet.load.load1", $1);
262 >        &print_pair(0, "packet.load.load5", $2);
263 >        &print_pair(0, "packet.load.load15", $3);
264 >    }
265 >    elsif ($ostype eq "Linux") {
266 >        my ($top) = "";
267 >        foreach my $line (@top) {
268 >            $top = $line . $top;
269 >        }
270 >        $top =~ s/\n/ /g;
271  
272 <    $top =~ /([^\s]+?)([MG]) real/;
273 <    my($real) = $1;
274 <    $real*=1024 if $2 eq "G";
275 <    &print_pair(1, "packet.memory.real", $real);
272 >        &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
273 >        &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
274 >        &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
275 >        &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
276 >        &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/);
277 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
278 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
279 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
280 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
281  
282 <    $top =~ /([^\s]+?)([MG]) free/;
283 <    my($free) = $1;
284 <    $free*=1024 if $2 eq "G";
285 <    &print_pair(1, "packet.memory.free", $free);
286 <
287 <    $top =~ /([^\s]+?)([MG]) swap in use/;
288 <    my($swap_in_use) = $1;
289 <    $swap_in_use*=1024 if $2 eq "G";
290 <    # DO NOT print this one out... save it for in a moment...
291 <
292 <    $top =~ /([^\s]+?)([MG]) swap free/;
293 <    my($swap_free) = $1;
294 <    $swap_free*=1024 if $2 eq "G";
295 <    &print_pair(1, "packet.memory.swap_free", $swap_free);
296 <
297 <    # AJ requested total swap instead of swap_in_use, so here we go!
298 <    &print_pair(1, "packet.memory.swap_total", $swap_free + $swap_in_use);
282 >        # FreeBSD is a bit different, we need to get user and nice.
283 >        my($user) = 0;
284 >        if($top =~ /([^\s]+?)% user/) { $user += $1; }
285 >        if($top =~ /([^\s]+?)% nice/) { $user += $1; }
286 >        &print_pair(0, "packet.cpu.user", $user);
287 >        
288 >        # The following need to be specified in megabytes.
289 >        # If they are preceeded by a G, then multiply by 1024.  
290 >        
291 >        $top =~ /Mem:.*?([0-9]+)([KMG])\s+(av|total)/;
292 >        my($real) = $1;
293 >        $real*=1024 if $2 eq "G";
294 >        $real/=1024 if $2 eq "K";
295 >        &print_pair(0, "packet.memory.total", int($real));
296 >        
297 >        $top =~ /Mem:.*?([0-9]+)([KMG])\s+free/;
298 >        my($free) = $1;
299 >        $free*=1024 if $2 eq "G";
300 >        $free/=1024 if $2 eq "K";
301 >        &print_pair(0, "packet.memory.free", int($free));
302 >        
303 >        $top =~ /Swap:.*?([0-9]+)([KMG])\s+(av|total)/;
304 >        my($swap_total) = $1;
305 >        $swap_total*=1024 if $2 eq "G";
306 >        $swap_total/=1024 if $2 eq "K";
307 >        &print_pair(0, "packet.swap.total", int($swap_total));
308 >        
309 >        $top =~ /Swap:.*?([0-9]+)([KMG])\s+free/;
310 >        my($swap_free) = $1;
311 >        $swap_free*=1024 if $2 eq "G";
312 >        $swap_free/=1024 if $2 eq "K";
313 >        &print_pair(0, "packet.swap.free", int($swap_free));
314 >    }
315 >    else {
316 >        # we could have some catchall here
317 >        # but as it stands this means we'll just skip top stuff
318 >        # for unknown systems
319 >    }
320   }
321  
322   # sub to get details of the machine's operating system.
# Line 180 | Line 331 | sub include_osver() {
331      my($os_sysname) = `$unamebin -n`;
332      my($os_version) = `$unamebin -v`;
333  
334 <    &print_pair(0, "packet.os.name", $os_name);
335 <    &print_pair(0, "packet.os.release", $os_release);
336 <    &print_pair(0, "packet.os.platform", $os_platform);
337 <    &print_pair(0, "packet.os.sysname", $os_sysname);
338 <    &print_pair(0, "packet.os.version", $os_version);
334 >    &print_pair("unknown", "packet.os.name", $os_name);
335 >    &print_pair("unknown", "packet.os.release", $os_release);
336 >    &print_pair("unknown", "packet.os.platform", $os_platform);
337 >    &print_pair("unknown", "packet.os.sysname", $os_sysname);
338 >    &print_pair("unknown", "packet.os.version", $os_version);
339 >
340 > }
341 >
342 > # sub to get system uptime in seconds.
343 > sub include_uptime() {
344 >
345 >    # debug stuff, all the different cases
346 >
347 >    # normal
348 >    #my($uptime) = "  4:48pm  up 49 day(s),  6:30,  201 users,  load average: 0.33, 0.35, 0.38\n";
349 >    # 0 days
350 >    #my($uptime) = "  4:48pm  up 6:30,  201 users,  load average: 0.33, 0.35, 0.38\n";
351 >    # 0 hours
352 >    #my($uptime) = "  4:48pm  up 49 day(s),  30 min(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
353 >    # 0 mins
354 >    #my($uptime) = "  4:48pm  up 49 day(s),  6 hr(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
355 >    # 0 days and 0 mins
356 >    #my($uptime) = "  4:48pm  up 6 hr(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
357 >    # 0 days and 0 hours
358 >    #my($uptime) = "  4:48pm  up 30 min(s),  201 users,  load average: 0.33, 0.35, 0.38\n";
359 >    # 0 hours and 0 mins
360 >    #my($uptime) = "  4:48pm  up 49 day(s), 201 users,  load average: 0.33, 0.35, 0.38\n";
361 >
362 >    # grab the uptime
363 >    my($uptime) = `$uptimebin`;
364 >    
365 >    if($ostype ne "FreeBSD") {
366 >        &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
367 >        &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
368 >        &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
369 >    }
370 >
371 >    # work out the days, hours, and minutes
372 >
373 >    if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
374 >      # normal
375 >        $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
376 >        $uptime = "$1:$2:$3";
377 >    }
378 >    else {
379 >        if ($uptime =~ /day/) {
380 >            if ($uptime =~ /hr/) {
381 >              # 0 minutes
382 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
383 >                $uptime = "$1:$2:0";
384 >            }
385 >            elsif ($uptime =~ /min/) {
386 >              # 0 hours
387 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
388 >                $uptime = "$1:0:$2";
389 >            }
390 >            else {
391 >              # 0 hours and 0 mins
392 >                $uptime =~ /up\s+([0-9]+)/;
393 >                $uptime = "$1:0:0";
394 >            }
395 >        }
396 >        elsif ($uptime =~ /hr/) {
397 >          # 0 days and 0 minutes
398 >            $uptime =~ /up\s+([0-9]+)\s+/;
399 >            $uptime = "0:$1:0";
400 >        }
401 >        elsif ($uptime =~ /min/) {
402 >          # 0 days and 0 hours
403 >            $uptime =~ /up\s+([0-9]+)\s+/;
404 >            $uptime = "0:0:$1";
405 >        }
406 >        else {
407 >          # 0 days
408 >            $uptime =~ /up\s+([0-9]+):([0-9]+)/;
409 >            $uptime = "0:$1:$2";
410 >        }
411 >    }
412 >
413 >    # turn into seconds
414 >    $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/;
415 >    $uptime = ($3+($2+($1*24))*60)*60;
416 >    
417 >    # print the value out
418 >    &print_pair("unknown", "packet.os.uptime", $uptime);
419  
420   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines