Changeset 7720 for trunk/src/inc/KMLFile.class.php
- Timestamp:
- Apr 12, 2010, 5:56:17 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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);
Note:
See TracChangeset
for help on using the changeset viewer.