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 |
< |
); |
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 "FQDN\n"; |
147 |
< |
$response = <$sock>; |
148 |
< |
chop $response; |
149 |
< |
$fqdn = $response; |
146 |
> |
print $sock "FQDN\n"; |
147 |
> |
$response = <$sock>; |
148 |
> |
chop $response; |
149 |
> |
$fqdn = $response; |
150 |
|
|
151 |
< |
print "FQDN returned: $fqdn\n"; |
151 |
> |
print "FQDN returned: $fqdn\n"; |
152 |
|
|
153 |
< |
print $sock "UDPUpdateTime\n"; |
154 |
< |
$response = <$sock>; |
155 |
< |
chop $response; |
156 |
< |
$udp_update_time = $response; |
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; |
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 |
|
|
255 |
|
} |
256 |
|
$disk_info .= "</disk>"; |
257 |
|
|
258 |
< |
my($hostname) = hostname(); |
213 |
< |
$hostname =~ s/\..*$//g; |
214 |
< |
my($resolv) = `cat /etc/resolv.conf`; |
215 |
< |
my($domainname); |
216 |
< |
my($machine_name); |
217 |
< |
if($resolv =~ /domain\s+([^\s]+)/) { |
218 |
< |
# some machines have domain <domain> in resolv.conf |
219 |
< |
$domainname = $1; |
220 |
< |
$machine_name = "$hostname.$domainname"; |
221 |
< |
} |
222 |
< |
elsif($resolv =~ /search\s+([^\s]+)/) { |
223 |
< |
# some machines have search <domain> in resolv.conf |
224 |
< |
$domainname = $1; |
225 |
< |
$machine_name = "$hostname.$domainname"; |
226 |
< |
} |
227 |
< |
else { |
228 |
< |
# we can't find out the domain |
229 |
< |
$machine_name = $hostname; |
230 |
< |
} |
231 |
< |
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 ;-) |
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> |
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); |
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 |
|
|
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 |
|
} |