libwanetmon.pm 7.73 KB
Newer Older
1
#!/usr/bin/perl -w
Dan Gebhardt's avatar
Dan Gebhardt committed
2
#
3
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Dan Gebhardt's avatar
Dan Gebhardt committed
23 24
#

25 26 27 28 29 30 31 32

package libwanetmon;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);
use IO::Socket::INET;
use IO::Select;
33 34
#use lib '/usr/testbed/lib';
#use event;
35 36 37
require Exporter;

@ISA    = "Exporter";
38
our @EXPORT = qw ( 
39 40 41 42 43
	       %deadnodes
	       %ERRID
	       deserialize_hash
	       serialize_hash
	       sendcmd
44
	       sendcmd_evsys
45 46 47
	       time_all
	       setcmdport
	       setexpid
48
	       setevexpid
49
	       stopnode
50
	       stopnode_evsys
51
	       edittest
52
	       edittest_evsys
53
	       killnode
54
	       getstatus
55
	       );
56 57
our @EXPORT_OK = qw(
		    );
58 59


60 61 62 63 64 65 66 67 68 69 70 71
# These errors define specifics of when a measurement value cannot be
# reported due to some error in the network or at the remote end.
our %ERRID;
$ERRID{timeout} = -1;
$ERRID{ttlexceed} = -2; # was an error for "ping", but is not seen in fping.
$ERRID{unknown} = -3;   # general error, which cannot be classified into others
$ERRID{unknownhost} = -4;
$ERRID{ICMPunreachable} = -5;  #used for all ICMP errors (see fping.c for strs)
$ERRID{iperfHostUnreachable} = -6;  #for iperf: error flagged by: write1 failed: Broken pipe

our %deadnodes;

72 73 74 75
my $socket;
my $sel = IO::Select->new();
my $port;
my $expid;
76
my $evexpid = "__none";
77 78 79 80

sub setcmdport($)
{
    $port = $_[0];
81
#    print "libwanetmon: port=$port\n";
82 83 84 85 86
}

sub setexpid($)
{
    $expid = $_[0];
87
#    print "libwanetmon: expid=$expid\n";
88 89
}

90 91 92 93 94 95
sub setevexpid($)
{
    $evexpid = $_[0];
#    print "libwanetmon: evexpid=$evexpid\n";
}

96 97 98 99 100 101 102 103 104 105 106 107 108

#
# Custom sub to turn a hash into a string. Hashes must not contain
# the substring of $separator anywhere!!!
#
sub serialize_hash($)
{
    my ($hashref) = @_;
    my %hash = %$hashref;
    my $separator = "::";
    my $out = "";

    for my $key (keys %hash){
109 110 111
        $out .= $separator if( $out ne "" );
        $out .= $key.$separator;
        $out .= $hash{$key} if( defined $hash{$key} );
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }
    return $out;
}



sub deserialize_hash($)
{
    my ($string) = @_;
    my $separator = "::";
    my %hashout;

    my @tokens = split( /$separator/, $string );

    for( my $i=0; $i<@tokens; $i+=2 ){
	$hashout{$tokens[$i]} = $tokens[$i+1];
#	print "setting $tokens[$i] => $tokens[$i+1]\n";
    }
    return \%hashout;
}



sub time_all()
{
    package main;
    require 'sys/syscall.ph';
    my $tv = pack("LL",());
    syscall( &SYS_gettimeofday, $tv, undef ) >=0
	or warn "gettimeofday: $!";
    my ($sec, $usec) = unpack ("LL",$tv);
    return $sec + ($usec / 1_000_000);
#    return time();
}


sub sendcmd($$)
{
    my $node = $_[0];
    my $hashref = $_[1];
    my %cmd = %$hashref;
153 154 155
    if( !defined $cmd{managerID} ){
	$cmd{managerID} = "default";
    }
156
    if( defined($expid) && !defined $cmd{expid} ){
157 158
        $cmd{expid} = $expid;
    }
159 160

    my $sercmd = serialize_hash( \%cmd );
161
#    print "sercmd=$sercmd\n";
162 163
    my $f_success = 0;
    my $max_tries = 3;
164
    my $retval;
165 166 167 168
    do{
	$socket = IO::Socket::INET->new( PeerPort => $port,
					 Proto    => 'tcp',
					 PeerAddr => $node,
169
					 Timeout  => 3 );
170
	if( defined $socket ){
171
#	    $sel->add($socket);
172 173
	    print $socket "$sercmd\n";
	    $sel->add($socket);
174
	    my ($ready) = $sel->can_read(2);  #timeout (seconds) (?)
175
	    if( defined($ready) && $ready eq $socket ){
176 177
		my $ack;
		($ack, $retval) = <$ready>;
178 179 180 181 182 183 184 185
		if( defined $ack && ( (chomp($ack),$ack) eq "ACK") ){
		    $f_success = 1;
#		        print "Got ACK from $node for command\n";
		    close $socket;
		    
		}else{
		    $max_tries--;
		}
186 187
		chomp $retval 
		    if( defined $retval );
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	    }else{
		$max_tries--;
	    }
	    $sel->remove($socket);
	    close($socket);
	}else{
	    select(undef, undef, undef, 0.2);
	    $max_tries--;
	}
    }while( $f_success != 1 && $max_tries != 0 );

    if( $f_success == 0 && $max_tries == 0 ){
	$deadnodes{$node} = 1;
#	print "DID NOT GET ACK from $node for command $sercmd\n";
	return -1;
    }elsif( $f_success == 1 ){
	#success!
	delete $deadnodes{$node};
206
	return (1,$retval);
207 208 209 210 211
    }

}


