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.12
Committed: Thu Feb 1 09:24:54 2001 UTC (23 years, 9 months ago) by pjm2
Content type: text/plain
Branch: MAIN
Changes since 1.11: +37 -3 lines
Log Message:
Tidied up the code a little, added explicit returns in the subroutines, a
couple more comments where it was deemed necessary.

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.11 2001/02/01 09:10:52 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 .= qq/<name>$packet{"packet.disk.p$i.attributes.name"}<\/name>/;
195 $disk_info .= qq/<kbytes>$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/;
196 $disk_info .= qq/<used>$packet{"packet.disk.p$i.attributes.used"}<\/used>/;
197 $disk_info .= qq/<avail>$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/;
198 $disk_info .= qq/<mount>$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/;
199 $disk_info .= "</p$i>";
200 ++$i;
201 }
202 $disk_info .= "</disk>";
203
204 my($hostname) = hostname();
205 $hostname =~ s/\..*$//g;
206 `cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/;
207 my($domainname) = $1;
208 my($machine_name) = "$hostname.$domainname";
209 my($ip) = inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost');
210
211 # Build the XML packet this way, as we can clearly
212 # see the structure and contents... I like this ;-)
213 # [Note that the server rejects UDP packets that are
214 # larger than 8196 bytes]
215 my($xml) = <<EOF;
216
217 <packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip">
218 <load>
219 <load1>$packet{"packet.load.load1"}</load1>
220 <load5>$packet{"packet.load.load5"}</load5>
221 <load15>$packet{"packet.load.load15"}</load15>
222 </load>
223 <os>
224 <name>$packet{"packet.os.name"}</name>
225 <release>$packet{"packet.os.release"}</release>
226 <platform>$packet{"packet.os.platform"}</platform>
227 <sysname>$packet{"packet.os.sysname"}</sysname>
228 <version>$packet{"packet.os.version"}</version>
229 <uptime>$packet{"packet.os.uptime"}</uptime>
230 </os>
231 <users>
232 <count>$packet{"packet.users.count"}</count>
233 <list>$packet{"packet.users.list"}</list>
234 </users>
235 <processes>
236 <total>$packet{"packet.processes.total"}</total>
237 <sleeping>$packet{"packet.processes.sleeping"}</sleeping>
238 <zombie>$packet{"packet.processes.zombie"}</zombie>
239 <stopped>$packet{"packet.processes.stopped"}</stopped>
240 <cpu>$packet{"packet.processes.cpu"}</cpu>
241 </processes>
242 <cpu>
243 <idle>$packet{"packet.cpu.idle"}</idle>
244 <user>$packet{"packet.cpu.user"}</user>
245 <kernel>$packet{"packet.cpu.kernel"}</kernel>
246 <iowait>$packet{"packet.cpu.iowait"}</iowait>
247 <swap>$packet{"packet.cpu.swap"}</swap>
248 </cpu>
249 <memory>
250 <total>$packet{"packet.memory.real"}</total>
251 <free>$packet{"packet.memory.free"}</free>
252 </memory>
253 <swap>
254 <total>$packet{"packet.memory.swap_total"}</total>
255 <free>$packet{"packet.memory.swap_free"}</free>
256 </swap>
257 $disk_info
258 </packet>
259
260 EOF
261
262 # Make the packet smaller by stripping out newlines and leading spaces.
263 $xml =~ s/\n\s*//g;
264
265 my($sock) = new IO::Socket::INET (
266 PeerPort => $udp_port,
267 PeerAddr => $filter_addr,
268 Proto => 'udp'
269 ) or die "Socket: $!\n";
270
271 print $sock $xml or die "Could not send UDP packet: $!\n";
272 close($sock);
273 $seq_no++;
274 print "-";
275
276 return;
277 }
278
279
280
281
282 #-----------------------------------------------------------------------
283 # send_tcp_heartbeat
284 # Establishes a TCP connection to an i-scream filter.
285 # The heartbeat is used as a guaranteed "I'm alive" delivery mechanism.
286 #-----------------------------------------------------------------------
287 sub send_tcp_heartbeat() {
288
289 my($sock) = new IO::Socket::INET(
290 PeerAddr => $filter_addr,
291 PeerPort => $tcp_port,
292 Proto => 'tcp'
293 );
294 if (!defined $sock) {
295 print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
296 return;
297 }
298
299 # Now run through the configuration process.
300 my($response);
301
302 print $sock "HEARTBEAT\n";
303 $response = <$sock>;
304 chop $response;
305 if (!$response eq "OK") {
306 close($sock);
307 print "Server gave wrong response to HEARTBEAT: $response\n";
308 return;
309 }
310
311 print $sock "CONFIG\n";
312 $response = <$sock>;
313 chop $response;
314 if (!$response eq "OK") {
315 close($sock);
316 print "Server gave wrong response to CONFIG: $response\n";
317 return;
318 }
319
320 print $sock "$file_list\n";
321 $response = <$sock>;
322 chop $response;
323 if (!$response eq "OK") {
324 close($sock);
325 print "Server gave wrong response to file list: $response\n";
326 return;
327 }
328
329 print $sock "$last_modified\n";
330 $response = <$sock>;
331 chop $response;
332 if ($response eq "ERROR") {
333 close($sock);
334 &tcp_configure();
335 return;
336 }
337 if (!$response eq "OK") {
338 close($sock);
339 print "Server gave wrong response to HEARTBEAT: $response\n";
340 return;
341 }
342
343 print $sock "ENDHEARTBEAT\n";
344 $response = <$sock>;
345 chop $response;
346 if (!$response eq "OK") {
347 close($sock);
348 print "Server gave wrong response to ENDHEARTBEAT: $response\n";
349 return;
350 }
351
352 close($sock);
353 print "^";
354
355 return;
356 }