1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# ----------------------------------------------------------- |
4 |
# Perl i-scream Host. |
5 |
# http://www.i-scream.org.uk |
6 |
# |
7 |
# An all-in-one script to act as an i-scream host on |
8 |
# a typical Unix/Linux box. You may adapt the data-gathering |
9 |
# methods as you see fit. |
10 |
# - pjm2@ukc.ac.uk |
11 |
# |
12 |
# $Author: tdb1 $ |
13 |
# $Id: ihost.pl,v 1.20 2001/02/28 10:53:13 tdb1 Exp $ |
14 |
#------------------------------------------------------------ |
15 |
|
16 |
$| = 1; |
17 |
|
18 |
use strict; |
19 |
use IO::Socket; |
20 |
use Sys::Hostname; |
21 |
|
22 |
use vars qw ( |
23 |
$filter_manager_addr |
24 |
$filter_manager_port |
25 |
$seq_no |
26 |
$udp_update_time |
27 |
$tcp_update_time |
28 |
$last_udp_time |
29 |
$last_tcp_time |
30 |
$last_modified |
31 |
$udp_port |
32 |
$tcp_port |
33 |
$filter_addr |
34 |
$file_list |
35 |
$fqdn |
36 |
); |
37 |
|
38 |
if (@ARGV != 2) { |
39 |
die "Usage: ihost.pl [i-scream filter manager] [TCP port]\n"; |
40 |
} |
41 |
|
42 |
$filter_manager_addr = $ARGV[0]; |
43 |
$filter_manager_port = $ARGV[1]; |
44 |
|
45 |
$seq_no = 1; |
46 |
|
47 |
&tcp_configure(); |
48 |
&send_udp_packet(); |
49 |
|
50 |
$last_udp_time = time; |
51 |
$last_tcp_time = time; |
52 |
while (1) { |
53 |
my($time) = time; |
54 |
if ($time >= $last_udp_time + $udp_update_time) { |
55 |
&send_udp_packet(); |
56 |
$last_udp_time = $time; |
57 |
} |
58 |
if ($time >= $last_tcp_time + $tcp_update_time) { |
59 |
&send_tcp_heartbeat(); |
60 |
$last_tcp_time = $time; |
61 |
} |
62 |
my($next_udp) = $udp_update_time - $time + $last_udp_time; |
63 |
my($next_tcp) = $tcp_update_time - $time + $last_tcp_time; |
64 |
my($delay); |
65 |
if ($next_udp < $next_tcp) { |
66 |
$delay = $next_udp |
67 |
} |
68 |
else { |
69 |
$delay = $next_tcp; |
70 |
} |
71 |
`sleep $delay`; |
72 |
} |
73 |
|
74 |
exit(0); |
75 |
|
76 |
|
77 |
#----------------------------------------------------------------------- |
78 |
# tcp_configure |
79 |
# Establishes a TCP connection to the specified i-scream filter manager. |
80 |
# The host then requests details from the server, such as the intervals |
81 |
# at which to send UDP packets. |
82 |
#----------------------------------------------------------------------- |
83 |
sub tcp_configure() { |
84 |
|
85 |
my($sock) = new IO::Socket::INET( |
86 |
PeerAddr => $filter_manager_addr, |
87 |
PeerPort => $filter_manager_port, |
88 |
Proto => 'tcp' |
89 |
) or die "Cannot connect!"; |
90 |
if (!defined $sock) { |
91 |
print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n"; |
92 |
print "Please check that there is an i-scream server at this address.\n"; |
93 |
print "Program ended.\n"; |
94 |
exit(1); |
95 |
} |
96 |
|
97 |
# Now run through the configuration process... |
98 |
my($response); |
99 |
|
100 |
print $sock "STARTCONFIG\n"; |
101 |
$response = <$sock>; |
102 |
if (!chop $response eq "OK") { |
103 |
print "The i-scream server rejected the STARTCONFIG command. Terminated."; |
104 |
exit(1); |
105 |
} |
106 |
|
107 |
print "Config started okay.\n"; |
108 |
|
109 |
print $sock "LASTMODIFIED\n"; |
110 |
$response = <$sock>; |
111 |
chop $response; |
112 |
$last_modified = $response; |
113 |
|
114 |
print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n"; |
115 |
|
116 |
print $sock "FILELIST\n"; |
117 |
$response = <$sock>; |
118 |
chop $response; |
119 |
$file_list = $response; |
120 |
|
121 |
print "File list obtained: $file_list\n"; |
122 |
|
123 |
print $sock "FQDN\n"; |
124 |
$response = <$sock>; |
125 |
chop $response; |
126 |
$fqdn = $response; |
127 |
|
128 |
print "FQDN returned: $fqdn\n"; |
129 |
|
130 |
print $sock "UDPUpdateTime\n"; |
131 |
$response = <$sock>; |
132 |
chop $response; |
133 |
$udp_update_time = $response; |
134 |
|
135 |
print $sock "TCPUpdateTime\n"; |
136 |
$response = <$sock>; |
137 |
chop $response; |
138 |
$tcp_update_time = $response; |
139 |
|
140 |
print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n"; |
141 |
|
142 |
print $sock "ENDCONFIG\n"; |
143 |
$response = <$sock>; |
144 |
chomp $response; |
145 |
if (!$response eq "OK") { |
146 |
print "ENDCONFIG command to server failed. Terminated.\n"; |
147 |
exit(1); |
148 |
} |
149 |
|
150 |
print "Config ended.\n"; |
151 |
|
152 |
print $sock "FILTER\n"; |
153 |
$response = <$sock>; |
154 |
chop $response; |
155 |
$response =~ /(.*);(.*);(.*)/; |
156 |
($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3); |
157 |
|
158 |
print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n"; |
159 |
|
160 |
print $sock "END\n"; |
161 |
$response = <$sock>; |
162 |
chop $response; |
163 |
if ($response eq "OK") { |
164 |
print "Host successfully configured via TCP.\n" |
165 |
} |
166 |
else { |
167 |
print "The server failed the host configuration on the END command."; |
168 |
exit(1); |
169 |
} |
170 |
|
171 |
close($sock); |
172 |
|
173 |
print "Configuration finished sucessfully!\n"; |
174 |
|
175 |
return; |
176 |
} |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
#----------------------------------------------------------------------- |
182 |
# send_udp_packet |
183 |
# Sends a UDP packet to an i-scream filter. |
184 |
# The packet contains XML markup describing some of the machine's state. |
185 |
# Receipt of UDP packets is not guaranteed. |
186 |
#----------------------------------------------------------------------- |
187 |
sub send_udp_packet() { |
188 |
|
189 |
my(@statgrab) = `./statgrab.pl`; |
190 |
my(%packet); |
191 |
for (my($i) = 0; $i <= $#statgrab; $i++) { |
192 |
$statgrab[$i] =~ /^([^\s]*) (.*)$/; |
193 |
$packet{$1} = $2; |
194 |
} |
195 |
|
196 |
my($date) = time; |
197 |
|
198 |
my($disk_info) = "<disk>"; |
199 |
my($i) = 0; |
200 |
while (defined $packet{"packet.disk.p$i.attributes.mount"}) { |
201 |
$disk_info .= "<p$i"; |
202 |
$disk_info .= " name=\"" . $packet{"packet.disk.p$i.attributes.name"} . "\""; |
203 |
$disk_info .= " kbytes=\"" . $packet{"packet.disk.p$i.attributes.kbytes"} . "\""; |
204 |
$disk_info .= " used=\"" . $packet{"packet.disk.p$i.attributes.used"} . "\""; |
205 |
$disk_info .= " avail=\"" . $packet{"packet.disk.p$i.attributes.avail"} . "\""; |
206 |
$disk_info .= " mount=\"" . $packet{"packet.disk.p$i.attributes.mount"} . "\""; |
207 |
$disk_info .= "></p$i>"; |
208 |
++$i; |
209 |
} |
210 |
$disk_info .= "</disk>"; |
211 |
|
212 |
my($ip) = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost'); |
213 |
|
214 |
# Build the XML packet this way, as we can clearly |
215 |
# see the structure and contents... I like this ;-) |
216 |
# [Note that the server rejects UDP packets that are |
217 |
# larger than 8196 bytes] |
218 |
my($xml) = <<EOF; |
219 |
|
220 |
<packet seq_no="$seq_no" machine_name="$fqdn" date="$date" type="data" ip="$ip"> |
221 |
<load> |
222 |
<load1>$packet{"packet.load.load1"}</load1> |
223 |
<load5>$packet{"packet.load.load5"}</load5> |
224 |
<load15>$packet{"packet.load.load15"}</load15> |
225 |
</load> |
226 |
<os> |
227 |
<name>$packet{"packet.os.name"}</name> |
228 |
<release>$packet{"packet.os.release"}</release> |
229 |
<platform>$packet{"packet.os.platform"}</platform> |
230 |
<sysname>$packet{"packet.os.sysname"}</sysname> |
231 |
<version>$packet{"packet.os.version"}</version> |
232 |
<uptime>$packet{"packet.os.uptime"}</uptime> |
233 |
</os> |
234 |
<users> |
235 |
<count>$packet{"packet.users.count"}</count> |
236 |
<list>$packet{"packet.users.list"}</list> |
237 |
</users> |
238 |
<processes> |
239 |
<total>$packet{"packet.processes.total"}</total> |
240 |
<sleeping>$packet{"packet.processes.sleeping"}</sleeping> |
241 |
<zombie>$packet{"packet.processes.zombie"}</zombie> |
242 |
<stopped>$packet{"packet.processes.stopped"}</stopped> |
243 |
<cpu>$packet{"packet.processes.cpu"}</cpu> |
244 |
</processes> |
245 |
<cpu> |
246 |
<idle>$packet{"packet.cpu.idle"}</idle> |
247 |
<user>$packet{"packet.cpu.user"}</user> |
248 |
<kernel>$packet{"packet.cpu.kernel"}</kernel> |
249 |
<iowait>$packet{"packet.cpu.iowait"}</iowait> |
250 |
<swap>$packet{"packet.cpu.swap"}</swap> |
251 |
</cpu> |
252 |
<memory> |
253 |
<total>$packet{"packet.memory.total"}</total> |
254 |
<free>$packet{"packet.memory.free"}</free> |
255 |
</memory> |
256 |
<swap> |
257 |
<total>$packet{"packet.swap.total"}</total> |
258 |
<free>$packet{"packet.swap.free"}</free> |
259 |
</swap> |
260 |
$disk_info |
261 |
</packet> |
262 |
|
263 |
EOF |
264 |
|
265 |
# Make the packet smaller by stripping out newlines and leading spaces. |
266 |
$xml =~ s/\n\s*//g; |
267 |
|
268 |
my($sock) = new IO::Socket::INET ( |
269 |
PeerPort => $udp_port, |
270 |
PeerAddr => $filter_addr, |
271 |
Proto => 'udp' |
272 |
) or die "Could not send UDP: $!\n"; |
273 |
|
274 |
print $sock $xml or die "Could not send UDP packet: $!\n"; |
275 |
close($sock); |
276 |
$seq_no++; |
277 |
print "-"; |
278 |
|
279 |
return; |
280 |
} |
281 |
|
282 |
|
283 |
|
284 |
|
285 |
#----------------------------------------------------------------------- |
286 |
# send_tcp_heartbeat |
287 |
# Establishes a TCP connection to an i-scream filter. |
288 |
# The heartbeat is used as a guaranteed "I'm alive" delivery mechanism. |
289 |
#----------------------------------------------------------------------- |
290 |
sub send_tcp_heartbeat() { |
291 |
|
292 |
my($sock) = new IO::Socket::INET( |
293 |
PeerAddr => $filter_addr, |
294 |
PeerPort => $tcp_port, |
295 |
Proto => 'tcp' |
296 |
) or return; |
297 |
if (!defined $sock) { |
298 |
print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n"; |
299 |
return; |
300 |
} |
301 |
|
302 |
# Now run through the configuration process. |
303 |
my($response); |
304 |
|
305 |
print $sock "HEARTBEAT\n"; |
306 |
$response = <$sock>; |
307 |
chop $response; |
308 |
if (!$response eq "OK") { |
309 |
close($sock); |
310 |
print "Server gave wrong response to HEARTBEAT: $response\n"; |
311 |
return; |
312 |
} |
313 |
|
314 |
print $sock "CONFIG\n"; |
315 |
$response = <$sock>; |
316 |
chop $response; |
317 |
if (!$response eq "OK") { |
318 |
close($sock); |
319 |
print "Server gave wrong response to CONFIG: $response\n"; |
320 |
return; |
321 |
} |
322 |
|
323 |
print $sock "$file_list\n"; |
324 |
$response = <$sock>; |
325 |
chop $response; |
326 |
if (!$response eq "OK") { |
327 |
close($sock); |
328 |
print "Server gave wrong response to file list: $response\n"; |
329 |
return; |
330 |
} |
331 |
|
332 |
print $sock "$last_modified\n"; |
333 |
$response = <$sock>; |
334 |
chop $response; |
335 |
if ($response eq "ERROR") { |
336 |
close($sock); |
337 |
&tcp_configure(); |
338 |
return; |
339 |
} |
340 |
if (!$response eq "OK") { |
341 |
close($sock); |
342 |
print "Server gave wrong response to HEARTBEAT: $response\n"; |
343 |
return; |
344 |
} |
345 |
|
346 |
print $sock "ENDHEARTBEAT\n"; |
347 |
$response = <$sock>; |
348 |
chop $response; |
349 |
if (!$response eq "OK") { |
350 |
close($sock); |
351 |
print "Server gave wrong response to ENDHEARTBEAT: $response\n"; |
352 |
return; |
353 |
} |
354 |
|
355 |
close($sock); |
356 |
print "^"; |
357 |
|
358 |
return; |
359 |
} |