Changeset 7720
- Timestamp:
- Apr 12, 2010, 5:56:17 PM (15 years ago)
- Location:
- trunk/src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/inc/ErrorHandler.php
r7661 r7720 26 26 /* 27 27 * Function: NodeErrorHandler 28 * Description: Handling with errors, decides what log level it is and send it to LogHandler 28 29 * Parameters: int $errno, string $errstr, string $errfile, int $errline, array $errcontext 29 * Function: Handling with errors, decides what log level it is and send it to LogHandler30 * Returns: true (Just so the internal PHP error handler is nog executed) 30 31 */ 31 32 function NodeErrorHandler(int $errno, string $errstr, string $errfile, int $errline, array $errcontext) { -
trunk/src/inc/FileHandler.class.php
r7661 r7720 7 7 8 8 class FileHandler { 9 private $file; 9 private $filename; 10 private $handle; 11 12 /* 13 * Function: __construct (constructor) 14 * Description: Handling with a node file 15 * Parameters: string $filename, string $mode 16 * Returns: true is we've succesfully opened the file, otherwise false 17 */ 18 public function __construct($filename, $mode) { 19 $this->filename = $filename; 20 21 try { 22 trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $this->filename . '"', __FILE__, __LINE__); 23 $this->handle = fopen($this->filename, $mode); 24 } catch (Exception $err) { 25 trigger_log(SYSLOG_ERR, 'Reading from file "' . $this->filename . '" failed', __FILE__, __LINE__); 26 return false; 27 } 28 29 return true; 30 } 10 31 11 32 /* 12 * Function: __construct (constructor) 13 * Parameters: string $filename 14 * Function: Handling with a node file 33 * Function: read 34 * Description: Try to read the file we've opened in the constructor 35 * Parameters: - 36 * Returns: The content of the file if we can read from the file, otherwise false 15 37 */ 16 public function __construct($filename) { 38 public function read() { 39 $file = ''; 40 17 41 try { 18 trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $filename . '"', __FILE__, __LINE__); 19 $handle = fopen($filename, 'r'); 42 trigger_log(SYSLOG_DEBUG, 'Reading from file "' . $this->filename . '"', __FILE__, __LINE__); 43 $file = stream_get_contents($this->handle); 44 } catch (Exception $err) { 45 trigger_log(SYSLOG_ERR, 'Reading from file "' . $this->filename . '" failed', __FILE__, __LINE__); 46 return false; 47 } 20 48 21 trigger_log(SYSLOG_DEBUG, 'Reading from file "' . $filename . '"', __FILE__, __LINE__); 22 $this->file = ''; 23 while (!feof($handle)) { 24 $this->file .= fread($handle, 8192); 25 } 26 27 trigger_log(SYSLOG_DEBUG, 'Closing file "' . $filename . '"', __FILE__, __LINE__); 28 fclose($handle); 29 30 trigger_log(SYSLOG_INFO, 'Reading from file "' . $filename . '" is done', __FILE__, __LINE__); 31 } catch (Exception $err) { 32 trigger_log(SYSLOG_ERR, 'Reading from file "' . $filename . '" failed', __FILE__, __LINE__); 33 } 49 return $file; 34 50 } 35 51 36 public function getFile() { 37 return $this->file; 52 /* 53 * Function: write 54 * Description: Try to write to the file we've opened in the constructor 55 * Parameters: string $data 56 * Returns: true if we can write to the file, otherwise false 57 */ 58 public function write($data) { 59 // TODO David: Make write function 60 61 return false; 62 } 63 64 public function __destruct() { 65 trigger_log(SYSLOG_DEBUG, 'Closing file "' . $this->filename . '"', __FILE__, __LINE__); 66 fclose($this->handle); 38 67 } 39 68 } -
trunk/src/inc/KMLFile.class.php
r7702 r7720 51 51 </kml>'; 52 52 53 // First line of the status file must be like this: 53 54 static $fileFirst = 'type,host_name,has_been_checked,check_execution_time,current_state,last_hard_state,last_check,problem_has_been_acknowledged'; 54 private $fileContent = array('string', 'string', 'integer', 'double', 'integer', 'integer', 'integer', 'integer'); 55 // Every following line will be checked using these functions 56 private $fileContent = array('string', 'string', 'int', 'double', 'int', 'int', 'int', 'int'); 55 57 56 58 private $KMLPlacemarks; … … 58 60 /* 59 61 * Function: __construct (constructor) 62 * Description: Creating a new KMLFile 60 63 * Parameters: - 61 * Function: Creating a new KMLFile64 * Returns: - 62 65 */ 63 66 public function __construct() { … … 65 68 } 66 69 70 /* 71 * Function: addPlacemark 72 * Description: Add a placemark to the local placemark array 73 * Parameters: KMLPlacemark $placemark 74 * Returns: - 75 */ 67 76 public function addPlacemark(KMLPlacemark $placemark) { 68 77 $this->KMLPlacemarks[] = $placemark; 69 78 } 70 79 80 /* 81 * Function: toString 82 * Description: Converts the content of this file and the placemarks to a KML valid string 83 * Parameters: - 84 * Returns: KML valid string 85 */ 71 86 public function toString() { 87 global $config; 88 72 89 $toString = $this->template; 73 90 … … 88 105 /* 89 106 * Function: write 107 * Description: Write KMLFile to a KML file 90 108 * Parameters: string $filename 91 * Function: Write KMLFile to a KML file109 * Returns: true if we can write to the file, otherwise false 92 110 */ 93 111 public function write($filename) { 112 // TODO: David: Needs to be placed in FileHandler.class.php. Here we just want to call our file handler. 113 94 114 trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $filename . '"', __FILE__, __LINE__); 95 115 $file = fopen("$filename","w") … … 105 125 /* 106 126 * Function: getPlacemarkByName 127 * Description: Find the first KMLPlacemark with the name $name and return its position. If not found, return false 107 128 * Parameters: string $name 108 * Function: Find the first KMLPlacemark with the name $name and return its position. If not found, returnfalse129 * Returns: Position of placemark in our array, if not found false 109 130 */ 110 131 public function getPlacemarkByName($name) { … … 115 136 } 116 137 } 138 117 139 return false; 118 140 } … … 120 142 /* 121 143 * Function: parseLocationFile 144 * Description: Parse the node location file updating or adding KMLPlacemark objects to the current KMLFile object 122 145 * Parameters: string $file 123 * Function: Parse the node location file updating or adding KMLPlacemark objects to the current KMLFile object146 * Returns: true is successfull, otherwise false 124 147 */ 125 148 public function parseLocationFile($file) { 126 149 $nodesCount = preg_match_all('/\[[a-zA-Z0-9]*\]/i', $file, $nodes, PREG_OFFSET_CAPTURE); 127 150 for ($i = 0; $i < $nodesCount; $i++) { 128 // TODO: Needs checking for parsing errors 129 $location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1]); 130 $status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1]); 131 $latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1]); 132 $longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1]); 133 $interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1]); 134 $masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1]); 135 $nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1]); 136 $name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1]); 137 151 // Looking for "location" of the node 152 if (!$location = $this->findInLocationFile($file, 'location', $nodes[0][$i][1])) { 153 trigger_log(SYSLOG_WARNING, 'Could not find the "location" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 154 continue; 155 } 156 // Looking for "status" of the node 157 if (!$status = $this->findInLocationFile($file, 'status', $nodes[0][$i][1])) { 158 trigger_log(SYSLOG_WARNING, 'Could not find the "status" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 159 continue; 160 } 161 // Looking for "latitude" of the node 162 if (!$latitude = $this->findInLocationFile($file, 'latitude', $nodes[0][$i][1])) { 163 trigger_log(SYSLOG_WARNING, 'Could not find the "latitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 164 continue; 165 } 166 // Looking for "longitude" of the node 167 if (!$longitude = $this->findInLocationFile($file, 'longitude', $nodes[0][$i][1])) { 168 trigger_log(SYSLOG_WARNING, 'Could not find the "longitude" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 169 continue; 170 } 171 // Looking for "interfaces" of the node 172 if (!$interfaces = $this->findInLocationFile($file, 'interfaces', $nodes[0][$i][1])) { 173 trigger_log(SYSLOG_WARNING, 'Could not find the "interfaces" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 174 continue; 175 } 176 // Looking for "masterip" of the node 177 if (!$masterip = $this->findInLocationFile($file, 'masterip', $nodes[0][$i][1])) { 178 trigger_log(SYSLOG_WARNING, 'Could not find the "masterip" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 179 continue; 180 } 181 // Looking for "nodetype" of the node 182 if (!$nodetype = $this->findInLocationFile($file, 'nodetype', $nodes[0][$i][1])) { 183 trigger_log(SYSLOG_WARNING, 'Could not find the "nodetype" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 184 continue; 185 } 186 // Looking for "name" of the node 187 if (!$name = $this->findInLocationFile($file, 'name', $nodes[0][$i][1])) { 188 trigger_log(SYSLOG_WARNING, 'Could not find the "name" of node "' . $i . '", skipping to next', __FILE__, __LINE__); 189 continue; 190 } 191 192 // Creating a string with the complete description of the node using all data in the location file 138 193 $descriptionLocation = 'Naam: ' . $name . '<br/>Locatie: ' . $location . '<br/>Status: ' . $status . '<br/>Latitude: ' . $latitude . '<br/>Longitude: ' . $longitude . '<br/>Interfaces: ' . $interfaces . '<br/>Master IP: ' . $masterip . '<br/>Node type: ' . $nodetype . '<br/><br/>'; 139 194 140 195 if ($placemarkPosition = $this->getPlacemarkByName($name)) { 196 // Updating an excisting placemark 141 197 $this->KMLPlacemarks[$placemarkPosition]->setDescriptionLocation($descriptionLocation); 142 198 $this->KMLPlacemarks[$placemarkPosition]->setLongitude($longitude); 143 199 $this->KMLPlacemarks[$placemarkPosition]->setLatitude($latitude); 144 200 } else { 201 // Adding a new placemark 145 202 $placemark = new KMLPlacemark(); 146 203 $placemark->setID($name); … … 156 213 /* 157 214 * Function: findInLocationFile 215 * Description: Find the $keyword in $file and return the value of $keyword, starting at $offset 158 216 * Parameters: string $file, string $keyword, integer $offset 159 * Function: Find the $keyword in $file and return the value of $keyword, starting at $offset, on errorreturn false217 * Returns: The value of the keyword if found, otherwise return false 160 218 */ 161 219 private function findInLocationFile($file, $keyword, $offset) { 162 $start 220 $start = strpos($file, $keyword, $offset) + strlen($keyword . ' = '); 163 221 $end = strpos($file, "\n", $start); 164 222 … … 172 230 /* 173 231 * Function: parseStatusFile 232 * Description: Parse the node status file updating or adding KMLPlacemark objects to the current KMLFile object 174 233 * Parameters: string $file 175 * Function: Parse the node status file updating or adding KMLPlacemark objects to the current KMLFile object234 * Returns: true is successfull, otherwise false 176 235 */ 177 236 public function parseStatusFile($file) { … … 179 238 180 239 if ($fileContents[0] != KMLFile::$fileFirst) { 181 trigger_log(LOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__); 182 } 183 240 trigger_log(SYSLOG_WARNING, 'Contents of file do not match with template of first line', __FILE__, __LINE__); 241 } 242 243 // For loop for all the lines in the file. Skipping first line (headers) and last line (blank) 184 244 $linesCount = count($fileContents); 185 245 for ($i = 1; $i < $linesCount - 1; $i++) { … … 187 247 188 248 if (count($lineContent) != count($this->fileContent)) { 189 trigger_log(LOG_WARNING, 'Contents of file do not match with template of lines', __FILE__, __LINE__); 190 } 191 192 // TODO: Process all lines and update KMLPlacemark objects we already have in the current object 193 $type = $lineContent[0]; 194 $host_name = str_replace('CNode', '', $lineContent[1]); 195 $has_been_checked = $lineContent[2]; 196 $check_execution_time = $lineContent[3]; 197 $current_state = $lineContent[4]; 198 $last_hard_state = $lineContent[5]; 199 $last_check = $lineContent[6]; 200 $problem_has_been_acknowledged = $lineContent[7]; 201 202 $descriptionStatus = 'Type: ' . $type . '<br/>Host name: ' . $host_name . '<br/>Has been checked: ' . $has_been_checked . '<br/>Check execution time: ' . $check_execution_time . '<br/>Currenr state: ' . $current_state . '<br/>Last hard state: ' . $last_hard_state . '<br/>Last check: ' . $last_check . '<br/>Problem has been acknowledged: ' . $problem_has_been_acknowledged . '<br/><br/>'; 203 204 if ($placemarkPosition = $this->getPlacemarkByName($host_name)) { 249 trigger_log(LOG_WARNING, 'Contents of the file do not match with template of lines on line "' . $i . '"', __FILE__, __LINE__); 250 continue; 251 } 252 253 // Checking for valid entries on this line 254 for ($j = 0; $j < 8; $j++) { 255 try { 256 switch ($this->fileContent[$j]) { 257 case 'string': 258 $lineContent[$j] = (string) $lineContent[$j]; 259 break; 260 case 'int': 261 $lineContent[$j] = (int) $lineContent[$j]; 262 break; 263 case 'double': 264 $lineContent[$j] = (double) $lineContent[$j]; 265 break; 266 default: 267 break; 268 } 269 } catch (Exception $err) { 270 trigger_log(SYSLOG_WARNING, 'The value "' . $j . '" on line "' . $i . '" is not valid, skipping to next line', __FILE__, __LINE__); 271 continue; 272 } 273 } 274 275 // Creating a string with the complete description of the node using all data in the status file 276 $descriptionStatus = 'Type: ' . $lineContent[0] . '<br/>Host name: ' . $lineContent[1] . '<br/>Has been checked: ' . $lineContent[2] . '<br/>Check execution time: ' . $lineContent[3] . '<br/>Currenr state: ' . $lineContent[4] . '<br/>Last hard state: ' . $lineContent[5] . '<br/>Last check: ' . $lineContent[6] . '<br/>Problem has been acknowledged: ' . $lineContent[7] . '<br/><br/>'; 277 278 if ($placemarkPosition = $this->getPlacemarkByName($lineContent[1])) { 279 // Updating an excisting placemark 205 280 $this->KMLPlacemarks[$placemarkPosition]->setDescriptionStatus($descriptionStatus); 206 281 } else { 282 // Adding a new placemark 207 283 $placemark = new KMLPlacemark(); 208 $placemark->setID($ host_name);209 $placemark->setName($ host_name);284 $placemark->setID($lineContent[1]); 285 $placemark->setName($lineContent[1]); 210 286 $placemark->setDescriptionStatus($descriptionStatus); 211 287 $this->addPlacemark($placemark); -
trunk/src/inc/KMLPlacemark.class.php
r7692 r7720 5 5 * Purpose: Placemark used in KMLFile 6 6 */ 7 8 define('PLACEMARK_GREEN', 'greenArrowIcon'); 9 define('PLACEMARK_ORANGE', 'orangeArrowIcon'); 10 define('PLACEMARK_RED', 'redArrowIcon'); 7 11 8 12 class KMLPlacemark { … … 41 45 /* 42 46 * Function: __construct (constructor) 47 * Description: Creating a new KMLFile 43 48 * Parameters: - 44 * Function: Creating a new KMLFile49 * Returns: - 45 50 */ 46 51 function __construct() { … … 54 59 } 55 60 61 /* 62 * Function: setID 63 * Description: Setting the ID of the placemark 64 * Parameters: string $newID 65 * Returns: - 66 */ 56 67 function setID($newID) { 57 68 $this->id = $newID; 58 69 } 59 70 71 /* 72 * Function: setName 73 * Description: Setting the name of the placemark 74 * Parameters: string $newName 75 * Returns: - 76 */ 60 77 function setName($newName) { 61 78 $this->name = $newName; 62 79 } 63 80 81 /* 82 * Function: getName 83 * Description: Getting the name of the placemark 84 * Parameters: - 85 * Returns: The name of the placemark 86 */ 64 87 function getName() { 65 88 return $this->name; 66 89 } 67 90 91 /* 92 * Function: setDescriptionLocation 93 * Description: Setting the location description of the placemark 94 * Parameters: string $newDescriptionLocation 95 * Returns: - 96 */ 68 97 function setDescriptionLocation($newDescriptionLocation) { 69 98 $this->descriptionLocation = (string) $newDescriptionLocation; 70 99 } 71 100 101 /* 102 * Function: setDescriptionStatus 103 * Description: Setting the status description of the placemark 104 * Parameters: string $newDescriptionStatus 105 * Returns: - 106 */ 72 107 function setDescriptionStatus($newDescriptionStatus) { 73 108 $this->descriptionStatus = (string) $newDescriptionStatus; 74 109 } 75 110 111 /* 112 * Function: setLongitude 113 * Description: Setting the longitude of the placemark 114 * Parameters: string $newLongitude 115 * Returns: - 116 */ 76 117 function setLongitude($newLongitude) { 77 118 $this->longitude = (double) $newLongitude; 78 119 } 79 120 121 /* 122 * Function: setLatitude 123 * Description: Setting the latitude of the placemark 124 * Parameters: string $newLatitude 125 * Returns: - 126 */ 80 127 function setLatitude($newLatitude) { 81 128 $this->latitude = (double) $newLatitude; 82 129 } 83 130 131 /* 132 * Function: setStyle 133 * Description: Setting the style of the placemark 134 * Parameters: string $newStyle 135 * Returns: - 136 */ 84 137 function setStyle($newStyle) { 85 138 $this->style = (string) $newStyle; 86 139 } 87 140 141 /* 142 * Function: toString 143 * Description: Converts the content of this placemark to a KML valid string 144 * Parameters: - 145 * Returns: KML valid string 146 */ 88 147 function toString() { 89 148 $toString = $this->template; -
trunk/src/inc/LogHandler.class.php
r7661 r7720 88 88 } 89 89 if ($logno <= LOG_LEVEL_WRITE) { 90 // TODO: Write $errorString to file90 // TODO: David: Write $errorString to file 91 91 } 92 92 if ($logno <= LOG_LEVEL_MAIL) { 93 // TODO: Mail $errorString to administrator93 // TODO: David: Mail $errorString to administrator 94 94 } 95 95 } -
trunk/src/index.php
r7702 r7720 42 42 43 43 // Let's try to read the node location file 44 $nodeLocation = new FileHandler($config['node_location_file'] );45 $kmlFile->parseLocationFile($nodeLocation-> getFile());44 $nodeLocation = new FileHandler($config['node_location_file'], 'r'); 45 $kmlFile->parseLocationFile($nodeLocation->read()); 46 46 /* 47 47 * For testing, echo the example KML file … … 51 51 52 52 // Let's try to read the node status file 53 $nodeStatus = new FileHandler($config['node_status_file'] );54 $kmlFile->parseStatusFile($nodeStatus-> getFile());53 $nodeStatus = new FileHandler($config['node_status_file'], 'r'); 54 $kmlFile->parseStatusFile($nodeStatus->read()); 55 55 56 56 // And echo the result to the screen
Note:
See TracChangeset
for help on using the changeset viewer.