linkmon_mon.php3 4.26 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2005, 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/>.
# 
# }}}
23 24 25 26 27 28
#
include("defs.php3");

#
# Only known and logged in users.
#
29 30 31
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
32 33

#
34
# Verify page arguments.
35
#
36 37 38 39 40 41 42 43 44 45 46 47
$reqargs = RequiredPageArguments("experiment", PAGEARG_EXPERIMENT,
				 "linklan",    PAGEARG_STRING,
				 "vnode",      PAGEARG_STRING);
$optargs = OptionalPageArguments("action",     PAGEARG_STRING,
				 "updatefreq", PAGEARG_INTEGER);

# Need these below.
$pid = $experiment->pid();
$eid = $experiment->eid();

if (!$experiment->AccessCheck($this_user, $TB_EXPT_READINFO)) {
    USERERROR("You do not have permission to run linktest on $pid/$eid!", 1);
48 49 50
}

#
51 52 53
# Must be active. The backend can deal with changing the base experiment
# when the experiment is swapped out, but we need to generate a form based
# on virt_lans instead of delays/linkdelays. Thats harder to do. 
54
#
55 56
if ($experiment->state() != $TB_EXPTSTATE_ACTIVE) {
    USERERROR("Experiment $eid must be active to monitor its links!", 1);
57
}
58

59 60 61
if (! TBvalid_linklanname($linklan)) {
    PAGEARGERROR("$linklan contains invalid characters!");
}
62
if (! TBvalid_vnode_id($vnode)) {
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
    PAGEARGERROR("$vnode contains invalid characters!");
}

#
# Look to see if an update frequency provide (in seconds, convert to ms).
# Default to 1 second.
#
$freq = 1000;

if (isset($updatefreq) && $updatefreq != "" &&
    TBvalid_tinyint($updatefreq)) {
    $freq = $updatefreq * 1000;
}

#
# Okay, grab the appropriate line from the traces table.
# 
$query_result =
    DBQueryFatal("select * from traces ".
		 "where pid='$pid' and eid='$eid' and ".
		 "      linkvname='$linklan' and vnode='$vnode'");
if (mysql_num_rows($query_result) == 0) {
    USERERROR("No trace is being performed on $linkname/$vnode", 1);
}
$tracerow = mysql_fetch_array($query_result);
$nodeid   = $tracerow['node_id'];
$idx      = $tracerow['idx'];

#
# Wow, what a kludge. The port number is the index + 4442. See rc.trace.
93 94
# Wow, an even bigger kludge.  If this is plabvmXXX-NN, add (10 * NN) to
# the portnum to create unique ports for each vnode on a pnode. See tmcd.c.
95 96
#
$portnum  = 4442 + $idx;
97 98 99 100
if (preg_match("/^plabvm\d+-(\d+)$/", $nodeid, $matches)) {
    $vnum = $matches[1];
    $portnum += ($vnum * 10);
}
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

#
# Clean up when the remote user disconnects
#
$socket = 0;

function SPEWCLEANUP()
{
    global $socket;

    if (!$socket || !connection_aborted()) {
	exit();
    }
    fclose($socket);
    exit();
}
set_time_limit(0);
register_shutdown_function("SPEWCLEANUP");

# Avoid PHP error reporting in sockopen that confuse the headers.
error_reporting(0);

$socket = fsockopen($nodeid, $portnum);
if (!$socket) {
    USERERROR("Error opening $nodeid/$portnum - $errstr",1);
}

#
# The protocol is that the other end spits out two lines, and then we
# tell it the delay between updates. 
# 
# First line is number of links.
#
if (! ($str = fgets($socket, 1024))) {
    TBERROR("Error getting first line of output", 1);
}
if (! preg_match("/^(\d*)$/", $str, $matches)) {
    TBERROR("Error parsing first line of output", 1);
}
$numlinks = $matches[1];

#
# Now suck up that many lines.
#
while ($numlinks) {
    fgets($socket, 1024);
    $numlinks--;
}

# And write the update frequency to it.
fwrite($socket, "$freq\n");

header("Content-Type: text/plain");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
flush();

while (! feof($socket)) {
    $buffer = fgets($socket, 1024);
    echo $buffer;
    flush();
}
fclose($socket);

?>