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(); |
38 |
|
|
39 |
+ |
# End the program normally. |
40 |
|
exit(0); |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
+ |
|
46 |
+ |
|
47 |
+ |
|
48 |
+ |
|
49 |
+ |
# prints out an identifier for this version of statgrab.pl |
50 |
+ |
# the host should check this when reading data |
51 |
+ |
# means the host must be checked and updated to work with newer versions. |
52 |
+ |
sub print_ident() { |
53 |
+ |
print 'version statgrab.pl $Revision$'; |
54 |
+ |
print "\n"; |
55 |
+ |
} |
56 |
+ |
|
57 |
|
# sub to print pairs of data, separated by a single space character. |
58 |
< |
sub print_pair($$) { |
59 |
< |
my($name, $value) = @_; |
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 |
> |
# If $type is non-zero, then "0" is printed instead of "unknown". |
61 |
> |
sub print_pair($$$) { |
62 |
> |
my($type, $name, $value) = @_; |
63 |
|
|
64 |
|
if (!defined $value) { |
65 |
< |
$value = "unknown"; |
65 |
> |
if ($type) { |
66 |
> |
$value = "0.00"; |
67 |
> |
} |
68 |
> |
else { |
69 |
> |
$value = "unknown"; |
70 |
> |
} |
71 |
|
} |
72 |
|
|
73 |
|
# Remove the trailing linefeed if we've not already done so. |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
+ |
# sub to find out disk partition information, if it exists. |
82 |
+ |
sub include_disk() { |
83 |
+ |
|
84 |
+ |
# Run the df program. |
85 |
+ |
my(@df) = `$dfbin -ak`; |
86 |
+ |
|
87 |
+ |
# Go through each line of the program, looking for each thing we want. |
88 |
+ |
my($partition_no) = 0; |
89 |
+ |
for (my($i) = 0; $i < $#df; $i++) { |
90 |
+ |
my($line) = $df[$i]; |
91 |
+ |
$line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; |
92 |
+ |
# $4 will not match unless everything before it does... |
93 |
+ |
if (defined $5) { |
94 |
+ |
my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); |
95 |
+ |
&print_pair(0, "packet.disk.p$partition_no.attributes.name", $filesystem); |
96 |
+ |
&print_pair(1, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); |
97 |
+ |
&print_pair(1, "packet.disk.p$partition_no.attributes.used", $used); |
98 |
+ |
&print_pair(1, "packet.disk.p$partition_no.attributes.avail", $avail); |
99 |
+ |
&print_pair(0, "packet.disk.p$partition_no.attributes.mount", $mount); |
100 |
+ |
++$partition_no; |
101 |
+ |
} |
102 |
+ |
} |
103 |
+ |
|
104 |
+ |
} |
105 |
+ |
|
106 |
+ |
# sub to find out the list of all (non-unique) usernames logged |
107 |
+ |
# in to the machine and how many their are. (not |
108 |
|
sub include_users() { |
109 |
|
|
110 |
|
# Find out all users on this machine. |
111 |
< |
my($users) = `users`; |
112 |
< |
my(@users) = split(/\s+/, $users); |
111 |
> |
my($users) = `$usersbin`; |
112 |
> |
$users = "\n" unless defined $users; |
113 |
> |
chop $users; |
114 |
> |
my($users_count) = 0; |
115 |
> |
$users_count++ while $users =~ /\w+/g; |
116 |
> |
my($users_list) = $users." "; |
117 |
|
|
118 |
< |
my($users_count) = $#users + 1; |
119 |
< |
my($users_list) = $users; |
57 |
< |
|
58 |
< |
&print_pair("packet.users.count", $users_count); |
59 |
< |
&print_pair("packet.users.list", $users_list); |
118 |
> |
&print_pair(1, "packet.users.count", $users_count); |
119 |
> |
&print_pair(0, "packet.users.list", $users_list); |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
+ |
# sub to run a series of regexps on the output of 'top' to |
124 |
+ |
# gather various machine statistics. |
125 |
|
sub include_top() { |
126 |
|
|
127 |
|
# Find out some numbers from top. |
128 |
< |
my(@top) = `top -d2 -s1 0`; |
128 |
> |
my(@top) = `$topbin -d2 -s1 0`; |
129 |
|
my($top) = join(" ", @top); |
130 |
|
$top =~ s/\n//g; |
131 |
|
|
132 |
< |
&print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
133 |
< |
&print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
134 |
< |
&print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
135 |
< |
&print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/); |
136 |
< |
&print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
137 |
< |
&print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
138 |
< |
&print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
139 |
< |
&print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
140 |
< |
&print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
141 |
< |
&print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/); |
142 |
< |
&print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
143 |
< |
&print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
144 |
< |
&print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
145 |
< |
&print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/); |
146 |
< |
&print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/); |
147 |
< |
&print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); |
148 |
< |
&print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); |
149 |
< |
|
132 |
> |
&print_pair(1, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
133 |
> |
&print_pair(1, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
134 |
> |
&print_pair(1, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s.*/); |
135 |
> |
&print_pair(1, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
136 |
> |
&print_pair(1, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
137 |
> |
&print_pair(1, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
138 |
> |
&print_pair(1, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
139 |
> |
&print_pair(1, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
140 |
> |
&print_pair(1, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
141 |
> |
&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/); |
145 |
> |
|
146 |
> |
# The following need to be specified in megabytes. |
147 |
> |
# If they are preceeded by a G, then multiply by 1024. |
148 |
> |
|
149 |
> |
$top =~ /([^\s]+?)([MG]) real/; |
150 |
> |
my($real) = $1; |
151 |
> |
$real*=1024 if $2 eq "G"; |
152 |
> |
&print_pair(1, "packet.memory.real", $real); |
153 |
> |
|
154 |
> |
$top =~ /([^\s]+?)([MG]) free/; |
155 |
> |
my($free) = $1; |
156 |
> |
$free*=1024 if $2 eq "G"; |
157 |
> |
&print_pair(1, "packet.memory.free", $free); |
158 |
> |
|
159 |
> |
$top =~ /([^\s]+?)([MG]) swap in use/; |
160 |
> |
my($swap_in_use) = $1; |
161 |
> |
$swap_in_use*=1024 if $2 eq "G"; |
162 |
> |
# DO NOT print this one out... save it for in a moment... |
163 |
> |
|
164 |
> |
$top =~ /([^\s]+?)([MG]) swap free/; |
165 |
> |
my($swap_free) = $1; |
166 |
> |
$swap_free*=1024 if $2 eq "G"; |
167 |
> |
&print_pair(1, "packet.memory.swap_free", $swap_free); |
168 |
> |
|
169 |
> |
# AJ requested total swap instead of swap_in_use, so here we go! |
170 |
> |
&print_pair(1, "packet.memory.swap_total", $swap_free + $swap_in_use); |
171 |
|
} |
172 |
|
|
173 |
+ |
# sub to get details of the machine's operating system. |
174 |
|
sub include_osver() { |
175 |
|
|
176 |
|
# Find out details about the operating system |
177 |
< |
my($os_name) = `uname -s`; |
178 |
< |
my($os_release) = `uname -r`; |
179 |
< |
my($os_platform) = `uname -m`; |
180 |
< |
my($os_sysname) = `uname -n`; |
181 |
< |
my($os_version) = `uname -v`; |
177 |
> |
# If these values remain undefined, then the print_pair |
178 |
> |
# function shall show the value to be the string "unknown". |
179 |
> |
my($os_name) = `$unamebin -s`; |
180 |
> |
my($os_release) = `$unamebin -r`; |
181 |
> |
my($os_platform) = `$unamebin -m`; |
182 |
> |
my($os_sysname) = `$unamebin -n`; |
183 |
> |
my($os_version) = `$unamebin -v`; |
184 |
|
|
185 |
< |
&print_pair("packet.os.name", $os_name); |
186 |
< |
&print_pair("packet.os.release", $os_release); |
187 |
< |
&print_pair("packet.os.platform", $os_platform); |
188 |
< |
&print_pair("packet.os.sysname", $os_sysname); |
189 |
< |
&print_pair("packet.os.version", $os_version); |
185 |
> |
&print_pair(0, "packet.os.name", $os_name); |
186 |
> |
&print_pair(0, "packet.os.release", $os_release); |
187 |
> |
&print_pair(0, "packet.os.platform", $os_platform); |
188 |
> |
&print_pair(0, "packet.os.sysname", $os_sysname); |
189 |
> |
&print_pair(0, "packet.os.version", $os_version); |
190 |
> |
|
191 |
> |
} |
192 |
> |
|
193 |
> |
# sub to get system uptime. |
194 |
> |
sub include_uptime() { |
195 |
> |
|
196 |
> |
# grab the uptime |
197 |
> |
my($uptime) = `$uptimebin`; |
198 |
> |
|
199 |
> |
# work out the days, hours, and minutes |
200 |
> |
if ($uptime =~ /hr/) { |
201 |
> |
# 0 minutes |
202 |
> |
$uptime =~ /up ([0-9]+) .*, ([0-9]+) .*,/; |
203 |
> |
$uptime = "$1:$2:0"; |
204 |
> |
} |
205 |
> |
elsif ($uptime =~ /min/) { |
206 |
> |
# 0 hours |
207 |
> |
$uptime =~ /up ([0-9]+) .*, ([0-9]+) .*,/; |
208 |
> |
$uptime = "$1:0:$2"; |
209 |
> |
} |
210 |
> |
elsif ($uptime =~ /day/) { |
211 |
> |
# normal |
212 |
> |
$uptime =~ /up ([0-9]+) .*, ([0-9]+):([0-9]+)/; |
213 |
> |
$uptime = "$1:$2:$3"; |
214 |
> |
} |
215 |
> |
else { |
216 |
> |
# 0 days |
217 |
> |
$uptime =~ /up ([0-9]+):([0-9]+)/; |
218 |
> |
$uptime = "0:$1:$2"; |
219 |
> |
} |
220 |
> |
|
221 |
> |
# turn into minutes |
222 |
> |
|
223 |
> |
$uptime =~ /([0-9]+):([0-9]+):([0-9]+)/; |
224 |
> |
$uptime = $3 + ($2 + $1*24)*60; |
225 |
> |
|
226 |
> |
&print_pair(0, "packet.os.uptime", $uptime); |
227 |
|
|
228 |
|
} |