--- 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/14 16:41:30 1.36 @@ -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.36 2001/11/14 16:41:30 tdb Exp $ #------------------------------------------------------------ $| = 1; @@ -35,6 +33,7 @@ use vars qw ( $fqdn $pidfile $retry_wait + @statgrab ); if (@ARGV != 2) { @@ -250,8 +249,6 @@ sub tcp_configure() { } - - #----------------------------------------------------------------------- # send_udp_packet # Sends a UDP packet to an i-scream filter. @@ -260,92 +257,29 @@ 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; - } + @statgrab = `./statgrab.pl`; + # 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(@statgrab, "packet.attributes.seq_no=$seq_no"); + push(@statgrab, "packet.attributes.machine_name=$fqdn"); + push(@statgrab, "packet.attributes.date=$date"); + push(@statgrab, "packet.attributes.type=data"); + push(@statgrab, "packet.attributes.ip=$ip"); + + # 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 +289,6 @@ EOF } - - #----------------------------------------------------------------------- # send_tcp_heartbeat # Establishes a TCP connection to an i-scream filter. @@ -440,6 +372,7 @@ sub send_tcp_heartbeat() { return; } + #----------------------------------------------------------------------- # write_pid # Writes the PID (process ID) of this instance to $pidfile. @@ -451,4 +384,37 @@ sub write_pid() { close PID; return; +} + +#----------------------------------------------------------------------- +# make_xml +# Turns an array of statgrab data into an XML string. +#----------------------------------------------------------------------- +sub make_xml() { + my($curlevel, $curline) = @_; + my($xmltemp) = ""; my($curtag) = ""; my($attributes) = ""; + while(true) { + $curline = shift(@statgrab) 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) = $statgrab[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; + } + } }