--- projects/cms/source/host/ihost-perl/ihost.pl 2001/11/14 14:17:12 1.35 +++ projects/cms/source/host/ihost-perl/ihost.pl 2001/11/21 10:08:20 1.42 @@ -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: tdb $ -# $Id: ihost.pl,v 1.35 2001/11/14 14:17:12 tdb Exp $ +# $Id: ihost.pl,v 1.42 2001/11/21 10:08:20 tdb Exp $ #------------------------------------------------------------ $| = 1; @@ -35,6 +33,7 @@ use vars qw ( $fqdn $pidfile $retry_wait + @data ); if (@ARGV != 2) { @@ -48,7 +47,11 @@ $seq_no = 1; $retry_wait = 60; # 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(); @@ -250,8 +253,6 @@ sub tcp_configure() { } - - #----------------------------------------------------------------------- # send_udp_packet # Sends a UDP packet to an i-scream filter. @@ -260,92 +261,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($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; - + # 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"); + + # 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++; @@ -355,8 +302,6 @@ EOF } - - #----------------------------------------------------------------------- # send_tcp_heartbeat # Establishes a TCP connection to an i-scream filter. @@ -440,6 +385,7 @@ sub send_tcp_heartbeat() { return; } + #----------------------------------------------------------------------- # write_pid # Writes the PID (process ID) of this instance to $pidfile. @@ -451,4 +397,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; + } + } }