| 32 |
|
$tcp_port |
| 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 = 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 |
|
|
| 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 |
> |
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 |
|
|
| 235 |
|
|
| 236 |
|
my(@statgrab) = `./statgrab.pl`; |
| 237 |
|
my(%packet); |
| 238 |
< |
for (my($i) = 0; $i < $#statgrab; $i++) { |
| 238 |
> |
for (my($i) = 0; $i <= $#statgrab; $i++) { |
| 239 |
|
$statgrab[$i] =~ /^([^\s]*) (.*)$/; |
| 240 |
|
$packet{$1} = $2; |
| 241 |
|
} |
| 245 |
|
my($disk_info) = "<disk>"; |
| 246 |
|
my($i) = 0; |
| 247 |
|
while (defined $packet{"packet.disk.p$i.attributes.mount"}) { |
| 248 |
< |
$disk_info .= "<p$i>"; |
| 249 |
< |
$disk_info .= qq/<name>$packet{"packet.disk.p$i.attributes.name"}<\/name>/; |
| 250 |
< |
$disk_info .= qq/<kbytes>$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/; |
| 251 |
< |
$disk_info .= qq/<used>$packet{"packet.disk.p$i.attributes.used"}<\/used>/; |
| 252 |
< |
$disk_info .= qq/<avail>$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/; |
| 253 |
< |
$disk_info .= qq/<mount>$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/; |
| 254 |
< |
$disk_info .= "</p$i>"; |
| 248 |
> |
$disk_info .= "<p$i"; |
| 249 |
> |
$disk_info .= " name=\"" . $packet{"packet.disk.p$i.attributes.name"} . "\""; |
| 250 |
> |
$disk_info .= " kbytes=\"" . $packet{"packet.disk.p$i.attributes.kbytes"} . "\""; |
| 251 |
> |
$disk_info .= " used=\"" . $packet{"packet.disk.p$i.attributes.used"} . "\""; |
| 252 |
> |
$disk_info .= " avail=\"" . $packet{"packet.disk.p$i.attributes.avail"} . "\""; |
| 253 |
> |
$disk_info .= " mount=\"" . $packet{"packet.disk.p$i.attributes.mount"} . "\""; |
| 254 |
> |
$disk_info .= "></p$i>"; |
| 255 |
|
++$i; |
| 256 |
|
} |
| 257 |
|
$disk_info .= "</disk>"; |
| 258 |
|
|
| 259 |
< |
my($hostname) = hostname(); |
| 205 |
< |
$hostname =~ s/\..*$//g; |
| 206 |
< |
`cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/; |
| 207 |
< |
my($domainname) = $1; |
| 208 |
< |
my($machine_name) = "$hostname.$domainname"; |
| 209 |
< |
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 ;-) |
| 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> |
| 297 |
|
<swap>$packet{"packet.cpu.swap"}</swap> |
| 298 |
|
</cpu> |
| 299 |
|
<memory> |
| 300 |
< |
<total>$packet{"packet.memory.real"}</total> |
| 300 |
> |
<total>$packet{"packet.memory.total"}</total> |
| 301 |
|
<free>$packet{"packet.memory.free"}</free> |
| 302 |
|
</memory> |
| 303 |
|
<swap> |
| 304 |
< |
<total>$packet{"packet.memory.swap_total"}</total> |
| 305 |
< |
<free>$packet{"packet.memory.swap_free"}</free> |
| 304 |
> |
<total>$packet{"packet.swap.total"}</total> |
| 305 |
> |
<free>$packet{"packet.swap.free"}</free> |
| 306 |
|
</swap> |
| 307 |
|
$disk_info |
| 308 |
|
</packet> |
| 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); |
| 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 |
|
|
| 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 |
|
} |