@@ -12,8 +12,8 @@ discard block |
||
| 12 | 12 | { |
| 13 | 13 | |
| 14 | 14 | /** Get host list with filter (IP or name) : host=<filter> |
| 15 | - * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
| 16 | - */ |
|
| 15 | + * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
| 16 | + */ |
|
| 17 | 17 | public function gethostsAction() |
| 18 | 18 | { |
| 19 | 19 | $postData=$this->getRequest()->getPost(); |
@@ -40,8 +40,8 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | |
| 42 | 42 | /** Get hostgroup list with filter (name) : hostgroup=<hostFilter> |
| 43 | - * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
| 44 | - */ |
|
| 43 | + * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
| 44 | + */ |
|
| 45 | 45 | public function gethostgroupsAction() |
| 46 | 46 | { |
| 47 | 47 | $postData=$this->getRequest()->getPost(); |
@@ -68,11 +68,11 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | |
| 70 | 70 | /** Get service list by host name ( host=<host> ) |
| 71 | - * returns in JSON : |
|
| 72 | - * status=>OK/No services found/More than one host matches |
|
| 73 | - * services=>array of services (name) |
|
| 74 | - * hostid = host object id or -1 if not found. |
|
| 75 | - */ |
|
| 71 | + * returns in JSON : |
|
| 72 | + * status=>OK/No services found/More than one host matches |
|
| 73 | + * services=>array of services (name) |
|
| 74 | + * hostid = host object id or -1 if not found. |
|
| 75 | + */ |
|
| 76 | 76 | public function getservicesAction() |
| 77 | 77 | { |
| 78 | 78 | $postData=$this->getRequest()->getPost(); |
@@ -112,11 +112,11 @@ discard block |
||
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | /** Get service list by host group ( name=<host> ) |
| 115 | - * returns in JSON : |
|
| 116 | - * status=>OK/No services found/More than one host matches |
|
| 117 | - * services=>array of services (name) |
|
| 118 | - * groupid = group object id or -1 if not found. |
|
| 119 | - */ |
|
| 115 | + * returns in JSON : |
|
| 116 | + * status=>OK/No services found/More than one host matches |
|
| 117 | + * services=>array of services (name) |
|
| 118 | + * groupid = group object id or -1 if not found. |
|
| 119 | + */ |
|
| 120 | 120 | public function gethostgroupservicesAction() |
| 121 | 121 | { |
| 122 | 122 | $postData=$this->getRequest()->getPost(); |
@@ -153,10 +153,10 @@ discard block |
||
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** Get traps from mib : entry : mib=<mib> |
| 156 | - * returns in JSON : |
|
| 157 | - * status=>OK/No mib/Error getting mibs |
|
| 158 | - * traps=>array of array( oid -> name) |
|
| 159 | - */ |
|
| 156 | + * returns in JSON : |
|
| 157 | + * status=>OK/No mib/Error getting mibs |
|
| 158 | + * traps=>array of array( oid -> name) |
|
| 159 | + */ |
|
| 160 | 160 | public function gettrapsAction() |
| 161 | 161 | { |
| 162 | 162 | $postData=$this->getRequest()->getPost(); |
@@ -182,10 +182,10 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /** Get trap objects from mib : entry : trap=<oid> |
| 185 | - * returns in JSON : |
|
| 186 | - * status=>OK/no trap/not found |
|
| 187 | - * objects=>array of array( oid -> name, oid->mib) |
|
| 188 | - */ |
|
| 185 | + * returns in JSON : |
|
| 186 | + * status=>OK/no trap/not found |
|
| 187 | + * objects=>array of array( oid -> name, oid->mib) |
|
| 188 | + */ |
|
| 189 | 189 | public function gettrapobjectsAction() |
| 190 | 190 | { |
| 191 | 191 | $postData=$this->getRequest()->getPost(); |
@@ -211,8 +211,8 @@ discard block |
||
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | /** Get list of all loaded mibs : entry : none |
| 214 | - * return : array of strings. |
|
| 215 | - */ |
|
| 214 | + * return : array of strings. |
|
| 215 | + */ |
|
| 216 | 216 | public function getmiblistAction() |
| 217 | 217 | { |
| 218 | 218 | try |
@@ -227,10 +227,10 @@ discard block |
||
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | /** Get MIB::Name from OID : entry : oid |
| 230 | - * status=>OK/No oid/not found |
|
| 231 | - * mib=>string |
|
| 232 | - * name=>string |
|
| 233 | - */ |
|
| 230 | + * status=>OK/No oid/not found |
|
| 231 | + * mib=>string |
|
| 232 | + * name=>string |
|
| 233 | + */ |
|
| 234 | 234 | public function translateoidAction() |
| 235 | 235 | { |
| 236 | 236 | $postData=$this->getRequest()->getPost(); |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | 'name' => $object['name'], |
| 259 | 259 | 'type' => $object['type'], |
| 260 | 260 | 'type_enum' => $object['type_enum'], |
| 261 | - 'description' => $object['description'] |
|
| 261 | + 'description' => $object['description'] |
|
| 262 | 262 | ) |
| 263 | 263 | ); |
| 264 | 264 | } |
@@ -267,10 +267,10 @@ discard block |
||
| 267 | 267 | |
| 268 | 268 | |
| 269 | 269 | /** Save or execute database purge of <n> days |
| 270 | - * days=>int |
|
| 271 | - * action=>save/execute |
|
| 272 | - * return : status=>OK/Message error |
|
| 273 | - */ |
|
| 270 | + * days=>int |
|
| 271 | + * action=>save/execute |
|
| 272 | + * return : status=>OK/Message error |
|
| 273 | + */ |
|
| 274 | 274 | public function dbmaintenanceAction() |
| 275 | 275 | { |
| 276 | 276 | |
@@ -339,11 +339,11 @@ discard block |
||
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | /** Save log output to db |
| 342 | - * destination=>log destination |
|
| 343 | - * file=>file name |
|
| 344 | - * level => int |
|
| 345 | - * return : status=>OK/Message error |
|
| 346 | - */ |
|
| 342 | + * destination=>log destination |
|
| 343 | + * file=>file name |
|
| 344 | + * level => int |
|
| 345 | + * return : status=>OK/Message error |
|
| 346 | + */ |
|
| 347 | 347 | public function logdestinationAction() |
| 348 | 348 | { |
| 349 | 349 | $postData=$this->getRequest()->getPost(); |
@@ -367,8 +367,8 @@ discard block |
||
| 367 | 367 | $fileHandler=@fopen($file,'w'); |
| 368 | 368 | if ($fileHandler == false) |
| 369 | 369 | { // File os note writabe / cannot create |
| 370 | - $this->_helper->json(array('status'=>'File not writable : '.$file)); |
|
| 371 | - return; |
|
| 370 | + $this->_helper->json(array('status'=>'File not writable : '.$file)); |
|
| 371 | + return; |
|
| 372 | 372 | } |
| 373 | 373 | } |
| 374 | 374 | else |
@@ -418,50 +418,50 @@ discard block |
||
| 418 | 418 | public function testruleAction() |
| 419 | 419 | { |
| 420 | 420 | |
| 421 | - $postData=$this->getRequest()->getPost(); |
|
| 422 | - if (isset($postData['rule'])) |
|
| 423 | - { |
|
| 424 | - $rule=$postData['rule']; |
|
| 425 | - } |
|
| 426 | - else |
|
| 427 | - { |
|
| 428 | - $this->_helper->json(array('status'=>'No Rule')); |
|
| 429 | - } |
|
| 430 | - if (isset($postData['action'])) |
|
| 431 | - { |
|
| 432 | - $action=$postData['action']; |
|
| 433 | - if ($action != 'evaluate') |
|
| 434 | - { |
|
| 435 | - $this->_helper->json(array('status'=>'unknown action '.$action)); |
|
| 436 | - return; |
|
| 437 | - } |
|
| 438 | - } |
|
| 439 | - else |
|
| 440 | - { |
|
| 441 | - $this->_helper->json(array('status'=>'No action')); |
|
| 442 | - return; |
|
| 443 | - } |
|
| 444 | - if ($action == 'evaluate') |
|
| 445 | - { |
|
| 446 | - try |
|
| 447 | - { |
|
| 448 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 449 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 450 | - $Trap = new Trap($icingaweb2_etc); |
|
| 451 | - // Cleanup spaces before eval |
|
| 452 | - $rule=$Trap->ruleClass->eval_cleanup($rule); |
|
| 453 | - // Eval |
|
| 454 | - $item=0; |
|
| 455 | - $rule=$Trap->ruleClass->evaluation($rule,$item); |
|
| 456 | - } |
|
| 457 | - catch (Exception $e) |
|
| 458 | - { |
|
| 459 | - $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
|
| 460 | - return; |
|
| 461 | - } |
|
| 462 | - $return=($rule==true)?'true':'false'; |
|
| 463 | - $this->_helper->json(array('status'=>'OK', 'message' => $return)); |
|
| 464 | - } |
|
| 421 | + $postData=$this->getRequest()->getPost(); |
|
| 422 | + if (isset($postData['rule'])) |
|
| 423 | + { |
|
| 424 | + $rule=$postData['rule']; |
|
| 425 | + } |
|
| 426 | + else |
|
| 427 | + { |
|
| 428 | + $this->_helper->json(array('status'=>'No Rule')); |
|
| 429 | + } |
|
| 430 | + if (isset($postData['action'])) |
|
| 431 | + { |
|
| 432 | + $action=$postData['action']; |
|
| 433 | + if ($action != 'evaluate') |
|
| 434 | + { |
|
| 435 | + $this->_helper->json(array('status'=>'unknown action '.$action)); |
|
| 436 | + return; |
|
| 437 | + } |
|
| 438 | + } |
|
| 439 | + else |
|
| 440 | + { |
|
| 441 | + $this->_helper->json(array('status'=>'No action')); |
|
| 442 | + return; |
|
| 443 | + } |
|
| 444 | + if ($action == 'evaluate') |
|
| 445 | + { |
|
| 446 | + try |
|
| 447 | + { |
|
| 448 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 449 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 450 | + $Trap = new Trap($icingaweb2_etc); |
|
| 451 | + // Cleanup spaces before eval |
|
| 452 | + $rule=$Trap->ruleClass->eval_cleanup($rule); |
|
| 453 | + // Eval |
|
| 454 | + $item=0; |
|
| 455 | + $rule=$Trap->ruleClass->evaluation($rule,$item); |
|
| 456 | + } |
|
| 457 | + catch (Exception $e) |
|
| 458 | + { |
|
| 459 | + $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
|
| 460 | + return; |
|
| 461 | + } |
|
| 462 | + $return=($rule==true)?'true':'false'; |
|
| 463 | + $this->_helper->json(array('status'=>'OK', 'message' => $return)); |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | 466 | } |
| 467 | 467 | |
@@ -472,53 +472,53 @@ discard block |
||
| 472 | 472 | */ |
| 473 | 473 | public function pluginAction() |
| 474 | 474 | { |
| 475 | - $postData=$this->getRequest()->getPost(); |
|
| 476 | - if (isset($postData['name'])) |
|
| 477 | - { |
|
| 478 | - $pluginName=$postData['name']; |
|
| 479 | - } |
|
| 480 | - else |
|
| 481 | - { |
|
| 482 | - $this->_helper->json(array('status'=>'No plugin name')); |
|
| 483 | - } |
|
| 484 | - if (isset($postData['action'])) |
|
| 485 | - { |
|
| 486 | - $action=$postData['action']; |
|
| 487 | - if ($action != 'enable' && $action != 'disable') |
|
| 488 | - { |
|
| 489 | - $this->_helper->json(array('status'=>'unknown action '.$action)); |
|
| 490 | - return; |
|
| 491 | - } |
|
| 492 | - } |
|
| 493 | - else |
|
| 494 | - { |
|
| 495 | - $this->_helper->json(array('status'=>'No action')); |
|
| 496 | - return; |
|
| 497 | - } |
|
| 475 | + $postData=$this->getRequest()->getPost(); |
|
| 476 | + if (isset($postData['name'])) |
|
| 477 | + { |
|
| 478 | + $pluginName=$postData['name']; |
|
| 479 | + } |
|
| 480 | + else |
|
| 481 | + { |
|
| 482 | + $this->_helper->json(array('status'=>'No plugin name')); |
|
| 483 | + } |
|
| 484 | + if (isset($postData['action'])) |
|
| 485 | + { |
|
| 486 | + $action=$postData['action']; |
|
| 487 | + if ($action != 'enable' && $action != 'disable') |
|
| 488 | + { |
|
| 489 | + $this->_helper->json(array('status'=>'unknown action '.$action)); |
|
| 490 | + return; |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + else |
|
| 494 | + { |
|
| 495 | + $this->_helper->json(array('status'=>'No action')); |
|
| 496 | + return; |
|
| 497 | + } |
|
| 498 | 498 | |
| 499 | - try |
|
| 500 | - { |
|
| 501 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 502 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 503 | - $Trap = new Trap($icingaweb2_etc); |
|
| 504 | - // Enable plugin. |
|
| 505 | - $action=($action == 'enable') ? true : false; |
|
| 506 | - $retVal=$Trap->pluginClass->enablePlugin($pluginName, $action); |
|
| 499 | + try |
|
| 500 | + { |
|
| 501 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 502 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 503 | + $Trap = new Trap($icingaweb2_etc); |
|
| 504 | + // Enable plugin. |
|
| 505 | + $action=($action == 'enable') ? true : false; |
|
| 506 | + $retVal=$Trap->pluginClass->enablePlugin($pluginName, $action); |
|
| 507 | 507 | |
| 508 | - } |
|
| 509 | - catch (Exception $e) |
|
| 510 | - { |
|
| 511 | - $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
| 512 | - return; |
|
| 513 | - } |
|
| 514 | - if ($retVal === true) |
|
| 515 | - { |
|
| 516 | - $this->_helper->json(array('status'=>'OK')); |
|
| 517 | - } |
|
| 518 | - else |
|
| 519 | - { |
|
| 520 | - $this->_helper->json(array('status'=>'Error, see logs')); |
|
| 521 | - } |
|
| 508 | + } |
|
| 509 | + catch (Exception $e) |
|
| 510 | + { |
|
| 511 | + $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
| 512 | + return; |
|
| 513 | + } |
|
| 514 | + if ($retVal === true) |
|
| 515 | + { |
|
| 516 | + $this->_helper->json(array('status'=>'OK')); |
|
| 517 | + } |
|
| 518 | + else |
|
| 519 | + { |
|
| 520 | + $this->_helper->json(array('status'=>'Error, see logs')); |
|
| 521 | + } |
|
| 522 | 522 | } |
| 523 | 523 | |
| 524 | 524 | /** Function evaluation |
@@ -528,46 +528,46 @@ discard block |
||
| 528 | 528 | */ |
| 529 | 529 | public function functionAction() |
| 530 | 530 | { |
| 531 | - $postData=$this->getRequest()->getPost(); |
|
| 532 | - if (isset($postData['function'])) |
|
| 533 | - { |
|
| 534 | - $functionString=$postData['function']; |
|
| 535 | - } |
|
| 536 | - else |
|
| 537 | - { |
|
| 538 | - $this->_helper->json(array('status'=>'No function name')); |
|
| 539 | - } |
|
| 540 | - if (isset($postData['action'])) |
|
| 541 | - { |
|
| 542 | - $action=$postData['action']; |
|
| 543 | - if ($action != 'evaluate') |
|
| 544 | - { |
|
| 545 | - $this->_helper->json(array('status'=>'unknown action '.$action)); |
|
| 546 | - return; |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - else |
|
| 550 | - { |
|
| 551 | - $this->_helper->json(array('status'=>'No action')); |
|
| 552 | - return; |
|
| 553 | - } |
|
| 531 | + $postData=$this->getRequest()->getPost(); |
|
| 532 | + if (isset($postData['function'])) |
|
| 533 | + { |
|
| 534 | + $functionString=$postData['function']; |
|
| 535 | + } |
|
| 536 | + else |
|
| 537 | + { |
|
| 538 | + $this->_helper->json(array('status'=>'No function name')); |
|
| 539 | + } |
|
| 540 | + if (isset($postData['action'])) |
|
| 541 | + { |
|
| 542 | + $action=$postData['action']; |
|
| 543 | + if ($action != 'evaluate') |
|
| 544 | + { |
|
| 545 | + $this->_helper->json(array('status'=>'unknown action '.$action)); |
|
| 546 | + return; |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + else |
|
| 550 | + { |
|
| 551 | + $this->_helper->json(array('status'=>'No action')); |
|
| 552 | + return; |
|
| 553 | + } |
|
| 554 | 554 | |
| 555 | - try |
|
| 556 | - { |
|
| 557 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 558 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 559 | - $Trap = new Trap($icingaweb2_etc); |
|
| 560 | - // load plugins. |
|
| 561 | - $Trap->pluginClass->registerAllPlugins(false); |
|
| 562 | - $result = $Trap->pluginClass->evaluateFunctionString($functionString); |
|
| 563 | - } |
|
| 564 | - catch (Exception $e) |
|
| 565 | - { |
|
| 566 | - $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
| 567 | - return; |
|
| 568 | - } |
|
| 555 | + try |
|
| 556 | + { |
|
| 557 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 558 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 559 | + $Trap = new Trap($icingaweb2_etc); |
|
| 560 | + // load plugins. |
|
| 561 | + $Trap->pluginClass->registerAllPlugins(false); |
|
| 562 | + $result = $Trap->pluginClass->evaluateFunctionString($functionString); |
|
| 563 | + } |
|
| 564 | + catch (Exception $e) |
|
| 565 | + { |
|
| 566 | + $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
| 567 | + return; |
|
| 568 | + } |
|
| 569 | 569 | |
| 570 | - $result = ($result === true)?'True':'False'; |
|
| 571 | - $this->_helper->json(array('status'=>'OK','message' => $result)); |
|
| 570 | + $result = ($result === true)?'True':'False'; |
|
| 571 | + $this->_helper->json(array('status'=>'OK','message' => $result)); |
|
| 572 | 572 | } |
| 573 | 573 | } |
@@ -64,10 +64,10 @@ discard block |
||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** Mib management |
| 67 | - * Post param : action=update_mib_db : update mib database |
|
| 68 | - * Post param : ation=check_update : check if mib update is finished |
|
| 69 | - * File post : mibfile -> save mib file |
|
| 70 | - */ |
|
| 67 | + * Post param : action=update_mib_db : update mib database |
|
| 68 | + * Post param : ation=check_update : check if mib update is finished |
|
| 69 | + * File post : mibfile -> save mib file |
|
| 70 | + */ |
|
| 71 | 71 | public function mibAction() |
| 72 | 72 | { |
| 73 | 73 | $this->prepareTabs()->activate('mib'); |
@@ -86,22 +86,22 @@ discard block |
||
| 86 | 86 | $return=exec('icingacli trapdirector mib update --pid /tmp/trapdirector_update.pid'); |
| 87 | 87 | if (preg_match('/OK/',$return)) |
| 88 | 88 | { |
| 89 | - $this->_helper->json(array('status'=>'OK')); |
|
| 89 | + $this->_helper->json(array('status'=>'OK')); |
|
| 90 | 90 | } |
| 91 | 91 | // Error |
| 92 | 92 | $this->_helper->json(array('status'=>$return)); |
| 93 | 93 | } |
| 94 | 94 | if ($action == 'check_update') |
| 95 | 95 | { |
| 96 | - $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
| 97 | - if ($file == false) |
|
| 98 | - { // process is dead |
|
| 99 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
| 100 | - return; |
|
| 101 | - } |
|
| 102 | - $pid=fgets($file); |
|
| 103 | - $output=array(); |
|
| 104 | - $retVal=0; |
|
| 96 | + $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
| 97 | + if ($file == false) |
|
| 98 | + { // process is dead |
|
| 99 | + $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | + $pid=fgets($file); |
|
| 103 | + $output=array(); |
|
| 104 | + $retVal=0; |
|
| 105 | 105 | exec('ps '.$pid,$output,$retVal); |
| 106 | 106 | if ($retVal == 0) |
| 107 | 107 | { // process is alive |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | } |
| 110 | 110 | else |
| 111 | 111 | { // process is dead |
| 112 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
| 112 | + $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | 115 | $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible' )); |
@@ -122,26 +122,26 @@ discard block |
||
| 122 | 122 | $destDir=array_shift($DirConf); |
| 123 | 123 | if (!is_dir($destDir)) |
| 124 | 124 | { |
| 125 | - $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
|
| 125 | + $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
|
| 126 | 126 | } |
| 127 | 127 | else |
| 128 | 128 | { |
| 129 | - if (!is_writable($destDir)) |
|
| 130 | - { |
|
| 131 | - $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
|
| 132 | - } |
|
| 133 | - else |
|
| 134 | - { |
|
| 135 | - $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
| 136 | - if (move_uploaded_file($_FILES['mibfile']['tmp_name'],$destination)===false) |
|
| 137 | - { |
|
| 138 | - $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
|
| 139 | - } |
|
| 140 | - else |
|
| 141 | - { |
|
| 142 | - $this->view->uploadStatus="File $name uploaded in $destDir"; |
|
| 143 | - } |
|
| 144 | - } |
|
| 129 | + if (!is_writable($destDir)) |
|
| 130 | + { |
|
| 131 | + $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
|
| 132 | + } |
|
| 133 | + else |
|
| 134 | + { |
|
| 135 | + $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
| 136 | + if (move_uploaded_file($_FILES['mibfile']['tmp_name'],$destination)===false) |
|
| 137 | + { |
|
| 138 | + $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
|
| 139 | + } |
|
| 140 | + else |
|
| 141 | + { |
|
| 142 | + $this->view->uploadStatus="File $name uploaded in $destDir"; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | } |
@@ -261,47 +261,47 @@ discard block |
||
| 261 | 261 | */ |
| 262 | 262 | public function pluginsAction() |
| 263 | 263 | { |
| 264 | - $this->prepareTabs()->activate('plugins'); |
|
| 264 | + $this->prepareTabs()->activate('plugins'); |
|
| 265 | 265 | |
| 266 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 267 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 268 | - $Trap = new Trap($icingaweb2_etc,4); |
|
| 266 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 267 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 268 | + $Trap = new Trap($icingaweb2_etc,4); |
|
| 269 | 269 | |
| 270 | - $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
| 270 | + $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
| 271 | 271 | |
| 272 | - $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
| 272 | + $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
| 273 | 273 | |
| 274 | - $pluginList = $Trap->pluginClass->pluginList(); |
|
| 274 | + $pluginList = $Trap->pluginClass->pluginList(); |
|
| 275 | 275 | |
| 276 | - // Plugin list and fill function name list |
|
| 277 | - $functionList=array(); |
|
| 278 | - $this->view->pluginArray=array(); |
|
| 279 | - foreach ($pluginList as $plugin) |
|
| 280 | - { |
|
| 281 | - $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
|
| 282 | - $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
| 283 | - $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
| 284 | - $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
| 285 | - $pluginDetails->description = htmlentities($pluginDetails->description); |
|
| 286 | - $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
| 287 | - array_push($this->view->pluginArray, $pluginDetails); |
|
| 288 | - // Get functions for function details |
|
| 289 | - foreach ($pluginDetails->funcArray as $function) |
|
| 290 | - { |
|
| 291 | - array_push($functionList,$function); |
|
| 292 | - } |
|
| 293 | - } |
|
| 276 | + // Plugin list and fill function name list |
|
| 277 | + $functionList=array(); |
|
| 278 | + $this->view->pluginArray=array(); |
|
| 279 | + foreach ($pluginList as $plugin) |
|
| 280 | + { |
|
| 281 | + $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
|
| 282 | + $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
| 283 | + $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
| 284 | + $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
| 285 | + $pluginDetails->description = htmlentities($pluginDetails->description); |
|
| 286 | + $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
| 287 | + array_push($this->view->pluginArray, $pluginDetails); |
|
| 288 | + // Get functions for function details |
|
| 289 | + foreach ($pluginDetails->funcArray as $function) |
|
| 290 | + { |
|
| 291 | + array_push($functionList,$function); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - // Function list with details |
|
| 296 | - $this->view->functionList=array(); |
|
| 297 | - foreach ($functionList as $function) |
|
| 298 | - { |
|
| 299 | - $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
| 300 | - $functionDetail->params = htmlentities($functionDetail->params); |
|
| 301 | - $functionDetail->description = htmlentities($functionDetail->description); |
|
| 302 | - $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
| 303 | - array_push($this->view->functionList, $functionDetail); |
|
| 304 | - } |
|
| 295 | + // Function list with details |
|
| 296 | + $this->view->functionList=array(); |
|
| 297 | + foreach ($functionList as $function) |
|
| 298 | + { |
|
| 299 | + $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
| 300 | + $functionDetail->params = htmlentities($functionDetail->params); |
|
| 301 | + $functionDetail->description = htmlentities($functionDetail->description); |
|
| 302 | + $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
| 303 | + array_push($this->view->functionList, $functionDetail); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | 306 | } |
| 307 | 307 | |
@@ -316,30 +316,30 @@ discard block |
||
| 316 | 316 | )->add('services', array( |
| 317 | 317 | 'label' => $this->translate('Services management'), |
| 318 | 318 | 'url' => $this->getModuleConfig()->urlPath() . '/status/services') |
| 319 | - )->add('plugins', array( |
|
| 320 | - 'label' => $this->translate('Plugins management'), |
|
| 321 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
| 322 | - ); |
|
| 319 | + )->add('plugins', array( |
|
| 320 | + 'label' => $this->translate('Plugins management'), |
|
| 321 | + 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
| 322 | + ); |
|
| 323 | 323 | } |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | // TODO : see if useless |
| 327 | 327 | class UploadForm extends Form |
| 328 | 328 | { |
| 329 | - public function __construct($options = null) |
|
| 330 | - { |
|
| 331 | - parent::__construct($options); |
|
| 332 | - $this->addElements2(); |
|
| 333 | - } |
|
| 329 | + public function __construct($options = null) |
|
| 330 | + { |
|
| 331 | + parent::__construct($options); |
|
| 332 | + $this->addElements2(); |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | - public function addElements2() |
|
| 336 | - { |
|
| 337 | - // File Input |
|
| 338 | - $file = new File('mib-file'); |
|
| 339 | - $file->setLabel('Mib upload'); |
|
| 340 | - //->setAttrib('multiple', null); |
|
| 341 | - $this->addElement($file); |
|
| 335 | + public function addElements2() |
|
| 336 | + { |
|
| 337 | + // File Input |
|
| 338 | + $file = new File('mib-file'); |
|
| 339 | + $file->setLabel('Mib upload'); |
|
| 340 | + //->setAttrib('multiple', null); |
|
| 341 | + $this->addElement($file); |
|
| 342 | 342 | $button = new Submit("upload",array('ignore'=>false)); |
| 343 | 343 | $this->addElement($button);//->setIgnore(false); |
| 344 | - } |
|
| 344 | + } |
|
| 345 | 345 | } |
@@ -9,113 +9,113 @@ |
||
| 9 | 9 | class Logging |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - //**** Options from config database |
|
| 13 | - // Default values |
|
| 14 | - public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
| 15 | - public $outputMode='syslog'; // alert type : file, syslog, display |
|
| 16 | - public $outputFile="/tmp/trapdebug.txt"; |
|
| 17 | - protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
| 18 | - protected $outputList=array('file', 'syslog', 'display'); |
|
| 12 | + //**** Options from config database |
|
| 13 | + // Default values |
|
| 14 | + public $debugLevel=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL |
|
| 15 | + public $outputMode='syslog'; // alert type : file, syslog, display |
|
| 16 | + public $outputFile="/tmp/trapdebug.txt"; |
|
| 17 | + protected $logLevels=array("","Error","Warning","Info","Debug"); |
|
| 18 | + protected $outputList=array('file', 'syslog', 'display'); |
|
| 19 | 19 | |
| 20 | - /** Send log. Throws exception on critical error |
|
| 21 | - * @param string $message Message to log |
|
| 22 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 23 | - * @param string $destination file/syslog/display |
|
| 24 | - * @return void |
|
| 25 | - * @throws Exception |
|
| 26 | - **/ |
|
| 27 | - public function log( $message, $level, $destination ='') |
|
| 28 | - { |
|
| 29 | - if ($this->debugLevel >= $level) |
|
| 30 | - { |
|
| 31 | - $date = '['. date("Y/m/d H:i:s") . '] '; // no date in syslog as already there |
|
| 32 | - $message = '[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
| 20 | + /** Send log. Throws exception on critical error |
|
| 21 | + * @param string $message Message to log |
|
| 22 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 23 | + * @param string $destination file/syslog/display |
|
| 24 | + * @return void |
|
| 25 | + * @throws Exception |
|
| 26 | + **/ |
|
| 27 | + public function log( $message, $level, $destination ='') |
|
| 28 | + { |
|
| 29 | + if ($this->debugLevel >= $level) |
|
| 30 | + { |
|
| 31 | + $date = '['. date("Y/m/d H:i:s") . '] '; // no date in syslog as already there |
|
| 32 | + $message = '[TrapDirector] ['.$this->logLevels[$level].']: ' .$message . "\n"; |
|
| 33 | 33 | |
| 34 | - $output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
| 35 | - switch ($output) |
|
| 36 | - { |
|
| 37 | - case 'file': |
|
| 38 | - file_put_contents ($this->outputFile, $date.$message , FILE_APPEND); |
|
| 39 | - break; |
|
| 40 | - case 'syslog': |
|
| 41 | - switch($level) |
|
| 42 | - { |
|
| 43 | - case 1 : $prio = LOG_ERR;break; |
|
| 44 | - case 2 : $prio = LOG_WARNING;break; |
|
| 45 | - case 3 : $prio = LOG_INFO;break; |
|
| 46 | - case 4 : $prio = LOG_INFO;break; // LOG_DEBUG isn't always displayed in syslog |
|
| 47 | - default: $prio = LOG_ERR; |
|
| 48 | - } |
|
| 49 | - syslog($prio,$message); |
|
| 50 | - break; |
|
| 51 | - case 'display': |
|
| 52 | - echo $date.$message; |
|
| 53 | - break; |
|
| 54 | - default : // nothing we can do at this point |
|
| 55 | - throw new Exception($date.$message); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - if ($level == 1) |
|
| 59 | - { |
|
| 60 | - throw new Exception($message); |
|
| 61 | - } |
|
| 62 | - } |
|
| 34 | + $output = ( $destination != '' ) ? $destination : $this->outputMode; |
|
| 35 | + switch ($output) |
|
| 36 | + { |
|
| 37 | + case 'file': |
|
| 38 | + file_put_contents ($this->outputFile, $date.$message , FILE_APPEND); |
|
| 39 | + break; |
|
| 40 | + case 'syslog': |
|
| 41 | + switch($level) |
|
| 42 | + { |
|
| 43 | + case 1 : $prio = LOG_ERR;break; |
|
| 44 | + case 2 : $prio = LOG_WARNING;break; |
|
| 45 | + case 3 : $prio = LOG_INFO;break; |
|
| 46 | + case 4 : $prio = LOG_INFO;break; // LOG_DEBUG isn't always displayed in syslog |
|
| 47 | + default: $prio = LOG_ERR; |
|
| 48 | + } |
|
| 49 | + syslog($prio,$message); |
|
| 50 | + break; |
|
| 51 | + case 'display': |
|
| 52 | + echo $date.$message; |
|
| 53 | + break; |
|
| 54 | + default : // nothing we can do at this point |
|
| 55 | + throw new Exception($date.$message); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + if ($level == 1) |
|
| 59 | + { |
|
| 60 | + throw new Exception($message); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | |
| 65 | - public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
| 66 | - { |
|
| 67 | - $this->setLevel($debugLvl); |
|
| 68 | - switch ($outputType) |
|
| 69 | - { |
|
| 70 | - case 'file': |
|
| 71 | - if ($outputFile == null) throw new Exception("File logging without file !"); |
|
| 72 | - $this->setFile($outputFile); |
|
| 73 | - $this->setDestination('file'); |
|
| 74 | - break; |
|
| 75 | - default: |
|
| 76 | - $this->setDestination($outputType); |
|
| 77 | - } |
|
| 78 | - } |
|
| 65 | + public function setLogging($debugLvl,$outputType,$outputFile=null) |
|
| 66 | + { |
|
| 67 | + $this->setLevel($debugLvl); |
|
| 68 | + switch ($outputType) |
|
| 69 | + { |
|
| 70 | + case 'file': |
|
| 71 | + if ($outputFile == null) throw new Exception("File logging without file !"); |
|
| 72 | + $this->setFile($outputFile); |
|
| 73 | + $this->setDestination('file'); |
|
| 74 | + break; |
|
| 75 | + default: |
|
| 76 | + $this->setDestination($outputType); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Set logging level |
|
| 82 | - * @param integer $level |
|
| 83 | - * @throws Exception |
|
| 84 | - */ |
|
| 85 | - public function setLevel($level) |
|
| 86 | - { |
|
| 87 | - if (!is_integer($level) || $level < 0 || $level > 10) |
|
| 88 | - { |
|
| 89 | - throw new Exception('Invalid log level'); |
|
| 90 | - } |
|
| 91 | - $this->debugLevel=$level; |
|
| 92 | - } |
|
| 80 | + /** |
|
| 81 | + * Set logging level |
|
| 82 | + * @param integer $level |
|
| 83 | + * @throws Exception |
|
| 84 | + */ |
|
| 85 | + public function setLevel($level) |
|
| 86 | + { |
|
| 87 | + if (!is_integer($level) || $level < 0 || $level > 10) |
|
| 88 | + { |
|
| 89 | + throw new Exception('Invalid log level'); |
|
| 90 | + } |
|
| 91 | + $this->debugLevel=$level; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Set logging destination |
|
| 96 | - * @param string $destination |
|
| 97 | - * @throws Exception |
|
| 98 | - */ |
|
| 99 | - public function setDestination($destination) |
|
| 100 | - { |
|
| 101 | - if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
| 102 | - { |
|
| 103 | - throw new Exception('Invalid log destination'); |
|
| 104 | - } |
|
| 105 | - $this->outputMode=$destination; |
|
| 106 | - } |
|
| 107 | - /** |
|
| 108 | - * Set file destination |
|
| 109 | - * @param string $file |
|
| 110 | - * @throws Exception |
|
| 111 | - */ |
|
| 112 | - public function setFile($file) |
|
| 113 | - { |
|
| 114 | - if (!is_string($file)) |
|
| 115 | - { |
|
| 116 | - throw new Exception('Invalid log file'); |
|
| 117 | - } |
|
| 118 | - $this->outputFile=$file; |
|
| 119 | - } |
|
| 94 | + /** |
|
| 95 | + * Set logging destination |
|
| 96 | + * @param string $destination |
|
| 97 | + * @throws Exception |
|
| 98 | + */ |
|
| 99 | + public function setDestination($destination) |
|
| 100 | + { |
|
| 101 | + if (!is_string($destination) || ! in_array($destination, $this->outputList)) |
|
| 102 | + { |
|
| 103 | + throw new Exception('Invalid log destination'); |
|
| 104 | + } |
|
| 105 | + $this->outputMode=$destination; |
|
| 106 | + } |
|
| 107 | + /** |
|
| 108 | + * Set file destination |
|
| 109 | + * @param string $file |
|
| 110 | + * @throws Exception |
|
| 111 | + */ |
|
| 112 | + public function setFile($file) |
|
| 113 | + { |
|
| 114 | + if (!is_string($file)) |
|
| 115 | + { |
|
| 116 | + throw new Exception('Invalid log file'); |
|
| 117 | + } |
|
| 118 | + $this->outputFile=$file; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
| 122 | 122 | \ No newline at end of file |
@@ -9,941 +9,941 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | class Trap |
| 11 | 11 | { |
| 12 | - // Configuration files and dirs |
|
| 13 | - /** @var string Icinga etc path */ |
|
| 14 | - protected $icingaweb2_etc; |
|
| 15 | - protected $trap_module_config; //< config.ini of module |
|
| 16 | - protected $icingaweb2_ressources; //< resources.ini of icingaweb2 |
|
| 17 | - // Options from config.ini |
|
| 18 | - protected $snmptranslate='/usr/bin/snmptranslate'; |
|
| 19 | - protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
| 20 | - protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
| 21 | - protected $db_prefix='traps_'; |
|
| 22 | - |
|
| 23 | - // API |
|
| 24 | - protected $api_use=false; |
|
| 25 | - protected $icinga2api=null; |
|
| 26 | - protected $api_hostname=''; |
|
| 27 | - protected $api_port=0; |
|
| 28 | - protected $api_username=''; |
|
| 29 | - protected $api_password=''; |
|
| 30 | - |
|
| 31 | - // Logs |
|
| 32 | - /** @var Logging Logging class. */ |
|
| 33 | - public $logging; //< Logging class. |
|
| 34 | - /** @var bool true if log was setup in constructor */ |
|
| 35 | - protected $logSetup; //< bool true if log was setup in constructor |
|
| 36 | - |
|
| 37 | - // Databases |
|
| 38 | - /** @var Database $trapsDB Database class*/ |
|
| 39 | - public $trapsDB = null; |
|
| 40 | - |
|
| 41 | - // Trap received data |
|
| 42 | - protected $receivingHost; |
|
| 43 | - /** @var array Main trap data (oid, source...) */ |
|
| 44 | - public $trap_data=array(); |
|
| 45 | - public $trap_data_ext=array(); //< Additional trap data objects (oid/value). |
|
| 46 | - public $trap_id=null; //< trap_id after sql insert |
|
| 47 | - public $trap_action=null; //< trap action for final write |
|
| 48 | - protected $trap_to_db=true; //< log trap to DB |
|
| 49 | - |
|
| 50 | - /** @var Mib mib class */ |
|
| 51 | - public $mibClass = null; |
|
| 52 | - |
|
| 53 | - /** @var Rule rule class */ |
|
| 54 | - public $ruleClass = null; |
|
| 55 | - |
|
| 56 | - /** @var Plugins plugins manager **/ |
|
| 57 | - public $pluginClass = null; |
|
| 58 | - |
|
| 59 | - function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 60 | - { |
|
| 61 | - // Paths of ini files |
|
| 62 | - $this->icingaweb2_etc=$etc_dir; |
|
| 63 | - $this->trap_module_config=$this->icingaweb2_etc."/modules/trapdirector/config.ini"; |
|
| 64 | - $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
|
| 65 | - |
|
| 66 | - //************* Setup logging |
|
| 67 | - $this->logging = new Logging(); |
|
| 68 | - if ($baseLogLevel != null) |
|
| 69 | - { |
|
| 70 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 71 | - $this->logSetup=true; |
|
| 72 | - } |
|
| 73 | - else |
|
| 74 | - { |
|
| 75 | - $this->logSetup=false; |
|
| 76 | - } |
|
| 77 | - $this->logging->log('Loggin started', INFO); |
|
| 78 | - |
|
| 79 | - //*************** Get options from ini files |
|
| 80 | - if (! is_file($this->trap_module_config)) |
|
| 81 | - { |
|
| 82 | - throw new Exception("Ini file ".$this->trap_module_config." does not exists"); |
|
| 83 | - } |
|
| 84 | - $trapConfig=parse_ini_file($this->trap_module_config,true); |
|
| 85 | - if ($trapConfig == false) |
|
| 86 | - { |
|
| 87 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 88 | - throw new Exception("Error reading ini file : ".$this->trap_module_config); |
|
| 89 | - } |
|
| 90 | - $this->getMainOptions($trapConfig); // Get main options from ini file |
|
| 91 | - |
|
| 92 | - //*************** Setup database class & get options |
|
| 93 | - $this->setupDatabase($trapConfig); |
|
| 94 | - |
|
| 95 | - $this->getDatabaseOptions(); // Get options in database |
|
| 96 | - |
|
| 97 | - //*************** Setup API |
|
| 98 | - if ($this->api_use === true) $this->getAPI(); // Setup API |
|
| 99 | - |
|
| 100 | - //*************** Setup MIB |
|
| 101 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 102 | - |
|
| 103 | - //*************** Setup Rule |
|
| 104 | - $this->ruleClass = new Rule($this->logging); //< Create Rule class |
|
| 105 | - |
|
| 106 | - $this->trap_data=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
| 107 | - 'source_ip' => 'unknown', |
|
| 108 | - 'source_port' => 'unknown', |
|
| 109 | - 'destination_ip' => 'unknown', |
|
| 110 | - 'destination_port' => 'unknown', |
|
| 111 | - 'trap_oid' => 'unknown' |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - //*************** Setup Plugins |
|
| 115 | - //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
| 116 | - $this->pluginClass = new Plugins($this); |
|
| 12 | + // Configuration files and dirs |
|
| 13 | + /** @var string Icinga etc path */ |
|
| 14 | + protected $icingaweb2_etc; |
|
| 15 | + protected $trap_module_config; //< config.ini of module |
|
| 16 | + protected $icingaweb2_ressources; //< resources.ini of icingaweb2 |
|
| 17 | + // Options from config.ini |
|
| 18 | + protected $snmptranslate='/usr/bin/snmptranslate'; |
|
| 19 | + protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
| 20 | + protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
| 21 | + protected $db_prefix='traps_'; |
|
| 22 | + |
|
| 23 | + // API |
|
| 24 | + protected $api_use=false; |
|
| 25 | + protected $icinga2api=null; |
|
| 26 | + protected $api_hostname=''; |
|
| 27 | + protected $api_port=0; |
|
| 28 | + protected $api_username=''; |
|
| 29 | + protected $api_password=''; |
|
| 30 | + |
|
| 31 | + // Logs |
|
| 32 | + /** @var Logging Logging class. */ |
|
| 33 | + public $logging; //< Logging class. |
|
| 34 | + /** @var bool true if log was setup in constructor */ |
|
| 35 | + protected $logSetup; //< bool true if log was setup in constructor |
|
| 36 | + |
|
| 37 | + // Databases |
|
| 38 | + /** @var Database $trapsDB Database class*/ |
|
| 39 | + public $trapsDB = null; |
|
| 40 | + |
|
| 41 | + // Trap received data |
|
| 42 | + protected $receivingHost; |
|
| 43 | + /** @var array Main trap data (oid, source...) */ |
|
| 44 | + public $trap_data=array(); |
|
| 45 | + public $trap_data_ext=array(); //< Additional trap data objects (oid/value). |
|
| 46 | + public $trap_id=null; //< trap_id after sql insert |
|
| 47 | + public $trap_action=null; //< trap action for final write |
|
| 48 | + protected $trap_to_db=true; //< log trap to DB |
|
| 49 | + |
|
| 50 | + /** @var Mib mib class */ |
|
| 51 | + public $mibClass = null; |
|
| 52 | + |
|
| 53 | + /** @var Rule rule class */ |
|
| 54 | + public $ruleClass = null; |
|
| 55 | + |
|
| 56 | + /** @var Plugins plugins manager **/ |
|
| 57 | + public $pluginClass = null; |
|
| 58 | + |
|
| 59 | + function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 60 | + { |
|
| 61 | + // Paths of ini files |
|
| 62 | + $this->icingaweb2_etc=$etc_dir; |
|
| 63 | + $this->trap_module_config=$this->icingaweb2_etc."/modules/trapdirector/config.ini"; |
|
| 64 | + $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
|
| 65 | + |
|
| 66 | + //************* Setup logging |
|
| 67 | + $this->logging = new Logging(); |
|
| 68 | + if ($baseLogLevel != null) |
|
| 69 | + { |
|
| 70 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 71 | + $this->logSetup=true; |
|
| 72 | + } |
|
| 73 | + else |
|
| 74 | + { |
|
| 75 | + $this->logSetup=false; |
|
| 76 | + } |
|
| 77 | + $this->logging->log('Loggin started', INFO); |
|
| 78 | + |
|
| 79 | + //*************** Get options from ini files |
|
| 80 | + if (! is_file($this->trap_module_config)) |
|
| 81 | + { |
|
| 82 | + throw new Exception("Ini file ".$this->trap_module_config." does not exists"); |
|
| 83 | + } |
|
| 84 | + $trapConfig=parse_ini_file($this->trap_module_config,true); |
|
| 85 | + if ($trapConfig == false) |
|
| 86 | + { |
|
| 87 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 88 | + throw new Exception("Error reading ini file : ".$this->trap_module_config); |
|
| 89 | + } |
|
| 90 | + $this->getMainOptions($trapConfig); // Get main options from ini file |
|
| 91 | + |
|
| 92 | + //*************** Setup database class & get options |
|
| 93 | + $this->setupDatabase($trapConfig); |
|
| 94 | + |
|
| 95 | + $this->getDatabaseOptions(); // Get options in database |
|
| 96 | + |
|
| 97 | + //*************** Setup API |
|
| 98 | + if ($this->api_use === true) $this->getAPI(); // Setup API |
|
| 99 | + |
|
| 100 | + //*************** Setup MIB |
|
| 101 | + $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 102 | + |
|
| 103 | + //*************** Setup Rule |
|
| 104 | + $this->ruleClass = new Rule($this->logging); //< Create Rule class |
|
| 105 | + |
|
| 106 | + $this->trap_data=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
| 107 | + 'source_ip' => 'unknown', |
|
| 108 | + 'source_port' => 'unknown', |
|
| 109 | + 'destination_ip' => 'unknown', |
|
| 110 | + 'destination_port' => 'unknown', |
|
| 111 | + 'trap_oid' => 'unknown' |
|
| 112 | + ); |
|
| 113 | + |
|
| 114 | + //*************** Setup Plugins |
|
| 115 | + //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
| 116 | + $this->pluginClass = new Plugins($this); |
|
| 117 | 117 | |
| 118 | 118 | |
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get option from array of ini file, send message if empty |
|
| 123 | - * @param string $option_array Array of ini file |
|
| 124 | - * @param string $option_category category in ini file |
|
| 125 | - * @param string $option_name name of option in category |
|
| 126 | - * @param mixed $option_var variable to fill if found, left untouched if not found |
|
| 127 | - * @param integer $log_level default 2 (warning) |
|
| 128 | - * @param string $message warning message if not found |
|
| 129 | - * @return boolean true if found, or false |
|
| 130 | - */ |
|
| 131 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
| 132 | - { |
|
| 133 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 134 | - { |
|
| 135 | - if ($message === null) |
|
| 136 | - { |
|
| 137 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 138 | - } |
|
| 139 | - $this->logging->log($message,$log_level); |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - else |
|
| 143 | - { |
|
| 144 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 145 | - return true; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get options from ini file |
|
| 151 | - * @param array $trap_config : ini file array |
|
| 152 | - */ |
|
| 153 | - protected function getMainOptions($trapConfig) |
|
| 154 | - { |
|
| 155 | - |
|
| 156 | - // Snmptranslate binary path |
|
| 157 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 158 | - |
|
| 159 | - // mibs path |
|
| 160 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 161 | - |
|
| 162 | - // icinga2cmd path |
|
| 163 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 164 | - |
|
| 165 | - // table prefix |
|
| 166 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->db_prefix); |
|
| 167 | - |
|
| 168 | - // API options |
|
| 169 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
|
| 170 | - { |
|
| 171 | - $this->api_use=true; |
|
| 172 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 173 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 174 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Create and setup database class for trap & ido (if no api) db |
|
| 180 | - * @param array $trap_config : ini file array |
|
| 181 | - */ |
|
| 182 | - protected function setupDatabase($trapConfig) |
|
| 183 | - { |
|
| 184 | - // Trap database |
|
| 185 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 186 | - { |
|
| 187 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 188 | - return; |
|
| 189 | - } |
|
| 190 | - $dbTrapName=$trapConfig['config']['database']; |
|
| 191 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 192 | - |
|
| 193 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
|
| 194 | - { |
|
| 195 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 196 | - return; |
|
| 197 | - } |
|
| 198 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 199 | - { |
|
| 200 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 201 | - return; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 205 | - |
|
| 206 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 207 | - |
|
| 208 | - // IDO Database |
|
| 209 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 210 | - { |
|
| 211 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 212 | - } |
|
| 213 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 214 | - |
|
| 215 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 216 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 217 | - { |
|
| 218 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 219 | - return; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Get options in database |
|
| 227 | - */ |
|
| 228 | - protected function getDatabaseOptions() |
|
| 229 | - { |
|
| 230 | - // Database options |
|
| 231 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 232 | - { |
|
| 233 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 234 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 235 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** Set $variable to value if $element found in database config table |
|
| 240 | - * @param string $element |
|
| 241 | - * @param string $variable |
|
| 242 | - */ |
|
| 243 | - protected function getDBConfigIfSet($element,&$variable) |
|
| 244 | - { |
|
| 245 | - $value=$this->getDBConfig($element); |
|
| 246 | - if ($value != null) $variable=$value; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Get data from db_config |
|
| 251 | - * @param $element string name of param |
|
| 252 | - * @return mixed : value (or null) |
|
| 253 | - */ |
|
| 254 | - protected function getDBConfig($element) // TODO : put this in DB class |
|
| 255 | - { |
|
| 256 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 257 | - $sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 258 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 259 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 260 | - return null; |
|
| 261 | - } |
|
| 262 | - $value=$ret_code->fetch(); |
|
| 263 | - if ($value != null && isset($value['value'])) |
|
| 264 | - { |
|
| 265 | - return $value['value']; |
|
| 266 | - } |
|
| 267 | - return null; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** OBSOLETE Send log. Throws exception on critical error |
|
| 271 | - * @param string $message Message to log |
|
| 272 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 273 | - * @param string $destination file/syslog/display |
|
| 274 | - * @return void |
|
| 275 | - **/ |
|
| 276 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 277 | - { |
|
| 278 | - // TODO : replace ref with $this->logging->log |
|
| 279 | - $this->logging->log($message, $level, $destination); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 283 | - { |
|
| 284 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Returns or create new IcingaAPI object |
|
| 289 | - * @return \Icinga\Module\Trapdirector\Icinga2API |
|
| 290 | - */ |
|
| 291 | - protected function getAPI() |
|
| 292 | - { |
|
| 293 | - if ($this->icinga2api == null) |
|
| 294 | - { |
|
| 295 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 296 | - } |
|
| 297 | - return $this->icinga2api; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * read data from stream |
|
| 303 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 304 | - * @return mixed array trap data or exception with error |
|
| 305 | - */ |
|
| 306 | - public function read_trap($stream='php://stdin') |
|
| 307 | - { |
|
| 308 | - //Read data from snmptrapd from stdin |
|
| 309 | - $input_stream=fopen($stream, 'r'); |
|
| 310 | - |
|
| 311 | - if ($input_stream === false) |
|
| 312 | - { |
|
| 313 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 314 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 315 | - return null; // note : exception thrown by logging |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - // line 1 : host |
|
| 319 | - $this->receivingHost=chop(fgets($input_stream)); |
|
| 320 | - if ($this->receivingHost === false) |
|
| 321 | - { |
|
| 322 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 323 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
| 324 | - } |
|
| 325 | - // line 2 IP:port=>IP:port |
|
| 326 | - $IP=chop(fgets($input_stream)); |
|
| 327 | - if ($IP === false) |
|
| 328 | - { |
|
| 329 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 330 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
| 331 | - } |
|
| 332 | - $matches=array(); |
|
| 333 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 334 | - if ($ret_code===0 || $ret_code===false) |
|
| 335 | - { |
|
| 336 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 337 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 338 | - } |
|
| 339 | - else |
|
| 340 | - { |
|
| 341 | - $this->trap_data['source_ip']=$matches[1]; |
|
| 342 | - $this->trap_data['destination_ip']=$matches[3]; |
|
| 343 | - $this->trap_data['source_port']=$matches[2]; |
|
| 344 | - $this->trap_data['destination_port']=$matches[4]; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - while (($vars=fgets($input_stream)) !==false) |
|
| 348 | - { |
|
| 349 | - $vars=chop($vars); |
|
| 350 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 351 | - if ($ret_code===0 || $ret_code===false) |
|
| 352 | - { |
|
| 353 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 354 | - } |
|
| 355 | - else |
|
| 356 | - { |
|
| 357 | - 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')) |
|
| 358 | - { |
|
| 359 | - $this->trap_data['trap_oid']=$matches[2]; |
|
| 360 | - } |
|
| 361 | - else |
|
| 362 | - { |
|
| 363 | - $object= new stdClass; |
|
| 364 | - $object->oid =$matches[1]; |
|
| 365 | - $object->value = $matches[2]; |
|
| 366 | - array_push($this->trap_data_ext,$object); |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - if ($this->trap_data['trap_oid']=='unknown') |
|
| 372 | - { |
|
| 373 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 374 | - $this->logging->log('no trap oid found',ERROR,''); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - // Translate oids. |
|
| 378 | - |
|
| 379 | - $retArray=$this->translateOID($this->trap_data['trap_oid']); |
|
| 380 | - if ($retArray != null) |
|
| 381 | - { |
|
| 382 | - $this->trap_data['trap_name']=$retArray['trap_name']; |
|
| 383 | - $this->trap_data['trap_name_mib']=$retArray['trap_name_mib']; |
|
| 384 | - } |
|
| 385 | - foreach ($this->trap_data_ext as $key => $val) |
|
| 386 | - { |
|
| 387 | - $retArray=$this->translateOID($val->oid); |
|
| 388 | - if ($retArray != null) |
|
| 389 | - { |
|
| 390 | - $this->trap_data_ext[$key]->oid_name=$retArray['trap_name']; |
|
| 391 | - $this->trap_data_ext[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - $this->trap_data['status']= 'waiting'; |
|
| 397 | - |
|
| 398 | - return $this->trap_data; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Translate oid into array(MIB,Name) |
|
| 403 | - * @param $oid string oid to translate |
|
| 404 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 405 | - */ |
|
| 406 | - public function translateOID($oid) |
|
| 407 | - { |
|
| 408 | - // try from database |
|
| 409 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 410 | - |
|
| 411 | - $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
| 412 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 413 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 414 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 415 | - } |
|
| 416 | - $name=$ret_code->fetch(); |
|
| 417 | - if ($name['name'] != null) |
|
| 418 | - { |
|
| 419 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - // Also check if it is an instance of OID |
|
| 423 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 424 | - |
|
| 425 | - $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
| 426 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 427 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 428 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 429 | - } |
|
| 430 | - $name=$ret_code->fetch(); |
|
| 431 | - if ($name['name'] != null) |
|
| 432 | - { |
|
| 433 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - // Try to get oid name from snmptranslate |
|
| 437 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 438 | - ' '.$oid); |
|
| 439 | - $matches=array(); |
|
| 440 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 441 | - if ($ret_code===0 || $ret_code === false) { |
|
| 442 | - return NULL; |
|
| 443 | - } else { |
|
| 444 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 445 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Erase old trap records |
|
| 451 | - * @param integer $days : erase traps when more than $days old |
|
| 452 | - * @return integer : number of lines deleted |
|
| 453 | - **/ |
|
| 454 | - public function eraseOldTraps($days=0) |
|
| 455 | - { |
|
| 456 | - if ($days==0) |
|
| 457 | - { |
|
| 458 | - if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
| 459 | - { |
|
| 460 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 461 | - return; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 465 | - $daysago = strtotime("-".$days." day"); |
|
| 466 | - $sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 467 | - if ($db_conn->query($sql) === false) { |
|
| 468 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 469 | - } |
|
| 470 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** Write error to received trap database |
|
| 474 | - */ |
|
| 475 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 476 | - { |
|
| 477 | - |
|
| 478 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 479 | - |
|
| 480 | - // add date time |
|
| 481 | - $insert_col ='date_received,status'; |
|
| 482 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 483 | - |
|
| 484 | - if ($sourceIP !=null) |
|
| 485 | - { |
|
| 486 | - $insert_col .=',source_ip'; |
|
| 487 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 488 | - } |
|
| 489 | - if ($trapoid !=null) |
|
| 490 | - { |
|
| 491 | - $insert_col .=',trap_oid'; |
|
| 492 | - $insert_val .=",'". $trapoid ."'"; |
|
| 493 | - } |
|
| 494 | - $insert_col .=',status_detail'; |
|
| 495 | - $insert_val .=",'". $message ."'"; |
|
| 496 | - |
|
| 497 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 498 | - |
|
| 499 | - switch ($this->trapsDB->trapDBType) |
|
| 500 | - { |
|
| 501 | - case 'pgsql': |
|
| 502 | - $sql .= ' RETURNING id;'; |
|
| 503 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 504 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 505 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 506 | - } |
|
| 507 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 508 | - // Get last id to insert oid/values in secondary table |
|
| 509 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get option from array of ini file, send message if empty |
|
| 123 | + * @param string $option_array Array of ini file |
|
| 124 | + * @param string $option_category category in ini file |
|
| 125 | + * @param string $option_name name of option in category |
|
| 126 | + * @param mixed $option_var variable to fill if found, left untouched if not found |
|
| 127 | + * @param integer $log_level default 2 (warning) |
|
| 128 | + * @param string $message warning message if not found |
|
| 129 | + * @return boolean true if found, or false |
|
| 130 | + */ |
|
| 131 | + protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
| 132 | + { |
|
| 133 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 134 | + { |
|
| 135 | + if ($message === null) |
|
| 136 | + { |
|
| 137 | + $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 138 | + } |
|
| 139 | + $this->logging->log($message,$log_level); |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + else |
|
| 143 | + { |
|
| 144 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 145 | + return true; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get options from ini file |
|
| 151 | + * @param array $trap_config : ini file array |
|
| 152 | + */ |
|
| 153 | + protected function getMainOptions($trapConfig) |
|
| 154 | + { |
|
| 155 | + |
|
| 156 | + // Snmptranslate binary path |
|
| 157 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 158 | + |
|
| 159 | + // mibs path |
|
| 160 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 161 | + |
|
| 162 | + // icinga2cmd path |
|
| 163 | + $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 164 | + |
|
| 165 | + // table prefix |
|
| 166 | + $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->db_prefix); |
|
| 167 | + |
|
| 168 | + // API options |
|
| 169 | + if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
|
| 170 | + { |
|
| 171 | + $this->api_use=true; |
|
| 172 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 173 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 174 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Create and setup database class for trap & ido (if no api) db |
|
| 180 | + * @param array $trap_config : ini file array |
|
| 181 | + */ |
|
| 182 | + protected function setupDatabase($trapConfig) |
|
| 183 | + { |
|
| 184 | + // Trap database |
|
| 185 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
| 186 | + { |
|
| 187 | + $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 188 | + return; |
|
| 189 | + } |
|
| 190 | + $dbTrapName=$trapConfig['config']['database']; |
|
| 191 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 192 | + |
|
| 193 | + if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
|
| 194 | + { |
|
| 195 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 196 | + return; |
|
| 197 | + } |
|
| 198 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 199 | + { |
|
| 200 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 201 | + return; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 205 | + |
|
| 206 | + if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 207 | + |
|
| 208 | + // IDO Database |
|
| 209 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 210 | + { |
|
| 211 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 212 | + } |
|
| 213 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 214 | + |
|
| 215 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 216 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 217 | + { |
|
| 218 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 219 | + return; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Get options in database |
|
| 227 | + */ |
|
| 228 | + protected function getDatabaseOptions() |
|
| 229 | + { |
|
| 230 | + // Database options |
|
| 231 | + if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 232 | + { |
|
| 233 | + $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 234 | + $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 235 | + $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** Set $variable to value if $element found in database config table |
|
| 240 | + * @param string $element |
|
| 241 | + * @param string $variable |
|
| 242 | + */ |
|
| 243 | + protected function getDBConfigIfSet($element,&$variable) |
|
| 244 | + { |
|
| 245 | + $value=$this->getDBConfig($element); |
|
| 246 | + if ($value != null) $variable=$value; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Get data from db_config |
|
| 251 | + * @param $element string name of param |
|
| 252 | + * @return mixed : value (or null) |
|
| 253 | + */ |
|
| 254 | + protected function getDBConfig($element) // TODO : put this in DB class |
|
| 255 | + { |
|
| 256 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 257 | + $sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 258 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 259 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 260 | + return null; |
|
| 261 | + } |
|
| 262 | + $value=$ret_code->fetch(); |
|
| 263 | + if ($value != null && isset($value['value'])) |
|
| 264 | + { |
|
| 265 | + return $value['value']; |
|
| 266 | + } |
|
| 267 | + return null; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** OBSOLETE Send log. Throws exception on critical error |
|
| 271 | + * @param string $message Message to log |
|
| 272 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 273 | + * @param string $destination file/syslog/display |
|
| 274 | + * @return void |
|
| 275 | + **/ |
|
| 276 | + public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 277 | + { |
|
| 278 | + // TODO : replace ref with $this->logging->log |
|
| 279 | + $this->logging->log($message, $level, $destination); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 283 | + { |
|
| 284 | + $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Returns or create new IcingaAPI object |
|
| 289 | + * @return \Icinga\Module\Trapdirector\Icinga2API |
|
| 290 | + */ |
|
| 291 | + protected function getAPI() |
|
| 292 | + { |
|
| 293 | + if ($this->icinga2api == null) |
|
| 294 | + { |
|
| 295 | + $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 296 | + } |
|
| 297 | + return $this->icinga2api; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * read data from stream |
|
| 303 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 304 | + * @return mixed array trap data or exception with error |
|
| 305 | + */ |
|
| 306 | + public function read_trap($stream='php://stdin') |
|
| 307 | + { |
|
| 308 | + //Read data from snmptrapd from stdin |
|
| 309 | + $input_stream=fopen($stream, 'r'); |
|
| 310 | + |
|
| 311 | + if ($input_stream === false) |
|
| 312 | + { |
|
| 313 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 314 | + $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 315 | + return null; // note : exception thrown by logging |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + // line 1 : host |
|
| 319 | + $this->receivingHost=chop(fgets($input_stream)); |
|
| 320 | + if ($this->receivingHost === false) |
|
| 321 | + { |
|
| 322 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 323 | + $this->logging->log("Error reading Host !",ERROR,''); |
|
| 324 | + } |
|
| 325 | + // line 2 IP:port=>IP:port |
|
| 326 | + $IP=chop(fgets($input_stream)); |
|
| 327 | + if ($IP === false) |
|
| 328 | + { |
|
| 329 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 330 | + $this->logging->log("Error reading IP !",ERROR,''); |
|
| 331 | + } |
|
| 332 | + $matches=array(); |
|
| 333 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 334 | + if ($ret_code===0 || $ret_code===false) |
|
| 335 | + { |
|
| 336 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 337 | + $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 338 | + } |
|
| 339 | + else |
|
| 340 | + { |
|
| 341 | + $this->trap_data['source_ip']=$matches[1]; |
|
| 342 | + $this->trap_data['destination_ip']=$matches[3]; |
|
| 343 | + $this->trap_data['source_port']=$matches[2]; |
|
| 344 | + $this->trap_data['destination_port']=$matches[4]; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + while (($vars=fgets($input_stream)) !==false) |
|
| 348 | + { |
|
| 349 | + $vars=chop($vars); |
|
| 350 | + $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 351 | + if ($ret_code===0 || $ret_code===false) |
|
| 352 | + { |
|
| 353 | + $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 354 | + } |
|
| 355 | + else |
|
| 356 | + { |
|
| 357 | + 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')) |
|
| 358 | + { |
|
| 359 | + $this->trap_data['trap_oid']=$matches[2]; |
|
| 360 | + } |
|
| 361 | + else |
|
| 362 | + { |
|
| 363 | + $object= new stdClass; |
|
| 364 | + $object->oid =$matches[1]; |
|
| 365 | + $object->value = $matches[2]; |
|
| 366 | + array_push($this->trap_data_ext,$object); |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + if ($this->trap_data['trap_oid']=='unknown') |
|
| 372 | + { |
|
| 373 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 374 | + $this->logging->log('no trap oid found',ERROR,''); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + // Translate oids. |
|
| 378 | + |
|
| 379 | + $retArray=$this->translateOID($this->trap_data['trap_oid']); |
|
| 380 | + if ($retArray != null) |
|
| 381 | + { |
|
| 382 | + $this->trap_data['trap_name']=$retArray['trap_name']; |
|
| 383 | + $this->trap_data['trap_name_mib']=$retArray['trap_name_mib']; |
|
| 384 | + } |
|
| 385 | + foreach ($this->trap_data_ext as $key => $val) |
|
| 386 | + { |
|
| 387 | + $retArray=$this->translateOID($val->oid); |
|
| 388 | + if ($retArray != null) |
|
| 389 | + { |
|
| 390 | + $this->trap_data_ext[$key]->oid_name=$retArray['trap_name']; |
|
| 391 | + $this->trap_data_ext[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + $this->trap_data['status']= 'waiting'; |
|
| 397 | + |
|
| 398 | + return $this->trap_data; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Translate oid into array(MIB,Name) |
|
| 403 | + * @param $oid string oid to translate |
|
| 404 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 405 | + */ |
|
| 406 | + public function translateOID($oid) |
|
| 407 | + { |
|
| 408 | + // try from database |
|
| 409 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 410 | + |
|
| 411 | + $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
| 412 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 413 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 414 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 415 | + } |
|
| 416 | + $name=$ret_code->fetch(); |
|
| 417 | + if ($name['name'] != null) |
|
| 418 | + { |
|
| 419 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + // Also check if it is an instance of OID |
|
| 423 | + $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 424 | + |
|
| 425 | + $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
| 426 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 427 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 428 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 429 | + } |
|
| 430 | + $name=$ret_code->fetch(); |
|
| 431 | + if ($name['name'] != null) |
|
| 432 | + { |
|
| 433 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + // Try to get oid name from snmptranslate |
|
| 437 | + $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 438 | + ' '.$oid); |
|
| 439 | + $matches=array(); |
|
| 440 | + $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 441 | + if ($ret_code===0 || $ret_code === false) { |
|
| 442 | + return NULL; |
|
| 443 | + } else { |
|
| 444 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 445 | + return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Erase old trap records |
|
| 451 | + * @param integer $days : erase traps when more than $days old |
|
| 452 | + * @return integer : number of lines deleted |
|
| 453 | + **/ |
|
| 454 | + public function eraseOldTraps($days=0) |
|
| 455 | + { |
|
| 456 | + if ($days==0) |
|
| 457 | + { |
|
| 458 | + if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
| 459 | + { |
|
| 460 | + $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 461 | + return; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 465 | + $daysago = strtotime("-".$days." day"); |
|
| 466 | + $sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 467 | + if ($db_conn->query($sql) === false) { |
|
| 468 | + $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 469 | + } |
|
| 470 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** Write error to received trap database |
|
| 474 | + */ |
|
| 475 | + public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 476 | + { |
|
| 477 | + |
|
| 478 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 479 | + |
|
| 480 | + // add date time |
|
| 481 | + $insert_col ='date_received,status'; |
|
| 482 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 483 | + |
|
| 484 | + if ($sourceIP !=null) |
|
| 485 | + { |
|
| 486 | + $insert_col .=',source_ip'; |
|
| 487 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 488 | + } |
|
| 489 | + if ($trapoid !=null) |
|
| 490 | + { |
|
| 491 | + $insert_col .=',trap_oid'; |
|
| 492 | + $insert_val .=",'". $trapoid ."'"; |
|
| 493 | + } |
|
| 494 | + $insert_col .=',status_detail'; |
|
| 495 | + $insert_val .=",'". $message ."'"; |
|
| 496 | + |
|
| 497 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 498 | + |
|
| 499 | + switch ($this->trapsDB->trapDBType) |
|
| 500 | + { |
|
| 501 | + case 'pgsql': |
|
| 502 | + $sql .= ' RETURNING id;'; |
|
| 503 | + $this->logging->log('sql : '.$sql,INFO); |
|
| 504 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 505 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 506 | + } |
|
| 507 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 508 | + // Get last id to insert oid/values in secondary table |
|
| 509 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 510 | 510 | |
| 511 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 512 | - } |
|
| 513 | - if (! isset($inserted_id_ret['id'])) { |
|
| 514 | - $this->logging->log('Error getting id',1,''); |
|
| 515 | - } |
|
| 516 | - $this->trap_id=$inserted_id_ret['id']; |
|
| 517 | - break; |
|
| 518 | - case 'mysql': |
|
| 519 | - $sql .= ';'; |
|
| 520 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 521 | - if ($db_conn->query($sql) === false) { |
|
| 522 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 523 | - } |
|
| 524 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 525 | - // Get last id to insert oid/values in secondary table |
|
| 526 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 527 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 528 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 529 | - } |
|
| 511 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 512 | + } |
|
| 513 | + if (! isset($inserted_id_ret['id'])) { |
|
| 514 | + $this->logging->log('Error getting id',1,''); |
|
| 515 | + } |
|
| 516 | + $this->trap_id=$inserted_id_ret['id']; |
|
| 517 | + break; |
|
| 518 | + case 'mysql': |
|
| 519 | + $sql .= ';'; |
|
| 520 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 521 | + if ($db_conn->query($sql) === false) { |
|
| 522 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 523 | + } |
|
| 524 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 525 | + // Get last id to insert oid/values in secondary table |
|
| 526 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 527 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 528 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 529 | + } |
|
| 530 | 530 | |
| 531 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 532 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 533 | - $this->trap_id=$inserted_id; |
|
| 534 | - break; |
|
| 535 | - default: |
|
| 536 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** Write trap data to trap database |
|
| 543 | - */ |
|
| 544 | - public function writeTrapToDB() |
|
| 545 | - { |
|
| 546 | - |
|
| 547 | - // If action is ignore -> don't send t DB |
|
| 548 | - if ($this->trap_to_db === false) return; |
|
| 549 | - |
|
| 550 | - |
|
| 551 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 552 | - |
|
| 553 | - $insert_col=''; |
|
| 554 | - $insert_val=''; |
|
| 555 | - // add date time |
|
| 556 | - $this->trap_data['date_received'] = date("Y-m-d H:i:s"); |
|
| 557 | - |
|
| 558 | - $firstcol=1; |
|
| 559 | - foreach ($this->trap_data as $col => $val) |
|
| 560 | - { |
|
| 561 | - if ($firstcol==0) |
|
| 562 | - { |
|
| 563 | - $insert_col .=','; |
|
| 564 | - $insert_val .=','; |
|
| 565 | - } |
|
| 566 | - $insert_col .= $col ; |
|
| 567 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 568 | - $firstcol=0; |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 572 | - switch ($this->trapsDB->trapDBType) |
|
| 573 | - { |
|
| 574 | - case 'pgsql': |
|
| 575 | - $sql .= ' RETURNING id;'; |
|
| 576 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 577 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 578 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 579 | - } |
|
| 580 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 581 | - // Get last id to insert oid/values in secondary table |
|
| 582 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 531 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 532 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 533 | + $this->trap_id=$inserted_id; |
|
| 534 | + break; |
|
| 535 | + default: |
|
| 536 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** Write trap data to trap database |
|
| 543 | + */ |
|
| 544 | + public function writeTrapToDB() |
|
| 545 | + { |
|
| 546 | + |
|
| 547 | + // If action is ignore -> don't send t DB |
|
| 548 | + if ($this->trap_to_db === false) return; |
|
| 549 | + |
|
| 550 | + |
|
| 551 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 552 | + |
|
| 553 | + $insert_col=''; |
|
| 554 | + $insert_val=''; |
|
| 555 | + // add date time |
|
| 556 | + $this->trap_data['date_received'] = date("Y-m-d H:i:s"); |
|
| 557 | + |
|
| 558 | + $firstcol=1; |
|
| 559 | + foreach ($this->trap_data as $col => $val) |
|
| 560 | + { |
|
| 561 | + if ($firstcol==0) |
|
| 562 | + { |
|
| 563 | + $insert_col .=','; |
|
| 564 | + $insert_val .=','; |
|
| 565 | + } |
|
| 566 | + $insert_col .= $col ; |
|
| 567 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 568 | + $firstcol=0; |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 572 | + switch ($this->trapsDB->trapDBType) |
|
| 573 | + { |
|
| 574 | + case 'pgsql': |
|
| 575 | + $sql .= ' RETURNING id;'; |
|
| 576 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 577 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 578 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 579 | + } |
|
| 580 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 581 | + // Get last id to insert oid/values in secondary table |
|
| 582 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 583 | 583 | |
| 584 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 585 | - } |
|
| 586 | - if (! isset($inserted_id_ret['id'])) { |
|
| 587 | - $this->logging->log('Error getting id',ERROR,''); |
|
| 588 | - } |
|
| 589 | - $this->trap_id=$inserted_id_ret['id']; |
|
| 590 | - break; |
|
| 591 | - case 'mysql': |
|
| 592 | - $sql .= ';'; |
|
| 593 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 594 | - if ($db_conn->query($sql) === false) { |
|
| 595 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 596 | - } |
|
| 597 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 598 | - // Get last id to insert oid/values in secondary table |
|
| 599 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 600 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 601 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 602 | - } |
|
| 584 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 585 | + } |
|
| 586 | + if (! isset($inserted_id_ret['id'])) { |
|
| 587 | + $this->logging->log('Error getting id',ERROR,''); |
|
| 588 | + } |
|
| 589 | + $this->trap_id=$inserted_id_ret['id']; |
|
| 590 | + break; |
|
| 591 | + case 'mysql': |
|
| 592 | + $sql .= ';'; |
|
| 593 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 594 | + if ($db_conn->query($sql) === false) { |
|
| 595 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 596 | + } |
|
| 597 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 598 | + // Get last id to insert oid/values in secondary table |
|
| 599 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 600 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 601 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 602 | + } |
|
| 603 | 603 | |
| 604 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 605 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 606 | - $this->trap_id=$inserted_id; |
|
| 607 | - break; |
|
| 608 | - default: |
|
| 609 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 610 | - } |
|
| 611 | - $this->logging->log('id found: '.$this->trap_id,INFO ); |
|
| 612 | - |
|
| 613 | - // Fill trap extended data table |
|
| 614 | - foreach ($this->trap_data_ext as $value) { |
|
| 615 | - // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
| 616 | - $firstcol=1; |
|
| 617 | - $value->trap_id = $this->trap_id; |
|
| 618 | - $insert_col=''; |
|
| 619 | - $insert_val=''; |
|
| 620 | - foreach ($value as $col => $val) |
|
| 621 | - { |
|
| 622 | - if ($firstcol==0) |
|
| 623 | - { |
|
| 624 | - $insert_col .=','; |
|
| 625 | - $insert_val .=','; |
|
| 626 | - } |
|
| 627 | - $insert_col .= $col; |
|
| 628 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 629 | - $firstcol=0; |
|
| 630 | - } |
|
| 604 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 605 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 606 | + $this->trap_id=$inserted_id; |
|
| 607 | + break; |
|
| 608 | + default: |
|
| 609 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 610 | + } |
|
| 611 | + $this->logging->log('id found: '.$this->trap_id,INFO ); |
|
| 612 | + |
|
| 613 | + // Fill trap extended data table |
|
| 614 | + foreach ($this->trap_data_ext as $value) { |
|
| 615 | + // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
| 616 | + $firstcol=1; |
|
| 617 | + $value->trap_id = $this->trap_id; |
|
| 618 | + $insert_col=''; |
|
| 619 | + $insert_val=''; |
|
| 620 | + foreach ($value as $col => $val) |
|
| 621 | + { |
|
| 622 | + if ($firstcol==0) |
|
| 623 | + { |
|
| 624 | + $insert_col .=','; |
|
| 625 | + $insert_val .=','; |
|
| 626 | + } |
|
| 627 | + $insert_col .= $col; |
|
| 628 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 629 | + $firstcol=0; |
|
| 630 | + } |
|
| 631 | 631 | |
| 632 | - $sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 632 | + $sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 633 | 633 | |
| 634 | - if ($db_conn->query($sql) === false) { |
|
| 635 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 636 | - } |
|
| 637 | - } |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** Get rules from rule database with ip and oid |
|
| 641 | - * @param $ip string ipv4 or ipv6 |
|
| 642 | - * @param $oid string oid in numeric |
|
| 643 | - * @return mixed|boolean : PDO object or false |
|
| 644 | - */ |
|
| 645 | - protected function getRules($ip,$oid) |
|
| 646 | - { |
|
| 647 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 648 | - // fetch rules based on IP in rule and OID |
|
| 649 | - $sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
| 650 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 651 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 652 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 653 | - return false; |
|
| 654 | - } |
|
| 655 | - $rules_all=$ret_code->fetchAll(); |
|
| 656 | - //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
| 657 | - $rules_ret=array(); |
|
| 658 | - $rule_ret_key=0; |
|
| 659 | - foreach ($rules_all as $key => $rule) |
|
| 660 | - { |
|
| 661 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 662 | - { |
|
| 663 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 664 | - //TODO : get host name by API (and check if correct in rule). |
|
| 665 | - $rule_ret_key++; |
|
| 666 | - continue; |
|
| 667 | - } |
|
| 668 | - // TODO : get hosts IP by API |
|
| 669 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 670 | - { // get ips of group members by oid |
|
| 671 | - $db_conn2=$this->trapsDB->db_connect_ido(); |
|
| 672 | - $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
| 634 | + if ($db_conn->query($sql) === false) { |
|
| 635 | + $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 636 | + } |
|
| 637 | + } |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** Get rules from rule database with ip and oid |
|
| 641 | + * @param $ip string ipv4 or ipv6 |
|
| 642 | + * @param $oid string oid in numeric |
|
| 643 | + * @return mixed|boolean : PDO object or false |
|
| 644 | + */ |
|
| 645 | + protected function getRules($ip,$oid) |
|
| 646 | + { |
|
| 647 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 648 | + // fetch rules based on IP in rule and OID |
|
| 649 | + $sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
| 650 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 651 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 652 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 653 | + return false; |
|
| 654 | + } |
|
| 655 | + $rules_all=$ret_code->fetchAll(); |
|
| 656 | + //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
| 657 | + $rules_ret=array(); |
|
| 658 | + $rule_ret_key=0; |
|
| 659 | + foreach ($rules_all as $key => $rule) |
|
| 660 | + { |
|
| 661 | + if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 662 | + { |
|
| 663 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 664 | + //TODO : get host name by API (and check if correct in rule). |
|
| 665 | + $rule_ret_key++; |
|
| 666 | + continue; |
|
| 667 | + } |
|
| 668 | + // TODO : get hosts IP by API |
|
| 669 | + if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 670 | + { // get ips of group members by oid |
|
| 671 | + $db_conn2=$this->trapsDB->db_connect_ido(); |
|
| 672 | + $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
| 673 | 673 | FROM icinga_objects as o |
| 674 | 674 | LEFT JOIN icinga_hostgroups as h ON o.object_id=h.hostgroup_object_id |
| 675 | 675 | LEFT JOIN icinga_hostgroup_members as m ON h.hostgroup_id=m.hostgroup_id |
| 676 | 676 | LEFT JOIN icinga_hosts as a ON a.host_object_id = m.host_object_id |
| 677 | 677 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
| 678 | 678 | WHERE o.name1='".$rule['host_group_name']."';"; |
| 679 | - if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
| 680 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 681 | - continue; |
|
| 682 | - } |
|
| 683 | - $grouphosts=$ret_code2->fetchAll(); |
|
| 684 | - //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
| 685 | - foreach ( $grouphosts as $host) |
|
| 686 | - { |
|
| 687 | - //echo $host['ip4']."\n"; |
|
| 688 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 689 | - { |
|
| 690 | - //echo "Rule added \n"; |
|
| 691 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 692 | - $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
| 693 | - $rule_ret_key++; |
|
| 694 | - } |
|
| 695 | - } |
|
| 696 | - } |
|
| 697 | - } |
|
| 698 | - //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
| 699 | - return $rules_ret; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** Add rule match to rule |
|
| 703 | - * @param id int : rule id |
|
| 704 | - * @param set int : value to set |
|
| 705 | - */ |
|
| 706 | - protected function add_rule_match($id, $set) |
|
| 707 | - { |
|
| 708 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 709 | - $sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
| 710 | - if ($db_conn->query($sql) === false) { |
|
| 711 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 712 | - } |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
| 716 | - * |
|
| 717 | - * @param string $host |
|
| 718 | - * @param string $service |
|
| 719 | - * @param integer $state numerical staus |
|
| 720 | - * @param string $display |
|
| 721 | - * @returnn bool true is service check was sent without error |
|
| 722 | - */ |
|
| 723 | - public function serviceCheckResult($host,$service,$state,$display) |
|
| 724 | - { |
|
| 725 | - if ($this->api_use === false) |
|
| 726 | - { |
|
| 727 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 728 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 729 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 679 | + if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
| 680 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 681 | + continue; |
|
| 682 | + } |
|
| 683 | + $grouphosts=$ret_code2->fetchAll(); |
|
| 684 | + //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
| 685 | + foreach ( $grouphosts as $host) |
|
| 686 | + { |
|
| 687 | + //echo $host['ip4']."\n"; |
|
| 688 | + if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 689 | + { |
|
| 690 | + //echo "Rule added \n"; |
|
| 691 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 692 | + $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
| 693 | + $rule_ret_key++; |
|
| 694 | + } |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | + } |
|
| 698 | + //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
| 699 | + return $rules_ret; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** Add rule match to rule |
|
| 703 | + * @param id int : rule id |
|
| 704 | + * @param set int : value to set |
|
| 705 | + */ |
|
| 706 | + protected function add_rule_match($id, $set) |
|
| 707 | + { |
|
| 708 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 709 | + $sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
| 710 | + if ($db_conn->query($sql) === false) { |
|
| 711 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 712 | + } |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
| 716 | + * |
|
| 717 | + * @param string $host |
|
| 718 | + * @param string $service |
|
| 719 | + * @param integer $state numerical staus |
|
| 720 | + * @param string $display |
|
| 721 | + * @returnn bool true is service check was sent without error |
|
| 722 | + */ |
|
| 723 | + public function serviceCheckResult($host,$service,$state,$display) |
|
| 724 | + { |
|
| 725 | + if ($this->api_use === false) |
|
| 726 | + { |
|
| 727 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 728 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 729 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 730 | 730 | |
| 731 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 732 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 733 | - return true; |
|
| 734 | - } |
|
| 735 | - else |
|
| 736 | - { |
|
| 737 | - // Get perfdata if found |
|
| 738 | - $matches=array(); |
|
| 739 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 740 | - { |
|
| 741 | - $display=$matches[1]; |
|
| 742 | - $perfdata=$matches[2]; |
|
| 743 | - } |
|
| 744 | - else |
|
| 745 | - { |
|
| 746 | - $perfdata=''; |
|
| 747 | - } |
|
| 731 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 732 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 733 | + return true; |
|
| 734 | + } |
|
| 735 | + else |
|
| 736 | + { |
|
| 737 | + // Get perfdata if found |
|
| 738 | + $matches=array(); |
|
| 739 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 740 | + { |
|
| 741 | + $display=$matches[1]; |
|
| 742 | + $perfdata=$matches[2]; |
|
| 743 | + } |
|
| 744 | + else |
|
| 745 | + { |
|
| 746 | + $perfdata=''; |
|
| 747 | + } |
|
| 748 | 748 | |
| 749 | - $api = $this->getAPI(); |
|
| 750 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 751 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 752 | - if ($retcode == false) |
|
| 753 | - { |
|
| 754 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 755 | - return false; |
|
| 756 | - } |
|
| 757 | - else |
|
| 758 | - { |
|
| 759 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 760 | - return true; |
|
| 761 | - } |
|
| 762 | - } |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - public function getHostByIP($ip) |
|
| 766 | - { |
|
| 767 | - $api = $this->getAPI(); |
|
| 768 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 769 | - return $api->getHostByIP($ip); |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - /** Resolve display. |
|
| 773 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 774 | - * @param $display string |
|
| 775 | - * @return string display |
|
| 776 | - */ |
|
| 777 | - protected function applyDisplay($display) |
|
| 778 | - { |
|
| 779 | - $matches=array(); |
|
| 780 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 781 | - { |
|
| 782 | - $oid=$matches[1]; |
|
| 783 | - $found=0; |
|
| 784 | - foreach($this->trap_data_ext as $val) |
|
| 785 | - { |
|
| 786 | - if ($oid == $val->oid) |
|
| 787 | - { |
|
| 788 | - $val->value=preg_replace('/"/','',$val->value); |
|
| 789 | - $rep=0; |
|
| 790 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 791 | - if ($rep==0) |
|
| 792 | - { |
|
| 793 | - $this->logging->log("Error in display",WARN,''); |
|
| 794 | - return $display; |
|
| 795 | - } |
|
| 796 | - $found=1; |
|
| 797 | - break; |
|
| 798 | - } |
|
| 799 | - } |
|
| 800 | - if ($found==0) |
|
| 801 | - { |
|
| 802 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 803 | - if ($rep==0) |
|
| 804 | - { |
|
| 805 | - $this->logging->log("Error in display",WARN,''); |
|
| 806 | - return $display; |
|
| 807 | - } |
|
| 808 | - } |
|
| 809 | - } |
|
| 810 | - return $display; |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - /** Match rules for current trap and do action |
|
| 814 | - */ |
|
| 815 | - public function applyRules() |
|
| 816 | - { |
|
| 817 | - $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
|
| 818 | - |
|
| 819 | - if ($rules===false || count($rules)==0) |
|
| 820 | - { |
|
| 821 | - $this->logging->log('No rules found for this trap',INFO ); |
|
| 822 | - $this->trap_data['status']='unknown'; |
|
| 823 | - $this->trap_to_db=true; |
|
| 824 | - return; |
|
| 825 | - } |
|
| 826 | - //print_r($rules); |
|
| 827 | - // Evaluate all rules in sequence |
|
| 828 | - $this->trap_action=null; |
|
| 829 | - foreach ($rules as $rule) |
|
| 830 | - { |
|
| 749 | + $api = $this->getAPI(); |
|
| 750 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 751 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 752 | + if ($retcode == false) |
|
| 753 | + { |
|
| 754 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 755 | + return false; |
|
| 756 | + } |
|
| 757 | + else |
|
| 758 | + { |
|
| 759 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 760 | + return true; |
|
| 761 | + } |
|
| 762 | + } |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + public function getHostByIP($ip) |
|
| 766 | + { |
|
| 767 | + $api = $this->getAPI(); |
|
| 768 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 769 | + return $api->getHostByIP($ip); |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + /** Resolve display. |
|
| 773 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 774 | + * @param $display string |
|
| 775 | + * @return string display |
|
| 776 | + */ |
|
| 777 | + protected function applyDisplay($display) |
|
| 778 | + { |
|
| 779 | + $matches=array(); |
|
| 780 | + while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 781 | + { |
|
| 782 | + $oid=$matches[1]; |
|
| 783 | + $found=0; |
|
| 784 | + foreach($this->trap_data_ext as $val) |
|
| 785 | + { |
|
| 786 | + if ($oid == $val->oid) |
|
| 787 | + { |
|
| 788 | + $val->value=preg_replace('/"/','',$val->value); |
|
| 789 | + $rep=0; |
|
| 790 | + $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 791 | + if ($rep==0) |
|
| 792 | + { |
|
| 793 | + $this->logging->log("Error in display",WARN,''); |
|
| 794 | + return $display; |
|
| 795 | + } |
|
| 796 | + $found=1; |
|
| 797 | + break; |
|
| 798 | + } |
|
| 799 | + } |
|
| 800 | + if ($found==0) |
|
| 801 | + { |
|
| 802 | + $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 803 | + if ($rep==0) |
|
| 804 | + { |
|
| 805 | + $this->logging->log("Error in display",WARN,''); |
|
| 806 | + return $display; |
|
| 807 | + } |
|
| 808 | + } |
|
| 809 | + } |
|
| 810 | + return $display; |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + /** Match rules for current trap and do action |
|
| 814 | + */ |
|
| 815 | + public function applyRules() |
|
| 816 | + { |
|
| 817 | + $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
|
| 818 | + |
|
| 819 | + if ($rules===false || count($rules)==0) |
|
| 820 | + { |
|
| 821 | + $this->logging->log('No rules found for this trap',INFO ); |
|
| 822 | + $this->trap_data['status']='unknown'; |
|
| 823 | + $this->trap_to_db=true; |
|
| 824 | + return; |
|
| 825 | + } |
|
| 826 | + //print_r($rules); |
|
| 827 | + // Evaluate all rules in sequence |
|
| 828 | + $this->trap_action=null; |
|
| 829 | + foreach ($rules as $rule) |
|
| 830 | + { |
|
| 831 | 831 | |
| 832 | - $host_name=$rule['host_name']; |
|
| 833 | - $service_name=$rule['service_name']; |
|
| 832 | + $host_name=$rule['host_name']; |
|
| 833 | + $service_name=$rule['service_name']; |
|
| 834 | 834 | |
| 835 | - $display=$this->applyDisplay($rule['display']); |
|
| 836 | - $this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', '; |
|
| 837 | - try |
|
| 838 | - { |
|
| 839 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 840 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trap_data_ext) ; |
|
| 841 | - //->eval_rule($rule['rule']); |
|
| 835 | + $display=$this->applyDisplay($rule['display']); |
|
| 836 | + $this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', '; |
|
| 837 | + try |
|
| 838 | + { |
|
| 839 | + $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 840 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trap_data_ext) ; |
|
| 841 | + //->eval_rule($rule['rule']); |
|
| 842 | 842 | |
| 843 | - if ($evalr == true) |
|
| 844 | - { |
|
| 845 | - //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
| 846 | - $action=$rule['action_match']; |
|
| 847 | - $this->logging->log('action OK : '.$action,INFO ); |
|
| 848 | - if ($action >= 0) |
|
| 849 | - { |
|
| 850 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 851 | - { |
|
| 852 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 853 | - } |
|
| 854 | - else |
|
| 855 | - { |
|
| 856 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 857 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 858 | - } |
|
| 859 | - } |
|
| 860 | - else |
|
| 861 | - { |
|
| 862 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 863 | - } |
|
| 864 | - $this->trap_to_db=($action==-2)?false:true; |
|
| 865 | - } |
|
| 866 | - else |
|
| 867 | - { |
|
| 868 | - //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
| 843 | + if ($evalr == true) |
|
| 844 | + { |
|
| 845 | + //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
| 846 | + $action=$rule['action_match']; |
|
| 847 | + $this->logging->log('action OK : '.$action,INFO ); |
|
| 848 | + if ($action >= 0) |
|
| 849 | + { |
|
| 850 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 851 | + { |
|
| 852 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 853 | + } |
|
| 854 | + else |
|
| 855 | + { |
|
| 856 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 857 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 858 | + } |
|
| 859 | + } |
|
| 860 | + else |
|
| 861 | + { |
|
| 862 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 863 | + } |
|
| 864 | + $this->trap_to_db=($action==-2)?false:true; |
|
| 865 | + } |
|
| 866 | + else |
|
| 867 | + { |
|
| 868 | + //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
| 869 | 869 | |
| 870 | - $action=$rule['action_nomatch']; |
|
| 871 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
| 872 | - if ($action >= 0) |
|
| 873 | - { |
|
| 874 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 875 | - { |
|
| 876 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 877 | - } |
|
| 878 | - else |
|
| 879 | - { |
|
| 880 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 881 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 882 | - } |
|
| 883 | - } |
|
| 884 | - else |
|
| 885 | - { |
|
| 886 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 887 | - } |
|
| 888 | - $this->trap_to_db=($action==-2)?false:true; |
|
| 889 | - } |
|
| 890 | - // Put name in source_name |
|
| 891 | - if (!isset($this->trap_data['source_name'])) |
|
| 892 | - { |
|
| 893 | - $this->trap_data['source_name']=$rule['host_name']; |
|
| 894 | - } |
|
| 895 | - else |
|
| 896 | - { |
|
| 897 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
|
| 898 | - { // only add if not present |
|
| 899 | - $this->trap_data['source_name'].=','.$rule['host_name']; |
|
| 900 | - } |
|
| 901 | - } |
|
| 902 | - } |
|
| 903 | - catch (Exception $e) |
|
| 904 | - { |
|
| 905 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 906 | - $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 907 | - $this->trap_data['status']='error'; |
|
| 908 | - } |
|
| 870 | + $action=$rule['action_nomatch']; |
|
| 871 | + $this->logging->log('action NOK : '.$action,INFO ); |
|
| 872 | + if ($action >= 0) |
|
| 873 | + { |
|
| 874 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 875 | + { |
|
| 876 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 877 | + } |
|
| 878 | + else |
|
| 879 | + { |
|
| 880 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 881 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 882 | + } |
|
| 883 | + } |
|
| 884 | + else |
|
| 885 | + { |
|
| 886 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 887 | + } |
|
| 888 | + $this->trap_to_db=($action==-2)?false:true; |
|
| 889 | + } |
|
| 890 | + // Put name in source_name |
|
| 891 | + if (!isset($this->trap_data['source_name'])) |
|
| 892 | + { |
|
| 893 | + $this->trap_data['source_name']=$rule['host_name']; |
|
| 894 | + } |
|
| 895 | + else |
|
| 896 | + { |
|
| 897 | + if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
|
| 898 | + { // only add if not present |
|
| 899 | + $this->trap_data['source_name'].=','.$rule['host_name']; |
|
| 900 | + } |
|
| 901 | + } |
|
| 902 | + } |
|
| 903 | + catch (Exception $e) |
|
| 904 | + { |
|
| 905 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 906 | + $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 907 | + $this->trap_data['status']='error'; |
|
| 908 | + } |
|
| 909 | 909 | |
| 910 | - } |
|
| 911 | - if ($this->trap_data['status']=='error') |
|
| 912 | - { |
|
| 913 | - $this->trap_to_db=true; // Always put errors in DB for the use can see |
|
| 914 | - } |
|
| 915 | - else |
|
| 916 | - { |
|
| 917 | - $this->trap_data['status']='done'; |
|
| 918 | - } |
|
| 919 | - } |
|
| 920 | - |
|
| 921 | - /** Add Time a action to rule |
|
| 922 | - * @param string $time : time to process to insert in SQL |
|
| 923 | - */ |
|
| 924 | - public function add_rule_final($time) |
|
| 925 | - { |
|
| 926 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 927 | - if ($this->trap_action==null) |
|
| 928 | - { |
|
| 929 | - $this->trap_action='No action'; |
|
| 930 | - } |
|
| 931 | - $sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."' WHERE (id = '".$this->trap_id."');"; |
|
| 932 | - if ($db_conn->query($sql) === false) { |
|
| 933 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 934 | - } |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - /*********** UTILITIES *********************/ |
|
| 938 | - |
|
| 939 | - /** reset service to OK after time defined in rule |
|
| 940 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 941 | - * @return integer : not in use |
|
| 942 | - **/ |
|
| 943 | - public function reset_services() |
|
| 944 | - { |
|
| 945 | - // Get all services not in 'ok' state |
|
| 946 | - $sql_query="SELECT s.service_object_id, |
|
| 910 | + } |
|
| 911 | + if ($this->trap_data['status']=='error') |
|
| 912 | + { |
|
| 913 | + $this->trap_to_db=true; // Always put errors in DB for the use can see |
|
| 914 | + } |
|
| 915 | + else |
|
| 916 | + { |
|
| 917 | + $this->trap_data['status']='done'; |
|
| 918 | + } |
|
| 919 | + } |
|
| 920 | + |
|
| 921 | + /** Add Time a action to rule |
|
| 922 | + * @param string $time : time to process to insert in SQL |
|
| 923 | + */ |
|
| 924 | + public function add_rule_final($time) |
|
| 925 | + { |
|
| 926 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 927 | + if ($this->trap_action==null) |
|
| 928 | + { |
|
| 929 | + $this->trap_action='No action'; |
|
| 930 | + } |
|
| 931 | + $sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."' WHERE (id = '".$this->trap_id."');"; |
|
| 932 | + if ($db_conn->query($sql) === false) { |
|
| 933 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 934 | + } |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + /*********** UTILITIES *********************/ |
|
| 938 | + |
|
| 939 | + /** reset service to OK after time defined in rule |
|
| 940 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 941 | + * @return integer : not in use |
|
| 942 | + **/ |
|
| 943 | + public function reset_services() |
|
| 944 | + { |
|
| 945 | + // Get all services not in 'ok' state |
|
| 946 | + $sql_query="SELECT s.service_object_id, |
|
| 947 | 947 | UNIX_TIMESTAMP(s.last_check) AS last_check, |
| 948 | 948 | s.current_state as state, |
| 949 | 949 | v.name1 as host_name, |
@@ -951,43 +951,43 @@ discard block |
||
| 951 | 951 | FROM icinga_servicestatus AS s |
| 952 | 952 | LEFT JOIN icinga_objects as v ON s.service_object_id=v.object_id |
| 953 | 953 | WHERE s.current_state != 0;"; |
| 954 | - $db_conn=$this->trapsDB->db_connect_ido(); |
|
| 955 | - if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
| 956 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 957 | - return 0; |
|
| 958 | - } |
|
| 959 | - $services=$services_db->fetchAll(); |
|
| 960 | - |
|
| 961 | - // Get all rules |
|
| 962 | - $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;"; |
|
| 963 | - $db_conn2=$this->trapsDB->db_connect_trap(); |
|
| 964 | - if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
| 965 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 966 | - return 0; |
|
| 967 | - } |
|
| 968 | - $rules=$rules_db->fetchAll(); |
|
| 969 | - |
|
| 970 | - $now=date('U'); |
|
| 971 | - |
|
| 972 | - $numreset=0; |
|
| 973 | - foreach ($rules as $rule) |
|
| 974 | - { |
|
| 975 | - foreach ($services as $service) |
|
| 976 | - { |
|
| 977 | - if ($service['service_name'] == $rule['service_name'] && |
|
| 978 | - $service['host_name'] == $rule['host_name'] && |
|
| 979 | - ($service['last_check'] + $rule['revert_ok']) < $now) |
|
| 980 | - { |
|
| 981 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 982 | - $numreset++; |
|
| 983 | - } |
|
| 984 | - } |
|
| 985 | - } |
|
| 986 | - echo "\n"; |
|
| 987 | - echo $numreset . " service(s) reset to OK\n"; |
|
| 988 | - return 0; |
|
| 989 | - |
|
| 990 | - } |
|
| 954 | + $db_conn=$this->trapsDB->db_connect_ido(); |
|
| 955 | + if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
| 956 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 957 | + return 0; |
|
| 958 | + } |
|
| 959 | + $services=$services_db->fetchAll(); |
|
| 960 | + |
|
| 961 | + // Get all rules |
|
| 962 | + $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;"; |
|
| 963 | + $db_conn2=$this->trapsDB->db_connect_trap(); |
|
| 964 | + if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
| 965 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 966 | + return 0; |
|
| 967 | + } |
|
| 968 | + $rules=$rules_db->fetchAll(); |
|
| 969 | + |
|
| 970 | + $now=date('U'); |
|
| 971 | + |
|
| 972 | + $numreset=0; |
|
| 973 | + foreach ($rules as $rule) |
|
| 974 | + { |
|
| 975 | + foreach ($services as $service) |
|
| 976 | + { |
|
| 977 | + if ($service['service_name'] == $rule['service_name'] && |
|
| 978 | + $service['host_name'] == $rule['host_name'] && |
|
| 979 | + ($service['last_check'] + $rule['revert_ok']) < $now) |
|
| 980 | + { |
|
| 981 | + $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 982 | + $numreset++; |
|
| 983 | + } |
|
| 984 | + } |
|
| 985 | + } |
|
| 986 | + echo "\n"; |
|
| 987 | + echo $numreset . " service(s) reset to OK\n"; |
|
| 988 | + return 0; |
|
| 989 | + |
|
| 990 | + } |
|
| 991 | 991 | |
| 992 | 992 | |
| 993 | 993 | } |
| 994 | 994 | \ No newline at end of file |
@@ -17,519 +17,519 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Plugins |
| 19 | 19 | { |
| 20 | - /** Array of plugin objects. Keys ar plugin name |
|
| 21 | - * @var PluginTemplate[string] $pluginsList Plugins array with name as index |
|
| 22 | - * $pluginsList[plugin name]['object'] : plugin object (NULL of not loaded) |
|
| 23 | - * $pluginsList[plugin name]['allOID'] : bool true if plugin catches all oid |
|
| 24 | - * $pluginsList[plugin name]['target'] : bool true if plugin can be trap processing target |
|
| 25 | - * $pluginsList[plugin name]['enabled'] : bool true if plugin is in enabled list |
|
| 26 | - **/ |
|
| 27 | - protected $pluginsList = array(); |
|
| 20 | + /** Array of plugin objects. Keys ar plugin name |
|
| 21 | + * @var PluginTemplate[string] $pluginsList Plugins array with name as index |
|
| 22 | + * $pluginsList[plugin name]['object'] : plugin object (NULL of not loaded) |
|
| 23 | + * $pluginsList[plugin name]['allOID'] : bool true if plugin catches all oid |
|
| 24 | + * $pluginsList[plugin name]['target'] : bool true if plugin can be trap processing target |
|
| 25 | + * $pluginsList[plugin name]['enabled'] : bool true if plugin is in enabled list |
|
| 26 | + **/ |
|
| 27 | + protected $pluginsList = array(); |
|
| 28 | 28 | |
| 29 | - /** Array of functions names |
|
| 30 | - * @var array $functionList |
|
| 31 | - * $functionList[name]['plugin'] : Plugin name |
|
| 32 | - * $functionList[name]['function'] : Plugin function to call (null if plugin not loaded) |
|
| 33 | - */ |
|
| 34 | - protected $functionList=array(); |
|
| 29 | + /** Array of functions names |
|
| 30 | + * @var array $functionList |
|
| 31 | + * $functionList[name]['plugin'] : Plugin name |
|
| 32 | + * $functionList[name]['function'] : Plugin function to call (null if plugin not loaded) |
|
| 33 | + */ |
|
| 34 | + protected $functionList=array(); |
|
| 35 | 35 | |
| 36 | - /** @var string[] $enabledPlugins list of enabled plugins */ |
|
| 37 | - //public $enabledPlugins = array(); |
|
| 36 | + /** @var string[] $enabledPlugins list of enabled plugins */ |
|
| 37 | + //public $enabledPlugins = array(); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | - /** @var Logging $logClass */ |
|
| 41 | - protected $logClass; |
|
| 40 | + /** @var Logging $logClass */ |
|
| 41 | + protected $logClass; |
|
| 42 | 42 | |
| 43 | - /** @var Trap $trapClass */ |
|
| 44 | - protected $trapClass; |
|
| 43 | + /** @var Trap $trapClass */ |
|
| 44 | + protected $trapClass; |
|
| 45 | 45 | |
| 46 | - /** @var string $pluginDir */ |
|
| 47 | - protected $pluginDir; |
|
| 46 | + /** @var string $pluginDir */ |
|
| 47 | + protected $pluginDir; |
|
| 48 | 48 | |
| 49 | - /** Setup class |
|
| 50 | - * @param Trap $logClass the top trap class |
|
| 51 | - * @param string $plugin_dir optional plugin directory |
|
| 52 | - * @throws \Exception |
|
| 53 | - */ |
|
| 54 | - function __construct(Trap $trapClass,string $pluginDir='') |
|
| 55 | - { |
|
| 56 | - if ($pluginDir == '') |
|
| 57 | - { |
|
| 58 | - $this->pluginDir=dirname(__DIR__).'/Plugins'; |
|
| 59 | - } |
|
| 60 | - else |
|
| 61 | - { |
|
| 62 | - $this->pluginDir=$pluginDir; |
|
| 63 | - } |
|
| 64 | - // Set and check Logging class |
|
| 65 | - $this->trapClass=$trapClass; |
|
| 66 | - if ($this->trapClass === null) |
|
| 67 | - { |
|
| 68 | - throw new Exception('Log class not loaded into trap class'); |
|
| 69 | - } |
|
| 70 | - $this->logClass=$trapClass->logging; |
|
| 71 | - if ($this->logClass === null) |
|
| 72 | - { |
|
| 73 | - throw new Exception('Log class not loaded into trap class'); |
|
| 74 | - } |
|
| 75 | - // check DB class and get plugins list. |
|
| 76 | - if ($this->trapClass->trapsDB === null) |
|
| 77 | - { |
|
| 78 | - throw new Exception('Database class not loaded into trap class'); |
|
| 79 | - } |
|
| 80 | - $this->loadEnabledPlugins(); |
|
| 81 | - } |
|
| 49 | + /** Setup class |
|
| 50 | + * @param Trap $logClass the top trap class |
|
| 51 | + * @param string $plugin_dir optional plugin directory |
|
| 52 | + * @throws \Exception |
|
| 53 | + */ |
|
| 54 | + function __construct(Trap $trapClass,string $pluginDir='') |
|
| 55 | + { |
|
| 56 | + if ($pluginDir == '') |
|
| 57 | + { |
|
| 58 | + $this->pluginDir=dirname(__DIR__).'/Plugins'; |
|
| 59 | + } |
|
| 60 | + else |
|
| 61 | + { |
|
| 62 | + $this->pluginDir=$pluginDir; |
|
| 63 | + } |
|
| 64 | + // Set and check Logging class |
|
| 65 | + $this->trapClass=$trapClass; |
|
| 66 | + if ($this->trapClass === null) |
|
| 67 | + { |
|
| 68 | + throw new Exception('Log class not loaded into trap class'); |
|
| 69 | + } |
|
| 70 | + $this->logClass=$trapClass->logging; |
|
| 71 | + if ($this->logClass === null) |
|
| 72 | + { |
|
| 73 | + throw new Exception('Log class not loaded into trap class'); |
|
| 74 | + } |
|
| 75 | + // check DB class and get plugins list. |
|
| 76 | + if ($this->trapClass->trapsDB === null) |
|
| 77 | + { |
|
| 78 | + throw new Exception('Database class not loaded into trap class'); |
|
| 79 | + } |
|
| 80 | + $this->loadEnabledPlugins(); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Load enabled plugins from database config table. |
|
| 86 | - * Fills enabledPlugins and functionList properties |
|
| 87 | - * @throws \Exception |
|
| 88 | - */ |
|
| 89 | - private function loadEnabledPlugins() |
|
| 90 | - { |
|
| 91 | - $PluginList = $this->trapClass->trapsDB->getDBConfig('enabled_plugins'); |
|
| 84 | + /** |
|
| 85 | + * Load enabled plugins from database config table. |
|
| 86 | + * Fills enabledPlugins and functionList properties |
|
| 87 | + * @throws \Exception |
|
| 88 | + */ |
|
| 89 | + private function loadEnabledPlugins() |
|
| 90 | + { |
|
| 91 | + $PluginList = $this->trapClass->trapsDB->getDBConfig('enabled_plugins'); |
|
| 92 | 92 | |
| 93 | - if ($PluginList === null || $PluginList == '') |
|
| 94 | - { |
|
| 95 | - $this->logClass->log('No enabled plugins',DEBUG); |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - else |
|
| 99 | - { // Saved config : <plugin name>;<Catch all OID ? 1|0>;<Trap target ? 1|0>;<func 1 name>|<func 2 name>... ,<plugin2 name>.... |
|
| 100 | - $this->logClass->log('Enabled plugins = '.$PluginList,DEBUG); |
|
| 93 | + if ($PluginList === null || $PluginList == '') |
|
| 94 | + { |
|
| 95 | + $this->logClass->log('No enabled plugins',DEBUG); |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + else |
|
| 99 | + { // Saved config : <plugin name>;<Catch all OID ? 1|0>;<Trap target ? 1|0>;<func 1 name>|<func 2 name>... ,<plugin2 name>.... |
|
| 100 | + $this->logClass->log('Enabled plugins = '.$PluginList,DEBUG); |
|
| 101 | 101 | |
| 102 | - $pluginArray = explode(',', $PluginList); |
|
| 103 | - foreach ($pluginArray as $pluginElmt) |
|
| 104 | - { |
|
| 105 | - $pluginElmt = explode(';',$pluginElmt); |
|
| 106 | - if ($pluginElmt === false || count($pluginElmt) != 4) |
|
| 107 | - { |
|
| 108 | - throw new \Exception('Invalid plugin configuration : '. $PluginList ); |
|
| 109 | - } |
|
| 110 | - $pluginName=$pluginElmt[0]; |
|
| 102 | + $pluginArray = explode(',', $PluginList); |
|
| 103 | + foreach ($pluginArray as $pluginElmt) |
|
| 104 | + { |
|
| 105 | + $pluginElmt = explode(';',$pluginElmt); |
|
| 106 | + if ($pluginElmt === false || count($pluginElmt) != 4) |
|
| 107 | + { |
|
| 108 | + throw new \Exception('Invalid plugin configuration : '. $PluginList ); |
|
| 109 | + } |
|
| 110 | + $pluginName=$pluginElmt[0]; |
|
| 111 | 111 | |
| 112 | - $pluginListElmt = array(); |
|
| 113 | - $pluginListElmt['object'] = null; // class not loaded |
|
| 114 | - $pluginListElmt['allOID'] = ($pluginElmt[1]=='1') ? true : false; |
|
| 115 | - $pluginListElmt['target'] = ($pluginElmt[2]=='1') ? true : false; |
|
| 116 | - $pluginListElmt['enabled'] = true; |
|
| 112 | + $pluginListElmt = array(); |
|
| 113 | + $pluginListElmt['object'] = null; // class not loaded |
|
| 114 | + $pluginListElmt['allOID'] = ($pluginElmt[1]=='1') ? true : false; |
|
| 115 | + $pluginListElmt['target'] = ($pluginElmt[2]=='1') ? true : false; |
|
| 116 | + $pluginListElmt['enabled'] = true; |
|
| 117 | 117 | |
| 118 | - $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 118 | + $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 119 | 119 | |
| 120 | - // deal with plugin functions |
|
| 121 | - $pluginFunctions = explode('|',$pluginElmt[3]); |
|
| 122 | - if ($pluginFunctions !== false) |
|
| 123 | - { |
|
| 124 | - foreach ($pluginFunctions as $function) |
|
| 125 | - { |
|
| 126 | - $this->functionList[$function] = array( |
|
| 127 | - 'plugin' => $pluginName, |
|
| 128 | - 'function' => null |
|
| 129 | - ); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - } |
|
| 120 | + // deal with plugin functions |
|
| 121 | + $pluginFunctions = explode('|',$pluginElmt[3]); |
|
| 122 | + if ($pluginFunctions !== false) |
|
| 123 | + { |
|
| 124 | + foreach ($pluginFunctions as $function) |
|
| 125 | + { |
|
| 126 | + $this->functionList[$function] = array( |
|
| 127 | + 'plugin' => $pluginName, |
|
| 128 | + 'function' => null |
|
| 129 | + ); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - } |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Save enabled plugin array in DB config |
|
| 140 | - * @return bool true if OK, or false (error logged by DB Class) |
|
| 141 | - */ |
|
| 142 | - private function saveEnabledPlugins() |
|
| 143 | - { |
|
| 144 | - $saveString=''; |
|
| 145 | - foreach ($this->pluginsList as $name => $value) |
|
| 146 | - { |
|
| 147 | - if ($value['enabled'] == false) |
|
| 148 | - { |
|
| 149 | - continue; |
|
| 150 | - } |
|
| 151 | - $functionString=''; |
|
| 152 | - foreach ($this->functionList as $fName => $fvalue) |
|
| 153 | - { |
|
| 154 | - if ($fvalue['plugin'] != $name) |
|
| 155 | - { |
|
| 156 | - continue; |
|
| 157 | - } |
|
| 158 | - $functionString .= ($functionString == '') ? '' : '|'; // add separator if not empty |
|
| 159 | - $functionString .= $fName; |
|
| 160 | - } |
|
| 161 | - $saveString .= ($saveString == '')?'':',' ; |
|
| 138 | + /** |
|
| 139 | + * Save enabled plugin array in DB config |
|
| 140 | + * @return bool true if OK, or false (error logged by DB Class) |
|
| 141 | + */ |
|
| 142 | + private function saveEnabledPlugins() |
|
| 143 | + { |
|
| 144 | + $saveString=''; |
|
| 145 | + foreach ($this->pluginsList as $name => $value) |
|
| 146 | + { |
|
| 147 | + if ($value['enabled'] == false) |
|
| 148 | + { |
|
| 149 | + continue; |
|
| 150 | + } |
|
| 151 | + $functionString=''; |
|
| 152 | + foreach ($this->functionList as $fName => $fvalue) |
|
| 153 | + { |
|
| 154 | + if ($fvalue['plugin'] != $name) |
|
| 155 | + { |
|
| 156 | + continue; |
|
| 157 | + } |
|
| 158 | + $functionString .= ($functionString == '') ? '' : '|'; // add separator if not empty |
|
| 159 | + $functionString .= $fName; |
|
| 160 | + } |
|
| 161 | + $saveString .= ($saveString == '')?'':',' ; |
|
| 162 | 162 | |
| 163 | - $allOID = ($value['allOID'] === true) ? 1 : 0; |
|
| 164 | - $target = ($value['target'] === true) ? 1 : 0; |
|
| 165 | - $saveString .= $name . ';' . $allOID . ';' . $target . ';' . $functionString ; |
|
| 166 | - } |
|
| 167 | - $this->logClass->log('Saving : ' . $saveString,DEBUG); |
|
| 168 | - return $this->trapClass->trapsDB->setDBConfig('enabled_plugins', $saveString); |
|
| 169 | - } |
|
| 163 | + $allOID = ($value['allOID'] === true) ? 1 : 0; |
|
| 164 | + $target = ($value['target'] === true) ? 1 : 0; |
|
| 165 | + $saveString .= $name . ';' . $allOID . ';' . $target . ';' . $functionString ; |
|
| 166 | + } |
|
| 167 | + $this->logClass->log('Saving : ' . $saveString,DEBUG); |
|
| 168 | + return $this->trapClass->trapsDB->setDBConfig('enabled_plugins', $saveString); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** Get enabled plugin list by name |
|
| 172 | - * @return array[string] |
|
| 173 | - */ |
|
| 174 | - public function getEnabledPlugins() : array |
|
| 175 | - { |
|
| 176 | - $retArray=array(); |
|
| 177 | - foreach ($this->pluginsList as $name => $value) |
|
| 178 | - { |
|
| 179 | - if ($value['enabled'] == true) |
|
| 180 | - { |
|
| 181 | - array_push($retArray,$name); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - return $retArray; |
|
| 185 | - } |
|
| 171 | + /** Get enabled plugin list by name |
|
| 172 | + * @return array[string] |
|
| 173 | + */ |
|
| 174 | + public function getEnabledPlugins() : array |
|
| 175 | + { |
|
| 176 | + $retArray=array(); |
|
| 177 | + foreach ($this->pluginsList as $name => $value) |
|
| 178 | + { |
|
| 179 | + if ($value['enabled'] == true) |
|
| 180 | + { |
|
| 181 | + array_push($retArray,$name); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + return $retArray; |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** Enable plugin (enabling an enabled plugin is OK, same for disabled). |
|
| 188 | - * and save in DB config |
|
| 189 | - * @param string $pluginName |
|
| 190 | - * @param bool $enabled true to enable, false to disable |
|
| 191 | - * @return bool true if OK, or false (error logged) |
|
| 192 | - */ |
|
| 193 | - public function enablePlugin(string $pluginName,bool $enabled) |
|
| 194 | - { |
|
| 195 | - if ($enabled == false) |
|
| 196 | - { |
|
| 197 | - // If plugin is defined set to disable |
|
| 198 | - if ( isset($this->pluginsList[$pluginName])) |
|
| 199 | - { |
|
| 200 | - $this->pluginsList[$pluginName]['enabled'] = false; |
|
| 201 | - } |
|
| 202 | - return $this->saveEnabledPlugins(); |
|
| 203 | - } |
|
| 204 | - // Check if plugin is loaded / exists |
|
| 205 | - if ( ! isset($this->pluginsList[$pluginName]) || |
|
| 206 | - $this->pluginsList[$pluginName]['object'] === null) |
|
| 207 | - { |
|
| 208 | - try { |
|
| 209 | - $this->registerPlugin($pluginName); |
|
| 210 | - } catch (Exception $e) { |
|
| 211 | - $this->logClass->log('Cannot enable plugin : ' . $e->getMessage()); |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - $this->pluginsList[$pluginName]['enabled'] = true; |
|
| 216 | - // save in DB and return |
|
| 217 | - return $this->saveEnabledPlugins(); |
|
| 218 | - } |
|
| 187 | + /** Enable plugin (enabling an enabled plugin is OK, same for disabled). |
|
| 188 | + * and save in DB config |
|
| 189 | + * @param string $pluginName |
|
| 190 | + * @param bool $enabled true to enable, false to disable |
|
| 191 | + * @return bool true if OK, or false (error logged) |
|
| 192 | + */ |
|
| 193 | + public function enablePlugin(string $pluginName,bool $enabled) |
|
| 194 | + { |
|
| 195 | + if ($enabled == false) |
|
| 196 | + { |
|
| 197 | + // If plugin is defined set to disable |
|
| 198 | + if ( isset($this->pluginsList[$pluginName])) |
|
| 199 | + { |
|
| 200 | + $this->pluginsList[$pluginName]['enabled'] = false; |
|
| 201 | + } |
|
| 202 | + return $this->saveEnabledPlugins(); |
|
| 203 | + } |
|
| 204 | + // Check if plugin is loaded / exists |
|
| 205 | + if ( ! isset($this->pluginsList[$pluginName]) || |
|
| 206 | + $this->pluginsList[$pluginName]['object'] === null) |
|
| 207 | + { |
|
| 208 | + try { |
|
| 209 | + $this->registerPlugin($pluginName); |
|
| 210 | + } catch (Exception $e) { |
|
| 211 | + $this->logClass->log('Cannot enable plugin : ' . $e->getMessage()); |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + $this->pluginsList[$pluginName]['enabled'] = true; |
|
| 216 | + // save in DB and return |
|
| 217 | + return $this->saveEnabledPlugins(); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Destroy plugin objects and reload them with new enabled list. |
|
| 222 | - * TODO : Code this function (ref DAEMON_MODE) |
|
| 223 | - */ |
|
| 224 | - public function reloadAllPlugins() |
|
| 225 | - { |
|
| 226 | - return; |
|
| 227 | - } |
|
| 220 | + /** |
|
| 221 | + * Destroy plugin objects and reload them with new enabled list. |
|
| 222 | + * TODO : Code this function (ref DAEMON_MODE) |
|
| 223 | + */ |
|
| 224 | + public function reloadAllPlugins() |
|
| 225 | + { |
|
| 226 | + return; |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** Load plugin by name. Create entry if not in $pluginsList |
|
| 230 | - * @param string $pluginName Plugin name to load |
|
| 231 | - * @return bool true if created, false if already loaded |
|
| 232 | - * @throws Exception on error loading plugin |
|
| 233 | - */ |
|
| 234 | - public function registerPlugin(string $pluginName) |
|
| 235 | - { |
|
| 236 | - if ( ! isset($this->pluginsList[$pluginName]) ) // Plugin isn't enable, create entry |
|
| 237 | - { |
|
| 238 | - $pluginListElmt = array(); |
|
| 239 | - $pluginListElmt['object'] = null; // class not loaded |
|
| 240 | - $pluginListElmt['enabled'] = false; |
|
| 241 | - $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 242 | - } |
|
| 229 | + /** Load plugin by name. Create entry if not in $pluginsList |
|
| 230 | + * @param string $pluginName Plugin name to load |
|
| 231 | + * @return bool true if created, false if already loaded |
|
| 232 | + * @throws Exception on error loading plugin |
|
| 233 | + */ |
|
| 234 | + public function registerPlugin(string $pluginName) |
|
| 235 | + { |
|
| 236 | + if ( ! isset($this->pluginsList[$pluginName]) ) // Plugin isn't enable, create entry |
|
| 237 | + { |
|
| 238 | + $pluginListElmt = array(); |
|
| 239 | + $pluginListElmt['object'] = null; // class not loaded |
|
| 240 | + $pluginListElmt['enabled'] = false; |
|
| 241 | + $this->pluginsList[$pluginName] = $pluginListElmt; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - if ($this->pluginsList[$pluginName]['object'] !== null) |
|
| 245 | - { |
|
| 246 | - return false; |
|
| 247 | - } |
|
| 248 | - try { |
|
| 249 | - // Include plugin file |
|
| 250 | - include_once($this->pluginDir.'/' . $pluginName . '.php'); |
|
| 244 | + if ($this->pluginsList[$pluginName]['object'] !== null) |
|
| 245 | + { |
|
| 246 | + return false; |
|
| 247 | + } |
|
| 248 | + try { |
|
| 249 | + // Include plugin file |
|
| 250 | + include_once($this->pluginDir.'/' . $pluginName . '.php'); |
|
| 251 | 251 | |
| 252 | - // Create full class name with namespace |
|
| 253 | - $pluginClassName = __NAMESPACE__ . '\\Plugins\\' . $pluginName; |
|
| 252 | + // Create full class name with namespace |
|
| 253 | + $pluginClassName = __NAMESPACE__ . '\\Plugins\\' . $pluginName; |
|
| 254 | 254 | |
| 255 | - // Create class |
|
| 256 | - $newClass = new $pluginClassName(); |
|
| 255 | + // Create class |
|
| 256 | + $newClass = new $pluginClassName(); |
|
| 257 | 257 | |
| 258 | - // Set logging |
|
| 259 | - $newClass->setLoggingClass($this->logClass); |
|
| 258 | + // Set logging |
|
| 259 | + $newClass->setLoggingClass($this->logClass); |
|
| 260 | 260 | |
| 261 | - // Add in plugin array |
|
| 262 | - $this->pluginsList[$pluginName]['object']=$newClass; |
|
| 263 | - $this->pluginsList[$pluginName]['allOID']=$newClass->catchAllTraps; |
|
| 264 | - $this->pluginsList[$pluginName]['target']=$newClass->processTraps; |
|
| 261 | + // Add in plugin array |
|
| 262 | + $this->pluginsList[$pluginName]['object']=$newClass; |
|
| 263 | + $this->pluginsList[$pluginName]['allOID']=$newClass->catchAllTraps; |
|
| 264 | + $this->pluginsList[$pluginName]['target']=$newClass->processTraps; |
|
| 265 | 265 | |
| 266 | - // Delete old functions |
|
| 267 | - foreach ($this->functionList as $fname => $fvalue) |
|
| 268 | - { |
|
| 269 | - if ($fvalue['plugin'] == $pluginName) |
|
| 270 | - { |
|
| 271 | - unset($this->functionList[$fname]); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - // Add functions |
|
| 275 | - foreach ($newClass->functions as $fname => $function) |
|
| 276 | - { |
|
| 277 | - if (isset($this->functionList[$fname])) |
|
| 278 | - { |
|
| 279 | - if ($this->functionList[$fname]['plugin'] != $pluginName ) |
|
| 280 | - { |
|
| 281 | - throw new Exception('Duplicate function name '.$fname . ' in ' |
|
| 282 | - . $pluginName . ' and ' . $this->functionList[$fname]['plugin']); |
|
| 283 | - } |
|
| 266 | + // Delete old functions |
|
| 267 | + foreach ($this->functionList as $fname => $fvalue) |
|
| 268 | + { |
|
| 269 | + if ($fvalue['plugin'] == $pluginName) |
|
| 270 | + { |
|
| 271 | + unset($this->functionList[$fname]); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + // Add functions |
|
| 275 | + foreach ($newClass->functions as $fname => $function) |
|
| 276 | + { |
|
| 277 | + if (isset($this->functionList[$fname])) |
|
| 278 | + { |
|
| 279 | + if ($this->functionList[$fname]['plugin'] != $pluginName ) |
|
| 280 | + { |
|
| 281 | + throw new Exception('Duplicate function name '.$fname . ' in ' |
|
| 282 | + . $pluginName . ' and ' . $this->functionList[$fname]['plugin']); |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - } |
|
| 286 | - else |
|
| 287 | - { |
|
| 288 | - $this->functionList[$fname]=array(); |
|
| 289 | - $this->functionList[$fname]['plugin'] = $pluginName; |
|
| 290 | - } |
|
| 291 | - $this->functionList[$fname]['function']=$function['function']; |
|
| 292 | - } |
|
| 293 | - $this->logClass->log('Registered plugin '.$pluginName,DEBUG); |
|
| 285 | + } |
|
| 286 | + else |
|
| 287 | + { |
|
| 288 | + $this->functionList[$fname]=array(); |
|
| 289 | + $this->functionList[$fname]['plugin'] = $pluginName; |
|
| 290 | + } |
|
| 291 | + $this->functionList[$fname]['function']=$function['function']; |
|
| 292 | + } |
|
| 293 | + $this->logClass->log('Registered plugin '.$pluginName,DEBUG); |
|
| 294 | 294 | |
| 295 | - } catch (Exception $e) { |
|
| 296 | - unset($this->pluginsList[$pluginName]); |
|
| 297 | - $errorMessage = "Error registering plugin $pluginName : ".$e->getMessage(); |
|
| 298 | - $this->logClass->log($errorMessage,WARN); |
|
| 299 | - // Disable the plugin |
|
| 300 | - $this->enablePlugin($pluginName, false); |
|
| 301 | - throw new \Exception($errorMessage); |
|
| 302 | - } catch (Throwable $t) { |
|
| 303 | - unset($this->pluginsList[$pluginName]); |
|
| 304 | - $errorMessage = $t->getMessage() . ' in file ' . $t->getFile() . ' line ' . $t->getLine(); |
|
| 305 | - $this->logClass->log($errorMessage,WARN); |
|
| 306 | - // Disable the plugin |
|
| 307 | - $this->enablePlugin($pluginName, false); |
|
| 308 | - throw new \Exception($errorMessage); |
|
| 309 | - } |
|
| 310 | - return true; |
|
| 311 | - } |
|
| 295 | + } catch (Exception $e) { |
|
| 296 | + unset($this->pluginsList[$pluginName]); |
|
| 297 | + $errorMessage = "Error registering plugin $pluginName : ".$e->getMessage(); |
|
| 298 | + $this->logClass->log($errorMessage,WARN); |
|
| 299 | + // Disable the plugin |
|
| 300 | + $this->enablePlugin($pluginName, false); |
|
| 301 | + throw new \Exception($errorMessage); |
|
| 302 | + } catch (Throwable $t) { |
|
| 303 | + unset($this->pluginsList[$pluginName]); |
|
| 304 | + $errorMessage = $t->getMessage() . ' in file ' . $t->getFile() . ' line ' . $t->getLine(); |
|
| 305 | + $this->logClass->log($errorMessage,WARN); |
|
| 306 | + // Disable the plugin |
|
| 307 | + $this->enablePlugin($pluginName, false); |
|
| 308 | + throw new \Exception($errorMessage); |
|
| 309 | + } |
|
| 310 | + return true; |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - /** Registers all plugins (check=false) or only those with name present in array (check=true) |
|
| 314 | - * @param bool $checkEnabled Check if plugin is enabled before loading it |
|
| 315 | - * @return string Errors encountered while registering plugins |
|
| 316 | - */ |
|
| 317 | - public function registerAllPlugins(bool $checkEnabled=true) |
|
| 318 | - { |
|
| 319 | - $retDisplay=''; |
|
| 320 | - // First load enabled plugins |
|
| 321 | - foreach (array_keys($this->pluginsList) as $pluginName) |
|
| 322 | - { |
|
| 323 | - try { |
|
| 324 | - $this->registerPlugin($pluginName); |
|
| 325 | - } catch (Exception $e) { |
|
| 326 | - $retDisplay .= $e->getMessage() . ' / '; |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - if ($checkEnabled === false) // Load all php files in plugin dir |
|
| 330 | - { |
|
| 331 | - foreach (glob($this->pluginDir."/*.php") as $filename) |
|
| 332 | - { |
|
| 333 | - $pluginName=basename($filename,'.php'); |
|
| 334 | - if (!preg_match('/^[a-zA-Z0-9]+$/',$pluginName)) |
|
| 335 | - { |
|
| 336 | - $this->logClass->log("Invalid plugin name : ".$pluginName, WARN); |
|
| 337 | - $retDisplay .= "Invalid plugin name : ".$pluginName . " / "; |
|
| 338 | - break; |
|
| 339 | - } |
|
| 340 | - try { // Already registerd plugin will simply return false |
|
| 341 | - $this->registerPlugin($pluginName); |
|
| 342 | - } catch (Exception $e) { |
|
| 343 | - $retDisplay .= $e->getMessage() . ' / '; |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - } |
|
| 313 | + /** Registers all plugins (check=false) or only those with name present in array (check=true) |
|
| 314 | + * @param bool $checkEnabled Check if plugin is enabled before loading it |
|
| 315 | + * @return string Errors encountered while registering plugins |
|
| 316 | + */ |
|
| 317 | + public function registerAllPlugins(bool $checkEnabled=true) |
|
| 318 | + { |
|
| 319 | + $retDisplay=''; |
|
| 320 | + // First load enabled plugins |
|
| 321 | + foreach (array_keys($this->pluginsList) as $pluginName) |
|
| 322 | + { |
|
| 323 | + try { |
|
| 324 | + $this->registerPlugin($pluginName); |
|
| 325 | + } catch (Exception $e) { |
|
| 326 | + $retDisplay .= $e->getMessage() . ' / '; |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + if ($checkEnabled === false) // Load all php files in plugin dir |
|
| 330 | + { |
|
| 331 | + foreach (glob($this->pluginDir."/*.php") as $filename) |
|
| 332 | + { |
|
| 333 | + $pluginName=basename($filename,'.php'); |
|
| 334 | + if (!preg_match('/^[a-zA-Z0-9]+$/',$pluginName)) |
|
| 335 | + { |
|
| 336 | + $this->logClass->log("Invalid plugin name : ".$pluginName, WARN); |
|
| 337 | + $retDisplay .= "Invalid plugin name : ".$pluginName . " / "; |
|
| 338 | + break; |
|
| 339 | + } |
|
| 340 | + try { // Already registerd plugin will simply return false |
|
| 341 | + $this->registerPlugin($pluginName); |
|
| 342 | + } catch (Exception $e) { |
|
| 343 | + $retDisplay .= $e->getMessage() . ' / '; |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | 347 | |
| 348 | - if ($retDisplay == '') |
|
| 349 | - { |
|
| 350 | - return 'All plugins loaded OK'; |
|
| 351 | - } |
|
| 352 | - else |
|
| 353 | - { |
|
| 354 | - return $retDisplay; |
|
| 355 | - } |
|
| 356 | - } |
|
| 348 | + if ($retDisplay == '') |
|
| 349 | + { |
|
| 350 | + return 'All plugins loaded OK'; |
|
| 351 | + } |
|
| 352 | + else |
|
| 353 | + { |
|
| 354 | + return $retDisplay; |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Returns array of name of loaded plugins |
|
| 360 | - * @return array |
|
| 361 | - */ |
|
| 362 | - public function pluginList() : array |
|
| 363 | - { |
|
| 364 | - return array_keys($this->pluginsList); |
|
| 365 | - } |
|
| 358 | + /** |
|
| 359 | + * Returns array of name of loaded plugins |
|
| 360 | + * @return array |
|
| 361 | + */ |
|
| 362 | + public function pluginList() : array |
|
| 363 | + { |
|
| 364 | + return array_keys($this->pluginsList); |
|
| 365 | + } |
|
| 366 | 366 | |
| 367 | - /** |
|
| 368 | - * Get plugin details |
|
| 369 | - * @param string $name name of plugins |
|
| 370 | - * @return boolean|stdClass result as stdClass or false if plugin not found. |
|
| 371 | - * @throws \Exception if registering is not possible |
|
| 372 | - */ |
|
| 373 | - public function pluginDetails(string $name) |
|
| 374 | - { |
|
| 375 | - if (!array_key_exists($name, $this->pluginsList)) |
|
| 376 | - { |
|
| 377 | - return false; |
|
| 378 | - } |
|
| 379 | - if ($this->pluginsList[$name]['object'] === null) |
|
| 380 | - { |
|
| 381 | - $this->registerPlugin($name); // can throw exception handled by caller |
|
| 382 | - } |
|
| 383 | - $retObj = new stdClass(); |
|
| 384 | - $retObj->name = $name; |
|
| 385 | - $retObj->catchAllTraps = $this->pluginsList[$name]['allOID']; |
|
| 386 | - $retObj->processTraps = $this->pluginsList[$name]['target']; |
|
| 387 | - $retObj->description = $this->pluginsList[$name]['object']->description; |
|
| 388 | - $functions=array(); |
|
| 389 | - foreach ($this->functionList as $fName => $func) |
|
| 390 | - { |
|
| 391 | - if ($func['plugin'] == $name) |
|
| 392 | - { |
|
| 393 | - array_push($functions,$fName); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - $retObj->funcArray=$functions; |
|
| 397 | - return $retObj; |
|
| 398 | - } |
|
| 367 | + /** |
|
| 368 | + * Get plugin details |
|
| 369 | + * @param string $name name of plugins |
|
| 370 | + * @return boolean|stdClass result as stdClass or false if plugin not found. |
|
| 371 | + * @throws \Exception if registering is not possible |
|
| 372 | + */ |
|
| 373 | + public function pluginDetails(string $name) |
|
| 374 | + { |
|
| 375 | + if (!array_key_exists($name, $this->pluginsList)) |
|
| 376 | + { |
|
| 377 | + return false; |
|
| 378 | + } |
|
| 379 | + if ($this->pluginsList[$name]['object'] === null) |
|
| 380 | + { |
|
| 381 | + $this->registerPlugin($name); // can throw exception handled by caller |
|
| 382 | + } |
|
| 383 | + $retObj = new stdClass(); |
|
| 384 | + $retObj->name = $name; |
|
| 385 | + $retObj->catchAllTraps = $this->pluginsList[$name]['allOID']; |
|
| 386 | + $retObj->processTraps = $this->pluginsList[$name]['target']; |
|
| 387 | + $retObj->description = $this->pluginsList[$name]['object']->description; |
|
| 388 | + $functions=array(); |
|
| 389 | + foreach ($this->functionList as $fName => $func) |
|
| 390 | + { |
|
| 391 | + if ($func['plugin'] == $name) |
|
| 392 | + { |
|
| 393 | + array_push($functions,$fName); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + $retObj->funcArray=$functions; |
|
| 397 | + return $retObj; |
|
| 398 | + } |
|
| 399 | 399 | |
| 400 | - /** |
|
| 401 | - * Get plugin name from function name |
|
| 402 | - * @param string $funcName |
|
| 403 | - * @param string $pluginName |
|
| 404 | - * @return boolean returns plugin object of false; |
|
| 405 | - */ |
|
| 406 | - public function getFunction($funcName,&$pluginName) |
|
| 407 | - { |
|
| 408 | - if (! isset($this->functionList[$funcName]) ) |
|
| 409 | - { |
|
| 410 | - return false; |
|
| 411 | - } |
|
| 412 | - $pluginName = $this->functionList[$funcName]['plugin']; |
|
| 413 | - return true; |
|
| 414 | - } |
|
| 400 | + /** |
|
| 401 | + * Get plugin name from function name |
|
| 402 | + * @param string $funcName |
|
| 403 | + * @param string $pluginName |
|
| 404 | + * @return boolean returns plugin object of false; |
|
| 405 | + */ |
|
| 406 | + public function getFunction($funcName,&$pluginName) |
|
| 407 | + { |
|
| 408 | + if (! isset($this->functionList[$funcName]) ) |
|
| 409 | + { |
|
| 410 | + return false; |
|
| 411 | + } |
|
| 412 | + $pluginName = $this->functionList[$funcName]['plugin']; |
|
| 413 | + return true; |
|
| 414 | + } |
|
| 415 | 415 | |
| 416 | - /** |
|
| 417 | - * Get functions params and description |
|
| 418 | - * @param string $funcName |
|
| 419 | - * @return boolean|stdClass false if not found or object (name,params,description) |
|
| 420 | - * @throws \Exception if registering is not possible |
|
| 421 | - */ |
|
| 422 | - public function getFunctionDetails($funcName) |
|
| 423 | - { |
|
| 424 | - if (! isset($this->functionList[$funcName]) ) |
|
| 425 | - { |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 429 | - $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 430 | - if ($plugin === null) |
|
| 431 | - { |
|
| 432 | - $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 433 | - } |
|
| 434 | - $retObj = new stdClass(); |
|
| 435 | - $retObj->name = $funcName; |
|
| 436 | - $retObj->plugin = $pluginName; |
|
| 437 | - $retObj->params = $plugin->functions[$funcName]['params']; |
|
| 438 | - $retObj->description = $plugin->functions[$funcName]['description']; |
|
| 439 | - return $retObj; |
|
| 440 | - } |
|
| 416 | + /** |
|
| 417 | + * Get functions params and description |
|
| 418 | + * @param string $funcName |
|
| 419 | + * @return boolean|stdClass false if not found or object (name,params,description) |
|
| 420 | + * @throws \Exception if registering is not possible |
|
| 421 | + */ |
|
| 422 | + public function getFunctionDetails($funcName) |
|
| 423 | + { |
|
| 424 | + if (! isset($this->functionList[$funcName]) ) |
|
| 425 | + { |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 429 | + $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 430 | + if ($plugin === null) |
|
| 431 | + { |
|
| 432 | + $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 433 | + } |
|
| 434 | + $retObj = new stdClass(); |
|
| 435 | + $retObj->name = $funcName; |
|
| 436 | + $retObj->plugin = $pluginName; |
|
| 437 | + $retObj->params = $plugin->functions[$funcName]['params']; |
|
| 438 | + $retObj->description = $plugin->functions[$funcName]['description']; |
|
| 439 | + return $retObj; |
|
| 440 | + } |
|
| 441 | 441 | |
| 442 | - /** |
|
| 443 | - * Evaluate function with parameters |
|
| 444 | - * @param string $funcName |
|
| 445 | - * @param mixed $params |
|
| 446 | - * @throws Exception |
|
| 447 | - * @return bool |
|
| 448 | - */ |
|
| 449 | - public function getFunctionEval(string $funcName,$params) : bool |
|
| 450 | - { |
|
| 451 | - if (! isset($this->functionList[$funcName]) ) |
|
| 452 | - { |
|
| 453 | - throw new Exception($funcName . ' not found.'); |
|
| 454 | - } |
|
| 455 | - $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 456 | - $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 442 | + /** |
|
| 443 | + * Evaluate function with parameters |
|
| 444 | + * @param string $funcName |
|
| 445 | + * @param mixed $params |
|
| 446 | + * @throws Exception |
|
| 447 | + * @return bool |
|
| 448 | + */ |
|
| 449 | + public function getFunctionEval(string $funcName,$params) : bool |
|
| 450 | + { |
|
| 451 | + if (! isset($this->functionList[$funcName]) ) |
|
| 452 | + { |
|
| 453 | + throw new Exception($funcName . ' not found.'); |
|
| 454 | + } |
|
| 455 | + $pluginName = $this->functionList[$funcName]['plugin']; // plugin name |
|
| 456 | + $plugin = $this->pluginsList[$pluginName]['object']; // plugin object |
|
| 457 | 457 | |
| 458 | - if ($plugin === null) |
|
| 459 | - { |
|
| 460 | - $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 461 | - $plugin = $this->pluginsList[$pluginName]['object']; |
|
| 462 | - } |
|
| 458 | + if ($plugin === null) |
|
| 459 | + { |
|
| 460 | + $this->registerPlugin($pluginName); // can throw exception handled by caller |
|
| 461 | + $plugin = $this->pluginsList[$pluginName]['object']; |
|
| 462 | + } |
|
| 463 | 463 | |
| 464 | - $propertyName = $this->functionList[$funcName]['function']; |
|
| 465 | - $this->logClass->log('Using property '. $propertyName . ' of class : '.$pluginName,DEBUG); |
|
| 464 | + $propertyName = $this->functionList[$funcName]['function']; |
|
| 465 | + $this->logClass->log('Using property '. $propertyName . ' of class : '.$pluginName,DEBUG); |
|
| 466 | 466 | |
| 467 | - return $plugin->{$propertyName}($params); |
|
| 468 | - } |
|
| 467 | + return $plugin->{$propertyName}($params); |
|
| 468 | + } |
|
| 469 | 469 | |
| 470 | - public function evaluateFunctionString(string $functionString) : bool |
|
| 471 | - { |
|
| 472 | - $matches=array(); |
|
| 473 | - // Cleanup spaces |
|
| 474 | - $functionString = $this->trapClass->ruleClass->eval_cleanup($functionString); |
|
| 475 | - $this->logClass->log('eval cleanup : '.$functionString,DEBUG); |
|
| 470 | + public function evaluateFunctionString(string $functionString) : bool |
|
| 471 | + { |
|
| 472 | + $matches=array(); |
|
| 473 | + // Cleanup spaces |
|
| 474 | + $functionString = $this->trapClass->ruleClass->eval_cleanup($functionString); |
|
| 475 | + $this->logClass->log('eval cleanup : '.$functionString,DEBUG); |
|
| 476 | 476 | |
| 477 | - // Match function call |
|
| 478 | - $num=preg_match('/^__([a-zA-Z0-9]+)\((.+)\)$/', $functionString , $matches); |
|
| 479 | - if ($num !=1) |
|
| 480 | - { |
|
| 481 | - throw new \ErrorException('Function syntax error : ' . $functionString ); |
|
| 482 | - } |
|
| 483 | - $this->logClass->log('Got function : '. $matches[1] . ', params : '.$matches[2],DEBUG); |
|
| 484 | - $funcName=$matches[1]; |
|
| 477 | + // Match function call |
|
| 478 | + $num=preg_match('/^__([a-zA-Z0-9]+)\((.+)\)$/', $functionString , $matches); |
|
| 479 | + if ($num !=1) |
|
| 480 | + { |
|
| 481 | + throw new \ErrorException('Function syntax error : ' . $functionString ); |
|
| 482 | + } |
|
| 483 | + $this->logClass->log('Got function : '. $matches[1] . ', params : '.$matches[2],DEBUG); |
|
| 484 | + $funcName=$matches[1]; |
|
| 485 | 485 | |
| 486 | - // Get parameters comma separated |
|
| 487 | - $funcParams=str_getcsv($matches[2],',','"',"\\"); |
|
| 488 | - $this->logClass->log('Function params : ' . print_r($funcParams,true),DEBUG); |
|
| 486 | + // Get parameters comma separated |
|
| 487 | + $funcParams=str_getcsv($matches[2],',','"',"\\"); |
|
| 488 | + $this->logClass->log('Function params : ' . print_r($funcParams,true),DEBUG); |
|
| 489 | 489 | |
| 490 | - // return evaluation |
|
| 491 | - return $this->getFunctionEval($funcName, $funcParams); |
|
| 490 | + // return evaluation |
|
| 491 | + return $this->getFunctionEval($funcName, $funcParams); |
|
| 492 | 492 | |
| 493 | - } |
|
| 493 | + } |
|
| 494 | 494 | |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | abstract class PluginTemplate |
| 498 | 498 | { |
| 499 | 499 | |
| 500 | - /** @var Logging $loggingClass */ |
|
| 501 | - private $loggingClass; |
|
| 500 | + /** @var Logging $loggingClass */ |
|
| 501 | + private $loggingClass; |
|
| 502 | 502 | |
| 503 | - /** @var string $name Name of plugin */ |
|
| 504 | - public $name; |
|
| 503 | + /** @var string $name Name of plugin */ |
|
| 504 | + public $name; |
|
| 505 | 505 | |
| 506 | - /** @var string $description Description of plugin */ |
|
| 507 | - public $description='Default plugin description'; |
|
| 506 | + /** @var string $description Description of plugin */ |
|
| 507 | + public $description='Default plugin description'; |
|
| 508 | 508 | |
| 509 | - /** @var array[string] $functions Functions of this plugin for rule eval*/ |
|
| 510 | - public $functions=array(); |
|
| 509 | + /** @var array[string] $functions Functions of this plugin for rule eval*/ |
|
| 510 | + public $functions=array(); |
|
| 511 | 511 | |
| 512 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
| 513 | - public $catchAllTraps=false; |
|
| 512 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
| 513 | + public $catchAllTraps=false; |
|
| 514 | 514 | |
| 515 | - /** @var boolean $processTraps Set to true if plugins can handle traps */ |
|
| 516 | - public $processTraps=false; |
|
| 515 | + /** @var boolean $processTraps Set to true if plugins can handle traps */ |
|
| 516 | + public $processTraps=false; |
|
| 517 | 517 | |
| 518 | - /** |
|
| 519 | - * @param \Trapdirector\Logging $loggingClass |
|
| 520 | - */ |
|
| 521 | - public function setLoggingClass($loggingClass) |
|
| 522 | - { |
|
| 523 | - $this->loggingClass = $loggingClass; |
|
| 524 | - } |
|
| 518 | + /** |
|
| 519 | + * @param \Trapdirector\Logging $loggingClass |
|
| 520 | + */ |
|
| 521 | + public function setLoggingClass($loggingClass) |
|
| 522 | + { |
|
| 523 | + $this->loggingClass = $loggingClass; |
|
| 524 | + } |
|
| 525 | 525 | |
| 526 | - /** |
|
| 527 | - * |
|
| 528 | - * @param string $message |
|
| 529 | - * @param int $level DEBUG/INFO/WARN/CRIT |
|
| 530 | - */ |
|
| 531 | - public function log($message,$level) |
|
| 532 | - { |
|
| 533 | - $this->loggingClass->log('[ '.get_class($this).'] '. $message, $level); |
|
| 534 | - } |
|
| 526 | + /** |
|
| 527 | + * |
|
| 528 | + * @param string $message |
|
| 529 | + * @param int $level DEBUG/INFO/WARN/CRIT |
|
| 530 | + */ |
|
| 531 | + public function log($message,$level) |
|
| 532 | + { |
|
| 533 | + $this->loggingClass->log('[ '.get_class($this).'] '. $message, $level); |
|
| 534 | + } |
|
| 535 | 535 | } |
| 536 | 536 | \ No newline at end of file |
@@ -9,383 +9,383 @@ |
||
| 9 | 9 | class Database |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - // Databases |
|
| 13 | - /** @var \PDO $trapDB trap database */ |
|
| 14 | - protected $trapDB=null; |
|
| 15 | - protected $idoDB=null; //< ido database |
|
| 16 | - public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 17 | - public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 12 | + // Databases |
|
| 13 | + /** @var \PDO $trapDB trap database */ |
|
| 14 | + protected $trapDB=null; |
|
| 15 | + protected $idoDB=null; //< ido database |
|
| 16 | + public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 17 | + public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 18 | 18 | |
| 19 | - protected $trapDSN; //< trap database connection params |
|
| 20 | - protected $trapUsername; //< trap database connection params |
|
| 21 | - protected $trapPass; //< trap database connection params |
|
| 22 | - public $dbPrefix; //< database tables prefix |
|
| 19 | + protected $trapDSN; //< trap database connection params |
|
| 20 | + protected $trapUsername; //< trap database connection params |
|
| 21 | + protected $trapPass; //< trap database connection params |
|
| 22 | + public $dbPrefix; //< database tables prefix |
|
| 23 | 23 | |
| 24 | - protected $idoSet; //< bool true is ido database set |
|
| 25 | - protected $idoDSN; //< trap database connection params |
|
| 26 | - protected $idoUsername; //< trap database connection params |
|
| 27 | - protected $idoPass; //< trap database connection params |
|
| 24 | + protected $idoSet; //< bool true is ido database set |
|
| 25 | + protected $idoDSN; //< trap database connection params |
|
| 26 | + protected $idoUsername; //< trap database connection params |
|
| 27 | + protected $idoPass; //< trap database connection params |
|
| 28 | 28 | |
| 29 | - // Logging function |
|
| 29 | + // Logging function |
|
| 30 | 30 | |
| 31 | - protected $logging; //< logging class |
|
| 31 | + protected $logging; //< logging class |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param Logging $logClass : where to log |
|
| 35 | - * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 36 | - */ |
|
| 37 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
| 38 | - { |
|
| 39 | - $this->logging=$logClass; |
|
| 40 | - $this->dbPrefix=$dbPrefix; |
|
| 33 | + /** |
|
| 34 | + * @param Logging $logClass : where to log |
|
| 35 | + * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 36 | + */ |
|
| 37 | + function __construct($logClass,$dbParam,$dbPrefix) |
|
| 38 | + { |
|
| 39 | + $this->logging=$logClass; |
|
| 40 | + $this->dbPrefix=$dbPrefix; |
|
| 41 | 41 | |
| 42 | - $this->trapDSN=$this->setupDSN($dbParam); |
|
| 43 | - $this->trapUsername = $dbParam['username']; |
|
| 44 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 45 | - $this->trapDBType=$dbParam['db']; |
|
| 46 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 47 | - $this->db_connect_trap(); |
|
| 42 | + $this->trapDSN=$this->setupDSN($dbParam); |
|
| 43 | + $this->trapUsername = $dbParam['username']; |
|
| 44 | + $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 45 | + $this->trapDBType=$dbParam['db']; |
|
| 46 | + $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 47 | + $this->db_connect_trap(); |
|
| 48 | 48 | |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Setup and connect to IDO database |
|
| 53 | - * @param array $dbParam : array of named params |
|
| 54 | - */ |
|
| 55 | - public function setupIDO($dbParam) |
|
| 56 | - { |
|
| 57 | - $this->idoDSN=$this->setupDSN($dbParam); |
|
| 58 | - $this->idoUsername = $dbParam['username']; |
|
| 59 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 60 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 61 | - $this->idoDBType=$dbParam['db']; |
|
| 62 | - $this->db_connect_ido(); |
|
| 63 | - } |
|
| 51 | + /** |
|
| 52 | + * Setup and connect to IDO database |
|
| 53 | + * @param array $dbParam : array of named params |
|
| 54 | + */ |
|
| 55 | + public function setupIDO($dbParam) |
|
| 56 | + { |
|
| 57 | + $this->idoDSN=$this->setupDSN($dbParam); |
|
| 58 | + $this->idoUsername = $dbParam['username']; |
|
| 59 | + $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 60 | + $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 61 | + $this->idoDBType=$dbParam['db']; |
|
| 62 | + $this->db_connect_ido(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Connect to IDO database |
|
| 67 | - * @return \PDO |
|
| 68 | - */ |
|
| 69 | - public function db_connect_ido() |
|
| 70 | - { |
|
| 71 | - if ($this->idoDB != null) { |
|
| 72 | - // Check if connection is still alive |
|
| 73 | - try { |
|
| 74 | - $this->idoDB->query('select 1')->fetchColumn(); |
|
| 75 | - return $this->idoDB; |
|
| 76 | - } catch (Exception $e) { |
|
| 77 | - // select 1 failed, try to reconnect. |
|
| 78 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - try { |
|
| 82 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 83 | - } catch (PDOException $e) { |
|
| 84 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 85 | - } |
|
| 86 | - return $this->idoDB; |
|
| 87 | - } |
|
| 65 | + /** |
|
| 66 | + * Connect to IDO database |
|
| 67 | + * @return \PDO |
|
| 68 | + */ |
|
| 69 | + public function db_connect_ido() |
|
| 70 | + { |
|
| 71 | + if ($this->idoDB != null) { |
|
| 72 | + // Check if connection is still alive |
|
| 73 | + try { |
|
| 74 | + $this->idoDB->query('select 1')->fetchColumn(); |
|
| 75 | + return $this->idoDB; |
|
| 76 | + } catch (Exception $e) { |
|
| 77 | + // select 1 failed, try to reconnect. |
|
| 78 | + $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + try { |
|
| 82 | + $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 83 | + } catch (PDOException $e) { |
|
| 84 | + $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 85 | + } |
|
| 86 | + return $this->idoDB; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Connect to Trap database |
|
| 91 | - * @return \PDO |
|
| 92 | - */ |
|
| 93 | - public function db_connect_trap() |
|
| 94 | - { |
|
| 95 | - if ($this->trapDB != null) { |
|
| 96 | - // Check if connection is still alive |
|
| 97 | - try { |
|
| 98 | - $this->trapDB->query('select 1')->fetchColumn(); |
|
| 99 | - return $this->trapDB; |
|
| 100 | - } catch (Exception $e) { |
|
| 101 | - // select 1 failed, try to reconnect. |
|
| 102 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - try { |
|
| 106 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 107 | - } catch (PDOException $e) { |
|
| 108 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 109 | - } |
|
| 110 | - return $this->trapDB; |
|
| 111 | - } |
|
| 89 | + /** |
|
| 90 | + * Connect to Trap database |
|
| 91 | + * @return \PDO |
|
| 92 | + */ |
|
| 93 | + public function db_connect_trap() |
|
| 94 | + { |
|
| 95 | + if ($this->trapDB != null) { |
|
| 96 | + // Check if connection is still alive |
|
| 97 | + try { |
|
| 98 | + $this->trapDB->query('select 1')->fetchColumn(); |
|
| 99 | + return $this->trapDB; |
|
| 100 | + } catch (Exception $e) { |
|
| 101 | + // select 1 failed, try to reconnect. |
|
| 102 | + $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + try { |
|
| 106 | + $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 107 | + } catch (PDOException $e) { |
|
| 108 | + $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 109 | + } |
|
| 110 | + return $this->trapDB; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Setup dsn and check parameters |
|
| 115 | - * @param array $configElmt |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - protected function setupDSN($configElmt) |
|
| 119 | - { |
|
| 120 | - if (!array_key_exists('db',$configElmt) || |
|
| 121 | - !array_key_exists('host',$configElmt) || |
|
| 122 | - !array_key_exists('dbname',$configElmt) || |
|
| 123 | - !array_key_exists('username',$configElmt)) |
|
| 124 | - { |
|
| 125 | - $this->logging->log('Missing DB params',ERROR); |
|
| 126 | - return ''; |
|
| 127 | - } |
|
| 113 | + /** |
|
| 114 | + * Setup dsn and check parameters |
|
| 115 | + * @param array $configElmt |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + protected function setupDSN($configElmt) |
|
| 119 | + { |
|
| 120 | + if (!array_key_exists('db',$configElmt) || |
|
| 121 | + !array_key_exists('host',$configElmt) || |
|
| 122 | + !array_key_exists('dbname',$configElmt) || |
|
| 123 | + !array_key_exists('username',$configElmt)) |
|
| 124 | + { |
|
| 125 | + $this->logging->log('Missing DB params',ERROR); |
|
| 126 | + return ''; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 130 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 129 | + // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 130 | + $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 131 | 131 | |
| 132 | - if (array_key_exists('port', $configElmt)) |
|
| 133 | - { |
|
| 134 | - $dsn .= ';port='.$configElmt['port']; |
|
| 135 | - } |
|
| 136 | - return $dsn; |
|
| 137 | - } |
|
| 132 | + if (array_key_exists('port', $configElmt)) |
|
| 133 | + { |
|
| 134 | + $dsn .= ';port='.$configElmt['port']; |
|
| 135 | + } |
|
| 136 | + return $dsn; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** Set name=element in database config table |
|
| 140 | - * @param string $name |
|
| 141 | - * @param string $element |
|
| 142 | - * @return boolean true on success, else false (error logged) |
|
| 143 | - */ |
|
| 144 | - public function setDBConfig($name,$element) |
|
| 145 | - { |
|
| 146 | - $db_conn=$this->db_connect_trap(); |
|
| 147 | - $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
| 148 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 149 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - $value=$ret_code->fetch(); |
|
| 153 | - if ($value != null && isset($value['id'])) |
|
| 154 | - { // Entry exists -> update |
|
| 155 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
| 156 | - } |
|
| 157 | - else |
|
| 158 | - { // Entry does no exists -> create |
|
| 159 | - $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
| 160 | - } |
|
| 161 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 162 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 163 | - return false; |
|
| 164 | - } |
|
| 165 | - $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 139 | + /** Set name=element in database config table |
|
| 140 | + * @param string $name |
|
| 141 | + * @param string $element |
|
| 142 | + * @return boolean true on success, else false (error logged) |
|
| 143 | + */ |
|
| 144 | + public function setDBConfig($name,$element) |
|
| 145 | + { |
|
| 146 | + $db_conn=$this->db_connect_trap(); |
|
| 147 | + $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
| 148 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 149 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + $value=$ret_code->fetch(); |
|
| 153 | + if ($value != null && isset($value['id'])) |
|
| 154 | + { // Entry exists -> update |
|
| 155 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
| 156 | + } |
|
| 157 | + else |
|
| 158 | + { // Entry does no exists -> create |
|
| 159 | + $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
| 160 | + } |
|
| 161 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 162 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
| 163 | + return false; |
|
| 164 | + } |
|
| 165 | + $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Get data from db_config |
|
| 171 | - * @param $element string name of param |
|
| 172 | - * @return mixed : value (or null) |
|
| 173 | - */ |
|
| 174 | - public function getDBConfig($element) |
|
| 175 | - { |
|
| 176 | - $db_conn=$this->db_connect_trap(); |
|
| 177 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 178 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 179 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 180 | - return null; |
|
| 181 | - } |
|
| 182 | - $value=$ret_code->fetch(); |
|
| 183 | - if ($value != null && isset($value['value'])) |
|
| 184 | - { |
|
| 185 | - return $value['value']; |
|
| 186 | - } |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 169 | + /** |
|
| 170 | + * Get data from db_config |
|
| 171 | + * @param $element string name of param |
|
| 172 | + * @return mixed : value (or null) |
|
| 173 | + */ |
|
| 174 | + public function getDBConfig($element) |
|
| 175 | + { |
|
| 176 | + $db_conn=$this->db_connect_trap(); |
|
| 177 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 178 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 179 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 180 | + return null; |
|
| 181 | + } |
|
| 182 | + $value=$ret_code->fetch(); |
|
| 183 | + if ($value != null && isset($value['value'])) |
|
| 184 | + { |
|
| 185 | + return $value['value']; |
|
| 186 | + } |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | |
| 191 | - //********* Schema Management *********************/ |
|
| 191 | + //********* Schema Management *********************/ |
|
| 192 | 192 | |
| 193 | - /** Create database schema |
|
| 194 | - * @param $schema_file string File to read schema from |
|
| 195 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 196 | - */ |
|
| 197 | - public function create_schema($schema_file,$table_prefix) |
|
| 198 | - { |
|
| 199 | - //Read data from snmptrapd from stdin |
|
| 200 | - $input_stream=fopen($schema_file, 'r'); |
|
| 193 | + /** Create database schema |
|
| 194 | + * @param $schema_file string File to read schema from |
|
| 195 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 196 | + */ |
|
| 197 | + public function create_schema($schema_file,$table_prefix) |
|
| 198 | + { |
|
| 199 | + //Read data from snmptrapd from stdin |
|
| 200 | + $input_stream=fopen($schema_file, 'r'); |
|
| 201 | 201 | |
| 202 | - if ($input_stream=== false) |
|
| 203 | - { |
|
| 204 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
| 205 | - return; |
|
| 206 | - } |
|
| 207 | - $newline=''; |
|
| 208 | - $cur_table=''; |
|
| 209 | - $cur_table_array=array(); |
|
| 210 | - $db_conn=$this->db_connect_trap(); |
|
| 202 | + if ($input_stream=== false) |
|
| 203 | + { |
|
| 204 | + $this->logging->log("Error reading schema !",ERROR,''); |
|
| 205 | + return; |
|
| 206 | + } |
|
| 207 | + $newline=''; |
|
| 208 | + $cur_table=''; |
|
| 209 | + $cur_table_array=array(); |
|
| 210 | + $db_conn=$this->db_connect_trap(); |
|
| 211 | 211 | |
| 212 | - while (($line=fgets($input_stream)) !== false) |
|
| 213 | - { |
|
| 214 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 215 | - if (preg_match('/; *$/', $newline)) |
|
| 216 | - { |
|
| 217 | - $sql= $newline; |
|
| 218 | - if ($db_conn->query($sql) === false) { |
|
| 219 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 220 | - return; |
|
| 221 | - } |
|
| 222 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 223 | - { |
|
| 224 | - $cur_table='table '.$cur_table_array[1]; |
|
| 225 | - } |
|
| 226 | - else |
|
| 227 | - { |
|
| 228 | - $cur_table='secret SQL stuff :-)'; |
|
| 229 | - } |
|
| 230 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 231 | - $newline=''; |
|
| 232 | - } |
|
| 233 | - } |
|
| 212 | + while (($line=fgets($input_stream)) !== false) |
|
| 213 | + { |
|
| 214 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 215 | + if (preg_match('/; *$/', $newline)) |
|
| 216 | + { |
|
| 217 | + $sql= $newline; |
|
| 218 | + if ($db_conn->query($sql) === false) { |
|
| 219 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 220 | + return; |
|
| 221 | + } |
|
| 222 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 223 | + { |
|
| 224 | + $cur_table='table '.$cur_table_array[1]; |
|
| 225 | + } |
|
| 226 | + else |
|
| 227 | + { |
|
| 228 | + $cur_table='secret SQL stuff :-)'; |
|
| 229 | + } |
|
| 230 | + $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 231 | + $newline=''; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - $sql= $newline; |
|
| 236 | - if ($sql != '' ) |
|
| 237 | - { |
|
| 238 | - if ($db_conn->query($sql) === false) { |
|
| 239 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 240 | - return; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - $this->logging->log('Schema created',INFO); |
|
| 244 | - } |
|
| 235 | + $sql= $newline; |
|
| 236 | + if ($sql != '' ) |
|
| 237 | + { |
|
| 238 | + if ($db_conn->query($sql) === false) { |
|
| 239 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 240 | + return; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + $this->logging->log('Schema created',INFO); |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * Update database schema from current (as set in db) to $target_version |
|
| 248 | - * @param $prefix string file prefix of sql update File |
|
| 249 | - * @param $target_version int target db version number |
|
| 250 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 251 | - * @param bool $getmsg : only get messages from version upgrades |
|
| 252 | - * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 253 | - */ |
|
| 254 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 255 | - { |
|
| 256 | - // Get current db number |
|
| 257 | - $db_conn=$this->db_connect_trap(); |
|
| 258 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 259 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 260 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 261 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 262 | - return 'ERROR'; |
|
| 263 | - } |
|
| 264 | - $version=$ret_code->fetchAll(); |
|
| 265 | - $cur_version=$version[0]['value']; |
|
| 246 | + /** |
|
| 247 | + * Update database schema from current (as set in db) to $target_version |
|
| 248 | + * @param $prefix string file prefix of sql update File |
|
| 249 | + * @param $target_version int target db version number |
|
| 250 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 251 | + * @param bool $getmsg : only get messages from version upgrades |
|
| 252 | + * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 253 | + */ |
|
| 254 | + public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 255 | + { |
|
| 256 | + // Get current db number |
|
| 257 | + $db_conn=$this->db_connect_trap(); |
|
| 258 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 259 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 260 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 261 | + $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 262 | + return 'ERROR'; |
|
| 263 | + } |
|
| 264 | + $version=$ret_code->fetchAll(); |
|
| 265 | + $cur_version=$version[0]['value']; |
|
| 266 | 266 | |
| 267 | - if ($this->trapDBType == 'pgsql') |
|
| 268 | - { |
|
| 269 | - $prefix .= 'update_pgsql/schema_'; |
|
| 270 | - } |
|
| 271 | - else |
|
| 272 | - { |
|
| 273 | - $prefix .= 'update_sql/schema_'; |
|
| 274 | - } |
|
| 275 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 276 | - if ($getmsg === true) |
|
| 277 | - { |
|
| 278 | - return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 279 | - } |
|
| 267 | + if ($this->trapDBType == 'pgsql') |
|
| 268 | + { |
|
| 269 | + $prefix .= 'update_pgsql/schema_'; |
|
| 270 | + } |
|
| 271 | + else |
|
| 272 | + { |
|
| 273 | + $prefix .= 'update_sql/schema_'; |
|
| 274 | + } |
|
| 275 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 276 | + if ($getmsg === true) |
|
| 277 | + { |
|
| 278 | + return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 282 | - { |
|
| 283 | - return 'ERROR'; |
|
| 284 | - } |
|
| 285 | - return ''; |
|
| 281 | + if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 282 | + { |
|
| 283 | + return 'ERROR'; |
|
| 284 | + } |
|
| 285 | + return ''; |
|
| 286 | 286 | |
| 287 | - } |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - /** |
|
| 290 | - * Update database schema from current (as set in db) to $target_version |
|
| 291 | - * @param string $prefix file prefix of sql update File |
|
| 292 | - * @param int $cur_version current db version number |
|
| 293 | - * @param int $target_version target db version number |
|
| 294 | - * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 295 | - * @return bool : true on error |
|
| 296 | - */ |
|
| 297 | - public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 298 | - { |
|
| 299 | - while($cur_version<$target_version) |
|
| 300 | - { // TODO : execute pre & post scripts |
|
| 301 | - $cur_version++; |
|
| 302 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 303 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 304 | - $input_stream=fopen($updateFile, 'r'); |
|
| 305 | - if ($input_stream=== false) |
|
| 306 | - { |
|
| 307 | - $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 308 | - return true; |
|
| 309 | - } |
|
| 310 | - $newline=''; |
|
| 311 | - $db_conn=$this->db_connect_trap(); |
|
| 312 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 313 | - while (($line=fgets($input_stream)) !== false) |
|
| 314 | - { |
|
| 315 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 316 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 317 | - if (preg_match('/; *$/', $newline)) |
|
| 318 | - { |
|
| 319 | - $sql_req=$db_conn->prepare($newline); |
|
| 320 | - if ($sql_req->execute() === false) { |
|
| 321 | - $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 322 | - return true; |
|
| 323 | - } |
|
| 324 | - $cur_table_array=array(); |
|
| 325 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 326 | - { |
|
| 327 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 328 | - } |
|
| 329 | - else |
|
| 330 | - { |
|
| 331 | - $cur_table='secret SQL stuff :-)'; |
|
| 332 | - //$cur_table=$newline; |
|
| 333 | - } |
|
| 334 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 289 | + /** |
|
| 290 | + * Update database schema from current (as set in db) to $target_version |
|
| 291 | + * @param string $prefix file prefix of sql update File |
|
| 292 | + * @param int $cur_version current db version number |
|
| 293 | + * @param int $target_version target db version number |
|
| 294 | + * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 295 | + * @return bool : true on error |
|
| 296 | + */ |
|
| 297 | + public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 298 | + { |
|
| 299 | + while($cur_version<$target_version) |
|
| 300 | + { // TODO : execute pre & post scripts |
|
| 301 | + $cur_version++; |
|
| 302 | + $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 303 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 304 | + $input_stream=fopen($updateFile, 'r'); |
|
| 305 | + if ($input_stream=== false) |
|
| 306 | + { |
|
| 307 | + $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 308 | + return true; |
|
| 309 | + } |
|
| 310 | + $newline=''; |
|
| 311 | + $db_conn=$this->db_connect_trap(); |
|
| 312 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 313 | + while (($line=fgets($input_stream)) !== false) |
|
| 314 | + { |
|
| 315 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 316 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 317 | + if (preg_match('/; *$/', $newline)) |
|
| 318 | + { |
|
| 319 | + $sql_req=$db_conn->prepare($newline); |
|
| 320 | + if ($sql_req->execute() === false) { |
|
| 321 | + $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 322 | + return true; |
|
| 323 | + } |
|
| 324 | + $cur_table_array=array(); |
|
| 325 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 326 | + { |
|
| 327 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 328 | + } |
|
| 329 | + else |
|
| 330 | + { |
|
| 331 | + $cur_table='secret SQL stuff :-)'; |
|
| 332 | + //$cur_table=$newline; |
|
| 333 | + } |
|
| 334 | + $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 335 | 335 | |
| 336 | - $newline=''; |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - fclose($input_stream); |
|
| 336 | + $newline=''; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + fclose($input_stream); |
|
| 340 | 340 | |
| 341 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 342 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 343 | - if ($db_conn->query($sql) === false) { |
|
| 344 | - $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 345 | - return true; |
|
| 346 | - } |
|
| 341 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 342 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 343 | + if ($db_conn->query($sql) === false) { |
|
| 344 | + $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 345 | + return true; |
|
| 346 | + } |
|
| 347 | 347 | |
| 348 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 349 | - } |
|
| 350 | - return false; |
|
| 351 | - } |
|
| 348 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 349 | + } |
|
| 350 | + return false; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - /** |
|
| 354 | - * Get database message for update to $target_version |
|
| 355 | - * @param string $prefix file prefix of sql update File |
|
| 356 | - * @param int $cur_version current db version number |
|
| 357 | - * @param int $target_version target db version number |
|
| 358 | - * @return string : return messages or 'ERROR'. |
|
| 359 | - */ |
|
| 360 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 361 | - { |
|
| 353 | + /** |
|
| 354 | + * Get database message for update to $target_version |
|
| 355 | + * @param string $prefix file prefix of sql update File |
|
| 356 | + * @param int $cur_version current db version number |
|
| 357 | + * @param int $target_version target db version number |
|
| 358 | + * @return string : return messages or 'ERROR'. |
|
| 359 | + */ |
|
| 360 | + private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 361 | + { |
|
| 362 | 362 | |
| 363 | - $message=''; |
|
| 364 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 365 | - while($cur_version<$target_version) |
|
| 366 | - { |
|
| 367 | - $cur_version++; |
|
| 368 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 369 | - $input_stream=fopen($updateFile, 'r'); |
|
| 370 | - if ($input_stream=== false) |
|
| 371 | - { |
|
| 372 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 373 | - return 'ERROR'; |
|
| 374 | - } |
|
| 375 | - do |
|
| 376 | - { |
|
| 377 | - $line=fgets($input_stream); |
|
| 378 | - } |
|
| 379 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 380 | - fclose($input_stream); |
|
| 381 | - if ($line === false) |
|
| 382 | - { |
|
| 383 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 384 | - return ''; |
|
| 385 | - } |
|
| 386 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 387 | - } |
|
| 388 | - return $message; |
|
| 389 | - } |
|
| 363 | + $message=''; |
|
| 364 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 365 | + while($cur_version<$target_version) |
|
| 366 | + { |
|
| 367 | + $cur_version++; |
|
| 368 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 369 | + $input_stream=fopen($updateFile, 'r'); |
|
| 370 | + if ($input_stream=== false) |
|
| 371 | + { |
|
| 372 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 373 | + return 'ERROR'; |
|
| 374 | + } |
|
| 375 | + do |
|
| 376 | + { |
|
| 377 | + $line=fgets($input_stream); |
|
| 378 | + } |
|
| 379 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 380 | + fclose($input_stream); |
|
| 381 | + if ($line === false) |
|
| 382 | + { |
|
| 383 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 384 | + return ''; |
|
| 385 | + } |
|
| 386 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 387 | + } |
|
| 388 | + return $message; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | 391 | } |
| 392 | 392 | \ No newline at end of file |
@@ -17,82 +17,82 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class NetworkRule extends PluginTemplate |
| 19 | 19 | { |
| 20 | - /** @var string $description Description of plugin */ |
|
| 21 | - public $description='Network functions to use into rules |
|
| 20 | + /** @var string $description Description of plugin */ |
|
| 21 | + public $description='Network functions to use into rules |
|
| 22 | 22 | test test test'; |
| 23 | 23 | |
| 24 | - /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 25 | - * If no functions are declared, set to empty array |
|
| 26 | - */ |
|
| 27 | - public $functions=array( |
|
| 28 | - 'inNetwork' => array( // The name of the function |
|
| 29 | - 'function' => 'isInNetwork', // Name of the function in rules |
|
| 30 | - 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 31 | - 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 24 | + /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 25 | + * If no functions are declared, set to empty array |
|
| 26 | + */ |
|
| 27 | + public $functions=array( |
|
| 28 | + 'inNetwork' => array( // The name of the function |
|
| 29 | + 'function' => 'isInNetwork', // Name of the function in rules |
|
| 30 | + 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 31 | + 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 32 | 32 | Does not work with IPV6' // Description (can be multiline). |
| 33 | - ) |
|
| 34 | - ); |
|
| 33 | + ) |
|
| 34 | + ); |
|
| 35 | 35 | |
| 36 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
| 37 | - public $catchAllTraps=false; |
|
| 36 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
| 37 | + public $catchAllTraps=false; |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 42 | - * @throws \Exception |
|
| 43 | - * @return \Trapdirector\Plugins\NetworkRule |
|
| 44 | - */ |
|
| 45 | - function __construct() |
|
| 46 | - { |
|
| 47 | - $this->name=basename(__FILE__,'.php'); |
|
| 48 | - return $this; |
|
| 49 | - } |
|
| 40 | + /** |
|
| 41 | + * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 42 | + * @throws \Exception |
|
| 43 | + * @return \Trapdirector\Plugins\NetworkRule |
|
| 44 | + */ |
|
| 45 | + function __construct() |
|
| 46 | + { |
|
| 47 | + $this->name=basename(__FILE__,'.php'); |
|
| 48 | + return $this; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * |
|
| 53 | - * @param array $params Function parameters |
|
| 54 | - * @throws Exception |
|
| 55 | - * @return bool Evaluation |
|
| 56 | - */ |
|
| 57 | - public function isInNetwork(array $params) : bool |
|
| 58 | - { |
|
| 59 | - $this->log('Function params : ' . print_r($params,true),DEBUG); |
|
| 60 | - if (count($params)!=3) |
|
| 61 | - { |
|
| 62 | - throw new Exception('Invalid number of parameters : ' . array_count_values($params)); |
|
| 63 | - } |
|
| 51 | + /** |
|
| 52 | + * |
|
| 53 | + * @param array $params Function parameters |
|
| 54 | + * @throws Exception |
|
| 55 | + * @return bool Evaluation |
|
| 56 | + */ |
|
| 57 | + public function isInNetwork(array $params) : bool |
|
| 58 | + { |
|
| 59 | + $this->log('Function params : ' . print_r($params,true),DEBUG); |
|
| 60 | + if (count($params)!=3) |
|
| 61 | + { |
|
| 62 | + throw new Exception('Invalid number of parameters : ' . array_count_values($params)); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - $ip = $params[0]; |
|
| 66 | - $net = $params[1]; |
|
| 67 | - $masq = $params[2]; |
|
| 65 | + $ip = $params[0]; |
|
| 66 | + $net = $params[1]; |
|
| 67 | + $masq = $params[2]; |
|
| 68 | 68 | |
| 69 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 69 | + $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 70 | 70 | |
| 71 | - $ip2 = ip2long($ip); |
|
| 72 | - $net2 = ip2long($net); |
|
| 71 | + $ip2 = ip2long($ip); |
|
| 72 | + $net2 = ip2long($net); |
|
| 73 | 73 | |
| 74 | - if ($ip2 === false ) |
|
| 75 | - { |
|
| 76 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 77 | - throw new Exception('Invalid IP'); |
|
| 78 | - } |
|
| 79 | - if ($net2 === false) |
|
| 80 | - { |
|
| 81 | - $this->log('Invalid network',WARN); |
|
| 82 | - throw new Exception('Invalid net'); |
|
| 83 | - } |
|
| 84 | - if ($masq<1 || $masq > 32) |
|
| 85 | - { |
|
| 86 | - $this->log('Invalid masq',WARN); |
|
| 87 | - throw new Exception('Invalid net masq'); |
|
| 88 | - } |
|
| 89 | - // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 74 | + if ($ip2 === false ) |
|
| 75 | + { |
|
| 76 | + $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 77 | + throw new Exception('Invalid IP'); |
|
| 78 | + } |
|
| 79 | + if ($net2 === false) |
|
| 80 | + { |
|
| 81 | + $this->log('Invalid network',WARN); |
|
| 82 | + throw new Exception('Invalid net'); |
|
| 83 | + } |
|
| 84 | + if ($masq<1 || $masq > 32) |
|
| 85 | + { |
|
| 86 | + $this->log('Invalid masq',WARN); |
|
| 87 | + throw new Exception('Invalid net masq'); |
|
| 88 | + } |
|
| 89 | + // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 90 | 90 | |
| 91 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 92 | - $masq = ~ $masq; |
|
| 93 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 91 | + $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 92 | + $masq = ~ $masq; |
|
| 93 | + return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 94 | 94 | |
| 95 | - } |
|
| 95 | + } |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | |