--- 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/12/18 04:07:17 1.44 @@ -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.44 2001/12/18 04:07:17 tdb Exp $ #------------------------------------------------------------ $| = 1; @@ -35,6 +33,8 @@ use vars qw ( $fqdn $pidfile $retry_wait + $ostype + @data ); if (@ARGV != 2) { @@ -45,10 +45,19 @@ $filter_manager_addr = $ARGV[0]; $filter_manager_port = $ARGV[1]; $seq_no = 1; -$retry_wait = 6; +$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(); @@ -75,7 +84,7 @@ while (1) { else { $delay = $next_tcp; } - `sleep $delay`; + sleep $delay; } # we'll probably never get here... @@ -90,7 +99,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 +259,6 @@ sub tcp_configure() { } - - #----------------------------------------------------------------------- # send_udp_packet # Sends a UDP packet to an i-scream filter. @@ -260,91 +267,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 $ostype` 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 - + # sort the data + @data = sort(grep(!/^$/, grep(/^packet\./, @data))); -EOF + # turn the array into some nice XML + my($xml) = &make_xml("", ""); - # Make the packet smaller by stripping out newlines and leading spaces. - $xml =~ s/\n\s*//g; - 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 +308,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 +365,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 +386,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 +403,53 @@ 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 ""; + 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; + if((defined $nextline) && ($nextline =~ /^$curlevel$curtag/)) { + $curline = ""; + } + else { + $curtag =~ s/(.*)\./$1/; + $xmltemp = "<$curtag$attributes>$xmltemp" unless $curtag eq ""; + return $xmltemp; + } + } + # 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; + } + } + } }