--- 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 16:28:57 1.7 @@ -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.7 2001/01/22 16:28:57 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.7 $'; + 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,33 @@ sub print_pair($$) { } +# sub to find out disk partition information, if it exists. +sub include_disk() { + + # Run the df program. + my(@df) = `df -ak`; + + # Go through each line of the program, looking for each thing we want. + 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*/; + # $4 will not match unless everything before it does... + if (defined $5) { + my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); + &print_pair("packet.disk.p$partition_no.attributes.name", $filesystem); + &print_pair("packet.disk.p$partition_no.attributes.kbytes", $kbytes); + &print_pair("packet.disk.p$partition_no.attributes.used", $used); + &print_pair("packet.disk.p$partition_no.attributes.avail", $avail); + &print_pair("packet.disk.p$partition_no.attributes.mount", $mount); + ++$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 +105,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 +132,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); + }