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.30 by pjm2, Thu Mar 15 09:44:08 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 = 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 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 >            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 (!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";
219 <    
218 >        print "Configuration finished sucessfully!\n";
219 >        last;
220 >    }
221      return;
222   }
223  
# Line 201 | Line 255 | sub send_udp_packet() {
255      }
256      $disk_info .= "</disk>";
257  
258 <    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');
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 ;-)
# Line 228 | Line 263 | sub send_udp_packet() {
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 275 | Line 310 | 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);
# Line 304 | Line 339 | sub send_tcp_heartbeat() {
339                                       PeerAddr => $filter_addr,
340                                       PeerPort => $tcp_port,
341                                       Proto => 'tcp'
342 <                                    );
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  
# Line 315 | Line 351 | sub send_tcp_heartbeat() {
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