@@ -88,24 +88,24 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
| 90 | 90 | { |
| 91 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 92 | - { |
|
| 93 | - if ($message === null) |
|
| 94 | - { |
|
| 95 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 96 | - } |
|
| 97 | - $this->trapLog($message,$log_level,'syslog'); |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - else |
|
| 101 | - { |
|
| 102 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 91 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 92 | + { |
|
| 93 | + if ($message === null) |
|
| 94 | + { |
|
| 95 | + $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 96 | + } |
|
| 97 | + $this->trapLog($message,$log_level,'syslog'); |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + else |
|
| 101 | + { |
|
| 102 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** Get options from ini file and database |
| 108 | - */ |
|
| 108 | + */ |
|
| 109 | 109 | protected function getOptions() |
| 110 | 110 | { |
| 111 | 111 | $trap_config=parse_ini_file($this->trap_module_config,true); |
@@ -128,10 +128,10 @@ discard block |
||
| 128 | 128 | // API options |
| 129 | 129 | if ($this->getOptionIfSet($trap_config,'config','icingaAPI_host', $this->api_hostname)) |
| 130 | 130 | { |
| 131 | - $this->api_use=true; |
|
| 132 | - $this->getOptionIfSet($trap_config,'config','icingaAPI_port', $this->api_port); |
|
| 133 | - $this->getOptionIfSet($trap_config,'config','icingaAPI_user', $this->api_username); |
|
| 134 | - $this->getOptionIfSet($trap_config,'config','icingaAPI_password', $this->api_password); |
|
| 131 | + $this->api_use=true; |
|
| 132 | + $this->getOptionIfSet($trap_config,'config','icingaAPI_port', $this->api_port); |
|
| 133 | + $this->getOptionIfSet($trap_config,'config','icingaAPI_user', $this->api_username); |
|
| 134 | + $this->getOptionIfSet($trap_config,'config','icingaAPI_password', $this->api_password); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | /***** Database options : ***/ |
@@ -148,9 +148,9 @@ discard block |
||
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | /** Get data from db_config |
| 151 | - * @param $element string name of param |
|
| 152 | - * @return mixed : value (or null) |
|
| 153 | - */ |
|
| 151 | + * @param $element string name of param |
|
| 152 | + * @return mixed : value (or null) |
|
| 153 | + */ |
|
| 154 | 154 | protected function getDBConfig($element) |
| 155 | 155 | { |
| 156 | 156 | $db_conn=$this->db_connect_trap(); |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** Send log. Throws exception on critical error |
| 171 | - * @param string $message Message to log |
|
| 172 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 173 | - * @param string $destination file/syslog/display |
|
| 174 | - * @return void |
|
| 175 | - **/ |
|
| 171 | + * @param string $message Message to log |
|
| 172 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 173 | + * @param string $destination file/syslog/display |
|
| 174 | + * @return void |
|
| 175 | + **/ |
|
| 176 | 176 | public function trapLog( $message, $level, $destination ='') |
| 177 | 177 | { |
| 178 | 178 | if ($this->debug_level >= $level) |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | switch ($output_type) |
| 218 | 218 | { |
| 219 | 219 | case 'file': |
| 220 | - if ($output_option == null) throw new Exception("File logging without file !"); |
|
| 220 | + if ($output_option == null) throw new Exception("File logging without file !"); |
|
| 221 | 221 | $this->debug_file=$output_option; |
| 222 | 222 | $this->alert_output='file'; |
| 223 | 223 | break; |
@@ -234,30 +234,30 @@ discard block |
||
| 234 | 234 | |
| 235 | 235 | protected function getAPI() |
| 236 | 236 | { |
| 237 | - if ($this->icinga2api == null) |
|
| 238 | - { |
|
| 239 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 240 | - } |
|
| 241 | - return $this->icinga2api; |
|
| 237 | + if ($this->icinga2api == null) |
|
| 238 | + { |
|
| 239 | + $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 240 | + } |
|
| 241 | + return $this->icinga2api; |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** Connects to trapdb |
| 245 | - * @return mixed : PDO connection or null |
|
| 246 | - */ |
|
| 245 | + * @return mixed : PDO connection or null |
|
| 246 | + */ |
|
| 247 | 247 | public function db_connect_trap() |
| 248 | 248 | { |
| 249 | 249 | if ($this->trapDB != null) { |
| 250 | - // Check if connection is still alive |
|
| 251 | - try { |
|
| 252 | - $this->trapDB->query('select 1')->fetchColumn(); |
|
| 253 | - return $this->trapDB; |
|
| 254 | - } catch (Exception $e) { |
|
| 255 | - // select 1 failed, try to reconnect. |
|
| 256 | - $this->trapDB=null; |
|
| 250 | + // Check if connection is still alive |
|
| 251 | + try { |
|
| 252 | + $this->trapDB->query('select 1')->fetchColumn(); |
|
| 253 | + return $this->trapDB; |
|
| 254 | + } catch (Exception $e) { |
|
| 255 | + // select 1 failed, try to reconnect. |
|
| 256 | + $this->trapDB=null; |
|
| 257 | 257 | $this->trapDB=$this->db_connect('traps'); |
| 258 | - $this->trapLog('Database connection lost, reconnecting',WARN,''); |
|
| 258 | + $this->trapLog('Database connection lost, reconnecting',WARN,''); |
|
| 259 | 259 | return $this->trapDB; |
| 260 | - } |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | 262 | } |
| 263 | 263 | $this->trapDB=$this->db_connect('traps'); |
@@ -266,8 +266,8 @@ discard block |
||
| 266 | 266 | |
| 267 | 267 | |
| 268 | 268 | /** Connects to idodb |
| 269 | - * @return PDO connection |
|
| 270 | - */ |
|
| 269 | + * @return PDO connection |
|
| 270 | + */ |
|
| 271 | 271 | public function db_connect_ido() |
| 272 | 272 | { |
| 273 | 273 | if ($this->idoDB != null) { return $this->idoDB; } |
@@ -276,9 +276,9 @@ discard block |
||
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | /** connects to database named by parameter |
| 279 | - * @param string $database : 'traps' for traps database, 'ido' for ido database |
|
| 280 | - * @return PDO connection |
|
| 281 | - **/ |
|
| 279 | + * @param string $database : 'traps' for traps database, 'ido' for ido database |
|
| 280 | + * @return PDO connection |
|
| 281 | + **/ |
|
| 282 | 282 | protected function db_connect($database) { |
| 283 | 283 | $confarray=$this->get_database($database); |
| 284 | 284 | // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
@@ -295,9 +295,9 @@ discard block |
||
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** Get database connexion options |
| 298 | - * @param string $database : 'traps' for traps database, 'ido' for ido database |
|
| 299 | - * @return mixed : null or array( DB type (mysql, pgsql.) , db_host, database name , db_user, db_pass) |
|
| 300 | - **/ |
|
| 298 | + * @param string $database : 'traps' for traps database, 'ido' for ido database |
|
| 299 | + * @return mixed : null or array( DB type (mysql, pgsql.) , db_host, database name , db_user, db_pass) |
|
| 300 | + **/ |
|
| 301 | 301 | protected function get_database($database) { |
| 302 | 302 | |
| 303 | 303 | $trap_config=parse_ini_file($this->trap_module_config,true); |
@@ -349,9 +349,9 @@ discard block |
||
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | /** read data from stream |
| 352 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 353 | - * @return mixed array trap data or exception with error |
|
| 354 | - */ |
|
| 352 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 353 | + * @return mixed array trap data or exception with error |
|
| 354 | + */ |
|
| 355 | 355 | public function read_trap($stream='php://stdin') |
| 356 | 356 | { |
| 357 | 357 | //Read data from snmptrapd from stdin |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | |
| 360 | 360 | if ($input_stream === false) |
| 361 | 361 | { |
| 362 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 362 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 363 | 363 | $this->trapLog("Error reading stdin !",ERROR,''); |
| 364 | 364 | return null; // note : exception thrown by traplog |
| 365 | 365 | } |
@@ -368,21 +368,21 @@ discard block |
||
| 368 | 368 | $this->receivingHost=chop(fgets($input_stream)); |
| 369 | 369 | if ($this->receivingHost === false) |
| 370 | 370 | { |
| 371 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 371 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 372 | 372 | $this->trapLog("Error reading Host !",ERROR,''); |
| 373 | 373 | } |
| 374 | 374 | // line 2 IP:port=>IP:port |
| 375 | 375 | $IP=chop(fgets($input_stream)); |
| 376 | 376 | if ($IP === false) |
| 377 | 377 | { |
| 378 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 378 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 379 | 379 | $this->trapLog("Error reading IP !",ERROR,''); |
| 380 | 380 | } |
| 381 | 381 | $matches=array(); |
| 382 | 382 | $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
| 383 | 383 | if ($ret_code===0 || $ret_code===false) |
| 384 | 384 | { |
| 385 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 385 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 386 | 386 | $this->trapLog('Error parsing IP : '.$IP,ERROR,''); |
| 387 | 387 | } |
| 388 | 388 | else |
@@ -403,7 +403,7 @@ discard block |
||
| 403 | 403 | } |
| 404 | 404 | else |
| 405 | 405 | { |
| 406 | - if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
| 406 | + if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
| 407 | 407 | { |
| 408 | 408 | $this->trap_data['trap_oid']=$matches[2]; |
| 409 | 409 | } |
@@ -419,7 +419,7 @@ discard block |
||
| 419 | 419 | |
| 420 | 420 | if ($this->trap_data['trap_oid']=='unknown') |
| 421 | 421 | { |
| 422 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 422 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 423 | 423 | $this->trapLog('no trap oid found',ERROR,''); |
| 424 | 424 | } |
| 425 | 425 | |
@@ -448,9 +448,9 @@ discard block |
||
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | /** Translate oid into array(MIB,Name) |
| 451 | - * @param $oid string oid to translate |
|
| 452 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 453 | - */ |
|
| 451 | + * @param $oid string oid to translate |
|
| 452 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 453 | + */ |
|
| 454 | 454 | public function translateOID($oid) |
| 455 | 455 | { |
| 456 | 456 | // try from database |
@@ -483,7 +483,7 @@ discard block |
||
| 483 | 483 | |
| 484 | 484 | // Try to get oid name from snmptranslate |
| 485 | 485 | $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
| 486 | - ' '.$oid); |
|
| 486 | + ' '.$oid); |
|
| 487 | 487 | $matches=array(); |
| 488 | 488 | $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
| 489 | 489 | if ($ret_code===0 || $ret_code ==== false) { |
@@ -495,9 +495,9 @@ discard block |
||
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | /** Erase old trap records |
| 498 | - * @param integer $days : erase traps when more than $days old |
|
| 499 | - * @return integer : number of lines deleted |
|
| 500 | - **/ |
|
| 498 | + * @param integer $days : erase traps when more than $days old |
|
| 499 | + * @return integer : number of lines deleted |
|
| 500 | + **/ |
|
| 501 | 501 | public function eraseOldTraps($days=0) |
| 502 | 502 | { |
| 503 | 503 | if ($days==0) |
@@ -522,72 +522,72 @@ discard block |
||
| 522 | 522 | public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
| 523 | 523 | { |
| 524 | 524 | |
| 525 | - $db_conn=$this->db_connect_trap(); |
|
| 525 | + $db_conn=$this->db_connect_trap(); |
|
| 526 | 526 | |
| 527 | - // add date time |
|
| 528 | - $insert_col ='date_received,status'; |
|
| 529 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 527 | + // add date time |
|
| 528 | + $insert_col ='date_received,status'; |
|
| 529 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 530 | 530 | |
| 531 | - if ($sourceIP !=null) |
|
| 532 | - { |
|
| 533 | - $insert_col .=',source_ip'; |
|
| 534 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 535 | - } |
|
| 536 | - if ($trapoid !=null) |
|
| 537 | - { |
|
| 538 | - $insert_col .=',trap_oid'; |
|
| 539 | - $insert_val .=",'". $trapoid ."'"; |
|
| 540 | - } |
|
| 541 | - $insert_col .=',status_detail'; |
|
| 542 | - $insert_val .=",'". $message ."'"; |
|
| 531 | + if ($sourceIP !=null) |
|
| 532 | + { |
|
| 533 | + $insert_col .=',source_ip'; |
|
| 534 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 535 | + } |
|
| 536 | + if ($trapoid !=null) |
|
| 537 | + { |
|
| 538 | + $insert_col .=',trap_oid'; |
|
| 539 | + $insert_val .=",'". $trapoid ."'"; |
|
| 540 | + } |
|
| 541 | + $insert_col .=',status_detail'; |
|
| 542 | + $insert_val .=",'". $message ."'"; |
|
| 543 | 543 | |
| 544 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 544 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 545 | 545 | |
| 546 | - switch ($this->trapDBType) |
|
| 547 | - { |
|
| 548 | - case 'pgsql': |
|
| 549 | - $sql .= ' RETURNING id;'; |
|
| 550 | - $this->trapLog('sql : '.$sql,INFO); |
|
| 551 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 552 | - $this->trapLog('Error SQL insert : '.$sql,1,''); |
|
| 553 | - } |
|
| 554 | - $this->trapLog('SQL insertion OK',INFO ); |
|
| 555 | - // Get last id to insert oid/values in secondary table |
|
| 556 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 546 | + switch ($this->trapDBType) |
|
| 547 | + { |
|
| 548 | + case 'pgsql': |
|
| 549 | + $sql .= ' RETURNING id;'; |
|
| 550 | + $this->trapLog('sql : '.$sql,INFO); |
|
| 551 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 552 | + $this->trapLog('Error SQL insert : '.$sql,1,''); |
|
| 553 | + } |
|
| 554 | + $this->trapLog('SQL insertion OK',INFO ); |
|
| 555 | + // Get last id to insert oid/values in secondary table |
|
| 556 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 557 | 557 | |
| 558 | - $this->trapLog('Erreur recuperation id',1,''); |
|
| 559 | - } |
|
| 560 | - if (! isset($inserted_id_ret['id'])) { |
|
| 561 | - $this->trapLog('Error getting id',1,''); |
|
| 562 | - } |
|
| 563 | - $this->trap_id=$inserted_id_ret['id']; |
|
| 564 | - break; |
|
| 565 | - case 'mysql': |
|
| 566 | - $sql .= ';'; |
|
| 567 | - $this->trapLog('sql : '.$sql,INFO ); |
|
| 568 | - if ($db_conn->query($sql) === false) { |
|
| 569 | - $this->trapLog('Error SQL insert : '.$sql,1,''); |
|
| 570 | - } |
|
| 571 | - $this->trapLog('SQL insertion OK',INFO ); |
|
| 572 | - // Get last id to insert oid/values in secondary table |
|
| 573 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 574 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 575 | - $this->trapLog('Erreur recuperation id',1,''); |
|
| 576 | - } |
|
| 558 | + $this->trapLog('Erreur recuperation id',1,''); |
|
| 559 | + } |
|
| 560 | + if (! isset($inserted_id_ret['id'])) { |
|
| 561 | + $this->trapLog('Error getting id',1,''); |
|
| 562 | + } |
|
| 563 | + $this->trap_id=$inserted_id_ret['id']; |
|
| 564 | + break; |
|
| 565 | + case 'mysql': |
|
| 566 | + $sql .= ';'; |
|
| 567 | + $this->trapLog('sql : '.$sql,INFO ); |
|
| 568 | + if ($db_conn->query($sql) === false) { |
|
| 569 | + $this->trapLog('Error SQL insert : '.$sql,1,''); |
|
| 570 | + } |
|
| 571 | + $this->trapLog('SQL insertion OK',INFO ); |
|
| 572 | + // Get last id to insert oid/values in secondary table |
|
| 573 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 574 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 575 | + $this->trapLog('Erreur recuperation id',1,''); |
|
| 576 | + } |
|
| 577 | 577 | |
| 578 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 579 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 580 | - $this->trap_id=$inserted_id; |
|
| 581 | - break; |
|
| 582 | - default: |
|
| 583 | - $this->trapLog('Error SQL type : '.$this->trapDBType,1,''); |
|
| 584 | - } |
|
| 578 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 579 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 580 | + $this->trap_id=$inserted_id; |
|
| 581 | + break; |
|
| 582 | + default: |
|
| 583 | + $this->trapLog('Error SQL type : '.$this->trapDBType,1,''); |
|
| 584 | + } |
|
| 585 | 585 | |
| 586 | - $this->trapLog('id found: '. $this->trap_id,INFO ); |
|
| 586 | + $this->trapLog('id found: '. $this->trap_id,INFO ); |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** Write trap data to trap database |
| 590 | - */ |
|
| 590 | + */ |
|
| 591 | 591 | public function writeTrapToDB() |
| 592 | 592 | { |
| 593 | 593 | |
@@ -685,10 +685,10 @@ discard block |
||
| 685 | 685 | } |
| 686 | 686 | |
| 687 | 687 | /** Get rules from rule database with ip and oid |
| 688 | - * @param $ip string ipv4 or ipv6 |
|
| 689 | - * @param $oid string oid in numeric |
|
| 690 | - * @return mixed : PDO object or false |
|
| 691 | - */ |
|
| 688 | + * @param $ip string ipv4 or ipv6 |
|
| 689 | + * @param $oid string oid in numeric |
|
| 690 | + * @return mixed : PDO object or false |
|
| 691 | + */ |
|
| 692 | 692 | protected function getRules($ip,$oid) |
| 693 | 693 | { |
| 694 | 694 | $db_conn=$this->db_connect_trap(); |
@@ -747,9 +747,9 @@ discard block |
||
| 747 | 747 | } |
| 748 | 748 | |
| 749 | 749 | /** Add rule match to rule |
| 750 | - * @param id int : rule id |
|
| 751 | - * @param set int : value to set |
|
| 752 | - */ |
|
| 750 | + * @param id int : rule id |
|
| 751 | + * @param set int : value to set |
|
| 752 | + */ |
|
| 753 | 753 | protected function add_rule_match($id, $set) |
| 754 | 754 | { |
| 755 | 755 | $db_conn=$this->db_connect_trap(); |
@@ -766,53 +766,53 @@ discard block |
||
| 766 | 766 | * @param integer $state numerical staus |
| 767 | 767 | * @param string $display |
| 768 | 768 | * @returnn bool true is service check was sent without error |
| 769 | - */ |
|
| 769 | + */ |
|
| 770 | 770 | public function serviceCheckResult($host,$service,$state,$display) |
| 771 | 771 | { |
| 772 | - if ($this->api_use === false) |
|
| 773 | - { |
|
| 774 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 775 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 776 | - $this->trapLog( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 772 | + if ($this->api_use === false) |
|
| 773 | + { |
|
| 774 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 775 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 776 | + $this->trapLog( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 777 | 777 | |
| 778 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 779 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 780 | - return true; |
|
| 781 | - } |
|
| 782 | - else |
|
| 783 | - { |
|
| 784 | - $api = $this->getAPI(); |
|
| 785 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 786 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
| 787 | - if ($retcode == false) |
|
| 788 | - { |
|
| 789 | - $this->trapLog( "Error sending result : " .$retmessage,WARN,''); |
|
| 790 | - return false; |
|
| 791 | - } |
|
| 792 | - else |
|
| 793 | - { |
|
| 794 | - $this->trapLog( "Sent result : " .$retmessage,INFO ); |
|
| 795 | - return true; |
|
| 796 | - } |
|
| 797 | - } |
|
| 778 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 779 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 780 | + return true; |
|
| 781 | + } |
|
| 782 | + else |
|
| 783 | + { |
|
| 784 | + $api = $this->getAPI(); |
|
| 785 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 786 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
| 787 | + if ($retcode == false) |
|
| 788 | + { |
|
| 789 | + $this->trapLog( "Error sending result : " .$retmessage,WARN,''); |
|
| 790 | + return false; |
|
| 791 | + } |
|
| 792 | + else |
|
| 793 | + { |
|
| 794 | + $this->trapLog( "Sent result : " .$retmessage,INFO ); |
|
| 795 | + return true; |
|
| 796 | + } |
|
| 797 | + } |
|
| 798 | 798 | } |
| 799 | 799 | |
| 800 | 800 | public function getHostByIP($ip) |
| 801 | 801 | { |
| 802 | - $api = $this->getAPI(); |
|
| 803 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 804 | - return $api->getHostByIP($ip); |
|
| 802 | + $api = $this->getAPI(); |
|
| 803 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 804 | + return $api->getHostByIP($ip); |
|
| 805 | 805 | } |
| 806 | 806 | |
| 807 | 807 | /** Resolve display. |
| 808 | - * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
| 809 | - * @param $display string |
|
| 810 | - * @return string display |
|
| 811 | - */ |
|
| 808 | + * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
| 809 | + * @param $display string |
|
| 810 | + * @return string display |
|
| 811 | + */ |
|
| 812 | 812 | protected function applyDisplay($display) |
| 813 | 813 | { |
| 814 | - $matches=array(); |
|
| 815 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 814 | + $matches=array(); |
|
| 815 | + while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 816 | 816 | { |
| 817 | 817 | $oid=$matches[1]; |
| 818 | 818 | $found=0; |
@@ -872,11 +872,11 @@ discard block |
||
| 872 | 872 | |
| 873 | 873 | if ($rule[$item] == '(') |
| 874 | 874 | { // grouping |
| 875 | - $item++; |
|
| 875 | + $item++; |
|
| 876 | 876 | $start=$item; |
| 877 | 877 | $parenthesis_count=0; |
| 878 | 878 | while (($item < strlen($rule)) // Not end of string AND |
| 879 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 879 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 880 | 880 | { |
| 881 | 881 | if ($rule[$item] == '"' ) |
| 882 | 882 | { // pass through string |
@@ -884,14 +884,14 @@ discard block |
||
| 884 | 884 | $item=$this->eval_getNext($rule,$item,'"'); |
| 885 | 885 | } |
| 886 | 886 | else{ |
| 887 | - if ($rule[$item] == '(') |
|
| 888 | - { |
|
| 889 | - $parenthesis_count++; |
|
| 890 | - } |
|
| 891 | - if ($rule[$item] == ')') |
|
| 892 | - { |
|
| 893 | - $parenthesis_count--; |
|
| 894 | - } |
|
| 887 | + if ($rule[$item] == '(') |
|
| 888 | + { |
|
| 889 | + $parenthesis_count++; |
|
| 890 | + } |
|
| 891 | + if ($rule[$item] == ')') |
|
| 892 | + { |
|
| 893 | + $parenthesis_count--; |
|
| 894 | + } |
|
| 895 | 895 | $item++; |
| 896 | 896 | } |
| 897 | 897 | } |
@@ -943,24 +943,24 @@ discard block |
||
| 943 | 943 | } |
| 944 | 944 | |
| 945 | 945 | /** Evaluation : makes token and evaluate. |
| 946 | - * Public function for expressions testing |
|
| 947 | - * accepts : < > = <= >= != (typec = 0) |
|
| 948 | - * operators : & | (typec=1) |
|
| 949 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 950 | - * comparison int vs strings will return null (error) |
|
| 951 | - * return : bool or null on error |
|
| 952 | - */ |
|
| 946 | + * Public function for expressions testing |
|
| 947 | + * accepts : < > = <= >= != (typec = 0) |
|
| 948 | + * operators : & | (typec=1) |
|
| 949 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 950 | + * comparison int vs strings will return null (error) |
|
| 951 | + * return : bool or null on error |
|
| 952 | + */ |
|
| 953 | 953 | public function evaluation($rule,&$item) |
| 954 | 954 | { |
| 955 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 955 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 956 | 956 | if ( $rule[$item] == '!') // If '!' found, negate next expression. |
| 957 | 957 | { |
| 958 | - $negate=true; |
|
| 959 | - $item++; |
|
| 958 | + $negate=true; |
|
| 959 | + $item++; |
|
| 960 | 960 | } |
| 961 | 961 | else |
| 962 | 962 | { |
| 963 | - $negate=false; |
|
| 963 | + $negate=false; |
|
| 964 | 964 | } |
| 965 | 965 | // First element : number, string or () |
| 966 | 966 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
@@ -980,24 +980,24 @@ discard block |
||
| 980 | 980 | // Third element : number, string or () |
| 981 | 981 | if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
| 982 | 982 | { |
| 983 | - $item++; |
|
| 984 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 985 | - $val2= ! $this->evaluation($rule,$item); |
|
| 986 | - $type2=2; // result is a boolean |
|
| 983 | + $item++; |
|
| 984 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 985 | + $val2= ! $this->evaluation($rule,$item); |
|
| 986 | + $type2=2; // result is a boolean |
|
| 987 | 987 | } |
| 988 | 988 | else |
| 989 | 989 | { |
| 990 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 990 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 991 | 991 | } |
| 992 | 992 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 993 | 993 | |
| 994 | 994 | if ($type1!=$type2) // cannot compare different types |
| 995 | 995 | { |
| 996 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
| 996 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
| 997 | 997 | } |
| 998 | 998 | if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
| 999 | 999 | { |
| 1000 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 1000 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | 1003 | switch ($comp){ |
@@ -1055,9 +1055,9 @@ discard block |
||
| 1055 | 1055 | } |
| 1056 | 1056 | |
| 1057 | 1057 | /** Evaluation rule (uses eval_* functions recursively) |
| 1058 | - * @param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
| 1059 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 1060 | - */ |
|
| 1058 | + * @param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
| 1059 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 1060 | + */ |
|
| 1061 | 1061 | |
| 1062 | 1062 | protected function eval_rule($rule) |
| 1063 | 1063 | { |
@@ -1102,7 +1102,7 @@ discard block |
||
| 1102 | 1102 | } |
| 1103 | 1103 | if ($found==0) |
| 1104 | 1104 | { // OID not found : throw error |
| 1105 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
| 1105 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
| 1106 | 1106 | } |
| 1107 | 1107 | } |
| 1108 | 1108 | $item=0; |
@@ -1113,7 +1113,7 @@ discard block |
||
| 1113 | 1113 | } |
| 1114 | 1114 | |
| 1115 | 1115 | /** Match rules for current trap and do action |
| 1116 | - */ |
|
| 1116 | + */ |
|
| 1117 | 1117 | public function applyRules() |
| 1118 | 1118 | { |
| 1119 | 1119 | $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
@@ -1150,12 +1150,12 @@ discard block |
||
| 1150 | 1150 | { |
| 1151 | 1151 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 1152 | 1152 | { |
| 1153 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1153 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1154 | 1154 | } |
| 1155 | 1155 | else |
| 1156 | 1156 | { |
| 1157 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1158 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1157 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1158 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1159 | 1159 | } |
| 1160 | 1160 | } |
| 1161 | 1161 | else |
@@ -1172,15 +1172,15 @@ discard block |
||
| 1172 | 1172 | $this->trapLog('action NOK : '.$action,INFO ); |
| 1173 | 1173 | if ($action >= 0) |
| 1174 | 1174 | { |
| 1175 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 1176 | - { |
|
| 1177 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1178 | - } |
|
| 1179 | - else |
|
| 1180 | - { |
|
| 1181 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1182 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1183 | - } |
|
| 1175 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 1176 | + { |
|
| 1177 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1178 | + } |
|
| 1179 | + else |
|
| 1180 | + { |
|
| 1181 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1182 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1183 | + } |
|
| 1184 | 1184 | } |
| 1185 | 1185 | else |
| 1186 | 1186 | { |
@@ -1203,9 +1203,9 @@ discard block |
||
| 1203 | 1203 | } |
| 1204 | 1204 | catch (Exception $e) |
| 1205 | 1205 | { |
| 1206 | - $this->trapLog('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 1207 | - $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 1208 | - $this->trap_data['status']='error'; |
|
| 1206 | + $this->trapLog('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 1207 | + $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 1208 | + $this->trap_data['status']='error'; |
|
| 1209 | 1209 | } |
| 1210 | 1210 | |
| 1211 | 1211 | } |
@@ -1220,8 +1220,8 @@ discard block |
||
| 1220 | 1220 | } |
| 1221 | 1221 | |
| 1222 | 1222 | /** Add Time a action to rule |
| 1223 | - * @param string $time : time to process to insert in SQL |
|
| 1224 | - */ |
|
| 1223 | + * @param string $time : time to process to insert in SQL |
|
| 1224 | + */ |
|
| 1225 | 1225 | public function add_rule_final($time) |
| 1226 | 1226 | { |
| 1227 | 1227 | $db_conn=$this->db_connect_trap(); |
@@ -1238,9 +1238,9 @@ discard block |
||
| 1238 | 1238 | /*********** UTILITIES *********************/ |
| 1239 | 1239 | |
| 1240 | 1240 | /** Create database schema |
| 1241 | - * @param $schema_file string File to read schema from |
|
| 1242 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 1243 | - */ |
|
| 1241 | + * @param $schema_file string File to read schema from |
|
| 1242 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 1243 | + */ |
|
| 1244 | 1244 | public function create_schema($schema_file,$table_prefix) |
| 1245 | 1245 | { |
| 1246 | 1246 | //Read data from snmptrapd from stdin |
@@ -1260,30 +1260,30 @@ discard block |
||
| 1260 | 1260 | { |
| 1261 | 1261 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
| 1262 | 1262 | if (preg_match('/; *$/', $newline)) |
| 1263 | - { |
|
| 1264 | - $sql= $newline; |
|
| 1265 | - if ($db_conn->query($sql) === false) { |
|
| 1266 | - $this->trapLog('Error create schema : '.$sql,ERROR,''); |
|
| 1267 | - } |
|
| 1268 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 1269 | - { |
|
| 1270 | - $cur_table='table '.$cur_table_array[1]; |
|
| 1271 | - } |
|
| 1272 | - else |
|
| 1273 | - { |
|
| 1274 | - $cur_table='secret SQL stuff :-)'; |
|
| 1275 | - } |
|
| 1276 | - $this->trapLog('Creating : ' . $cur_table,INFO ); |
|
| 1277 | - $newline=''; |
|
| 1278 | - } |
|
| 1263 | + { |
|
| 1264 | + $sql= $newline; |
|
| 1265 | + if ($db_conn->query($sql) === false) { |
|
| 1266 | + $this->trapLog('Error create schema : '.$sql,ERROR,''); |
|
| 1267 | + } |
|
| 1268 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 1269 | + { |
|
| 1270 | + $cur_table='table '.$cur_table_array[1]; |
|
| 1271 | + } |
|
| 1272 | + else |
|
| 1273 | + { |
|
| 1274 | + $cur_table='secret SQL stuff :-)'; |
|
| 1275 | + } |
|
| 1276 | + $this->trapLog('Creating : ' . $cur_table,INFO ); |
|
| 1277 | + $newline=''; |
|
| 1278 | + } |
|
| 1279 | 1279 | } |
| 1280 | 1280 | |
| 1281 | 1281 | $sql= $newline; |
| 1282 | 1282 | if ($sql != '') |
| 1283 | 1283 | { |
| 1284 | - if ($db_conn->query($sql) === false) { |
|
| 1285 | - $this->trapLog('Error create schema : '.$sql,ERROR,''); |
|
| 1286 | - } |
|
| 1284 | + if ($db_conn->query($sql) === false) { |
|
| 1285 | + $this->trapLog('Error create schema : '.$sql,ERROR,''); |
|
| 1286 | + } |
|
| 1287 | 1287 | } |
| 1288 | 1288 | $this->trapLog('Schema created',INFO); |
| 1289 | 1289 | } |
@@ -1298,113 +1298,113 @@ discard block |
||
| 1298 | 1298 | */ |
| 1299 | 1299 | public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
| 1300 | 1300 | { |
| 1301 | - // Get current db number |
|
| 1302 | - $db_conn=$this->db_connect_trap(); |
|
| 1303 | - $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' '; |
|
| 1304 | - $this->trapLog('SQL query : '.$sql,DEBUG ); |
|
| 1305 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1306 | - $this->trapLog('Cannot get db version. Query : ' . $sql,2,''); |
|
| 1307 | - return; |
|
| 1308 | - } |
|
| 1309 | - $version=$ret_code->fetchAll(); |
|
| 1310 | - $cur_version=$version[0]['value']; |
|
| 1311 | - $db_version_id=$version[0]['id']; |
|
| 1301 | + // Get current db number |
|
| 1302 | + $db_conn=$this->db_connect_trap(); |
|
| 1303 | + $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' '; |
|
| 1304 | + $this->trapLog('SQL query : '.$sql,DEBUG ); |
|
| 1305 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1306 | + $this->trapLog('Cannot get db version. Query : ' . $sql,2,''); |
|
| 1307 | + return; |
|
| 1308 | + } |
|
| 1309 | + $version=$ret_code->fetchAll(); |
|
| 1310 | + $cur_version=$version[0]['value']; |
|
| 1311 | + $db_version_id=$version[0]['id']; |
|
| 1312 | 1312 | |
| 1313 | - if ($this->trapDBType == 'pgsql') |
|
| 1314 | - { |
|
| 1315 | - $prefix .= 'update_pgsql/schema_'; |
|
| 1316 | - } |
|
| 1317 | - else |
|
| 1318 | - { |
|
| 1319 | - $prefix .= 'update_sql/schema_'; |
|
| 1320 | - } |
|
| 1321 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 1322 | - if ($getmsg === true) |
|
| 1323 | - { |
|
| 1324 | - $message=''; |
|
| 1325 | - $this->trapLog('getting message for upgrade',DEBUG ); |
|
| 1326 | - while($cur_version<$target_version) |
|
| 1327 | - { |
|
| 1328 | - $cur_version++; |
|
| 1329 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 1330 | - $input_stream=fopen($updateFile, 'r'); |
|
| 1331 | - if ($input_stream=== false) |
|
| 1332 | - { |
|
| 1333 | - $this->trapLog("Error reading update file ". $updateFile,2,''); |
|
| 1334 | - return; |
|
| 1335 | - } |
|
| 1336 | - do { $line=fgets($input_stream); } |
|
| 1337 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 1338 | - if ($line === false) |
|
| 1339 | - { |
|
| 1340 | - $this->trapLog("No message in file ". $updateFile,2,''); |
|
| 1341 | - return; |
|
| 1342 | - } |
|
| 1343 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 1344 | - } |
|
| 1345 | - return $message; |
|
| 1346 | - } |
|
| 1347 | - while($cur_version<$target_version) |
|
| 1348 | - { // tODO : execute pre & post scripts |
|
| 1349 | - $cur_version++; |
|
| 1350 | - $this->trapLog('Updating to version : ' .$cur_version ,INFO ); |
|
| 1351 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 1352 | - $input_stream=fopen($updateFile, 'r'); |
|
| 1353 | - if ($input_stream=== false) |
|
| 1354 | - { |
|
| 1355 | - $this->trapLog("Error reading update file ". $updateFile,2,''); |
|
| 1356 | - return; |
|
| 1357 | - } |
|
| 1358 | - $newline=''; |
|
| 1359 | - $db_conn=$this->db_connect_trap(); |
|
| 1360 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 1361 | - while (($line=fgets($input_stream)) !== false) |
|
| 1362 | - { |
|
| 1363 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 1364 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 1365 | - if (preg_match('/; *$/', $newline)) |
|
| 1366 | - { |
|
| 1367 | - $sql_req=$db_conn->prepare($newline); |
|
| 1368 | - if ($sql_req->execute() === false) { |
|
| 1369 | - $this->trapLog('Error create schema : '.$newline,1,''); |
|
| 1370 | - } |
|
| 1371 | - $cur_table_array=array(); |
|
| 1372 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 1373 | - { |
|
| 1374 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 1375 | - } |
|
| 1376 | - else |
|
| 1377 | - { |
|
| 1378 | - $cur_table='secret SQL stuff :-)'; |
|
| 1379 | - //$cur_table=$newline; |
|
| 1380 | - } |
|
| 1381 | - $this->trapLog('Doing : ' . $cur_table,INFO ); |
|
| 1313 | + if ($this->trapDBType == 'pgsql') |
|
| 1314 | + { |
|
| 1315 | + $prefix .= 'update_pgsql/schema_'; |
|
| 1316 | + } |
|
| 1317 | + else |
|
| 1318 | + { |
|
| 1319 | + $prefix .= 'update_sql/schema_'; |
|
| 1320 | + } |
|
| 1321 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 1322 | + if ($getmsg === true) |
|
| 1323 | + { |
|
| 1324 | + $message=''; |
|
| 1325 | + $this->trapLog('getting message for upgrade',DEBUG ); |
|
| 1326 | + while($cur_version<$target_version) |
|
| 1327 | + { |
|
| 1328 | + $cur_version++; |
|
| 1329 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 1330 | + $input_stream=fopen($updateFile, 'r'); |
|
| 1331 | + if ($input_stream=== false) |
|
| 1332 | + { |
|
| 1333 | + $this->trapLog("Error reading update file ". $updateFile,2,''); |
|
| 1334 | + return; |
|
| 1335 | + } |
|
| 1336 | + do { $line=fgets($input_stream); } |
|
| 1337 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 1338 | + if ($line === false) |
|
| 1339 | + { |
|
| 1340 | + $this->trapLog("No message in file ". $updateFile,2,''); |
|
| 1341 | + return; |
|
| 1342 | + } |
|
| 1343 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 1344 | + } |
|
| 1345 | + return $message; |
|
| 1346 | + } |
|
| 1347 | + while($cur_version<$target_version) |
|
| 1348 | + { // tODO : execute pre & post scripts |
|
| 1349 | + $cur_version++; |
|
| 1350 | + $this->trapLog('Updating to version : ' .$cur_version ,INFO ); |
|
| 1351 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 1352 | + $input_stream=fopen($updateFile, 'r'); |
|
| 1353 | + if ($input_stream=== false) |
|
| 1354 | + { |
|
| 1355 | + $this->trapLog("Error reading update file ". $updateFile,2,''); |
|
| 1356 | + return; |
|
| 1357 | + } |
|
| 1358 | + $newline=''; |
|
| 1359 | + $db_conn=$this->db_connect_trap(); |
|
| 1360 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 1361 | + while (($line=fgets($input_stream)) !== false) |
|
| 1362 | + { |
|
| 1363 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 1364 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 1365 | + if (preg_match('/; *$/', $newline)) |
|
| 1366 | + { |
|
| 1367 | + $sql_req=$db_conn->prepare($newline); |
|
| 1368 | + if ($sql_req->execute() === false) { |
|
| 1369 | + $this->trapLog('Error create schema : '.$newline,1,''); |
|
| 1370 | + } |
|
| 1371 | + $cur_table_array=array(); |
|
| 1372 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 1373 | + { |
|
| 1374 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 1375 | + } |
|
| 1376 | + else |
|
| 1377 | + { |
|
| 1378 | + $cur_table='secret SQL stuff :-)'; |
|
| 1379 | + //$cur_table=$newline; |
|
| 1380 | + } |
|
| 1381 | + $this->trapLog('Doing : ' . $cur_table,INFO ); |
|
| 1382 | 1382 | |
| 1383 | - $newline=''; |
|
| 1384 | - } |
|
| 1385 | - } |
|
| 1386 | - fclose($input_stream); |
|
| 1383 | + $newline=''; |
|
| 1384 | + } |
|
| 1385 | + } |
|
| 1386 | + fclose($input_stream); |
|
| 1387 | 1387 | |
| 1388 | - //$sql= $newline; |
|
| 1389 | - //if ($db_conn->query($sql) === false) { |
|
| 1390 | - // $this->trapLog('Error updating schema : '.$sql,1,''); |
|
| 1391 | - //} |
|
| 1388 | + //$sql= $newline; |
|
| 1389 | + //if ($db_conn->query($sql) === false) { |
|
| 1390 | + // $this->trapLog('Error updating schema : '.$sql,1,''); |
|
| 1391 | + //} |
|
| 1392 | 1392 | |
| 1393 | - $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
|
| 1394 | - $this->trapLog('SQL query : '.$sql,DEBUG ); |
|
| 1395 | - if (($db_conn->query($sql) === false) { |
|
| 1396 | - $this->trapLog('Cannot update db version. Query : ' . $sql,2,''); |
|
| 1397 | - return; |
|
| 1398 | - } |
|
| 1393 | + $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
|
| 1394 | + $this->trapLog('SQL query : '.$sql,DEBUG ); |
|
| 1395 | + if (($db_conn->query($sql) === false) { |
|
| 1396 | + $this->trapLog('Cannot update db version. Query : ' . $sql,2,''); |
|
| 1397 | + return; |
|
| 1398 | + } |
|
| 1399 | 1399 | |
| 1400 | - $this->trapLog('Schema updated to version : '.$cur_version ,INFO); |
|
| 1401 | - } |
|
| 1400 | + $this->trapLog('Schema updated to version : '.$cur_version ,INFO); |
|
| 1401 | + } |
|
| 1402 | 1402 | } |
| 1403 | 1403 | |
| 1404 | 1404 | /** reset service to OK after time defined in rule |
| 1405 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 1406 | - * @return : like a plugin : status code (0->3) <message> | <perfdata> |
|
| 1407 | - **/ |
|
| 1405 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 1406 | + * @return : like a plugin : status code (0->3) <message> | <perfdata> |
|
| 1407 | + **/ |
|
| 1408 | 1408 | public function reset_services() |
| 1409 | 1409 | { |
| 1410 | 1410 | // Get all services not in 'ok' state |
@@ -1474,105 +1474,105 @@ discard block |
||
| 1474 | 1474 | $description=$db_conn->quote($description); |
| 1475 | 1475 | if (isset($this->dbOidIndex[$oid])) |
| 1476 | 1476 | { |
| 1477 | - if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 1478 | - { // newly created. |
|
| 1479 | - return 0; |
|
| 1480 | - } |
|
| 1477 | + if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 1478 | + { // newly created. |
|
| 1479 | + return 0; |
|
| 1480 | + } |
|
| 1481 | 1481 | if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
| 1482 | - $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 1483 | - $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 1484 | - //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 1485 | - //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 1486 | - //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 1487 | - //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 1488 | - //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 1489 | - ) |
|
| 1482 | + $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 1483 | + $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 1484 | + //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 1485 | + //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 1486 | + //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 1487 | + //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 1488 | + //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 1489 | + ) |
|
| 1490 | 1490 | { // Do update |
| 1491 | - $sql='UPDATE '.$this->db_prefix.'mib_cache SET '. |
|
| 1492 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 1493 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 1494 | - ' WHERE id= :id'; |
|
| 1495 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 1491 | + $sql='UPDATE '.$this->db_prefix.'mib_cache SET '. |
|
| 1492 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 1493 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 1494 | + ' WHERE id= :id'; |
|
| 1495 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 1496 | 1496 | |
| 1497 | - $sqlParam=array( |
|
| 1498 | - ':name' => $name, |
|
| 1499 | - ':type' => $type, |
|
| 1500 | - ':mib' => $mib, |
|
| 1501 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1502 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1503 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1504 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1505 | - ':description' => ($description==null)?'null':$description, |
|
| 1506 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 1507 | - ); |
|
| 1497 | + $sqlParam=array( |
|
| 1498 | + ':name' => $name, |
|
| 1499 | + ':type' => $type, |
|
| 1500 | + ':mib' => $mib, |
|
| 1501 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1502 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1503 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1504 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1505 | + ':description' => ($description==null)?'null':$description, |
|
| 1506 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 1507 | + ); |
|
| 1508 | 1508 | |
| 1509 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1510 | - $this->trapLog('Error in query : ' . $sql,ERROR,''); |
|
| 1511 | - } |
|
| 1512 | - $this->trapLog('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1509 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1510 | + $this->trapLog('Error in query : ' . $sql,ERROR,''); |
|
| 1511 | + } |
|
| 1512 | + $this->trapLog('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1513 | 1513 | return 1; |
| 1514 | 1514 | } |
| 1515 | 1515 | else |
| 1516 | 1516 | { |
| 1517 | - $this->trapLog('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1518 | - return 0; |
|
| 1517 | + $this->trapLog('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1518 | + return 0; |
|
| 1519 | 1519 | } |
| 1520 | 1520 | } |
| 1521 | - // create new OID. |
|
| 1521 | + // create new OID. |
|
| 1522 | 1522 | |
| 1523 | 1523 | // Insert data |
| 1524 | 1524 | |
| 1525 | 1525 | $sql='INSERT INTO '.$this->db_prefix.'mib_cache '. |
| 1526 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 1527 | - ', syntax, type_enum , description ) ' . |
|
| 1528 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 1529 | - ', :syntax, :type_enum, :description )'; |
|
| 1526 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 1527 | + ', syntax, type_enum , description ) ' . |
|
| 1528 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 1529 | + ', :syntax, :type_enum, :description )'; |
|
| 1530 | 1530 | |
| 1531 | 1531 | if ($this->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
| 1532 | 1532 | |
| 1533 | 1533 | $sqlQuery=$db_conn->prepare($sql); |
| 1534 | 1534 | |
| 1535 | 1535 | $sqlParam=array( |
| 1536 | - ':oid' => $oid, |
|
| 1537 | - ':name' => $name, |
|
| 1538 | - ':type' => $type, |
|
| 1539 | - ':mib' => $mib, |
|
| 1540 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1541 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1542 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1543 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1544 | - ':description' => ($description==null)?'null':$description |
|
| 1536 | + ':oid' => $oid, |
|
| 1537 | + ':name' => $name, |
|
| 1538 | + ':type' => $type, |
|
| 1539 | + ':mib' => $mib, |
|
| 1540 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1541 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1542 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1543 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1544 | + ':description' => ($description==null)?'null':$description |
|
| 1545 | 1545 | ); |
| 1546 | 1546 | |
| 1547 | 1547 | if ($sqlQuery->execute($sqlParam) === false) { |
| 1548 | - $this->trapLog('Error in query : ' . $sql,1,''); |
|
| 1548 | + $this->trapLog('Error in query : ' . $sql,1,''); |
|
| 1549 | 1549 | } |
| 1550 | 1550 | |
| 1551 | 1551 | switch ($this->trapDBType) |
| 1552 | 1552 | { |
| 1553 | - case 'pgsql': |
|
| 1554 | - // Get last id to insert oid/values in secondary table |
|
| 1555 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 1556 | - $this->trapLog('Error getting id - pgsql - ',1,''); |
|
| 1557 | - } |
|
| 1558 | - if (! isset($inserted_id_ret['id'])) { |
|
| 1559 | - $this->trapLog('Error getting id - pgsql - empty.',1,''); |
|
| 1560 | - } |
|
| 1561 | - $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 1562 | - break; |
|
| 1563 | - case 'mysql': |
|
| 1564 | - // Get last id to insert oid/values in secondary table |
|
| 1565 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 1566 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1567 | - $this->trapLog('Erreur getting id - mysql - ',1,''); |
|
| 1568 | - } |
|
| 1553 | + case 'pgsql': |
|
| 1554 | + // Get last id to insert oid/values in secondary table |
|
| 1555 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 1556 | + $this->trapLog('Error getting id - pgsql - ',1,''); |
|
| 1557 | + } |
|
| 1558 | + if (! isset($inserted_id_ret['id'])) { |
|
| 1559 | + $this->trapLog('Error getting id - pgsql - empty.',1,''); |
|
| 1560 | + } |
|
| 1561 | + $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 1562 | + break; |
|
| 1563 | + case 'mysql': |
|
| 1564 | + // Get last id to insert oid/values in secondary table |
|
| 1565 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 1566 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1567 | + $this->trapLog('Erreur getting id - mysql - ',1,''); |
|
| 1568 | + } |
|
| 1569 | 1569 | |
| 1570 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 1571 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1572 | - $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 1573 | - break; |
|
| 1574 | - default: |
|
| 1575 | - $this->trapLog('Error SQL type : '.$this->trapDBType,1,''); |
|
| 1570 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 1571 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1572 | + $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 1573 | + break; |
|
| 1574 | + default: |
|
| 1575 | + $this->trapLog('Error SQL type : '.$this->trapDBType,1,''); |
|
| 1576 | 1576 | } |
| 1577 | 1577 | |
| 1578 | 1578 | // Set as newly created. |
@@ -1580,148 +1580,148 @@ discard block |
||
| 1580 | 1580 | return 2; |
| 1581 | 1581 | } |
| 1582 | 1582 | |
| 1583 | - /** |
|
| 1584 | - * create or update (with check_existing = true) objects of trap |
|
| 1585 | - * @param string $trapOID : trap oid |
|
| 1586 | - * @param string $trapmib : mib of trap |
|
| 1587 | - * @param array $objects : array of objects name (without MIB) |
|
| 1588 | - * @param bool $check_existing : check instead of create |
|
| 1589 | - */ |
|
| 1583 | + /** |
|
| 1584 | + * create or update (with check_existing = true) objects of trap |
|
| 1585 | + * @param string $trapOID : trap oid |
|
| 1586 | + * @param string $trapmib : mib of trap |
|
| 1587 | + * @param array $objects : array of objects name (without MIB) |
|
| 1588 | + * @param bool $check_existing : check instead of create |
|
| 1589 | + */ |
|
| 1590 | 1590 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
| 1591 | 1591 | { |
| 1592 | - $dbObjects=null; // cache of objects for trap in db |
|
| 1593 | - $db_conn=$this->db_connect_trap(); |
|
| 1592 | + $dbObjects=null; // cache of objects for trap in db |
|
| 1593 | + $db_conn=$this->db_connect_trap(); |
|
| 1594 | 1594 | |
| 1595 | - // Get id of trapmib. |
|
| 1595 | + // Get id of trapmib. |
|
| 1596 | 1596 | |
| 1597 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 1598 | - if ($check_existing === true) |
|
| 1599 | - { |
|
| 1600 | - // Get all objects |
|
| 1601 | - $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 1602 | - $this->trapLog('SQL query get all traps: '.$sql,DEBUG ); |
|
| 1603 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1604 | - $this->trapLog('No result in query : ' . $sql,1,''); |
|
| 1605 | - } |
|
| 1606 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 1597 | + $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 1598 | + if ($check_existing === true) |
|
| 1599 | + { |
|
| 1600 | + // Get all objects |
|
| 1601 | + $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 1602 | + $this->trapLog('SQL query get all traps: '.$sql,DEBUG ); |
|
| 1603 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1604 | + $this->trapLog('No result in query : ' . $sql,1,''); |
|
| 1605 | + } |
|
| 1606 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 1607 | 1607 | |
| 1608 | - foreach ($dbObjectsRaw as $val) |
|
| 1609 | - { |
|
| 1610 | - $dbObjects[$val['object_id']]=1; |
|
| 1611 | - } |
|
| 1612 | - } |
|
| 1613 | - foreach ($objects as $object) |
|
| 1614 | - { |
|
| 1615 | - $match=$snmptrans=array(); |
|
| 1616 | - $retVal=0; |
|
| 1617 | - $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 1618 | - $tmpdesc='';$indesc=false; |
|
| 1608 | + foreach ($dbObjectsRaw as $val) |
|
| 1609 | + { |
|
| 1610 | + $dbObjects[$val['object_id']]=1; |
|
| 1611 | + } |
|
| 1612 | + } |
|
| 1613 | + foreach ($objects as $object) |
|
| 1614 | + { |
|
| 1615 | + $match=$snmptrans=array(); |
|
| 1616 | + $retVal=0; |
|
| 1617 | + $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 1618 | + $tmpdesc='';$indesc=false; |
|
| 1619 | 1619 | |
| 1620 | - $objMib=$trapmib; |
|
| 1621 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1622 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1623 | - if ($retVal!=0) |
|
| 1624 | - { |
|
| 1625 | - // Maybe not trap mib, search with IR |
|
| 1626 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1627 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1628 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 1629 | - { // Not found -> continue with warning |
|
| 1630 | - $this->trapLog('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 1631 | - continue; |
|
| 1632 | - } |
|
| 1633 | - $objMib=$match[1]; |
|
| 1620 | + $objMib=$trapmib; |
|
| 1621 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1622 | + ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1623 | + if ($retVal!=0) |
|
| 1624 | + { |
|
| 1625 | + // Maybe not trap mib, search with IR |
|
| 1626 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1627 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1628 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 1629 | + { // Not found -> continue with warning |
|
| 1630 | + $this->trapLog('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 1631 | + continue; |
|
| 1632 | + } |
|
| 1633 | + $objMib=$match[1]; |
|
| 1634 | 1634 | |
| 1635 | - // Do the snmptranslate again. |
|
| 1636 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1637 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 1638 | - if ($retVal!=0) { |
|
| 1639 | - $this->trapLog('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 1640 | - } |
|
| 1635 | + // Do the snmptranslate again. |
|
| 1636 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1637 | + ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 1638 | + if ($retVal!=0) { |
|
| 1639 | + $this->trapLog('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 1640 | + } |
|
| 1641 | 1641 | |
| 1642 | - } |
|
| 1643 | - foreach ($snmptrans as $line) |
|
| 1644 | - { |
|
| 1645 | - if ($indesc===true) |
|
| 1646 | - { |
|
| 1647 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 1648 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 1649 | - { |
|
| 1650 | - $objDesc = $tmpdesc . $match[1]; |
|
| 1651 | - $indesc=false; |
|
| 1652 | - } |
|
| 1653 | - $tmpdesc.=$line; |
|
| 1654 | - continue; |
|
| 1655 | - } |
|
| 1656 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 1657 | - { |
|
| 1658 | - $objOid=$line; |
|
| 1659 | - continue; |
|
| 1660 | - } |
|
| 1661 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 1662 | - { |
|
| 1663 | - $objSyntax=$match[1]; |
|
| 1664 | - $objEnum=$match[2]; |
|
| 1665 | - continue; |
|
| 1666 | - } |
|
| 1667 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 1668 | - { |
|
| 1669 | - $objSyntax=$match[1]; |
|
| 1670 | - continue; |
|
| 1671 | - } |
|
| 1672 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 1673 | - { |
|
| 1674 | - $objDispHint=$match[1]; |
|
| 1675 | - continue; |
|
| 1676 | - } |
|
| 1677 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 1678 | - { |
|
| 1679 | - $objDesc=$match[1]; |
|
| 1680 | - continue; |
|
| 1681 | - } |
|
| 1682 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 1683 | - { |
|
| 1684 | - $tmpdesc=$match[1]; |
|
| 1685 | - $indesc=true; |
|
| 1686 | - continue; |
|
| 1687 | - } |
|
| 1688 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 1689 | - { |
|
| 1690 | - $objTc=$match[1]; |
|
| 1691 | - continue; |
|
| 1692 | - } |
|
| 1693 | - } |
|
| 1694 | - $this->trapLog("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 1695 | - //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 1696 | - // Update |
|
| 1697 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 1642 | + } |
|
| 1643 | + foreach ($snmptrans as $line) |
|
| 1644 | + { |
|
| 1645 | + if ($indesc===true) |
|
| 1646 | + { |
|
| 1647 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 1648 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
| 1649 | + { |
|
| 1650 | + $objDesc = $tmpdesc . $match[1]; |
|
| 1651 | + $indesc=false; |
|
| 1652 | + } |
|
| 1653 | + $tmpdesc.=$line; |
|
| 1654 | + continue; |
|
| 1655 | + } |
|
| 1656 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 1657 | + { |
|
| 1658 | + $objOid=$line; |
|
| 1659 | + continue; |
|
| 1660 | + } |
|
| 1661 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 1662 | + { |
|
| 1663 | + $objSyntax=$match[1]; |
|
| 1664 | + $objEnum=$match[2]; |
|
| 1665 | + continue; |
|
| 1666 | + } |
|
| 1667 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 1668 | + { |
|
| 1669 | + $objSyntax=$match[1]; |
|
| 1670 | + continue; |
|
| 1671 | + } |
|
| 1672 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 1673 | + { |
|
| 1674 | + $objDispHint=$match[1]; |
|
| 1675 | + continue; |
|
| 1676 | + } |
|
| 1677 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 1678 | + { |
|
| 1679 | + $objDesc=$match[1]; |
|
| 1680 | + continue; |
|
| 1681 | + } |
|
| 1682 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 1683 | + { |
|
| 1684 | + $tmpdesc=$match[1]; |
|
| 1685 | + $indesc=true; |
|
| 1686 | + continue; |
|
| 1687 | + } |
|
| 1688 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 1689 | + { |
|
| 1690 | + $objTc=$match[1]; |
|
| 1691 | + continue; |
|
| 1692 | + } |
|
| 1693 | + } |
|
| 1694 | + $this->trapLog("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 1695 | + //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 1696 | + // Update |
|
| 1697 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 1698 | 1698 | |
| 1699 | - if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 1700 | - { // if link exists, continue |
|
| 1701 | - $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 1702 | - continue; |
|
| 1703 | - } |
|
| 1704 | - if ($check_existing === true) |
|
| 1705 | - { |
|
| 1706 | - // TODO : check link trap - objects exists, mark them. |
|
| 1707 | - } |
|
| 1708 | - // Associate in object table |
|
| 1709 | - $sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 1710 | - 'values (:trap_id, :object_id)'; |
|
| 1711 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 1712 | - $sqlParam=array( |
|
| 1713 | - ':trap_id' => $trapId, |
|
| 1714 | - ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 1715 | - ); |
|
| 1699 | + if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 1700 | + { // if link exists, continue |
|
| 1701 | + $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 1702 | + continue; |
|
| 1703 | + } |
|
| 1704 | + if ($check_existing === true) |
|
| 1705 | + { |
|
| 1706 | + // TODO : check link trap - objects exists, mark them. |
|
| 1707 | + } |
|
| 1708 | + // Associate in object table |
|
| 1709 | + $sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 1710 | + 'values (:trap_id, :object_id)'; |
|
| 1711 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 1712 | + $sqlParam=array( |
|
| 1713 | + ':trap_id' => $trapId, |
|
| 1714 | + ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 1715 | + ); |
|
| 1716 | 1716 | |
| 1717 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1718 | - $this->trapLog('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 1719 | - } |
|
| 1720 | - } |
|
| 1721 | - if ($check_existing === true) |
|
| 1722 | - { |
|
| 1723 | - // TODO : remove link trap - objects that wasn't marked. |
|
| 1724 | - } |
|
| 1717 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1718 | + $this->trapLog('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 1719 | + } |
|
| 1720 | + } |
|
| 1721 | + if ($check_existing === true) |
|
| 1722 | + { |
|
| 1723 | + // TODO : remove link trap - objects that wasn't marked. |
|
| 1724 | + } |
|
| 1725 | 1725 | |
| 1726 | 1726 | } |
| 1727 | 1727 | |
@@ -1731,7 +1731,7 @@ discard block |
||
| 1731 | 1731 | * @param boolean $check_change : Force check of trap params & objects |
| 1732 | 1732 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
| 1733 | 1733 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
| 1734 | - */ |
|
| 1734 | + */ |
|
| 1735 | 1735 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
| 1736 | 1736 | { |
| 1737 | 1737 | // Timing |
@@ -1783,7 +1783,7 @@ discard block |
||
| 1783 | 1783 | |
| 1784 | 1784 | for ($curElement=0;$curElement < $numElements;$curElement++) |
| 1785 | 1785 | { |
| 1786 | - $time_1= microtime(true); |
|
| 1786 | + $time_1= microtime(true); |
|
| 1787 | 1787 | if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
| 1788 | 1788 | { // echo a . every 2 sec |
| 1789 | 1789 | echo '.'; |
@@ -1801,8 +1801,8 @@ discard block |
||
| 1801 | 1801 | // Get oid or pass if not found |
| 1802 | 1802 | if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
| 1803 | 1803 | { |
| 1804 | - $time_parse1 += microtime(true) - $time_1; |
|
| 1805 | - $time_parse1N ++; |
|
| 1804 | + $time_parse1 += microtime(true) - $time_1; |
|
| 1805 | + $time_parse1N ++; |
|
| 1806 | 1806 | continue; |
| 1807 | 1807 | } |
| 1808 | 1808 | $oid=$this->objectsAll[$curElement]; |
@@ -1813,7 +1813,7 @@ discard block |
||
| 1813 | 1813 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
| 1814 | 1814 | $this->objectsAll[$curElement],$match)) |
| 1815 | 1815 | { |
| 1816 | - $time_check1 += microtime(true) - $time_1; |
|
| 1816 | + $time_check1 += microtime(true) - $time_1; |
|
| 1817 | 1817 | $time_check1N++; |
| 1818 | 1818 | continue; |
| 1819 | 1819 | } |
@@ -1826,8 +1826,8 @@ discard block |
||
| 1826 | 1826 | // Check if next is suboid -> in that case is cannot be a trap |
| 1827 | 1827 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
| 1828 | 1828 | { |
| 1829 | - $time_check2 += microtime(true) - $time_1; |
|
| 1830 | - $time_check2N++; |
|
| 1829 | + $time_check2 += microtime(true) - $time_1; |
|
| 1830 | + $time_check2N++; |
|
| 1831 | 1831 | continue; |
| 1832 | 1832 | } |
| 1833 | 1833 | unset($snmptrans); |
@@ -1835,8 +1835,8 @@ discard block |
||
| 1835 | 1835 | ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
| 1836 | 1836 | if ($retVal!=0) |
| 1837 | 1837 | { |
| 1838 | - $time_check2 += microtime(true) - $time_1; |
|
| 1839 | - $time_check2N++; |
|
| 1838 | + $time_check2 += microtime(true) - $time_1; |
|
| 1839 | + $time_check2N++; |
|
| 1840 | 1840 | continue; |
| 1841 | 1841 | } |
| 1842 | 1842 | //echo "\n v1 trap found : $oid \n"; |
@@ -1845,7 +1845,7 @@ discard block |
||
| 1845 | 1845 | } |
| 1846 | 1846 | if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
| 1847 | 1847 | { |
| 1848 | - $time_check3 += microtime(true) - $time_1; |
|
| 1848 | + $time_check3 += microtime(true) - $time_1; |
|
| 1849 | 1849 | $time_check3N++; |
| 1850 | 1850 | continue; |
| 1851 | 1851 | } |
@@ -1866,7 +1866,7 @@ discard block |
||
| 1866 | 1866 | |
| 1867 | 1867 | if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
| 1868 | 1868 | { |
| 1869 | - $this->trapLog('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 1869 | + $this->trapLog('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 1870 | 1870 | } |
| 1871 | 1871 | $trapMib=$match[1]; |
| 1872 | 1872 | |
@@ -1874,17 +1874,17 @@ discard block |
||
| 1874 | 1874 | while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
| 1875 | 1875 | if (isset($snmptrans[$numLine])) |
| 1876 | 1876 | { |
| 1877 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 1877 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 1878 | 1878 | |
| 1879 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 1880 | - { |
|
| 1881 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 1882 | - $numLine++; |
|
| 1883 | - } |
|
| 1884 | - if (isset($snmptrans[$numLine])) { |
|
| 1885 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 1886 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 1887 | - } |
|
| 1879 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 1880 | + { |
|
| 1881 | + $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 1882 | + $numLine++; |
|
| 1883 | + } |
|
| 1884 | + if (isset($snmptrans[$numLine])) { |
|
| 1885 | + $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 1886 | + $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 1887 | + } |
|
| 1888 | 1888 | |
| 1889 | 1889 | } |
| 1890 | 1890 | $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
@@ -1892,23 +1892,23 @@ discard block |
||
| 1892 | 1892 | |
| 1893 | 1893 | if (($update==0) && ($check_change===false)) |
| 1894 | 1894 | { // Trapd didn't change & force check disabled |
| 1895 | - $time_objects += microtime(true) - $time_1; |
|
| 1896 | - if ($display_progress) echo "C"; |
|
| 1897 | - continue; |
|
| 1895 | + $time_objects += microtime(true) - $time_1; |
|
| 1896 | + if ($display_progress) echo "C"; |
|
| 1897 | + continue; |
|
| 1898 | 1898 | } |
| 1899 | 1899 | |
| 1900 | 1900 | $synt=null; |
| 1901 | 1901 | foreach ($snmptrans as $line) |
| 1902 | 1902 | { |
| 1903 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 1904 | - { |
|
| 1905 | - $synt=$match[1]; |
|
| 1906 | - } |
|
| 1903 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 1904 | + { |
|
| 1905 | + $synt=$match[1]; |
|
| 1906 | + } |
|
| 1907 | 1907 | } |
| 1908 | 1908 | if ($synt == null) |
| 1909 | 1909 | { |
| 1910 | 1910 | //echo "No objects for $trapOID\n"; |
| 1911 | - $time_objects += microtime(true) - $time_1; |
|
| 1911 | + $time_objects += microtime(true) - $time_1; |
|
| 1912 | 1912 | continue; |
| 1913 | 1913 | } |
| 1914 | 1914 | //echo "$synt \n"; |
@@ -1927,18 +1927,18 @@ discard block |
||
| 1927 | 1927 | |
| 1928 | 1928 | if ($display_progress) |
| 1929 | 1929 | { |
| 1930 | - echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 1931 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 1932 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 1933 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 1934 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 1930 | + echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 1931 | + echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 1932 | + echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 1933 | + echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 1934 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 1935 | 1935 | } |
| 1936 | 1936 | |
| 1937 | 1937 | // Timing ends |
| 1938 | 1938 | $timeTaken=microtime(true) - $timeTaken; |
| 1939 | 1939 | if ($display_progress) |
| 1940 | 1940 | { |
| 1941 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 1941 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 1942 | 1942 | } |
| 1943 | 1943 | |
| 1944 | 1944 | } |