linkmon_mon.php3 3.57 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005, 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11
# All rights reserved.
#
include("defs.php3");

#
# Only known and logged in users.
#
12 13 14
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
15 16

#
17
# Verify page arguments.
18
#
19 20 21 22 23 24 25 26 27 28 29 30
$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);
31 32 33
}

#
34 35 36
# 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. 
37
#
38 39
if ($experiment->state() != $TB_EXPTSTATE_ACTIVE) {
    USERERROR("Experiment $eid must be active to monitor its links!", 1);
40
}
41

42 43 44
if (! TBvalid_linklanname($linklan)) {
    PAGEARGERROR("$linklan contains invalid characters!");
}
45
if (! TBvalid_vnode_id($vnode)) {
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
    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.
76 77
# 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.
78 79
#
$portnum  = 4442 + $idx;
80 81 82 83
if (preg_match("/^plabvm\d+-(\d+)$/", $nodeid, $matches)) {
    $vnum = $matches[1];
    $portnum += ($vnum * 10);
}
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

#
# 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);

?>