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.15
Committed: Mon Feb 5 17:38:38 2001 UTC (23 years, 9 months ago) by pjm2
Content type: text/plain
Branch: MAIN
Changes since 1.14: +8 -8 lines
Log Message:
The building of disk information has been reworked to provide its
information as attribute values, as these only occur at most once, thus
reducing overall packet size.

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.15 # $Id: ihost.pl,v 1.14 2001/02/05 17:20:07 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 pjm2 1.15 $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 pjm2 1.1 ++$i;
201     }
202     $disk_info .= "</disk>";
203 pjm2 1.13
204 pjm2 1.1 my($hostname) = hostname();
205 pjm2 1.4 $hostname =~ s/\..*$//g;
206 pjm2 1.8 `cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/;
207     my($domainname) = $1;
208 pjm2 1.1 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 pjm2 1.12 # [Note that the server rejects UDP packets that are
214     # larger than 8196 bytes]
215 pjm2 1.1 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 tdb 1.10 <uptime>$packet{"packet.os.uptime"}</uptime>
230 pjm2 1.1 </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 pjm2 1.13 <total>$packet{"packet.memory.total"}</total>
251 pjm2 1.1 <free>$packet{"packet.memory.free"}</free>
252     </memory>
253     <swap>
254 pjm2 1.13 <total>$packet{"packet.swap.total"}</total>
255     <free>$packet{"packet.swap.free"}</free>
256 pjm2 1.1 </swap>
257     $disk_info
258     </packet>
259    
260     EOF
261    
262 pjm2 1.12 # Make the packet smaller by stripping out newlines and leading spaces.
263 pjm2 1.1 $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 pjm2 1.12
276     return;
277 pjm2 1.1 }
278    
279 pjm2 1.12
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 pjm2 1.1 sub send_tcp_heartbeat() {
288    
289     my($sock) = new IO::Socket::INET(
290     PeerAddr => $filter_addr,
291     PeerPort => $tcp_port,
292     Proto => 'tcp'
293 pjm2 1.11 );
294     if (!defined $sock) {
295     print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
296     return;
297     }
298 pjm2 1.1
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 pjm2 1.12
355     return;
356 pjm2 1.1 }