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