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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines