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.16 by tdb, Tue Feb 13 12:28:50 2001 UTC vs.
Revision 1.28 by pjm2, Wed Mar 14 18:48:33 2001 UTC

# Line 32 | 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 42 | Line 45 | $filter_manager_addr = $ARGV[0];
45   $filter_manager_port = $ARGV[1];
46  
47   $seq_no = 1;
48 + $retry_wait = 6;
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 70 | 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 81 | 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 == 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 <    # Now run through the configuration process...
120 <    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";
220 <    
219 >        print "Configuration finished sucessfully!\n";
220 >        last;
221 >    }
222      return;
223   }
224  
# Line 201 | Line 256 | sub send_udp_packet() {
256      }
257      $disk_info .= "</disk>";
258  
259 <    my($hostname) = hostname();
205 <    $hostname =~ s/\..*$//g;
206 <    my($resolv) = `cat /etc/resolv.conf`;
207 <    my($domainname);
208 <    my($machine_name);
209 <    if($resolv =~ /domain\s+([^\s]+)/) {
210 <        # some machines have domain <domain> in resolv.conf
211 <        $domainname = $1;
212 <        $machine_name = "$hostname.$domainname";
213 <    }
214 <    elsif($resolv =~ /search\s+([^\s]+)/) {
215 <        # some machines have search <domain> in resolv.conf
216 <        $domainname = $1;
217 <        $machine_name = "$hostname.$domainname";
218 <    }
219 <    else {
220 <        # we can't find out the domain
221 <        $machine_name = $hostname;
222 <    }
223 <    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 ;-)
# Line 228 | Line 264 | sub send_udp_packet() {
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 275 | Line 311 | 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);
# Line 304 | Line 340 | sub send_tcp_heartbeat() {
340                                       PeerAddr => $filter_addr,
341                                       PeerPort => $tcp_port,
342                                       Proto => 'tcp'
343 <                                    );
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  
# Line 315 | Line 352 | sub send_tcp_heartbeat() {
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