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.28 by tdb, Mon Feb 5 17:43:45 2001 UTC vs.
Revision 1.44 by tdb, Fri May 25 19:42:26 2001 UTC

# Line 21 | Line 21 | $| = 1;
21   # You'd be silly not to use this ;)
22   use strict;
23  
24 < # Paths
25 < 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 < my($uptimebin) = "/usr/bin/uptime";
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 -n2 -b -p0";
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();
# Line 42 | Line 74 | exit(0);
74  
75  
76  
45
46
47
48
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 83 | Line 111 | sub include_disk() {
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);
# Line 120 | Line 148 | sub include_users() {
148   sub include_top() {
149  
150      # Find out some numbers from top.
151 <    my(@top) = `$topbin -d2 -s1 0`;
151 >    my(@top) = `$topbin`;
152      my($top) = join(" ", @top);
153 <    $top =~ s/\n//g;
153 >    $top =~ s/\n/ /g;
154  
155 <    &print_pair(0, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/);
156 <    &print_pair(0, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/);
157 <    &print_pair(0, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s/);
158 <    &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/);
159 <    &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/);
160 <    &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/);
161 <    &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/);
162 <    &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/);
163 <    &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
164 <    &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
165 <    &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
166 <    &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
167 <    &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
155 >    if($ostype eq "SunOS") {
156 >        &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
157 >        &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
158 >        &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
159 >        &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
160 >        &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\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 =~ /([0-9]+?)([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 =~ /([0-9]+?)([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 =~ /([0-9]+?)([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 =~ /([0-9]+?)([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 >        &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use);
195 >    }
196 >    elsif ($ostype eq "FreeBSD") {
197 >        &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
198 >        &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
199 >        &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
200 >        &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
201 >        &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/);
202 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
203 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
204 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
205 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
206  
207 <    # The following need to be specified in megabytes.
208 <    # If they are preceeded by a G, then multiply by 1024.  
207 >        # FreeBSD is a bit different, we need to get user and nice.
208 >        my($user) = 0;
209 >        if($top =~ /([^\s]+?)% user/) { $user += $1; }
210 >        if($top =~ /([^\s]+?)% nice/) { $user += $1; }
211 >        &print_pair(0, "packet.cpu.user", $user);
212 >        
213 >        # The following need to be specified in megabytes.
214 >        # If they are preceeded by a G, then multiply by 1024.  
215 >        
216 >        # get RAM slightly differently
217 >        my($real) = `$sysctlbin -n hw.physmem`;
218 >        my($free) = $real - `$sysctlbin -n hw.usermem`;
219 >        
220 >        # turn bytes to megabytes
221 >        $real = ($real / 1024) / 1024;
222 >        $free = ($free / 1024) / 1024;
223 >        
224 >        &print_pair(0, "packet.memory.total", $real);
225 >        &print_pair(0, "packet.memory.free", $free);
226 >        
227 >        $top =~ /Swap: ([0-9]+?)([KMG]) Total/;
228 >        my($swap_total) = $1;
229 >        $swap_total*=1024 if $2 eq "G";
230 >        $swap_total/=1024 if $2 eq "K";
231 >        &print_pair(0, "packet.swap.total", $swap_total);
232 >        
233 >        $top =~ /Swap:.*, ([0-9]+?)([KMG]) Free/;
234 >        my($swap_free) = $1;
235 >        $swap_free*=1024 if $2 eq "G";
236 >        $swap_free/=1024 if $2 eq "K";
237 >        &print_pair(0, "packet.swap.free", $swap_free);
238 >        
239 >        my($loads) = `$sysctlbin -n vm.loadavg`;
240 >        $loads =~ /\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+?)\s+/;
241 >        &print_pair(0, "packet.load.load1", $1);
242 >        &print_pair(0, "packet.load.load5", $2);
243 >        &print_pair(0, "packet.load.load15", $3);
244 >    }
245 >    elsif ($ostype eq "Linux") {
246 >        my ($top) = "";
247 >        foreach my $line (@top) {
248 >            $top = $line . $top;
249 >        }
250 >        $top =~ s/\n/ /g;
251  
252 <    $top =~ /([^\s]+?)([MG]) real/;
253 <    my($real) = $1;
254 <    $real*=1024 if $2 eq "G";
255 <    &print_pair(0, "packet.memory.total", $real);
252 >        &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
253 >        &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
254 >        &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
255 >        &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
256 >        &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/);
257 >        &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
258 >        &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
259 >        &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
260 >        &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
261  
262 <    $top =~ /([^\s]+?)([MG]) free/;
263 <    my($free) = $1;
264 <    $free*=1024 if $2 eq "G";
265 <    &print_pair(0, "packet.memory.free", $free);
266 <
267 <    $top =~ /([^\s]+?)([MG]) swap in use/;
268 <    my($swap_in_use) = $1;
269 <    $swap_in_use*=1024 if $2 eq "G";
270 <    # DO NOT print this one out... save it for in a moment...
271 <
272 <    $top =~ /([^\s]+?)([MG]) swap free/;
273 <    my($swap_free) = $1;
274 <    $swap_free*=1024 if $2 eq "G";
275 <    &print_pair(0, "packet.swap.free", $swap_free);
276 <
277 <    # AJ requested total swap instead of swap_in_use, so here we go!
278 <    &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use);
262 >        # FreeBSD is a bit different, we need to get user and nice.
263 >        my($user) = 0;
264 >        if($top =~ /([^\s]+?)% user/) { $user += $1; }
265 >        if($top =~ /([^\s]+?)% nice/) { $user += $1; }
266 >        &print_pair(0, "packet.cpu.user", $user);
267 >        
268 >        # The following need to be specified in megabytes.
269 >        # If they are preceeded by a G, then multiply by 1024.  
270 >        
271 >        $top =~ /Mem:.*?([0-9]+)([KMG])\s+(av|total)/;
272 >        my($real) = $1;
273 >        $real*=1024 if $2 eq "G";
274 >        $real/=1024 if $2 eq "K";
275 >        &print_pair(0, "packet.memory.total", int($real));
276 >        
277 >        $top =~ /Mem:.*?([0-9]+)([KMG])\s+free/;
278 >        my($free) = $1;
279 >        $free*=1024 if $2 eq "G";
280 >        $free/=1024 if $2 eq "K";
281 >        &print_pair(0, "packet.memory.free", int($free));
282 >        
283 >        $top =~ /Swap:.*?([0-9]+)([KMG])\s+(av|total)/;
284 >        my($swap_total) = $1;
285 >        $swap_total*=1024 if $2 eq "G";
286 >        $swap_total/=1024 if $2 eq "K";
287 >        &print_pair(0, "packet.swap.total", int($swap_total));
288 >        
289 >        $top =~ /Swap:.*?([0-9]+)([KMG])\s+free/;
290 >        my($swap_free) = $1;
291 >        $swap_free*=1024 if $2 eq "G";
292 >        $swap_free/=1024 if $2 eq "K";
293 >        &print_pair(0, "packet.swap.free", int($swap_free));
294 >    }
295 >    else {
296 >        # we could have some catchall here
297 >        # but as it stands this means we'll just skip top stuff
298 >        # for unknown systems
299 >    }
300   }
301  
302   # sub to get details of the machine's operating system.
# Line 207 | Line 341 | sub include_uptime() {
341  
342      # grab the uptime
343      my($uptime) = `$uptimebin`;
344 +    
345 +    if($ostype ne "FreeBSD") {
346 +        &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/);
347 +        &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/);
348 +        &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/);
349 +    }
350  
351      # work out the days, hours, and minutes
352  
353      if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) {
354        # normal
355 <        $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+):([0-9]+)/;
355 >        $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/;
356          $uptime = "$1:$2:$3";
357      }
358      else {
359          if ($uptime =~ /day/) {
360              if ($uptime =~ /hr/) {
361                # 0 minutes
362 <                $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/;
362 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
363                  $uptime = "$1:$2:0";
364              }
365              elsif ($uptime =~ /min/) {
366                # 0 hours
367 <                $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/;
367 >                $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/;
368                  $uptime = "$1:0:$2";
369              }
370              else {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines