--- projects/cms/source/host/generic/statgrab.pl 2001/01/22 09:59:09 1.5 +++ projects/cms/source/host/generic/statgrab.pl 2001/01/22 16:51:02 1.8 @@ -3,7 +3,7 @@ #----------------------------------------------------------------- # Machine statistics grabber # $Author: pjm2 $ -# $Id: statgrab.pl,v 1.5 2001/01/22 09:59:09 pjm2 Exp $ +# $Id: statgrab.pl,v 1.8 2001/01/22 16:51:02 pjm2 Exp $ # # A Perl script to return various information about a host machine # by examining the output of some common Unix/Linux commands. @@ -21,32 +21,45 @@ $| = 1; # You'd be silly not to use this ;) use strict; +# Run the following components: - &print_ident(); &include_osver(); &include_users(); &include_top(); &include_disk(); +# End the program normally. exit(0); + + + + + # prints out an identifier for this version of statgrab.pl # 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.5 $'; + print 'version statgrab.pl $Revision: 1.8 $'; print "\n"; } # sub to print pairs of data, separated by a single space character. # If the second argument is undefined, then the pair is still printed, # however, the value shall be displayed as the string "unknown". -sub print_pair($$) { - my($name, $value) = @_; +# If $type is non-zero, then "0" is printed instead of "unknown". +sub print_pair($$$) { + my($type, $name, $value) = @_; if (!defined $value) { - $value = "unknown"; + if ($type) { + $value = 0; + } + else { + $value = "unknown"; + } } # Remove the trailing linefeed if we've not already done so. @@ -61,24 +74,21 @@ sub print_pair($$) { sub include_disk() { # Run the df program. - my(@df) = `df -a`; + my(@df) = `df -ak`; - # Only look for these partitions at the moment. - my(@partition_list) = qw{ / /home /var /tmp }; - # Go through each line of the program, looking for each thing we want. - my($scan_for) = '('.join('|', @partition_list).')'; my($partition_no) = 0; for (my($i) = 0; $i < $#df; $i++) { my($line) = $df[$i]; - $line =~ /^$scan_for\s*\(([^\s]*)\s*\):\s*([0-9]*)\s*blocks\s*([0-9]*)\s*files/; + $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 $4) { - my ($partition, $mounted, $blocks, $files) = ($1, $2, $3, $4); - &print_pair("packet.disk.p$partition_no.attributes.name", $partition); - &print_pair("packet.disk.p$partition_no.mounted", $mounted); - &print_pair("packet.disk.p$partition_no.blocks", $blocks); - &print_pair("packet.disk.p$partition_no.name", $files); + if (defined $5) { + my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); + &print_pair(0, "packet.disk.p$partition_no.attributes.name", $filesystem); + &print_pair(1, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); + &print_pair(1, "packet.disk.p$partition_no.attributes.used", $used); + &print_pair(1, "packet.disk.p$partition_no.attributes.avail", $avail); + &print_pair(0, "packet.disk.p$partition_no.attributes.mount", $mount); ++$partition_no; } } @@ -96,8 +106,8 @@ sub include_users() { my($users_count) = $#users + 1; my($users_list) = $users; - &print_pair("packet.users.count", $users_count); - &print_pair("packet.users.list", $users_list); + &print_pair(1, "packet.users.count", $users_count); + &print_pair(0, "packet.users.list", $users_list); } @@ -110,23 +120,23 @@ sub include_top() { my($top) = join(" ", @top); $top =~ s/\n//g; - &print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); - &print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); - &print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); - &print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/); - &print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); - &print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); - &print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); - &print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); - &print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/); - &print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/); - &print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); - &print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); - &print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/); - &print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/); - &print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/); - &print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); - &print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); + &print_pair(1, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); + &print_pair(1, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); + &print_pair(1, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); + &print_pair(1, "packet.processes.total", $top =~ /([^\s]+?) processes:/); + &print_pair(1, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); + &print_pair(1, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); + &print_pair(1, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); + &print_pair(1, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); + &print_pair(1, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); + &print_pair(1, "packet.cpu.user", $top =~ /([^\s]+?)% user/); + &print_pair(1, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); + &print_pair(1, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); + &print_pair(1, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); + &print_pair(1, "packet.memory.real", $top =~ /([^\s]+?)[MG] real/); + &print_pair(1, "packet.memory.free", $top =~ /([^\s]+?)[MG] free/); + &print_pair(1, "packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); + &print_pair(1, "packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); } @@ -142,10 +152,10 @@ sub include_osver() { my($os_sysname) = `uname -n`; my($os_version) = `uname -v`; - &print_pair("packet.os.name", $os_name); - &print_pair("packet.os.release", $os_release); - &print_pair("packet.os.platform", $os_platform); - &print_pair("packet.os.sysname", $os_sysname); - &print_pair("packet.os.version", $os_version); + &print_pair(0, "packet.os.name", $os_name); + &print_pair(0, "packet.os.release", $os_release); + &print_pair(0, "packet.os.platform", $os_platform); + &print_pair(0, "packet.os.sysname", $os_sysname); + &print_pair(0, "packet.os.version", $os_version); }