21 |
|
# You'd be silly not to use this ;) |
22 |
|
use strict; |
23 |
|
|
24 |
< |
# 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 |
< |
my($uptimebin) = "/usr/bin/uptime"; |
24 |
> |
# Have to hope this will work really. |
25 |
> |
my($ostype) = `uname -s`; chop($ostype); |
26 |
|
|
27 |
+ |
# Decide which paths we should use. |
28 |
+ |
my($topbin); my($dfbin); my($usersbin); my($unamebin); my($uptimebin); |
29 |
+ |
|
30 |
+ |
if ($ostype eq "SunOS") { |
31 |
+ |
# covers: Solaris 8 |
32 |
+ |
$topbin = "/usr/local/sbin/top -d2 -s1 0"; |
33 |
+ |
$dfbin = "/usr/bin/df"; |
34 |
+ |
$usersbin = "/usr/ucb/users"; |
35 |
+ |
$unamebin = "/usr/bin/uname"; |
36 |
+ |
$uptimebin = "/usr/bin/uptime"; |
37 |
+ |
} |
38 |
+ |
elsif ($ostype eq "Linux") { |
39 |
+ |
# covers: Debian r2.2 |
40 |
+ |
$topbin = "/usr/bin/top -d1 -n0 -b"; |
41 |
+ |
$dfbin = "/bin/df"; |
42 |
+ |
$usersbin = "/usr/bin/users"; |
43 |
+ |
$unamebin = "/bin/uname"; |
44 |
+ |
$uptimebin = "/usr/bin/uptime"; |
45 |
+ |
} |
46 |
+ |
elsif ($ostype eq "FreeBSD") { |
47 |
+ |
# covers: FreeBSD 4.2-STABLE |
48 |
+ |
$topbin = "/usr/bin/top -d2 -s1 0"; |
49 |
+ |
$dfbin = "/bin/df"; |
50 |
+ |
$usersbin = "/usr/bin/users"; |
51 |
+ |
$unamebin = "/usr/bin/uname"; |
52 |
+ |
$uptimebin = "/usr/bin/uptime"; |
53 |
+ |
} |
54 |
+ |
else { |
55 |
+ |
print "statgrab.pl Error: Unable to identify system type - \"$ostype\".\n"; |
56 |
+ |
print "\"uname -s\" does not report one of the following known types;\n"; |
57 |
+ |
print " SunOS, Linux, FreeBSD\n"; |
58 |
+ |
exit(1); |
59 |
+ |
} |
60 |
+ |
|
61 |
|
# Run the following components: - |
62 |
|
&print_ident(); |
63 |
|
&include_osver(); |
72 |
|
|
73 |
|
|
74 |
|
|
45 |
– |
|
46 |
– |
|
47 |
– |
|
48 |
– |
|
75 |
|
# prints out an identifier for this version of statgrab.pl |
76 |
|
# the host should check this when reading data |
77 |
|
# means the host must be checked and updated to work with newer versions. |
82 |
|
|
83 |
|
# sub to print pairs of data, separated by a single space character. |
84 |
|
# If the second argument is undefined, then the pair is still printed, |
85 |
< |
# however, the value shall be displayed as the string "unknown". |
86 |
< |
# If $type is non-zero, then "0" is printed instead of "unknown". |
85 |
> |
# however, the value shall be displayed as the the 'default' value |
86 |
> |
# if the passed value was undefined. |
87 |
|
sub print_pair($$$) { |
88 |
< |
my($type, $name, $value) = @_; |
88 |
> |
my($default, $name, $value) = @_; |
89 |
|
|
90 |
|
if (!defined $value) { |
91 |
< |
if ($type) { |
66 |
< |
$value = "0.00"; |
67 |
< |
} |
68 |
< |
else { |
69 |
< |
$value = "unknown"; |
70 |
< |
} |
91 |
> |
$value = $default; |
92 |
|
} |
93 |
|
|
94 |
|
# Remove the trailing linefeed if we've not already done so. |
113 |
|
# $4 will not match unless everything before it does... |
114 |
|
if (defined $5) { |
115 |
|
my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); |
116 |
< |
&print_pair(0, "packet.disk.p$partition_no.attributes.name", $filesystem); |
117 |
< |
&print_pair(1, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); |
118 |
< |
&print_pair(1, "packet.disk.p$partition_no.attributes.used", $used); |
119 |
< |
&print_pair(1, "packet.disk.p$partition_no.attributes.avail", $avail); |
120 |
< |
&print_pair(0, "packet.disk.p$partition_no.attributes.mount", $mount); |
116 |
> |
&print_pair("unknown", "packet.disk.p$partition_no.attributes.name", $filesystem); |
117 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); |
118 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.used", $used); |
119 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.avail", $avail); |
120 |
> |
&print_pair("unknown", "packet.disk.p$partition_no.attributes.mount", $mount); |
121 |
|
++$partition_no; |
122 |
|
} |
123 |
|
} |
134 |
|
chop $users; |
135 |
|
my($users_count) = 0; |
136 |
|
$users_count++ while $users =~ /\w+/g; |
137 |
< |
my($users_list) = $users; |
137 |
> |
my($users_list) = $users." "; |
138 |
|
|
139 |
< |
&print_pair(1, "packet.users.count", $users_count); |
140 |
< |
&print_pair(0, "packet.users.list", $users_list); |
139 |
> |
&print_pair(0, "packet.users.count", $users_count); |
140 |
> |
&print_pair("unknown", "packet.users.list", $users_list); |
141 |
|
} |
142 |
|
|
143 |
|
|
146 |
|
sub include_top() { |
147 |
|
|
148 |
|
# Find out some numbers from top. |
149 |
< |
my(@top) = `$topbin -d2 -s1 0`; |
149 |
> |
my(@top) = `$topbin`; |
150 |
|
my($top) = join(" ", @top); |
151 |
|
$top =~ s/\n//g; |
152 |
|
|
153 |
< |
&print_pair(1, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
154 |
< |
&print_pair(1, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
155 |
< |
&print_pair(1, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
156 |
< |
&print_pair(1, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
157 |
< |
&print_pair(1, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
158 |
< |
&print_pair(1, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
159 |
< |
&print_pair(1, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
160 |
< |
&print_pair(1, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
161 |
< |
&print_pair(1, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
162 |
< |
&print_pair(1, "packet.cpu.user", $top =~ /([^\s]+?)% user/); |
142 |
< |
&print_pair(1, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
143 |
< |
&print_pair(1, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
144 |
< |
&print_pair(1, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
153 |
> |
&print_pair(0, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
154 |
> |
&print_pair(0, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
155 |
> |
&print_pair(0, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
156 |
> |
&print_pair(0, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
157 |
> |
&print_pair(0, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
158 |
> |
&print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
159 |
> |
&print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/); |
160 |
> |
&print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
161 |
> |
&print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
162 |
> |
&print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
163 |
|
|
164 |
|
# The following need to be specified in megabytes. |
165 |
|
# If they are preceeded by a G, then multiply by 1024. |
167 |
|
$top =~ /([^\s]+?)([MG]) real/; |
168 |
|
my($real) = $1; |
169 |
|
$real*=1024 if $2 eq "G"; |
170 |
< |
&print_pair(1, "packet.memory.real", $real); |
170 |
> |
&print_pair(0, "packet.memory.total", $real); |
171 |
|
|
172 |
|
$top =~ /([^\s]+?)([MG]) free/; |
173 |
|
my($free) = $1; |
174 |
|
$free*=1024 if $2 eq "G"; |
175 |
< |
&print_pair(1, "packet.memory.free", $free); |
175 |
> |
&print_pair(0, "packet.memory.free", $free); |
176 |
|
|
177 |
|
$top =~ /([^\s]+?)([MG]) swap in use/; |
178 |
|
my($swap_in_use) = $1; |
182 |
|
$top =~ /([^\s]+?)([MG]) swap free/; |
183 |
|
my($swap_free) = $1; |
184 |
|
$swap_free*=1024 if $2 eq "G"; |
185 |
< |
&print_pair(1, "packet.memory.swap_free", $swap_free); |
185 |
> |
&print_pair(0, "packet.swap.free", $swap_free); |
186 |
|
|
187 |
|
# AJ requested total swap instead of swap_in_use, so here we go! |
188 |
< |
&print_pair(1, "packet.memory.swap_total", $swap_free + $swap_in_use); |
188 |
> |
&print_pair(0, "packet.swap.total", $swap_free + $swap_in_use); |
189 |
|
} |
190 |
|
|
191 |
|
# sub to get details of the machine's operating system. |
200 |
|
my($os_sysname) = `$unamebin -n`; |
201 |
|
my($os_version) = `$unamebin -v`; |
202 |
|
|
203 |
< |
&print_pair(0, "packet.os.name", $os_name); |
204 |
< |
&print_pair(0, "packet.os.release", $os_release); |
205 |
< |
&print_pair(0, "packet.os.platform", $os_platform); |
206 |
< |
&print_pair(0, "packet.os.sysname", $os_sysname); |
207 |
< |
&print_pair(0, "packet.os.version", $os_version); |
203 |
> |
&print_pair("unknown", "packet.os.name", $os_name); |
204 |
> |
&print_pair("unknown", "packet.os.release", $os_release); |
205 |
> |
&print_pair("unknown", "packet.os.platform", $os_platform); |
206 |
> |
&print_pair("unknown", "packet.os.sysname", $os_sysname); |
207 |
> |
&print_pair("unknown", "packet.os.version", $os_version); |
208 |
|
|
209 |
|
} |
210 |
|
|
211 |
< |
# sub to get system uptime. |
211 |
> |
# sub to get system uptime in seconds. |
212 |
|
sub include_uptime() { |
213 |
|
|
214 |
< |
# Need a regexp guru to strip the junk on this line |
214 |
> |
# debug stuff, all the different cases |
215 |
> |
|
216 |
> |
# normal |
217 |
> |
#my($uptime) = " 4:48pm up 49 day(s), 6:30, 201 users, load average: 0.33, 0.35, 0.38\n"; |
218 |
> |
# 0 days |
219 |
> |
#my($uptime) = " 4:48pm up 6:30, 201 users, load average: 0.33, 0.35, 0.38\n"; |
220 |
> |
# 0 hours |
221 |
> |
#my($uptime) = " 4:48pm up 49 day(s), 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
222 |
> |
# 0 mins |
223 |
> |
#my($uptime) = " 4:48pm up 49 day(s), 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
224 |
> |
# 0 days and 0 mins |
225 |
> |
#my($uptime) = " 4:48pm up 6 hr(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
226 |
> |
# 0 days and 0 hours |
227 |
> |
#my($uptime) = " 4:48pm up 30 min(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
228 |
> |
# 0 hours and 0 mins |
229 |
> |
#my($uptime) = " 4:48pm up 49 day(s), 201 users, load average: 0.33, 0.35, 0.38\n"; |
230 |
> |
|
231 |
> |
# grab the uptime |
232 |
|
my($uptime) = `$uptimebin`; |
233 |
+ |
|
234 |
+ |
&print_pair(0, "packet.load.load1", $uptime =~ /load average.?:\s*([^\s]+?),/); |
235 |
+ |
&print_pair(0, "packet.load.load5", $uptime =~ /load average.?:\s*.+?,\s*([^\s]+?),/); |
236 |
+ |
&print_pair(0, "packet.load.load15", $uptime =~ /load average.?:\s*.+?,\s*.+?,\s*([^\s]+)/); |
237 |
|
|
238 |
< |
&print_pair(0, "packet.os.uptime", $uptime); |
238 |
> |
# work out the days, hours, and minutes |
239 |
> |
|
240 |
> |
if ($uptime =~ /day.*,\s+([0-9]+):([0-9]+)/) { |
241 |
> |
# normal |
242 |
> |
$uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+):([0-9]+)/; |
243 |
> |
$uptime = "$1:$2:$3"; |
244 |
> |
} |
245 |
> |
else { |
246 |
> |
if ($uptime =~ /day/) { |
247 |
> |
if ($uptime =~ /hr/) { |
248 |
> |
# 0 minutes |
249 |
> |
$uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/; |
250 |
> |
$uptime = "$1:$2:0"; |
251 |
> |
} |
252 |
> |
elsif ($uptime =~ /min/) { |
253 |
> |
# 0 hours |
254 |
> |
$uptime =~ /up\s+([0-9]+)\s+.*,\s+([0-9]+)\s+.*,/; |
255 |
> |
$uptime = "$1:0:$2"; |
256 |
> |
} |
257 |
> |
else { |
258 |
> |
# 0 hours and 0 mins |
259 |
> |
$uptime =~ /up\s+([0-9]+)/; |
260 |
> |
$uptime = "$1:0:0"; |
261 |
> |
} |
262 |
> |
} |
263 |
> |
elsif ($uptime =~ /hr/) { |
264 |
> |
# 0 days and 0 minutes |
265 |
> |
$uptime =~ /up\s+([0-9]+)\s+/; |
266 |
> |
$uptime = "0:$1:0"; |
267 |
> |
} |
268 |
> |
elsif ($uptime =~ /min/) { |
269 |
> |
# 0 days and 0 hours |
270 |
> |
$uptime =~ /up\s+([0-9]+)\s+/; |
271 |
> |
$uptime = "0:0:$1"; |
272 |
> |
} |
273 |
> |
else { |
274 |
> |
# 0 days |
275 |
> |
$uptime =~ /up\s+([0-9]+):([0-9]+)/; |
276 |
> |
$uptime = "0:$1:$2"; |
277 |
> |
} |
278 |
> |
} |
279 |
> |
|
280 |
> |
# turn into seconds |
281 |
> |
$uptime =~ /([0-9]+):([0-9]+):([0-9]+)/; |
282 |
> |
$uptime = ($3+($2+($1*24))*60)*60; |
283 |
> |
|
284 |
> |
# print the value out |
285 |
> |
&print_pair("unknown", "packet.os.uptime", $uptime); |
286 |
|
|
287 |
< |
} |
287 |
> |
} |