@@ -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 | } |
@@ -84,8 +84,7 @@ discard block |
||
84 | 84 | } |
85 | 85 | $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
86 | 86 | return 1; |
87 | - } |
|
88 | - else |
|
87 | + } else |
|
89 | 88 | { |
90 | 89 | $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
91 | 90 | return 0; |
@@ -107,7 +106,9 @@ discard block |
||
107 | 106 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
108 | 107 | ', :syntax, :type_enum, :description )'; |
109 | 108 | |
110 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
109 | + if ($this->trapsDB->trapDBType == 'pgsql') { |
|
110 | + $sql .= 'RETURNING id'; |
|
111 | + } |
|
111 | 112 | |
112 | 113 | $sqlQuery=$db_conn->prepare($sql); |
113 | 114 | |
@@ -147,7 +148,9 @@ discard block |
||
147 | 148 | } |
148 | 149 | |
149 | 150 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
150 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
151 | + if ($inserted_id==false) { |
|
152 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
153 | + } |
|
151 | 154 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
152 | 155 | break; |
153 | 156 | default: |
@@ -325,7 +328,10 @@ discard block |
||
325 | 328 | $this->reset_oidDesc(); |
326 | 329 | |
327 | 330 | $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
328 | - if ($snmptrans === null) continue; // object not found |
|
331 | + if ($snmptrans === null) { |
|
332 | + continue; |
|
333 | + } |
|
334 | + // object not found |
|
329 | 335 | |
330 | 336 | $this->parse_object($snmptrans); |
331 | 337 | |
@@ -476,12 +482,14 @@ discard block |
||
476 | 482 | $this->oidDesc['name']=$match[1]; // Name |
477 | 483 | $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
478 | 484 | |
479 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
485 | + if ($this->oidDesc['type']==0) { |
|
486 | + // object type=0 : check if v1 trap |
|
480 | 487 | { |
481 | 488 | // Check if next is suboid -> in that case is cannot be a trap |
482 | 489 | if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
483 | 490 | { |
484 | 491 | $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
492 | + } |
|
485 | 493 | $this->timing['type0_check_num']++; |
486 | 494 | return true; |
487 | 495 | } |
@@ -499,9 +507,11 @@ discard block |
||
499 | 507 | // Force as trap. |
500 | 508 | $this->oidDesc['type']=21; |
501 | 509 | } |
502 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
510 | + if ($onlyTraps===true && $this->oidDesc['type']!=21) { |
|
511 | + // if only traps and not a trap, continue |
|
503 | 512 | { |
504 | 513 | $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
514 | + } |
|
505 | 515 | $this->timing['nottrap_num']++; |
506 | 516 | return true; |
507 | 517 | } |
@@ -532,7 +542,9 @@ discard block |
||
532 | 542 | $this->oidDesc['mib']=$match[1]; |
533 | 543 | |
534 | 544 | $numLine=1; |
535 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
545 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) { |
|
546 | + $numLine++; |
|
547 | + } |
|
536 | 548 | if (isset($snmptrans[$numLine])) |
537 | 549 | { |
538 | 550 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
@@ -636,7 +648,10 @@ discard block |
||
636 | 648 | $this->timing['num_traps']++; |
637 | 649 | |
638 | 650 | $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
639 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
651 | + if ($display_progress) { |
|
652 | + echo '#'; |
|
653 | + } |
|
654 | + // echo a # when trap found |
|
640 | 655 | |
641 | 656 | // get trap objects & source MIB |
642 | 657 | |
@@ -653,7 +668,9 @@ discard block |
||
653 | 668 | if (($update==0) && ($check_change===false)) |
654 | 669 | { // Trapd didn't change & force check disabled |
655 | 670 | $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
656 | - if ($display_progress) echo "C"; |
|
671 | + if ($display_progress) { |
|
672 | + echo "C"; |
|
673 | + } |
|
657 | 674 | continue; |
658 | 675 | } |
659 | 676 |
@@ -11,200 +11,200 @@ discard block |
||
11 | 11 | class Mib |
12 | 12 | { |
13 | 13 | |
14 | - protected $logging; //< logging class |
|
15 | - protected $trapsDB; //< Database class |
|
14 | + protected $logging; //< logging class |
|
15 | + protected $trapsDB; //< Database class |
|
16 | 16 | |
17 | - public $snmptranslate; |
|
18 | - public $snmptranslateDirs; |
|
17 | + public $snmptranslate; |
|
18 | + public $snmptranslateDirs; |
|
19 | 19 | |
20 | - private $dbOidAll; //< All oid in database; |
|
21 | - private $dbOidIndex; //< Index of oid in dbOidAll |
|
22 | - private $objectsAll; //< output lines of snmptranslate list |
|
23 | - private $trapObjectsIndex; //< array of traps objects (as OID) |
|
20 | + private $dbOidAll; //< All oid in database; |
|
21 | + private $dbOidIndex; //< Index of oid in dbOidAll |
|
22 | + private $objectsAll; //< output lines of snmptranslate list |
|
23 | + private $trapObjectsIndex; //< array of traps objects (as OID) |
|
24 | 24 | |
25 | - private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
|
25 | + private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
|
26 | 26 | |
27 | - // Timing vars for update |
|
28 | - private $timing=array(); |
|
27 | + // Timing vars for update |
|
28 | + private $timing=array(); |
|
29 | 29 | |
30 | - /** |
|
31 | - * Setup Mib Class |
|
32 | - * @param Logging $logClass : where to log |
|
33 | - * @param Database $dbClass : Database |
|
34 | - */ |
|
35 | - function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
36 | - { |
|
37 | - $this->logging=$logClass; |
|
38 | - $this->trapsDB=$dbClass; |
|
39 | - $this->snmptranslate=$snmptrans; |
|
40 | - $this->snmptranslateDirs=$snmptransdir; |
|
30 | + /** |
|
31 | + * Setup Mib Class |
|
32 | + * @param Logging $logClass : where to log |
|
33 | + * @param Database $dbClass : Database |
|
34 | + */ |
|
35 | + function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
36 | + { |
|
37 | + $this->logging=$logClass; |
|
38 | + $this->trapsDB=$dbClass; |
|
39 | + $this->snmptranslate=$snmptrans; |
|
40 | + $this->snmptranslateDirs=$snmptransdir; |
|
41 | 41 | |
42 | - } |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
46 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
47 | - */ |
|
48 | - private function update_oid_update() |
|
49 | - { |
|
44 | + /** |
|
45 | + * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
|
46 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
47 | + */ |
|
48 | + private function update_oid_update() |
|
49 | + { |
|
50 | 50 | |
51 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
51 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
52 | 52 | |
53 | - if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
54 | - { // newly created. |
|
55 | - return 0; |
|
56 | - } |
|
57 | - $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
58 | - $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
59 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
60 | - $this->oidDesc['mib'] != $dbOid['mib'] || |
|
61 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
62 | - ) |
|
63 | - { // Do update |
|
64 | - $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
65 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
66 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
67 | - ' WHERE id= :id'; |
|
68 | - $sqlQuery=$db_conn->prepare($sql); |
|
53 | + if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
54 | + { // newly created. |
|
55 | + return 0; |
|
56 | + } |
|
57 | + $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
58 | + $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
59 | + if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
60 | + $this->oidDesc['mib'] != $dbOid['mib'] || |
|
61 | + $this->oidDesc['type'] !=$dbOid['type'] |
|
62 | + ) |
|
63 | + { // Do update |
|
64 | + $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
65 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
66 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
67 | + ' WHERE id= :id'; |
|
68 | + $sqlQuery=$db_conn->prepare($sql); |
|
69 | 69 | |
70 | - $sqlParam=array( |
|
71 | - ':name' => $this->oidDesc['name'], |
|
72 | - ':type' => $this->oidDesc['type'], |
|
73 | - ':mib' => $this->oidDesc['mib'], |
|
74 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
75 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
76 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
77 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
78 | - ':description' => $this->oidDesc['description']??'null', |
|
79 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
80 | - ); |
|
70 | + $sqlParam=array( |
|
71 | + ':name' => $this->oidDesc['name'], |
|
72 | + ':type' => $this->oidDesc['type'], |
|
73 | + ':mib' => $this->oidDesc['mib'], |
|
74 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
75 | + ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
76 | + ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
77 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
78 | + ':description' => $this->oidDesc['description']??'null', |
|
79 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
80 | + ); |
|
81 | 81 | |
82 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
83 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
84 | - } |
|
85 | - $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
86 | - return 1; |
|
87 | - } |
|
88 | - else |
|
89 | - { |
|
90 | - $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
91 | - return 0; |
|
92 | - } |
|
93 | - } |
|
82 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
83 | + $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
84 | + } |
|
85 | + $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
86 | + return 1; |
|
87 | + } |
|
88 | + else |
|
89 | + { |
|
90 | + $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
91 | + return 0; |
|
92 | + } |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Create object in DB with object in dbOidIndex |
|
97 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
98 | - */ |
|
99 | - private function update_oid_create() |
|
100 | - { |
|
101 | - // Insert data |
|
95 | + /** |
|
96 | + * Create object in DB with object in dbOidIndex |
|
97 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
98 | + */ |
|
99 | + private function update_oid_create() |
|
100 | + { |
|
101 | + // Insert data |
|
102 | 102 | |
103 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
104 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
105 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
106 | - ', syntax, type_enum , description ) ' . |
|
107 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
108 | - ', :syntax, :type_enum, :description )'; |
|
103 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
104 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
105 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
106 | + ', syntax, type_enum , description ) ' . |
|
107 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
108 | + ', :syntax, :type_enum, :description )'; |
|
109 | 109 | |
110 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
110 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
111 | 111 | |
112 | - $sqlQuery=$db_conn->prepare($sql); |
|
112 | + $sqlQuery=$db_conn->prepare($sql); |
|
113 | 113 | |
114 | - $sqlParam=array( |
|
115 | - ':oid' => $this->oidDesc['oid'], |
|
116 | - ':name' => $this->oidDesc['name'], |
|
117 | - ':type' => $this->oidDesc['type'], |
|
118 | - ':mib' => $this->oidDesc['mib'], |
|
119 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
120 | - ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
121 | - ':syntax' => $this->oidDesc['syntax']??'null', |
|
122 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
123 | - ':description' => $this->oidDesc['description']??'null' |
|
124 | - ); |
|
114 | + $sqlParam=array( |
|
115 | + ':oid' => $this->oidDesc['oid'], |
|
116 | + ':name' => $this->oidDesc['name'], |
|
117 | + ':type' => $this->oidDesc['type'], |
|
118 | + ':mib' => $this->oidDesc['mib'], |
|
119 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
120 | + ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
121 | + ':syntax' => $this->oidDesc['syntax']??'null', |
|
122 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
123 | + ':description' => $this->oidDesc['description']??'null' |
|
124 | + ); |
|
125 | 125 | |
126 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
127 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
128 | - } |
|
126 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
127 | + $this->logging->log('Error in query : ' . $sql,1,''); |
|
128 | + } |
|
129 | 129 | |
130 | - switch ($this->trapsDB->trapDBType) |
|
131 | - { |
|
132 | - case 'pgsql': |
|
133 | - // Get last id to insert oid/values in secondary table |
|
134 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
135 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
136 | - } |
|
137 | - if (! isset($inserted_id_ret['id'])) { |
|
138 | - $this->logging->log('Error getting id - pgsql - empty.',ERROR); |
|
139 | - return 0; |
|
140 | - } |
|
141 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
142 | - break; |
|
143 | - case 'mysql': |
|
144 | - // Get last id to insert oid/values in secondary table |
|
145 | - $sql='SELECT LAST_INSERT_ID();'; |
|
146 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
147 | - $this->logging->log('Erreur getting id - mysql - ',ERROR); |
|
148 | - return 0; |
|
149 | - } |
|
130 | + switch ($this->trapsDB->trapDBType) |
|
131 | + { |
|
132 | + case 'pgsql': |
|
133 | + // Get last id to insert oid/values in secondary table |
|
134 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
135 | + $this->logging->log('Error getting id - pgsql - ',1,''); |
|
136 | + } |
|
137 | + if (! isset($inserted_id_ret['id'])) { |
|
138 | + $this->logging->log('Error getting id - pgsql - empty.',ERROR); |
|
139 | + return 0; |
|
140 | + } |
|
141 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
142 | + break; |
|
143 | + case 'mysql': |
|
144 | + // Get last id to insert oid/values in secondary table |
|
145 | + $sql='SELECT LAST_INSERT_ID();'; |
|
146 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
147 | + $this->logging->log('Erreur getting id - mysql - ',ERROR); |
|
148 | + return 0; |
|
149 | + } |
|
150 | 150 | |
151 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
152 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
153 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
154 | - break; |
|
155 | - default: |
|
156 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,ERROR); |
|
157 | - return 0; |
|
158 | - } |
|
151 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
152 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
153 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
154 | + break; |
|
155 | + default: |
|
156 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,ERROR); |
|
157 | + return 0; |
|
158 | + } |
|
159 | 159 | |
160 | - // Set as newly created. |
|
161 | - $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
162 | - return 2; |
|
163 | - } |
|
160 | + // Set as newly created. |
|
161 | + $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
162 | + return 2; |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
167 | - * and $this->oidDesc doe data |
|
168 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
169 | - */ |
|
170 | - public function update_oid() |
|
171 | - { |
|
172 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
173 | - // Quote description. |
|
174 | - $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
165 | + /** |
|
166 | + * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
167 | + * and $this->oidDesc doe data |
|
168 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
169 | + */ |
|
170 | + public function update_oid() |
|
171 | + { |
|
172 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
173 | + // Quote description. |
|
174 | + $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
175 | 175 | |
176 | - if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
177 | - { // oid exists in db, so update |
|
178 | - return $this->update_oid_update(); |
|
179 | - } |
|
180 | - // create new OID. |
|
181 | - return $this->update_oid_create(); |
|
176 | + if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
177 | + { // oid exists in db, so update |
|
178 | + return $this->update_oid_update(); |
|
179 | + } |
|
180 | + // create new OID. |
|
181 | + return $this->update_oid_create(); |
|
182 | 182 | |
183 | - } |
|
183 | + } |
|
184 | 184 | |
185 | 185 | /** |
186 | 186 | * get all objects for a trap. |
187 | 187 | * @param integer $trapId |
188 | 188 | * @return array : array of cached objects |
189 | 189 | */ |
190 | - private function cache_db_objects($trapId) |
|
191 | - { |
|
192 | - $dbObjects=array(); // cache of objects for trap in db |
|
193 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
194 | - // Get all objects |
|
195 | - $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
196 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
197 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
198 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
199 | - } |
|
200 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
190 | + private function cache_db_objects($trapId) |
|
191 | + { |
|
192 | + $dbObjects=array(); // cache of objects for trap in db |
|
193 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
194 | + // Get all objects |
|
195 | + $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
196 | + $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
197 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
198 | + $this->logging->log('No result in query : ' . $sql,1,''); |
|
199 | + } |
|
200 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
201 | 201 | |
202 | - foreach ($dbObjectsRaw as $val) |
|
203 | - { |
|
204 | - $dbObjects[$val['object_id']]=1; |
|
205 | - } |
|
206 | - return $dbObjects; |
|
207 | - } |
|
202 | + foreach ($dbObjectsRaw as $val) |
|
203 | + { |
|
204 | + $dbObjects[$val['object_id']]=1; |
|
205 | + } |
|
206 | + return $dbObjects; |
|
207 | + } |
|
208 | 208 | |
209 | 209 | /** |
210 | 210 | * Get object details & mib , returns snmptranslate output |
@@ -212,478 +212,478 @@ discard block |
||
212 | 212 | * @param string $trapmib : mib of trap |
213 | 213 | * @return NULL|array : null if not found, or output of snmptranslate |
214 | 214 | */ |
215 | - private function get_object_details($object,$trapmib) |
|
216 | - { |
|
217 | - $match=$snmptrans=array(); |
|
218 | - $retVal=0; |
|
219 | - $this->oidDesc['mib']=$trapmib; |
|
220 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
221 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
222 | - if ($retVal!=0) |
|
223 | - { |
|
224 | - // Maybe not trap mib, search with IR |
|
225 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
226 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
227 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
228 | - { // Not found -> continue with warning |
|
229 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
230 | - return null; |
|
231 | - } |
|
232 | - $this->oidDesc['mib']=$match[1]; |
|
215 | + private function get_object_details($object,$trapmib) |
|
216 | + { |
|
217 | + $match=$snmptrans=array(); |
|
218 | + $retVal=0; |
|
219 | + $this->oidDesc['mib']=$trapmib; |
|
220 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
221 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
222 | + if ($retVal!=0) |
|
223 | + { |
|
224 | + // Maybe not trap mib, search with IR |
|
225 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
226 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
227 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
228 | + { // Not found -> continue with warning |
|
229 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
230 | + return null; |
|
231 | + } |
|
232 | + $this->oidDesc['mib']=$match[1]; |
|
233 | 233 | |
234 | - // Do the snmptranslate again. |
|
235 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
236 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
237 | - if ($retVal!=0) { |
|
238 | - $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
239 | - return null; |
|
240 | - } |
|
234 | + // Do the snmptranslate again. |
|
235 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
236 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
237 | + if ($retVal!=0) { |
|
238 | + $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
239 | + return null; |
|
240 | + } |
|
241 | 241 | |
242 | - } |
|
243 | - return $snmptrans; |
|
244 | - } |
|
242 | + } |
|
243 | + return $snmptrans; |
|
244 | + } |
|
245 | 245 | |
246 | 246 | /** |
247 | 247 | * Parse snmptranslate output and set $this->oidDesc with elements |
248 | 248 | * @param array $snmptrans : multi line output of snmptrans |
249 | 249 | */ |
250 | - private function parse_object($snmptrans) |
|
251 | - { |
|
252 | - $tmpdesc=''; // For multiline description |
|
253 | - $indesc=false; // true if currently inside multiline description |
|
254 | - $match=array(); |
|
250 | + private function parse_object($snmptrans) |
|
251 | + { |
|
252 | + $tmpdesc=''; // For multiline description |
|
253 | + $indesc=false; // true if currently inside multiline description |
|
254 | + $match=array(); |
|
255 | 255 | |
256 | - foreach ($snmptrans as $line) |
|
257 | - { |
|
258 | - if ($indesc===true) |
|
259 | - { |
|
260 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
261 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
262 | - { |
|
263 | - $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
264 | - $indesc=false; |
|
265 | - } |
|
266 | - $tmpdesc.=$line; |
|
267 | - continue; |
|
268 | - } |
|
269 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
270 | - { |
|
271 | - $this->oidDesc['oid']=$line; |
|
272 | - continue; |
|
273 | - } |
|
274 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
275 | - { |
|
276 | - $this->oidDesc['syntax']=$match[1]; |
|
277 | - $this->oidDesc['type_enum']=$match[2]; |
|
278 | - continue; |
|
279 | - } |
|
280 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
281 | - { |
|
282 | - $this->oidDesc['syntax']=$match[1]; |
|
283 | - continue; |
|
284 | - } |
|
285 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
286 | - { |
|
287 | - $this->oidDesc['dispHint']=$match[1]; |
|
288 | - continue; |
|
289 | - } |
|
290 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
291 | - { |
|
292 | - $this->oidDesc['description']=$match[1]; |
|
293 | - continue; |
|
294 | - } |
|
295 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
296 | - { |
|
297 | - $tmpdesc=$match[1]; |
|
298 | - $indesc=true; |
|
299 | - continue; |
|
300 | - } |
|
301 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
302 | - { |
|
303 | - $this->oidDesc['textconv']=$match[1]; |
|
304 | - continue; |
|
305 | - } |
|
306 | - } |
|
307 | - } |
|
256 | + foreach ($snmptrans as $line) |
|
257 | + { |
|
258 | + if ($indesc===true) |
|
259 | + { |
|
260 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
261 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
262 | + { |
|
263 | + $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
264 | + $indesc=false; |
|
265 | + } |
|
266 | + $tmpdesc.=$line; |
|
267 | + continue; |
|
268 | + } |
|
269 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
270 | + { |
|
271 | + $this->oidDesc['oid']=$line; |
|
272 | + continue; |
|
273 | + } |
|
274 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
275 | + { |
|
276 | + $this->oidDesc['syntax']=$match[1]; |
|
277 | + $this->oidDesc['type_enum']=$match[2]; |
|
278 | + continue; |
|
279 | + } |
|
280 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
281 | + { |
|
282 | + $this->oidDesc['syntax']=$match[1]; |
|
283 | + continue; |
|
284 | + } |
|
285 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
286 | + { |
|
287 | + $this->oidDesc['dispHint']=$match[1]; |
|
288 | + continue; |
|
289 | + } |
|
290 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
291 | + { |
|
292 | + $this->oidDesc['description']=$match[1]; |
|
293 | + continue; |
|
294 | + } |
|
295 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
296 | + { |
|
297 | + $tmpdesc=$match[1]; |
|
298 | + $indesc=true; |
|
299 | + continue; |
|
300 | + } |
|
301 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
302 | + { |
|
303 | + $this->oidDesc['textconv']=$match[1]; |
|
304 | + continue; |
|
305 | + } |
|
306 | + } |
|
307 | + } |
|
308 | 308 | |
309 | - /** |
|
310 | - * create or update (with check_existing = true) objects of trap |
|
311 | - * @param string $trapOID : trap oid |
|
312 | - * @param string $trapmib : mib of trap |
|
313 | - * @param array $objects : array of objects name (without MIB) |
|
314 | - * @param bool $check_existing : check instead of create |
|
315 | - */ |
|
316 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
317 | - { |
|
318 | - $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
309 | + /** |
|
310 | + * create or update (with check_existing = true) objects of trap |
|
311 | + * @param string $trapOID : trap oid |
|
312 | + * @param string $trapmib : mib of trap |
|
313 | + * @param array $objects : array of objects name (without MIB) |
|
314 | + * @param bool $check_existing : check instead of create |
|
315 | + */ |
|
316 | + public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
317 | + { |
|
318 | + $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
319 | 319 | |
320 | - if ($check_existing === true) |
|
321 | - { |
|
322 | - $dbObjects=$this->cache_db_objects($trapId); |
|
323 | - } |
|
320 | + if ($check_existing === true) |
|
321 | + { |
|
322 | + $dbObjects=$this->cache_db_objects($trapId); |
|
323 | + } |
|
324 | 324 | |
325 | - foreach ($objects as $object) |
|
326 | - { |
|
325 | + foreach ($objects as $object) |
|
326 | + { |
|
327 | 327 | |
328 | - $this->reset_oidDesc(); |
|
328 | + $this->reset_oidDesc(); |
|
329 | 329 | |
330 | - $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
|
331 | - if ($snmptrans === null) continue; // object not found |
|
330 | + $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
|
331 | + if ($snmptrans === null) continue; // object not found |
|
332 | 332 | |
333 | - $this->parse_object($snmptrans); |
|
333 | + $this->parse_object($snmptrans); |
|
334 | 334 | |
335 | - $this->oidDesc['name'] = $object; |
|
335 | + $this->oidDesc['name'] = $object; |
|
336 | 336 | |
337 | - $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
337 | + $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
338 | 338 | |
339 | - // Update |
|
340 | - $this->update_oid(); |
|
339 | + // Update |
|
340 | + $this->update_oid(); |
|
341 | 341 | |
342 | - if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
|
343 | - { // if link exists, continue |
|
344 | - $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
|
345 | - continue; |
|
346 | - } |
|
347 | - if ($check_existing === true) |
|
348 | - { |
|
349 | - // TODO : check link trap - objects exists, mark them. |
|
350 | - } |
|
351 | - // Associate in object table |
|
352 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
353 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
354 | - 'values (:trap_id, :object_id)'; |
|
355 | - $sqlQuery=$db_conn->prepare($sql); |
|
356 | - $sqlParam=array( |
|
357 | - ':trap_id' => $trapId, |
|
358 | - ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
|
359 | - ); |
|
342 | + if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
|
343 | + { // if link exists, continue |
|
344 | + $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
|
345 | + continue; |
|
346 | + } |
|
347 | + if ($check_existing === true) |
|
348 | + { |
|
349 | + // TODO : check link trap - objects exists, mark them. |
|
350 | + } |
|
351 | + // Associate in object table |
|
352 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
353 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
354 | + 'values (:trap_id, :object_id)'; |
|
355 | + $sqlQuery=$db_conn->prepare($sql); |
|
356 | + $sqlParam=array( |
|
357 | + ':trap_id' => $trapId, |
|
358 | + ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
|
359 | + ); |
|
360 | 360 | |
361 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
362 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
363 | - } |
|
364 | - } |
|
365 | - if ($check_existing === true) |
|
366 | - { |
|
367 | - // TODO : remove link trap - objects that wasn't marked. |
|
368 | - } |
|
361 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
362 | + $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
363 | + } |
|
364 | + } |
|
365 | + if ($check_existing === true) |
|
366 | + { |
|
367 | + // TODO : remove link trap - objects that wasn't marked. |
|
368 | + } |
|
369 | 369 | |
370 | - } |
|
370 | + } |
|
371 | 371 | |
372 | - private function reset_oidDesc() |
|
373 | - { |
|
374 | - $this->oidDesc['oid']=null; |
|
375 | - $this->oidDesc['name']=null; |
|
376 | - $this->oidDesc['type']=null; |
|
377 | - $this->oidDesc['mib']=null; |
|
378 | - $this->oidDesc['textconv']=null; |
|
379 | - $this->oidDesc['dispHint'] =null; |
|
380 | - $this->oidDesc['syntax']=null; |
|
381 | - $this->oidDesc['type_enum']=null; |
|
382 | - $this->oidDesc['description']=null; |
|
383 | - } |
|
372 | + private function reset_oidDesc() |
|
373 | + { |
|
374 | + $this->oidDesc['oid']=null; |
|
375 | + $this->oidDesc['name']=null; |
|
376 | + $this->oidDesc['type']=null; |
|
377 | + $this->oidDesc['mib']=null; |
|
378 | + $this->oidDesc['textconv']=null; |
|
379 | + $this->oidDesc['dispHint'] =null; |
|
380 | + $this->oidDesc['syntax']=null; |
|
381 | + $this->oidDesc['type_enum']=null; |
|
382 | + $this->oidDesc['description']=null; |
|
383 | + } |
|
384 | 384 | |
385 | - /** |
|
386 | - * Fills $this->objectsAll with all mibs from snmptranslate |
|
387 | - * @return integer : number of elements |
|
388 | - */ |
|
389 | - private function load_mibs_snmptranslate() |
|
390 | - { |
|
391 | - $retVal=0; |
|
392 | - // Get all mib objects from all mibs |
|
393 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
394 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
395 | - unset($this->objectsAll); |
|
396 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
397 | - if ($retVal!=0) |
|
398 | - { |
|
399 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
400 | - } |
|
401 | - // Count elements to show progress |
|
402 | - $numElements=count($this->objectsAll); |
|
403 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
404 | - return $numElements; |
|
405 | - } |
|
385 | + /** |
|
386 | + * Fills $this->objectsAll with all mibs from snmptranslate |
|
387 | + * @return integer : number of elements |
|
388 | + */ |
|
389 | + private function load_mibs_snmptranslate() |
|
390 | + { |
|
391 | + $retVal=0; |
|
392 | + // Get all mib objects from all mibs |
|
393 | + $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
394 | + $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
395 | + unset($this->objectsAll); |
|
396 | + exec($snmpCommand,$this->objectsAll,$retVal); |
|
397 | + if ($retVal!=0) |
|
398 | + { |
|
399 | + $this->logging->log('error executing snmptranslate',ERROR,''); |
|
400 | + } |
|
401 | + // Count elements to show progress |
|
402 | + $numElements=count($this->objectsAll); |
|
403 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
404 | + return $numElements; |
|
405 | + } |
|
406 | 406 | |
407 | - /** |
|
408 | - * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
|
409 | - */ |
|
410 | - private function load_mibs_from_db() |
|
411 | - { |
|
412 | - // Get all mibs from databse to have a memory index |
|
407 | + /** |
|
408 | + * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
|
409 | + */ |
|
410 | + private function load_mibs_from_db() |
|
411 | + { |
|
412 | + // Get all mibs from databse to have a memory index |
|
413 | 413 | |
414 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
414 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
415 | 415 | |
416 | - $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
417 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
418 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
419 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
420 | - } |
|
421 | - $this->dbOidAll=$ret_code->fetchAll(); |
|
422 | - $this->dbOidIndex=array(); |
|
423 | - // Create the index for db; |
|
424 | - foreach($this->dbOidAll as $key=>$val) |
|
425 | - { |
|
426 | - $this->dbOidIndex[$val['oid']]['key']=$key; |
|
427 | - $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
428 | - } |
|
429 | - } |
|
416 | + $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
417 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
418 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
419 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
420 | + } |
|
421 | + $this->dbOidAll=$ret_code->fetchAll(); |
|
422 | + $this->dbOidIndex=array(); |
|
423 | + // Create the index for db; |
|
424 | + foreach($this->dbOidAll as $key=>$val) |
|
425 | + { |
|
426 | + $this->dbOidIndex[$val['oid']]['key']=$key; |
|
427 | + $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
428 | + } |
|
429 | + } |
|
430 | 430 | |
431 | - /** |
|
432 | - * Reset all update timers & count to zero |
|
433 | - */ |
|
434 | - private function reset_update_timers() |
|
435 | - { |
|
436 | - $this->timing['base_parse_time']=0; |
|
437 | - $this->timing['base_check_time']=0; |
|
438 | - $this->timing['type0_check_time']=0; |
|
439 | - $this->timing['nottrap_time']=0; |
|
440 | - $this->timing['update_time']=0; |
|
441 | - $this->timing['objects_time']=0; |
|
442 | - $this->timing['base_parse_num']=0; |
|
443 | - $this->timing['base_check_num']=0; |
|
444 | - $this->timing['type0_check_num']=0; |
|
445 | - $this->timing['nottrap_num']=0; |
|
446 | - $this->timing['update_num']=0; |
|
447 | - $this->timing['objects_num']=0; |
|
448 | - $this->timing['num_traps']=0; |
|
449 | - } |
|
431 | + /** |
|
432 | + * Reset all update timers & count to zero |
|
433 | + */ |
|
434 | + private function reset_update_timers() |
|
435 | + { |
|
436 | + $this->timing['base_parse_time']=0; |
|
437 | + $this->timing['base_check_time']=0; |
|
438 | + $this->timing['type0_check_time']=0; |
|
439 | + $this->timing['nottrap_time']=0; |
|
440 | + $this->timing['update_time']=0; |
|
441 | + $this->timing['objects_time']=0; |
|
442 | + $this->timing['base_parse_num']=0; |
|
443 | + $this->timing['base_check_num']=0; |
|
444 | + $this->timing['type0_check_num']=0; |
|
445 | + $this->timing['nottrap_num']=0; |
|
446 | + $this->timing['update_num']=0; |
|
447 | + $this->timing['objects_num']=0; |
|
448 | + $this->timing['num_traps']=0; |
|
449 | + } |
|
450 | 450 | |
451 | - /** |
|
452 | - * Detect if $this->objectsAll[$curElement] is a trap |
|
453 | - * @param integer $curElement |
|
454 | - * @param bool $onlyTraps : set to false to get all and not only traps. |
|
455 | - * @return boolean : false if it's a trap , true if not |
|
456 | - */ |
|
457 | - private function detect_trap($curElement,$onlyTraps) |
|
458 | - { |
|
459 | - // Get oid or pass if not found |
|
460 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
461 | - { |
|
462 | - $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
463 | - $this->timing['base_parse_num'] ++; |
|
464 | - return true; |
|
465 | - } |
|
466 | - $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
|
451 | + /** |
|
452 | + * Detect if $this->objectsAll[$curElement] is a trap |
|
453 | + * @param integer $curElement |
|
454 | + * @param bool $onlyTraps : set to false to get all and not only traps. |
|
455 | + * @return boolean : false if it's a trap , true if not |
|
456 | + */ |
|
457 | + private function detect_trap($curElement,$onlyTraps) |
|
458 | + { |
|
459 | + // Get oid or pass if not found |
|
460 | + if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
461 | + { |
|
462 | + $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
463 | + $this->timing['base_parse_num'] ++; |
|
464 | + return true; |
|
465 | + } |
|
466 | + $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
|
467 | 467 | |
468 | - // get next line |
|
469 | - $curElement++; |
|
470 | - $match=$snmptrans=array(); |
|
471 | - if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
472 | - $this->objectsAll[$curElement],$match)) |
|
473 | - { |
|
474 | - $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
475 | - $this->timing['base_check_num']++; |
|
476 | - return true; |
|
477 | - } |
|
468 | + // get next line |
|
469 | + $curElement++; |
|
470 | + $match=$snmptrans=array(); |
|
471 | + if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
472 | + $this->objectsAll[$curElement],$match)) |
|
473 | + { |
|
474 | + $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
475 | + $this->timing['base_check_num']++; |
|
476 | + return true; |
|
477 | + } |
|
478 | 478 | |
479 | - $this->oidDesc['name']=$match[1]; // Name |
|
480 | - $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
479 | + $this->oidDesc['name']=$match[1]; // Name |
|
480 | + $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
481 | 481 | |
482 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
483 | - { |
|
484 | - // Check if next is suboid -> in that case is cannot be a trap |
|
485 | - if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
486 | - { |
|
487 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
488 | - $this->timing['type0_check_num']++; |
|
489 | - return true; |
|
490 | - } |
|
491 | - unset($snmptrans); |
|
492 | - $retVal=0; |
|
493 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
494 | - ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
495 | - if ($retVal!=0) |
|
496 | - { |
|
497 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
498 | - $this->timing['type0_check_num']++; |
|
499 | - return true; |
|
500 | - } |
|
501 | - //echo "\n v1 trap found : $this->oidDesc['oid'] \n"; |
|
502 | - // Force as trap. |
|
503 | - $this->oidDesc['type']=21; |
|
504 | - } |
|
505 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
506 | - { |
|
507 | - $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
508 | - $this->timing['nottrap_num']++; |
|
509 | - return true; |
|
510 | - } |
|
511 | - return false; |
|
512 | - } |
|
482 | + if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
483 | + { |
|
484 | + // Check if next is suboid -> in that case is cannot be a trap |
|
485 | + if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
486 | + { |
|
487 | + $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
488 | + $this->timing['type0_check_num']++; |
|
489 | + return true; |
|
490 | + } |
|
491 | + unset($snmptrans); |
|
492 | + $retVal=0; |
|
493 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
494 | + ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
495 | + if ($retVal!=0) |
|
496 | + { |
|
497 | + $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
498 | + $this->timing['type0_check_num']++; |
|
499 | + return true; |
|
500 | + } |
|
501 | + //echo "\n v1 trap found : $this->oidDesc['oid'] \n"; |
|
502 | + // Force as trap. |
|
503 | + $this->oidDesc['type']=21; |
|
504 | + } |
|
505 | + if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
506 | + { |
|
507 | + $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
508 | + $this->timing['nottrap_num']++; |
|
509 | + return true; |
|
510 | + } |
|
511 | + return false; |
|
512 | + } |
|
513 | 513 | |
514 | - /** |
|
515 | - * get_trap_mib_description |
|
516 | - * @return array|null : array of snmptranslate output or null on error |
|
517 | - **/ |
|
518 | - private function get_trap_mib_description() |
|
519 | - { |
|
520 | - $retVal=0; |
|
521 | - $match=$snmptrans=array(); |
|
522 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
523 | - ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
524 | - if ($retVal!=0) |
|
525 | - { |
|
526 | - $this->logging->log('error executing snmptranslate',ERROR); |
|
527 | - return $snmptrans; |
|
528 | - } |
|
514 | + /** |
|
515 | + * get_trap_mib_description |
|
516 | + * @return array|null : array of snmptranslate output or null on error |
|
517 | + **/ |
|
518 | + private function get_trap_mib_description() |
|
519 | + { |
|
520 | + $retVal=0; |
|
521 | + $match=$snmptrans=array(); |
|
522 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
523 | + ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
524 | + if ($retVal!=0) |
|
525 | + { |
|
526 | + $this->logging->log('error executing snmptranslate',ERROR); |
|
527 | + return $snmptrans; |
|
528 | + } |
|
529 | 529 | |
530 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
531 | - { |
|
532 | - $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
533 | - return $snmptrans; |
|
534 | - } |
|
535 | - $this->oidDesc['mib']=$match[1]; |
|
530 | + if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
531 | + { |
|
532 | + $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
533 | + return $snmptrans; |
|
534 | + } |
|
535 | + $this->oidDesc['mib']=$match[1]; |
|
536 | 536 | |
537 | - $numLine=1; |
|
538 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
539 | - if (isset($snmptrans[$numLine])) |
|
540 | - { |
|
541 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
537 | + $numLine=1; |
|
538 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
539 | + if (isset($snmptrans[$numLine])) |
|
540 | + { |
|
541 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
542 | 542 | |
543 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
544 | - { |
|
545 | - $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
546 | - $numLine++; |
|
547 | - } |
|
548 | - if (isset($snmptrans[$numLine])) { |
|
549 | - $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
550 | - $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
551 | - } |
|
543 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
544 | + { |
|
545 | + $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
546 | + $numLine++; |
|
547 | + } |
|
548 | + if (isset($snmptrans[$numLine])) { |
|
549 | + $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
550 | + $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
551 | + } |
|
552 | 552 | |
553 | - } |
|
554 | - return $snmptrans; |
|
555 | - } |
|
553 | + } |
|
554 | + return $snmptrans; |
|
555 | + } |
|
556 | 556 | |
557 | - /** |
|
558 | - * Get trap objects |
|
559 | - * @param array $snmptrans : output of snmptranslate for TrapModuleConfig |
|
560 | - * @return array|null : array of objects or null if not found |
|
561 | - **/ |
|
562 | - private function get_trap_objects($snmptrans) |
|
563 | - { |
|
564 | - $objectName=null; |
|
565 | - $match=array(); |
|
566 | - foreach ($snmptrans as $line) |
|
567 | - { |
|
568 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
569 | - { |
|
570 | - $objectName=$match[1]; |
|
571 | - } |
|
572 | - } |
|
573 | - if ($objectName == null) |
|
574 | - { |
|
575 | - $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
|
576 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
577 | - return null; |
|
578 | - } |
|
557 | + /** |
|
558 | + * Get trap objects |
|
559 | + * @param array $snmptrans : output of snmptranslate for TrapModuleConfig |
|
560 | + * @return array|null : array of objects or null if not found |
|
561 | + **/ |
|
562 | + private function get_trap_objects($snmptrans) |
|
563 | + { |
|
564 | + $objectName=null; |
|
565 | + $match=array(); |
|
566 | + foreach ($snmptrans as $line) |
|
567 | + { |
|
568 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
569 | + { |
|
570 | + $objectName=$match[1]; |
|
571 | + } |
|
572 | + } |
|
573 | + if ($objectName == null) |
|
574 | + { |
|
575 | + $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
|
576 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
577 | + return null; |
|
578 | + } |
|
579 | 579 | |
580 | - $trapObjects=array(); |
|
581 | - while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
|
582 | - { |
|
583 | - array_push($trapObjects,$match[1]); |
|
584 | - $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
|
585 | - } |
|
586 | - return $trapObjects; |
|
587 | - } |
|
580 | + $trapObjects=array(); |
|
581 | + while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
|
582 | + { |
|
583 | + array_push($trapObjects,$match[1]); |
|
584 | + $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
|
585 | + } |
|
586 | + return $trapObjects; |
|
587 | + } |
|
588 | 588 | |
589 | - /** |
|
590 | - * Cache mib in database |
|
591 | - * @param boolean $display_progress : Display progress on standard output |
|
592 | - * @param boolean $check_change : Force check of trap params & objects |
|
593 | - * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
594 | - * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
595 | - */ |
|
596 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
597 | - { |
|
598 | - // Global Timing |
|
599 | - $timeTaken = microtime(true); |
|
589 | + /** |
|
590 | + * Cache mib in database |
|
591 | + * @param boolean $display_progress : Display progress on standard output |
|
592 | + * @param boolean $check_change : Force check of trap params & objects |
|
593 | + * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
594 | + * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
595 | + */ |
|
596 | + public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
597 | + { |
|
598 | + // Global Timing |
|
599 | + $timeTaken = microtime(true); |
|
600 | 600 | |
601 | - $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
|
601 | + $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
|
602 | 602 | |
603 | - $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
|
603 | + $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
|
604 | 604 | |
605 | - $step=$basestep=$numElements/10; // output display of % done |
|
606 | - $num_step=0; |
|
607 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
605 | + $step=$basestep=$numElements/10; // output display of % done |
|
606 | + $num_step=0; |
|
607 | + $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
608 | 608 | |
609 | - // Create index for trap objects |
|
610 | - $this->trapObjectsIndex=array(); |
|
609 | + // Create index for trap objects |
|
610 | + $this->trapObjectsIndex=array(); |
|
611 | 611 | |
612 | - // detailed timing (time_* vars) |
|
613 | - $this->reset_update_timers(); |
|
612 | + // detailed timing (time_* vars) |
|
613 | + $this->reset_update_timers(); |
|
614 | 614 | |
615 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
616 | - { |
|
617 | - $this->timing['base_time']= microtime(true); |
|
618 | - if ($display_progress) |
|
619 | - { |
|
620 | - if ((microtime(true)-$timeFiveSec) > 2) |
|
621 | - { // echo a . every 2 sec |
|
622 | - echo '.'; |
|
623 | - $timeFiveSec = microtime(true); |
|
624 | - } |
|
625 | - if ($curElement>$step) |
|
626 | - { // display progress |
|
627 | - $num_step++; |
|
628 | - $step+=$basestep; |
|
629 | - echo "\n" . ($num_step*10). '% : '; |
|
630 | - } |
|
631 | - } |
|
615 | + for ($curElement=0;$curElement < $numElements;$curElement++) |
|
616 | + { |
|
617 | + $this->timing['base_time']= microtime(true); |
|
618 | + if ($display_progress) |
|
619 | + { |
|
620 | + if ((microtime(true)-$timeFiveSec) > 2) |
|
621 | + { // echo a . every 2 sec |
|
622 | + echo '.'; |
|
623 | + $timeFiveSec = microtime(true); |
|
624 | + } |
|
625 | + if ($curElement>$step) |
|
626 | + { // display progress |
|
627 | + $num_step++; |
|
628 | + $step+=$basestep; |
|
629 | + echo "\n" . ($num_step*10). '% : '; |
|
630 | + } |
|
631 | + } |
|
632 | 632 | |
633 | - $this->reset_oidDesc(); |
|
634 | - if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
635 | - { |
|
636 | - continue; |
|
637 | - } |
|
633 | + $this->reset_oidDesc(); |
|
634 | + if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
635 | + { |
|
636 | + continue; |
|
637 | + } |
|
638 | 638 | |
639 | - $this->timing['num_traps']++; |
|
639 | + $this->timing['num_traps']++; |
|
640 | 640 | |
641 | - $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
642 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
641 | + $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
642 | + if ($display_progress) echo '#'; // echo a # when trap found |
|
643 | 643 | |
644 | - // get trap objects & source MIB |
|
644 | + // get trap objects & source MIB |
|
645 | 645 | |
646 | - $snmptrans=$this->get_trap_mib_description(); // get MIB & description |
|
646 | + $snmptrans=$this->get_trap_mib_description(); // get MIB & description |
|
647 | 647 | |
648 | 648 | |
649 | - $update=$this->update_oid(); // Do update of trap. |
|
649 | + $update=$this->update_oid(); // Do update of trap. |
|
650 | 650 | |
651 | - $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
652 | - $this->timing['update_num']++; |
|
651 | + $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
652 | + $this->timing['update_num']++; |
|
653 | 653 | |
654 | - $this->timing['base_time']= microtime(true); // Reset to check object time |
|
654 | + $this->timing['base_time']= microtime(true); // Reset to check object time |
|
655 | 655 | |
656 | - if (($update==0) && ($check_change===false)) |
|
657 | - { // Trapd didn't change & force check disabled |
|
658 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
659 | - if ($display_progress) echo "C"; |
|
660 | - continue; |
|
661 | - } |
|
656 | + if (($update==0) && ($check_change===false)) |
|
657 | + { // Trapd didn't change & force check disabled |
|
658 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
659 | + if ($display_progress) echo "C"; |
|
660 | + continue; |
|
661 | + } |
|
662 | 662 | |
663 | - $trapObjects=$this->get_trap_objects($snmptrans); // Get trap objects from snmptranslate output |
|
664 | - if ($trapObjects == null) |
|
665 | - { |
|
666 | - continue; |
|
667 | - } |
|
663 | + $trapObjects=$this->get_trap_objects($snmptrans); // Get trap objects from snmptranslate output |
|
664 | + if ($trapObjects == null) |
|
665 | + { |
|
666 | + continue; |
|
667 | + } |
|
668 | 668 | |
669 | - $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
|
669 | + $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
|
670 | 670 | |
671 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
672 | - $this->timing['objects_num']++; |
|
673 | - } |
|
671 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
672 | + $this->timing['objects_num']++; |
|
673 | + } |
|
674 | 674 | |
675 | - if ($display_progress) |
|
676 | - { |
|
677 | - echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
678 | - echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
679 | - echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
680 | - echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
681 | - echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
675 | + if ($display_progress) |
|
676 | + { |
|
677 | + echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
678 | + echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
679 | + echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
680 | + echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
681 | + echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
682 | 682 | |
683 | - $timeTaken=microtime(true) - $timeTaken; |
|
684 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
685 | - } |
|
686 | - } |
|
683 | + $timeTaken=microtime(true) - $timeTaken; |
|
684 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
685 | + } |
|
686 | + } |
|
687 | 687 | |
688 | 688 | |
689 | 689 | } |
690 | 690 | \ No newline at end of file |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * @param Logging $logClass : where to log |
33 | 33 | * @param Database $dbClass : Database |
34 | 34 | */ |
35 | - function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
35 | + function __construct($logClass, $dbClass, $snmptrans, $snmptransdir) |
|
36 | 36 | { |
37 | 37 | $this->logging=$logClass; |
38 | 38 | $this->trapsDB=$dbClass; |
@@ -56,9 +56,9 @@ discard block |
||
56 | 56 | } |
57 | 57 | $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
58 | 58 | $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
59 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
59 | + if ($this->oidDesc['name'] != $dbOid['name'] || |
|
60 | 60 | $this->oidDesc['mib'] != $dbOid['mib'] || |
61 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
61 | + $this->oidDesc['type'] != $dbOid['type'] |
|
62 | 62 | ) |
63 | 63 | { // Do update |
64 | 64 | $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
@@ -72,22 +72,22 @@ discard block |
||
72 | 72 | ':type' => $this->oidDesc['type'], |
73 | 73 | ':mib' => $this->oidDesc['mib'], |
74 | 74 | ':tc' => $this->oidDesc['textconv']??'null', |
75 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
76 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
75 | + ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
76 | + ':syntax' => $this->oidDesc['syntax'] == null??'null', |
|
77 | 77 | ':type_enum' => $this->oidDesc['type_enum']??'null', |
78 | 78 | ':description' => $this->oidDesc['description']??'null', |
79 | 79 | ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
80 | 80 | ); |
81 | 81 | |
82 | 82 | if ($sqlQuery->execute($sqlParam) === false) { |
83 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
83 | + $this->logging->log('Error in query : '.$sql, ERROR, ''); |
|
84 | 84 | } |
85 | - $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
85 | + $this->logging->log('Trap updated : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
86 | 86 | return 1; |
87 | 87 | } |
88 | 88 | else |
89 | 89 | { |
90 | - $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
90 | + $this->logging->log('Trap unchanged : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
91 | 91 | return 0; |
92 | 92 | } |
93 | 93 | } |
@@ -103,11 +103,11 @@ discard block |
||
103 | 103 | $db_conn=$this->trapsDB->db_connect_trap(); |
104 | 104 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
105 | 105 | '(oid, name, type , mib, textual_convention, display_hint '. |
106 | - ', syntax, type_enum , description ) ' . |
|
106 | + ', syntax, type_enum , description ) '. |
|
107 | 107 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
108 | 108 | ', :syntax, :type_enum, :description )'; |
109 | 109 | |
110 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
110 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql.='RETURNING id'; |
|
111 | 111 | |
112 | 112 | $sqlQuery=$db_conn->prepare($sql); |
113 | 113 | |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | ); |
125 | 125 | |
126 | 126 | if ($sqlQuery->execute($sqlParam) === false) { |
127 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
127 | + $this->logging->log('Error in query : '.$sql, 1, ''); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | switch ($this->trapsDB->trapDBType) |
@@ -132,10 +132,10 @@ discard block |
||
132 | 132 | case 'pgsql': |
133 | 133 | // Get last id to insert oid/values in secondary table |
134 | 134 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
135 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
135 | + $this->logging->log('Error getting id - pgsql - ', 1, ''); |
|
136 | 136 | } |
137 | - if (! isset($inserted_id_ret['id'])) { |
|
138 | - $this->logging->log('Error getting id - pgsql - empty.',ERROR); |
|
137 | + if (!isset($inserted_id_ret['id'])) { |
|
138 | + $this->logging->log('Error getting id - pgsql - empty.', ERROR); |
|
139 | 139 | return 0; |
140 | 140 | } |
141 | 141 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
@@ -144,16 +144,16 @@ discard block |
||
144 | 144 | // Get last id to insert oid/values in secondary table |
145 | 145 | $sql='SELECT LAST_INSERT_ID();'; |
146 | 146 | if (($ret_code=$db_conn->query($sql)) === false) { |
147 | - $this->logging->log('Erreur getting id - mysql - ',ERROR); |
|
147 | + $this->logging->log('Erreur getting id - mysql - ', ERROR); |
|
148 | 148 | return 0; |
149 | 149 | } |
150 | 150 | |
151 | 151 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
152 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
152 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
153 | 153 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
154 | 154 | break; |
155 | 155 | default: |
156 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,ERROR); |
|
156 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType, ERROR); |
|
157 | 157 | return 0; |
158 | 158 | } |
159 | 159 | |
@@ -193,9 +193,9 @@ discard block |
||
193 | 193 | $db_conn=$this->trapsDB->db_connect_trap(); |
194 | 194 | // Get all objects |
195 | 195 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
196 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
196 | + $this->logging->log('SQL query get all traps: '.$sql, DEBUG); |
|
197 | 197 | if (($ret_code=$db_conn->query($sql)) === false) { |
198 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
198 | + $this->logging->log('No result in query : '.$sql, 1, ''); |
|
199 | 199 | } |
200 | 200 | $dbObjectsRaw=$ret_code->fetchAll(); |
201 | 201 | |
@@ -212,30 +212,30 @@ discard block |
||
212 | 212 | * @param string $trapmib : mib of trap |
213 | 213 | * @return NULL|array : null if not found, or output of snmptranslate |
214 | 214 | */ |
215 | - private function get_object_details($object,$trapmib) |
|
215 | + private function get_object_details($object, $trapmib) |
|
216 | 216 | { |
217 | 217 | $match=$snmptrans=array(); |
218 | 218 | $retVal=0; |
219 | 219 | $this->oidDesc['mib']=$trapmib; |
220 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
221 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
222 | - if ($retVal!=0) |
|
220 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
221 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
222 | + if ($retVal != 0) |
|
223 | 223 | { |
224 | 224 | // Maybe not trap mib, search with IR |
225 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
226 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
227 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
225 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
226 | + ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
227 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match)) |
|
228 | 228 | { // Not found -> continue with warning |
229 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
229 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, ''); |
|
230 | 230 | return null; |
231 | 231 | } |
232 | 232 | $this->oidDesc['mib']=$match[1]; |
233 | 233 | |
234 | 234 | // Do the snmptranslate again. |
235 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
236 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
237 | - if ($retVal!=0) { |
|
238 | - $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
235 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
236 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object, $snmptrans, $retVal); |
|
237 | + if ($retVal != 0) { |
|
238 | + $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object, 2, ''); |
|
239 | 239 | return null; |
240 | 240 | } |
241 | 241 | |
@@ -255,12 +255,12 @@ discard block |
||
255 | 255 | |
256 | 256 | foreach ($snmptrans as $line) |
257 | 257 | { |
258 | - if ($indesc===true) |
|
258 | + if ($indesc === true) |
|
259 | 259 | { |
260 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
261 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
260 | + $line=preg_replace('/[\t ]+/', ' ', $line); |
|
261 | + if (preg_match('/(.*)"$/', $line, $match)) |
|
262 | 262 | { |
263 | - $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
263 | + $this->oidDesc['description']=$tmpdesc.$match[1]; |
|
264 | 264 | $indesc=false; |
265 | 265 | } |
266 | 266 | $tmpdesc.=$line; |
@@ -271,34 +271,34 @@ discard block |
||
271 | 271 | $this->oidDesc['oid']=$line; |
272 | 272 | continue; |
273 | 273 | } |
274 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
274 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match)) |
|
275 | 275 | { |
276 | 276 | $this->oidDesc['syntax']=$match[1]; |
277 | 277 | $this->oidDesc['type_enum']=$match[2]; |
278 | 278 | continue; |
279 | 279 | } |
280 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
280 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match)) |
|
281 | 281 | { |
282 | 282 | $this->oidDesc['syntax']=$match[1]; |
283 | 283 | continue; |
284 | 284 | } |
285 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
285 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match)) |
|
286 | 286 | { |
287 | 287 | $this->oidDesc['dispHint']=$match[1]; |
288 | 288 | continue; |
289 | 289 | } |
290 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
290 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match)) |
|
291 | 291 | { |
292 | 292 | $this->oidDesc['description']=$match[1]; |
293 | 293 | continue; |
294 | 294 | } |
295 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
295 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match)) |
|
296 | 296 | { |
297 | 297 | $tmpdesc=$match[1]; |
298 | 298 | $indesc=true; |
299 | 299 | continue; |
300 | 300 | } |
301 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
301 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match)) |
|
302 | 302 | { |
303 | 303 | $this->oidDesc['textconv']=$match[1]; |
304 | 304 | continue; |
@@ -313,9 +313,9 @@ discard block |
||
313 | 313 | * @param array $objects : array of objects name (without MIB) |
314 | 314 | * @param bool $check_existing : check instead of create |
315 | 315 | */ |
316 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
316 | + public function trap_objects($trapOID, $trapmib, $objects, $check_existing) |
|
317 | 317 | { |
318 | - $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
318 | + $trapId=$this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
319 | 319 | |
320 | 320 | if ($check_existing === true) |
321 | 321 | { |
@@ -332,9 +332,9 @@ discard block |
||
332 | 332 | |
333 | 333 | $this->parse_object($snmptrans); |
334 | 334 | |
335 | - $this->oidDesc['name'] = $object; |
|
335 | + $this->oidDesc['name']=$object; |
|
336 | 336 | |
337 | - $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
337 | + $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'], DEBUG); |
|
338 | 338 | |
339 | 339 | // Update |
340 | 340 | $this->update_oid(); |
@@ -359,7 +359,7 @@ discard block |
||
359 | 359 | ); |
360 | 360 | |
361 | 361 | if ($sqlQuery->execute($sqlParam) === false) { |
362 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
362 | + $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$this->oidDesc['oid']]['id'], 1, ''); |
|
363 | 363 | } |
364 | 364 | } |
365 | 365 | if ($check_existing === true) |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | $this->oidDesc['type']=null; |
377 | 377 | $this->oidDesc['mib']=null; |
378 | 378 | $this->oidDesc['textconv']=null; |
379 | - $this->oidDesc['dispHint'] =null; |
|
379 | + $this->oidDesc['dispHint']=null; |
|
380 | 380 | $this->oidDesc['syntax']=null; |
381 | 381 | $this->oidDesc['type_enum']=null; |
382 | 382 | $this->oidDesc['description']=null; |
@@ -390,17 +390,17 @@ discard block |
||
390 | 390 | { |
391 | 391 | $retVal=0; |
392 | 392 | // Get all mib objects from all mibs |
393 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
394 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
393 | + $snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
394 | + $this->logging->log('Getting all traps : '.$snmpCommand, DEBUG); |
|
395 | 395 | unset($this->objectsAll); |
396 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
397 | - if ($retVal!=0) |
|
396 | + exec($snmpCommand, $this->objectsAll, $retVal); |
|
397 | + if ($retVal != 0) |
|
398 | 398 | { |
399 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
399 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
400 | 400 | } |
401 | 401 | // Count elements to show progress |
402 | 402 | $numElements=count($this->objectsAll); |
403 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
403 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO); |
|
404 | 404 | return $numElements; |
405 | 405 | } |
406 | 406 | |
@@ -414,14 +414,14 @@ discard block |
||
414 | 414 | $db_conn=$this->trapsDB->db_connect_trap(); |
415 | 415 | |
416 | 416 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
417 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
417 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
418 | 418 | if (($ret_code=$db_conn->query($sql)) === false) { |
419 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
419 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
420 | 420 | } |
421 | 421 | $this->dbOidAll=$ret_code->fetchAll(); |
422 | 422 | $this->dbOidIndex=array(); |
423 | 423 | // Create the index for db; |
424 | - foreach($this->dbOidAll as $key=>$val) |
|
424 | + foreach ($this->dbOidAll as $key=>$val) |
|
425 | 425 | { |
426 | 426 | $this->dbOidIndex[$val['oid']]['key']=$key; |
427 | 427 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
@@ -454,13 +454,13 @@ discard block |
||
454 | 454 | * @param bool $onlyTraps : set to false to get all and not only traps. |
455 | 455 | * @return boolean : false if it's a trap , true if not |
456 | 456 | */ |
457 | - private function detect_trap($curElement,$onlyTraps) |
|
457 | + private function detect_trap($curElement, $onlyTraps) |
|
458 | 458 | { |
459 | 459 | // Get oid or pass if not found |
460 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
460 | + if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement])) |
|
461 | 461 | { |
462 | - $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
463 | - $this->timing['base_parse_num'] ++; |
|
462 | + $this->timing['base_parse_time']+=microtime(true) - $this->timing['base_time']; |
|
463 | + $this->timing['base_parse_num']++; |
|
464 | 464 | return true; |
465 | 465 | } |
466 | 466 | $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
@@ -469,9 +469,9 @@ discard block |
||
469 | 469 | $curElement++; |
470 | 470 | $match=$snmptrans=array(); |
471 | 471 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
472 | - $this->objectsAll[$curElement],$match)) |
|
472 | + $this->objectsAll[$curElement], $match)) |
|
473 | 473 | { |
474 | - $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
474 | + $this->timing['base_check_time']+=microtime(true) - $this->timing['base_time']; |
|
475 | 475 | $this->timing['base_check_num']++; |
476 | 476 | return true; |
477 | 477 | } |
@@ -479,22 +479,22 @@ discard block |
||
479 | 479 | $this->oidDesc['name']=$match[1]; // Name |
480 | 480 | $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
481 | 481 | |
482 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
482 | + if ($this->oidDesc['type'] == 0) // object type=0 : check if v1 trap |
|
483 | 483 | { |
484 | 484 | // Check if next is suboid -> in that case is cannot be a trap |
485 | - if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
485 | + if (preg_match("/^".$this->oidDesc['oid']."/", $this->objectsAll[$curElement + 1])) |
|
486 | 486 | { |
487 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
487 | + $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time']; |
|
488 | 488 | $this->timing['type0_check_num']++; |
489 | 489 | return true; |
490 | 490 | } |
491 | 491 | unset($snmptrans); |
492 | 492 | $retVal=0; |
493 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
494 | - ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
495 | - if ($retVal!=0) |
|
493 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
494 | + ' -Td '.$this->oidDesc['oid'].' | grep OBJECTS ', $snmptrans, $retVal); |
|
495 | + if ($retVal != 0) |
|
496 | 496 | { |
497 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
497 | + $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time']; |
|
498 | 498 | $this->timing['type0_check_num']++; |
499 | 499 | return true; |
500 | 500 | } |
@@ -502,9 +502,9 @@ discard block |
||
502 | 502 | // Force as trap. |
503 | 503 | $this->oidDesc['type']=21; |
504 | 504 | } |
505 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
505 | + if ($onlyTraps === true && $this->oidDesc['type'] != 21) // if only traps and not a trap, continue |
|
506 | 506 | { |
507 | - $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
507 | + $this->timing['nottrap_time']+=microtime(true) - $this->timing['base_time']; |
|
508 | 508 | $this->timing['nottrap_num']++; |
509 | 509 | return true; |
510 | 510 | } |
@@ -519,35 +519,35 @@ discard block |
||
519 | 519 | { |
520 | 520 | $retVal=0; |
521 | 521 | $match=$snmptrans=array(); |
522 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
523 | - ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
524 | - if ($retVal!=0) |
|
522 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
523 | + ' -Td '.$this->oidDesc['oid'], $snmptrans, $retVal); |
|
524 | + if ($retVal != 0) |
|
525 | 525 | { |
526 | - $this->logging->log('error executing snmptranslate',ERROR); |
|
526 | + $this->logging->log('error executing snmptranslate', ERROR); |
|
527 | 527 | return $snmptrans; |
528 | 528 | } |
529 | 529 | |
530 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
530 | + if (!preg_match('/^(.*)::/', $snmptrans[0], $match)) |
|
531 | 531 | { |
532 | - $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
532 | + $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : '.$snmptrans[0], ERROR); |
|
533 | 533 | return $snmptrans; |
534 | 534 | } |
535 | 535 | $this->oidDesc['mib']=$match[1]; |
536 | 536 | |
537 | 537 | $numLine=1; |
538 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
538 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++; |
|
539 | 539 | if (isset($snmptrans[$numLine])) |
540 | 540 | { |
541 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
541 | + $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]); |
|
542 | 542 | |
543 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
543 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine])) |
|
544 | 544 | { |
545 | - $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
545 | + $this->oidDesc['description'].=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]); |
|
546 | 546 | $numLine++; |
547 | 547 | } |
548 | 548 | if (isset($snmptrans[$numLine])) { |
549 | - $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
550 | - $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
549 | + $this->oidDesc['description'].=preg_replace('/".*/', '', $snmptrans[$numLine]); |
|
550 | + $this->oidDesc['description']=preg_replace('/[\t ]+/', ' ', $this->oidDesc['description']); |
|
551 | 551 | } |
552 | 552 | |
553 | 553 | } |
@@ -565,23 +565,23 @@ discard block |
||
565 | 565 | $match=array(); |
566 | 566 | foreach ($snmptrans as $line) |
567 | 567 | { |
568 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
568 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match)) |
|
569 | 569 | { |
570 | 570 | $objectName=$match[1]; |
571 | 571 | } |
572 | 572 | } |
573 | 573 | if ($objectName == null) |
574 | 574 | { |
575 | - $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
|
576 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
575 | + $this->logging->log('No objects for '.$this->oidDesc['oid'], DEBUG); |
|
576 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
577 | 577 | return null; |
578 | 578 | } |
579 | 579 | |
580 | 580 | $trapObjects=array(); |
581 | - while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
|
581 | + while (preg_match('/ *([^ ,]+) *,* */', $objectName, $match)) |
|
582 | 582 | { |
583 | - array_push($trapObjects,$match[1]); |
|
584 | - $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
|
583 | + array_push($trapObjects, $match[1]); |
|
584 | + $objectName=preg_replace('/'.$match[0].'/', '', $objectName); |
|
585 | 585 | } |
586 | 586 | return $trapObjects; |
587 | 587 | } |
@@ -593,18 +593,18 @@ discard block |
||
593 | 593 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
594 | 594 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
595 | 595 | */ |
596 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
596 | + public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1') |
|
597 | 597 | { |
598 | 598 | // Global Timing |
599 | - $timeTaken = microtime(true); |
|
599 | + $timeTaken=microtime(true); |
|
600 | 600 | |
601 | 601 | $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
602 | 602 | |
603 | 603 | $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
604 | 604 | |
605 | - $step=$basestep=$numElements/10; // output display of % done |
|
605 | + $step=$basestep=$numElements / 10; // output display of % done |
|
606 | 606 | $num_step=0; |
607 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
607 | + $timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds |
|
608 | 608 | |
609 | 609 | // Create index for trap objects |
610 | 610 | $this->trapObjectsIndex=array(); |
@@ -612,33 +612,33 @@ discard block |
||
612 | 612 | // detailed timing (time_* vars) |
613 | 613 | $this->reset_update_timers(); |
614 | 614 | |
615 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
615 | + for ($curElement=0; $curElement < $numElements; $curElement++) |
|
616 | 616 | { |
617 | - $this->timing['base_time']= microtime(true); |
|
617 | + $this->timing['base_time']=microtime(true); |
|
618 | 618 | if ($display_progress) |
619 | 619 | { |
620 | - if ((microtime(true)-$timeFiveSec) > 2) |
|
620 | + if ((microtime(true) - $timeFiveSec) > 2) |
|
621 | 621 | { // echo a . every 2 sec |
622 | 622 | echo '.'; |
623 | - $timeFiveSec = microtime(true); |
|
623 | + $timeFiveSec=microtime(true); |
|
624 | 624 | } |
625 | - if ($curElement>$step) |
|
625 | + if ($curElement > $step) |
|
626 | 626 | { // display progress |
627 | 627 | $num_step++; |
628 | 628 | $step+=$basestep; |
629 | - echo "\n" . ($num_step*10). '% : '; |
|
629 | + echo "\n".($num_step * 10).'% : '; |
|
630 | 630 | } |
631 | 631 | } |
632 | 632 | |
633 | 633 | $this->reset_oidDesc(); |
634 | - if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
634 | + if ($this->detect_trap($curElement, $onlyTraps) === true) |
|
635 | 635 | { |
636 | 636 | continue; |
637 | 637 | } |
638 | 638 | |
639 | 639 | $this->timing['num_traps']++; |
640 | 640 | |
641 | - $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
641 | + $this->logging->log('Found trap : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], INFO); |
|
642 | 642 | if ($display_progress) echo '#'; // echo a # when trap found |
643 | 643 | |
644 | 644 | // get trap objects & source MIB |
@@ -648,14 +648,14 @@ discard block |
||
648 | 648 | |
649 | 649 | $update=$this->update_oid(); // Do update of trap. |
650 | 650 | |
651 | - $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
651 | + $this->timing['update_time']+=microtime(true) - $this->timing['base_time']; |
|
652 | 652 | $this->timing['update_num']++; |
653 | 653 | |
654 | - $this->timing['base_time']= microtime(true); // Reset to check object time |
|
654 | + $this->timing['base_time']=microtime(true); // Reset to check object time |
|
655 | 655 | |
656 | - if (($update==0) && ($check_change===false)) |
|
656 | + if (($update == 0) && ($check_change === false)) |
|
657 | 657 | { // Trapd didn't change & force check disabled |
658 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
658 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
659 | 659 | if ($display_progress) echo "C"; |
660 | 660 | continue; |
661 | 661 | } |
@@ -668,17 +668,17 @@ discard block |
||
668 | 668 | |
669 | 669 | $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
670 | 670 | |
671 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
671 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
672 | 672 | $this->timing['objects_num']++; |
673 | 673 | } |
674 | 674 | |
675 | 675 | if ($display_progress) |
676 | 676 | { |
677 | - echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
678 | - echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
679 | - echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
680 | - echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
681 | - echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
677 | + echo "\nNumber of processed traps : ".$this->timing['num_traps']."\n"; |
|
678 | + echo "\nParsing : ".number_format($this->timing['base_parse_time'] + $this->timing['base_check_time'], 1)." sec / ".($this->timing['base_parse_num'] + $this->timing['base_check_num'])." occurences\n"; |
|
679 | + echo "Detecting traps : ".number_format($this->timing['type0_check_time'] + $this->timing['nottrap_time'], 1)." sec / ".($this->timing['type0_check_num'] + $this->timing['nottrap_num'])." occurences\n"; |
|
680 | + echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'], 1)." sec , "; |
|
681 | + echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'], 1)." sec \n"; |
|
682 | 682 | |
683 | 683 | $timeTaken=microtime(true) - $timeTaken; |
684 | 684 | echo "Global time : ".round($timeTaken)." seconds\n"; |
@@ -3,18 +3,18 @@ discard block |
||
3 | 3 | |
4 | 4 | require_once 'bin/trap_class.php'; |
5 | 5 | |
6 | -$options = getopt("r:d:"); |
|
6 | +$options=getopt("r:d:"); |
|
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); |
|
13 | -$trap->setLogging($debugLevel,'display'); |
|
12 | +$trap=new trap($icingaweb2Etc); |
|
13 | +$trap->setLogging($debugLevel, 'display'); |
|
14 | 14 | |
15 | -$input=array_key_exists('r',$options); |
|
15 | +$input=array_key_exists('r', $options); |
|
16 | 16 | |
17 | -if (! $input) { |
|
17 | +if (!$input) { |
|
18 | 18 | $inputStream=fopen('php://stdin', 'r'); |
19 | 19 | $rule=chop(fgets($inputStream)); |
20 | 20 | } else |
@@ -25,11 +25,11 @@ discard block |
||
25 | 25 | $rule=$trap->ruleClass->eval_cleanup($rule); |
26 | 26 | //echo 'After cleanup : #'.$rule."#\n"; |
27 | 27 | $item=0; |
28 | - $val = $trap->ruleClass->evaluation($rule,$item); |
|
29 | - if ($val==true) { printf( "true"); } else { printf( "false");} |
|
28 | + $val=$trap->ruleClass->evaluation($rule, $item); |
|
29 | + if ($val == true) { printf("true"); } else { printf("false"); } |
|
30 | 30 | printf("\n"); |
31 | 31 | } |
32 | -catch (Exception $e) { printf("%s\n",$e->getMessage()); exit(1);} |
|
32 | +catch (Exception $e) { printf("%s\n", $e->getMessage()); exit(1); } |
|
33 | 33 | |
34 | 34 | exit(0); |
35 | 35 | ?> |
@@ -17,8 +17,9 @@ discard block |
||
17 | 17 | if (! $input) { |
18 | 18 | $inputStream=fopen('php://stdin', 'r'); |
19 | 19 | $rule=chop(fgets($inputStream)); |
20 | -} else |
|
20 | +} else { |
|
21 | 21 | $rule=$options['r']; |
22 | +} |
|
22 | 23 | |
23 | 24 | try |
24 | 25 | { |
@@ -28,8 +29,7 @@ discard block |
||
28 | 29 | $val = $trap->ruleClass->evaluation($rule,$item); |
29 | 30 | if ($val==true) { printf( "true"); } else { printf( "false");} |
30 | 31 | printf("\n"); |
31 | -} |
|
32 | -catch (Exception $e) { printf("%s\n",$e->getMessage()); exit(1);} |
|
32 | +} catch (Exception $e) { printf("%s\n",$e->getMessage()); exit(1);} |
|
33 | 33 | |
34 | 34 | exit(0); |
35 | 35 | ?> |
@@ -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) { |
|
131 | - throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl)); |
|
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) { |
|
131 | + throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl)); |
|
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,9 +126,9 @@ 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) { |
131 | - throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl)); |
|
131 | + throw new Exception('CURL INIT ERROR: '.curl_error($this->curl)); |
|
132 | 132 | } |
133 | 133 | } |
134 | 134 | return $this->curl; |
@@ -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 | } |
@@ -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[string] $functions Functions of this plugin for rule eval*/ |
|
510 | - public $functions=array(); |
|
509 | + /** @var array[string] $functions Functions of this plugin for rule eval*/ |
|
510 | + public $functions=array(); |
|
511 | 511 | |
512 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
513 | - public $catchAllTraps=false; |
|
512 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */ |
|
513 | + public $catchAllTraps=false; |
|
514 | 514 | |
515 | - /** @var boolean $processTraps Set to true if plugins can handle traps */ |
|
516 | - public $processTraps=false; |
|
515 | + /** @var boolean $processTraps Set to true if plugins can handle traps */ |
|
516 | + public $processTraps=false; |
|
517 | 517 | |
518 | - /** |
|
519 | - * @param \Trapdirector\Logging $loggingClass |
|
520 | - */ |
|
521 | - public function setLoggingClass($loggingClass) |
|
522 | - { |
|
523 | - $this->loggingClass = $loggingClass; |
|
524 | - } |
|
518 | + /** |
|
519 | + * @param \Trapdirector\Logging $loggingClass |
|
520 | + */ |
|
521 | + public function setLoggingClass($loggingClass) |
|
522 | + { |
|
523 | + $this->loggingClass = $loggingClass; |
|
524 | + } |
|
525 | 525 | |
526 | - /** |
|
527 | - * |
|
528 | - * @param string $message |
|
529 | - * @param int $level DEBUG/INFO/WARN/CRIT |
|
530 | - */ |
|
531 | - public function log($message,$level) |
|
532 | - { |
|
533 | - $this->loggingClass->log('[ '.get_class($this).'] '. $message, $level); |
|
534 | - } |
|
526 | + /** |
|
527 | + * |
|
528 | + * @param string $message |
|
529 | + * @param int $level DEBUG/INFO/WARN/CRIT |
|
530 | + */ |
|
531 | + public function log($message,$level) |
|
532 | + { |
|
533 | + $this->loggingClass->log('[ '.get_class($this).'] '. $message, $level); |
|
534 | + } |
|
535 | 535 | } |
536 | 536 | \ No newline at end of file |
@@ -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 |
@@ -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 | } |