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, 9 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

# User Rev Content
1 pjm2 1.6 #!/usr/bin/perl -w
2 pjm2 1.1
3     # -----------------------------------------------------------
4     # Perl i-scream Host.
5 pjm2 1.12 # http://www.i-scream.org.uk
6 pjm2 1.1 #
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 pjm2 1.12 # $Author: pjm2 $
13 pjm2 1.13 # $Id: ihost.pl,v 1.12 2001/02/01 09:24:54 pjm2 Exp $
14 pjm2 1.1 #------------------------------------------------------------
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 pjm2 1.9 my($time) = time;
53     if ($time >= $last_udp_time + $udp_update_time) {
54 pjm2 1.1 &send_udp_packet();
55 pjm2 1.9 $last_udp_time = $time;
56 pjm2 1.1 }
57 pjm2 1.9 if ($time >= $last_tcp_time + $tcp_update_time) {
58 pjm2 1.1 &send_tcp_heartbeat();
59 pjm2 1.9 $last_tcp_time = $time;
60 pjm2 1.1 }
61 pjm2 1.9 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 pjm2 1.1 }
72    
73     exit(0);
74    
75 pjm2 1.12
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 pjm2 1.1 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 pjm2 1.11 );
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 pjm2 1.1
96 pjm2 1.12 # Now run through the configuration process...
97 pjm2 1.1 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 pjm2 1.7 print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
114 pjm2 1.1
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 pjm2 1.7 print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
133 pjm2 1.1
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 pjm2 1.12
167     return;
168 pjm2 1.1 }
169    
170 pjm2 1.12
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 pjm2 1.1 sub send_udp_packet() {
180    
181     my(@statgrab) = `./statgrab.pl`;
182     my(%packet);
183 pjm2 1.13 for (my($i) = 0; $i <= $#statgrab; $i++) {
184 pjm2 1.1 $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 pjm2 1.13 print "$disk_info\n";
205    
206 pjm2 1.1 my($hostname) = hostname();
207 pjm2 1.4 $hostname =~ s/\..*$//g;
208 pjm2 1.8 `cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/;
209     my($domainname) = $1;
210 pjm2 1.1 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 pjm2 1.12 # [Note that the server rejects UDP packets that are
216     # larger than 8196 bytes]
217 pjm2 1.1 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 tdb 1.10 <uptime>$packet{"packet.os.uptime"}</uptime>
232 pjm2 1.1 </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 pjm2 1.13 <total>$packet{"packet.memory.total"}</total>
253 pjm2 1.1 <free>$packet{"packet.memory.free"}</free>
254     </memory>
255     <swap>
256 pjm2 1.13 <total>$packet{"packet.swap.total"}</total>
257     <free>$packet{"packet.swap.free"}</free>
258 pjm2 1.1 </swap>
259     $disk_info
260     </packet>
261    
262     EOF
263    
264 pjm2 1.12 # Make the packet smaller by stripping out newlines and leading spaces.
265 pjm2 1.1 $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 pjm2 1.12
278     return;
279 pjm2 1.1 }
280    
281 pjm2 1.12
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 pjm2 1.1 sub send_tcp_heartbeat() {
290    
291     my($sock) = new IO::Socket::INET(
292     PeerAddr => $filter_addr,
293     PeerPort => $tcp_port,
294     Proto => 'tcp'
295 pjm2 1.11 );
296     if (!defined $sock) {
297     print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
298     return;
299     }
300 pjm2 1.1
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 pjm2 1.12
357     return;
358 pjm2 1.1 }