| 1 | pjm2 | 1.1 | #!/usr/bin/perl -w | 
 
 
 
 
 | 2 |  |  |  | 
 
 
 
 
 | 3 |  |  | #----------------------------------------------------------------- | 
 
 
 
 
 | 4 |  |  | # Machine statistics grabber | 
 
 
 
 
 
 
 
 | 5 | pjm2 | 1.15 | #     $Author: pjm2 $ | 
 
 
 
 
 
 
 
 | 6 | pjm2 | 1.16 | #     $Id: statgrab.pl,v 1.15 2001/01/28 19:53:39 pjm2 Exp $ | 
 
 
 
 
 
 
 
 | 7 | pjm2 | 1.1 | # | 
 
 
 
 
 | 8 |  |  | # A Perl script to return various information about a host machine | 
 
 
 
 
 | 9 |  |  | # by examining the output of some common Unix/Linux commands. | 
 
 
 
 
 | 10 |  |  | # This is a stopgap to act as a generic way of collecting the | 
 
 
 
 
 | 11 |  |  | # data.  It is perhaps more reliable than the current Java host | 
 
 
 
 
 | 12 |  |  | # at doing this and it can obviously be used by a C++ program as | 
 
 
 
 
 | 13 |  |  | # well until the C++ host is ready to find the information out | 
 
 
 
 
 | 14 |  |  | # itself. | 
 
 
 
 
 | 15 |  |  | #----------------------------------------------------------------- | 
 
 
 
 
 | 16 |  |  |  | 
 
 
 
 
 | 17 |  |  |  | 
 
 
 
 
 | 18 |  |  | $| = 1; | 
 
 
 
 
 | 19 |  |  |  | 
 
 
 
 
 | 20 |  |  |  | 
 
 
 
 
 | 21 |  |  | # You'd be silly not to use this ;) | 
 
 
 
 
 | 22 |  |  | use strict; | 
 
 
 
 
 | 23 |  |  |  | 
 
 
 
 
 
 
 
 | 24 | tdb | 1.13 | # Path's | 
 
 
 
 
 | 25 |  |  | my($topbin) = "/usr/local/sbin/top"; | 
 
 
 
 
 | 26 |  |  | my($dfbin) = "/usr/bin/df"; | 
 
 
 
 
 | 27 |  |  | my($usersbin) = "/usr/ucb/users"; | 
 
 
 
 
 | 28 |  |  | my($unamebin) = "/usr/bin/uname"; | 
 
 
 
 
 | 29 |  |  |  | 
 
 
 
 
 
 
 
 | 30 | pjm2 | 1.6 | # Run the following components: - | 
 
 
 
 
 
 
 
 | 31 | tdb | 1.3 | &print_ident(); | 
 
 
 
 
 
 
 
 | 32 | tdb | 1.2 | &include_osver(); | 
 
 
 
 
 
 
 
 | 33 | pjm2 | 1.1 | &include_users(); | 
 
 
 
 
 | 34 |  |  | &include_top(); | 
 
 
 
 
 
 
 
 | 35 | pjm2 | 1.5 | &include_disk(); | 
 
 
 
 
 
 
 
 | 36 | pjm2 | 1.1 |  | 
 
 
 
 
 
 
 
 | 37 | pjm2 | 1.6 | # End the program normally. | 
 
 
 
 
 
 
 
 | 38 | pjm2 | 1.1 | exit(0); | 
 
 
 
 
 | 39 |  |  |  | 
 
 
 
 
 | 40 |  |  |  | 
 
 
 
 
 | 41 |  |  |  | 
 
 
 
 
 
 
 
 | 42 | pjm2 | 1.6 |  | 
 
 
 
 
 | 43 |  |  |  | 
 
 
 
 
 | 44 |  |  |  | 
 
 
 
 
 | 45 |  |  |  | 
 
 
 
 
 | 46 |  |  |  | 
 
 
 
 
 
 
 
 | 47 | tdb | 1.3 | # prints out an identifier for this version of statgrab.pl | 
 
 
 
 
 | 48 |  |  | # the host should check this when reading data | 
 
 
 
 
 | 49 |  |  | # means the host must be checked and updated to work with newer versions. | 
 
 
 
 
 | 50 |  |  | sub print_ident() { | 
 
 
 
 
 
 
 
 | 51 | pjm2 | 1.16 | print 'version statgrab.pl $Revision: 1.15 $'; | 
 
 
 
 
 
 
 
 | 52 | tdb | 1.3 | print "\n"; | 
 
 
 
 
 | 53 |  |  | } | 
 
 
 
 
 
 
 
 | 54 | pjm2 | 1.1 |  | 
 
 
 
 
 | 55 |  |  | # sub to print pairs of data, separated by a single space character. | 
 
 
 
 
 
 
 
 | 56 | pjm2 | 1.5 | # If the second argument is undefined, then the pair is still printed, | 
 
 
 
 
 | 57 |  |  | # however, the value shall be displayed as the string "unknown". | 
 
 
 
 
 
 
 
 | 58 | pjm2 | 1.8 | # If $type is non-zero, then "0" is printed instead of "unknown". | 
 
 
 
 
 | 59 |  |  | sub print_pair($$$) { | 
 
 
 
 
 | 60 |  |  | my($type, $name, $value) = @_; | 
 
 
 
 
 
 
 
 | 61 | pjm2 | 1.1 |  | 
 
 
 
 
 | 62 |  |  | if (!defined $value) { | 
 
 
 
 
 
 
 
 | 63 | pjm2 | 1.8 | if ($type) { | 
 
 
 
 
 
 
 
 | 64 | pjm2 | 1.9 | $value = "0.00"; | 
 
 
 
 
 
 
 
 | 65 | pjm2 | 1.8 | } | 
 
 
 
 
 | 66 |  |  | else { | 
 
 
 
 
 | 67 |  |  | $value = "unknown"; | 
 
 
 
 
 | 68 |  |  | } | 
 
 
 
 
 
 
 
 | 69 | pjm2 | 1.1 | } | 
 
 
 
 
 | 70 |  |  |  | 
 
 
 
 
 | 71 |  |  | # Remove the trailing linefeed if we've not already done so. | 
 
 
 
 
 | 72 |  |  | chomp($value); | 
 
 
 
 
 | 73 |  |  |  | 
 
 
 
 
 | 74 |  |  | # print the pair of data with a space inbetween. | 
 
 
 
 
 | 75 |  |  | print "$name $value\n"; | 
 
 
 
 
 | 76 |  |  | } | 
 
 
 
 
 | 77 |  |  |  | 
 
 
 
 
 | 78 |  |  |  | 
 
 
 
 
 
 
 
 | 79 | pjm2 | 1.5 | # sub to find out disk partition information, if it exists. | 
 
 
 
 
 | 80 |  |  | sub include_disk() { | 
 
 
 
 
 | 81 |  |  |  | 
 
 
 
 
 | 82 |  |  | # Run the df program. | 
 
 
 
 
 
 
 
 | 83 | tdb | 1.13 | my(@df) = `$dfbin -ak`; | 
 
 
 
 
 
 
 
 | 84 | pjm2 | 1.5 |  | 
 
 
 
 
 | 85 |  |  | # Go through each line of the program, looking for each thing we want. | 
 
 
 
 
 | 86 |  |  | my($partition_no) = 0; | 
 
 
 
 
 | 87 |  |  | for (my($i) = 0; $i < $#df; $i++) { | 
 
 
 
 
 | 88 |  |  | my($line) = $df[$i]; | 
 
 
 
 
 
 
 
 | 89 | pjm2 | 1.7 | $line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; | 
 
 
 
 
 
 
 
 | 90 | pjm2 | 1.5 | # $4 will not match unless everything before it does... | 
 
 
 
 
 
 
 
 | 91 | pjm2 | 1.7 | if (defined $5) { | 
 
 
 
 
 | 92 |  |  | my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); | 
 
 
 
 
 
 
 
 | 93 | pjm2 | 1.8 | &print_pair(0, "packet.disk.p$partition_no.attributes.name", $filesystem); | 
 
 
 
 
 | 94 |  |  | &print_pair(1, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); | 
 
 
 
 
 | 95 |  |  | &print_pair(1, "packet.disk.p$partition_no.attributes.used", $used); | 
 
 
 
 
 | 96 |  |  | &print_pair(1, "packet.disk.p$partition_no.attributes.avail", $avail); | 
 
 
 
 
 | 97 |  |  | &print_pair(0, "packet.disk.p$partition_no.attributes.mount", $mount); | 
 
 
 
 
 
 
 
 | 98 | pjm2 | 1.5 | ++$partition_no; | 
 
 
 
 
 | 99 |  |  | } | 
 
 
 
 
 | 100 |  |  | } | 
 
 
 
 
 | 101 |  |  |  | 
 
 
 
 
 | 102 |  |  | } | 
 
 
 
 
 | 103 |  |  |  | 
 
 
 
 
 | 104 |  |  | # sub to find out the list of all (non-unique) usernames logged | 
 
 
 
 
 | 105 |  |  | # in to the machine and how many their are. (not | 
 
 
 
 
 
 
 
 | 106 | pjm2 | 1.1 | sub include_users() { | 
 
 
 
 
 | 107 |  |  |  | 
 
 
 
 
 | 108 |  |  | # Find out all users on this machine. | 
 
 
 
 
 
 
 
 | 109 | tdb | 1.13 | my($users) = `$usersbin`; | 
 
 
 
 
 
 
 
 | 110 | pjm2 | 1.16 | $users = "\n" unless defined $users; | 
 
 
 
 
 | 111 |  |  | chop $users; | 
 
 
 
 
 
 
 
 | 112 | pjm2 | 1.14 | my($users_count) = 0; | 
 
 
 
 
 | 113 |  |  | $users_count++ while $users =~ /\w+/g; | 
 
 
 
 
 
 
 
 | 114 | pjm2 | 1.1 | my($users_list) = $users; | 
 
 
 
 
 | 115 |  |  |  | 
 
 
 
 
 
 
 
 | 116 | pjm2 | 1.8 | &print_pair(1, "packet.users.count", $users_count); | 
 
 
 
 
 | 117 |  |  | &print_pair(0, "packet.users.list", $users_list); | 
 
 
 
 
 
 
 
 | 118 | pjm2 | 1.1 | } | 
 
 
 
 
 | 119 |  |  |  | 
 
 
 
 
 | 120 |  |  |  | 
 
 
 
 
 
 
 
 | 121 | pjm2 | 1.5 | # sub to run a series of regexps on the output of 'top' to | 
 
 
 
 
 | 122 |  |  | # gather various machine statistics. | 
 
 
 
 
 
 
 
 | 123 | pjm2 | 1.1 | sub include_top() { | 
 
 
 
 
 | 124 |  |  |  | 
 
 
 
 
 | 125 |  |  | # Find out some numbers from top. | 
 
 
 
 
 
 
 
 | 126 | tdb | 1.13 | my(@top) = `$topbin -d2 -s1 0`; | 
 
 
 
 
 
 
 
 | 127 | pjm2 | 1.1 | my($top) = join(" ", @top); | 
 
 
 
 
 | 128 |  |  | $top =~ s/\n//g; | 
 
 
 
 
 | 129 |  |  |  | 
 
 
 
 
 
 
 
 | 130 | pjm2 | 1.8 | &print_pair(1, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); | 
 
 
 
 
 | 131 |  |  | &print_pair(1, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); | 
 
 
 
 
 | 132 |  |  | &print_pair(1, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); | 
 
 
 
 
 | 133 |  |  | &print_pair(1, "packet.processes.total", $top =~ /([^\s]+?) processes:/); | 
 
 
 
 
 | 134 |  |  | &print_pair(1, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); | 
 
 
 
 
 | 135 |  |  | &print_pair(1, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); | 
 
 
 
 
 | 136 |  |  | &print_pair(1, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); | 
 
 
 
 
 | 137 |  |  | &print_pair(1, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); | 
 
 
 
 
 | 138 |  |  | &print_pair(1, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); | 
 
 
 
 
 | 139 |  |  | &print_pair(1, "packet.cpu.user", $top =~ /([^\s]+?)% user/); | 
 
 
 
 
 | 140 |  |  | &print_pair(1, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); | 
 
 
 
 
 | 141 |  |  | &print_pair(1, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); | 
 
 
 
 
 | 142 |  |  | &print_pair(1, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); | 
 
 
 
 
 
 
 
 | 143 | pjm2 | 1.10 |  | 
 
 
 
 
 | 144 |  |  | # The following need to be specified in megabytes. | 
 
 
 
 
 | 145 |  |  | # If they are preceeded by a G, then multiply by 1024. | 
 
 
 
 
 | 146 |  |  |  | 
 
 
 
 
 | 147 |  |  | $top =~ /([^\s]+?)([MG]) real/; | 
 
 
 
 
 
 
 
 | 148 | pjm2 | 1.11 | my($real) = $1; | 
 
 
 
 
 | 149 |  |  | $real*=1024 if $2 eq "G"; | 
 
 
 
 
 
 
 
 | 150 | pjm2 | 1.10 | &print_pair(1, "packet.memory.real", $real); | 
 
 
 
 
 | 151 |  |  |  | 
 
 
 
 
 | 152 |  |  | $top =~ /([^\s]+?)([MG]) free/; | 
 
 
 
 
 
 
 
 | 153 | pjm2 | 1.11 | my($free) = $1; | 
 
 
 
 
 | 154 |  |  | $free*=1024 if $2 eq "G"; | 
 
 
 
 
 
 
 
 | 155 | pjm2 | 1.10 | &print_pair(1, "packet.memory.free", $free); | 
 
 
 
 
 | 156 |  |  |  | 
 
 
 
 
 | 157 |  |  | $top =~ /([^\s]+?)([MG]) swap in use/; | 
 
 
 
 
 
 
 
 | 158 | pjm2 | 1.11 | my($swap_in_use) = $1; | 
 
 
 
 
 | 159 |  |  | $swap_in_use*=1024 if $2 eq "G"; | 
 
 
 
 
 
 
 
 | 160 | pjm2 | 1.12 | # DO NOT print this one out... save it for in a moment... | 
 
 
 
 
 
 
 
 | 161 | pjm2 | 1.10 |  | 
 
 
 
 
 | 162 |  |  | $top =~ /([^\s]+?)([MG]) swap free/; | 
 
 
 
 
 
 
 
 | 163 | pjm2 | 1.11 | my($swap_free) = $1; | 
 
 
 
 
 | 164 |  |  | $swap_free*=1024 if $2 eq "G"; | 
 
 
 
 
 
 
 
 | 165 | pjm2 | 1.10 | &print_pair(1, "packet.memory.swap_free", $swap_free); | 
 
 
 
 
 
 
 
 | 166 | pjm2 | 1.12 |  | 
 
 
 
 
 | 167 |  |  | # AJ requested total swap instead of swap_in_use, so here we go! | 
 
 
 
 
 | 168 |  |  | &print_pair(1, "packet.memory.swap_total", $swap_free + $swap_in_use); | 
 
 
 
 
 
 
 
 | 169 | tdb | 1.2 | } | 
 
 
 
 
 | 170 |  |  |  | 
 
 
 
 
 
 
 
 | 171 | pjm2 | 1.5 | # sub to get details of the machine's operating system. | 
 
 
 
 
 
 
 
 | 172 | tdb | 1.2 | sub include_osver() { | 
 
 
 
 
 | 173 |  |  |  | 
 
 
 
 
 | 174 |  |  | # Find out details about the operating system | 
 
 
 
 
 
 
 
 | 175 | pjm2 | 1.5 | # If these values remain undefined, then the print_pair | 
 
 
 
 
 | 176 |  |  | # function shall show the value to be the string "unknown". | 
 
 
 
 
 
 
 
 | 177 | tdb | 1.13 | my($os_name) = `$unamebin -s`; | 
 
 
 
 
 | 178 |  |  | my($os_release) = `$unamebin -r`; | 
 
 
 
 
 | 179 |  |  | my($os_platform) = `$unamebin -m`; | 
 
 
 
 
 | 180 |  |  | my($os_sysname) = `$unamebin -n`; | 
 
 
 
 
 | 181 |  |  | my($os_version) = `$unamebin -v`; | 
 
 
 
 
 
 
 
 | 182 | tdb | 1.2 |  | 
 
 
 
 
 
 
 
 | 183 | pjm2 | 1.8 | &print_pair(0, "packet.os.name", $os_name); | 
 
 
 
 
 | 184 |  |  | &print_pair(0, "packet.os.release", $os_release); | 
 
 
 
 
 | 185 |  |  | &print_pair(0, "packet.os.platform", $os_platform); | 
 
 
 
 
 | 186 |  |  | &print_pair(0, "packet.os.sysname", $os_sysname); | 
 
 
 
 
 | 187 |  |  | &print_pair(0, "packet.os.version", $os_version); | 
 
 
 
 
 
 
 
 | 188 | tdb | 1.2 |  | 
 
 
 
 
 
 
 
 | 189 | pjm2 | 1.1 | } |