.
*/
// eventGenerator.php
/**
* Test tool to generate events
* @package netmap
* @subpackage eventGenerator
* @todo Add a debug function
* @todo If this proj goes somewhere, use a decent ORM
*/
//Generates these events:
//Adds new server to a router
//'disconnects' a device
//'reconnects' a device
/*
* INCLUSIONS
*/
require_once('config/config.php');
require_once('config/db.php');
$htmlData = "";
/**
* sends update/insert queries to DB
* @param unknown_type $query
*/
function myInsert($query) {
$result = mysql_query($query);
//echo "
".var_dump($query)."
";
}
function getDeviceFromName($nodeName) {
$query="SELECT * FROM Devices where name ='".$nodeName."';";
$result = mysql_query($query);
$val = mysql_fetch_object($result);
return $val;
}
function getDevice($id) {
$query="SELECT * FROM Devices where id ='".$id."';";
$result = mysql_query($query);
$val = mysql_fetch_object($result);
return $val;
}
function insertConnection ($nodeAID,$nodeBID) {
global $htmlData;
$nodeA = getDevice($nodeAID);
$nodeB = getDevice($nodeBID);
($nodeA->type == 'routerLAN') ? $type = 'LAN' : $type = 'WAN';
$query="INSERT INTO Connections
(device_id1, device_id2, status, type, created_at, updated_at)
VALUES (
".$nodeA->id.",
".$nodeB->id.",
1,
'".$type."',
NOW(),
NOW()
);";
$htmlData .= sprintf ("Inserting Connection %s (%s) => %s (%s) ",
$nodeA->name,
$nodeA->id,
$nodeB->name,
$nodeB->id);
myInsert($query);
}
/**
* Adds random (1 to 5) sibling nodes
* @param unknown_type $node The parent node
*/
function addNodes($nodeID){
global $htmlData;
$nodes_to_add = rand (1,5);
$node = getDevice($nodeID);
$pieces = explode(".",$node->name,2);
$dc = $pieces[1];
for ($serverID = 1 ; $serverID <= $nodes_to_add; $serverID++ ){
$serverName="server-".($nodes_to_add*20+$serverID).".".$dc;
//update the Devices and Connection Tables
$query="INSERT INTO Devices
(name, LANip, WANip, LANmac, WANmac, dc, LANstatus, WANstatus, type, created_at, updated_at)
VALUES (
'".$serverName."',
'192.168.".$nodeID.".".($serverID*$nodes_to_add+20)."',
'1.2.".$nodeID.".".($serverID*$nodes_to_add+20)."',
'aa:bb:cc:00:11:2".$serverID."',
'ff:ee:gg:66:55:4".$serverID."',
'".$dc."',
true,
true,
'server',
NOW(),
NOW()
);";
$htmlData .= sprintf("Inserting server %s ",$serverName);
myInsert($query);
$newNode = getDeviceFromName ($serverName);
insertConnection($node->id,$newNode->id);
//Update the Events table (in this case we add the two devices to say
//it is a new node plus a new connection!
$query = sprintf("INSERT INTO Events
(device_id1, device_id2, status, type, created_at, updated_at)
VALUES (
'%s', %s, true, NULL, NOW(), NOW() );",
mysql_real_escape_string($node->id),
mysql_real_escape_string($newNode->id),
mysql_real_escape_string($node->type)
);
$htmlData .= sprintf("Inserting Event ",$serverName);
myInsert($query);
}
}
function disconnect($nodeID,$type) {
global $htmlData;
$node = getDevice($nodeID);
$htmlData .= sprintf("Disconnecting %s on node %s ",$type,$node->name);
toggleStatus($node,$type,"false");
}
function reconnect($nodeID,$type) {
global $htmlData;
$node = getDevice($nodeID);
$htmlData .= sprintf("Reconnecting %s on node %s ",$type, $node->name);
toggleStatus($node,$type,"true");
}
/**
* Modifies the status of a device
* @param $node Node object
* @param $type "LAN" or"WAN"
* @param $status
*/
function toggleStatus($node,$type,$status){
//Add event to the Events list
$statusType = $type."status";
$query = sprintf("INSERT INTO Events
(device_id1, status, type, created_at, updated_at)
VALUES (
'%s', %s, '%s' , NOW(), NOW() );",
mysql_real_escape_string($node->id),
$status,
mysql_real_escape_string($type)
);
myInsert($query);
//Modify the Devices list
$query=sprintf ("UPDATE Devices SET %s=%s WHERE id='%s';",
mysql_real_escape_string($statusType),
$status,
mysql_real_escape_string($node->id)
);
myInsert($query);
//Update all his siblings? Nope...!
}
if ( isset ( $_GET['action'] ) )
$_GET['action']( $_GET['node'], $_GET['type'] );
//Show the nodes and options
$query = "SELECT * FROM Devices;";
$res = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($res);
if ( $num_rows ) {
$html = "
";
//Parse each event and create a new element for the output
while ($node = mysql_fetch_object($res)) {
$html .= "