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.13
Committed: Mon Feb 5 17:14:41 2001 UTC (23 years, 8 months ago) by pjm2
Content type: text/plain
Branch: MAIN
Changes since 1.12: +7 -5 lines
Log Message:
Now using the correct names for packet values when obtaining them from the
packet hash.

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