| 21 |
|
# You'd be silly not to use this ;) |
| 22 |
|
use strict; |
| 23 |
|
|
| 24 |
+ |
# Paths |
| 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 |
+ |
my($uptimebin) = "/usr/bin/uptime"; |
| 30 |
+ |
|
| 31 |
|
# Run the following components: - |
| 32 |
|
&print_ident(); |
| 33 |
|
&include_osver(); |
| 34 |
+ |
&include_uptime(); |
| 35 |
|
&include_users(); |
| 36 |
|
&include_top(); |
| 37 |
|
&include_disk(); |
| 56 |
|
|
| 57 |
|
# sub to print pairs of data, separated by a single space character. |
| 58 |
|
# If the second argument is undefined, then the pair is still printed, |
| 59 |
< |
# however, the value shall be displayed as the string "unknown". |
| 60 |
< |
sub print_pair($$) { |
| 61 |
< |
my($name, $value) = @_; |
| 59 |
> |
# however, the value shall be displayed as the the 'default' value |
| 60 |
> |
# if the passed value was undefined. |
| 61 |
> |
sub print_pair($$$) { |
| 62 |
> |
my($default, $name, $value) = @_; |
| 63 |
|
|
| 64 |
|
if (!defined $value) { |
| 65 |
< |
$value = "unknown"; |
| 65 |
> |
$value = $default; |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
# Remove the trailing linefeed if we've not already done so. |
| 77 |
|
sub include_disk() { |
| 78 |
|
|
| 79 |
|
# Run the df program. |
| 80 |
< |
my(@df) = `df -a`; |
| 80 |
> |
my(@df) = `$dfbin -ak`; |
| 81 |
|
|
| 73 |
– |
# Only look for these partitions at the moment. |
| 74 |
– |
my(@partition_list) = qw{ / /home /var /tmp }; |
| 75 |
– |
|
| 82 |
|
# Go through each line of the program, looking for each thing we want. |
| 77 |
– |
my($scan_for) = '('.join('|', @partition_list).')'; |
| 83 |
|
my($partition_no) = 0; |
| 84 |
|
for (my($i) = 0; $i < $#df; $i++) { |
| 85 |
|
my($line) = $df[$i]; |
| 86 |
< |
$line =~ /^$scan_for\s*\(([^\s]*)\s*\):\s*([0-9]*)\s*blocks\s*([0-9]*)\s*files/; |
| 86 |
> |
$line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; |
| 87 |
|
# $4 will not match unless everything before it does... |
| 88 |
< |
if (defined $4) { |
| 89 |
< |
my ($partition, $mounted, $blocks, $files) = ($1, $2, $3, $4); |
| 90 |
< |
&print_pair("packet.disk.p$partition_no.attributes.name", $partition); |
| 91 |
< |
&print_pair("packet.disk.p$partition_no.mounted", $mounted); |
| 92 |
< |
&print_pair("packet.disk.p$partition_no.blocks", $blocks); |
| 93 |
< |
&print_pair("packet.disk.p$partition_no.name", $files); |
| 88 |
> |
if (defined $5) { |
| 89 |
> |
my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); |
| 90 |
> |
&print_pair("unknown", "packet.disk.p$partition_no.attributes.name", $filesystem); |
| 91 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); |
| 92 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.used", $used); |
| 93 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.avail", $avail); |
| 94 |
> |
&print_pair("unknown", "packet.disk.p$partition_no.attributes.mount", $mount); |
| 95 |
|
++$partition_no; |
| 96 |
|
} |
| 97 |
|
} |
| 103 |
|
sub include_users() { |
| 104 |
|
|
| 105 |
|
# Find out all users on this machine. |
| 106 |
< |
my($users) = `users`; |
| 107 |
< |
my(@users) = split(/\s+/, $users); |
| 106 |
> |
my($users) = `$usersbin`; |
| 107 |
> |
$users = "\n" unless defined $users; |
| 108 |
> |
chop $users; |
| 109 |
> |
my($users_count) = 0; |
| 110 |
> |
$users_count++ while $users =~ /\w+/g; |
| 111 |
> |
my($users_list) = $users." "; |
| 112 |
|
|
| 113 |
< |
my($users_count) = $#users + 1; |
| 114 |
< |
my($users_list) = $users; |
| 105 |
< |
|
| 106 |
< |
&print_pair("packet.users.count", $users_count); |
| 107 |
< |
&print_pair("packet.users.list", $users_list); |
| 113 |
> |
&print_pair(0, "packet.users.count", $users_count); |
| 114 |
> |
&print_pair("unknown", "packet.users.list", $users_list); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| 120 |
|
sub include_top() { |
| 121 |
|
|
| 122 |
|
# Find out some numbers from top. |
| 123 |
< |
my(@top) = `top -d2 -s1 0`; |
| 123 |
> |
my(@top) = `$topbin -d2 -s1 0`; |
| 124 |
|
my($top) = join(" ", @top); |
| 125 |
|
$top =~ s/\n//g; |
| 126 |
|
|
| 127 |
< |
&print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
| 128 |
< |
&print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
| 129 |
< |
&print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
| 130 |
< |
&print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/); |
| 131 |
< |
&print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
| 132 |
< |
&print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
| 133 |
< |
&print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
| 134 |
< |
&print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
| 135 |
< |
&print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
| 136 |
< |
&print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/); |
| 137 |
< |
&print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
| 138 |
< |
&print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
| 139 |
< |
&print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
| 140 |
< |
&print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/); |
| 141 |
< |
&print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/); |
| 142 |
< |
&print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); |
| 143 |
< |
&print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); |
| 144 |
< |
|
| 127 |
> |
&print_pair(0, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
| 128 |
> |
&print_pair(0, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
| 129 |
> |
&print_pair(0, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s/); |
| 130 |
> |
&print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
| 131 |
> |
&print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
| 132 |
> |
&print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
| 133 |
> |
&print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
| 134 |
> |
&print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
| 135 |
> |
&print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
| 136 |
> |
&print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/); |
| 137 |
> |
&print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
| 138 |
> |
&print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
| 139 |
> |
&print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
| 140 |
> |
|
| 141 |
> |
# The following need to be specified in megabytes. |
| 142 |
> |
# If they are preceeded by a G, then multiply by 1024. |
| 143 |
> |
|
| 144 |
> |
$top =~ /([^\s]+?)([MG]) real/; |
| 145 |
> |
my($real) = $1; |
| 146 |
> |
$real*=1024 if $2 eq "G"; |
| 147 |
> |
&print_pair(0, "packet.memory.real", $real); |
| 148 |
> |
|
| 149 |
> |
$top =~ /([^\s]+?)([MG]) free/; |
| 150 |
> |
my($free) = $1; |
| 151 |
> |
$free*=1024 if $2 eq "G"; |
| 152 |
> |
&print_pair(0, "packet.memory.free", $free); |
| 153 |
> |
|
| 154 |
> |
$top =~ /([^\s]+?)([MG]) swap in use/; |
| 155 |
> |
my($swap_in_use) = $1; |
| 156 |
> |
$swap_in_use*=1024 if $2 eq "G"; |
| 157 |
> |
# DO NOT print this one out... save it for in a moment... |
| 158 |
> |
|
| 159 |
> |
$top =~ /([^\s]+?)([MG]) swap free/; |
| 160 |
> |
my($swap_free) = $1; |
| 161 |
> |
$swap_free*=1024 if $2 eq "G"; |
| 162 |
> |
&print_pair(0, "packet.memory.swap_free", $swap_free); |
| 163 |
> |
|
| 164 |
> |
# AJ requested total swap instead of swap_in_use, so here we go! |
| 165 |
> |
&print_pair(0, "packet.memory.swap_total", $swap_free + $swap_in_use); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
# sub to get details of the machine's operating system. |
| 171 |
|
# Find out details about the operating system |
| 172 |
|
# If these values remain undefined, then the print_pair |
| 173 |
|
# function shall show the value to be the string "unknown". |
| 174 |
< |
my($os_name) = `uname -s`; |
| 175 |
< |
my($os_release) = `uname -r`; |
| 176 |
< |
my($os_platform) = `uname -m`; |
| 177 |
< |
my($os_sysname) = `uname -n`; |
| 178 |
< |
my($os_version) = `uname -v`; |
| 174 |
> |
my($os_name) = `$unamebin -s`; |
| 175 |
> |
my($os_release) = `$unamebin -r`; |
| 176 |
> |
my($os_platform) = `$unamebin -m`; |
| 177 |
> |
my($os_sysname) = `$unamebin -n`; |
| 178 |
> |
my($os_version) = `$unamebin -v`; |
| 179 |
|
|
| 180 |
< |
&print_pair("packet.os.name", $os_name); |
| 181 |
< |
&print_pair("packet.os.release", $os_release); |
| 182 |
< |
&print_pair("packet.os.platform", $os_platform); |
| 183 |
< |
&print_pair("packet.os.sysname", $os_sysname); |
| 184 |
< |
&print_pair("packet.os.version", $os_version); |
| 180 |
> |
&print_pair("unknown", "packet.os.name", $os_name); |
| 181 |
> |
&print_pair("unknown", "packet.os.release", $os_release); |
| 182 |
> |
&print_pair("unknown", "packet.os.platform", $os_platform); |
| 183 |
> |
&print_pair("unknown", "packet.os.sysname", $os_sysname); |
| 184 |
> |
&print_pair("unknown", "packet.os.version", $os_version); |
| 185 |
> |
|
| 186 |
> |
} |
| 187 |
> |
|
| 188 |
> |
# sub to get system uptime. |
| 189 |
> |
sub include_uptime() { |
| 190 |
> |
|
| 191 |
> |
# grab the uptime |
| 192 |
> |
my($uptime) = `$uptimebin`; |
| 193 |
> |
|
| 194 |
> |
# work out the days, hours, and minutes |
| 195 |
> |
if ($uptime =~ /hr/) { |
| 196 |
> |
# 0 minutes |
| 197 |
> |
$uptime =~ s/up ([0-9]+) .*, ([0-9]+) .*,/$1:$2:0/; |
| 198 |
> |
} |
| 199 |
> |
elsif ($uptime =~ /min/) { |
| 200 |
> |
# 0 hours |
| 201 |
> |
$uptime =~ s/up ([0-9]+) .*, ([0-9]+) .*,/$1:0:$2/; |
| 202 |
> |
} |
| 203 |
> |
elsif ($uptime =~ /day/) { |
| 204 |
> |
# normal |
| 205 |
> |
$uptime =~ s/up ([0-9]+) .*, ([0-9]+):([0-9]+)/$1:$2:$3/; |
| 206 |
> |
} |
| 207 |
> |
else { |
| 208 |
> |
# 0 days |
| 209 |
> |
$uptime =~ s/up ([0-9]+):([0-9]+)/0:$1:$2/; |
| 210 |
> |
} |
| 211 |
> |
|
| 212 |
> |
# turn into minutes |
| 213 |
> |
|
| 214 |
> |
$uptime =~ /([0-9]+):([0-9]+):([0-9]+)/; |
| 215 |
> |
$uptime = $3 + ($2 + $1*24)*60; |
| 216 |
> |
|
| 217 |
> |
&print_pair("unknown", "packet.os.uptime", $uptime); |
| 218 |
|
|
| 219 |
|
} |