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.11
Committed: Thu Feb 1 09:10:52 2001 UTC (23 years, 9 months ago) by pjm2
Content type: text/plain
Branch: MAIN
Changes since 1.10: +14 -8 lines
Log Message:
If the server 'disappears', then the ihost host will carry on running
until it is once again able to deliver TCP heartbeats.

File Contents

# User Rev Content
1 pjm2 1.6 #!/usr/bin/perl -w
2 pjm2 1.1
3     # -----------------------------------------------------------
4     # Perl i-scream Host.
5     #
6     # An all-in-one script to act as an i-scream host on
7     # a typical Unix/Linux box. You may adapt the data-gathering
8     # methods as you see fit.
9     # - pjm2@ukc.ac.uk
10     #
11 pjm2 1.11 # $Author: tdb1 $
12     # $Id: ihost.pl,v 1.10 2001/02/01 03:17:32 tdb1 Exp $
13 pjm2 1.1 #------------------------------------------------------------
14    
15     $| = 1;
16    
17     use strict;
18     use IO::Socket;
19     use Sys::Hostname;
20    
21     use vars qw (
22     $filter_manager_addr
23     $filter_manager_port
24     $seq_no
25     $udp_update_time
26     $tcp_update_time
27     $last_udp_time
28     $last_tcp_time
29     $last_modified
30     $udp_port
31     $tcp_port
32     $filter_addr
33     $file_list
34     );
35    
36     if (@ARGV != 2) {
37     die "Usage: ihost.pl [i-scream filter manager] [TCP port]\n";
38     }
39    
40     $filter_manager_addr = $ARGV[0];
41     $filter_manager_port = $ARGV[1];
42    
43     $seq_no = 1;
44    
45     &tcp_configure();
46     &send_udp_packet();
47    
48     $last_udp_time = time;
49     $last_tcp_time = time;
50     while (1) {
51 pjm2 1.9 my($time) = time;
52     if ($time >= $last_udp_time + $udp_update_time) {
53 pjm2 1.1 &send_udp_packet();
54 pjm2 1.9 $last_udp_time = $time;
55 pjm2 1.1 }
56 pjm2 1.9 if ($time >= $last_tcp_time + $tcp_update_time) {
57 pjm2 1.1 &send_tcp_heartbeat();
58 pjm2 1.9 $last_tcp_time = $time;
59 pjm2 1.1 }
60 pjm2 1.9 my($next_udp) = $udp_update_time - $time + $last_udp_time;
61     my($next_tcp) = $tcp_update_time - $time + $last_tcp_time;
62     my($delay);
63     if ($next_udp < $next_tcp) {
64     $delay = $next_udp
65     }
66     else {
67     $delay = $next_tcp;
68     }
69     `sleep $delay`;
70 pjm2 1.1 }
71    
72     exit(0);
73    
74     sub tcp_configure() {
75    
76     my($sock) = new IO::Socket::INET(
77     PeerAddr => $filter_manager_addr,
78     PeerPort => $filter_manager_port,
79     Proto => 'tcp'
80 pjm2 1.11 );
81     if (!defined $sock) {
82     print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n";
83     print "Please check that there is an i-scream server at this address.\n";
84     print "Program ended.\n";
85     exit(1);
86     }
87 pjm2 1.1
88     # Now run through the configuration process.
89     my($response);
90    
91     print $sock "STARTCONFIG\n";
92     $response = <$sock>;
93     if (!chop $response eq "OK") {
94     print "The i-scream server rejected the STARTCONFIG command. Terminated.";
95     exit(1);
96     }
97    
98     print "Config started okay.\n";
99    
100     print $sock "LASTMODIFIED\n";
101     $response = <$sock>;
102     chop $response;
103     $last_modified = $response;
104    
105 pjm2 1.7 print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
106 pjm2 1.1
107     print $sock "FILELIST\n";
108     $response = <$sock>;
109     chop $response;
110     $file_list = $response;
111    
112     print "File list obtained: $file_list\n";
113    
114     print $sock "UDPUpdateTime\n";
115     $response = <$sock>;
116     chop $response;
117     $udp_update_time = $response;
118    
119     print $sock "TCPUpdateTime\n";
120     $response = <$sock>;
121     chop $response;
122     $tcp_update_time = $response;
123    
124 pjm2 1.7 print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
125 pjm2 1.1
126     print $sock "ENDCONFIG\n";
127     $response = <$sock>;
128     chomp $response;
129     if (!$response eq "OK") {
130     print "ENDCONFIG command to server failed. Terminated.\n";
131     exit(1);
132     }
133    
134     print "Config ended.\n";
135    
136     print $sock "FILTER\n";
137     $response = <$sock>;
138     chop $response;
139     $response =~ /(.*);(.*);(.*)/;
140     ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
141    
142     print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n";
143    
144     print $sock "END\n";
145     $response = <$sock>;
146     chop $response;
147     if ($response eq "OK") {
148     print "Host successfully configured via TCP.\n"
149     }
150     else {
151     print "The server failed the host configuration on the END command.";
152     exit(1);
153     }
154    
155     close($sock);
156    
157     print "Configuration finished sucessfully!\n";
158     }
159    
160     sub send_udp_packet() {
161    
162     my(@statgrab) = `./statgrab.pl`;
163     my(%packet);
164     for (my($i) = 0; $i < $#statgrab; $i++) {
165     $statgrab[$i] =~ /^([^\s]*) (.*)$/;
166     $packet{$1} = $2;
167     }
168    
169     my($date) = time;
170    
171     my($disk_info) = "<disk>";
172     my($i) = 0;
173     while (defined $packet{"packet.disk.p$i.attributes.mount"}) {
174     $disk_info .= "<p$i>";
175     $disk_info .= qq/<name>$packet{"packet.disk.p$i.attributes.name"}<\/name>/;
176     $disk_info .= qq/<kbytes>$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/;
177     $disk_info .= qq/<used>$packet{"packet.disk.p$i.attributes.used"}<\/used>/;
178     $disk_info .= qq/<avail>$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/;
179     $disk_info .= qq/<mount>$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/;
180     $disk_info .= "</p$i>";
181     ++$i;
182     }
183     $disk_info .= "</disk>";
184    
185     my($hostname) = hostname();
186 pjm2 1.4 $hostname =~ s/\..*$//g;
187 pjm2 1.8 `cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/;
188     my($domainname) = $1;
189 pjm2 1.1 my($machine_name) = "$hostname.$domainname";
190     my($ip) = inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost');
191    
192     # Build the XML packet this way, as we can clearly
193     # see the structure and contents... I like this ;-)
194     my($xml) = <<EOF;
195    
196     <packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip">
197     <load>
198     <load1>$packet{"packet.load.load1"}</load1>
199     <load5>$packet{"packet.load.load5"}</load5>
200     <load15>$packet{"packet.load.load15"}</load15>
201     </load>
202     <os>
203     <name>$packet{"packet.os.name"}</name>
204     <release>$packet{"packet.os.release"}</release>
205     <platform>$packet{"packet.os.platform"}</platform>
206     <sysname>$packet{"packet.os.sysname"}</sysname>
207     <version>$packet{"packet.os.version"}</version>
208 tdb 1.10 <uptime>$packet{"packet.os.uptime"}</uptime>
209 pjm2 1.1 </os>
210     <users>
211     <count>$packet{"packet.users.count"}</count>
212     <list>$packet{"packet.users.list"}</list>
213     </users>
214     <processes>
215     <total>$packet{"packet.processes.total"}</total>
216     <sleeping>$packet{"packet.processes.sleeping"}</sleeping>
217     <zombie>$packet{"packet.processes.zombie"}</zombie>
218     <stopped>$packet{"packet.processes.stopped"}</stopped>
219     <cpu>$packet{"packet.processes.cpu"}</cpu>
220     </processes>
221     <cpu>
222     <idle>$packet{"packet.cpu.idle"}</idle>
223     <user>$packet{"packet.cpu.user"}</user>
224     <kernel>$packet{"packet.cpu.kernel"}</kernel>
225     <iowait>$packet{"packet.cpu.iowait"}</iowait>
226     <swap>$packet{"packet.cpu.swap"}</swap>
227     </cpu>
228     <memory>
229     <total>$packet{"packet.memory.real"}</total>
230     <free>$packet{"packet.memory.free"}</free>
231     </memory>
232     <swap>
233     <total>$packet{"packet.memory.swap_total"}</total>
234     <free>$packet{"packet.memory.swap_free"}</free>
235     </swap>
236     $disk_info
237     </packet>
238    
239     EOF
240    
241     $xml =~ s/\n\s*//g;
242    
243     my($sock) = new IO::Socket::INET (
244     PeerPort => $udp_port,
245     PeerAddr => $filter_addr,
246     Proto => 'udp'
247     ) or die "Socket: $!\n";
248    
249     print $sock $xml or die "Could not send UDP packet: $!\n";
250     close($sock);
251     $seq_no++;
252     print "-";
253     }
254    
255     sub send_tcp_heartbeat() {
256    
257     my($sock) = new IO::Socket::INET(
258     PeerAddr => $filter_addr,
259     PeerPort => $tcp_port,
260     Proto => 'tcp'
261 pjm2 1.11 );
262     if (!defined $sock) {
263     print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
264     return;
265     }
266 pjm2 1.1
267     # Now run through the configuration process.
268     my($response);
269    
270     print $sock "HEARTBEAT\n";
271     $response = <$sock>;
272     chop $response;
273     if (!$response eq "OK") {
274     close($sock);
275     print "Server gave wrong response to HEARTBEAT: $response\n";
276     return;
277     }
278    
279     print $sock "CONFIG\n";
280     $response = <$sock>;
281     chop $response;
282     if (!$response eq "OK") {
283     close($sock);
284     print "Server gave wrong response to CONFIG: $response\n";
285     return;
286     }
287    
288     print $sock "$file_list\n";
289     $response = <$sock>;
290     chop $response;
291     if (!$response eq "OK") {
292     close($sock);
293     print "Server gave wrong response to file list: $response\n";
294     return;
295     }
296    
297     print $sock "$last_modified\n";
298     $response = <$sock>;
299     chop $response;
300     if ($response eq "ERROR") {
301     close($sock);
302     &tcp_configure();
303     return;
304     }
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 "ENDHEARTBEAT\n";
312     $response = <$sock>;
313     chop $response;
314     if (!$response eq "OK") {
315     close($sock);
316     print "Server gave wrong response to ENDHEARTBEAT: $response\n";
317     return;
318     }
319    
320     close($sock);
321     print "^";
322     }