--- projects/cms/source/host/generic/statgrab.pl 2001/01/19 19:37:59 1.1 +++ projects/cms/source/host/generic/statgrab.pl 2001/01/22 10:03:30 1.6 @@ -3,7 +3,7 @@ #----------------------------------------------------------------- # Machine statistics grabber # $Author: pjm2 $ -# $Id: statgrab.pl,v 1.1 2001/01/19 19:37:59 pjm2 Exp $ +# $Id: statgrab.pl,v 1.6 2001/01/22 10:03:30 pjm2 Exp $ # # A Perl script to return various information about a host machine # by examining the output of some common Unix/Linux commands. @@ -21,15 +21,34 @@ $| = 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.6 $'; + 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) = @_; @@ -45,6 +64,36 @@ sub print_pair($$) { } +# sub to find out disk partition information, if it exists. +sub include_disk() { + + # Run the df program. + my(@df) = `df -a`; + + # 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/; + # $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); + ++$partition_no; + } + } + +} + +# sub to find out the list of all (non-unique) usernames logged +# in to the machine and how many their are. (not sub include_users() { # Find out all users on this machine. @@ -59,6 +108,8 @@ sub include_users() { } +# sub to run a series of regexps on the output of 'top' to +# gather various machine statistics. sub include_top() { # Find out some numbers from top. @@ -84,4 +135,24 @@ sub include_top() { &print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); &print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); +} + +# sub to get details of the machine's operating system. +sub include_osver() { + + # Find out details about the operating system + # If these values remain undefined, then the print_pair + # function shall show the value to be the string "unknown". + my($os_name) = `uname -s`; + my($os_release) = `uname -r`; + my($os_platform) = `uname -m`; + 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); + }