linkmon_ctl.in 3.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#

use English;
use Getopt::Std;

#
# Control tracing/monitoring on links.
#
sub usage()
{
    print(STDERR
	  "Usage: linkmon_ctl [-d] [-s vnode] <pid> <eid> <link> <action>\n".
	  "  pid = Project ID\n".
	  "  eid = Experiment ID\n".
	  " link = link name from ns file, ie. 'link1' in\n".
	  "        'set link1 [\$ns duplex-link \$A \$B 10Kb 0ms DropTail]'\n".
	  "action= One of pause, restart, kill\n".
	  "Options:\n".
	  "   -d = turn on debugging\n".
	  "   -s = Select the source of the link to determine which pipe\n");
    # Web interface cares about this return value!
    exit(2);
}
my  $optlist = "ds:";

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TEVC        = "$TB/bin/tevc";
my $debug	= 0;

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (@ARGV != 4) {
    usage();
}
# Locals
my $pid    = shift(@ARGV);
my $eid    = shift(@ARGV);
my $link   = shift(@ARGV);
my $action = shift(@ARGV);
my $srcvnode;

if (defined($options{"s"})) {
    $srcvnode = $options{"s"};

    if ($srcvnode =~ /^([-\w]+)$/) {
	$srcvnode = $1;
    }
    else {
	die("*** Bad srcvnode name: $srcvnode.\n");
    }
}
#
# Untaint args.
#
if ($pid =~ /^([-\w]+)$/) {
    $pid = $1;
}
else {
    die("*** Bad data in pid: $pid.\n");
}
if ($eid =~ /^([-\w]+)$/) {
    $eid = $1;
}
else {
    die("*** Bad data in eid: $eid.\n");
}
if ($link =~ /^([-\w]+)$/) {
    $link = $1;
}
else {
    die("*** Bad data in link: $link.\n");
}
if ($action =~ /^([\w]+)$/) {
    $action = $1;
}
else {
    die("*** Bad data in action: $action\n");
}
usage()
    if ($action ne "pause" &&
	$action ne "restart" &&
	$action ne "kill" &&
	$action ne "snapshot");

# Permission check.
#
if ($UID && !TBAdmin($UID) &&
    ! TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_MODIFY)) {
    die("*** $0:\n".
	"    You do not have permission to modify the delay parameters!\n");
}

#
# No transitional experiments.
#
my $estate = ExpState($pid, $eid);
if (! $estate) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid exists!\n");
}
if ($estate ne EXPTSTATE_ACTIVE) {
    die("*** $0:\n".
	"    Experiment $pid/$eid must be ACTIVE\n".
	"    to control its tracing/monitoring\n");
}

#
# Link or Lan!
# 
$query_result =
    DBQueryFatal("select member from virt_lans ".
		 "where pid='$pid' and eid='$eid' and vname='$link'");
if (!$query_result->numrows) {
    die("*** $0:\n".
	"    $link is not a link in $pid/$eid!\n");
}
my $islink = ($query_result->numrows == 2 ? 1 : 0);

#
# Inject an event. 
#
my $inject_string = "$TEVC -e $pid/$eid now $link";

# Direct the event to the whoever is handling this particular delay.
$inject_string .= "-${srcvnode}"
    if (defined($srcvnode));

# XXX The actual agent name. Yuck!
$inject_string .= "-tracemon";

#
# Map action
#
if ($action eq "pause") {
    $action = "STOP";
}
elsif ($action eq "restart") {
    $action = "START";
}
elsif ($action eq "stop") {
    $action = "KILL";
}
elsif ($action eq "snapshot") {
    $action = "SNAPSHOT";
}
$inject_string .= " $action";

if ($debug) {
    print "$inject_string\n";
}

system($inject_string) &&
    die("*** $0:\n".
	"    Failed to inject delay update event!\n");
    
exit(0);