ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/ihost.pl
Revision: 1.28
Committed: Wed Mar 14 18:48:33 2001 UTC (23 years, 7 months ago) by pjm2
Content type: text/plain
Branch: MAIN
Changes since 1.27: +129 -97 lines
Log Message:
Added retry waits to the configuration process.

File Contents

# Content
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: pjm2 $
13 # $Id: ihost.pl,v 1.27 2001/03/14 18:00:16 pjm2 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 $pidfile
37 $retry_wait
38 );
39
40 if (@ARGV != 2) {
41 die "Usage: ihost.pl [i-scream filter manager] [TCP port]\n";
42 }
43
44 $filter_manager_addr = $ARGV[0];
45 $filter_manager_port = $ARGV[1];
46
47 $seq_no = 1;
48 $retry_wait = 6;
49
50 # write our PID to a file
51 $pidfile = "/var/tmp/ihost.pid";
52 &write_pid();
53
54 &tcp_configure();
55 &send_udp_packet();
56
57 $last_udp_time = time;
58 $last_tcp_time = time;
59 while (1) {
60 my($time) = time;
61 if ($time >= $last_udp_time + $udp_update_time) {
62 &send_udp_packet();
63 $last_udp_time = $time;
64 }
65 if ($time >= $last_tcp_time + $tcp_update_time) {
66 &send_tcp_heartbeat();
67 $last_tcp_time = $time;
68 }
69 my($next_udp) = $udp_update_time - $time + $last_udp_time;
70 my($next_tcp) = $tcp_update_time - $time + $last_tcp_time;
71 my($delay);
72 if ($next_udp < $next_tcp) {
73 $delay = $next_udp
74 }
75 else {
76 $delay = $next_tcp;
77 }
78 `sleep $delay`;
79 }
80
81 # we'll probably never get here...
82 `rm -f $pidfile`;
83 exit(0);
84
85
86 #-----------------------------------------------------------------------
87 # wait_then_retry
88 # Waits for the period of time specified in $retry_wait, then attempts
89 # to reconfigure with the server.
90 #-----------------------------------------------------------------------
91 sub wait_then_retry() {
92 print "Will retry configuration with filter manager in $retry_wait seconds.\n";
93 `sleep $retry_wait`;
94 }
95
96
97 #-----------------------------------------------------------------------
98 # tcp_configure
99 # Establishes a TCP connection to the specified i-scream filter manager.
100 # The host then requests details from the server, such as the intervals
101 # at which to send UDP packets.
102 #-----------------------------------------------------------------------
103 sub tcp_configure() {
104
105 while (1 == 1) {
106 my($sock) = new IO::Socket::INET(
107 PeerAddr => $filter_manager_addr,
108 PeerPort => $filter_manager_port,
109 Proto => 'tcp'
110 ) or die "Cannot connect!";
111 if (!defined $sock) {
112 print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n";
113 print "Please check that there is an i-scream server at this address.\n";
114 close($sock);
115 wait_then_retry();
116 next;
117 }
118
119 # Now run through the configuration process...
120 my($response);
121
122 print $sock "STARTCONFIG\n";
123 $response = <$sock>;
124 if (!chop $response eq "OK") {
125 print "The i-scream server rejected the STARTCONFIG command. Terminated.";
126 close($sock);
127 wait_then_retry();
128 next;
129 }
130
131 print "Config started okay.\n";
132
133 print $sock "LASTMODIFIED\n";
134 $response = <$sock>;
135 chop $response;
136 $last_modified = $response;
137
138 print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
139
140 print $sock "FILELIST\n";
141 $response = <$sock>;
142 chop $response;
143 $file_list = $response;
144
145 print "File list obtained: $file_list\n";
146
147 print $sock "FQDN\n";
148 $response = <$sock>;
149 chop $response;
150 $fqdn = $response;
151
152 print "FQDN returned: $fqdn\n";
153
154 print $sock "UDPUpdateTime\n";
155 $response = <$sock>;
156 chop $response;
157 $udp_update_time = $response;
158
159 print $sock "TCPUpdateTime\n";
160 $response = <$sock>;
161 chop $response;
162 $tcp_update_time = $response;
163
164 print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
165
166 print $sock "ENDCONFIG\n";
167 $response = <$sock>;
168 chomp $response;
169 if (!$response eq "OK") {
170 print "ENDCONFIG command to server failed. Terminated.\n";
171 close($sock);
172 wait_then_retry();
173 next;
174 }
175
176 print "Config ended.\n";
177
178 print $sock "FILTER\n";
179 $response = <$sock>;
180 unless (defined($response)) {
181 print "Failed: Could not get a filter address from the filter manager.\n";
182 close($sock);
183 wait_then_retry();
184 next;
185 }
186 chop $response;
187 $response =~ /(.*);(.*);(.*)/;
188 if ($response eq "ERROR") {
189 print "There are no active configured filters for your host.\n";
190 close($sock);
191 wait_then_retry();
192 next;
193 }
194 ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
195 unless (defined($filter_addr) && defined($udp_port) && defined($tcp_port)) {
196 print "Failed: Filter address response from server did not make sense: $response\n";
197 close($sock);
198 wait_then_retry();
199 next;
200 }
201
202 print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n";
203
204 print $sock "END\n";
205 $response = <$sock>;
206 chop $response;
207 if ($response eq "OK") {
208 print "Host successfully configured via TCP.\n"
209 }
210 else {
211 print "The server failed the host configuration on the END command.";
212 close($sock);
213 wait_then_retry();
214 next;
215 }
216
217 close($sock);
218
219 print "Configuration finished sucessfully!\n";
220 last;
221 }
222 return;
223 }
224
225
226
227
228 #-----------------------------------------------------------------------
229 # send_udp_packet
230 # Sends a UDP packet to an i-scream filter.
231 # The packet contains XML markup describing some of the machine's state.
232 # Receipt of UDP packets is not guaranteed.
233 #-----------------------------------------------------------------------
234 sub send_udp_packet() {
235
236 my(@statgrab) = `./statgrab.pl`;
237 my(%packet);
238 for (my($i) = 0; $i <= $#statgrab; $i++) {
239 $statgrab[$i] =~ /^([^\s]*) (.*)$/;
240 $packet{$1} = $2;
241 }
242
243 my($date) = time;
244
245 my($disk_info) = "<disk>";
246 my($i) = 0;
247 while (defined $packet{"packet.disk.p$i.attributes.mount"}) {
248 $disk_info .= "<p$i";
249 $disk_info .= " name=\"" . $packet{"packet.disk.p$i.attributes.name"} . "\"";
250 $disk_info .= " kbytes=\"" . $packet{"packet.disk.p$i.attributes.kbytes"} . "\"";
251 $disk_info .= " used=\"" . $packet{"packet.disk.p$i.attributes.used"} . "\"";
252 $disk_info .= " avail=\"" . $packet{"packet.disk.p$i.attributes.avail"} . "\"";
253 $disk_info .= " mount=\"" . $packet{"packet.disk.p$i.attributes.mount"} . "\"";
254 $disk_info .= "></p$i>";
255 ++$i;
256 }
257 $disk_info .= "</disk>";
258
259 my($ip) = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost');
260
261 # Build the XML packet this way, as we can clearly
262 # see the structure and contents... I like this ;-)
263 # [Note that the server rejects UDP packets that are
264 # larger than 8196 bytes]
265 my($xml) = <<EOF;
266
267 <packet seq_no="$seq_no" machine_name="$fqdn" date="$date" type="data" ip="$ip">
268 <load>
269 <load1>$packet{"packet.load.load1"}</load1>
270 <load5>$packet{"packet.load.load5"}</load5>
271 <load15>$packet{"packet.load.load15"}</load15>
272 </load>
273 <os>
274 <name>$packet{"packet.os.name"}</name>
275 <release>$packet{"packet.os.release"}</release>
276 <platform>$packet{"packet.os.platform"}</platform>
277 <sysname>$packet{"packet.os.sysname"}</sysname>
278 <version>$packet{"packet.os.version"}</version>
279 <uptime>$packet{"packet.os.uptime"}</uptime>
280 </os>
281 <users>
282 <count>$packet{"packet.users.count"}</count>
283 <list>$packet{"packet.users.list"}</list>
284 </users>
285 <processes>
286 <total>$packet{"packet.processes.total"}</total>
287 <sleeping>$packet{"packet.processes.sleeping"}</sleeping>
288 <zombie>$packet{"packet.processes.zombie"}</zombie>
289 <stopped>$packet{"packet.processes.stopped"}</stopped>
290 <cpu>$packet{"packet.processes.cpu"}</cpu>
291 </processes>
292 <cpu>
293 <idle>$packet{"packet.cpu.idle"}</idle>
294 <user>$packet{"packet.cpu.user"}</user>
295 <kernel>$packet{"packet.cpu.kernel"}</kernel>
296 <iowait>$packet{"packet.cpu.iowait"}</iowait>
297 <swap>$packet{"packet.cpu.swap"}</swap>
298 </cpu>
299 <memory>
300 <total>$packet{"packet.memory.total"}</total>
301 <free>$packet{"packet.memory.free"}</free>
302 </memory>
303 <swap>
304 <total>$packet{"packet.swap.total"}</total>
305 <free>$packet{"packet.swap.free"}</free>
306 </swap>
307 $disk_info
308 </packet>
309
310 EOF
311
312 # Make the packet smaller by stripping out newlines and leading spaces.
313 $xml =~ s/\n\s*//g;
314
315 my($sock) = new IO::Socket::INET (
316 PeerPort => $udp_port,
317 PeerAddr => $filter_addr,
318 Proto => 'udp'
319 ) or die "Could not send UDP: $!\n";
320
321 print $sock $xml or die "Could not send UDP packet: $!\n";
322 close($sock);
323 $seq_no++;
324 print "-";
325
326 return;
327 }
328
329
330
331
332 #-----------------------------------------------------------------------
333 # send_tcp_heartbeat
334 # Establishes a TCP connection to an i-scream filter.
335 # The heartbeat is used as a guaranteed "I'm alive" delivery mechanism.
336 #-----------------------------------------------------------------------
337 sub send_tcp_heartbeat() {
338
339 my($sock) = new IO::Socket::INET(
340 PeerAddr => $filter_addr,
341 PeerPort => $tcp_port,
342 Proto => 'tcp'
343 ) or return;
344 if (!defined $sock) {
345 print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
346 &tcp_configure();
347 return;
348 }
349
350 # Now run through the configuration process.
351 my($response);
352
353 print $sock "HEARTBEAT\n";
354 $response = <$sock>;
355 return if (!defined $response);
356 chop $response;
357 if (!$response eq "OK") {
358 close($sock);
359 print "Server gave wrong response to HEARTBEAT: $response\n";
360 &tcp_configure();
361 return;
362 }
363
364 print $sock "CONFIG\n";
365 $response = <$sock>;
366 return if (!defined $response);
367 chop $response;
368 if (!$response eq "OK") {
369 close($sock);
370 print "Server gave wrong response to CONFIG: $response\n";
371 &tcp_configure();
372 return;
373 }
374
375 print $sock "$file_list\n";
376 $response = <$sock>;
377 return if (!defined $response);
378 chop $response;
379 if (!$response eq "OK") {
380 close($sock);
381 print "Server gave wrong response to file list: $response\n";
382 &tcp_configure();
383 return;
384 }
385
386 print $sock "$last_modified\n";
387 $response = <$sock>;
388 return if (!defined $response);
389 chop $response;
390 if ($response eq "ERROR") {
391 close($sock);
392 print "Server configuration changed. Reconfiguring with filter manager.\n";
393 &tcp_configure();
394 return;
395 }
396 if (!$response eq "OK") {
397 close($sock);
398 print "Server gave wrong response to HEARTBEAT: $response\n";
399 &tcp_configure();
400 return;
401 }
402
403 print $sock "ENDHEARTBEAT\n";
404 $response = <$sock>;
405 return if (!defined $response);
406 chop $response;
407 if (!$response eq "OK") {
408 close($sock);
409 print "Server gave wrong response to ENDHEARTBEAT: $response\n";
410 &tcp_configure();
411 return;
412 }
413
414 close($sock);
415 print "^";
416
417 return;
418 }
419
420 #-----------------------------------------------------------------------
421 # write_pid
422 # Writes the PID (process ID) of this instance to $pidfile.
423 # This is then used by a seperate script to check (and restart) ihost.
424 #-----------------------------------------------------------------------
425 sub write_pid() {
426 open PID, ">$pidfile";
427 print PID $$;
428 close PID;
429
430 return;
431 }