Wireless Leiden Interactive Nodemap 2.0
1
Wireless Leiden Interactive Nodemap 2.0
%OVERALL_LONGITUDE%
%OVERALL_LATITUDE%
%OVERALL_ALTITUDE%
%OVERALL_HEADING%
%OVERALL_TILT%
%OVERALL_RANGE%
Nodes
Nodes from the Wireless Leiden network
%OVERALL_LONGITUDE%
%OVERALL_LATITUDE%
%OVERALL_ALTITUDE%
%OVERALL_HEADING%
%OVERALL_TILT%
%OVERALL_RANGE%
%NODESCONTENT%
Lines
Lines from nodes to nodes from the Wireless Leiden network
%OVERALL_LONGITUDE%
%OVERALL_LATITUDE%
%OVERALL_ALTITUDE%
%OVERALL_HEADING%
%OVERALL_TILT%
%OVERALL_RANGE%
%LINESCONTENT%
';
// First line of the status file must be like this:
static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged';
// Every following line will be checked using these functions
private $fileContent = array('string', 'string', 'int', 'double', 'int', 'int', 'int', 'int');
private $KMLNodes;
private $KMLLines;
private $NetworkList;
/*
* Function: __construct (constructor)
* Description: Creating a new KMLFile
* Parameters: -
* Returns: -
*/
public function __construct() {
$this->KMLNodes = array();
$this->KMLLines = array();
$this->NetworkList = new NetworkList();
}
/*
* Function: addNode
* Description: Add a node to the local node array
* Parameters: KMLNode $node
* Returns: -
*/
public function addNode(KMLNode $node) {
$this->KMLNodes[] = $node;
}
/*
* Function: addLIne
* Description: Add a line to the local line array
* Parameters: KMLLine $line
* Returns: -
*/
public function addLine(KMLLine $line) {
$this->KMLLines[] = $line;
}
/*
* Function: toString
* Description: Converts the content of this file and the placemarks to a KML valid string
* Parameters: -
* Returns: KML valid string
*/
public function toString() {
global $config;
$toString = $this->template;
// Add all nodes to the string
$nodeString = '';
$nodeCount = count($this->KMLNodes);
for ($i = 0; $i < $nodeCount; $i++) {
$nodeString .= $this->KMLNodes[$i]->toString();
}
// Add all connected lines to the string
$lineString = '';
$lineCount = count($this->KMLLines);
for ($i = 0; $i < $lineCount; $i++) {
// If longitude2 and latitude2 aren't set, ignore the lines
// This happens with all the connections that don't connect 2 nodes
if ($this->KMLLines[$i]->isConnected()) {
$lineString .= $this->KMLLines[$i]->toString();
}
}
$toString = str_replace('%NODE_GREEN%', $config['node_green'], $toString);
$toString = str_replace('%NODE_ORANGE%', $config['node_orange'], $toString);
$toString = str_replace('%NODE_RED%', $config['node_red'], $toString);
$toString = str_replace('%LINE_BLACK%', $config['line_black'], $toString);
$toString = str_replace('%NODESCONTENT%', $nodeString, $toString);
$toString = str_replace('%LINESCONTENT%', $lineString, $toString);
$toString = str_replace('%OVERALL_LONGITUDE%', $config['overall_longitude'], $toString);
$toString = str_replace('%OVERALL_LATITUDE%', $config['overall_latitude'], $toString);
$toString = str_replace('%OVERALL_ALTITUDE%', $config['overall_altitude'], $toString);
$toString = str_replace('%OVERALL_HEADING%', $config['overall_heading'], $toString);
$toString = str_replace('%OVERALL_TILT%', $config['overall_tilt'], $toString);
$toString = str_replace('%OVERALL_RANGE%', $config['overall_range'], $toString);
return $toString;
}
/*
* Function: getNodeByName
* Description: Find the first KMLNode with the name $name and return its position. If not found, return false
* Parameters: string $name
* Returns: Position of node in our array, if not found false
*/
public function getNodeByName($name) {
$nodesCount = count($this->KMLNodes);
for ($i = 0; $i < $nodesCount; $i++) {
if ($this->KMLNodes[$i]->getName() == $name) {
return $i;
}
}
return false;
}
/*
* Function: parseLocationFile
* Description: Parse the node location file updating or adding KMLNode objects to the current KMLFile object
* Parameters: string $file
* Returns: true is successfull, otherwise false
*/
public function parseLocationFile($file) {
// We want to find all the nodes in the location file and store information of the nodes...
$nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE);
for ($i = 0; $i < $nodesCount; $i++) {
// Looking for "location" of the node
if (!$location = $this->findInLocationFile($file, 'location = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "location" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Looking for "status" of the node
if (!$status = $this->findInLocationFile($file, 'status = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "status" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Looking for "latitude" of the node
if (!$latitude = $this->findInLocationFile($file, 'latitude = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "latitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Looking for "longitude" of the node
if (!$longitude = $this->findInLocationFile($file, 'longitude = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "longitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Looking for "interfaces" of the node
if (!$interfaces = $this->findInLocationFile($file, 'interfaces = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "interfaces" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Looking for "masterip" of the node
if (!$masterip = $this->findInLocationFile($file, 'masterip = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "masterip" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Looking for "nodetype" of the node
if (!$nodetype = $this->findInLocationFile($file, 'nodetype = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "nodetype" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Looking for "name" of the node
if (!$name = $this->findInLocationFile($file, 'name = ', $nodes[0][$i][1])) {
trigger_log(SYSLOG_WARNING, 'Could not find the "name" of node "' . $i . '", skipping to next', __FILE__, __LINE__);
continue;
}
// Creating a string with the complete description of the node using all data in the location file
$dataLocation = '
' . $status . '
' . $interfaces . '
' . $masterip . '
' . $nodetype . '
';
if ($placemarkPosition = $this->getNodeByName($name)) {
// Updating an excisting placemark
$this->KMLNodes[$placemarkPosition]->setDataLocation($dataLocation);
$this->KMLNodes[$placemarkPosition]->setLongitude($longitude);
$this->KMLNodes[$placemarkPosition]->setLatitude($latitude);
} else {
// Adding a new placemark
$placemark = new KMLNode();
$placemark->setID($name);
$placemark->setName($name);
$placemark->setDataLocation($dataLocation);
$placemark->setLongitude($longitude);
$placemark->setLatitude($latitude);
$this->addNode($placemark);
}
// Now let's find all interlinks with this node
$position = $nodes[0][$i][1];
while (($position = strpos($file, 'ip=', $position + 1)) && (isset($nodes[0][$i + 1][1]) && $position < $nodes[0][$i + 1][1])) {
$ipAddress = $this->findInLocationFile($file, 'ip=', $position);
$posSlash = strpos($ipAddress, '/');
$ip = substr($ipAddress, 0, $posSlash);
$netmask = substr($ipAddress, $posSlash + 1);
$posNetwork = $this->NetworkList->find($ip);
if ($posNetwork) {
// Network already excists in list, just need to add longitude2 en latitude2 to KMLLine
// and add the node name to the name of the line.
// Position in network equals position in KMLLine array :-)
$this->KMLLines[$posNetwork]->setName($this->KMLLines[$posNetwork]->getName() . $name);
$this->KMLLines[$posNetwork]->setLongitude2($longitude);
$this->KMLLines[$posNetwork]->setLatitude2($latitude);
} else {
// Network doesn't excist. We create a new network and a new line.
$network = new Network($ip, $netmask);
$this->NetworkList->add($network);
$data = '
' . $network->netmaskDecimal . '
' . $network->netmaskBinary . '
' . $network->wildcardDecimal . '
' . $network->wildcardBinary . '
' . $network->networkDecimal . '
' . $network->networkBinary . '
' . $network->broadcastDecimal . '
' . $network->broadcastBinary . '
' . $network->hostminDecimal . '
' . $network->hostminBinary . '
' . $network->hostmaxDecimal . '
' . $network->hostmaxBinary . '
' . $network->numberHosts . '
';
$line = new KMLLine();
$line->setID(str_replace('.', '', $network->networkDecimal));
$line->setName('Link: Van ' . $name . ' naar ');
$line->setData($data);
$line->setLongitude1($longitude);
$line->setLatitude1($latitude);
$this->KMLLines[] = $line;
}
}
}
}
/*
* Function: findInLocationFile
* Description: Find the $keyword in $file and return the value of $keyword, starting at $offset
* Parameters: string $file, string $keyword, integer $offset
* Returns: The value of the keyword if found, otherwise return false
*/
private function findInLocationFile($file, $keyword, $offset) {
$start = strpos($file, $keyword, $offset) + strlen($keyword);
$end = strpos($file, "\n", $start);
if ($start && $end && ($start < $end)) {
return substr($file, $start, $end - $start);
} else {
return false;
}
}
/*
* Function: parseStatusFile
* Description: Parse the node status file updating or adding KMLNode objects to the current KMLFile object
* Parameters: string $file
* Returns: true is successfull, otherwise false
*/
public function parseStatusFile($file) {
$fileContents = explode("\r\n", $file);
if ($fileContents[0] != KMLFile::$fileFirst) {
trigger_log(SYSLOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__);
}
// For loop for all the lines in the file. Skipping first line (headers) and last line (blank)
$linesCount = count($fileContents);
for ($i = 1; $i < $linesCount - 1; $i++) {
$lineContent = explode(',', $fileContents[$i]);
$lineContent[1] = str_replace('CNode', '', $lineContent[1]);
echo $lineContent[1];
if (count($lineContent) != count($this->fileContent)) {
trigger_log(LOG_WARNING, 'Contents of the file do not match with template of lines on line "' . $i . '"', __FILE__, __LINE__);
continue;
}
// Checking for valid entries on this line
for ($j = 0; $j < 8; $j++) {
try {
switch ($this->fileContent[$j]) {
case 'string':
$lineContent[$j] = (string) $lineContent[$j];
break;
case 'int':
$lineContent[$j] = (int) $lineContent[$j];
break;
case 'double':
$lineContent[$j] = (double) $lineContent[$j];
break;
default:
break;
}
} catch (Exception $err) {
trigger_log(SYSLOG_WARNING, 'The value "' . $j . '" on line "' . $i . '" is not valid, skipping to next line', __FILE__, __LINE__);
continue;
}
}
// Creating a string with the complete description of the node using all data in the status file
$dataStatus = '
' . $lineContent[0] . '
' . $lineContent[1] . '
' . $lineContent[2] . '
' . $lineContent[3] . '
' . $lineContent[4] . '
' . $lineContent[5] . '
' . $lineContent[6] . '
' . $lineContent[7] . '
';
$style = NODE_RED;
if ($lineContent[4] == NODE_STATUS_ERROR) {
$style = NODE_ORANGE;
} elseif ($lineContent[4] == NODE_STATUS_UP) {
$style = NODE_GREEN;
}
if ($placemarkPosition = $this->getNodeByName($lineContent[1])) {
// Updating an excisting placemark
$this->KMLNodes[$placemarkPosition]->setDataStatus($dataStatus);
$this->KMLNodes[$placemarkPosition]->setStyle($style);
} else {
// Adding a new placemark
$placemark = new KMLNode();
$placemark->setID($lineContent[1]);
$placemark->setName($lineContent[1]);
$placemark->setDataStatus($dataStatus);
$placemark->setStyle($style);
$this->addNode($placemark);
}
}
}
}
?>