--- projects/cms/source/host/generic/statgrab.pl 2001/01/22 04:09:41 1.4 +++ 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.4 2001/01/22 04:09:41 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. @@ -21,29 +41,76 @@ $| = 1; # You'd be silly not to use this ;) use strict; +# Have to hope this will work really. +my($ostype) = `uname -s`; chop($ostype); + +# Decide which paths we should use. +my($topbin); my($dfbin); my($usersbin); +my($unamebin); my($uptimebin); my($sysctlbin); + +if ($ostype eq "SunOS") { + # covers: Solaris 8 + $topbin = "/usr/local/sbin/top -d2 -s1 0"; + $dfbin = "/usr/bin/df"; + $usersbin = "/usr/ucb/users"; + $unamebin = "/usr/bin/uname"; + $uptimebin = "/usr/bin/uptime"; +} +elsif ($ostype eq "Linux") { + # covers: Debian r2.2 + $topbin = "/usr/bin/top -d1 -n2 -b -p0"; + $dfbin = "/bin/df"; + $usersbin = "/usr/bin/users"; + $unamebin = "/bin/uname"; + $uptimebin = "/usr/bin/uptime"; +} +elsif ($ostype eq "FreeBSD") { + # covers: FreeBSD 4.2-STABLE + $topbin = "/usr/bin/top -d2 -s1 0"; + $dfbin = "/bin/df"; + $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"; + print "\"uname -s\" does not report one of the following known types;\n"; + print " SunOS, Linux, FreeBSD\n"; + exit(1); +} + +# Run the following components: - &print_ident(); &include_osver(); +&include_uptime(); &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.4 $'; + print 'packet.version statgrab.pl $Revision: 1.48 $'; print "\n"; } # sub to print pairs of data, separated by a single space character. -sub print_pair($$) { - my($name, $value) = @_; +# If the second argument is undefined, then the pair is still printed, +# however, the value shall be displayed as the the 'default' value +# if the passed value was undefined. +sub print_pair($$$) { + my($default, $name, $value) = @_; if (!defined $value) { - $value = "unknown"; + $value = $default; } # Remove the trailing linefeed if we've not already done so. @@ -54,60 +121,300 @@ sub print_pair($$) { } +# sub to find out disk partition information, if it exists. +sub include_disk() { + + # Run the df program. + my(@df) = `$dfbin -akl`; + + # 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("unknown", "packet.disk.p$partition_no.attributes.name", $filesystem); + &print_pair(0, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); + &print_pair(0, "packet.disk.p$partition_no.attributes.used", $used); + &print_pair(0, "packet.disk.p$partition_no.attributes.avail", $avail); + &print_pair("unknown", "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. - my($users) = `users`; - my(@users) = split(/\s+/, $users); + my($users) = `$usersbin`; + $users = "\n" unless defined $users; + chop $users; + my($users_count) = 0; + $users_count++ while $users =~ /\w+/g; + my($users_list) = $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(0, "packet.users.count", $users_count); + &print_pair("unknown", "packet.users.list", $users_list); } +# 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. - my(@top) = `top -d2 -s1 0`; + my(@top) = `$topbin`; my($top) = join(" ", @top); - $top =~ s/\n//g; + $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/); - + if($ostype eq "SunOS") { + &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/); + &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); + &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); + + # The following need to be specified in megabytes. + # If they are preceeded by a G, then multiply by 1024. + + $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 =~ /([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 =~ /([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 =~ /([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); + + &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use); + } + elsif ($ostype eq "FreeBSD") { + &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. + + # get RAM slightly differently + my($real) = `$sysctlbin -n hw.physmem`; + my($free) = $real - `$sysctlbin -n hw.usermem`; + + # 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: ([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:.*, ([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 + } } +# sub to get details of the machine's operating system. sub include_osver() { # Find out details about the operating system - 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`; + # If these values remain undefined, then the print_pair + # function shall show the value to be the string "unknown". + my($os_name) = `$unamebin -s`; + my($os_release) = `$unamebin -r`; + my($os_platform) = `$unamebin -m`; + my($os_sysname) = `$unamebin -n`; + my($os_version) = `$unamebin -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("unknown", "packet.os.name", $os_name); + &print_pair("unknown", "packet.os.release", $os_release); + &print_pair("unknown", "packet.os.platform", $os_platform); + &print_pair("unknown", "packet.os.sysname", $os_sysname); + &print_pair("unknown", "packet.os.version", $os_version); + +} + +# sub to get system uptime in seconds. +sub include_uptime() { + + # debug stuff, all the different cases + + # normal + #my($uptime) = " 4:48pm up 49 day(s), 6:30, 201 users, load average: 0.33, 0.35, 0.38\n"; + # 0 days + #my($uptime) = " 4:48pm up 6:30, 201 users, load average: 0.33, 0.35, 0.38\n"; + # 0 hours + #my($uptime) = " 4:48pm up 49 day(s), 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n"; + # 0 mins + #my($uptime) = " 4:48pm up 49 day(s), 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n"; + # 0 days and 0 mins + #my($uptime) = " 4:48pm up 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n"; + # 0 days and 0 hours + #my($uptime) = " 4:48pm up 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n"; + # 0 hours and 0 mins + #my($uptime) = " 4:48pm up 49 day(s), 201 users, load average: 0.33, 0.35, 0.38\n"; + + # grab the uptime + my($uptime) = `$uptimebin`; + + 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]+,\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]+,\s+([0-9]+)\s+[^\s]+,/; + $uptime = "$1:$2:0"; + } + elsif ($uptime =~ /min/) { + # 0 hours + $uptime =~ /up\s+([0-9]+)\s+[^\s]+,\s+([0-9]+)\s+[^\s]+,/; + $uptime = "$1:0:$2"; + } + else { + # 0 hours and 0 mins + $uptime =~ /up\s+([0-9]+)/; + $uptime = "$1:0:0"; + } + } + elsif ($uptime =~ /hr/) { + # 0 days and 0 minutes + $uptime =~ /up\s+([0-9]+)\s+/; + $uptime = "0:$1:0"; + } + elsif ($uptime =~ /min/) { + # 0 days and 0 hours + $uptime =~ /up\s+([0-9]+)\s+/; + $uptime = "0:0:$1"; + } + else { + # 0 days + $uptime =~ /up\s+([0-9]+):([0-9]+)/; + $uptime = "0:$1:$2"; + } + } + + # turn into seconds + $uptime =~ /([0-9]+):([0-9]+):([0-9]+)/; + $uptime = ($3+($2+($1*24))*60)*60; + + # print the value out + &print_pair("unknown", "packet.os.uptime", $uptime); }