--- projects/cms/source/host/ihost-perl/ihost.pl 2001/01/26 17:41:48 1.5 +++ projects/cms/source/host/ihost-perl/ihost.pl 2001/02/05 17:38:38 1.15 @@ -1,7 +1,8 @@ -#!/usr/local/bin/perl -w +#!/usr/bin/perl -w # ----------------------------------------------------------- # 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.5 2001/01/26 17:41:48 pjm2 Exp $ +# $Id: ihost.pl,v 1.15 2001/02/05 17:38:38 pjm2 Exp $ #------------------------------------------------------------ $| = 1; @@ -48,30 +49,51 @@ $seq_no = 1; $last_udp_time = time; $last_tcp_time = time; while (1) { - if (time >= $last_udp_time + $udp_update_time) { + my($time) = time; + if ($time >= $last_udp_time + $udp_update_time) { &send_udp_packet(); - $last_udp_time = time; + $last_udp_time = $time; } - if (time >= $last_tcp_time + $tcp_update_time) { + if ($time >= $last_tcp_time + $tcp_update_time) { &send_tcp_heartbeat(); - $last_tcp_time = time; + $last_tcp_time = $time; } - `sleep 1`; + my($next_udp) = $udp_update_time - $time + $last_udp_time; + my($next_tcp) = $tcp_update_time - $time + $last_tcp_time; + my($delay); + if ($next_udp < $next_tcp) { + $delay = $next_udp + } + else { + $delay = $next_tcp; + } + `sleep $delay`; } 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 "Could not perform configuration via TCP: $!\n"; + ); + 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"; + print "Program ended.\n"; + exit(1); + } - die "Could not connect to the i-scream filter manager: $!\n" unless $sock; - - # Now run through the configuration process. + # Now run through the configuration process... my($response); print $sock "STARTCONFIG\n"; @@ -88,7 +110,7 @@ sub tcp_configure() { chop $response; $last_modified = $response; - print "Config last modified: ". scalar localtime $last_modified . "\n"; + print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n"; print $sock "FILELIST\n"; $response = <$sock>; @@ -107,7 +129,7 @@ sub tcp_configure() { chop $response; $tcp_update_time = $response; - print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time.\n"; + print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n"; print $sock "ENDCONFIG\n"; $response = <$sock>; @@ -141,13 +163,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; } @@ -157,26 +190,28 @@ 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 .= " @@ -191,6 +226,7 @@ sub send_udp_packet() { $packet{"packet.os.platform"} $packet{"packet.os.sysname"} $packet{"packet.os.version"} + $packet{"packet.os.uptime"} $packet{"packet.users.count"} @@ -211,18 +247,19 @@ 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 ( @@ -235,18 +272,30 @@ EOF 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 die "Could not perform heartbeat via TCP: $!\n"; + ); + if (!defined $sock) { + print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n"; + return; + } - die "Could not connect to the i-scream filter: $!\n" unless $sock; - # Now run through the configuration process. my($response); @@ -302,4 +351,6 @@ sub send_tcp_heartbeat() { close($sock); print "^"; + + return; }