| 33 |
|
$fqdn |
| 34 |
|
$pidfile |
| 35 |
|
$retry_wait |
| 36 |
< |
@statgrab |
| 36 |
> |
@data |
| 37 |
|
); |
| 38 |
|
|
| 39 |
|
if (@ARGV != 2) { |
| 47 |
|
$retry_wait = 60; |
| 48 |
|
|
| 49 |
|
# write our PID to a file |
| 50 |
< |
$pidfile = "/var/tmp/ihost.pid"; |
| 50 |
> |
# use home dir by default |
| 51 |
> |
#$pidfile = $ENV{"HOME"}; |
| 52 |
> |
# or drop it in /var/tmp if we can't find HOME |
| 53 |
> |
$pidfile = "/var/tmp" if not defined $pidfile; |
| 54 |
> |
$pidfile .= "/.ihost.pid"; |
| 55 |
|
&write_pid(); |
| 56 |
|
|
| 57 |
|
&tcp_configure(); |
| 261 |
|
#----------------------------------------------------------------------- |
| 262 |
|
sub send_udp_packet() { |
| 263 |
|
|
| 264 |
< |
@statgrab = `./statgrab.pl`; |
| 264 |
> |
my($plugins_dir) = "plugins"; |
| 265 |
|
|
| 266 |
+ |
opendir PLUGINS, $plugins_dir; |
| 267 |
+ |
my(@plugins) = readdir PLUGINS; |
| 268 |
+ |
foreach my $plugin (@plugins) { |
| 269 |
+ |
push @data, `$plugins_dir/$plugin` if -x "$plugins_dir/$plugin" && -f "$plugins_dir/$plugin"; |
| 270 |
+ |
} |
| 271 |
+ |
|
| 272 |
|
# get some extra data |
| 273 |
|
my($date) = time; |
| 274 |
|
my($ip); |
| 275 |
|
$ip = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost') or $ip = 'localhost'; |
| 276 |
|
|
| 277 |
|
# add some extra data to the array |
| 278 |
< |
push(@statgrab, "packet.attributes.seq_no=$seq_no"); |
| 279 |
< |
push(@statgrab, "packet.attributes.machine_name=$fqdn"); |
| 280 |
< |
push(@statgrab, "packet.attributes.date=$date"); |
| 281 |
< |
push(@statgrab, "packet.attributes.type=data"); |
| 282 |
< |
push(@statgrab, "packet.attributes.ip=$ip"); |
| 278 |
> |
push(@data, "packet.attributes.seq_no $seq_no"); |
| 279 |
> |
push(@data, "packet.attributes.machine_name $fqdn"); |
| 280 |
> |
push(@data, "packet.attributes.date $date"); |
| 281 |
> |
push(@data, "packet.attributes.type data"); |
| 282 |
> |
push(@data, "packet.attributes.ip $ip"); |
| 283 |
|
|
| 284 |
+ |
# sort the data |
| 285 |
+ |
@data = sort(grep(!/^$/, grep(/^packet\./, @data))); |
| 286 |
+ |
|
| 287 |
|
# turn the array into some nice XML |
| 288 |
|
my($xml) = &make_xml("", ""); |
| 289 |
< |
|
| 289 |
> |
|
| 290 |
|
my($sock) = new IO::Socket::INET ( |
| 291 |
|
PeerPort => $udp_port, |
| 292 |
|
PeerAddr => $filter_addr, |
| 401 |
|
|
| 402 |
|
#----------------------------------------------------------------------- |
| 403 |
|
# make_xml |
| 404 |
< |
# Turns an array of statgrab data into an XML string. |
| 404 |
> |
# Turns an array of plugins data into an XML string. |
| 405 |
|
#----------------------------------------------------------------------- |
| 406 |
|
sub make_xml() { |
| 407 |
|
my($curlevel, $curline) = @_; |
| 408 |
|
my($xmltemp) = ""; my($curtag) = ""; my($attributes) = ""; |
| 409 |
< |
while(true) { |
| 410 |
< |
$curline = shift(@statgrab) if $curline eq ""; chomp $curline; |
| 409 |
> |
while(1) { |
| 410 |
> |
$curline = shift(@data) if $curline eq ""; |
| 411 |
> |
return $xmltemp if not defined $curline; |
| 412 |
> |
chomp $curline; |
| 413 |
> |
# dealing with nest (or attributes) |
| 414 |
|
if($curline =~ /^$curlevel([^\.\s]+\.)/) { |
| 415 |
|
$curtag=$1; |
| 416 |
+ |
if($curline =~ /^$curlevel$curtag([^\.\s]+)\s+(.*)$/) { |
| 417 |
+ |
$xmltemp .= &make_xml("$curlevel$curtag", $curline); |
| 418 |
+ |
} |
| 419 |
+ |
elsif($curline =~ /^$curlevel$curtag(attributes)\.([^\.\s]+)\s+(.*)$/) { |
| 420 |
+ |
$attributes .= " $2=\"$3\""; |
| 421 |
+ |
} |
| 422 |
+ |
else { |
| 423 |
+ |
$xmltemp .= &make_xml("$curlevel$curtag", $curline); |
| 424 |
+ |
} |
| 425 |
+ |
my($nextline) = $data[0]; chomp $nextline if defined $nextline; |
| 426 |
+ |
if((defined $nextline) && ($nextline =~ /^$curlevel$curtag/)) { |
| 427 |
+ |
$curline = ""; |
| 428 |
+ |
} |
| 429 |
+ |
else { |
| 430 |
+ |
$curtag =~ s/(.*)\./$1/; |
| 431 |
+ |
$xmltemp = "<$curtag$attributes>$xmltemp</$curtag>" unless $curtag eq ""; |
| 432 |
+ |
return $xmltemp; |
| 433 |
+ |
} |
| 434 |
|
} |
| 435 |
< |
if($curline =~ /^$curlevel$curtag([^\.\s]+)\s+(.*)$/) { |
| 436 |
< |
$xmltemp .= "<$1$attributes>$2</$1>"; |
| 437 |
< |
} |
| 438 |
< |
elsif($curline =~ /^$curlevel$curtag(attributes)\.([^\.=]+)=(.*)$/) { |
| 439 |
< |
$attributes .= " $2=\"$3\""; |
| 440 |
< |
} |
| 441 |
< |
else { |
| 442 |
< |
$xmltemp .= &make_xml("$curlevel$curtag", $curline); |
| 443 |
< |
} |
| 444 |
< |
my($nextline) = $statgrab[0]; chomp $nextline if defined $nextline; |
| 445 |
< |
$curtag =~ s/(.*)\./$1/; |
| 446 |
< |
if(defined $nextline && $nextline =~ /^$curlevel$curtag\./) { |
| 413 |
< |
$curline = ""; |
| 414 |
< |
} |
| 415 |
< |
else { |
| 416 |
< |
$xmltemp = "<$curtag$attributes>$xmltemp</$curtag>" unless $curtag eq ""; |
| 417 |
< |
return $xmltemp; |
| 435 |
> |
# dealing with value |
| 436 |
> |
elsif($curline =~ /^$curlevel([^\.\s]+)\s+(.*)$/) { |
| 437 |
> |
$curtag=$1; |
| 438 |
> |
$xmltemp=$2; |
| 439 |
> |
my($nextline) = $data[0]; chomp $nextline if defined $nextline; |
| 440 |
> |
if(defined $nextline && ($nextline =~ /^$curlevel$curtag\./ || $nextline =~ /^$curlevel$curtag\s+/)) { |
| 441 |
> |
$curline = ""; |
| 442 |
> |
} |
| 443 |
> |
else { |
| 444 |
> |
$xmltemp = "<$curtag$attributes>$xmltemp</$curtag>" unless $curtag eq ""; |
| 445 |
> |
return $xmltemp; |
| 446 |
> |
} |
| 447 |
|
} |
| 448 |
|
} |
| 449 |
|
} |