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.32 by tdb, Tue Feb 13 11:57:48 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 25 | Line 45 | use strict;
45   my($ostype) = `uname -s`; chop($ostype);
46  
47   # Decide which paths we should use.
48 < my($topbin); my($dfbin); my($usersbin); my($unamebin); my($uptimebin);
48 > my($topbin); my($dfbin); my($usersbin);
49 > my($unamebin); my($uptimebin); my($sysctlbin);
50  
51   if ($ostype eq "SunOS") {
52      # covers: Solaris 8
# Line 37 | Line 58 | if ($ostype eq "SunOS") {
58   }
59   elsif ($ostype eq "Linux") {
60      # covers: Debian r2.2
61 <    $topbin = "/usr/bin/top -d1 -n0 -b";
61 >    $topbin = "/usr/bin/top -d1 -n2 -b -p0";
62      $dfbin = "/bin/df";
63      $usersbin = "/usr/bin/users";
64      $unamebin = "/bin/uname";
# Line 50 | Line 71 | elsif ($ostype eq "FreeBSD") {
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";
# Line 76 | Line 98 | exit(0);
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  
# Line 103 | 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);
# Line 148 | Line 170 | sub include_top() {
170      # Find out some numbers from top.
171      my(@top) = `$topbin`;
172      my($top) = join(" ", @top);
173 <    $top =~ s/\n//g;
173 >    $top =~ s/\n/ /g;
174  
175      if($ostype eq "SunOS") {
176 <        &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
177 <        &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
178 <        &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
179 <        &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
180 <        &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/);
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/);
# Line 165 | Line 187 | sub include_top() {
187          # The following need to be specified in megabytes.
188          # If they are preceeded by a G, then multiply by 1024.  
189          
190 <        $top =~ /([^\s]+?)([MG]) real/;
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 =~ /([^\s]+?)([MG]) free/;
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 =~ /([^\s]+?)([MG]) swap in use/;
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 =~ /([^\s]+?)([MG]) swap free/;
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          
188        # AJ requested total swap instead of swap_in_use, so here we go!
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 =~ /([^\s]+?) processes:/);
218 <        &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
219 <        &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
220 <        &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
221 <        &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/);
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/);
198        &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
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 +        # 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 <        # !! Can't get the total memory !!
237 <        #$top =~ /([^\s]+?)([MG]) real/;
238 <        #my($real) = $1;
209 <        #$real*=1024 if $2 eq "G";
210 <        #&print_pair(0, "packet.memory.total", $real);
211 <        &print_pair(0, "packet.memory.total", 0);
236 >        # get RAM slightly differently
237 >        my($real) = `$sysctlbin -n hw.physmem`;
238 >        my($free) = $real - `$sysctlbin -n hw.usermem`;
239          
240 <        $top =~ /([^\s]+?)([MG]) Free/;
241 <        my($free) = $1;
242 <        $free*=1024 if $2 eq "G";
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: ([^\s]+?)([MG]) Total/;
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:.*, ([^\s]+?)([MG]) Free/;
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 +        &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 +        # 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  
# Line 273 | Line 362 | sub include_uptime() {
362      # grab the uptime
363      my($uptime) = `$uptimebin`;
364      
365 <    &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
366 <    &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
367 <    &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
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+([0-9]+):([0-9]+)/;
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+([0-9]+)\s+.*,/;
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+([0-9]+)\s+.*,/;
387 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
388                  $uptime = "$1:0:$2";
389              }
390              else {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines