ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/ihost.pl
(Generate patch)

Comparing projects/cms/source/host/ihost-perl/ihost.pl (file contents):
Revision 1.8 by pjm2, Mon Jan 29 09:19:16 2001 UTC vs.
Revision 1.29 by pjm2, Wed Mar 14 18:49:00 2001 UTC

# Line 2 | Line 2
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
# Line 31 | Line 32 | use vars qw (
32               $tcp_port
33               $filter_addr
34               $file_list
35 +             $fqdn
36 +             $pidfile
37 +             $retry_wait
38              );
39  
40   if (@ARGV != 2) {
# Line 41 | Line 45 | $filter_manager_addr = $ARGV[0];
45   $filter_manager_port = $ARGV[1];
46  
47   $seq_no = 1;
48 + $retry_wait = 600;
49  
50 + # write our PID to a file
51 + $pidfile = "/var/tmp/ihost.pid";
52 + &write_pid();
53 +
54   &tcp_configure();
55   &send_udp_packet();
56  
57   $last_udp_time = time;
58   $last_tcp_time = time;
59   while (1) {
60 <    if (time >= $last_udp_time + $udp_update_time) {
60 >    my($time) = time;
61 >    if ($time >= $last_udp_time + $udp_update_time) {
62          &send_udp_packet();
63 <        $last_udp_time = time;
63 >        $last_udp_time = $time;
64      }
65 <    if (time >= $last_tcp_time + $tcp_update_time) {
65 >    if ($time >= $last_tcp_time + $tcp_update_time) {
66          &send_tcp_heartbeat();
67 <        $last_tcp_time = time;
67 >        $last_tcp_time = $time;
68      }
69 <    `sleep 1`;
69 >    my($next_udp) = $udp_update_time - $time + $last_udp_time;
70 >    my($next_tcp) = $tcp_update_time - $time + $last_tcp_time;
71 >    my($delay);
72 >    if ($next_udp < $next_tcp) {
73 >        $delay = $next_udp
74 >    }
75 >    else {
76 >        $delay = $next_tcp;
77 >    }
78 >    `sleep $delay`;
79   }
80  
81 + # we'll probably never get here...
82 + `rm -f $pidfile`;
83   exit(0);
84  
85 +
86 + #-----------------------------------------------------------------------
87 + # wait_then_retry
88 + # Waits for the period of time specified in $retry_wait, then attempts
89 + # to reconfigure with the server.
90 + #-----------------------------------------------------------------------
91 + sub wait_then_retry() {
92 +    print "Will retry configuration with filter manager in $retry_wait seconds.\n";
93 +    `sleep $retry_wait`;
94 + }
95 +
96 +
97 + #-----------------------------------------------------------------------
98 + # tcp_configure
99 + # Establishes a TCP connection to the specified i-scream filter manager.
100 + # The host then requests details from the server, such as the intervals
101 + # at which to send UDP packets.
102 + #-----------------------------------------------------------------------
103   sub tcp_configure() {
104      
105 <    my($sock) = new IO::Socket::INET(
106 <                                     PeerAddr => $filter_manager_addr,
107 <                                     PeerPort => $filter_manager_port,
108 <                                     Proto => 'tcp'
109 <                                    ) or die "Could not perform configuration via TCP: $!\n";
105 >    while (1 == 1) {
106 >        my($sock) = new IO::Socket::INET(
107 >                                         PeerAddr => $filter_manager_addr,
108 >                                         PeerPort => $filter_manager_port,
109 >                                         Proto => 'tcp'
110 >                                        ) or die "Cannot connect!";
111 >        if (!defined $sock) {
112 >            print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n";
113 >            print "Please check that there is an i-scream server at this address.\n";
114 >            close($sock);
115 >            wait_then_retry();
116 >            next;
117 >        }
118  
119 <    die "Could not connect to the i-scream filter manager: $!\n" unless $sock;
120 <
74 <    # Now run through the configuration process.
75 <    my($response);
119 >        # Now run through the configuration process...
120 >        my($response);
121      
122 <    print $sock "STARTCONFIG\n";
123 <    $response = <$sock>;
124 <    if (!chop $response eq "OK") {
125 <        print "The i-scream server rejected the STARTCONFIG command.  Terminated.";
126 <        exit(1);
127 <    }
122 >        print $sock "STARTCONFIG\n";
123 >        $response = <$sock>;
124 >        if (!chop $response eq "OK") {
125 >            print "The i-scream server rejected the STARTCONFIG command.  Terminated.";
126 >            close($sock);
127 >            wait_then_retry();
128 >            next;
129 >        }
130  
131 <    print "Config started okay.\n";
131 >        print "Config started okay.\n";
132      
133 <    print $sock "LASTMODIFIED\n";
134 <    $response = <$sock>;
135 <    chop $response;
136 <    $last_modified = $response;
133 >        print $sock "LASTMODIFIED\n";
134 >        $response = <$sock>;
135 >        chop $response;
136 >        $last_modified = $response;
137      
138 <    print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
138 >        print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
139  
140 <    print $sock "FILELIST\n";
141 <    $response = <$sock>;
142 <    chop $response;
143 <    $file_list = $response;
140 >        print $sock "FILELIST\n";
141 >        $response = <$sock>;
142 >        chop $response;
143 >        $file_list = $response;
144  
145 <    print "File list obtained: $file_list\n";
145 >        print "File list obtained: $file_list\n";
146  
147 <    print $sock "UDPUpdateTime\n";
148 <    $response = <$sock>;
149 <    chop $response;
150 <    $udp_update_time = $response;
147 >        print $sock "FQDN\n";
148 >        $response = <$sock>;
149 >        chop $response;
150 >        $fqdn = $response;
151  
152 <    print $sock "TCPUpdateTime\n";
153 <    $response = <$sock>;
154 <    chop $response;
155 <    $tcp_update_time = $response;
152 >        print "FQDN returned: $fqdn\n";
153 >
154 >        print $sock "UDPUpdateTime\n";
155 >        $response = <$sock>;
156 >        chop $response;
157 >        $udp_update_time = $response;
158 >
159 >        print $sock "TCPUpdateTime\n";
160 >        $response = <$sock>;
161 >        chop $response;
162 >        $tcp_update_time = $response;
163      
164 <    print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
164 >        print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
165  
166 <    print $sock "ENDCONFIG\n";
167 <    $response = <$sock>;
168 <    chomp $response;
169 <    if (!$response eq "OK") {
170 <        print "ENDCONFIG command to server failed.  Terminated.\n";
171 <        exit(1);
172 <    }
166 >        print $sock "ENDCONFIG\n";
167 >        $response = <$sock>;
168 >        chomp $response;
169 >        if (!$response eq "OK") {
170 >            print "ENDCONFIG command to server failed.  Terminated.\n";
171 >            close($sock);
172 >            wait_then_retry();
173 >            next;
174 >        }
175  
176 <    print "Config ended.\n";
176 >        print "Config ended.\n";
177      
178 <    print $sock "FILTER\n";
179 <    $response = <$sock>;
180 <    chop $response;
181 <    $response =~ /(.*);(.*);(.*)/;
182 <    ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
178 >        print $sock "FILTER\n";
179 >        $response = <$sock>;
180 >        unless (defined($response)) {
181 >            print "Failed: Could not get a filter address from the filter manager.\n";
182 >            close($sock);
183 >            wait_then_retry();
184 >            next;
185 >        }
186 >        chop $response;
187 >        $response =~ /(.*);(.*);(.*)/;
188 >        if ($response eq "ERROR") {
189 >            print "There are no active configured filters for your host.\n";
190 >            close($sock);
191 >            wait_then_retry();
192 >            next;
193 >        }
194 >        ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
195 >        unless (defined($filter_addr) && defined($udp_port) && defined($tcp_port)) {
196 >            print "Failed: Filter address response from server did not make sense: $response\n";
197 >            close($sock);
198 >            wait_then_retry();
199 >            next;
200 >        }
201      
202 <    print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n";
202 >        print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n";
203  
204 <    print $sock "END\n";
205 <    $response = <$sock>;
206 <    chop $response;
207 <    if ($response eq "OK") {
208 <        print "Host successfully configured via TCP.\n"
209 <    }
210 <    else {
211 <        print "The server failed the host configuration on the END command.";
212 <        exit(1);
213 <    }
204 >        print $sock "END\n";
205 >        $response = <$sock>;
206 >        chop $response;
207 >        if ($response eq "OK") {
208 >            print "Host successfully configured via TCP.\n"
209 >        }
210 >        else {
211 >            print "The server failed the host configuration on the END command.";
212 >            close($sock);
213 >            wait_then_retry();
214 >            next;
215 >        }
216      
217 <    close($sock);
217 >        close($sock);
218  
219 <    print "Configuration finished sucessfully!\n";
219 >        print "Configuration finished sucessfully!\n";
220 >        last;
221 >    }
222 >    return;
223   }
224  
225 +
226 +
227 +
228 + #-----------------------------------------------------------------------
229 + # send_udp_packet
230 + # Sends a UDP packet to an i-scream filter.
231 + # The packet contains XML markup describing some of the machine's state.
232 + # Receipt of UDP packets is not guaranteed.
233 + #-----------------------------------------------------------------------
234   sub send_udp_packet() {
235  
236      my(@statgrab) = `./statgrab.pl`;
237      my(%packet);
238 <    for (my($i) = 0; $i < $#statgrab; $i++) {
238 >    for (my($i) = 0; $i <= $#statgrab; $i++) {
239          $statgrab[$i] =~ /^([^\s]*) (.*)$/;
240          $packet{$1} = $2;
241      }
# Line 157 | Line 245 | sub send_udp_packet() {
245      my($disk_info) = "<disk>";
246      my($i) = 0;
247      while (defined $packet{"packet.disk.p$i.attributes.mount"}) {
248 <        $disk_info .= "<p$i>";
249 <        $disk_info .= qq/<name>$packet{"packet.disk.p$i.attributes.name"}<\/name>/;
250 <        $disk_info .= qq/<kbytes>$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/;
251 <        $disk_info .= qq/<used>$packet{"packet.disk.p$i.attributes.used"}<\/used>/;
252 <        $disk_info .= qq/<avail>$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/;
253 <        $disk_info .= qq/<mount>$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/;
254 <        $disk_info .= "</p$i>";
248 >        $disk_info .= "<p$i";
249 >        $disk_info .= " name=\"" . $packet{"packet.disk.p$i.attributes.name"} . "\"";
250 >        $disk_info .= " kbytes=\"" . $packet{"packet.disk.p$i.attributes.kbytes"} . "\"";
251 >        $disk_info .= " used=\"" . $packet{"packet.disk.p$i.attributes.used"} . "\"";
252 >        $disk_info .= " avail=\"" . $packet{"packet.disk.p$i.attributes.avail"} . "\"";
253 >        $disk_info .= " mount=\"" . $packet{"packet.disk.p$i.attributes.mount"} . "\"";
254 >        $disk_info .= "></p$i>";
255          ++$i;
256      }
257      $disk_info .= "</disk>";
258  
259 <    my($hostname) = hostname();
172 <    $hostname =~ s/\..*$//g;
173 <    `cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/;
174 <    my($domainname) = $1;
175 <    my($machine_name) = "$hostname.$domainname";
176 <    my($ip) = inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost');
259 >    my($ip) = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost');
260  
261      # Build the XML packet this way, as we can clearly
262      # see the structure and contents... I like this ;-)
263 +    # [Note that the server rejects UDP packets that are
264 +    # larger than 8196 bytes]
265      my($xml) = <<EOF;
266      
267 < <packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip">
267 > <packet seq_no="$seq_no" machine_name="$fqdn" date="$date" type="data" ip="$ip">
268      <load>
269          <load1>$packet{"packet.load.load1"}</load1>
270          <load5>$packet{"packet.load.load5"}</load5>
# Line 191 | Line 276 | sub send_udp_packet() {
276          <platform>$packet{"packet.os.platform"}</platform>
277          <sysname>$packet{"packet.os.sysname"}</sysname>
278          <version>$packet{"packet.os.version"}</version>
279 +        <uptime>$packet{"packet.os.uptime"}</uptime>
280      </os>
281      <users>
282          <count>$packet{"packet.users.count"}</count>
# Line 211 | Line 297 | sub send_udp_packet() {
297          <swap>$packet{"packet.cpu.swap"}</swap>
298      </cpu>
299      <memory>
300 <        <total>$packet{"packet.memory.real"}</total>
300 >        <total>$packet{"packet.memory.total"}</total>
301          <free>$packet{"packet.memory.free"}</free>
302      </memory>
303      <swap>
304 <        <total>$packet{"packet.memory.swap_total"}</total>
305 <        <free>$packet{"packet.memory.swap_free"}</free>
304 >        <total>$packet{"packet.swap.total"}</total>
305 >        <free>$packet{"packet.swap.free"}</free>
306      </swap>
307      $disk_info
308   </packet>
309  
310   EOF
311  
312 +    # Make the packet smaller by stripping out newlines and leading spaces.
313      $xml =~ s/\n\s*//g;
314 <    
314 >
315      my($sock) = new IO::Socket::INET (
316                                        PeerPort => $udp_port,
317                                        PeerAddr => $filter_addr,
318                                        Proto => 'udp'
319 <                                     ) or die "Socket: $!\n";
319 >                                     ) or die "Could not send UDP: $!\n";
320  
321      print $sock $xml or die "Could not send UDP packet: $!\n";
322      close($sock);
323      $seq_no++;
324      print "-";
325 +    
326 +    return;
327   }
328  
329 +
330 +
331 +
332 + #-----------------------------------------------------------------------
333 + # send_tcp_heartbeat
334 + # Establishes a TCP connection to an i-scream filter.
335 + # The heartbeat is used as a guaranteed "I'm alive" delivery mechanism.
336 + #-----------------------------------------------------------------------
337   sub send_tcp_heartbeat() {
338  
339      my($sock) = new IO::Socket::INET(
340                                       PeerAddr => $filter_addr,
341                                       PeerPort => $tcp_port,
342                                       Proto => 'tcp'
343 <                                    ) or die "Could not perform heartbeat via TCP: $!\n";
343 >                                    ) or return;
344 >    if (!defined $sock) {
345 >        print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
346 >        &tcp_configure();
347 >        return;
348 >    }
349  
248    die "Could not connect to the i-scream filter: $!\n" unless $sock;
249
350      # Now run through the configuration process.
351      my($response);
352  
353      print $sock "HEARTBEAT\n";
354      $response = <$sock>;
355 +    return if (!defined $response);
356      chop $response;
357      if (!$response eq "OK") {
358          close($sock);
359          print "Server gave wrong response to HEARTBEAT: $response\n";
360 +        &tcp_configure();
361          return;
362      }
363      
364      print $sock "CONFIG\n";
365      $response = <$sock>;
366 +    return if (!defined $response);
367      chop $response;
368      if (!$response eq "OK") {
369          close($sock);
370          print "Server gave wrong response to CONFIG: $response\n";
371 +        &tcp_configure();
372          return;
373      }
374  
375      print $sock "$file_list\n";
376      $response = <$sock>;
377 +    return if (!defined $response);
378      chop $response;
379      if (!$response eq "OK") {
380          close($sock);
381          print "Server gave wrong response to file list: $response\n";
382 +        &tcp_configure();
383          return;
384      }
385      
386      print $sock "$last_modified\n";
387      $response = <$sock>;
388 +    return if (!defined $response);
389      chop $response;
390      if ($response eq "ERROR") {
391          close($sock);
392 +        print "Server configuration changed.  Reconfiguring with filter manager.\n";
393          &tcp_configure();
394          return;
395      }
396      if (!$response eq "OK") {
397          close($sock);
398          print "Server gave wrong response to HEARTBEAT: $response\n";
399 +        &tcp_configure();
400          return;
401      }
402      
403      print $sock "ENDHEARTBEAT\n";
404      $response = <$sock>;
405 +    return if (!defined $response);
406      chop $response;
407      if (!$response eq "OK") {
408          close($sock);
409          print "Server gave wrong response to ENDHEARTBEAT: $response\n";
410 +        &tcp_configure();
411          return;
412      }
413      
414      close($sock);
415      print "^";
416 +    
417 +    return;
418 + }
419 +
420 + #-----------------------------------------------------------------------
421 + # write_pid
422 + # Writes the PID (process ID) of this instance to $pidfile.
423 + # This is then used by a seperate script to check (and restart) ihost.
424 + #-----------------------------------------------------------------------
425 + sub write_pid() {
426 +    open PID, ">$pidfile";
427 +    print PID $$;
428 +    close PID;
429 +    
430 +    return;
431   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines