--- projects/cms/source/host/generic/statgrab.pl 2001/02/13 11:57:48 1.32 +++ projects/cms/source/host/generic/statgrab.pl 2001/05/25 19:42:26 1.44 @@ -3,7 +3,7 @@ #----------------------------------------------------------------- # Machine statistics grabber # $Author: tdb $ -# $Id: statgrab.pl,v 1.32 2001/02/13 11:57:48 tdb Exp $ +# $Id: statgrab.pl,v 1.44 2001/05/25 19:42:26 tdb Exp $ # # A Perl script to return various information about a host machine # by examining the output of some common Unix/Linux commands. @@ -25,7 +25,8 @@ use strict; my($ostype) = `uname -s`; chop($ostype); # Decide which paths we should use. -my($topbin); my($dfbin); my($usersbin); my($unamebin); my($uptimebin); +my($topbin); my($dfbin); my($usersbin); +my($unamebin); my($uptimebin); my($sysctlbin); if ($ostype eq "SunOS") { # covers: Solaris 8 @@ -37,7 +38,7 @@ if ($ostype eq "SunOS") { } elsif ($ostype eq "Linux") { # covers: Debian r2.2 - $topbin = "/usr/bin/top -d1 -n0 -b"; + $topbin = "/usr/bin/top -d1 -n2 -b -p0"; $dfbin = "/bin/df"; $usersbin = "/usr/bin/users"; $unamebin = "/bin/uname"; @@ -50,6 +51,7 @@ elsif ($ostype eq "FreeBSD") { $usersbin = "/usr/bin/users"; $unamebin = "/usr/bin/uname"; $uptimebin = "/usr/bin/uptime"; + $sysctlbin = "/sbin/sysctl"; } else { print "statgrab.pl Error: Unable to identify system type - \"$ostype\".\n"; @@ -76,7 +78,7 @@ exit(0); # the host should check this when reading data # means the host must be checked and updated to work with newer versions. sub print_ident() { - print 'version statgrab.pl $Revision: 1.32 $'; + print 'version statgrab.pl $Revision: 1.44 $'; print "\n"; } @@ -109,7 +111,7 @@ sub include_disk() { my($partition_no) = 0; for (my($i) = 0; $i < $#df; $i++) { my($line) = $df[$i]; - $line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; + $line =~ /^([^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; # $4 will not match unless everything before it does... if (defined $5) { my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); @@ -148,14 +150,14 @@ sub include_top() { # Find out some numbers from top. my(@top) = `$topbin`; my($top) = join(" ", @top); - $top =~ s/\n//g; + $top =~ s/\n/ /g; if($ostype eq "SunOS") { - &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); - &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); - &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); - &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); - &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); + &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/); + &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/); + &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/); + &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/); + &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*on cpu/); &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/); &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); @@ -165,68 +167,135 @@ sub include_top() { # The following need to be specified in megabytes. # If they are preceeded by a G, then multiply by 1024. - $top =~ /([^\s]+?)([MG]) real/; + $top =~ /([0-9]+?)([KMG]) real/; my($real) = $1; $real*=1024 if $2 eq "G"; + $real/=1024 if $2 eq "K"; &print_pair(0, "packet.memory.total", $real); - $top =~ /([^\s]+?)([MG]) free/; + $top =~ /([0-9]+?)([KMG]) free/; my($free) = $1; $free*=1024 if $2 eq "G"; + $free/=1024 if $2 eq "K"; &print_pair(0, "packet.memory.free", $free); - $top =~ /([^\s]+?)([MG]) swap in use/; + $top =~ /([0-9]+?)([KMG]) swap in use/; my($swap_in_use) = $1; $swap_in_use*=1024 if $2 eq "G"; + $swap_in_use/=1024 if $2 eq "K"; # DO NOT print this one out... save it for in a moment... - $top =~ /([^\s]+?)([MG]) swap free/; + $top =~ /([0-9]+?)([KMG]) swap free/; my($swap_free) = $1; $swap_free*=1024 if $2 eq "G"; + $swap_free/=1024 if $2 eq "K"; &print_pair(0, "packet.swap.free", $swap_free); - # AJ requested total swap instead of swap_in_use, so here we go! &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use); } elsif ($ostype eq "FreeBSD") { - &print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); - &print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); - &print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); - &print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); - &print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*running/); + &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/); + &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/); + &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/); + &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/); + &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/); &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); - &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/); &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/); &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/); &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); + + # FreeBSD is a bit different, we need to get user and nice. + my($user) = 0; + if($top =~ /([^\s]+?)% user/) { $user += $1; } + if($top =~ /([^\s]+?)% nice/) { $user += $1; } + &print_pair(0, "packet.cpu.user", $user); # The following need to be specified in megabytes. # If they are preceeded by a G, then multiply by 1024. - # !! Can't get the total memory !! - #$top =~ /([^\s]+?)([MG]) real/; - #my($real) = $1; - #$real*=1024 if $2 eq "G"; - #&print_pair(0, "packet.memory.total", $real); - &print_pair(0, "packet.memory.total", 0); + # get RAM slightly differently + my($real) = `$sysctlbin -n hw.physmem`; + my($free) = $real - `$sysctlbin -n hw.usermem`; - $top =~ /([^\s]+?)([MG]) Free/; - my($free) = $1; - $free*=1024 if $2 eq "G"; + # turn bytes to megabytes + $real = ($real / 1024) / 1024; + $free = ($free / 1024) / 1024; + + &print_pair(0, "packet.memory.total", $real); &print_pair(0, "packet.memory.free", $free); - $top =~ /Swap: ([^\s]+?)([MG]) Total/; + $top =~ /Swap: ([0-9]+?)([KMG]) Total/; my($swap_total) = $1; $swap_total*=1024 if $2 eq "G"; + $swap_total/=1024 if $2 eq "K"; &print_pair(0, "packet.swap.total", $swap_total); - $top =~ /Swap:.*, ([^\s]+?)([MG]) Free/; + $top =~ /Swap:.*, ([0-9]+?)([KMG]) Free/; my($swap_free) = $1; $swap_free*=1024 if $2 eq "G"; + $swap_free/=1024 if $2 eq "K"; &print_pair(0, "packet.swap.free", $swap_free); + + my($loads) = `$sysctlbin -n vm.loadavg`; + $loads =~ /\s+([^\s]+?)\s+([^\s]+?)\s+([^\s]+?)\s+/; + &print_pair(0, "packet.load.load1", $1); + &print_pair(0, "packet.load.load5", $2); + &print_pair(0, "packet.load.load15", $3); } + elsif ($ostype eq "Linux") { + my ($top) = ""; + foreach my $line (@top) { + $top = $line . $top; + } + $top =~ s/\n/ /g; + + &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/); + &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/); + &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/); + &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/); + &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/); + &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); + &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/); + &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/); + &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); + + # FreeBSD is a bit different, we need to get user and nice. + my($user) = 0; + if($top =~ /([^\s]+?)% user/) { $user += $1; } + if($top =~ /([^\s]+?)% nice/) { $user += $1; } + &print_pair(0, "packet.cpu.user", $user); + + # The following need to be specified in megabytes. + # If they are preceeded by a G, then multiply by 1024. + + $top =~ /Mem:.*?([0-9]+)([KMG])\s+(av|total)/; + my($real) = $1; + $real*=1024 if $2 eq "G"; + $real/=1024 if $2 eq "K"; + &print_pair(0, "packet.memory.total", int($real)); + + $top =~ /Mem:.*?([0-9]+)([KMG])\s+free/; + my($free) = $1; + $free*=1024 if $2 eq "G"; + $free/=1024 if $2 eq "K"; + &print_pair(0, "packet.memory.free", int($free)); + + $top =~ /Swap:.*?([0-9]+)([KMG])\s+(av|total)/; + my($swap_total) = $1; + $swap_total*=1024 if $2 eq "G"; + $swap_total/=1024 if $2 eq "K"; + &print_pair(0, "packet.swap.total", int($swap_total)); + + $top =~ /Swap:.*?([0-9]+)([KMG])\s+free/; + my($swap_free) = $1; + $swap_free*=1024 if $2 eq "G"; + $swap_free/=1024 if $2 eq "K"; + &print_pair(0, "packet.swap.free", int($swap_free)); + } else { # we could have some catchall here + # but as it stands this means we'll just skip top stuff + # for unknown systems } } @@ -273,27 +342,29 @@ sub include_uptime() { # grab the uptime my($uptime) = `$uptimebin`; - &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/); - &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/); - &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/); + if($ostype ne "FreeBSD") { + &print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/); + &print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/); + &print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/); + } # work out the days, hours, and minutes if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) { # normal - $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+):([0-9]+)/; + $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+):([0-9]+)/; $uptime = "$1:$2:$3"; } else { if ($uptime =~ /day/) { if ($uptime =~ /hr/) { # 0 minutes - $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/; + $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/; $uptime = "$1:$2:0"; } elsif ($uptime =~ /min/) { # 0 hours - $uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/; + $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/; $uptime = "$1:0:$2"; } else {