--- projects/cms/source/host/generic/statgrab.pl 2001/03/19 23:05:55 1.40 +++ projects/cms/source/host/generic/statgrab.pl 2002/05/21 16:47:11 1.48 @@ -1,9 +1,29 @@ #!/usr/bin/perl -w +# +# i-scream central monitoring system +# http://www.i-scream.org.uk +# Copyright (C) 2000-2002 i-scream +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# + #----------------------------------------------------------------- # Machine statistics grabber # $Author: tdb $ -# $Id: statgrab.pl,v 1.40 2001/03/19 23:05:55 tdb Exp $ +# $Id: statgrab.pl,v 1.48 2002/05/21 16:47:11 tdb Exp $ # # A Perl script to return various information about a host machine # by examining the output of some common Unix/Linux commands. @@ -38,7 +58,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"; @@ -78,7 +98,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.40 $'; + print 'packet.version statgrab.pl $Revision: 1.48 $'; print "\n"; } @@ -105,7 +125,7 @@ sub print_pair($$$) { sub include_disk() { # Run the df program. - my(@df) = `$dfbin -ak`; + my(@df) = `$dfbin -akl`; # Go through each line of the program, looking for each thing we want. my($partition_no) = 0; @@ -150,7 +170,7 @@ 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 =~ /([0-9]+?) processes:/); @@ -191,7 +211,6 @@ sub include_top() { $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") { @@ -244,39 +263,50 @@ sub include_top() { &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.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. - $top =~ / ([0-9]+?)([KMG]) av/; + $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 =~ / ([0-9]+?)([KMG]) free/; + $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:\s+([0-9]]+?)([KMG]) av/; + $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]) free/; + $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";