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.16
Committed: Tue Feb 13 12:28:50 2001 UTC (23 years, 9 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.15: +18 -4 lines
Log Message:
It appears /etc/resolv.conf is different on some hosts, so we have to do some
careful checks.

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