--- projects/cms/source/host/ihost-perl/ihost.pl 2001/11/19 14:15:14 1.37 +++ projects/cms/source/host/ihost-perl/ihost.pl 2002/03/11 13:34:24 1.46 @@ -8,7 +8,7 @@ # a typical Unix/Linux box. # # $Author: tdb $ -# $Id: ihost.pl,v 1.37 2001/11/19 14:15:14 tdb Exp $ +# $Id: ihost.pl,v 1.46 2002/03/11 13:34:24 tdb Exp $ #------------------------------------------------------------ $| = 1; @@ -33,6 +33,7 @@ use vars qw ( $fqdn $pidfile $retry_wait + $ostype @data ); @@ -46,8 +47,17 @@ $filter_manager_port = $ARGV[1]; $seq_no = 1; $retry_wait = 60; +# work out our platform, if we can. +$ostype = `uname -s`; +chomp $ostype; +$ostype = "unknown" if not defined $ostype; + # write our PID to a file -$pidfile = "/var/tmp/ihost.pid"; +# use home dir by default +#$pidfile = $ENV{"HOME"}; +# or drop it in /var/tmp if we can't find HOME +$pidfile = "/var/tmp" if not defined $pidfile; +$pidfile .= "/.ihost.pid"; &write_pid(); &tcp_configure(); @@ -262,7 +272,7 @@ sub send_udp_packet() { opendir PLUGINS, $plugins_dir; my(@plugins) = readdir PLUGINS; foreach my $plugin (@plugins) { - push @data, `$plugins_dir/$plugin` if -x "$plugins_dir/$plugin" && -f "$plugins_dir/$plugin"; + push @data, `$plugins_dir/$plugin $ostype` if -x "$plugins_dir/$plugin" && -f "$plugins_dir/$plugin"; } # get some extra data @@ -271,15 +281,18 @@ sub send_udp_packet() { $ip = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost') or $ip = 'localhost'; # add some extra data to the array - push(@data, "packet.attributes.seq_no=$seq_no"); - push(@data, "packet.attributes.machine_name=$fqdn"); - push(@data, "packet.attributes.date=$date"); - push(@data, "packet.attributes.type=data"); - push(@data, "packet.attributes.ip=$ip"); + push(@data, "packet.attributes.seq_no $seq_no"); + push(@data, "packet.attributes.machine_name $fqdn"); + push(@data, "packet.attributes.date $date"); + push(@data, "packet.attributes.type data"); + push(@data, "packet.attributes.ip $ip"); + # sort the data + @data = sort(grep(!/^$/, grep(/^packet\./, @data))); + # turn the array into some nice XML my($xml) = &make_xml("", ""); - + my($sock) = new IO::Socket::INET ( PeerPort => $udp_port, PeerAddr => $filter_addr, @@ -289,7 +302,7 @@ sub send_udp_packet() { print $sock $xml or die "Could not send UDP packet: $!\n"; close($sock); $seq_no++; - print "-: $xml\n"; + print "-"; return; } @@ -399,28 +412,54 @@ sub write_pid() { sub make_xml() { my($curlevel, $curline) = @_; my($xmltemp) = ""; my($curtag) = ""; my($attributes) = ""; - while(true) { - $curline = shift(@data) if $curline eq ""; chomp $curline; + while(1) { + $curline = shift(@data) if $curline eq ""; + return $xmltemp if not defined $curline; + chomp $curline; + # dealing with nest (or attributes) if($curline =~ /^$curlevel([^\.\s]+\.)/) { $curtag=$1; + if($curline =~ /^$curlevel$curtag([^\.\s]+)\s+(.*)$/) { + $xmltemp .= &make_xml("$curlevel$curtag", $curline); + } + elsif($curline =~ /^$curlevel$curtag(attributes)\.([^\.\s]+)\s+(.*)$/) { + $attributes .= " $2=\"$3\""; + } + else { + $xmltemp .= &make_xml("$curlevel$curtag", $curline); + } + my($nextline) = $data[0]; chomp $nextline if defined $nextline; + $curtag =~ s/(.*)\./$1/; + if((defined $nextline) && ($nextline =~ /^$curlevel$curtag\./)) { + $curline = ""; + } + else { + $xmltemp = "<$curtag$attributes>$xmltemp" unless $curtag eq ""; + return $xmltemp; + } } - if($curline =~ /^$curlevel$curtag([^\.\s]+)\s+(.*)$/) { - $xmltemp .= "<$1$attributes>$2"; + # dealing with value + elsif($curline =~ /^$curlevel([^\.\s]+)\s+(.*)$/) { + $curtag=$1; + $xmltemp=$2; + my($nextline) = $data[0]; chomp $nextline if defined $nextline; + if(defined $nextline && ($nextline =~ /^$curlevel$curtag\./ || $nextline =~ /^$curlevel$curtag\s+/)) { + $curline = ""; + } + else { + $xmltemp = "<$curtag$attributes>$xmltemp" unless $curtag eq ""; + return $xmltemp; + } } - elsif($curline =~ /^$curlevel$curtag(attributes)\.([^\.=]+)=(.*)$/) { - $attributes .= " $2=\"$3\""; - } - else { - $xmltemp .= &make_xml("$curlevel$curtag", $curline); - } - my($nextline) = $data[0]; chomp $nextline if defined $nextline; - $curtag =~ s/(.*)\./$1/; - if(defined $nextline && $nextline =~ /^$curlevel$curtag\./) { - $curline = ""; - } - else { - $xmltemp = "<$curtag$attributes>$xmltemp" unless $curtag eq ""; - return $xmltemp; - } + # dealing with a null value + elsif($curline =~ /^$curlevel([^\.\s]+)$/) { + # simply adding a space makes the above elsif deal with it :) + # just level with an empty tag in the XML + $curline .= " "; + } + # failing all that, skip the line + else { + $curline = ""; + } } }