--- projects/cms/source/host/ihost-perl/ihost.pl 2001/02/01 09:10:52 1.11 +++ projects/cms/source/host/ihost-perl/ihost.pl 2001/03/01 18:53:42 1.23 @@ -2,6 +2,7 @@ # ----------------------------------------------------------- # Perl i-scream Host. +# 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 @@ -9,7 +10,7 @@ # - pjm2@ukc.ac.uk # # $Author: pjm2 $ -# $Id: ihost.pl,v 1.11 2001/02/01 09:10:52 pjm2 Exp $ +# $Id: ihost.pl,v 1.23 2001/03/01 18:53:42 pjm2 Exp $ #------------------------------------------------------------ $| = 1; @@ -31,6 +32,7 @@ use vars qw ( $tcp_port $filter_addr $file_list + $fqdn ); if (@ARGV != 2) { @@ -71,13 +73,20 @@ while (1) { exit(0); + +#----------------------------------------------------------------------- +# tcp_configure +# Establishes a TCP connection to the specified i-scream filter manager. +# The host then requests details from the server, such as the intervals +# at which to send UDP packets. +#----------------------------------------------------------------------- sub tcp_configure() { my($sock) = new IO::Socket::INET( PeerAddr => $filter_manager_addr, PeerPort => $filter_manager_port, Proto => 'tcp' - ); + ) or die "Cannot connect!"; if (!defined $sock) { print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n"; print "Please check that there is an i-scream server at this address.\n"; @@ -85,7 +94,7 @@ sub tcp_configure() { exit(1); } - # Now run through the configuration process. + # Now run through the configuration process... my($response); print $sock "STARTCONFIG\n"; @@ -111,6 +120,13 @@ sub tcp_configure() { print "File list obtained: $file_list\n"; + print $sock "FQDN\n"; + $response = <$sock>; + chop $response; + $fqdn = $response; + + print "FQDN returned: $fqdn\n"; + print $sock "UDPUpdateTime\n"; $response = <$sock>; chop $response; @@ -155,13 +171,24 @@ sub tcp_configure() { close($sock); print "Configuration finished sucessfully!\n"; + + return; } + + + +#----------------------------------------------------------------------- +# send_udp_packet +# Sends a UDP packet to an i-scream filter. +# The packet contains XML markup describing some of the machine's state. +# Receipt of UDP packets is not guaranteed. +#----------------------------------------------------------------------- sub send_udp_packet() { my(@statgrab) = `./statgrab.pl`; my(%packet); - for (my($i) = 0; $i < $#statgrab; $i++) { + for (my($i) = 0; $i <= $#statgrab; $i++) { $statgrab[$i] =~ /^([^\s]*) (.*)$/; $packet{$1} = $2; } @@ -171,29 +198,26 @@ sub send_udp_packet() { my($disk_info) = ""; my($i) = 0; while (defined $packet{"packet.disk.p$i.attributes.mount"}) { - $disk_info .= ""; - $disk_info .= qq/$packet{"packet.disk.p$i.attributes.name"}<\/name>/; - $disk_info .= qq/$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/; - $disk_info .= qq/$packet{"packet.disk.p$i.attributes.used"}<\/used>/; - $disk_info .= qq/$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/; - $disk_info .= qq/$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/; - $disk_info .= ""; + $disk_info .= " + $packet{"packet.load.load1"} $packet{"packet.load.load5"} @@ -226,39 +250,50 @@ sub send_udp_packet() { $packet{"packet.cpu.swap"} - $packet{"packet.memory.real"} + $packet{"packet.memory.total"} $packet{"packet.memory.free"} - $packet{"packet.memory.swap_total"} - $packet{"packet.memory.swap_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; - + my($sock) = new IO::Socket::INET ( PeerPort => $udp_port, PeerAddr => $filter_addr, Proto => 'udp' - ) or die "Socket: $!\n"; + ) or die "Could not send UDP: $!\n"; print $sock $xml or die "Could not send UDP packet: $!\n"; close($sock); $seq_no++; print "-"; + + return; } + + + +#----------------------------------------------------------------------- +# send_tcp_heartbeat +# Establishes a TCP connection to an i-scream filter. +# The heartbeat is used as a guaranteed "I'm alive" delivery mechanism. +#----------------------------------------------------------------------- sub send_tcp_heartbeat() { my($sock) = new IO::Socket::INET( PeerAddr => $filter_addr, PeerPort => $tcp_port, Proto => 'tcp' - ); + ) or return; if (!defined $sock) { print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n"; return; @@ -269,6 +304,7 @@ sub send_tcp_heartbeat() { print $sock "HEARTBEAT\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -278,6 +314,7 @@ sub send_tcp_heartbeat() { print $sock "CONFIG\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -287,6 +324,7 @@ sub send_tcp_heartbeat() { print $sock "$file_list\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -296,6 +334,7 @@ sub send_tcp_heartbeat() { print $sock "$last_modified\n"; $response = <$sock>; + return if (!defined $response); chop $response; if ($response eq "ERROR") { close($sock); @@ -310,6 +349,7 @@ sub send_tcp_heartbeat() { print $sock "ENDHEARTBEAT\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -319,4 +359,6 @@ sub send_tcp_heartbeat() { close($sock); print "^"; + + return; }