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.18 by pjm2, Tue Feb 27 19:26:51 2001 UTC vs.
Revision 1.33 by pjm2, Tue Mar 20 12:58:22 2001 UTC

# Line 33 | Line 33 | use vars qw (
33               $filter_addr
34               $file_list
35               $fqdn
36 +             $pidfile
37 +             $retry_wait
38              );
39  
40   if (@ARGV != 2) {
# Line 43 | Line 45 | $filter_manager_addr = $ARGV[0];
45   $filter_manager_port = $ARGV[1];
46  
47   $seq_no = 1;
48 + $retry_wait = 60;
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 71 | 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
# Line 82 | Line 102 | exit(0);
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 <                                    );
110 <    if (!defined $sock) {
111 <        print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n";
112 <        print "Please check that there is an i-scream server at this address.\n";
113 <        print "Program ended.\n";
114 <        exit(1);
115 <    }
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 <    # Now run through the configuration process...
119 <    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 ($response && !($response eq "OK\n")) {
124 >            print "The i-scream server rejected the STARTCONFIG command.\n";
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 >        if (!$response) {
135 >            print "The i-scream server did not return anything for the LASTMODIFIED command.\n";
136 >            close($sock);
137 >            wait_then_retry();
138 >            next;
139 >        }
140 >        chop $response;
141 >        $last_modified = $response;
142      
143 <    print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
143 >        print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
144  
145 <    print $sock "FILELIST\n";
146 <    $response = <$sock>;
147 <    chop $response;
148 <    $file_list = $response;
145 >        print $sock "FILELIST\n";
146 >        $response = <$sock>;
147 >        if (!$response) {
148 >            print "The i-scream server did not provide a configuration file list.\n";
149 >            close($sock);
150 >            wait_then_retry();
151 >            next;
152 >        }
153 >        chop $response;
154 >        $file_list = $response;
155  
156 <    print "File list obtained: $file_list\n";
156 >        print "File list obtained: $file_list\n";
157  
158 <    print $sock "FQDN\n";
159 <    $response = <$sock>;
160 <    chop $response;
161 <    $fqdn = $response;
158 >        print $sock "FQDN\n";
159 >        $response = <$sock>;
160 >        if (!$response) {
161 >            print "The i-scream server did not tell us our FQDN.\n";
162 >            close($sock);
163 >            wait_then_retry();
164 >            next;
165 >        }
166 >        chop $response;
167 >        $fqdn = $response;
168  
169 <    print "FQDN returned: $fqdn\n";
169 >        print "FQDN returned: $fqdn\n";
170  
171 <    print $sock "UDPUpdateTime\n";
172 <    $response = <$sock>;
173 <    chop $response;
174 <    $udp_update_time = $response;
171 >        print $sock "UDPUpdateTime\n";
172 >        $response = <$sock>;
173 >        if (!$response) {
174 >            print "The i-scream server did not give us a UDPUpdateTime.\n";
175 >            close($sock);
176 >            wait_then_retry();
177 >            next;
178 >        }
179 >        chop $response;
180 >        $udp_update_time = $response;
181  
182 <    print $sock "TCPUpdateTime\n";
183 <    $response = <$sock>;
184 <    chop $response;
185 <    $tcp_update_time = $response;
182 >        print $sock "TCPUpdateTime\n";
183 >        $response = <$sock>;
184 >        if (!$response) {
185 >            print "The i-scream server did not give us a TCPUpdateTime.\n";
186 >            close($sock);
187 >            wait_then_retry();
188 >            next;
189 >        }
190 >        chop $response;
191 >        $tcp_update_time = $response;
192      
193 <    print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
193 >        print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
194  
195 <    print $sock "ENDCONFIG\n";
196 <    $response = <$sock>;
197 <    chomp $response;
198 <    if (!$response eq "OK") {
199 <        print "ENDCONFIG command to server failed.  Terminated.\n";
200 <        exit(1);
201 <    }
195 >        print $sock "ENDCONFIG\n";
196 >        $response = <$sock>;
197 >        if ($response && !($response eq "OK\n")) {
198 >            print "ENDCONFIG command to server failed.  Terminated.\n";
199 >            close($sock);
200 >            wait_then_retry();
201 >            next;
202 >        }
203  
204 <    print "Config ended.\n";
204 >        print "Config ended.\n";
205      
206 <    print $sock "FILTER\n";
207 <    $response = <$sock>;
208 <    chop $response;
209 <    $response =~ /(.*);(.*);(.*)/;
210 <    ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
206 >        print $sock "FILTER\n";
207 >        $response = <$sock>;
208 >        if (!$response) {
209 >            print "Failed: Could not get a filter address from the filter manager.\n";
210 >            close($sock);
211 >            wait_then_retry();
212 >            next;
213 >        }
214 >        chop $response;
215 >        $response =~ /^(.*);(.*);(.*)/;
216 >        if ($response eq "ERROR") {
217 >            print "There are no active configured filters for your host.\n";
218 >            close($sock);
219 >            wait_then_retry();
220 >            next;
221 >        }
222 >        ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
223 >        unless (defined($filter_addr) && defined($udp_port) && defined($tcp_port)) {
224 >            print "Failed: Filter address response from server did not make sense: $response\n";
225 >            close($sock);
226 >            wait_then_retry();
227 >            next;
228 >        }
229      
230 <    print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n";
230 >        print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n";
231  
232 <    print $sock "END\n";
233 <    $response = <$sock>;
234 <    chop $response;
235 <    if ($response eq "OK") {
236 <        print "Host successfully configured via TCP.\n"
237 <    }
238 <    else {
239 <        print "The server failed the host configuration on the END command.";
240 <        exit(1);
241 <    }
232 >        print $sock "END\n";
233 >        $response = <$sock>;
234 >        if ($response && ($response eq "OK\n")) {
235 >            print "Host successfully configured via TCP.\n"
236 >        }
237 >        else {
238 >            print "The server failed the host configuration on the END command.\n";
239 >            close($sock);
240 >            wait_then_retry();
241 >            next;
242 >        }
243      
244 <    close($sock);
244 >        close($sock);
245  
246 <    print "Configuration finished sucessfully!\n";
247 <    
246 >        print "Configuration finished sucessfully!\n";
247 >        last;
248 >    }
249      return;
250   }
251  
# Line 209 | Line 283 | sub send_udp_packet() {
283      }
284      $disk_info .= "</disk>";
285  
286 <    my($machine_name) = hostname();
287 <    my($ip) = inet_ntoa(scalar(gethostbyname($machine_name)) || 'localhost');
286 >    my($ip);
287 >    $ip = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost') or $ip = 'localhost';
288  
289      # Build the XML packet this way, as we can clearly
290      # see the structure and contents... I like this ;-)
# Line 218 | Line 292 | sub send_udp_packet() {
292      # larger than 8196 bytes]
293      my($xml) = <<EOF;
294      
295 < <packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip">
295 > <packet seq_no="$seq_no" machine_name="$fqdn" date="$date" type="data" ip="$ip">
296      <load>
297          <load1>$packet{"packet.load.load1"}</load1>
298          <load5>$packet{"packet.load.load5"}</load5>
# Line 265 | Line 339 | EOF
339  
340      # Make the packet smaller by stripping out newlines and leading spaces.
341      $xml =~ s/\n\s*//g;
342 <    
342 >
343      my($sock) = new IO::Socket::INET (
344                                        PeerPort => $udp_port,
345                                        PeerAddr => $filter_addr,
346                                        Proto => 'udp'
347 <                                     ) or die "Socket: $!\n";
347 >                                     ) or die "Could not send UDP: $!\n";
348  
349      print $sock $xml or die "Could not send UDP packet: $!\n";
350      close($sock);
# Line 294 | Line 368 | sub send_tcp_heartbeat() {
368                                       PeerAddr => $filter_addr,
369                                       PeerPort => $tcp_port,
370                                       Proto => 'tcp'
371 <                                    );
371 >                                    ) or return;
372      if (!defined $sock) {
373          print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
374 +        &tcp_configure();
375          return;
376      }
377  
# Line 305 | Line 380 | sub send_tcp_heartbeat() {
380  
381      print $sock "HEARTBEAT\n";
382      $response = <$sock>;
383 <    chop $response;
309 <    if (!$response eq "OK") {
383 >    if (!$response eq "OK\n") {
384          close($sock);
385          print "Server gave wrong response to HEARTBEAT: $response\n";
386 +        &tcp_configure();
387          return;
388      }
389      
390      print $sock "CONFIG\n";
391      $response = <$sock>;
392 <    chop $response;
318 <    if (!$response eq "OK") {
392 >    if (!$response eq "OK\n") {
393          close($sock);
394          print "Server gave wrong response to CONFIG: $response\n";
395 +        &tcp_configure();
396          return;
397      }
398  
399      print $sock "$file_list\n";
400      $response = <$sock>;
401 <    chop $response;
327 <    if (!$response eq "OK") {
401 >    if (!$response eq "OK\n") {
402          close($sock);
403          print "Server gave wrong response to file list: $response\n";
404 +        &tcp_configure();
405          return;
406      }
407      
408      print $sock "$last_modified\n";
409      $response = <$sock>;
410 <    chop $response;
336 <    if ($response eq "ERROR") {
410 >    if ($response eq "ERROR\n") {
411          close($sock);
412 +        print "Server configuration changed.  Reconfiguring with filter manager.\n";
413          &tcp_configure();
414          return;
415      }
416 <    if (!$response eq "OK") {
416 >    if (!$response eq "OK\n") {
417          close($sock);
418          print "Server gave wrong response to HEARTBEAT: $response\n";
419 +        &tcp_configure();
420          return;
421      }
422      
423      print $sock "ENDHEARTBEAT\n";
424      $response = <$sock>;
425 <    chop $response;
350 <    if (!$response eq "OK") {
425 >    if (!$response eq "OK\n") {
426          close($sock);
427          print "Server gave wrong response to ENDHEARTBEAT: $response\n";
428 +        &tcp_configure();
429          return;
430      }
431      
432      close($sock);
433      print "^";
434 +    
435 +    return;
436 + }
437 +
438 + #-----------------------------------------------------------------------
439 + # write_pid
440 + # Writes the PID (process ID) of this instance to $pidfile.
441 + # This is then used by a seperate script to check (and restart) ihost.
442 + #-----------------------------------------------------------------------
443 + sub write_pid() {
444 +    open PID, ">$pidfile";
445 +    print PID $$;
446 +    close PID;
447      
448      return;
449   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines