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