33 |
|
$filter_addr |
34 |
|
$file_list |
35 |
|
$fqdn |
36 |
+ |
$pidfile |
37 |
+ |
$retry_wait |
38 |
|
); |
39 |
|
|
40 |
|
if (@ARGV != 2) { |
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 |
|
|
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 |
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 "Cannot connect!"; |
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 "FQDN\n"; |
148 |
< |
$response = <$sock>; |
149 |
< |
chop $response; |
150 |
< |
$fqdn = $response; |
147 |
> |
print $sock "FQDN\n"; |
148 |
> |
$response = <$sock>; |
149 |
> |
chop $response; |
150 |
> |
$fqdn = $response; |
151 |
|
|
152 |
< |
print "FQDN returned: $fqdn\n"; |
152 |
> |
print "FQDN returned: $fqdn\n"; |
153 |
|
|
154 |
< |
print $sock "UDPUpdateTime\n"; |
155 |
< |
$response = <$sock>; |
156 |
< |
chop $response; |
157 |
< |
$udp_update_time = $response; |
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; |
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 |
|
|
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 |
|
|
352 |
|
|
353 |
|
print $sock "HEARTBEAT\n"; |
354 |
|
$response = <$sock>; |
355 |
< |
chop $response or return; |
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 |
< |
chop $response or return; |
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 |
< |
chop $response or return; |
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 |
< |
chop $response or return; |
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> or return; |
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 |
|
} |