212 213 214 215 216 217 218


#
# input params:
# - name of command (EDIT, INIT, etc..)
# - hash of extra strings to add to event notification
# - handle to eventsystem "handle"
219
sub sendcmd_evsys($$$;$)
220
{
221
    my ($cmdname, $hashref,$handle,$manType) = @_;
222 223
    my %cmd = %$hashref;

224 225 226 227 228
    if( !defined $manType ){
	$manType = "managerclient";
    }
    print "manType = $manType\n";

229 230 231 232
    if( defined($expid) && !defined $cmd{expid} ){
        $cmd{expid} = $expid;
    }

233 234 235 236 237 238
    #
    # This is the evsys command to send
    #
    my $tuple = event::address_tuple_alloc();
    if (!$tuple) { die "Could not allocate an address tuple\n"; }

239 240
    %$tuple = ( objtype   => "WANETMON",
		objname   => $manType,
241
		eventtype => $cmdname,
242
		expt      => $evexpid,
243 244 245 246 247 248 249 250 251 252 253 254
		);

    my $notification = event::event_notification_alloc($handle,$tuple);
    if (!$notification) { die "Could not allocate notification\n"; }

    # set extra params
    foreach my $name (keys %cmd){
	if( 0 == event::event_notification_put_string( $handle,
						$notification,
						"$name",
						$cmd{$name} ) )
	{ warn "Could not add attribute to notification\n"; }
Dan Gebhardt's avatar
Dan Gebhardt committed
255
#	print "adding $name => ".$cmd{$name}."\n";
256 257 258 259 260 261 262 263 264 265 266 267 268 269
    }

    #send notification
    if (!event::event_notify($handle, $notification)) {
	die("could not send test event notification");
    }

    event::event_notification_free($handle, $notification);
}


sub stopnode($$)
{
    my ($node,$managerID) = @_;
270
    my %cmd = (
271
		managerID => $managerID,
272 273 274 275
		cmdtype  => "STOPALL" );
    sendcmd($node,\%cmd);
}

276 277 278 279 280 281 282 283 284

#
#
sub stopnode_evsys($$$)
{
    my ($node, $managerID, $handle) = @_;
    my %cmd = ( srcnode      => $node,
		managerID    => $managerID,
		cmdtype  => "STOPALL" );
285 286 287 288 289 290 291 292
    my $manType;
    if( $managerID eq "automanagerclient" ){
	$manType = $managerID;
    }else{
	$manType = "managerclient";
    }

    sendcmd_evsys("STOPALL",\%cmd,$handle,$manType);
293 294 295
}


Dan Gebhardt's avatar
Dan Gebhardt committed
296
sub killnode($)
297
{
Dan Gebhardt's avatar
Dan Gebhardt committed
298
    my ($node) = @_;
299
    my %cmd = (
300 301 302 303 304
		cmdtype  => "DIE" );
    sendcmd($node,\%cmd);
}


305
sub edittest($$$$$$)
306
{
307 308
    my ($srcnode, $destnode, $testper, $testtype, $duration, $managerID) = @_;

309 310 311 312 313 314 315 316
    if ($srcnode eq $destnode ){
	return -1;
    }

    my %cmd = ( expid    => $expid,
		cmdtype  => "EDIT",
		dstnode  => $destnode,
		testtype => $testtype,
317 318
		testper  => "$testper",
		duration => "$duration" );
319

320
    return ${[sendcmd($srcnode,\%cmd)]}[0];
321 322
}

323 324 325 326 327 328 329 330 331 332 333 334 335
sub edittest_evsys($$$$$$$)
{
    my ($srcnode, $destnode, $testper, $testtype, 
	$duration, $managerID, $handle) = @_;

    if ($srcnode eq $destnode ){
	return -1;
    }

    my %cmd = ( managerID    => $managerID,
		srcnode  => $srcnode,
		dstnode  => $destnode,
		testtype => $testtype,
336 337 338 339 340 341 342 343 344
		testper  => "$testper",
		 duration => "$duration" );

    my $manType;
    if( $managerID eq "automanagerclient" ){
	$manType = $managerID;
    }else{
	$manType = "managerclient";
    }
345

346
    sendcmd_evsys("EDIT",\%cmd,$handle,$manType);
347 348 349 350

    #return ${[sendcmd($srcnode,\%cmd)]}[0];
}

351

352 353 354 355 356 357 358 359 360 361 362
sub getstatus($){
    my ($node) = @_;
    my %cmd = ( expid    => $expid,
		cmdtype  => "GETSTATUS" );
    my @cmdresult = sendcmd($node,\%cmd);
    if( defined $cmdresult[1] ){
	return $cmdresult[1];
    }else{
	return "error";
    }
}
363 364


365 366


367
1;