--- projects/cms/source/host/ihost-perl/ihost.pl 2001/03/16 18:25:59 1.32 +++ projects/cms/source/host/ihost-perl/ihost.pl 2001/11/19 22:31:18 1.40 @@ -5,12 +5,10 @@ # http://www.i-scream.org.uk # # An all-in-one script to act as an i-scream host on -# a typical Unix/Linux box. You may adapt the data-gathering -# methods as you see fit. -# - pjm2@ukc.ac.uk +# a typical Unix/Linux box. # -# $Author: pjm2 $ -# $Id: ihost.pl,v 1.32 2001/03/16 18:25:59 pjm2 Exp $ +# $Author: tdb $ +# $Id: ihost.pl,v 1.40 2001/11/19 22:31:18 tdb Exp $ #------------------------------------------------------------ $| = 1; @@ -35,6 +33,7 @@ use vars qw ( $fqdn $pidfile $retry_wait + @data ); if (@ARGV != 2) { @@ -45,7 +44,7 @@ $filter_manager_addr = $ARGV[0]; $filter_manager_port = $ARGV[1]; $seq_no = 1; -$retry_wait = 6; +$retry_wait = 60; # write our PID to a file $pidfile = "/var/tmp/ihost.pid"; @@ -75,7 +74,7 @@ while (1) { else { $delay = $next_tcp; } - `sleep $delay`; + sleep $delay; } # we'll probably never get here... @@ -90,7 +89,7 @@ exit(0); #----------------------------------------------------------------------- sub wait_then_retry() { print "Will retry configuration with filter manager in $retry_wait seconds.\n"; - `sleep $retry_wait`; + sleep $retry_wait; } @@ -250,8 +249,6 @@ sub tcp_configure() { } - - #----------------------------------------------------------------------- # send_udp_packet # Sends a UDP packet to an i-scream filter. @@ -260,91 +257,38 @@ sub tcp_configure() { #----------------------------------------------------------------------- sub send_udp_packet() { - my(@statgrab) = `./statgrab.pl`; - my(%packet); - for (my($i) = 0; $i <= $#statgrab; $i++) { - $statgrab[$i] =~ /^([^\s]*) (.*)$/; - $packet{$1} = $2; + my($plugins_dir) = "plugins"; + + 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"; } + # get some extra data my($date) = time; + my($ip); + $ip = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost') or $ip = 'localhost'; - my($disk_info) = ""; - my($i) = 0; - while (defined $packet{"packet.disk.p$i.attributes.mount"}) { - $disk_info .= " - - $packet{"packet.load.load1"} - $packet{"packet.load.load5"} - $packet{"packet.load.load15"} - - - $packet{"packet.os.name"} - $packet{"packet.os.release"} - $packet{"packet.os.platform"} - $packet{"packet.os.sysname"} - $packet{"packet.os.version"} - $packet{"packet.os.uptime"} - - - $packet{"packet.users.count"} - $packet{"packet.users.list"} - - - $packet{"packet.processes.total"} - $packet{"packet.processes.sleeping"} - $packet{"packet.processes.zombie"} - $packet{"packet.processes.stopped"} - $packet{"packet.processes.cpu"} - - - $packet{"packet.cpu.idle"} - $packet{"packet.cpu.user"} - $packet{"packet.cpu.kernel"} - $packet{"packet.cpu.iowait"} - $packet{"packet.cpu.swap"} - - - $packet{"packet.memory.total"} - $packet{"packet.memory.free"} - - - $packet{"packet.swap.total"} - $packet{"packet.swap.free"} - - $disk_info - - -EOF - - # Make the packet smaller by stripping out newlines and leading spaces. - $xml =~ s/\n\s*//g; - + # sort the data + @data = sort(@data); + + # turn the array into some nice XML + my($xml) = &make_xml("", ""); + my($sock) = new IO::Socket::INET ( PeerPort => $udp_port, PeerAddr => $filter_addr, Proto => 'udp' ) or die "Could not send UDP: $!\n"; - + print $sock $xml or die "Could not send UDP packet: $!\n"; close($sock); $seq_no++; @@ -354,15 +298,17 @@ EOF } - - #----------------------------------------------------------------------- # send_tcp_heartbeat # Establishes a TCP connection to an i-scream filter. # The heartbeat is used as a guaranteed "I'm alive" delivery mechanism. +# If we need to reconfigure, then we complete the heartbeat before +# doing so. #----------------------------------------------------------------------- sub send_tcp_heartbeat() { + my ($doReconfigure) = 0; + my($sock) = new IO::Socket::INET( PeerAddr => $filter_addr, PeerPort => $tcp_port, @@ -409,8 +355,7 @@ sub send_tcp_heartbeat() { if ($response eq "ERROR\n") { close($sock); print "Server configuration changed. Reconfiguring with filter manager.\n"; - &tcp_configure(); - return; + $doReconfigure = 1; } if (!$response eq "OK\n") { close($sock); @@ -431,9 +376,12 @@ sub send_tcp_heartbeat() { close($sock); print "^"; + &tcp_configure() if $doReconfigure; + return; } + #----------------------------------------------------------------------- # write_pid # Writes the PID (process ID) of this instance to $pidfile. @@ -445,4 +393,37 @@ sub write_pid() { close PID; return; +} + +#----------------------------------------------------------------------- +# make_xml +# Turns an array of plugins data into an XML string. +#----------------------------------------------------------------------- +sub make_xml() { + my($curlevel, $curline) = @_; + my($xmltemp) = ""; my($curtag) = ""; my($attributes) = ""; + while(1) { + $curline = shift(@data) if $curline eq ""; chomp $curline; + if($curline =~ /^$curlevel([^\.\s]+\.)/) { + $curtag=$1; + } + if($curline =~ /^$curlevel$curtag([^\.\s]+)\s+(.*)$/) { + $xmltemp .= "<$1$attributes>$2"; + } + 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; + } + } }