@@ -15,13 +15,13 @@ discard block |
||
15 | 15 | |
16 | 16 | try |
17 | 17 | { |
18 | - $Trap = new Trap($icingaweb2_etc); |
|
19 | - //$Trap = new Trap($icingaweb2_etc,4,'display'); // For debug |
|
20 | - //$Trap = new Trap($icingaweb2_etc,4,'syslog'); // For debug |
|
21 | - //$Trap->setLogging(4,'syslog'); |
|
18 | + $Trap = new Trap($icingaweb2_etc); |
|
19 | + //$Trap = new Trap($icingaweb2_etc,4,'display'); // For debug |
|
20 | + //$Trap = new Trap($icingaweb2_etc,4,'syslog'); // For debug |
|
21 | + //$Trap->setLogging(4,'syslog'); |
|
22 | 22 | |
23 | - // TODO : tranfer this to reset_trap cli command |
|
24 | - $Trap->eraseOldTraps(); |
|
23 | + // TODO : tranfer this to reset_trap cli command |
|
24 | + $Trap->eraseOldTraps(); |
|
25 | 25 | |
26 | 26 | $Trap->read_trap('php://stdin'); |
27 | 27 | |
@@ -34,15 +34,15 @@ discard block |
||
34 | 34 | } |
35 | 35 | catch (Exception $e) |
36 | 36 | { |
37 | - if ($Trap == null) |
|
38 | - { // Exception in trap creation : log in display & syslog |
|
39 | - $logging = new Logging(); |
|
40 | - $logging->log("Caught exception creating Trap class",2); |
|
41 | - } |
|
42 | - else |
|
43 | - { |
|
37 | + if ($Trap == null) |
|
38 | + { // Exception in trap creation : log in display & syslog |
|
39 | + $logging = new Logging(); |
|
40 | + $logging->log("Caught exception creating Trap class",2); |
|
41 | + } |
|
42 | + else |
|
43 | + { |
|
44 | 44 | $Trap->trapLog("Exception : ". $e->getMessage(),2,0); |
45 | - } |
|
45 | + } |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | //end |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | use Trapdirector\Logging; |
4 | 4 | |
5 | 5 | // start |
6 | -$time1 = microtime(true); |
|
6 | +$time1=microtime(true); |
|
7 | 7 | |
8 | 8 | require_once ('trap_class.php'); |
9 | 9 | |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | |
16 | 16 | try |
17 | 17 | { |
18 | - $Trap = new Trap($icingaweb2_etc); |
|
18 | + $Trap=new Trap($icingaweb2_etc); |
|
19 | 19 | //$Trap = new Trap($icingaweb2_etc,4,'display'); // For debug |
20 | 20 | //$Trap = new Trap($icingaweb2_etc,4,'syslog'); // For debug |
21 | 21 | //$Trap->setLogging(4,'syslog'); |
@@ -36,12 +36,12 @@ discard block |
||
36 | 36 | { |
37 | 37 | if ($Trap == null) |
38 | 38 | { // Exception in trap creation : log in display & syslog |
39 | - $logging = new Logging(); |
|
40 | - $logging->log("Caught exception creating Trap class",2); |
|
39 | + $logging=new Logging(); |
|
40 | + $logging->log("Caught exception creating Trap class", 2); |
|
41 | 41 | } |
42 | 42 | else |
43 | 43 | { |
44 | - $Trap->trapLog("Exception : ". $e->getMessage(),2,0); |
|
44 | + $Trap->trapLog("Exception : ".$e->getMessage(), 2, 0); |
|
45 | 45 | } |
46 | 46 | } |
47 | 47 |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | |
55 | 55 | function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
56 | 56 | { |
57 | - // Paths of ini files |
|
57 | + // Paths of ini files |
|
58 | 58 | $this->icingaweb2_etc=$etc_dir; |
59 | 59 | $this->trap_module_config=$this->icingaweb2_etc."/modules/trapdirector/config.ini"; |
60 | 60 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
@@ -63,18 +63,18 @@ discard block |
||
63 | 63 | $this->logging = new Logging(); |
64 | 64 | if ($baseLogLevel != null) |
65 | 65 | { |
66 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
67 | - $this->logSetup=true; |
|
66 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
67 | + $this->logSetup=true; |
|
68 | 68 | } |
69 | 69 | else |
70 | - $this->logSetup=false; |
|
70 | + $this->logSetup=false; |
|
71 | 71 | $this->logging->log('Loggin started', INFO); |
72 | 72 | |
73 | 73 | // Get options from ini files |
74 | 74 | $trapConfig=parse_ini_file($this->trap_module_config,true); |
75 | 75 | if ($trapConfig == false) |
76 | 76 | { |
77 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
77 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
78 | 78 | } |
79 | 79 | $this->getMainOptions($trapConfig); // Get main options from ini file |
80 | 80 | $this->setupDatabase($trapConfig); // Setup database class |
@@ -104,26 +104,26 @@ discard block |
||
104 | 104 | */ |
105 | 105 | protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
106 | 106 | { |
107 | - if (!isset($option_array[$option_category][$option_name])) |
|
108 | - { |
|
109 | - if ($message === null) |
|
110 | - { |
|
111 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
112 | - } |
|
113 | - $this->logging->log($message,$log_level,'syslog'); |
|
114 | - return false; |
|
115 | - } |
|
116 | - else |
|
117 | - { |
|
118 | - $option_var=$option_array[$option_category][$option_name]; |
|
119 | - return true; |
|
120 | - } |
|
107 | + if (!isset($option_array[$option_category][$option_name])) |
|
108 | + { |
|
109 | + if ($message === null) |
|
110 | + { |
|
111 | + $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
112 | + } |
|
113 | + $this->logging->log($message,$log_level,'syslog'); |
|
114 | + return false; |
|
115 | + } |
|
116 | + else |
|
117 | + { |
|
118 | + $option_var=$option_array[$option_category][$option_name]; |
|
119 | + return true; |
|
120 | + } |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
124 | 124 | * Get options from ini file |
125 | 125 | * @param array $trap_config : ini file array |
126 | - */ |
|
126 | + */ |
|
127 | 127 | protected function getMainOptions($trapConfig) |
128 | 128 | { |
129 | 129 | |
@@ -142,10 +142,10 @@ discard block |
||
142 | 142 | // API options |
143 | 143 | if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
144 | 144 | { |
145 | - $this->api_use=true; |
|
146 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
147 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
148 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
145 | + $this->api_use=true; |
|
146 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
147 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
148 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
149 | 149 | } |
150 | 150 | } |
151 | 151 | |
@@ -155,45 +155,45 @@ discard block |
||
155 | 155 | */ |
156 | 156 | protected function setupDatabase($trapConfig) |
157 | 157 | { |
158 | - // Trap database |
|
159 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
160 | - { |
|
161 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
162 | - return; |
|
163 | - } |
|
164 | - $dbTrapName=$trapConfig['config']['database']; |
|
165 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
158 | + // Trap database |
|
159 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
160 | + { |
|
161 | + $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
162 | + return; |
|
163 | + } |
|
164 | + $dbTrapName=$trapConfig['config']['database']; |
|
165 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
166 | 166 | |
167 | 167 | if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
168 | - { |
|
169 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
170 | - return; |
|
171 | - } |
|
172 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
173 | - { |
|
174 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
175 | - return; |
|
176 | - } |
|
168 | + { |
|
169 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
170 | + return; |
|
171 | + } |
|
172 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
173 | + { |
|
174 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
175 | + return; |
|
176 | + } |
|
177 | 177 | |
178 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName]); |
|
178 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName]); |
|
179 | 179 | |
180 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
180 | + if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
181 | 181 | |
182 | - // IDO Database |
|
183 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
184 | - { |
|
185 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
186 | - } |
|
187 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
182 | + // IDO Database |
|
183 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
184 | + { |
|
185 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
186 | + } |
|
187 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
188 | 188 | |
189 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
190 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
191 | - { |
|
192 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
193 | - return; |
|
194 | - } |
|
189 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
190 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
191 | + { |
|
192 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
193 | + return; |
|
194 | + } |
|
195 | 195 | |
196 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
196 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -204,9 +204,9 @@ discard block |
||
204 | 204 | // Database options |
205 | 205 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
206 | 206 | { |
207 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
208 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
209 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
207 | + $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
208 | + $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
209 | + $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
210 | 210 | } |
211 | 211 | } |
212 | 212 | |
@@ -217,10 +217,10 @@ discard block |
||
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
220 | - * Get data from db_config |
|
221 | - * @param $element string name of param |
|
222 | - * @return mixed : value (or null) |
|
223 | - */ |
|
220 | + * Get data from db_config |
|
221 | + * @param $element string name of param |
|
222 | + * @return mixed : value (or null) |
|
223 | + */ |
|
224 | 224 | protected function getDBConfig($element) |
225 | 225 | { |
226 | 226 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -238,15 +238,15 @@ discard block |
||
238 | 238 | } |
239 | 239 | |
240 | 240 | /** OBSOLETE Send log. Throws exception on critical error |
241 | - * @param string $message Message to log |
|
242 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
243 | - * @param string $destination file/syslog/display |
|
244 | - * @return void |
|
245 | - **/ |
|
241 | + * @param string $message Message to log |
|
242 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
243 | + * @param string $destination file/syslog/display |
|
244 | + * @return void |
|
245 | + **/ |
|
246 | 246 | public function trapLog( $message, $level, $destination ='') // OBSOLETE |
247 | 247 | { |
248 | 248 | // TODO : replace ref with $this->logging->log |
249 | - $this->logging->log($message, $level, $destination); |
|
249 | + $this->logging->log($message, $level, $destination); |
|
250 | 250 | } |
251 | 251 | |
252 | 252 | public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
@@ -256,19 +256,19 @@ discard block |
||
256 | 256 | |
257 | 257 | protected function getAPI() |
258 | 258 | { |
259 | - if ($this->icinga2api == null) |
|
260 | - { |
|
261 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
262 | - } |
|
263 | - return $this->icinga2api; |
|
259 | + if ($this->icinga2api == null) |
|
260 | + { |
|
261 | + $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
262 | + } |
|
263 | + return $this->icinga2api; |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | |
267 | 267 | /** |
268 | 268 | * read data from stream |
269 | - * @param $stream string input stream, defaults to "php://stdin" |
|
270 | - * @return mixed array trap data or exception with error |
|
271 | - */ |
|
269 | + * @param $stream string input stream, defaults to "php://stdin" |
|
270 | + * @return mixed array trap data or exception with error |
|
271 | + */ |
|
272 | 272 | public function read_trap($stream='php://stdin') |
273 | 273 | { |
274 | 274 | //Read data from snmptrapd from stdin |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | |
277 | 277 | if ($input_stream === false) |
278 | 278 | { |
279 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
279 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
280 | 280 | $this->logging->log("Error reading stdin !",ERROR,''); |
281 | 281 | return null; // note : exception thrown by logging |
282 | 282 | } |
@@ -285,21 +285,21 @@ discard block |
||
285 | 285 | $this->receivingHost=chop(fgets($input_stream)); |
286 | 286 | if ($this->receivingHost === false) |
287 | 287 | { |
288 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
288 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
289 | 289 | $this->logging->log("Error reading Host !",ERROR,''); |
290 | 290 | } |
291 | 291 | // line 2 IP:port=>IP:port |
292 | 292 | $IP=chop(fgets($input_stream)); |
293 | 293 | if ($IP === false) |
294 | 294 | { |
295 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
295 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
296 | 296 | $this->logging->log("Error reading IP !",ERROR,''); |
297 | 297 | } |
298 | 298 | $matches=array(); |
299 | 299 | $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
300 | 300 | if ($ret_code===0 || $ret_code===false) |
301 | 301 | { |
302 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
302 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
303 | 303 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
304 | 304 | } |
305 | 305 | else |
@@ -320,7 +320,7 @@ discard block |
||
320 | 320 | } |
321 | 321 | else |
322 | 322 | { |
323 | - if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
323 | + if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
324 | 324 | { |
325 | 325 | $this->trap_data['trap_oid']=$matches[2]; |
326 | 326 | } |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | |
337 | 337 | if ($this->trap_data['trap_oid']=='unknown') |
338 | 338 | { |
339 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
339 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
340 | 340 | $this->logging->log('no trap oid found',ERROR,''); |
341 | 341 | } |
342 | 342 | |
@@ -366,9 +366,9 @@ discard block |
||
366 | 366 | |
367 | 367 | /** |
368 | 368 | * Translate oid into array(MIB,Name) |
369 | - * @param $oid string oid to translate |
|
370 | - * @return mixed : null if not found or array(MIB,Name) |
|
371 | - */ |
|
369 | + * @param $oid string oid to translate |
|
370 | + * @return mixed : null if not found or array(MIB,Name) |
|
371 | + */ |
|
372 | 372 | public function translateOID($oid) |
373 | 373 | { |
374 | 374 | // try from database |
@@ -401,7 +401,7 @@ discard block |
||
401 | 401 | |
402 | 402 | // Try to get oid name from snmptranslate |
403 | 403 | $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
404 | - ' '.$oid); |
|
404 | + ' '.$oid); |
|
405 | 405 | $matches=array(); |
406 | 406 | $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
407 | 407 | if ($ret_code===0 || $ret_code === false) { |
@@ -414,9 +414,9 @@ discard block |
||
414 | 414 | |
415 | 415 | /** |
416 | 416 | * Erase old trap records |
417 | - * @param integer $days : erase traps when more than $days old |
|
418 | - * @return integer : number of lines deleted |
|
419 | - **/ |
|
417 | + * @param integer $days : erase traps when more than $days old |
|
418 | + * @return integer : number of lines deleted |
|
419 | + **/ |
|
420 | 420 | public function eraseOldTraps($days=0) |
421 | 421 | { |
422 | 422 | if ($days==0) |
@@ -441,72 +441,72 @@ discard block |
||
441 | 441 | public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
442 | 442 | { |
443 | 443 | |
444 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
444 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
445 | 445 | |
446 | - // add date time |
|
447 | - $insert_col ='date_received,status'; |
|
448 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
446 | + // add date time |
|
447 | + $insert_col ='date_received,status'; |
|
448 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
449 | 449 | |
450 | - if ($sourceIP !=null) |
|
451 | - { |
|
452 | - $insert_col .=',source_ip'; |
|
453 | - $insert_val .=",'". $sourceIP ."'"; |
|
454 | - } |
|
455 | - if ($trapoid !=null) |
|
456 | - { |
|
457 | - $insert_col .=',trap_oid'; |
|
458 | - $insert_val .=",'". $trapoid ."'"; |
|
459 | - } |
|
460 | - $insert_col .=',status_detail'; |
|
461 | - $insert_val .=",'". $message ."'"; |
|
450 | + if ($sourceIP !=null) |
|
451 | + { |
|
452 | + $insert_col .=',source_ip'; |
|
453 | + $insert_val .=",'". $sourceIP ."'"; |
|
454 | + } |
|
455 | + if ($trapoid !=null) |
|
456 | + { |
|
457 | + $insert_col .=',trap_oid'; |
|
458 | + $insert_val .=",'". $trapoid ."'"; |
|
459 | + } |
|
460 | + $insert_col .=',status_detail'; |
|
461 | + $insert_val .=",'". $message ."'"; |
|
462 | 462 | |
463 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
463 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
464 | 464 | |
465 | - switch ($this->trapsDB->trapDBType) |
|
466 | - { |
|
467 | - case 'pgsql': |
|
468 | - $sql .= ' RETURNING id;'; |
|
469 | - $this->logging->log('sql : '.$sql,INFO); |
|
470 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
471 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
472 | - } |
|
473 | - $this->logging->log('SQL insertion OK',INFO ); |
|
474 | - // Get last id to insert oid/values in secondary table |
|
475 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
465 | + switch ($this->trapsDB->trapDBType) |
|
466 | + { |
|
467 | + case 'pgsql': |
|
468 | + $sql .= ' RETURNING id;'; |
|
469 | + $this->logging->log('sql : '.$sql,INFO); |
|
470 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
471 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
472 | + } |
|
473 | + $this->logging->log('SQL insertion OK',INFO ); |
|
474 | + // Get last id to insert oid/values in secondary table |
|
475 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
476 | 476 | |
477 | - $this->logging->log('Erreur recuperation id',1,''); |
|
478 | - } |
|
479 | - if (! isset($inserted_id_ret['id'])) { |
|
480 | - $this->logging->log('Error getting id',1,''); |
|
481 | - } |
|
482 | - $this->trap_id=$inserted_id_ret['id']; |
|
483 | - break; |
|
484 | - case 'mysql': |
|
485 | - $sql .= ';'; |
|
486 | - $this->logging->log('sql : '.$sql,INFO ); |
|
487 | - if ($db_conn->query($sql) === false) { |
|
488 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
489 | - } |
|
490 | - $this->logging->log('SQL insertion OK',INFO ); |
|
491 | - // Get last id to insert oid/values in secondary table |
|
492 | - $sql='SELECT LAST_INSERT_ID();'; |
|
493 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
494 | - $this->logging->log('Erreur recuperation id',1,''); |
|
495 | - } |
|
477 | + $this->logging->log('Erreur recuperation id',1,''); |
|
478 | + } |
|
479 | + if (! isset($inserted_id_ret['id'])) { |
|
480 | + $this->logging->log('Error getting id',1,''); |
|
481 | + } |
|
482 | + $this->trap_id=$inserted_id_ret['id']; |
|
483 | + break; |
|
484 | + case 'mysql': |
|
485 | + $sql .= ';'; |
|
486 | + $this->logging->log('sql : '.$sql,INFO ); |
|
487 | + if ($db_conn->query($sql) === false) { |
|
488 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
489 | + } |
|
490 | + $this->logging->log('SQL insertion OK',INFO ); |
|
491 | + // Get last id to insert oid/values in secondary table |
|
492 | + $sql='SELECT LAST_INSERT_ID();'; |
|
493 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
494 | + $this->logging->log('Erreur recuperation id',1,''); |
|
495 | + } |
|
496 | 496 | |
497 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
498 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
499 | - $this->trap_id=$inserted_id; |
|
500 | - break; |
|
501 | - default: |
|
502 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
503 | - } |
|
497 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
498 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
499 | + $this->trap_id=$inserted_id; |
|
500 | + break; |
|
501 | + default: |
|
502 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
503 | + } |
|
504 | 504 | |
505 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
505 | + $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
506 | 506 | } |
507 | 507 | |
508 | 508 | /** Write trap data to trap database |
509 | - */ |
|
509 | + */ |
|
510 | 510 | public function writeTrapToDB() |
511 | 511 | { |
512 | 512 | |
@@ -604,10 +604,10 @@ discard block |
||
604 | 604 | } |
605 | 605 | |
606 | 606 | /** Get rules from rule database with ip and oid |
607 | - * @param $ip string ipv4 or ipv6 |
|
608 | - * @param $oid string oid in numeric |
|
609 | - * @return mixed : PDO object or false |
|
610 | - */ |
|
607 | + * @param $ip string ipv4 or ipv6 |
|
608 | + * @param $oid string oid in numeric |
|
609 | + * @return mixed : PDO object or false |
|
610 | + */ |
|
611 | 611 | protected function getRules($ip,$oid) |
612 | 612 | { |
613 | 613 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -666,9 +666,9 @@ discard block |
||
666 | 666 | } |
667 | 667 | |
668 | 668 | /** Add rule match to rule |
669 | - * @param id int : rule id |
|
670 | - * @param set int : value to set |
|
671 | - */ |
|
669 | + * @param id int : rule id |
|
670 | + * @param set int : value to set |
|
671 | + */ |
|
672 | 672 | protected function add_rule_match($id, $set) |
673 | 673 | { |
674 | 674 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -685,53 +685,53 @@ discard block |
||
685 | 685 | * @param integer $state numerical staus |
686 | 686 | * @param string $display |
687 | 687 | * @returnn bool true is service check was sent without error |
688 | - */ |
|
688 | + */ |
|
689 | 689 | public function serviceCheckResult($host,$service,$state,$display) |
690 | 690 | { |
691 | - if ($this->api_use === false) |
|
692 | - { |
|
693 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
694 | - $host.';' .$service .';' . $state . ';'.$display; |
|
695 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
691 | + if ($this->api_use === false) |
|
692 | + { |
|
693 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
694 | + $host.';' .$service .';' . $state . ';'.$display; |
|
695 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
696 | 696 | |
697 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
698 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
699 | - return true; |
|
700 | - } |
|
701 | - else |
|
702 | - { |
|
703 | - $api = $this->getAPI(); |
|
704 | - $api->setCredentials($this->api_username, $this->api_password); |
|
705 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
706 | - if ($retcode == false) |
|
707 | - { |
|
708 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
709 | - return false; |
|
710 | - } |
|
711 | - else |
|
712 | - { |
|
713 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
714 | - return true; |
|
715 | - } |
|
716 | - } |
|
697 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
698 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
699 | + return true; |
|
700 | + } |
|
701 | + else |
|
702 | + { |
|
703 | + $api = $this->getAPI(); |
|
704 | + $api->setCredentials($this->api_username, $this->api_password); |
|
705 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
706 | + if ($retcode == false) |
|
707 | + { |
|
708 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
709 | + return false; |
|
710 | + } |
|
711 | + else |
|
712 | + { |
|
713 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
714 | + return true; |
|
715 | + } |
|
716 | + } |
|
717 | 717 | } |
718 | 718 | |
719 | 719 | public function getHostByIP($ip) |
720 | 720 | { |
721 | - $api = $this->getAPI(); |
|
722 | - $api->setCredentials($this->api_username, $this->api_password); |
|
723 | - return $api->getHostByIP($ip); |
|
721 | + $api = $this->getAPI(); |
|
722 | + $api->setCredentials($this->api_username, $this->api_password); |
|
723 | + return $api->getHostByIP($ip); |
|
724 | 724 | } |
725 | 725 | |
726 | 726 | /** Resolve display. |
727 | - * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
728 | - * @param $display string |
|
729 | - * @return string display |
|
730 | - */ |
|
727 | + * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
728 | + * @param $display string |
|
729 | + * @return string display |
|
730 | + */ |
|
731 | 731 | protected function applyDisplay($display) |
732 | 732 | { |
733 | - $matches=array(); |
|
734 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
733 | + $matches=array(); |
|
734 | + while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
735 | 735 | { |
736 | 736 | $oid=$matches[1]; |
737 | 737 | $found=0; |
@@ -791,11 +791,11 @@ discard block |
||
791 | 791 | |
792 | 792 | if ($rule[$item] == '(') |
793 | 793 | { // grouping |
794 | - $item++; |
|
794 | + $item++; |
|
795 | 795 | $start=$item; |
796 | 796 | $parenthesis_count=0; |
797 | 797 | while (($item < strlen($rule)) // Not end of string AND |
798 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
798 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
799 | 799 | { |
800 | 800 | if ($rule[$item] == '"' ) |
801 | 801 | { // pass through string |
@@ -803,14 +803,14 @@ discard block |
||
803 | 803 | $item=$this->eval_getNext($rule,$item,'"'); |
804 | 804 | } |
805 | 805 | else{ |
806 | - if ($rule[$item] == '(') |
|
807 | - { |
|
808 | - $parenthesis_count++; |
|
809 | - } |
|
810 | - if ($rule[$item] == ')') |
|
811 | - { |
|
812 | - $parenthesis_count--; |
|
813 | - } |
|
806 | + if ($rule[$item] == '(') |
|
807 | + { |
|
808 | + $parenthesis_count++; |
|
809 | + } |
|
810 | + if ($rule[$item] == ')') |
|
811 | + { |
|
812 | + $parenthesis_count--; |
|
813 | + } |
|
814 | 814 | $item++; |
815 | 815 | } |
816 | 816 | } |
@@ -862,24 +862,24 @@ discard block |
||
862 | 862 | } |
863 | 863 | |
864 | 864 | /** Evaluation : makes token and evaluate. |
865 | - * Public function for expressions testing |
|
866 | - * accepts : < > = <= >= != (typec = 0) |
|
867 | - * operators : & | (typec=1) |
|
868 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
869 | - * comparison int vs strings will return null (error) |
|
870 | - * return : bool or null on error |
|
871 | - */ |
|
865 | + * Public function for expressions testing |
|
866 | + * accepts : < > = <= >= != (typec = 0) |
|
867 | + * operators : & | (typec=1) |
|
868 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
869 | + * comparison int vs strings will return null (error) |
|
870 | + * return : bool or null on error |
|
871 | + */ |
|
872 | 872 | public function evaluation($rule,&$item) |
873 | 873 | { |
874 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
874 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
875 | 875 | if ( $rule[$item] == '!') // If '!' found, negate next expression. |
876 | 876 | { |
877 | - $negate=true; |
|
878 | - $item++; |
|
877 | + $negate=true; |
|
878 | + $item++; |
|
879 | 879 | } |
880 | 880 | else |
881 | 881 | { |
882 | - $negate=false; |
|
882 | + $negate=false; |
|
883 | 883 | } |
884 | 884 | // First element : number, string or () |
885 | 885 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
@@ -899,24 +899,24 @@ discard block |
||
899 | 899 | // Third element : number, string or () |
900 | 900 | if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
901 | 901 | { |
902 | - $item++; |
|
903 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
904 | - $val2= ! $this->evaluation($rule,$item); |
|
905 | - $type2=2; // result is a boolean |
|
902 | + $item++; |
|
903 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
904 | + $val2= ! $this->evaluation($rule,$item); |
|
905 | + $type2=2; // result is a boolean |
|
906 | 906 | } |
907 | 907 | else |
908 | 908 | { |
909 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
909 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
910 | 910 | } |
911 | 911 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
912 | 912 | |
913 | 913 | if ($type1!=$type2) // cannot compare different types |
914 | 914 | { |
915 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
915 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
916 | 916 | } |
917 | 917 | if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
918 | 918 | { |
919 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
919 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
920 | 920 | } |
921 | 921 | |
922 | 922 | switch ($comp){ |
@@ -974,9 +974,9 @@ discard block |
||
974 | 974 | } |
975 | 975 | |
976 | 976 | /** Evaluation rule (uses eval_* functions recursively) |
977 | - * @param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
978 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
979 | - */ |
|
977 | + * @param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
978 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
979 | + */ |
|
980 | 980 | |
981 | 981 | protected function eval_rule($rule) |
982 | 982 | { |
@@ -1021,7 +1021,7 @@ discard block |
||
1021 | 1021 | } |
1022 | 1022 | if ($found==0) |
1023 | 1023 | { // OID not found : throw error |
1024 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
1024 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
1025 | 1025 | } |
1026 | 1026 | } |
1027 | 1027 | $item=0; |
@@ -1032,7 +1032,7 @@ discard block |
||
1032 | 1032 | } |
1033 | 1033 | |
1034 | 1034 | /** Match rules for current trap and do action |
1035 | - */ |
|
1035 | + */ |
|
1036 | 1036 | public function applyRules() |
1037 | 1037 | { |
1038 | 1038 | $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
@@ -1069,12 +1069,12 @@ discard block |
||
1069 | 1069 | { |
1070 | 1070 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
1071 | 1071 | { |
1072 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
1072 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
1073 | 1073 | } |
1074 | 1074 | else |
1075 | 1075 | { |
1076 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1077 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
1076 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1077 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
1078 | 1078 | } |
1079 | 1079 | } |
1080 | 1080 | else |
@@ -1091,15 +1091,15 @@ discard block |
||
1091 | 1091 | $this->logging->log('action NOK : '.$action,INFO ); |
1092 | 1092 | if ($action >= 0) |
1093 | 1093 | { |
1094 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
1095 | - { |
|
1096 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
1097 | - } |
|
1098 | - else |
|
1099 | - { |
|
1100 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1101 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
1102 | - } |
|
1094 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
1095 | + { |
|
1096 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
1097 | + } |
|
1098 | + else |
|
1099 | + { |
|
1100 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1101 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
1102 | + } |
|
1103 | 1103 | } |
1104 | 1104 | else |
1105 | 1105 | { |
@@ -1122,9 +1122,9 @@ discard block |
||
1122 | 1122 | } |
1123 | 1123 | catch (Exception $e) |
1124 | 1124 | { |
1125 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
1126 | - $this->trap_action.=' ERR : '.$e->getMessage(); |
|
1127 | - $this->trap_data['status']='error'; |
|
1125 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
1126 | + $this->trap_action.=' ERR : '.$e->getMessage(); |
|
1127 | + $this->trap_data['status']='error'; |
|
1128 | 1128 | } |
1129 | 1129 | |
1130 | 1130 | } |
@@ -1139,8 +1139,8 @@ discard block |
||
1139 | 1139 | } |
1140 | 1140 | |
1141 | 1141 | /** Add Time a action to rule |
1142 | - * @param string $time : time to process to insert in SQL |
|
1143 | - */ |
|
1142 | + * @param string $time : time to process to insert in SQL |
|
1143 | + */ |
|
1144 | 1144 | public function add_rule_final($time) |
1145 | 1145 | { |
1146 | 1146 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -1157,9 +1157,9 @@ discard block |
||
1157 | 1157 | /*********** UTILITIES *********************/ |
1158 | 1158 | |
1159 | 1159 | /** Create database schema |
1160 | - * @param $schema_file string File to read schema from |
|
1161 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
1162 | - */ |
|
1160 | + * @param $schema_file string File to read schema from |
|
1161 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
1162 | + */ |
|
1163 | 1163 | public function create_schema($schema_file,$table_prefix) |
1164 | 1164 | { |
1165 | 1165 | //Read data from snmptrapd from stdin |
@@ -1179,30 +1179,30 @@ discard block |
||
1179 | 1179 | { |
1180 | 1180 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
1181 | 1181 | if (preg_match('/; *$/', $newline)) |
1182 | - { |
|
1183 | - $sql= $newline; |
|
1184 | - if ($db_conn->query($sql) === false) { |
|
1185 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
1186 | - } |
|
1187 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
1188 | - { |
|
1189 | - $cur_table='table '.$cur_table_array[1]; |
|
1190 | - } |
|
1191 | - else |
|
1192 | - { |
|
1193 | - $cur_table='secret SQL stuff :-)'; |
|
1194 | - } |
|
1195 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
1196 | - $newline=''; |
|
1197 | - } |
|
1182 | + { |
|
1183 | + $sql= $newline; |
|
1184 | + if ($db_conn->query($sql) === false) { |
|
1185 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
1186 | + } |
|
1187 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
1188 | + { |
|
1189 | + $cur_table='table '.$cur_table_array[1]; |
|
1190 | + } |
|
1191 | + else |
|
1192 | + { |
|
1193 | + $cur_table='secret SQL stuff :-)'; |
|
1194 | + } |
|
1195 | + $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
1196 | + $newline=''; |
|
1197 | + } |
|
1198 | 1198 | } |
1199 | 1199 | |
1200 | 1200 | $sql= $newline; |
1201 | 1201 | if ($sql != '') |
1202 | 1202 | { |
1203 | - if ($db_conn->query($sql) === false) { |
|
1204 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
1205 | - } |
|
1203 | + if ($db_conn->query($sql) === false) { |
|
1204 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
1205 | + } |
|
1206 | 1206 | } |
1207 | 1207 | $this->logging->log('Schema created',INFO); |
1208 | 1208 | } |
@@ -1217,113 +1217,113 @@ discard block |
||
1217 | 1217 | */ |
1218 | 1218 | public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
1219 | 1219 | { |
1220 | - // Get current db number |
|
1221 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
1222 | - $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' '; |
|
1223 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
1224 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
1225 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
1226 | - return; |
|
1227 | - } |
|
1228 | - $version=$ret_code->fetchAll(); |
|
1229 | - $cur_version=$version[0]['value']; |
|
1230 | - $db_version_id=$version[0]['id']; |
|
1220 | + // Get current db number |
|
1221 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
1222 | + $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' '; |
|
1223 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
1224 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
1225 | + $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
1226 | + return; |
|
1227 | + } |
|
1228 | + $version=$ret_code->fetchAll(); |
|
1229 | + $cur_version=$version[0]['value']; |
|
1230 | + $db_version_id=$version[0]['id']; |
|
1231 | 1231 | |
1232 | - if ($this->trapsDB->trapDBType == 'pgsql') |
|
1233 | - { |
|
1234 | - $prefix .= 'update_pgsql/schema_'; |
|
1235 | - } |
|
1236 | - else |
|
1237 | - { |
|
1238 | - $prefix .= 'update_sql/schema_'; |
|
1239 | - } |
|
1240 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
1241 | - if ($getmsg === true) |
|
1242 | - { |
|
1243 | - $message=''; |
|
1244 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
1245 | - while($cur_version<$target_version) |
|
1246 | - { |
|
1247 | - $cur_version++; |
|
1248 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
1249 | - $input_stream=fopen($updateFile, 'r'); |
|
1250 | - if ($input_stream=== false) |
|
1251 | - { |
|
1252 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
1253 | - return; |
|
1254 | - } |
|
1255 | - do { $line=fgets($input_stream); } |
|
1256 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
1257 | - if ($line === false) |
|
1258 | - { |
|
1259 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
1260 | - return; |
|
1261 | - } |
|
1262 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
1263 | - } |
|
1264 | - return $message; |
|
1265 | - } |
|
1266 | - while($cur_version<$target_version) |
|
1267 | - { // tODO : execute pre & post scripts |
|
1268 | - $cur_version++; |
|
1269 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
1270 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
1271 | - $input_stream=fopen($updateFile, 'r'); |
|
1272 | - if ($input_stream=== false) |
|
1273 | - { |
|
1274 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
1275 | - return; |
|
1276 | - } |
|
1277 | - $newline=''; |
|
1278 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
1279 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
1280 | - while (($line=fgets($input_stream)) !== false) |
|
1281 | - { |
|
1282 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
1283 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
1284 | - if (preg_match('/; *$/', $newline)) |
|
1285 | - { |
|
1286 | - $sql_req=$db_conn->prepare($newline); |
|
1287 | - if ($sql_req->execute() === false) { |
|
1288 | - $this->logging->log('Error create schema : '.$newline,1,''); |
|
1289 | - } |
|
1290 | - $cur_table_array=array(); |
|
1291 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
1292 | - { |
|
1293 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
1294 | - } |
|
1295 | - else |
|
1296 | - { |
|
1297 | - $cur_table='secret SQL stuff :-)'; |
|
1298 | - //$cur_table=$newline; |
|
1299 | - } |
|
1300 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
1232 | + if ($this->trapsDB->trapDBType == 'pgsql') |
|
1233 | + { |
|
1234 | + $prefix .= 'update_pgsql/schema_'; |
|
1235 | + } |
|
1236 | + else |
|
1237 | + { |
|
1238 | + $prefix .= 'update_sql/schema_'; |
|
1239 | + } |
|
1240 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
1241 | + if ($getmsg === true) |
|
1242 | + { |
|
1243 | + $message=''; |
|
1244 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
1245 | + while($cur_version<$target_version) |
|
1246 | + { |
|
1247 | + $cur_version++; |
|
1248 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
1249 | + $input_stream=fopen($updateFile, 'r'); |
|
1250 | + if ($input_stream=== false) |
|
1251 | + { |
|
1252 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
1253 | + return; |
|
1254 | + } |
|
1255 | + do { $line=fgets($input_stream); } |
|
1256 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
1257 | + if ($line === false) |
|
1258 | + { |
|
1259 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
1260 | + return; |
|
1261 | + } |
|
1262 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
1263 | + } |
|
1264 | + return $message; |
|
1265 | + } |
|
1266 | + while($cur_version<$target_version) |
|
1267 | + { // tODO : execute pre & post scripts |
|
1268 | + $cur_version++; |
|
1269 | + $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
1270 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
1271 | + $input_stream=fopen($updateFile, 'r'); |
|
1272 | + if ($input_stream=== false) |
|
1273 | + { |
|
1274 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
1275 | + return; |
|
1276 | + } |
|
1277 | + $newline=''; |
|
1278 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
1279 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
1280 | + while (($line=fgets($input_stream)) !== false) |
|
1281 | + { |
|
1282 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
1283 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
1284 | + if (preg_match('/; *$/', $newline)) |
|
1285 | + { |
|
1286 | + $sql_req=$db_conn->prepare($newline); |
|
1287 | + if ($sql_req->execute() === false) { |
|
1288 | + $this->logging->log('Error create schema : '.$newline,1,''); |
|
1289 | + } |
|
1290 | + $cur_table_array=array(); |
|
1291 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
1292 | + { |
|
1293 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
1294 | + } |
|
1295 | + else |
|
1296 | + { |
|
1297 | + $cur_table='secret SQL stuff :-)'; |
|
1298 | + //$cur_table=$newline; |
|
1299 | + } |
|
1300 | + $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
1301 | 1301 | |
1302 | - $newline=''; |
|
1303 | - } |
|
1304 | - } |
|
1305 | - fclose($input_stream); |
|
1302 | + $newline=''; |
|
1303 | + } |
|
1304 | + } |
|
1305 | + fclose($input_stream); |
|
1306 | 1306 | |
1307 | - //$sql= $newline; |
|
1308 | - //if ($db_conn->query($sql) === false) { |
|
1309 | - // $this->logging->log('Error updating schema : '.$sql,1,''); |
|
1310 | - //} |
|
1307 | + //$sql= $newline; |
|
1308 | + //if ($db_conn->query($sql) === false) { |
|
1309 | + // $this->logging->log('Error updating schema : '.$sql,1,''); |
|
1310 | + //} |
|
1311 | 1311 | |
1312 | - $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
|
1313 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
1314 | - if ($db_conn->query($sql) === false) { |
|
1315 | - $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
|
1316 | - return; |
|
1317 | - } |
|
1312 | + $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
|
1313 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
1314 | + if ($db_conn->query($sql) === false) { |
|
1315 | + $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
|
1316 | + return; |
|
1317 | + } |
|
1318 | 1318 | |
1319 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
1320 | - } |
|
1319 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
1320 | + } |
|
1321 | 1321 | } |
1322 | 1322 | |
1323 | 1323 | /** reset service to OK after time defined in rule |
1324 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
1325 | - * @return integer : not in use |
|
1326 | - **/ |
|
1324 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
1325 | + * @return integer : not in use |
|
1326 | + **/ |
|
1327 | 1327 | public function reset_services() |
1328 | 1328 | { |
1329 | 1329 | // Get all services not in 'ok' state |
@@ -1395,105 +1395,105 @@ discard block |
||
1395 | 1395 | $description=$db_conn->quote($description); |
1396 | 1396 | if (isset($this->dbOidIndex[$oid])) |
1397 | 1397 | { |
1398 | - if ($this->dbOidIndex[$oid]['key'] == -1) |
|
1399 | - { // newly created. |
|
1400 | - return 0; |
|
1401 | - } |
|
1398 | + if ($this->dbOidIndex[$oid]['key'] == -1) |
|
1399 | + { // newly created. |
|
1400 | + return 0; |
|
1401 | + } |
|
1402 | 1402 | if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
1403 | - $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
1404 | - $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
1405 | - //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
1406 | - //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
1407 | - //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
1408 | - //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
1409 | - //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
1410 | - ) |
|
1403 | + $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
1404 | + $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
1405 | + //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
1406 | + //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
1407 | + //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
1408 | + //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
1409 | + //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
1410 | + ) |
|
1411 | 1411 | { // Do update |
1412 | - $sql='UPDATE '.$this->db_prefix.'mib_cache SET '. |
|
1413 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
1414 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
1415 | - ' WHERE id= :id'; |
|
1416 | - $sqlQuery=$db_conn->prepare($sql); |
|
1412 | + $sql='UPDATE '.$this->db_prefix.'mib_cache SET '. |
|
1413 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
1414 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
1415 | + ' WHERE id= :id'; |
|
1416 | + $sqlQuery=$db_conn->prepare($sql); |
|
1417 | 1417 | |
1418 | - $sqlParam=array( |
|
1419 | - ':name' => $name, |
|
1420 | - ':type' => $type, |
|
1421 | - ':mib' => $mib, |
|
1422 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
1423 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
1424 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
1425 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
1426 | - ':description' => ($description==null)?'null':$description, |
|
1427 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
1428 | - ); |
|
1418 | + $sqlParam=array( |
|
1419 | + ':name' => $name, |
|
1420 | + ':type' => $type, |
|
1421 | + ':mib' => $mib, |
|
1422 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
1423 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
1424 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
1425 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
1426 | + ':description' => ($description==null)?'null':$description, |
|
1427 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
1428 | + ); |
|
1429 | 1429 | |
1430 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
1431 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
1432 | - } |
|
1433 | - $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
1430 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
1431 | + $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
1432 | + } |
|
1433 | + $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
1434 | 1434 | return 1; |
1435 | 1435 | } |
1436 | 1436 | else |
1437 | 1437 | { |
1438 | - $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
1439 | - return 0; |
|
1438 | + $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
1439 | + return 0; |
|
1440 | 1440 | } |
1441 | 1441 | } |
1442 | - // create new OID. |
|
1442 | + // create new OID. |
|
1443 | 1443 | |
1444 | 1444 | // Insert data |
1445 | 1445 | |
1446 | 1446 | $sql='INSERT INTO '.$this->db_prefix.'mib_cache '. |
1447 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
1448 | - ', syntax, type_enum , description ) ' . |
|
1449 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
1450 | - ', :syntax, :type_enum, :description )'; |
|
1447 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
1448 | + ', syntax, type_enum , description ) ' . |
|
1449 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
1450 | + ', :syntax, :type_enum, :description )'; |
|
1451 | 1451 | |
1452 | 1452 | if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
1453 | 1453 | |
1454 | 1454 | $sqlQuery=$db_conn->prepare($sql); |
1455 | 1455 | |
1456 | 1456 | $sqlParam=array( |
1457 | - ':oid' => $oid, |
|
1458 | - ':name' => $name, |
|
1459 | - ':type' => $type, |
|
1460 | - ':mib' => $mib, |
|
1461 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
1462 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
1463 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
1464 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
1465 | - ':description' => ($description==null)?'null':$description |
|
1457 | + ':oid' => $oid, |
|
1458 | + ':name' => $name, |
|
1459 | + ':type' => $type, |
|
1460 | + ':mib' => $mib, |
|
1461 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
1462 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
1463 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
1464 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
1465 | + ':description' => ($description==null)?'null':$description |
|
1466 | 1466 | ); |
1467 | 1467 | |
1468 | 1468 | if ($sqlQuery->execute($sqlParam) === false) { |
1469 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
1469 | + $this->logging->log('Error in query : ' . $sql,1,''); |
|
1470 | 1470 | } |
1471 | 1471 | |
1472 | 1472 | switch ($this->trapsDB->trapDBType) |
1473 | 1473 | { |
1474 | - case 'pgsql': |
|
1475 | - // Get last id to insert oid/values in secondary table |
|
1476 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
1477 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
1478 | - } |
|
1479 | - if (! isset($inserted_id_ret['id'])) { |
|
1480 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
1481 | - } |
|
1482 | - $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
1483 | - break; |
|
1484 | - case 'mysql': |
|
1485 | - // Get last id to insert oid/values in secondary table |
|
1486 | - $sql='SELECT LAST_INSERT_ID();'; |
|
1487 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
1488 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
1489 | - } |
|
1474 | + case 'pgsql': |
|
1475 | + // Get last id to insert oid/values in secondary table |
|
1476 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
1477 | + $this->logging->log('Error getting id - pgsql - ',1,''); |
|
1478 | + } |
|
1479 | + if (! isset($inserted_id_ret['id'])) { |
|
1480 | + $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
1481 | + } |
|
1482 | + $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
1483 | + break; |
|
1484 | + case 'mysql': |
|
1485 | + // Get last id to insert oid/values in secondary table |
|
1486 | + $sql='SELECT LAST_INSERT_ID();'; |
|
1487 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
1488 | + $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
1489 | + } |
|
1490 | 1490 | |
1491 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
1492 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
1493 | - $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
1494 | - break; |
|
1495 | - default: |
|
1496 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
1491 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
1492 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
1493 | + $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
1494 | + break; |
|
1495 | + default: |
|
1496 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
1497 | 1497 | } |
1498 | 1498 | |
1499 | 1499 | // Set as newly created. |
@@ -1501,148 +1501,148 @@ discard block |
||
1501 | 1501 | return 2; |
1502 | 1502 | } |
1503 | 1503 | |
1504 | - /** |
|
1505 | - * create or update (with check_existing = true) objects of trap |
|
1506 | - * @param string $trapOID : trap oid |
|
1507 | - * @param string $trapmib : mib of trap |
|
1508 | - * @param array $objects : array of objects name (without MIB) |
|
1509 | - * @param bool $check_existing : check instead of create |
|
1510 | - */ |
|
1504 | + /** |
|
1505 | + * create or update (with check_existing = true) objects of trap |
|
1506 | + * @param string $trapOID : trap oid |
|
1507 | + * @param string $trapmib : mib of trap |
|
1508 | + * @param array $objects : array of objects name (without MIB) |
|
1509 | + * @param bool $check_existing : check instead of create |
|
1510 | + */ |
|
1511 | 1511 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
1512 | 1512 | { |
1513 | - $dbObjects=null; // cache of objects for trap in db |
|
1514 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
1513 | + $dbObjects=null; // cache of objects for trap in db |
|
1514 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
1515 | 1515 | |
1516 | - // Get id of trapmib. |
|
1516 | + // Get id of trapmib. |
|
1517 | 1517 | |
1518 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
1519 | - if ($check_existing === true) |
|
1520 | - { |
|
1521 | - // Get all objects |
|
1522 | - $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
1523 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
1524 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
1525 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
1526 | - } |
|
1527 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
1518 | + $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
1519 | + if ($check_existing === true) |
|
1520 | + { |
|
1521 | + // Get all objects |
|
1522 | + $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
1523 | + $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
1524 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
1525 | + $this->logging->log('No result in query : ' . $sql,1,''); |
|
1526 | + } |
|
1527 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
1528 | 1528 | |
1529 | - foreach ($dbObjectsRaw as $val) |
|
1530 | - { |
|
1531 | - $dbObjects[$val['object_id']]=1; |
|
1532 | - } |
|
1533 | - } |
|
1534 | - foreach ($objects as $object) |
|
1535 | - { |
|
1536 | - $match=$snmptrans=array(); |
|
1537 | - $retVal=0; |
|
1538 | - $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
1539 | - $tmpdesc='';$indesc=false; |
|
1529 | + foreach ($dbObjectsRaw as $val) |
|
1530 | + { |
|
1531 | + $dbObjects[$val['object_id']]=1; |
|
1532 | + } |
|
1533 | + } |
|
1534 | + foreach ($objects as $object) |
|
1535 | + { |
|
1536 | + $match=$snmptrans=array(); |
|
1537 | + $retVal=0; |
|
1538 | + $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
1539 | + $tmpdesc='';$indesc=false; |
|
1540 | 1540 | |
1541 | - $objMib=$trapmib; |
|
1542 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1543 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
1544 | - if ($retVal!=0) |
|
1545 | - { |
|
1546 | - // Maybe not trap mib, search with IR |
|
1547 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1548 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
1549 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
1550 | - { // Not found -> continue with warning |
|
1551 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
1552 | - continue; |
|
1553 | - } |
|
1554 | - $objMib=$match[1]; |
|
1541 | + $objMib=$trapmib; |
|
1542 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1543 | + ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
1544 | + if ($retVal!=0) |
|
1545 | + { |
|
1546 | + // Maybe not trap mib, search with IR |
|
1547 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1548 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
1549 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
1550 | + { // Not found -> continue with warning |
|
1551 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
1552 | + continue; |
|
1553 | + } |
|
1554 | + $objMib=$match[1]; |
|
1555 | 1555 | |
1556 | - // Do the snmptranslate again. |
|
1557 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1558 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
1559 | - if ($retVal!=0) { |
|
1560 | - $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
1561 | - } |
|
1556 | + // Do the snmptranslate again. |
|
1557 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1558 | + ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
1559 | + if ($retVal!=0) { |
|
1560 | + $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
1561 | + } |
|
1562 | 1562 | |
1563 | - } |
|
1564 | - foreach ($snmptrans as $line) |
|
1565 | - { |
|
1566 | - if ($indesc===true) |
|
1567 | - { |
|
1568 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
1569 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
1570 | - { |
|
1571 | - $objDesc = $tmpdesc . $match[1]; |
|
1572 | - $indesc=false; |
|
1573 | - } |
|
1574 | - $tmpdesc.=$line; |
|
1575 | - continue; |
|
1576 | - } |
|
1577 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
1578 | - { |
|
1579 | - $objOid=$line; |
|
1580 | - continue; |
|
1581 | - } |
|
1582 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
1583 | - { |
|
1584 | - $objSyntax=$match[1]; |
|
1585 | - $objEnum=$match[2]; |
|
1586 | - continue; |
|
1587 | - } |
|
1588 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
1589 | - { |
|
1590 | - $objSyntax=$match[1]; |
|
1591 | - continue; |
|
1592 | - } |
|
1593 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
1594 | - { |
|
1595 | - $objDispHint=$match[1]; |
|
1596 | - continue; |
|
1597 | - } |
|
1598 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
1599 | - { |
|
1600 | - $objDesc=$match[1]; |
|
1601 | - continue; |
|
1602 | - } |
|
1603 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
1604 | - { |
|
1605 | - $tmpdesc=$match[1]; |
|
1606 | - $indesc=true; |
|
1607 | - continue; |
|
1608 | - } |
|
1609 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
1610 | - { |
|
1611 | - $objTc=$match[1]; |
|
1612 | - continue; |
|
1613 | - } |
|
1614 | - } |
|
1615 | - $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
1616 | - //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
1617 | - // Update |
|
1618 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
1563 | + } |
|
1564 | + foreach ($snmptrans as $line) |
|
1565 | + { |
|
1566 | + if ($indesc===true) |
|
1567 | + { |
|
1568 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
1569 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
1570 | + { |
|
1571 | + $objDesc = $tmpdesc . $match[1]; |
|
1572 | + $indesc=false; |
|
1573 | + } |
|
1574 | + $tmpdesc.=$line; |
|
1575 | + continue; |
|
1576 | + } |
|
1577 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
1578 | + { |
|
1579 | + $objOid=$line; |
|
1580 | + continue; |
|
1581 | + } |
|
1582 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
1583 | + { |
|
1584 | + $objSyntax=$match[1]; |
|
1585 | + $objEnum=$match[2]; |
|
1586 | + continue; |
|
1587 | + } |
|
1588 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
1589 | + { |
|
1590 | + $objSyntax=$match[1]; |
|
1591 | + continue; |
|
1592 | + } |
|
1593 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
1594 | + { |
|
1595 | + $objDispHint=$match[1]; |
|
1596 | + continue; |
|
1597 | + } |
|
1598 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
1599 | + { |
|
1600 | + $objDesc=$match[1]; |
|
1601 | + continue; |
|
1602 | + } |
|
1603 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
1604 | + { |
|
1605 | + $tmpdesc=$match[1]; |
|
1606 | + $indesc=true; |
|
1607 | + continue; |
|
1608 | + } |
|
1609 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
1610 | + { |
|
1611 | + $objTc=$match[1]; |
|
1612 | + continue; |
|
1613 | + } |
|
1614 | + } |
|
1615 | + $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
1616 | + //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
1617 | + // Update |
|
1618 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
1619 | 1619 | |
1620 | - if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
1621 | - { // if link exists, continue |
|
1622 | - $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
1623 | - continue; |
|
1624 | - } |
|
1625 | - if ($check_existing === true) |
|
1626 | - { |
|
1627 | - // TODO : check link trap - objects exists, mark them. |
|
1628 | - } |
|
1629 | - // Associate in object table |
|
1630 | - $sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
1631 | - 'values (:trap_id, :object_id)'; |
|
1632 | - $sqlQuery=$db_conn->prepare($sql); |
|
1633 | - $sqlParam=array( |
|
1634 | - ':trap_id' => $trapId, |
|
1635 | - ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
1636 | - ); |
|
1620 | + if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
1621 | + { // if link exists, continue |
|
1622 | + $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
1623 | + continue; |
|
1624 | + } |
|
1625 | + if ($check_existing === true) |
|
1626 | + { |
|
1627 | + // TODO : check link trap - objects exists, mark them. |
|
1628 | + } |
|
1629 | + // Associate in object table |
|
1630 | + $sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
1631 | + 'values (:trap_id, :object_id)'; |
|
1632 | + $sqlQuery=$db_conn->prepare($sql); |
|
1633 | + $sqlParam=array( |
|
1634 | + ':trap_id' => $trapId, |
|
1635 | + ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
1636 | + ); |
|
1637 | 1637 | |
1638 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
1639 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
1640 | - } |
|
1641 | - } |
|
1642 | - if ($check_existing === true) |
|
1643 | - { |
|
1644 | - // TODO : remove link trap - objects that wasn't marked. |
|
1645 | - } |
|
1638 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
1639 | + $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
1640 | + } |
|
1641 | + } |
|
1642 | + if ($check_existing === true) |
|
1643 | + { |
|
1644 | + // TODO : remove link trap - objects that wasn't marked. |
|
1645 | + } |
|
1646 | 1646 | |
1647 | 1647 | } |
1648 | 1648 | |
@@ -1652,7 +1652,7 @@ discard block |
||
1652 | 1652 | * @param boolean $check_change : Force check of trap params & objects |
1653 | 1653 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
1654 | 1654 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
1655 | - */ |
|
1655 | + */ |
|
1656 | 1656 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
1657 | 1657 | { |
1658 | 1658 | // Timing |
@@ -1704,7 +1704,7 @@ discard block |
||
1704 | 1704 | |
1705 | 1705 | for ($curElement=0;$curElement < $numElements;$curElement++) |
1706 | 1706 | { |
1707 | - $time_1= microtime(true); |
|
1707 | + $time_1= microtime(true); |
|
1708 | 1708 | if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
1709 | 1709 | { // echo a . every 2 sec |
1710 | 1710 | echo '.'; |
@@ -1722,8 +1722,8 @@ discard block |
||
1722 | 1722 | // Get oid or pass if not found |
1723 | 1723 | if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
1724 | 1724 | { |
1725 | - $time_parse1 += microtime(true) - $time_1; |
|
1726 | - $time_parse1N ++; |
|
1725 | + $time_parse1 += microtime(true) - $time_1; |
|
1726 | + $time_parse1N ++; |
|
1727 | 1727 | continue; |
1728 | 1728 | } |
1729 | 1729 | $oid=$this->objectsAll[$curElement]; |
@@ -1734,7 +1734,7 @@ discard block |
||
1734 | 1734 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
1735 | 1735 | $this->objectsAll[$curElement],$match)) |
1736 | 1736 | { |
1737 | - $time_check1 += microtime(true) - $time_1; |
|
1737 | + $time_check1 += microtime(true) - $time_1; |
|
1738 | 1738 | $time_check1N++; |
1739 | 1739 | continue; |
1740 | 1740 | } |
@@ -1747,8 +1747,8 @@ discard block |
||
1747 | 1747 | // Check if next is suboid -> in that case is cannot be a trap |
1748 | 1748 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
1749 | 1749 | { |
1750 | - $time_check2 += microtime(true) - $time_1; |
|
1751 | - $time_check2N++; |
|
1750 | + $time_check2 += microtime(true) - $time_1; |
|
1751 | + $time_check2N++; |
|
1752 | 1752 | continue; |
1753 | 1753 | } |
1754 | 1754 | unset($snmptrans); |
@@ -1756,8 +1756,8 @@ discard block |
||
1756 | 1756 | ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
1757 | 1757 | if ($retVal!=0) |
1758 | 1758 | { |
1759 | - $time_check2 += microtime(true) - $time_1; |
|
1760 | - $time_check2N++; |
|
1759 | + $time_check2 += microtime(true) - $time_1; |
|
1760 | + $time_check2N++; |
|
1761 | 1761 | continue; |
1762 | 1762 | } |
1763 | 1763 | //echo "\n v1 trap found : $oid \n"; |
@@ -1766,7 +1766,7 @@ discard block |
||
1766 | 1766 | } |
1767 | 1767 | if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
1768 | 1768 | { |
1769 | - $time_check3 += microtime(true) - $time_1; |
|
1769 | + $time_check3 += microtime(true) - $time_1; |
|
1770 | 1770 | $time_check3N++; |
1771 | 1771 | continue; |
1772 | 1772 | } |
@@ -1787,7 +1787,7 @@ discard block |
||
1787 | 1787 | |
1788 | 1788 | if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
1789 | 1789 | { |
1790 | - $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
1790 | + $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
1791 | 1791 | } |
1792 | 1792 | $trapMib=$match[1]; |
1793 | 1793 | |
@@ -1795,17 +1795,17 @@ discard block |
||
1795 | 1795 | while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
1796 | 1796 | if (isset($snmptrans[$numLine])) |
1797 | 1797 | { |
1798 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
1798 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
1799 | 1799 | |
1800 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
1801 | - { |
|
1802 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
1803 | - $numLine++; |
|
1804 | - } |
|
1805 | - if (isset($snmptrans[$numLine])) { |
|
1806 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
1807 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
1808 | - } |
|
1800 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
1801 | + { |
|
1802 | + $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
1803 | + $numLine++; |
|
1804 | + } |
|
1805 | + if (isset($snmptrans[$numLine])) { |
|
1806 | + $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
1807 | + $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
1808 | + } |
|
1809 | 1809 | |
1810 | 1810 | } |
1811 | 1811 | $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
@@ -1813,23 +1813,23 @@ discard block |
||
1813 | 1813 | |
1814 | 1814 | if (($update==0) && ($check_change===false)) |
1815 | 1815 | { // Trapd didn't change & force check disabled |
1816 | - $time_objects += microtime(true) - $time_1; |
|
1817 | - if ($display_progress) echo "C"; |
|
1818 | - continue; |
|
1816 | + $time_objects += microtime(true) - $time_1; |
|
1817 | + if ($display_progress) echo "C"; |
|
1818 | + continue; |
|
1819 | 1819 | } |
1820 | 1820 | |
1821 | 1821 | $synt=null; |
1822 | 1822 | foreach ($snmptrans as $line) |
1823 | 1823 | { |
1824 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
1825 | - { |
|
1826 | - $synt=$match[1]; |
|
1827 | - } |
|
1824 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
1825 | + { |
|
1826 | + $synt=$match[1]; |
|
1827 | + } |
|
1828 | 1828 | } |
1829 | 1829 | if ($synt == null) |
1830 | 1830 | { |
1831 | 1831 | //echo "No objects for $trapOID\n"; |
1832 | - $time_objects += microtime(true) - $time_1; |
|
1832 | + $time_objects += microtime(true) - $time_1; |
|
1833 | 1833 | continue; |
1834 | 1834 | } |
1835 | 1835 | //echo "$synt \n"; |
@@ -1848,18 +1848,18 @@ discard block |
||
1848 | 1848 | |
1849 | 1849 | if ($display_progress) |
1850 | 1850 | { |
1851 | - echo "\nNumber of processed traps : $time_num_traps \n"; |
|
1852 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
1853 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
1854 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
1855 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
1851 | + echo "\nNumber of processed traps : $time_num_traps \n"; |
|
1852 | + echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
1853 | + echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
1854 | + echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
1855 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
1856 | 1856 | } |
1857 | 1857 | |
1858 | 1858 | // Timing ends |
1859 | 1859 | $timeTaken=microtime(true) - $timeTaken; |
1860 | 1860 | if ($display_progress) |
1861 | 1861 | { |
1862 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
1862 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
1863 | 1863 | } |
1864 | 1864 | |
1865 | 1865 | } |
@@ -32,8 +32,8 @@ discard block |
||
32 | 32 | protected $api_password=''; |
33 | 33 | |
34 | 34 | // Logs |
35 | - protected $logging; //< Logging class. |
|
36 | - protected $logSetup; //< bool true if log was setup in constructor |
|
35 | + protected $logging; //< Logging class. |
|
36 | + protected $logSetup; //< bool true if log was setup in constructor |
|
37 | 37 | |
38 | 38 | // Databases |
39 | 39 | protected $trapsDB; //< Database class |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | private $objectsAll; //< output lines of snmptranslate list |
53 | 53 | private $trapObjectsIndex; //< array of traps objects (as OID) |
54 | 54 | |
55 | - function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
55 | + function __construct($etc_dir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='') |
|
56 | 56 | { |
57 | 57 | // Paths of ini files |
58 | 58 | $this->icingaweb2_etc=$etc_dir; |
@@ -60,10 +60,10 @@ discard block |
||
60 | 60 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
61 | 61 | |
62 | 62 | // Setup logging |
63 | - $this->logging = new Logging(); |
|
63 | + $this->logging=new Logging(); |
|
64 | 64 | if ($baseLogLevel != null) |
65 | 65 | { |
66 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
66 | + $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile); |
|
67 | 67 | $this->logSetup=true; |
68 | 68 | } |
69 | 69 | else |
@@ -71,10 +71,10 @@ discard block |
||
71 | 71 | $this->logging->log('Loggin started', INFO); |
72 | 72 | |
73 | 73 | // Get options from ini files |
74 | - $trapConfig=parse_ini_file($this->trap_module_config,true); |
|
74 | + $trapConfig=parse_ini_file($this->trap_module_config, true); |
|
75 | 75 | if ($trapConfig == false) |
76 | 76 | { |
77 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
77 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config, ERROR, 'syslog'); |
|
78 | 78 | } |
79 | 79 | $this->getMainOptions($trapConfig); // Get main options from ini file |
80 | 80 | $this->setupDatabase($trapConfig); // Setup database class |
@@ -102,15 +102,15 @@ discard block |
||
102 | 102 | * @param string $message warning message if not found |
103 | 103 | * @return boolean true if found, or false |
104 | 104 | */ |
105 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
105 | + protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null) |
|
106 | 106 | { |
107 | 107 | if (!isset($option_array[$option_category][$option_name])) |
108 | 108 | { |
109 | 109 | if ($message === null) |
110 | 110 | { |
111 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
111 | + $message='No '.$option_name.' in config file: '.$this->trap_module_config; |
|
112 | 112 | } |
113 | - $this->logging->log($message,$log_level,'syslog'); |
|
113 | + $this->logging->log($message, $log_level, 'syslog'); |
|
114 | 114 | return false; |
115 | 115 | } |
116 | 116 | else |
@@ -128,24 +128,24 @@ discard block |
||
128 | 128 | { |
129 | 129 | |
130 | 130 | // Snmptranslate binary path |
131 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
131 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate); |
|
132 | 132 | |
133 | 133 | // mibs path |
134 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
134 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs); |
|
135 | 135 | |
136 | 136 | // icinga2cmd path |
137 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
137 | + $this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd); |
|
138 | 138 | |
139 | 139 | // table prefix |
140 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->db_prefix); |
|
140 | + $this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->db_prefix); |
|
141 | 141 | |
142 | 142 | // API options |
143 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
|
143 | + if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->api_hostname)) |
|
144 | 144 | { |
145 | 145 | $this->api_use=true; |
146 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
147 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
148 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
146 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_port', $this->api_port); |
|
147 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_user', $this->api_username); |
|
148 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_password', $this->api_password); |
|
149 | 149 | } |
150 | 150 | } |
151 | 151 | |
@@ -156,40 +156,40 @@ discard block |
||
156 | 156 | protected function setupDatabase($trapConfig) |
157 | 157 | { |
158 | 158 | // Trap database |
159 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
159 | + if (!array_key_exists('database', $trapConfig['config'])) |
|
160 | 160 | { |
161 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
161 | + $this->logging->log("No database in config file: ".$this->trap_module_config, ERROR, ''); |
|
162 | 162 | return; |
163 | 163 | } |
164 | 164 | $dbTrapName=$trapConfig['config']['database']; |
165 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
165 | + $this->logging->log("Found database in config file: ".$dbTrapName, INFO); |
|
166 | 166 | |
167 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
|
167 | + if (($dbConfig=parse_ini_file($this->icingaweb2_ressources, true)) === false) |
|
168 | 168 | { |
169 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
169 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources, ERROR, ''); |
|
170 | 170 | return; |
171 | 171 | } |
172 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
172 | + if (!array_key_exists($dbTrapName, $dbConfig)) |
|
173 | 173 | { |
174 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
174 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources, ERROR, ''); |
|
175 | 175 | return; |
176 | 176 | } |
177 | 177 | |
178 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName]); |
|
178 | + $this->trapsDB=new Database($this->logging, $dbConfig[$dbTrapName]); |
|
179 | 179 | |
180 | 180 | if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
181 | 181 | |
182 | 182 | // IDO Database |
183 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
183 | + if (!array_key_exists('IDOdatabase', $trapConfig['config'])) |
|
184 | 184 | { |
185 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
185 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config, ERROR, ''); |
|
186 | 186 | } |
187 | 187 | $dbIdoName=$trapConfig['config']['IDOdatabase']; |
188 | 188 | |
189 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
190 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
189 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO); |
|
190 | + if (!array_key_exists($dbIdoName, $dbConfig)) |
|
191 | 191 | { |
192 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
192 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources, ERROR, ''); |
|
193 | 193 | return; |
194 | 194 | } |
195 | 195 | |
@@ -204,13 +204,13 @@ discard block |
||
204 | 204 | // Database options |
205 | 205 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
206 | 206 | { |
207 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
208 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
209 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
207 | + $this->getDBConfigIfSet('log_level', $this->logging->debugLevel); |
|
208 | + $this->getDBConfigIfSet('log_destination', $this->logging->outputMode); |
|
209 | + $this->getDBConfigIfSet('log_file', $this->logging->outputFile); |
|
210 | 210 | } |
211 | 211 | } |
212 | 212 | |
213 | - protected function getDBConfigIfSet($element,&$variable) |
|
213 | + protected function getDBConfigIfSet($element, &$variable) |
|
214 | 214 | { |
215 | 215 | $value=$this->getDBConfig($element); |
216 | 216 | if ($value != 'null') $variable=$value; |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $db_conn=$this->trapsDB->db_connect_trap(); |
227 | 227 | $sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )'; |
228 | 228 | if (($ret_code=$db_conn->query($sql)) === false) { |
229 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
229 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
230 | 230 | return null; |
231 | 231 | } |
232 | 232 | $value=$ret_code->fetch(); |
@@ -243,22 +243,22 @@ discard block |
||
243 | 243 | * @param string $destination file/syslog/display |
244 | 244 | * @return void |
245 | 245 | **/ |
246 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
246 | + public function trapLog($message, $level, $destination='') // OBSOLETE |
|
247 | 247 | { |
248 | 248 | // TODO : replace ref with $this->logging->log |
249 | 249 | $this->logging->log($message, $level, $destination); |
250 | 250 | } |
251 | 251 | |
252 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
252 | + public function setLogging($debugLvl, $outputType, $outputOption=null) // OBSOLETE |
|
253 | 253 | { |
254 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
254 | + $this->logging->setLogging($debugLvl, $outputType, $outputOption); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | protected function getAPI() |
258 | 258 | { |
259 | 259 | if ($this->icinga2api == null) |
260 | 260 | { |
261 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
261 | + $this->icinga2api=new Icinga2API($this->api_hostname, $this->api_port); |
|
262 | 262 | } |
263 | 263 | return $this->icinga2api; |
264 | 264 | } |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | if ($input_stream === false) |
278 | 278 | { |
279 | 279 | $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
280 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
280 | + $this->logging->log("Error reading stdin !", ERROR, ''); |
|
281 | 281 | return null; // note : exception thrown by logging |
282 | 282 | } |
283 | 283 | |
@@ -286,21 +286,21 @@ discard block |
||
286 | 286 | if ($this->receivingHost === false) |
287 | 287 | { |
288 | 288 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
289 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
289 | + $this->logging->log("Error reading Host !", ERROR, ''); |
|
290 | 290 | } |
291 | 291 | // line 2 IP:port=>IP:port |
292 | 292 | $IP=chop(fgets($input_stream)); |
293 | 293 | if ($IP === false) |
294 | 294 | { |
295 | 295 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
296 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
296 | + $this->logging->log("Error reading IP !", ERROR, ''); |
|
297 | 297 | } |
298 | 298 | $matches=array(); |
299 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
300 | - if ($ret_code===0 || $ret_code===false) |
|
299 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches); |
|
300 | + if ($ret_code === 0 || $ret_code === false) |
|
301 | 301 | { |
302 | 302 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
303 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
303 | + $this->logging->log('Error parsing IP : '.$IP, ERROR, ''); |
|
304 | 304 | } |
305 | 305 | else |
306 | 306 | { |
@@ -310,34 +310,34 @@ discard block |
||
310 | 310 | $this->trap_data['destination_port']=$matches[4]; |
311 | 311 | } |
312 | 312 | |
313 | - while (($vars=fgets($input_stream)) !==false) |
|
313 | + while (($vars=fgets($input_stream)) !== false) |
|
314 | 314 | { |
315 | 315 | $vars=chop($vars); |
316 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
317 | - if ($ret_code===0 || $ret_code===false) |
|
316 | + $ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches); |
|
317 | + if ($ret_code === 0 || $ret_code === false) |
|
318 | 318 | { |
319 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
319 | + $this->logging->log('No match on trap data : '.$vars, WARN, ''); |
|
320 | 320 | } |
321 | 321 | else |
322 | 322 | { |
323 | - if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
|
323 | + if (($matches[1] == '.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1] == '.1.3.6.1.6.3.1.1.4.1')) |
|
324 | 324 | { |
325 | 325 | $this->trap_data['trap_oid']=$matches[2]; |
326 | 326 | } |
327 | 327 | else |
328 | 328 | { |
329 | - $object= new stdClass; |
|
330 | - $object->oid =$matches[1]; |
|
331 | - $object->value = $matches[2]; |
|
332 | - array_push($this->trap_data_ext,$object); |
|
329 | + $object=new stdClass; |
|
330 | + $object->oid=$matches[1]; |
|
331 | + $object->value=$matches[2]; |
|
332 | + array_push($this->trap_data_ext, $object); |
|
333 | 333 | } |
334 | 334 | } |
335 | 335 | } |
336 | 336 | |
337 | - if ($this->trap_data['trap_oid']=='unknown') |
|
337 | + if ($this->trap_data['trap_oid'] == 'unknown') |
|
338 | 338 | { |
339 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
340 | - $this->logging->log('no trap oid found',ERROR,''); |
|
339 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trap_data['source_ip']); |
|
340 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
341 | 341 | } |
342 | 342 | |
343 | 343 | // Translate oids. |
@@ -359,7 +359,7 @@ discard block |
||
359 | 359 | } |
360 | 360 | |
361 | 361 | |
362 | - $this->trap_data['status']= 'waiting'; |
|
362 | + $this->trap_data['status']='waiting'; |
|
363 | 363 | |
364 | 364 | return $this->trap_data; |
365 | 365 | } |
@@ -375,40 +375,40 @@ discard block |
||
375 | 375 | $db_conn=$this->trapsDB->db_connect_trap(); |
376 | 376 | |
377 | 377 | $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
378 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
378 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
379 | 379 | if (($ret_code=$db_conn->query($sql)) === false) { |
380 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
380 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
381 | 381 | } |
382 | 382 | $name=$ret_code->fetch(); |
383 | 383 | if ($name['name'] != null) |
384 | 384 | { |
385 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
385 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
386 | 386 | } |
387 | 387 | |
388 | 388 | // Also check if it is an instance of OID |
389 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
389 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
390 | 390 | |
391 | 391 | $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
392 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
392 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
393 | 393 | if (($ret_code=$db_conn->query($sql)) === false) { |
394 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
394 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
395 | 395 | } |
396 | 396 | $name=$ret_code->fetch(); |
397 | 397 | if ($name['name'] != null) |
398 | 398 | { |
399 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
399 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
400 | 400 | } |
401 | 401 | |
402 | 402 | // Try to get oid name from snmptranslate |
403 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
403 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
404 | 404 | ' '.$oid); |
405 | 405 | $matches=array(); |
406 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
407 | - if ($ret_code===0 || $ret_code === false) { |
|
406 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
407 | + if ($ret_code === 0 || $ret_code === false) { |
|
408 | 408 | return NULL; |
409 | 409 | } else { |
410 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
411 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
410 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
411 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
412 | 412 | } |
413 | 413 | } |
414 | 414 | |
@@ -419,90 +419,90 @@ discard block |
||
419 | 419 | **/ |
420 | 420 | public function eraseOldTraps($days=0) |
421 | 421 | { |
422 | - if ($days==0) |
|
422 | + if ($days == 0) |
|
423 | 423 | { |
424 | 424 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
425 | 425 | { |
426 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
426 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
427 | 427 | return; |
428 | 428 | } |
429 | 429 | } |
430 | 430 | $db_conn=$this->trapsDB->db_connect_trap(); |
431 | - $daysago = strtotime("-".$days." day"); |
|
432 | - $sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
431 | + $daysago=strtotime("-".$days." day"); |
|
432 | + $sql='delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
433 | 433 | if ($db_conn->query($sql) === false) { |
434 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
434 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
435 | 435 | } |
436 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
436 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
437 | 437 | } |
438 | 438 | |
439 | 439 | /** Write error to received trap database |
440 | 440 | */ |
441 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
441 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
442 | 442 | { |
443 | 443 | |
444 | 444 | $db_conn=$this->trapsDB->db_connect_trap(); |
445 | 445 | |
446 | 446 | // add date time |
447 | - $insert_col ='date_received,status'; |
|
448 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
447 | + $insert_col='date_received,status'; |
|
448 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
449 | 449 | |
450 | - if ($sourceIP !=null) |
|
450 | + if ($sourceIP != null) |
|
451 | 451 | { |
452 | - $insert_col .=',source_ip'; |
|
453 | - $insert_val .=",'". $sourceIP ."'"; |
|
452 | + $insert_col.=',source_ip'; |
|
453 | + $insert_val.=",'".$sourceIP."'"; |
|
454 | 454 | } |
455 | - if ($trapoid !=null) |
|
455 | + if ($trapoid != null) |
|
456 | 456 | { |
457 | - $insert_col .=',trap_oid'; |
|
458 | - $insert_val .=",'". $trapoid ."'"; |
|
457 | + $insert_col.=',trap_oid'; |
|
458 | + $insert_val.=",'".$trapoid."'"; |
|
459 | 459 | } |
460 | - $insert_col .=',status_detail'; |
|
461 | - $insert_val .=",'". $message ."'"; |
|
460 | + $insert_col.=',status_detail'; |
|
461 | + $insert_val.=",'".$message."'"; |
|
462 | 462 | |
463 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
463 | + $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
464 | 464 | |
465 | 465 | switch ($this->trapsDB->trapDBType) |
466 | 466 | { |
467 | 467 | case 'pgsql': |
468 | - $sql .= ' RETURNING id;'; |
|
469 | - $this->logging->log('sql : '.$sql,INFO); |
|
468 | + $sql.=' RETURNING id;'; |
|
469 | + $this->logging->log('sql : '.$sql, INFO); |
|
470 | 470 | if (($ret_code=$db_conn->query($sql)) === false) { |
471 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
471 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
472 | 472 | } |
473 | - $this->logging->log('SQL insertion OK',INFO ); |
|
473 | + $this->logging->log('SQL insertion OK', INFO); |
|
474 | 474 | // Get last id to insert oid/values in secondary table |
475 | 475 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
476 | 476 | |
477 | - $this->logging->log('Erreur recuperation id',1,''); |
|
477 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
478 | 478 | } |
479 | - if (! isset($inserted_id_ret['id'])) { |
|
480 | - $this->logging->log('Error getting id',1,''); |
|
479 | + if (!isset($inserted_id_ret['id'])) { |
|
480 | + $this->logging->log('Error getting id', 1, ''); |
|
481 | 481 | } |
482 | 482 | $this->trap_id=$inserted_id_ret['id']; |
483 | 483 | break; |
484 | 484 | case 'mysql': |
485 | - $sql .= ';'; |
|
486 | - $this->logging->log('sql : '.$sql,INFO ); |
|
485 | + $sql.=';'; |
|
486 | + $this->logging->log('sql : '.$sql, INFO); |
|
487 | 487 | if ($db_conn->query($sql) === false) { |
488 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
488 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
489 | 489 | } |
490 | - $this->logging->log('SQL insertion OK',INFO ); |
|
490 | + $this->logging->log('SQL insertion OK', INFO); |
|
491 | 491 | // Get last id to insert oid/values in secondary table |
492 | 492 | $sql='SELECT LAST_INSERT_ID();'; |
493 | 493 | if (($ret_code=$db_conn->query($sql)) === false) { |
494 | - $this->logging->log('Erreur recuperation id',1,''); |
|
494 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
495 | 495 | } |
496 | 496 | |
497 | 497 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
498 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
498 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
499 | 499 | $this->trap_id=$inserted_id; |
500 | 500 | break; |
501 | 501 | default: |
502 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
502 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
503 | 503 | } |
504 | 504 | |
505 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
505 | + $this->logging->log('id found: '.$this->trap_id, INFO); |
|
506 | 506 | } |
507 | 507 | |
508 | 508 | /** Write trap data to trap database |
@@ -519,86 +519,86 @@ discard block |
||
519 | 519 | $insert_col=''; |
520 | 520 | $insert_val=''; |
521 | 521 | // add date time |
522 | - $this->trap_data['date_received'] = date("Y-m-d H:i:s"); |
|
522 | + $this->trap_data['date_received']=date("Y-m-d H:i:s"); |
|
523 | 523 | |
524 | 524 | $firstcol=1; |
525 | 525 | foreach ($this->trap_data as $col => $val) |
526 | 526 | { |
527 | - if ($firstcol==0) |
|
527 | + if ($firstcol == 0) |
|
528 | 528 | { |
529 | - $insert_col .=','; |
|
530 | - $insert_val .=','; |
|
529 | + $insert_col.=','; |
|
530 | + $insert_val.=','; |
|
531 | 531 | } |
532 | - $insert_col .= $col ; |
|
533 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
532 | + $insert_col.=$col; |
|
533 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
534 | 534 | $firstcol=0; |
535 | 535 | } |
536 | 536 | |
537 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
537 | + $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
538 | 538 | switch ($this->trapsDB->trapDBType) |
539 | 539 | { |
540 | 540 | case 'pgsql': |
541 | - $sql .= ' RETURNING id;'; |
|
542 | - $this->logging->log('sql : '.$sql,INFO ); |
|
541 | + $sql.=' RETURNING id;'; |
|
542 | + $this->logging->log('sql : '.$sql, INFO); |
|
543 | 543 | if (($ret_code=$db_conn->query($sql)) === false) { |
544 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
544 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
545 | 545 | } |
546 | - $this->logging->log('SQL insertion OK',INFO ); |
|
546 | + $this->logging->log('SQL insertion OK', INFO); |
|
547 | 547 | // Get last id to insert oid/values in secondary table |
548 | 548 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
549 | 549 | |
550 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
550 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
551 | 551 | } |
552 | - if (! isset($inserted_id_ret['id'])) { |
|
553 | - $this->logging->log('Error getting id',ERROR,''); |
|
552 | + if (!isset($inserted_id_ret['id'])) { |
|
553 | + $this->logging->log('Error getting id', ERROR, ''); |
|
554 | 554 | } |
555 | 555 | $this->trap_id=$inserted_id_ret['id']; |
556 | 556 | break; |
557 | 557 | case 'mysql': |
558 | - $sql .= ';'; |
|
559 | - $this->logging->log('sql : '.$sql,INFO ); |
|
558 | + $sql.=';'; |
|
559 | + $this->logging->log('sql : '.$sql, INFO); |
|
560 | 560 | if ($db_conn->query($sql) === false) { |
561 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
561 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
562 | 562 | } |
563 | - $this->logging->log('SQL insertion OK',INFO ); |
|
563 | + $this->logging->log('SQL insertion OK', INFO); |
|
564 | 564 | // Get last id to insert oid/values in secondary table |
565 | 565 | $sql='SELECT LAST_INSERT_ID();'; |
566 | 566 | if (($ret_code=$db_conn->query($sql)) === false) { |
567 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
567 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
568 | 568 | } |
569 | 569 | |
570 | 570 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
571 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
571 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
572 | 572 | $this->trap_id=$inserted_id; |
573 | 573 | break; |
574 | 574 | default: |
575 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
575 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
576 | 576 | } |
577 | - $this->logging->log('id found: '.$this->trap_id,INFO ); |
|
577 | + $this->logging->log('id found: '.$this->trap_id, INFO); |
|
578 | 578 | |
579 | 579 | // Fill trap extended data table |
580 | 580 | foreach ($this->trap_data_ext as $value) { |
581 | 581 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
582 | 582 | $firstcol=1; |
583 | - $value->trap_id = $this->trap_id; |
|
583 | + $value->trap_id=$this->trap_id; |
|
584 | 584 | $insert_col=''; |
585 | 585 | $insert_val=''; |
586 | 586 | foreach ($value as $col => $val) |
587 | 587 | { |
588 | - if ($firstcol==0) |
|
588 | + if ($firstcol == 0) |
|
589 | 589 | { |
590 | - $insert_col .=','; |
|
591 | - $insert_val .=','; |
|
590 | + $insert_col.=','; |
|
591 | + $insert_val.=','; |
|
592 | 592 | } |
593 | - $insert_col .= $col; |
|
594 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
593 | + $insert_col.=$col; |
|
594 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
595 | 595 | $firstcol=0; |
596 | 596 | } |
597 | 597 | |
598 | - $sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
598 | + $sql='INSERT INTO '.$this->db_prefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
599 | 599 | |
600 | 600 | if ($db_conn->query($sql) === false) { |
601 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
601 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
602 | 602 | } |
603 | 603 | } |
604 | 604 | } |
@@ -608,14 +608,14 @@ discard block |
||
608 | 608 | * @param $oid string oid in numeric |
609 | 609 | * @return mixed : PDO object or false |
610 | 610 | */ |
611 | - protected function getRules($ip,$oid) |
|
611 | + protected function getRules($ip, $oid) |
|
612 | 612 | { |
613 | 613 | $db_conn=$this->trapsDB->db_connect_trap(); |
614 | 614 | // fetch rules based on IP in rule and OID |
615 | 615 | $sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
616 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
616 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
617 | 617 | if (($ret_code=$db_conn->query($sql)) === false) { |
618 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
618 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
619 | 619 | return false; |
620 | 620 | } |
621 | 621 | $rules_all=$ret_code->fetchAll(); |
@@ -624,7 +624,7 @@ discard block |
||
624 | 624 | $rule_ret_key=0; |
625 | 625 | foreach ($rules_all as $key => $rule) |
626 | 626 | { |
627 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
627 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
628 | 628 | { |
629 | 629 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
630 | 630 | //TODO : get host name by API (and check if correct in rule). |
@@ -632,7 +632,7 @@ discard block |
||
632 | 632 | continue; |
633 | 633 | } |
634 | 634 | // TODO : get hosts IP by API |
635 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
635 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
636 | 636 | { // get ips of group members by oid |
637 | 637 | $db_conn2=$this->trapsDB->db_connect_ido(); |
638 | 638 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -643,15 +643,15 @@ discard block |
||
643 | 643 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
644 | 644 | WHERE o.name1='".$rule['host_group_name']."';"; |
645 | 645 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
646 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
646 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
647 | 647 | continue; |
648 | 648 | } |
649 | 649 | $grouphosts=$ret_code2->fetchAll(); |
650 | 650 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
651 | - foreach ( $grouphosts as $host) |
|
651 | + foreach ($grouphosts as $host) |
|
652 | 652 | { |
653 | 653 | //echo $host['ip4']."\n"; |
654 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
654 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
655 | 655 | { |
656 | 656 | //echo "Rule added \n"; |
657 | 657 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -674,7 +674,7 @@ discard block |
||
674 | 674 | $db_conn=$this->trapsDB->db_connect_trap(); |
675 | 675 | $sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
676 | 676 | if ($db_conn->query($sql) === false) { |
677 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
677 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
678 | 678 | } |
679 | 679 | } |
680 | 680 | |
@@ -686,31 +686,31 @@ discard block |
||
686 | 686 | * @param string $display |
687 | 687 | * @returnn bool true is service check was sent without error |
688 | 688 | */ |
689 | - public function serviceCheckResult($host,$service,$state,$display) |
|
689 | + public function serviceCheckResult($host, $service, $state, $display) |
|
690 | 690 | { |
691 | 691 | if ($this->api_use === false) |
692 | 692 | { |
693 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
694 | - $host.';' .$service .';' . $state . ';'.$display; |
|
695 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
693 | + $send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'. |
|
694 | + $host.';'.$service.';'.$state.';'.$display; |
|
695 | + $this->logging->log($send." : to : ".$this->icinga2cmd, INFO); |
|
696 | 696 | |
697 | 697 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
698 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
698 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
699 | 699 | return true; |
700 | 700 | } |
701 | 701 | else |
702 | 702 | { |
703 | - $api = $this->getAPI(); |
|
703 | + $api=$this->getAPI(); |
|
704 | 704 | $api->setCredentials($this->api_username, $this->api_password); |
705 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
705 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display); |
|
706 | 706 | if ($retcode == false) |
707 | 707 | { |
708 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
708 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
709 | 709 | return false; |
710 | 710 | } |
711 | 711 | else |
712 | 712 | { |
713 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
713 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
714 | 714 | return true; |
715 | 715 | } |
716 | 716 | } |
@@ -718,7 +718,7 @@ discard block |
||
718 | 718 | |
719 | 719 | public function getHostByIP($ip) |
720 | 720 | { |
721 | - $api = $this->getAPI(); |
|
721 | + $api=$this->getAPI(); |
|
722 | 722 | $api->setCredentials($this->api_username, $this->api_password); |
723 | 723 | return $api->getHostByIP($ip); |
724 | 724 | } |
@@ -731,32 +731,32 @@ discard block |
||
731 | 731 | protected function applyDisplay($display) |
732 | 732 | { |
733 | 733 | $matches=array(); |
734 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
734 | + while (preg_match('/_OID\(([0-9\.]+)\)/', $display, $matches) == 1) |
|
735 | 735 | { |
736 | 736 | $oid=$matches[1]; |
737 | 737 | $found=0; |
738 | - foreach($this->trap_data_ext as $val) |
|
738 | + foreach ($this->trap_data_ext as $val) |
|
739 | 739 | { |
740 | 740 | if ($oid == $val->oid) |
741 | 741 | { |
742 | - $val->value=preg_replace('/"/','',$val->value); |
|
742 | + $val->value=preg_replace('/"/', '', $val->value); |
|
743 | 743 | $rep=0; |
744 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
745 | - if ($rep==0) |
|
744 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
745 | + if ($rep == 0) |
|
746 | 746 | { |
747 | - $this->logging->log("Error in display",WARN,''); |
|
747 | + $this->logging->log("Error in display", WARN, ''); |
|
748 | 748 | return $display; |
749 | 749 | } |
750 | 750 | $found=1; |
751 | 751 | break; |
752 | 752 | } |
753 | 753 | } |
754 | - if ($found==0) |
|
754 | + if ($found == 0) |
|
755 | 755 | { |
756 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
757 | - if ($rep==0) |
|
756 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
757 | + if ($rep == 0) |
|
758 | 758 | { |
759 | - $this->logging->log("Error in display",WARN,''); |
|
759 | + $this->logging->log("Error in display", WARN, ''); |
|
760 | 760 | return $display; |
761 | 761 | } |
762 | 762 | } |
@@ -766,27 +766,27 @@ discard block |
||
766 | 766 | |
767 | 767 | |
768 | 768 | /***************** Eval & tokenizer functions ****************/ |
769 | - protected function eval_getElement($rule,&$item) |
|
769 | + protected function eval_getElement($rule, &$item) |
|
770 | 770 | { |
771 | - while ($rule[$item]==' ') $item++; |
|
772 | - if (preg_match('/[0-9\.]/',$rule[$item])) |
|
771 | + while ($rule[$item] == ' ') $item++; |
|
772 | + if (preg_match('/[0-9\.]/', $rule[$item])) |
|
773 | 773 | { // number |
774 | 774 | |
775 | - $item2=$item+1; |
|
776 | - while (($item2!=strlen($rule)) && (preg_match('/[0-9\.]/',$rule[$item2]))) { $item2++ ;} |
|
777 | - $val=substr($rule,$item,$item2-$item); |
|
775 | + $item2=$item + 1; |
|
776 | + while (($item2 != strlen($rule)) && (preg_match('/[0-9\.]/', $rule[$item2]))) { $item2++; } |
|
777 | + $val=substr($rule, $item, $item2 - $item); |
|
778 | 778 | $item=$item2; |
779 | 779 | //echo "number ".$val."\n"; |
780 | - return array(0,$val); |
|
780 | + return array(0, $val); |
|
781 | 781 | } |
782 | 782 | if ($rule[$item] == '"') |
783 | 783 | { // string |
784 | 784 | $item++; |
785 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
786 | - $val=substr($rule,$item,$item2-$item-1); |
|
785 | + $item2=$this->eval_getNext($rule, $item, '"'); |
|
786 | + $val=substr($rule, $item, $item2 - $item - 1); |
|
787 | 787 | $item=$item2; |
788 | 788 | //echo "string : ".$val."\n"; |
789 | - return array(1,$val); |
|
789 | + return array(1, $val); |
|
790 | 790 | } |
791 | 791 | |
792 | 792 | if ($rule[$item] == '(') |
@@ -795,14 +795,14 @@ discard block |
||
795 | 795 | $start=$item; |
796 | 796 | $parenthesis_count=0; |
797 | 797 | while (($item < strlen($rule)) // Not end of string AND |
798 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
798 | + && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded () |
|
799 | 799 | { |
800 | - if ($rule[$item] == '"' ) |
|
800 | + if ($rule[$item] == '"') |
|
801 | 801 | { // pass through string |
802 | 802 | $item++; |
803 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
803 | + $item=$this->eval_getNext($rule, $item, '"'); |
|
804 | 804 | } |
805 | - else{ |
|
805 | + else { |
|
806 | 806 | if ($rule[$item] == '(') |
807 | 807 | { |
808 | 808 | $parenthesis_count++; |
@@ -815,49 +815,49 @@ discard block |
||
815 | 815 | } |
816 | 816 | } |
817 | 817 | |
818 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
819 | - $val=substr($rule,$start,$item-$start); |
|
818 | + if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); } |
|
819 | + $val=substr($rule, $start, $item - $start); |
|
820 | 820 | $item++; |
821 | 821 | $start=0; |
822 | 822 | //echo "group : ".$val."\n"; |
823 | 823 | // returns evaluation of group as type 2 (boolean) |
824 | - return array(2,$this->evaluation($val,$start)); |
|
824 | + return array(2, $this->evaluation($val, $start)); |
|
825 | 825 | } |
826 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
826 | + throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]); |
|
827 | 827 | |
828 | 828 | } |
829 | 829 | |
830 | - protected function eval_getNext($rule,$item,$tok) |
|
830 | + protected function eval_getNext($rule, $item, $tok) |
|
831 | 831 | { |
832 | - while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;} |
|
833 | - if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
834 | - return $item+1; |
|
832 | + while (($rule[$item] != $tok) && ($item < strlen($rule))) { $item++; } |
|
833 | + if ($item == strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item); |
|
834 | + return $item + 1; |
|
835 | 835 | } |
836 | 836 | |
837 | - protected function eval_getOper($rule,&$item) |
|
837 | + protected function eval_getOper($rule, &$item) |
|
838 | 838 | { |
839 | - while ($rule[$item]==' ') $item++; |
|
839 | + while ($rule[$item] == ' ') $item++; |
|
840 | 840 | switch ($rule[$item]) |
841 | 841 | { |
842 | 842 | case '<': |
843 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
844 | - $item++; return array(0,"<"); |
|
843 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); } |
|
844 | + $item++; return array(0, "<"); |
|
845 | 845 | case '>': |
846 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
847 | - $item++; return array(0,">"); |
|
846 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); } |
|
847 | + $item++; return array(0, ">"); |
|
848 | 848 | case '=': |
849 | - $item++; return array(0,"="); |
|
849 | + $item++; return array(0, "="); |
|
850 | 850 | case '!': |
851 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
852 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
851 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); } |
|
852 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule." at ".$item); |
|
853 | 853 | case '~': |
854 | - $item++; return array(0,"~"); |
|
854 | + $item++; return array(0, "~"); |
|
855 | 855 | case '|': |
856 | - $item++; return array(1,"|"); |
|
856 | + $item++; return array(1, "|"); |
|
857 | 857 | case '&': |
858 | - $item++; return array(1,"&"); |
|
858 | + $item++; return array(1, "&"); |
|
859 | 859 | default : |
860 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
860 | + throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item); |
|
861 | 861 | } |
862 | 862 | } |
863 | 863 | |
@@ -869,10 +869,10 @@ discard block |
||
869 | 869 | * comparison int vs strings will return null (error) |
870 | 870 | * return : bool or null on error |
871 | 871 | */ |
872 | - public function evaluation($rule,&$item) |
|
872 | + public function evaluation($rule, &$item) |
|
873 | 873 | { |
874 | 874 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
875 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
875 | + if ($rule[$item] == '!') // If '!' found, negate next expression. |
|
876 | 876 | { |
877 | 877 | $negate=true; |
878 | 878 | $item++; |
@@ -882,63 +882,63 @@ discard block |
||
882 | 882 | $negate=false; |
883 | 883 | } |
884 | 884 | // First element : number, string or () |
885 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
885 | + list($type1, $val1)=$this->eval_getElement($rule, $item); |
|
886 | 886 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
887 | 887 | |
888 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
888 | + if ($item == strlen($rule)) // If only element, return value, but only boolean |
|
889 | 889 | { |
890 | 890 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
891 | - if ($negate === true) $val1= ! $val1; |
|
891 | + if ($negate === true) $val1=!$val1; |
|
892 | 892 | return $val1; |
893 | 893 | } |
894 | 894 | |
895 | 895 | // Second element : operator |
896 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
896 | + list($typec, $comp)=$this->eval_getOper($rule, $item); |
|
897 | 897 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
898 | 898 | |
899 | 899 | // Third element : number, string or () |
900 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
900 | + if ($rule[$item] == '!') // starts with a ! so evaluate whats next |
|
901 | 901 | { |
902 | 902 | $item++; |
903 | 903 | if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
904 | - $val2= ! $this->evaluation($rule,$item); |
|
904 | + $val2=!$this->evaluation($rule, $item); |
|
905 | 905 | $type2=2; // result is a boolean |
906 | 906 | } |
907 | 907 | else |
908 | 908 | { |
909 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
909 | + list($type2, $val2)=$this->eval_getElement($rule, $item); |
|
910 | 910 | } |
911 | 911 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
912 | 912 | |
913 | - if ($type1!=$type2) // cannot compare different types |
|
913 | + if ($type1 != $type2) // cannot compare different types |
|
914 | 914 | { |
915 | 915 | throw new Exception("Cannot compare string & number : ".$rule); |
916 | 916 | } |
917 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
917 | + if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number |
|
918 | 918 | { |
919 | 919 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
920 | 920 | } |
921 | 921 | |
922 | - switch ($comp){ |
|
923 | - case '<': $retVal= ($val1 < $val2); break; |
|
924 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
925 | - case '>': $retVal= ($val1 > $val2); break; |
|
926 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
927 | - case '=': $retVal= ($val1 == $val2); break; |
|
928 | - case '!=': $retVal= ($val1 != $val2); break; |
|
929 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
930 | - case '|': $retVal= ($val1 || $val2); break; |
|
931 | - case '&': $retVal= ($val1 && $val2); break; |
|
922 | + switch ($comp) { |
|
923 | + case '<': $retVal=($val1 < $val2); break; |
|
924 | + case '<=': $retVal=($val1 <= $val2); break; |
|
925 | + case '>': $retVal=($val1 > $val2); break; |
|
926 | + case '>=': $retVal=($val1 >= $val2); break; |
|
927 | + case '=': $retVal=($val1 == $val2); break; |
|
928 | + case '!=': $retVal=($val1 != $val2); break; |
|
929 | + case '~': $retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break; |
|
930 | + case '|': $retVal=($val1 || $val2); break; |
|
931 | + case '&': $retVal=($val1 && $val2); break; |
|
932 | 932 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
933 | 933 | } |
934 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
934 | + if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression |
|
935 | 935 | |
936 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
936 | + if ($item == strlen($rule)) return $retVal; // End of string : return evaluation |
|
937 | 937 | // check for logical operator : |
938 | 938 | switch ($rule[$item]) |
939 | 939 | { |
940 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
941 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
940 | + case '|': $item++; return ($retVal || $this->evaluation($rule, $item)); |
|
941 | + case '&': $item++; return ($retVal && $this->evaluation($rule, $item)); |
|
942 | 942 | |
943 | 943 | default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
944 | 944 | } |
@@ -950,17 +950,17 @@ discard block |
||
950 | 950 | $rule2=''; |
951 | 951 | while ($item < strlen($rule)) |
952 | 952 | { |
953 | - if ($rule[$item]==' ') { $item++; continue; } |
|
954 | - if ($rule[$item]=='"') |
|
953 | + if ($rule[$item] == ' ') { $item++; continue; } |
|
954 | + if ($rule[$item] == '"') |
|
955 | 955 | { |
956 | 956 | $rule2.=$rule[$item]; |
957 | 957 | $item++; |
958 | - while (($rule[$item]!='"') && ($item < strlen($rule))) |
|
958 | + while (($rule[$item] != '"') && ($item < strlen($rule))) |
|
959 | 959 | { |
960 | 960 | $rule2.=$rule[$item]; |
961 | 961 | $item++; |
962 | 962 | } |
963 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
963 | + if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item); |
|
964 | 964 | $rule2.=$rule[$item]; |
965 | 965 | $item++; |
966 | 966 | continue; |
@@ -980,12 +980,12 @@ discard block |
||
980 | 980 | |
981 | 981 | protected function eval_rule($rule) |
982 | 982 | { |
983 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
983 | + if ($rule == null || $rule == '') // Empty rule is always true |
|
984 | 984 | { |
985 | 985 | return true; |
986 | 986 | } |
987 | 987 | $matches=array(); |
988 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
988 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1) |
|
989 | 989 | { |
990 | 990 | $oid=$matches[1]; |
991 | 991 | $found=0; |
@@ -997,49 +997,49 @@ discard block |
||
997 | 997 | // replace * with \* in oid for preg_replace |
998 | 998 | $oid=preg_replace('/\*/', '\*', $oid); |
999 | 999 | |
1000 | - $this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG ); |
|
1000 | + $this->logging->log('OID in rule : '.$oid.' / '.$oidR, DEBUG); |
|
1001 | 1001 | |
1002 | - foreach($this->trap_data_ext as $val) |
|
1002 | + foreach ($this->trap_data_ext as $val) |
|
1003 | 1003 | { |
1004 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
1004 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
1005 | 1005 | { |
1006 | - if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value)) |
|
1006 | + if (!preg_match('/^[0-9]*\.?[0-9]+$/', $val->value)) |
|
1007 | 1007 | { // If not a number, change " to ' and put " around it |
1008 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
1008 | + $val->value=preg_replace('/"/', "'", $val->value); |
|
1009 | 1009 | $val->value='"'.$val->value.'"'; |
1010 | 1010 | } |
1011 | 1011 | $rep=0; |
1012 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
1013 | - if ($rep==0) |
|
1012 | + $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep); |
|
1013 | + if ($rep == 0) |
|
1014 | 1014 | { |
1015 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
1015 | + $this->logging->log("Error in rule_eval", WARN, ''); |
|
1016 | 1016 | return false; |
1017 | 1017 | } |
1018 | 1018 | $found=1; |
1019 | 1019 | break; |
1020 | 1020 | } |
1021 | 1021 | } |
1022 | - if ($found==0) |
|
1022 | + if ($found == 0) |
|
1023 | 1023 | { // OID not found : throw error |
1024 | 1024 | throw new Exception('OID '.$oid.' not found in trap'); |
1025 | 1025 | } |
1026 | 1026 | } |
1027 | 1027 | $item=0; |
1028 | 1028 | $rule=$this->eval_cleanup($rule); |
1029 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
1029 | + $this->logging->log('Rule after clenup: '.$rule, INFO); |
|
1030 | 1030 | |
1031 | - return $this->evaluation($rule,$item); |
|
1031 | + return $this->evaluation($rule, $item); |
|
1032 | 1032 | } |
1033 | 1033 | |
1034 | 1034 | /** Match rules for current trap and do action |
1035 | 1035 | */ |
1036 | 1036 | public function applyRules() |
1037 | 1037 | { |
1038 | - $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
|
1038 | + $rules=$this->getRules($this->trap_data['source_ip'], $this->trap_data['trap_oid']); |
|
1039 | 1039 | |
1040 | - if ($rules===false || count($rules)==0) |
|
1040 | + if ($rules === false || count($rules) == 0) |
|
1041 | 1041 | { |
1042 | - $this->logging->log('No rules found for this trap',INFO ); |
|
1042 | + $this->logging->log('No rules found for this trap', INFO); |
|
1043 | 1043 | $this->trap_data['status']='unknown'; |
1044 | 1044 | $this->trap_to_db=true; |
1045 | 1045 | return; |
@@ -1054,58 +1054,58 @@ discard block |
||
1054 | 1054 | $service_name=$rule['service_name']; |
1055 | 1055 | |
1056 | 1056 | $display=$this->applyDisplay($rule['display']); |
1057 | - $this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', '; |
|
1057 | + $this->trap_action=($this->trap_action == null) ? '' : $this->trap_action.', '; |
|
1058 | 1058 | try |
1059 | 1059 | { |
1060 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
1060 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
1061 | 1061 | $evalr=$this->eval_rule($rule['rule']); |
1062 | 1062 | |
1063 | 1063 | if ($evalr == true) |
1064 | 1064 | { |
1065 | 1065 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
1066 | 1066 | $action=$rule['action_match']; |
1067 | - $this->logging->log('action OK : '.$action,INFO ); |
|
1067 | + $this->logging->log('action OK : '.$action, INFO); |
|
1068 | 1068 | if ($action >= 0) |
1069 | 1069 | { |
1070 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
1070 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
1071 | 1071 | { |
1072 | 1072 | $this->trap_action.='Error sending status : check cmd/API'; |
1073 | 1073 | } |
1074 | 1074 | else |
1075 | 1075 | { |
1076 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1076 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
1077 | 1077 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
1078 | 1078 | } |
1079 | 1079 | } |
1080 | 1080 | else |
1081 | 1081 | { |
1082 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1082 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
1083 | 1083 | } |
1084 | - $this->trap_to_db=($action==-2)?false:true; |
|
1084 | + $this->trap_to_db=($action == -2) ?false:true; |
|
1085 | 1085 | } |
1086 | 1086 | else |
1087 | 1087 | { |
1088 | 1088 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
1089 | 1089 | |
1090 | 1090 | $action=$rule['action_nomatch']; |
1091 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
1091 | + $this->logging->log('action NOK : '.$action, INFO); |
|
1092 | 1092 | if ($action >= 0) |
1093 | 1093 | { |
1094 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
1094 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
1095 | 1095 | { |
1096 | 1096 | $this->trap_action.='Error sending status : check cmd/API'; |
1097 | 1097 | } |
1098 | 1098 | else |
1099 | 1099 | { |
1100 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1100 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
1101 | 1101 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
1102 | 1102 | } |
1103 | 1103 | } |
1104 | 1104 | else |
1105 | 1105 | { |
1106 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
1106 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
1107 | 1107 | } |
1108 | - $this->trap_to_db=($action==-2)?false:true; |
|
1108 | + $this->trap_to_db=($action == -2) ?false:true; |
|
1109 | 1109 | } |
1110 | 1110 | // Put name in source_name |
1111 | 1111 | if (!isset($this->trap_data['source_name'])) |
@@ -1114,7 +1114,7 @@ discard block |
||
1114 | 1114 | } |
1115 | 1115 | else |
1116 | 1116 | { |
1117 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
|
1117 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trap_data['source_name'])) |
|
1118 | 1118 | { // only add if not present |
1119 | 1119 | $this->trap_data['source_name'].=','.$rule['host_name']; |
1120 | 1120 | } |
@@ -1122,13 +1122,13 @@ discard block |
||
1122 | 1122 | } |
1123 | 1123 | catch (Exception $e) |
1124 | 1124 | { |
1125 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
1125 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
1126 | 1126 | $this->trap_action.=' ERR : '.$e->getMessage(); |
1127 | 1127 | $this->trap_data['status']='error'; |
1128 | 1128 | } |
1129 | 1129 | |
1130 | 1130 | } |
1131 | - if ($this->trap_data['status']=='error') |
|
1131 | + if ($this->trap_data['status'] == 'error') |
|
1132 | 1132 | { |
1133 | 1133 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
1134 | 1134 | } |
@@ -1144,13 +1144,13 @@ discard block |
||
1144 | 1144 | public function add_rule_final($time) |
1145 | 1145 | { |
1146 | 1146 | $db_conn=$this->trapsDB->db_connect_trap(); |
1147 | - if ($this->trap_action==null) |
|
1147 | + if ($this->trap_action == null) |
|
1148 | 1148 | { |
1149 | 1149 | $this->trap_action='No action'; |
1150 | 1150 | } |
1151 | 1151 | $sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."' WHERE (id = '".$this->trap_id."');"; |
1152 | 1152 | if ($db_conn->query($sql) === false) { |
1153 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
1153 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
1154 | 1154 | } |
1155 | 1155 | } |
1156 | 1156 | |
@@ -1160,14 +1160,14 @@ discard block |
||
1160 | 1160 | * @param $schema_file string File to read schema from |
1161 | 1161 | * @param $table_prefix string to replace #PREFIX# in schema file by this |
1162 | 1162 | */ |
1163 | - public function create_schema($schema_file,$table_prefix) |
|
1163 | + public function create_schema($schema_file, $table_prefix) |
|
1164 | 1164 | { |
1165 | 1165 | //Read data from snmptrapd from stdin |
1166 | 1166 | $input_stream=fopen($schema_file, 'r'); |
1167 | 1167 | |
1168 | - if ($input_stream=== false) |
|
1168 | + if ($input_stream === false) |
|
1169 | 1169 | { |
1170 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
1170 | + $this->logging->log("Error reading schema !", ERROR, ''); |
|
1171 | 1171 | return; |
1172 | 1172 | } |
1173 | 1173 | $newline=''; |
@@ -1177,14 +1177,14 @@ discard block |
||
1177 | 1177 | |
1178 | 1178 | while (($line=fgets($input_stream)) !== false) |
1179 | 1179 | { |
1180 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
1180 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
1181 | 1181 | if (preg_match('/; *$/', $newline)) |
1182 | 1182 | { |
1183 | - $sql= $newline; |
|
1183 | + $sql=$newline; |
|
1184 | 1184 | if ($db_conn->query($sql) === false) { |
1185 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
1185 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
1186 | 1186 | } |
1187 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
1187 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
1188 | 1188 | { |
1189 | 1189 | $cur_table='table '.$cur_table_array[1]; |
1190 | 1190 | } |
@@ -1192,19 +1192,19 @@ discard block |
||
1192 | 1192 | { |
1193 | 1193 | $cur_table='secret SQL stuff :-)'; |
1194 | 1194 | } |
1195 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
1195 | + $this->logging->log('Creating : '.$cur_table, INFO); |
|
1196 | 1196 | $newline=''; |
1197 | 1197 | } |
1198 | 1198 | } |
1199 | 1199 | |
1200 | - $sql= $newline; |
|
1200 | + $sql=$newline; |
|
1201 | 1201 | if ($sql != '') |
1202 | 1202 | { |
1203 | 1203 | if ($db_conn->query($sql) === false) { |
1204 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
1204 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
1205 | 1205 | } |
1206 | 1206 | } |
1207 | - $this->logging->log('Schema created',INFO); |
|
1207 | + $this->logging->log('Schema created', INFO); |
|
1208 | 1208 | } |
1209 | 1209 | |
1210 | 1210 | /** |
@@ -1215,14 +1215,14 @@ discard block |
||
1215 | 1215 | * @param bool $getmsg : only get messages from version upgrades |
1216 | 1216 | * @return string : if $getmsg=true, return messages. |
1217 | 1217 | */ |
1218 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
1218 | + public function update_schema($prefix, $target_version, $table_prefix, $getmsg=false) |
|
1219 | 1219 | { |
1220 | 1220 | // Get current db number |
1221 | 1221 | $db_conn=$this->trapsDB->db_connect_trap(); |
1222 | 1222 | $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' '; |
1223 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
1223 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
1224 | 1224 | if (($ret_code=$db_conn->query($sql)) === false) { |
1225 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
1225 | + $this->logging->log('Cannot get db version. Query : '.$sql, 2, ''); |
|
1226 | 1226 | return; |
1227 | 1227 | } |
1228 | 1228 | $version=$ret_code->fetchAll(); |
@@ -1231,47 +1231,47 @@ discard block |
||
1231 | 1231 | |
1232 | 1232 | if ($this->trapsDB->trapDBType == 'pgsql') |
1233 | 1233 | { |
1234 | - $prefix .= 'update_pgsql/schema_'; |
|
1234 | + $prefix.='update_pgsql/schema_'; |
|
1235 | 1235 | } |
1236 | 1236 | else |
1237 | 1237 | { |
1238 | - $prefix .= 'update_sql/schema_'; |
|
1238 | + $prefix.='update_sql/schema_'; |
|
1239 | 1239 | } |
1240 | 1240 | //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
1241 | 1241 | if ($getmsg === true) |
1242 | 1242 | { |
1243 | 1243 | $message=''; |
1244 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
1245 | - while($cur_version<$target_version) |
|
1244 | + $this->logging->log('getting message for upgrade', DEBUG); |
|
1245 | + while ($cur_version < $target_version) |
|
1246 | 1246 | { |
1247 | 1247 | $cur_version++; |
1248 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
1248 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
1249 | 1249 | $input_stream=fopen($updateFile, 'r'); |
1250 | - if ($input_stream=== false) |
|
1250 | + if ($input_stream === false) |
|
1251 | 1251 | { |
1252 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
1252 | + $this->logging->log("Error reading update file ".$updateFile, 2, ''); |
|
1253 | 1253 | return; |
1254 | 1254 | } |
1255 | 1255 | do { $line=fgets($input_stream); } |
1256 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
1256 | + while ($line !== false && !preg_match('/#MESSAGE/', $line)); |
|
1257 | 1257 | if ($line === false) |
1258 | 1258 | { |
1259 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
1259 | + $this->logging->log("No message in file ".$updateFile, 2, ''); |
|
1260 | 1260 | return; |
1261 | 1261 | } |
1262 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
1262 | + $message.=($cur_version - 1).'->'.$cur_version.' : '.preg_replace('/#MESSAGE : /', '', $line)."\n"; |
|
1263 | 1263 | } |
1264 | 1264 | return $message; |
1265 | 1265 | } |
1266 | - while($cur_version<$target_version) |
|
1266 | + while ($cur_version < $target_version) |
|
1267 | 1267 | { // tODO : execute pre & post scripts |
1268 | 1268 | $cur_version++; |
1269 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
1270 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
1269 | + $this->logging->log('Updating to version : '.$cur_version, INFO); |
|
1270 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
1271 | 1271 | $input_stream=fopen($updateFile, 'r'); |
1272 | - if ($input_stream=== false) |
|
1272 | + if ($input_stream === false) |
|
1273 | 1273 | { |
1274 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
1274 | + $this->logging->log("Error reading update file ".$updateFile, 2, ''); |
|
1275 | 1275 | return; |
1276 | 1276 | } |
1277 | 1277 | $newline=''; |
@@ -1280,24 +1280,24 @@ discard block |
||
1280 | 1280 | while (($line=fgets($input_stream)) !== false) |
1281 | 1281 | { |
1282 | 1282 | if (preg_match('/^#/', $line)) continue; // ignore comment lines |
1283 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
1283 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
1284 | 1284 | if (preg_match('/; *$/', $newline)) |
1285 | 1285 | { |
1286 | 1286 | $sql_req=$db_conn->prepare($newline); |
1287 | 1287 | if ($sql_req->execute() === false) { |
1288 | - $this->logging->log('Error create schema : '.$newline,1,''); |
|
1288 | + $this->logging->log('Error create schema : '.$newline, 1, ''); |
|
1289 | 1289 | } |
1290 | 1290 | $cur_table_array=array(); |
1291 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
1291 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
1292 | 1292 | { |
1293 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
1293 | + $cur_table=$cur_table_array[1].' SQL table '.$cur_table_array[2]; |
|
1294 | 1294 | } |
1295 | 1295 | else |
1296 | 1296 | { |
1297 | 1297 | $cur_table='secret SQL stuff :-)'; |
1298 | 1298 | //$cur_table=$newline; |
1299 | 1299 | } |
1300 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
1300 | + $this->logging->log('Doing : '.$cur_table, INFO); |
|
1301 | 1301 | |
1302 | 1302 | $newline=''; |
1303 | 1303 | } |
@@ -1310,13 +1310,13 @@ discard block |
||
1310 | 1310 | //} |
1311 | 1311 | |
1312 | 1312 | $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
1313 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
1313 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
1314 | 1314 | if ($db_conn->query($sql) === false) { |
1315 | - $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
|
1315 | + $this->logging->log('Cannot update db version. Query : '.$sql, 2); |
|
1316 | 1316 | return; |
1317 | 1317 | } |
1318 | 1318 | |
1319 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
1319 | + $this->logging->log('Schema updated to version : '.$cur_version, INFO); |
|
1320 | 1320 | } |
1321 | 1321 | } |
1322 | 1322 | |
@@ -1337,7 +1337,7 @@ discard block |
||
1337 | 1337 | WHERE s.current_state != 0;"; |
1338 | 1338 | $db_conn=$this->trapsDB->db_connect_ido(); |
1339 | 1339 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
1340 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
1340 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
1341 | 1341 | return 0; |
1342 | 1342 | } |
1343 | 1343 | $services=$services_db->fetchAll(); |
@@ -1346,7 +1346,7 @@ discard block |
||
1346 | 1346 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;"; |
1347 | 1347 | $db_conn2=$this->trapsDB->db_connect_trap(); |
1348 | 1348 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
1349 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
1349 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
1350 | 1350 | return 0; |
1351 | 1351 | } |
1352 | 1352 | $rules=$rules_db->fetchAll(); |
@@ -1362,13 +1362,13 @@ discard block |
||
1362 | 1362 | $service['host_name'] == $rule['host_name'] && |
1363 | 1363 | ($service['last_check'] + $rule['revert_ok']) < $now) |
1364 | 1364 | { |
1365 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
1365 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
1366 | 1366 | $numreset++; |
1367 | 1367 | } |
1368 | 1368 | } |
1369 | 1369 | } |
1370 | 1370 | echo "\n"; |
1371 | - echo $numreset . " service(s) reset to OK\n"; |
|
1371 | + echo $numreset." service(s) reset to OK\n"; |
|
1372 | 1372 | return 0; |
1373 | 1373 | |
1374 | 1374 | } |
@@ -1389,7 +1389,7 @@ discard block |
||
1389 | 1389 | * @param string $description |
1390 | 1390 | * @return number : 0=unchanged, 1 = changed, 2=created |
1391 | 1391 | */ |
1392 | - public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
|
1392 | + public function update_oid($oid, $mib, $name, $type, $textConv, $dispHint, $syntax, $type_enum, $description=NULL) |
|
1393 | 1393 | { |
1394 | 1394 | $db_conn=$this->trapsDB->db_connect_trap(); |
1395 | 1395 | $description=$db_conn->quote($description); |
@@ -1399,7 +1399,7 @@ discard block |
||
1399 | 1399 | { // newly created. |
1400 | 1400 | return 0; |
1401 | 1401 | } |
1402 | - if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
1402 | + if ($name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
1403 | 1403 | $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
1404 | 1404 | $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
1405 | 1405 | //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
@@ -1419,23 +1419,23 @@ discard block |
||
1419 | 1419 | ':name' => $name, |
1420 | 1420 | ':type' => $type, |
1421 | 1421 | ':mib' => $mib, |
1422 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
1423 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
1424 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
1425 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
1426 | - ':description' => ($description==null)?'null':$description, |
|
1422 | + ':tc' => ($textConv == null) ? 'null' : $textConv, |
|
1423 | + ':display_hint' => ($dispHint == null) ? 'null' : $dispHint, |
|
1424 | + ':syntax' => ($syntax == null) ? 'null' : $syntax, |
|
1425 | + ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, |
|
1426 | + ':description' => ($description == null) ? 'null' : $description, |
|
1427 | 1427 | ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
1428 | 1428 | ); |
1429 | 1429 | |
1430 | 1430 | if ($sqlQuery->execute($sqlParam) === false) { |
1431 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
1431 | + $this->logging->log('Error in query : '.$sql, ERROR, ''); |
|
1432 | 1432 | } |
1433 | - $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
1433 | + $this->logging->log('Trap updated : '.$name.' / OID : '.$oid, DEBUG); |
|
1434 | 1434 | return 1; |
1435 | 1435 | } |
1436 | 1436 | else |
1437 | 1437 | { |
1438 | - $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
1438 | + $this->logging->log('Trap unchanged : '.$name.' / OID : '.$oid, DEBUG); |
|
1439 | 1439 | return 0; |
1440 | 1440 | } |
1441 | 1441 | } |
@@ -1445,11 +1445,11 @@ discard block |
||
1445 | 1445 | |
1446 | 1446 | $sql='INSERT INTO '.$this->db_prefix.'mib_cache '. |
1447 | 1447 | '(oid, name, type , mib, textual_convention, display_hint '. |
1448 | - ', syntax, type_enum , description ) ' . |
|
1448 | + ', syntax, type_enum , description ) '. |
|
1449 | 1449 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
1450 | 1450 | ', :syntax, :type_enum, :description )'; |
1451 | 1451 | |
1452 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
1452 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql.='RETURNING id'; |
|
1453 | 1453 | |
1454 | 1454 | $sqlQuery=$db_conn->prepare($sql); |
1455 | 1455 | |
@@ -1458,15 +1458,15 @@ discard block |
||
1458 | 1458 | ':name' => $name, |
1459 | 1459 | ':type' => $type, |
1460 | 1460 | ':mib' => $mib, |
1461 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
1462 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
1463 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
1464 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
1465 | - ':description' => ($description==null)?'null':$description |
|
1461 | + ':tc' => ($textConv == null) ? 'null' : $textConv, |
|
1462 | + ':display_hint' => ($dispHint == null) ? 'null' : $dispHint, |
|
1463 | + ':syntax' => ($syntax == null) ? 'null' : $syntax, |
|
1464 | + ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, |
|
1465 | + ':description' => ($description == null) ? 'null' : $description |
|
1466 | 1466 | ); |
1467 | 1467 | |
1468 | 1468 | if ($sqlQuery->execute($sqlParam) === false) { |
1469 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
1469 | + $this->logging->log('Error in query : '.$sql, 1, ''); |
|
1470 | 1470 | } |
1471 | 1471 | |
1472 | 1472 | switch ($this->trapsDB->trapDBType) |
@@ -1474,10 +1474,10 @@ discard block |
||
1474 | 1474 | case 'pgsql': |
1475 | 1475 | // Get last id to insert oid/values in secondary table |
1476 | 1476 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
1477 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
1477 | + $this->logging->log('Error getting id - pgsql - ', 1, ''); |
|
1478 | 1478 | } |
1479 | - if (! isset($inserted_id_ret['id'])) { |
|
1480 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
1479 | + if (!isset($inserted_id_ret['id'])) { |
|
1480 | + $this->logging->log('Error getting id - pgsql - empty.', 1, ''); |
|
1481 | 1481 | } |
1482 | 1482 | $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
1483 | 1483 | break; |
@@ -1485,15 +1485,15 @@ discard block |
||
1485 | 1485 | // Get last id to insert oid/values in secondary table |
1486 | 1486 | $sql='SELECT LAST_INSERT_ID();'; |
1487 | 1487 | if (($ret_code=$db_conn->query($sql)) === false) { |
1488 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
1488 | + $this->logging->log('Erreur getting id - mysql - ', 1, ''); |
|
1489 | 1489 | } |
1490 | 1490 | |
1491 | 1491 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
1492 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
1492 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
1493 | 1493 | $this->dbOidIndex[$oid]['id']=$inserted_id; |
1494 | 1494 | break; |
1495 | 1495 | default: |
1496 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
1496 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
1497 | 1497 | } |
1498 | 1498 | |
1499 | 1499 | // Set as newly created. |
@@ -1508,21 +1508,21 @@ discard block |
||
1508 | 1508 | * @param array $objects : array of objects name (without MIB) |
1509 | 1509 | * @param bool $check_existing : check instead of create |
1510 | 1510 | */ |
1511 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
1511 | + public function trap_objects($trapOID, $trapmib, $objects, $check_existing) |
|
1512 | 1512 | { |
1513 | 1513 | $dbObjects=null; // cache of objects for trap in db |
1514 | 1514 | $db_conn=$this->trapsDB->db_connect_trap(); |
1515 | 1515 | |
1516 | 1516 | // Get id of trapmib. |
1517 | 1517 | |
1518 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
1518 | + $trapId=$this->dbOidIndex[$trapOID]['id']; |
|
1519 | 1519 | if ($check_existing === true) |
1520 | 1520 | { |
1521 | 1521 | // Get all objects |
1522 | 1522 | $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
1523 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
1523 | + $this->logging->log('SQL query get all traps: '.$sql, DEBUG); |
|
1524 | 1524 | if (($ret_code=$db_conn->query($sql)) === false) { |
1525 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
1525 | + $this->logging->log('No result in query : '.$sql, 1, ''); |
|
1526 | 1526 | } |
1527 | 1527 | $dbObjectsRaw=$ret_code->fetchAll(); |
1528 | 1528 | |
@@ -1536,39 +1536,39 @@ discard block |
||
1536 | 1536 | $match=$snmptrans=array(); |
1537 | 1537 | $retVal=0; |
1538 | 1538 | $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
1539 | - $tmpdesc='';$indesc=false; |
|
1539 | + $tmpdesc=''; $indesc=false; |
|
1540 | 1540 | |
1541 | 1541 | $objMib=$trapmib; |
1542 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1543 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
1544 | - if ($retVal!=0) |
|
1542 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1543 | + ' -On -Td '.$objMib.'::'.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
1544 | + if ($retVal != 0) |
|
1545 | 1545 | { |
1546 | 1546 | // Maybe not trap mib, search with IR |
1547 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1548 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
1549 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
1547 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1548 | + ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
1549 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match)) |
|
1550 | 1550 | { // Not found -> continue with warning |
1551 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
1551 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, ''); |
|
1552 | 1552 | continue; |
1553 | 1553 | } |
1554 | 1554 | $objMib=$match[1]; |
1555 | 1555 | |
1556 | 1556 | // Do the snmptranslate again. |
1557 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1558 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
1559 | - if ($retVal!=0) { |
|
1560 | - $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
1557 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1558 | + ' -On -Td '.$objMib.'::'.$object, $snmptrans, $retVal); |
|
1559 | + if ($retVal != 0) { |
|
1560 | + $this->logging->log('Error finding trap object : '.$objMib.'::'.$object, 2, ''); |
|
1561 | 1561 | } |
1562 | 1562 | |
1563 | 1563 | } |
1564 | 1564 | foreach ($snmptrans as $line) |
1565 | 1565 | { |
1566 | - if ($indesc===true) |
|
1566 | + if ($indesc === true) |
|
1567 | 1567 | { |
1568 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
1569 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
1568 | + $line=preg_replace('/[\t ]+/', ' ', $line); |
|
1569 | + if (preg_match('/(.*)"$/', $line, $match)) |
|
1570 | 1570 | { |
1571 | - $objDesc = $tmpdesc . $match[1]; |
|
1571 | + $objDesc=$tmpdesc.$match[1]; |
|
1572 | 1572 | $indesc=false; |
1573 | 1573 | } |
1574 | 1574 | $tmpdesc.=$line; |
@@ -1579,43 +1579,43 @@ discard block |
||
1579 | 1579 | $objOid=$line; |
1580 | 1580 | continue; |
1581 | 1581 | } |
1582 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
1582 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match)) |
|
1583 | 1583 | { |
1584 | 1584 | $objSyntax=$match[1]; |
1585 | 1585 | $objEnum=$match[2]; |
1586 | 1586 | continue; |
1587 | 1587 | } |
1588 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
1588 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match)) |
|
1589 | 1589 | { |
1590 | 1590 | $objSyntax=$match[1]; |
1591 | 1591 | continue; |
1592 | 1592 | } |
1593 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
1593 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match)) |
|
1594 | 1594 | { |
1595 | 1595 | $objDispHint=$match[1]; |
1596 | 1596 | continue; |
1597 | 1597 | } |
1598 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
1598 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match)) |
|
1599 | 1599 | { |
1600 | 1600 | $objDesc=$match[1]; |
1601 | 1601 | continue; |
1602 | 1602 | } |
1603 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
1603 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match)) |
|
1604 | 1604 | { |
1605 | 1605 | $tmpdesc=$match[1]; |
1606 | 1606 | $indesc=true; |
1607 | 1607 | continue; |
1608 | 1608 | } |
1609 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
1609 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match)) |
|
1610 | 1610 | { |
1611 | 1611 | $objTc=$match[1]; |
1612 | 1612 | continue; |
1613 | 1613 | } |
1614 | 1614 | } |
1615 | - $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
1615 | + $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc", DEBUG); |
|
1616 | 1616 | //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
1617 | 1617 | // Update |
1618 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
1618 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum, $objDesc); |
|
1619 | 1619 | |
1620 | 1620 | if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
1621 | 1621 | { // if link exists, continue |
@@ -1636,7 +1636,7 @@ discard block |
||
1636 | 1636 | ); |
1637 | 1637 | |
1638 | 1638 | if ($sqlQuery->execute($sqlParam) === false) { |
1639 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
1639 | + $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$objOid]['id'], 1, ''); |
|
1640 | 1640 | } |
1641 | 1641 | } |
1642 | 1642 | if ($check_existing === true) |
@@ -1653,19 +1653,19 @@ discard block |
||
1653 | 1653 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
1654 | 1654 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
1655 | 1655 | */ |
1656 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
1656 | + public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1') |
|
1657 | 1657 | { |
1658 | 1658 | // Timing |
1659 | - $timeTaken = microtime(true); |
|
1659 | + $timeTaken=microtime(true); |
|
1660 | 1660 | $retVal=0; |
1661 | 1661 | // Get all mib objects from all mibs |
1662 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
1663 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
1662 | + $snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
1663 | + $this->logging->log('Getting all traps : '.$snmpCommand, DEBUG); |
|
1664 | 1664 | unset($this->objectsAll); |
1665 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
1666 | - if ($retVal!=0) |
|
1665 | + exec($snmpCommand, $this->objectsAll, $retVal); |
|
1666 | + if ($retVal != 0) |
|
1667 | 1667 | { |
1668 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
1668 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
1669 | 1669 | } |
1670 | 1670 | |
1671 | 1671 | // Get all mibs from databse to have a memory index |
@@ -1673,14 +1673,14 @@ discard block |
||
1673 | 1673 | $db_conn=$this->trapsDB->db_connect_trap(); |
1674 | 1674 | |
1675 | 1675 | $sql='SELECT * from '.$this->db_prefix.'mib_cache;'; |
1676 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
1676 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
1677 | 1677 | if (($ret_code=$db_conn->query($sql)) === false) { |
1678 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
1678 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
1679 | 1679 | } |
1680 | 1680 | $this->dbOidAll=$ret_code->fetchAll(); |
1681 | 1681 | $this->dbOidIndex=array(); |
1682 | 1682 | // Create the index for db; |
1683 | - foreach($this->dbOidAll as $key=>$val) |
|
1683 | + foreach ($this->dbOidAll as $key=>$val) |
|
1684 | 1684 | { |
1685 | 1685 | $this->dbOidIndex[$val['oid']]['key']=$key; |
1686 | 1686 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
@@ -1688,11 +1688,11 @@ discard block |
||
1688 | 1688 | |
1689 | 1689 | // Count elements to show progress |
1690 | 1690 | $numElements=count($this->objectsAll); |
1691 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
1691 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO); |
|
1692 | 1692 | |
1693 | - $step=$basestep=$numElements/10; // output display of % done |
|
1693 | + $step=$basestep=$numElements / 10; // output display of % done |
|
1694 | 1694 | $num_step=0; |
1695 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
1695 | + $timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds |
|
1696 | 1696 | |
1697 | 1697 | // Create index for trap objects |
1698 | 1698 | $this->trapObjectsIndex=array(); |
@@ -1702,28 +1702,28 @@ discard block |
||
1702 | 1702 | $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
1703 | 1703 | $time_num_traps=0; |
1704 | 1704 | |
1705 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
1705 | + for ($curElement=0; $curElement < $numElements; $curElement++) |
|
1706 | 1706 | { |
1707 | - $time_1= microtime(true); |
|
1708 | - if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
|
1707 | + $time_1=microtime(true); |
|
1708 | + if ((microtime(true) - $timeFiveSec) > 2 && $display_progress) |
|
1709 | 1709 | { // echo a . every 2 sec |
1710 | 1710 | echo '.'; |
1711 | - $timeFiveSec = microtime(true); |
|
1711 | + $timeFiveSec=microtime(true); |
|
1712 | 1712 | } |
1713 | - if ($curElement>$step) |
|
1713 | + if ($curElement > $step) |
|
1714 | 1714 | { // display progress |
1715 | 1715 | $num_step++; |
1716 | 1716 | $step+=$basestep; |
1717 | 1717 | if ($display_progress) |
1718 | 1718 | { |
1719 | - echo "\n" . ($num_step*10). '% : '; |
|
1719 | + echo "\n".($num_step * 10).'% : '; |
|
1720 | 1720 | } |
1721 | 1721 | } |
1722 | 1722 | // Get oid or pass if not found |
1723 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
1723 | + if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement])) |
|
1724 | 1724 | { |
1725 | - $time_parse1 += microtime(true) - $time_1; |
|
1726 | - $time_parse1N ++; |
|
1725 | + $time_parse1+=microtime(true) - $time_1; |
|
1726 | + $time_parse1N++; |
|
1727 | 1727 | continue; |
1728 | 1728 | } |
1729 | 1729 | $oid=$this->objectsAll[$curElement]; |
@@ -1732,9 +1732,9 @@ discard block |
||
1732 | 1732 | $curElement++; |
1733 | 1733 | $match=$snmptrans=array(); |
1734 | 1734 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
1735 | - $this->objectsAll[$curElement],$match)) |
|
1735 | + $this->objectsAll[$curElement], $match)) |
|
1736 | 1736 | { |
1737 | - $time_check1 += microtime(true) - $time_1; |
|
1737 | + $time_check1+=microtime(true) - $time_1; |
|
1738 | 1738 | $time_check1N++; |
1739 | 1739 | continue; |
1740 | 1740 | } |
@@ -1742,21 +1742,21 @@ discard block |
||
1742 | 1742 | $name=$match[1]; // Name |
1743 | 1743 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
1744 | 1744 | |
1745 | - if ($type==0) // object type=0 : check if v1 trap |
|
1745 | + if ($type == 0) // object type=0 : check if v1 trap |
|
1746 | 1746 | { |
1747 | 1747 | // Check if next is suboid -> in that case is cannot be a trap |
1748 | - if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
|
1748 | + if (preg_match("/^$oid/", $this->objectsAll[$curElement + 1])) |
|
1749 | 1749 | { |
1750 | - $time_check2 += microtime(true) - $time_1; |
|
1750 | + $time_check2+=microtime(true) - $time_1; |
|
1751 | 1751 | $time_check2N++; |
1752 | 1752 | continue; |
1753 | 1753 | } |
1754 | 1754 | unset($snmptrans); |
1755 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1756 | - ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
1757 | - if ($retVal!=0) |
|
1755 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1756 | + ' -Td '.$oid.' | grep OBJECTS ', $snmptrans, $retVal); |
|
1757 | + if ($retVal != 0) |
|
1758 | 1758 | { |
1759 | - $time_check2 += microtime(true) - $time_1; |
|
1759 | + $time_check2+=microtime(true) - $time_1; |
|
1760 | 1760 | $time_check2N++; |
1761 | 1761 | continue; |
1762 | 1762 | } |
@@ -1764,56 +1764,56 @@ discard block |
||
1764 | 1764 | // Force as trap. |
1765 | 1765 | $type=21; |
1766 | 1766 | } |
1767 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
1767 | + if ($onlyTraps === true && $type != 21) // if only traps and not a trap, continue |
|
1768 | 1768 | { |
1769 | - $time_check3 += microtime(true) - $time_1; |
|
1769 | + $time_check3+=microtime(true) - $time_1; |
|
1770 | 1770 | $time_check3N++; |
1771 | 1771 | continue; |
1772 | 1772 | } |
1773 | 1773 | |
1774 | 1774 | $time_num_traps++; |
1775 | 1775 | |
1776 | - $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
|
1776 | + $this->logging->log('Found trap : '.$match[1].' / OID : '.$oid, INFO); |
|
1777 | 1777 | if ($display_progress) echo '#'; // echo a # when trap found |
1778 | 1778 | |
1779 | 1779 | // get trap objects & source MIB |
1780 | 1780 | unset($snmptrans); |
1781 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1782 | - ' -Td '.$oid,$snmptrans,$retVal); |
|
1783 | - if ($retVal!=0) |
|
1781 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
1782 | + ' -Td '.$oid, $snmptrans, $retVal); |
|
1783 | + if ($retVal != 0) |
|
1784 | 1784 | { |
1785 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
1785 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
1786 | 1786 | } |
1787 | 1787 | |
1788 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
1788 | + if (!preg_match('/^(.*)::/', $snmptrans[0], $match)) |
|
1789 | 1789 | { |
1790 | - $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
1790 | + $this->logging->log('Error getting mib from trap '.$oid.' : '.$snmptrans[0], 1, ''); |
|
1791 | 1791 | } |
1792 | 1792 | $trapMib=$match[1]; |
1793 | 1793 | |
1794 | - $numLine=1;$trapDesc=''; |
|
1795 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
1794 | + $numLine=1; $trapDesc=''; |
|
1795 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++; |
|
1796 | 1796 | if (isset($snmptrans[$numLine])) |
1797 | 1797 | { |
1798 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
1798 | + $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]); |
|
1799 | 1799 | |
1800 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
1800 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine])) |
|
1801 | 1801 | { |
1802 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
1802 | + $trapDesc.=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]); |
|
1803 | 1803 | $numLine++; |
1804 | 1804 | } |
1805 | 1805 | if (isset($snmptrans[$numLine])) { |
1806 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
1807 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
1806 | + $trapDesc.=preg_replace('/".*/', '', $snmptrans[$numLine]); |
|
1807 | + $trapDesc=preg_replace('/[\t ]+/', ' ', $trapDesc); |
|
1808 | 1808 | } |
1809 | 1809 | |
1810 | 1810 | } |
1811 | - $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
|
1812 | - $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
|
1811 | + $update=$this->update_oid($oid, $trapMib, $name, $type, NULL, NULL, NULL, NULL, $trapDesc); |
|
1812 | + $time_update+=microtime(true) - $time_1; $time_1=microtime(true); |
|
1813 | 1813 | |
1814 | - if (($update==0) && ($check_change===false)) |
|
1814 | + if (($update == 0) && ($check_change === false)) |
|
1815 | 1815 | { // Trapd didn't change & force check disabled |
1816 | - $time_objects += microtime(true) - $time_1; |
|
1816 | + $time_objects+=microtime(true) - $time_1; |
|
1817 | 1817 | if ($display_progress) echo "C"; |
1818 | 1818 | continue; |
1819 | 1819 | } |
@@ -1821,7 +1821,7 @@ discard block |
||
1821 | 1821 | $synt=null; |
1822 | 1822 | foreach ($snmptrans as $line) |
1823 | 1823 | { |
1824 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
1824 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match)) |
|
1825 | 1825 | { |
1826 | 1826 | $synt=$match[1]; |
1827 | 1827 | } |
@@ -1829,30 +1829,30 @@ discard block |
||
1829 | 1829 | if ($synt == null) |
1830 | 1830 | { |
1831 | 1831 | //echo "No objects for $trapOID\n"; |
1832 | - $time_objects += microtime(true) - $time_1; |
|
1832 | + $time_objects+=microtime(true) - $time_1; |
|
1833 | 1833 | continue; |
1834 | 1834 | } |
1835 | 1835 | //echo "$synt \n"; |
1836 | 1836 | $trapObjects=array(); |
1837 | - while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
1837 | + while (preg_match('/ *([^ ,]+) *,* */', $synt, $match)) |
|
1838 | 1838 | { |
1839 | - array_push($trapObjects,$match[1]); |
|
1840 | - $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
1839 | + array_push($trapObjects, $match[1]); |
|
1840 | + $synt=preg_replace('/'.$match[0].'/', '', $synt); |
|
1841 | 1841 | } |
1842 | 1842 | |
1843 | 1843 | $this->trap_objects($oid, $trapMib, $trapObjects, false); |
1844 | 1844 | |
1845 | - $time_objects += microtime(true) - $time_1; |
|
1845 | + $time_objects+=microtime(true) - $time_1; |
|
1846 | 1846 | $time_objectsN++; |
1847 | 1847 | } |
1848 | 1848 | |
1849 | 1849 | if ($display_progress) |
1850 | 1850 | { |
1851 | 1851 | echo "\nNumber of processed traps : $time_num_traps \n"; |
1852 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
1853 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
1854 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
1855 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
1852 | + echo "\nParsing : ".number_format($time_parse1 + $time_check1, 1)." sec / ".($time_parse1N + $time_check1N)." occurences\n"; |
|
1853 | + echo "Detecting traps : ".number_format($time_check2 + $time_check3, 1)." sec / ".($time_check2N + $time_check3N)." occurences\n"; |
|
1854 | + echo "Trap processing ($time_updateN): ".number_format($time_update, 1)." sec , "; |
|
1855 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects, 1)." sec \n"; |
|
1856 | 1856 | } |
1857 | 1857 | |
1858 | 1858 | // Timing ends |
@@ -65,9 +65,9 @@ discard block |
||
65 | 65 | { |
66 | 66 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
67 | 67 | $this->logSetup=true; |
68 | + } else { |
|
69 | + $this->logSetup=false; |
|
68 | 70 | } |
69 | - else |
|
70 | - $this->logSetup=false; |
|
71 | 71 | $this->logging->log('Loggin started', INFO); |
72 | 72 | |
73 | 73 | // Get options from ini files |
@@ -80,7 +80,10 @@ discard block |
||
80 | 80 | $this->setupDatabase($trapConfig); // Setup database class |
81 | 81 | |
82 | 82 | $this->getDatabaseOptions(); // Get options in database |
83 | - if ($this->api_use === true) $this->getAPI(); // Setup API |
|
83 | + if ($this->api_use === true) { |
|
84 | + $this->getAPI(); |
|
85 | + } |
|
86 | + // Setup API |
|
84 | 87 | |
85 | 88 | $this->trap_data=array( |
86 | 89 | 'source_ip' => 'unknown', |
@@ -112,8 +115,7 @@ discard block |
||
112 | 115 | } |
113 | 116 | $this->logging->log($message,$log_level,'syslog'); |
114 | 117 | return false; |
115 | - } |
|
116 | - else |
|
118 | + } else |
|
117 | 119 | { |
118 | 120 | $option_var=$option_array[$option_category][$option_name]; |
119 | 121 | return true; |
@@ -177,7 +179,10 @@ discard block |
||
177 | 179 | |
178 | 180 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName]); |
179 | 181 | |
180 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
182 | + if ($this->api_use === true) { |
|
183 | + return; |
|
184 | + } |
|
185 | + // In case of API use, no IDO is necessary |
|
181 | 186 | |
182 | 187 | // IDO Database |
183 | 188 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -202,9 +207,11 @@ discard block |
||
202 | 207 | protected function getDatabaseOptions() |
203 | 208 | { |
204 | 209 | // Database options |
205 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
210 | + if ($this->logSetup === false) { |
|
211 | + // Only if logging was no setup in constructor |
|
206 | 212 | { |
207 | 213 | $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
214 | + } |
|
208 | 215 | $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
209 | 216 | $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
210 | 217 | } |
@@ -213,7 +220,9 @@ discard block |
||
213 | 220 | protected function getDBConfigIfSet($element,&$variable) |
214 | 221 | { |
215 | 222 | $value=$this->getDBConfig($element); |
216 | - if ($value != 'null') $variable=$value; |
|
223 | + if ($value != 'null') { |
|
224 | + $variable=$value; |
|
225 | + } |
|
217 | 226 | } |
218 | 227 | |
219 | 228 | /** |
@@ -301,8 +310,7 @@ discard block |
||
301 | 310 | { |
302 | 311 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
303 | 312 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
304 | - } |
|
305 | - else |
|
313 | + } else |
|
306 | 314 | { |
307 | 315 | $this->trap_data['source_ip']=$matches[1]; |
308 | 316 | $this->trap_data['destination_ip']=$matches[3]; |
@@ -317,14 +325,12 @@ discard block |
||
317 | 325 | if ($ret_code===0 || $ret_code===false) |
318 | 326 | { |
319 | 327 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
320 | - } |
|
321 | - else |
|
328 | + } else |
|
322 | 329 | { |
323 | 330 | if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1')) |
324 | 331 | { |
325 | 332 | $this->trap_data['trap_oid']=$matches[2]; |
326 | - } |
|
327 | - else |
|
333 | + } else |
|
328 | 334 | { |
329 | 335 | $object= new stdClass; |
330 | 336 | $object->oid =$matches[1]; |
@@ -495,7 +501,9 @@ discard block |
||
495 | 501 | } |
496 | 502 | |
497 | 503 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
498 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
504 | + if ($inserted_id==false) { |
|
505 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
506 | + } |
|
499 | 507 | $this->trap_id=$inserted_id; |
500 | 508 | break; |
501 | 509 | default: |
@@ -511,7 +519,9 @@ discard block |
||
511 | 519 | { |
512 | 520 | |
513 | 521 | // If action is ignore -> don't send t DB |
514 | - if ($this->trap_to_db === false) return; |
|
522 | + if ($this->trap_to_db === false) { |
|
523 | + return; |
|
524 | + } |
|
515 | 525 | |
516 | 526 | |
517 | 527 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -568,7 +578,9 @@ discard block |
||
568 | 578 | } |
569 | 579 | |
570 | 580 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
571 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
581 | + if ($inserted_id==false) { |
|
582 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
583 | + } |
|
572 | 584 | $this->trap_id=$inserted_id; |
573 | 585 | break; |
574 | 586 | default: |
@@ -697,8 +709,7 @@ discard block |
||
697 | 709 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
698 | 710 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
699 | 711 | return true; |
700 | - } |
|
701 | - else |
|
712 | + } else |
|
702 | 713 | { |
703 | 714 | $api = $this->getAPI(); |
704 | 715 | $api->setCredentials($this->api_username, $this->api_password); |
@@ -707,8 +718,7 @@ discard block |
||
707 | 718 | { |
708 | 719 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
709 | 720 | return false; |
710 | - } |
|
711 | - else |
|
721 | + } else |
|
712 | 722 | { |
713 | 723 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
714 | 724 | return true; |
@@ -768,7 +778,9 @@ discard block |
||
768 | 778 | /***************** Eval & tokenizer functions ****************/ |
769 | 779 | protected function eval_getElement($rule,&$item) |
770 | 780 | { |
771 | - while ($rule[$item]==' ') $item++; |
|
781 | + while ($rule[$item]==' ') { |
|
782 | + $item++; |
|
783 | + } |
|
772 | 784 | if (preg_match('/[0-9\.]/',$rule[$item])) |
773 | 785 | { // number |
774 | 786 | |
@@ -795,14 +807,15 @@ discard block |
||
795 | 807 | $start=$item; |
796 | 808 | $parenthesis_count=0; |
797 | 809 | while (($item < strlen($rule)) // Not end of string AND |
798 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
810 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) { |
|
811 | + // Closing ')' or embeded () |
|
799 | 812 | { |
800 | 813 | if ($rule[$item] == '"' ) |
801 | 814 | { // pass through string |
802 | 815 | $item++; |
816 | + } |
|
803 | 817 | $item=$this->eval_getNext($rule,$item,'"'); |
804 | - } |
|
805 | - else{ |
|
818 | + } else{ |
|
806 | 819 | if ($rule[$item] == '(') |
807 | 820 | { |
808 | 821 | $parenthesis_count++; |
@@ -830,13 +843,17 @@ discard block |
||
830 | 843 | protected function eval_getNext($rule,$item,$tok) |
831 | 844 | { |
832 | 845 | while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;} |
833 | - if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
846 | + if ($item==strlen($rule)) { |
|
847 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
848 | + } |
|
834 | 849 | return $item+1; |
835 | 850 | } |
836 | 851 | |
837 | 852 | protected function eval_getOper($rule,&$item) |
838 | 853 | { |
839 | - while ($rule[$item]==' ') $item++; |
|
854 | + while ($rule[$item]==' ') { |
|
855 | + $item++; |
|
856 | + } |
|
840 | 857 | switch ($rule[$item]) |
841 | 858 | { |
842 | 859 | case '<': |
@@ -872,12 +889,13 @@ discard block |
||
872 | 889 | public function evaluation($rule,&$item) |
873 | 890 | { |
874 | 891 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
875 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
892 | + if ( $rule[$item] == '!') { |
|
893 | + // If '!' found, negate next expression. |
|
876 | 894 | { |
877 | 895 | $negate=true; |
878 | - $item++; |
|
879 | 896 | } |
880 | - else |
|
897 | + $item++; |
|
898 | + } else |
|
881 | 899 | { |
882 | 900 | $negate=false; |
883 | 901 | } |
@@ -885,10 +903,14 @@ discard block |
||
885 | 903 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
886 | 904 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
887 | 905 | |
888 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
906 | + if ($item==strlen($rule)) { |
|
907 | + // If only element, return value, but only boolean |
|
889 | 908 | { |
890 | 909 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
891 | - if ($negate === true) $val1= ! $val1; |
|
910 | + } |
|
911 | + if ($negate === true) { |
|
912 | + $val1= ! $val1; |
|
913 | + } |
|
892 | 914 | return $val1; |
893 | 915 | } |
894 | 916 | |
@@ -897,27 +919,34 @@ discard block |
||
897 | 919 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
898 | 920 | |
899 | 921 | // Third element : number, string or () |
900 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
922 | + if ( $rule[$item] == '!') { |
|
923 | + // starts with a ! so evaluate whats next |
|
901 | 924 | { |
902 | 925 | $item++; |
903 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
926 | + } |
|
927 | + if ($typec != 1) { |
|
928 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
929 | + } |
|
904 | 930 | $val2= ! $this->evaluation($rule,$item); |
905 | 931 | $type2=2; // result is a boolean |
906 | - } |
|
907 | - else |
|
932 | + } else |
|
908 | 933 | { |
909 | 934 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
910 | 935 | } |
911 | 936 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
912 | 937 | |
913 | - if ($type1!=$type2) // cannot compare different types |
|
938 | + if ($type1!=$type2) { |
|
939 | + // cannot compare different types |
|
914 | 940 | { |
915 | 941 | throw new Exception("Cannot compare string & number : ".$rule); |
916 | 942 | } |
917 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
943 | + } |
|
944 | + if ($typec==1 && $type1 !=2) { |
|
945 | + // cannot use & or | with string/number |
|
918 | 946 | { |
919 | 947 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
920 | 948 | } |
949 | + } |
|
921 | 950 | |
922 | 951 | switch ($comp){ |
923 | 952 | case '<': $retVal= ($val1 < $val2); break; |
@@ -931,9 +960,15 @@ discard block |
||
931 | 960 | case '&': $retVal= ($val1 && $val2); break; |
932 | 961 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
933 | 962 | } |
934 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
963 | + if ($negate === true) { |
|
964 | + $retVal = ! $retVal; |
|
965 | + } |
|
966 | + // Inverse result if negate before expression |
|
935 | 967 | |
936 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
968 | + if ($item==strlen($rule)) { |
|
969 | + return $retVal; |
|
970 | + } |
|
971 | + // End of string : return evaluation |
|
937 | 972 | // check for logical operator : |
938 | 973 | switch ($rule[$item]) |
939 | 974 | { |
@@ -960,7 +995,9 @@ discard block |
||
960 | 995 | $rule2.=$rule[$item]; |
961 | 996 | $item++; |
962 | 997 | } |
963 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
998 | + if ($item == strlen ($rule)) { |
|
999 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
1000 | + } |
|
964 | 1001 | $rule2.=$rule[$item]; |
965 | 1002 | $item++; |
966 | 1003 | continue; |
@@ -980,10 +1017,12 @@ discard block |
||
980 | 1017 | |
981 | 1018 | protected function eval_rule($rule) |
982 | 1019 | { |
983 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
1020 | + if ($rule==null || $rule == '') { |
|
1021 | + // Empty rule is always true |
|
984 | 1022 | { |
985 | 1023 | return true; |
986 | 1024 | } |
1025 | + } |
|
987 | 1026 | $matches=array(); |
988 | 1027 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
989 | 1028 | { |
@@ -1070,20 +1109,17 @@ discard block |
||
1070 | 1109 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
1071 | 1110 | { |
1072 | 1111 | $this->trap_action.='Error sending status : check cmd/API'; |
1073 | - } |
|
1074 | - else |
|
1112 | + } else |
|
1075 | 1113 | { |
1076 | 1114 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
1077 | 1115 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
1078 | 1116 | } |
1079 | - } |
|
1080 | - else |
|
1117 | + } else |
|
1081 | 1118 | { |
1082 | 1119 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
1083 | 1120 | } |
1084 | 1121 | $this->trap_to_db=($action==-2)?false:true; |
1085 | - } |
|
1086 | - else |
|
1122 | + } else |
|
1087 | 1123 | { |
1088 | 1124 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
1089 | 1125 | |
@@ -1094,14 +1130,12 @@ discard block |
||
1094 | 1130 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
1095 | 1131 | { |
1096 | 1132 | $this->trap_action.='Error sending status : check cmd/API'; |
1097 | - } |
|
1098 | - else |
|
1133 | + } else |
|
1099 | 1134 | { |
1100 | 1135 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
1101 | 1136 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
1102 | 1137 | } |
1103 | - } |
|
1104 | - else |
|
1138 | + } else |
|
1105 | 1139 | { |
1106 | 1140 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
1107 | 1141 | } |
@@ -1111,16 +1145,14 @@ discard block |
||
1111 | 1145 | if (!isset($this->trap_data['source_name'])) |
1112 | 1146 | { |
1113 | 1147 | $this->trap_data['source_name']=$rule['host_name']; |
1114 | - } |
|
1115 | - else |
|
1148 | + } else |
|
1116 | 1149 | { |
1117 | 1150 | if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
1118 | 1151 | { // only add if not present |
1119 | 1152 | $this->trap_data['source_name'].=','.$rule['host_name']; |
1120 | 1153 | } |
1121 | 1154 | } |
1122 | - } |
|
1123 | - catch (Exception $e) |
|
1155 | + } catch (Exception $e) |
|
1124 | 1156 | { |
1125 | 1157 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
1126 | 1158 | $this->trap_action.=' ERR : '.$e->getMessage(); |
@@ -1131,8 +1163,7 @@ discard block |
||
1131 | 1163 | if ($this->trap_data['status']=='error') |
1132 | 1164 | { |
1133 | 1165 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
1134 | - } |
|
1135 | - else |
|
1166 | + } else |
|
1136 | 1167 | { |
1137 | 1168 | $this->trap_data['status']='done'; |
1138 | 1169 | } |
@@ -1187,8 +1218,7 @@ discard block |
||
1187 | 1218 | if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
1188 | 1219 | { |
1189 | 1220 | $cur_table='table '.$cur_table_array[1]; |
1190 | - } |
|
1191 | - else |
|
1221 | + } else |
|
1192 | 1222 | { |
1193 | 1223 | $cur_table='secret SQL stuff :-)'; |
1194 | 1224 | } |
@@ -1232,8 +1262,7 @@ discard block |
||
1232 | 1262 | if ($this->trapsDB->trapDBType == 'pgsql') |
1233 | 1263 | { |
1234 | 1264 | $prefix .= 'update_pgsql/schema_'; |
1235 | - } |
|
1236 | - else |
|
1265 | + } else |
|
1237 | 1266 | { |
1238 | 1267 | $prefix .= 'update_sql/schema_'; |
1239 | 1268 | } |
@@ -1279,7 +1308,10 @@ discard block |
||
1279 | 1308 | $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
1280 | 1309 | while (($line=fgets($input_stream)) !== false) |
1281 | 1310 | { |
1282 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
1311 | + if (preg_match('/^#/', $line)) { |
|
1312 | + continue; |
|
1313 | + } |
|
1314 | + // ignore comment lines |
|
1283 | 1315 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
1284 | 1316 | if (preg_match('/; *$/', $newline)) |
1285 | 1317 | { |
@@ -1291,8 +1323,7 @@ discard block |
||
1291 | 1323 | if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
1292 | 1324 | { |
1293 | 1325 | $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
1294 | - } |
|
1295 | - else |
|
1326 | + } else |
|
1296 | 1327 | { |
1297 | 1328 | $cur_table='secret SQL stuff :-)'; |
1298 | 1329 | //$cur_table=$newline; |
@@ -1432,8 +1463,7 @@ discard block |
||
1432 | 1463 | } |
1433 | 1464 | $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
1434 | 1465 | return 1; |
1435 | - } |
|
1436 | - else |
|
1466 | + } else |
|
1437 | 1467 | { |
1438 | 1468 | $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
1439 | 1469 | return 0; |
@@ -1449,7 +1479,9 @@ discard block |
||
1449 | 1479 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
1450 | 1480 | ', :syntax, :type_enum, :description )'; |
1451 | 1481 | |
1452 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
1482 | + if ($this->trapsDB->trapDBType == 'pgsql') { |
|
1483 | + $sql .= 'RETURNING id'; |
|
1484 | + } |
|
1453 | 1485 | |
1454 | 1486 | $sqlQuery=$db_conn->prepare($sql); |
1455 | 1487 | |
@@ -1489,7 +1521,9 @@ discard block |
||
1489 | 1521 | } |
1490 | 1522 | |
1491 | 1523 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
1492 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
1524 | + if ($inserted_id==false) { |
|
1525 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
1526 | + } |
|
1493 | 1527 | $this->dbOidIndex[$oid]['id']=$inserted_id; |
1494 | 1528 | break; |
1495 | 1529 | default: |
@@ -1742,12 +1776,14 @@ discard block |
||
1742 | 1776 | $name=$match[1]; // Name |
1743 | 1777 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
1744 | 1778 | |
1745 | - if ($type==0) // object type=0 : check if v1 trap |
|
1779 | + if ($type==0) { |
|
1780 | + // object type=0 : check if v1 trap |
|
1746 | 1781 | { |
1747 | 1782 | // Check if next is suboid -> in that case is cannot be a trap |
1748 | 1783 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
1749 | 1784 | { |
1750 | 1785 | $time_check2 += microtime(true) - $time_1; |
1786 | + } |
|
1751 | 1787 | $time_check2N++; |
1752 | 1788 | continue; |
1753 | 1789 | } |
@@ -1764,9 +1800,11 @@ discard block |
||
1764 | 1800 | // Force as trap. |
1765 | 1801 | $type=21; |
1766 | 1802 | } |
1767 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
1803 | + if ($onlyTraps===true && $type!=21) { |
|
1804 | + // if only traps and not a trap, continue |
|
1768 | 1805 | { |
1769 | 1806 | $time_check3 += microtime(true) - $time_1; |
1807 | + } |
|
1770 | 1808 | $time_check3N++; |
1771 | 1809 | continue; |
1772 | 1810 | } |
@@ -1774,7 +1812,10 @@ discard block |
||
1774 | 1812 | $time_num_traps++; |
1775 | 1813 | |
1776 | 1814 | $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
1777 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
1815 | + if ($display_progress) { |
|
1816 | + echo '#'; |
|
1817 | + } |
|
1818 | + // echo a # when trap found |
|
1778 | 1819 | |
1779 | 1820 | // get trap objects & source MIB |
1780 | 1821 | unset($snmptrans); |
@@ -1792,7 +1833,9 @@ discard block |
||
1792 | 1833 | $trapMib=$match[1]; |
1793 | 1834 | |
1794 | 1835 | $numLine=1;$trapDesc=''; |
1795 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
1836 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) { |
|
1837 | + $numLine++; |
|
1838 | + } |
|
1796 | 1839 | if (isset($snmptrans[$numLine])) |
1797 | 1840 | { |
1798 | 1841 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
@@ -1814,7 +1857,9 @@ discard block |
||
1814 | 1857 | if (($update==0) && ($check_change===false)) |
1815 | 1858 | { // Trapd didn't change & force check disabled |
1816 | 1859 | $time_objects += microtime(true) - $time_1; |
1817 | - if ($display_progress) echo "C"; |
|
1860 | + if ($display_progress) { |
|
1861 | + echo "C"; |
|
1862 | + } |
|
1818 | 1863 | continue; |
1819 | 1864 | } |
1820 | 1865 |
@@ -32,8 +32,8 @@ discard block |
||
32 | 32 | protected $trapClass; //< Trap class for bin/trap_class.php |
33 | 33 | |
34 | 34 | /** Get instance of TrapModuleConfig class |
35 | - * @return TrapModuleConfig |
|
36 | - */ |
|
35 | + * @return TrapModuleConfig |
|
36 | + */ |
|
37 | 37 | public function getModuleConfig() |
38 | 38 | { |
39 | 39 | if ($this->moduleConfig == Null) |
@@ -58,12 +58,12 @@ discard block |
||
58 | 58 | |
59 | 59 | public function getTrapHostListTable() |
60 | 60 | { |
61 | - if ($this->trapTableHostList == Null) |
|
61 | + if ($this->trapTableHostList == Null) |
|
62 | 62 | { |
63 | - $this->trapTableHostList = new TrapTableHostList(); |
|
64 | - $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
65 | - } |
|
66 | - return $this->trapTableHostList; |
|
63 | + $this->trapTableHostList = new TrapTableHostList(); |
|
64 | + $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
65 | + } |
|
66 | + return $this->trapTableHostList; |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | public function getHandlerListTable() |
@@ -77,11 +77,11 @@ discard block |
||
77 | 77 | } |
78 | 78 | |
79 | 79 | /** Get Database connexion |
80 | - * @param $DBname string DB name in resource.ini_ge |
|
81 | - * @param $test bool if set to true, returns error code and not database |
|
82 | - * @param $test_version bool if set to flase, does not test database version of trapDB |
|
83 | - * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
84 | - */ |
|
80 | + * @param $DBname string DB name in resource.ini_ge |
|
81 | + * @param $test bool if set to true, returns error code and not database |
|
82 | + * @param $test_version bool if set to flase, does not test database version of trapDB |
|
83 | + * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
84 | + */ |
|
85 | 85 | public function getDbByName($DBname,$test=false,$test_version=true) |
86 | 86 | { |
87 | 87 | try |
@@ -166,20 +166,20 @@ discard block |
||
166 | 166 | |
167 | 167 | if ( ! $dbresource ) |
168 | 168 | { |
169 | - if ($test) return array(1,'No database in config.ini'); |
|
170 | - $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
171 | - return null; |
|
169 | + if ($test) return array(1,'No database in config.ini'); |
|
170 | + $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
171 | + return null; |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | try |
175 | 175 | { |
176 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
176 | + $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
177 | 177 | } |
178 | 178 | catch (Exception $e) |
179 | 179 | { |
180 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
181 | - $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
182 | - return null; |
|
180 | + if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
181 | + $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
182 | + return null; |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | if ($test == false) |
@@ -190,13 +190,13 @@ discard block |
||
190 | 190 | |
191 | 191 | try |
192 | 192 | { |
193 | - $query = $dbconn->select() |
|
194 | - ->from('icinga_dbversion',array('version')); |
|
195 | - $version=$dbconn->fetchRow($query); |
|
196 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
197 | - { |
|
198 | - return array(4,"$dbresource does not look like an IDO database"); |
|
199 | - } |
|
193 | + $query = $dbconn->select() |
|
194 | + ->from('icinga_dbversion',array('version')); |
|
195 | + $version=$dbconn->fetchRow($query); |
|
196 | + if ( ($version == null) || ! property_exists($version,'version') ) |
|
197 | + { |
|
198 | + return array(4,"$dbresource does not look like an IDO database"); |
|
199 | + } |
|
200 | 200 | } |
201 | 201 | catch (Exception $e) |
202 | 202 | { |
@@ -206,15 +206,15 @@ discard block |
||
206 | 206 | return array(0,''); |
207 | 207 | } |
208 | 208 | |
209 | - protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
210 | - { |
|
211 | - $limit = $this->params->get('limit', $limit); |
|
212 | - $page = $this->params->get('page', $offset); |
|
209 | + protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
210 | + { |
|
211 | + $limit = $this->params->get('limit', $limit); |
|
212 | + $page = $this->params->get('page', $offset); |
|
213 | 213 | |
214 | - $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
214 | + $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
215 | 215 | |
216 | - return $paginatable; |
|
217 | - } |
|
216 | + return $paginatable; |
|
217 | + } |
|
218 | 218 | |
219 | 219 | public function displayExitError($source,$message) |
220 | 220 | { // TODO : check better ways to transmit data (with POST ?) |
@@ -223,33 +223,33 @@ discard block |
||
223 | 223 | |
224 | 224 | protected function checkReadPermission() |
225 | 225 | { |
226 | - if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
227 | - $this->displayExitError('Permissions','No permission fo view content'); |
|
228 | - } |
|
226 | + if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
227 | + $this->displayExitError('Permissions','No permission fo view content'); |
|
228 | + } |
|
229 | 229 | } |
230 | 230 | |
231 | 231 | protected function checkConfigPermission() |
232 | 232 | { |
233 | - if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
234 | - $this->displayExitError('Permissions','No permission fo configure'); |
|
235 | - } |
|
233 | + if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
234 | + $this->displayExitError('Permissions','No permission fo configure'); |
|
235 | + } |
|
236 | 236 | } |
237 | 237 | |
238 | - /** |
|
239 | - * Check if user has write permission |
|
240 | - * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
241 | - * @return boolean : user has permission |
|
242 | - */ |
|
238 | + /** |
|
239 | + * Check if user has write permission |
|
240 | + * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
241 | + * @return boolean : user has permission |
|
242 | + */ |
|
243 | 243 | protected function checkModuleConfigPermission($check=0) |
244 | 244 | { |
245 | - if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
246 | - if ($check == 0) |
|
247 | - { |
|
248 | - $this->displayExitError('Permissions','No permission fo configure module'); |
|
249 | - } |
|
250 | - return false; |
|
251 | - } |
|
252 | - return true; |
|
245 | + if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
246 | + if ($check == 0) |
|
247 | + { |
|
248 | + $this->displayExitError('Permissions','No permission fo configure module'); |
|
249 | + } |
|
250 | + return false; |
|
251 | + } |
|
252 | + return true; |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | /************************* Trap class get **********************/ |
@@ -269,8 +269,8 @@ discard block |
||
269 | 269 | /************************** MIB related **************************/ |
270 | 270 | |
271 | 271 | /** Get MIBLoader class |
272 | - * @return MIBLoader class |
|
273 | - */ |
|
272 | + * @return MIBLoader class |
|
273 | + */ |
|
274 | 274 | protected function getMIB() |
275 | 275 | { |
276 | 276 | if ($this->MIBData == null) |
@@ -288,9 +288,9 @@ discard block |
||
288 | 288 | /************************** Database queries *******************/ |
289 | 289 | |
290 | 290 | /** Get host(s) by IP (v4 or v6) or by name in IDO database |
291 | - * does not catch exceptions |
|
292 | - * @return array of objects ( name, id (object_id), display_name) |
|
293 | - */ |
|
291 | + * does not catch exceptions |
|
292 | + * @return array of objects ( name, id (object_id), display_name) |
|
293 | + */ |
|
294 | 294 | protected function getHostByIP($ip) |
295 | 295 | { |
296 | 296 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -309,9 +309,9 @@ discard block |
||
309 | 309 | } |
310 | 310 | |
311 | 311 | /** Get host(s) by name in IDO database |
312 | - * does not catch exceptions |
|
313 | - * @return array of objects ( name, id (object_id), display_name) |
|
314 | - */ |
|
312 | + * does not catch exceptions |
|
313 | + * @return array of objects ( name, id (object_id), display_name) |
|
314 | + */ |
|
315 | 315 | protected function getHostByName($name) |
316 | 316 | { |
317 | 317 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -330,9 +330,9 @@ discard block |
||
330 | 330 | } |
331 | 331 | |
332 | 332 | /** Get host groups by name in IDO database |
333 | - * does not catch exceptions |
|
334 | - * @return array of objects ( name, id (object_id), display_name) |
|
335 | - */ |
|
333 | + * does not catch exceptions |
|
334 | + * @return array of objects ( name, id (object_id), display_name) |
|
335 | + */ |
|
336 | 336 | protected function getHostGroupByName($ip) |
337 | 337 | { |
338 | 338 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -352,9 +352,9 @@ discard block |
||
352 | 352 | |
353 | 353 | |
354 | 354 | /** Get host IP (v4 and v6) by name in IDO database |
355 | - * does not catch exceptions |
|
356 | - * @return array ( name, display_name, ip4, ip6) |
|
357 | - */ |
|
355 | + * does not catch exceptions |
|
356 | + * @return array ( name, display_name, ip4, ip6) |
|
357 | + */ |
|
358 | 358 | protected function getHostInfoByID($id) |
359 | 359 | { |
360 | 360 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -373,9 +373,9 @@ discard block |
||
373 | 373 | |
374 | 374 | |
375 | 375 | /** Get host by objectid in IDO database |
376 | - * does not catch exceptions |
|
377 | - * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
378 | - */ |
|
376 | + * does not catch exceptions |
|
377 | + * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
378 | + */ |
|
379 | 379 | protected function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
380 | 380 | { |
381 | 381 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -393,10 +393,10 @@ discard block |
||
393 | 393 | } |
394 | 394 | |
395 | 395 | /** Get services from object ( host_object_id) in IDO database |
396 | - * does not catch exceptions |
|
397 | - * @param $id int object_id |
|
398 | - * @return array display_name (of service), service_object_id |
|
399 | - */ |
|
396 | + * does not catch exceptions |
|
397 | + * @param $id int object_id |
|
398 | + * @return array display_name (of service), service_object_id |
|
399 | + */ |
|
400 | 400 | protected function getServicesByHostid($id) |
401 | 401 | { |
402 | 402 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -416,11 +416,11 @@ discard block |
||
416 | 416 | } |
417 | 417 | |
418 | 418 | /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
419 | - * gets all hosts in hostgroup and return common services |
|
420 | - * does not catch exceptions |
|
421 | - * @param $id int object_id |
|
422 | - * @return array display_name (of service), service_object_id |
|
423 | - */ |
|
419 | + * gets all hosts in hostgroup and return common services |
|
420 | + * does not catch exceptions |
|
421 | + * @param $id int object_id |
|
422 | + * @return array display_name (of service), service_object_id |
|
423 | + */ |
|
424 | 424 | protected function getServicesByHostGroupid($id) |
425 | 425 | { |
426 | 426 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | //print_r($common_services); |
460 | 460 | foreach (array_keys($common_services) as $key) |
461 | 461 | { |
462 | - if ($common_services[$key]['num'] == $num_hosts) |
|
462 | + if ($common_services[$key]['num'] == $num_hosts) |
|
463 | 463 | { |
464 | 464 | array_push($result,array($key,$common_services[$key]['name'])); |
465 | 465 | } |
@@ -469,11 +469,11 @@ discard block |
||
469 | 469 | } |
470 | 470 | |
471 | 471 | /** Get services object id by host name / service name in IDO database |
472 | - * does not catch exceptions |
|
473 | - * @param $hostname string host name |
|
474 | - * @param $name string service name |
|
475 | - * @return array service id |
|
476 | - */ |
|
472 | + * does not catch exceptions |
|
473 | + * @param $hostname string host name |
|
474 | + * @param $name string service name |
|
475 | + * @return array service id |
|
476 | + */ |
|
477 | 477 | protected function getServiceIDByName($hostname,$name) |
478 | 478 | { |
479 | 479 | $db = $this->getIdoDb()->getConnection(); |
@@ -496,10 +496,10 @@ discard block |
||
496 | 496 | } |
497 | 497 | |
498 | 498 | /** Get object name from object_id in IDO database |
499 | - * does not catch exceptions |
|
500 | - * @param int $id object_id (default to null, used first if not null) |
|
501 | - * @return array name1 (host) name2 (service) |
|
502 | - */ |
|
499 | + * does not catch exceptions |
|
500 | + * @param int $id object_id (default to null, used first if not null) |
|
501 | + * @return array name1 (host) name2 (service) |
|
502 | + */ |
|
503 | 503 | protected function getObjectNameByid($id) |
504 | 504 | { |
505 | 505 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -515,9 +515,9 @@ discard block |
||
515 | 515 | } |
516 | 516 | |
517 | 517 | /** Add handler rule in traps DB |
518 | - * @param array $params : array(<db item>=><value>) |
|
519 | - * @return int inserted id |
|
520 | - */ |
|
518 | + * @param array $params : array(<db item>=><value>) |
|
519 | + * @return int inserted id |
|
520 | + */ |
|
521 | 521 | protected function addHandlerRule($params) |
522 | 522 | { |
523 | 523 | // TODO Check for rule consistency |
@@ -539,10 +539,10 @@ discard block |
||
539 | 539 | } |
540 | 540 | |
541 | 541 | /** Update handler rule in traps DB |
542 | - * @param array $params : (<db item>=><value>) |
|
543 | - * @param integer $ruleID : rule id in db |
|
544 | - * @return array affected rows |
|
545 | - */ |
|
542 | + * @param array $params : (<db item>=><value>) |
|
543 | + * @param integer $ruleID : rule id in db |
|
544 | + * @return array affected rows |
|
545 | + */ |
|
546 | 546 | protected function updateHandlerRule($params,$ruleID) |
547 | 547 | { |
548 | 548 | // TODO Check for rule consistency |
@@ -560,8 +560,8 @@ discard block |
||
560 | 560 | } |
561 | 561 | |
562 | 562 | /** Delete rule by id |
563 | - * @param int $ruleID rule id |
|
564 | - */ |
|
563 | + * @param int $ruleID rule id |
|
564 | + */ |
|
565 | 565 | protected function deleteRule($ruleID) |
566 | 566 | { |
567 | 567 | if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
@@ -575,9 +575,9 @@ discard block |
||
575 | 575 | } |
576 | 576 | |
577 | 577 | /** Delete trap by ip & oid |
578 | - * @param $ip string source IP (v4 or v6) |
|
579 | - * @param $oid string oid |
|
580 | - */ |
|
578 | + * @param $ip string source IP (v4 or v6) |
|
579 | + * @param $oid string oid |
|
580 | + */ |
|
581 | 581 | protected function deleteTrap($ip,$oid) |
582 | 582 | { |
583 | 583 | |
@@ -603,9 +603,9 @@ discard block |
||
603 | 603 | |
604 | 604 | |
605 | 605 | /** count trap by ip & oid |
606 | - * @param $ip string source IP (v4 or v6) |
|
607 | - * @param $oid string oid |
|
608 | - */ |
|
606 | + * @param $ip string source IP (v4 or v6) |
|
607 | + * @param $oid string oid |
|
608 | + */ |
|
609 | 609 | protected function countTrap($ip,$oid) |
610 | 610 | { |
611 | 611 | |
@@ -631,8 +631,8 @@ discard block |
||
631 | 631 | } |
632 | 632 | |
633 | 633 | /** get configuration value |
634 | - * @param string $element : configuration name in db |
|
635 | - */ |
|
634 | + * @param string $element : configuration name in db |
|
635 | + */ |
|
636 | 636 | protected function getDBConfigValue($element) |
637 | 637 | { |
638 | 638 | |
@@ -663,9 +663,9 @@ discard block |
||
663 | 663 | } |
664 | 664 | |
665 | 665 | /** add configuration value |
666 | - * @param string $element : name of config element |
|
667 | - * @param string $value : value |
|
668 | - */ |
|
666 | + * @param string $element : name of config element |
|
667 | + * @param string $value : value |
|
668 | + */ |
|
669 | 669 | |
670 | 670 | protected function addDBConfigValue($element,$value) |
671 | 671 | { |
@@ -683,9 +683,9 @@ discard block |
||
683 | 683 | } |
684 | 684 | |
685 | 685 | /** set configuration value |
686 | - * @param string $element : name of config element |
|
687 | - * @param string $value : value |
|
688 | - */ |
|
686 | + * @param string $element : name of config element |
|
687 | + * @param string $value : value |
|
688 | + */ |
|
689 | 689 | protected function setDBConfigValue($element,$value) |
690 | 690 | { |
691 | 691 | |
@@ -699,13 +699,13 @@ discard block |
||
699 | 699 | } |
700 | 700 | |
701 | 701 | /** Check if director is installed |
702 | - * @return bool true/false |
|
703 | - */ |
|
702 | + * @return bool true/false |
|
703 | + */ |
|
704 | 704 | protected function isDirectorInstalled() |
705 | 705 | { |
706 | - $output=array(); |
|
707 | - exec('icingacli module list',$output); |
|
708 | - foreach ($output as $line) |
|
706 | + $output=array(); |
|
707 | + exec('icingacli module list',$output); |
|
708 | + foreach ($output as $line) |
|
709 | 709 | { |
710 | 710 | if (preg_match('/^director .*enabled/',$line)) |
711 | 711 | { |
@@ -22,14 +22,14 @@ discard block |
||
22 | 22 | |
23 | 23 | class TrapsController extends Controller |
24 | 24 | { |
25 | - protected $moduleConfig; //< TrapModuleConfig instance |
|
26 | - protected $trapTableList; //< TrapTableList (by date) |
|
27 | - protected $trapTableHostList; //< TrapTableList (by hosts) |
|
28 | - protected $handlerTableList; //< HandlerTableList instance |
|
29 | - protected $trapDB; //< Trap database |
|
30 | - protected $icingaDB; //< Icinga IDO database; |
|
31 | - protected $MIBData; //< MIBLoader class |
|
32 | - protected $trapClass; //< Trap class for bin/trap_class.php |
|
25 | + protected $moduleConfig; //< TrapModuleConfig instance |
|
26 | + protected $trapTableList; //< TrapTableList (by date) |
|
27 | + protected $trapTableHostList; //< TrapTableList (by hosts) |
|
28 | + protected $handlerTableList; //< HandlerTableList instance |
|
29 | + protected $trapDB; //< Trap database |
|
30 | + protected $icingaDB; //< Icinga IDO database; |
|
31 | + protected $MIBData; //< MIBLoader class |
|
32 | + protected $trapClass; //< Trap class for bin/trap_class.php |
|
33 | 33 | |
34 | 34 | /** Get instance of TrapModuleConfig class |
35 | 35 | * @return TrapModuleConfig |
@@ -43,14 +43,14 @@ discard block |
||
43 | 43 | { |
44 | 44 | $this->redirectNow('trapdirector/settings?message=No database prefix'); |
45 | 45 | } |
46 | - $this->moduleConfig = new TrapModuleConfig($db_prefix); |
|
46 | + $this->moduleConfig=new TrapModuleConfig($db_prefix); |
|
47 | 47 | } |
48 | 48 | return $this->moduleConfig; |
49 | 49 | } |
50 | 50 | |
51 | 51 | public function getTrapListTable() { |
52 | 52 | if ($this->trapTableList == Null) { |
53 | - $this->trapTableList = new TrapTableList(); |
|
53 | + $this->trapTableList=new TrapTableList(); |
|
54 | 54 | $this->trapTableList->setConfig($this->getModuleConfig()); |
55 | 55 | } |
56 | 56 | return $this->trapTableList; |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | { |
61 | 61 | if ($this->trapTableHostList == Null) |
62 | 62 | { |
63 | - $this->trapTableHostList = new TrapTableHostList(); |
|
63 | + $this->trapTableHostList=new TrapTableHostList(); |
|
64 | 64 | $this->trapTableHostList->setConfig($this->getModuleConfig()); |
65 | 65 | } |
66 | 66 | return $this->trapTableHostList; |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | { |
71 | 71 | if ($this->handlerTableList == Null) |
72 | 72 | { |
73 | - $this->handlerTableList = new HandlerTableList(); |
|
73 | + $this->handlerTableList=new HandlerTableList(); |
|
74 | 74 | $this->handlerTableList->setConfig($this->getModuleConfig()); |
75 | 75 | } |
76 | 76 | return $this->handlerTableList; |
@@ -82,15 +82,15 @@ discard block |
||
82 | 82 | * @param $test_version bool if set to flase, does not test database version of trapDB |
83 | 83 | * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
84 | 84 | */ |
85 | - public function getDbByName($DBname,$test=false,$test_version=true) |
|
85 | + public function getDbByName($DBname, $test=false, $test_version=true) |
|
86 | 86 | { |
87 | 87 | try |
88 | 88 | { |
89 | - $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
89 | + $dbconn=IcingaDbConnection::fromResourceName($DBname); |
|
90 | 90 | } |
91 | 91 | catch (Exception $e) |
92 | 92 | { |
93 | - if ($test) return array(2,$DBname); |
|
93 | + if ($test) return array(2, $DBname); |
|
94 | 94 | $this->redirectNow('trapdirector/settings?dberror=2'); |
95 | 95 | return null; |
96 | 96 | } |
@@ -101,37 +101,37 @@ discard block |
||
101 | 101 | } |
102 | 102 | catch (Exception $e) |
103 | 103 | { |
104 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
104 | + if ($test) return array(3, $DBname, $e->getMessage()); |
|
105 | 105 | $this->redirectNow('trapdirector/settings?dberror=3'); |
106 | 106 | return null; |
107 | 107 | } |
108 | 108 | try |
109 | 109 | { |
110 | - $query = $db->select() |
|
111 | - ->from($this->getModuleConfig()->getDbConfigTableName(),'value') |
|
110 | + $query=$db->select() |
|
111 | + ->from($this->getModuleConfig()->getDbConfigTableName(), 'value') |
|
112 | 112 | ->where('name=\'db_version\''); |
113 | 113 | $version=$db->fetchRow($query); |
114 | - if ( ($version == null) || ! property_exists($version,'value') ) |
|
114 | + if (($version == null) || !property_exists($version, 'value')) |
|
115 | 115 | { |
116 | - if ($test) return array(4,$DBname); |
|
116 | + if ($test) return array(4, $DBname); |
|
117 | 117 | $this->redirectNow('trapdirector/settings?dberror=4'); |
118 | 118 | return null; |
119 | 119 | } |
120 | 120 | if ($version->value < $this->getModuleConfig()->getDbMinVersion()) |
121 | 121 | { |
122 | - if ($test) return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
122 | + if ($test) return array(5, $version->value, $this->getModuleConfig()->getDbMinVersion()); |
|
123 | 123 | $this->redirectNow('trapdirector/settings?dberror=5'); |
124 | 124 | return null; |
125 | 125 | } |
126 | 126 | } |
127 | 127 | catch (Exception $e) |
128 | 128 | { |
129 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
129 | + if ($test) return array(3, $DBname, $e->getMessage()); |
|
130 | 130 | $this->redirectNow('trapdirector/settings?dberror=4'); |
131 | 131 | return null; |
132 | 132 | } |
133 | 133 | } |
134 | - if ($test) return array(0,''); |
|
134 | + if ($test) return array(0, ''); |
|
135 | 135 | return $dbconn; |
136 | 136 | } |
137 | 137 | |
@@ -142,17 +142,17 @@ discard block |
||
142 | 142 | */ |
143 | 143 | public function getDb($test=false) |
144 | 144 | { |
145 | - if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
145 | + if ($this->trapDB != null && $test=false) return $this->trapDB; |
|
146 | 146 | |
147 | 147 | $dbresource=$this->Config()->get('config', 'database'); |
148 | 148 | |
149 | - if ( ! $dbresource ) |
|
149 | + if (!$dbresource) |
|
150 | 150 | { |
151 | - if ($test) return array(1,''); |
|
151 | + if ($test) return array(1, ''); |
|
152 | 152 | $this->redirectNow('trapdirector/settings?dberror=1'); |
153 | 153 | return null; |
154 | 154 | } |
155 | - $retDB=$this->getDbByName($dbresource,$test,true); |
|
155 | + $retDB=$this->getDbByName($dbresource, $test, true); |
|
156 | 156 | if ($test == true) return $retDB; |
157 | 157 | $this->trapDB=$retDB; |
158 | 158 | return $this->trapDB; |
@@ -160,78 +160,78 @@ discard block |
||
160 | 160 | |
161 | 161 | public function getIdoDb($test=false) |
162 | 162 | { |
163 | - if ($this->icingaDB != null && $test = false) return $this->icingaDB; |
|
163 | + if ($this->icingaDB != null && $test=false) return $this->icingaDB; |
|
164 | 164 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
165 | - $dbresource=$this->Config()->get('config', 'IDOdatabase');; |
|
165 | + $dbresource=$this->Config()->get('config', 'IDOdatabase'); ; |
|
166 | 166 | |
167 | - if ( ! $dbresource ) |
|
167 | + if (!$dbresource) |
|
168 | 168 | { |
169 | - if ($test) return array(1,'No database in config.ini'); |
|
169 | + if ($test) return array(1, 'No database in config.ini'); |
|
170 | 170 | $this->redirectNow('trapdirector/settings?idodberror=1'); |
171 | 171 | return null; |
172 | 172 | } |
173 | 173 | |
174 | 174 | try |
175 | 175 | { |
176 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
176 | + $dbconn=IcingaDbConnection::fromResourceName($dbresource); |
|
177 | 177 | } |
178 | 178 | catch (Exception $e) |
179 | 179 | { |
180 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
180 | + if ($test) return array(2, "Database $dbresource does not exists in IcingaWeb2"); |
|
181 | 181 | $this->redirectNow('trapdirector/settings?idodberror=2'); |
182 | 182 | return null; |
183 | 183 | } |
184 | 184 | |
185 | 185 | if ($test == false) |
186 | 186 | { |
187 | - $this->icingaDB = $dbconn; |
|
187 | + $this->icingaDB=$dbconn; |
|
188 | 188 | return $this->icingaDB; |
189 | 189 | } |
190 | 190 | |
191 | 191 | try |
192 | 192 | { |
193 | - $query = $dbconn->select() |
|
194 | - ->from('icinga_dbversion',array('version')); |
|
193 | + $query=$dbconn->select() |
|
194 | + ->from('icinga_dbversion', array('version')); |
|
195 | 195 | $version=$dbconn->fetchRow($query); |
196 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
196 | + if (($version == null) || !property_exists($version, 'version')) |
|
197 | 197 | { |
198 | - return array(4,"$dbresource does not look like an IDO database"); |
|
198 | + return array(4, "$dbresource does not look like an IDO database"); |
|
199 | 199 | } |
200 | 200 | } |
201 | 201 | catch (Exception $e) |
202 | 202 | { |
203 | - return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
|
203 | + return array(3, "Error connecting to $dbresource : ".$e->getMessage()); |
|
204 | 204 | } |
205 | 205 | |
206 | - return array(0,''); |
|
206 | + return array(0, ''); |
|
207 | 207 | } |
208 | 208 | |
209 | - protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
209 | + protected function applyPaginationLimits(Paginatable $paginatable, $limit=25, $offset=null) |
|
210 | 210 | { |
211 | - $limit = $this->params->get('limit', $limit); |
|
212 | - $page = $this->params->get('page', $offset); |
|
211 | + $limit=$this->params->get('limit', $limit); |
|
212 | + $page=$this->params->get('page', $offset); |
|
213 | 213 | |
214 | 214 | $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
215 | 215 | |
216 | 216 | return $paginatable; |
217 | 217 | } |
218 | 218 | |
219 | - public function displayExitError($source,$message) |
|
219 | + public function displayExitError($source, $message) |
|
220 | 220 | { // TODO : check better ways to transmit data (with POST ?) |
221 | 221 | $this->redirectNow('trapdirector/error?source='.$source.'&message='.$message); |
222 | 222 | } |
223 | 223 | |
224 | 224 | protected function checkReadPermission() |
225 | 225 | { |
226 | - if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
227 | - $this->displayExitError('Permissions','No permission fo view content'); |
|
226 | + if (!$this->Auth()->hasPermission('trapdirector/view')) { |
|
227 | + $this->displayExitError('Permissions', 'No permission fo view content'); |
|
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
231 | 231 | protected function checkConfigPermission() |
232 | 232 | { |
233 | - if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
234 | - $this->displayExitError('Permissions','No permission fo configure'); |
|
233 | + if (!$this->Auth()->hasPermission('trapdirector/config')) { |
|
234 | + $this->displayExitError('Permissions', 'No permission fo configure'); |
|
235 | 235 | } |
236 | 236 | } |
237 | 237 | |
@@ -242,10 +242,10 @@ discard block |
||
242 | 242 | */ |
243 | 243 | protected function checkModuleConfigPermission($check=0) |
244 | 244 | { |
245 | - if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
245 | + if (!$this->Auth()->hasPermission('trapdirector/module_config')) { |
|
246 | 246 | if ($check == 0) |
247 | 247 | { |
248 | - $this->displayExitError('Permissions','No permission fo configure module'); |
|
248 | + $this->displayExitError('Permissions', 'No permission fo configure module'); |
|
249 | 249 | } |
250 | 250 | return false; |
251 | 251 | } |
@@ -257,10 +257,10 @@ discard block |
||
257 | 257 | { // TODO : try/catch here ? or within caller |
258 | 258 | if ($this->trapClass == null) |
259 | 259 | { |
260 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
260 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
261 | 261 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
262 | 262 | //$debug_level=4; |
263 | - $this->trapClass = new Trap($icingaweb2_etc); |
|
263 | + $this->trapClass=new Trap($icingaweb2_etc); |
|
264 | 264 | //$Trap->setLogging($debug_level,'syslog'); |
265 | 265 | } |
266 | 266 | return $this->trapClass; |
@@ -294,12 +294,12 @@ discard block |
||
294 | 294 | protected function getHostByIP($ip) |
295 | 295 | { |
296 | 296 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
297 | - $db = $this->getIdoDb()->getConnection(); |
|
297 | + $db=$this->getIdoDb()->getConnection(); |
|
298 | 298 | // TODO : check for SQL injections |
299 | 299 | $query=$db->select() |
300 | 300 | ->from( |
301 | 301 | array('a' => 'icinga_objects'), |
302 | - array('name' => 'a.name1','id' => 'object_id')) |
|
302 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
303 | 303 | ->join( |
304 | 304 | array('b' => 'icinga_hosts'), |
305 | 305 | 'b.host_object_id=a.object_id', |
@@ -315,12 +315,12 @@ discard block |
||
315 | 315 | protected function getHostByName($name) |
316 | 316 | { |
317 | 317 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
318 | - $db = $this->getIdoDb()->getConnection(); |
|
318 | + $db=$this->getIdoDb()->getConnection(); |
|
319 | 319 | // TODO : check for SQL injections |
320 | 320 | $query=$db->select() |
321 | 321 | ->from( |
322 | 322 | array('a' => 'icinga_objects'), |
323 | - array('name' => 'a.name1','id' => 'object_id')) |
|
323 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
324 | 324 | ->join( |
325 | 325 | array('b' => 'icinga_hosts'), |
326 | 326 | 'b.host_object_id=a.object_id', |
@@ -336,12 +336,12 @@ discard block |
||
336 | 336 | protected function getHostGroupByName($ip) |
337 | 337 | { |
338 | 338 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
339 | - $db = $this->getIdoDb()->getConnection(); |
|
339 | + $db=$this->getIdoDb()->getConnection(); |
|
340 | 340 | // TODO : check for SQL injections |
341 | 341 | $query=$db->select() |
342 | 342 | ->from( |
343 | 343 | array('a' => 'icinga_objects'), |
344 | - array('name' => 'a.name1','id' => 'object_id')) |
|
344 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
345 | 345 | ->join( |
346 | 346 | array('b' => 'icinga_hostgroups'), |
347 | 347 | 'b.hostgroup_object_id=a.object_id', |
@@ -357,8 +357,8 @@ discard block |
||
357 | 357 | */ |
358 | 358 | protected function getHostInfoByID($id) |
359 | 359 | { |
360 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
361 | - $db = $this->getIdoDb()->getConnection(); |
|
360 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
361 | + $db=$this->getIdoDb()->getConnection(); |
|
362 | 362 | $query=$db->select() |
363 | 363 | ->from( |
364 | 364 | array('a' => 'icinga_objects'), |
@@ -378,17 +378,17 @@ discard block |
||
378 | 378 | */ |
379 | 379 | protected function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
380 | 380 | { |
381 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
382 | - $db = $this->getIdoDb()->getConnection(); |
|
381 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
382 | + $db=$this->getIdoDb()->getConnection(); |
|
383 | 383 | $query=$db->select() |
384 | 384 | ->from( |
385 | 385 | array('a' => 'icinga_objects'), |
386 | - array('name' => 'a.name1','id' => 'a.object_id')) |
|
386 | + array('name' => 'a.name1', 'id' => 'a.object_id')) |
|
387 | 387 | ->join( |
388 | 388 | array('b' => 'icinga_hosts'), |
389 | 389 | 'b.host_object_id=a.object_id', |
390 | - array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
391 | - ->where('a.object_id = ?',$id); |
|
390 | + array('display_name' => 'b.display_name', 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
391 | + ->where('a.object_id = ?', $id); |
|
392 | 392 | return $db->fetchRow($query); |
393 | 393 | } |
394 | 394 | |
@@ -400,16 +400,16 @@ discard block |
||
400 | 400 | protected function getServicesByHostid($id) |
401 | 401 | { |
402 | 402 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
403 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
404 | - $db = $this->getIdoDb()->getConnection(); |
|
403 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
404 | + $db=$this->getIdoDb()->getConnection(); |
|
405 | 405 | $query=$db->select() |
406 | 406 | ->from( |
407 | 407 | array('s' => 'icinga_services'), |
408 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
408 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
409 | 409 | ->join( |
410 | 410 | array('a' => 'icinga_objects'), |
411 | 411 | 's.service_object_id=a.object_id', |
412 | - array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
412 | + array('is_active'=>'a.is_active', 'name2'=>'a.name2')) |
|
413 | 413 | ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
414 | 414 | |
415 | 415 | return $db->fetchAll($query); |
@@ -423,8 +423,8 @@ discard block |
||
423 | 423 | */ |
424 | 424 | protected function getServicesByHostGroupid($id) |
425 | 425 | { |
426 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
427 | - $db = $this->getIdoDb()->getConnection(); |
|
426 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
427 | + $db=$this->getIdoDb()->getConnection(); |
|
428 | 428 | $query=$db->select() |
429 | 429 | ->from( |
430 | 430 | array('s' => 'icinga_hostgroup_members'), |
@@ -441,11 +441,11 @@ discard block |
||
441 | 441 | foreach ($hosts as $key => $host) |
442 | 442 | { // For each host, get all services and add in common_services if not found or add counter |
443 | 443 | $host_services=$this->getServicesByHostid($host->host_object_id); |
444 | - foreach($host_services as $service) |
|
444 | + foreach ($host_services as $service) |
|
445 | 445 | { |
446 | 446 | if (isset($common_services[$service->name2]['num'])) |
447 | 447 | { |
448 | - $common_services[$service->name2]['num'] +=1; |
|
448 | + $common_services[$service->name2]['num']+=1; |
|
449 | 449 | } |
450 | 450 | else |
451 | 451 | { |
@@ -461,7 +461,7 @@ discard block |
||
461 | 461 | { |
462 | 462 | if ($common_services[$key]['num'] == $num_hosts) |
463 | 463 | { |
464 | - array_push($result,array($key,$common_services[$key]['name'])); |
|
464 | + array_push($result, array($key, $common_services[$key]['name'])); |
|
465 | 465 | } |
466 | 466 | } |
467 | 467 | |
@@ -474,9 +474,9 @@ discard block |
||
474 | 474 | * @param $name string service name |
475 | 475 | * @return array service id |
476 | 476 | */ |
477 | - protected function getServiceIDByName($hostname,$name) |
|
477 | + protected function getServiceIDByName($hostname, $name) |
|
478 | 478 | { |
479 | - $db = $this->getIdoDb()->getConnection(); |
|
479 | + $db=$this->getIdoDb()->getConnection(); |
|
480 | 480 | if ($name == null) |
481 | 481 | { |
482 | 482 | return array(); |
@@ -485,7 +485,7 @@ discard block |
||
485 | 485 | $query=$db->select() |
486 | 486 | ->from( |
487 | 487 | array('s' => 'icinga_services'), |
488 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
488 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
489 | 489 | ->join( |
490 | 490 | array('a' => 'icinga_objects'), |
491 | 491 | 's.service_object_id=a.object_id', |
@@ -503,12 +503,12 @@ discard block |
||
503 | 503 | protected function getObjectNameByid($id) |
504 | 504 | { |
505 | 505 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
506 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
507 | - $db = $this->getIdoDb()->getConnection(); |
|
506 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
507 | + $db=$this->getIdoDb()->getConnection(); |
|
508 | 508 | $query=$db->select() |
509 | 509 | ->from( |
510 | 510 | array('a' => 'icinga_objects'), |
511 | - array('name1' => 'a.name1','name2' => 'a.name2')) |
|
511 | + array('name1' => 'a.name1', 'name2' => 'a.name2')) |
|
512 | 512 | ->where('a.object_id='.$id.' AND a.is_active = 1'); |
513 | 513 | |
514 | 514 | return $db->fetchRow($query); |
@@ -521,17 +521,17 @@ discard block |
||
521 | 521 | protected function addHandlerRule($params) |
522 | 522 | { |
523 | 523 | // TODO Check for rule consistency |
524 | - $db = $this->getDb()->getConnection(); |
|
524 | + $db=$this->getDb()->getConnection(); |
|
525 | 525 | // Add last modified date = creation date and username |
526 | - $params['created'] = new Zend_Db_Expr('NOW()'); |
|
527 | - $params['modified'] = new Zend_Db_Expr('NOW()'); |
|
528 | - $params['modifier'] = $this->Auth()->getUser()->getUsername(); |
|
526 | + $params['created']=new Zend_Db_Expr('NOW()'); |
|
527 | + $params['modified']=new Zend_Db_Expr('NOW()'); |
|
528 | + $params['modifier']=$this->Auth()->getUser()->getUsername(); |
|
529 | 529 | |
530 | 530 | $query=$db->insert( |
531 | 531 | $this->getModuleConfig()->getTrapRuleName(), |
532 | 532 | $params |
533 | 533 | ); |
534 | - if($query==false) |
|
534 | + if ($query == false) |
|
535 | 535 | { |
536 | 536 | return null; |
537 | 537 | } |
@@ -543,13 +543,13 @@ discard block |
||
543 | 543 | * @param integer $ruleID : rule id in db |
544 | 544 | * @return array affected rows |
545 | 545 | */ |
546 | - protected function updateHandlerRule($params,$ruleID) |
|
546 | + protected function updateHandlerRule($params, $ruleID) |
|
547 | 547 | { |
548 | 548 | // TODO Check for rule consistency |
549 | - $db = $this->getDb()->getConnection(); |
|
549 | + $db=$this->getDb()->getConnection(); |
|
550 | 550 | // Add last modified date = creation date and username |
551 | - $params['modified'] = new Zend_Db_Expr('NOW()'); |
|
552 | - $params['modifier'] = $this->Auth()->getUser()->getUsername(); |
|
551 | + $params['modified']=new Zend_Db_Expr('NOW()'); |
|
552 | + $params['modifier']=$this->Auth()->getUser()->getUsername(); |
|
553 | 553 | |
554 | 554 | $numRows=$db->update( |
555 | 555 | $this->getModuleConfig()->getTrapRuleName(), |
@@ -564,8 +564,8 @@ discard block |
||
564 | 564 | */ |
565 | 565 | protected function deleteRule($ruleID) |
566 | 566 | { |
567 | - if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
|
568 | - $db = $this->getDb()->getConnection(); |
|
567 | + if (!preg_match('/^[0-9]+$/', $ruleID)) { throw new Exception('Invalid id'); } |
|
568 | + $db=$this->getDb()->getConnection(); |
|
569 | 569 | |
570 | 570 | $query=$db->delete( |
571 | 571 | $this->getModuleConfig()->getTrapRuleName(), |
@@ -578,10 +578,10 @@ discard block |
||
578 | 578 | * @param $ip string source IP (v4 or v6) |
579 | 579 | * @param $oid string oid |
580 | 580 | */ |
581 | - protected function deleteTrap($ip,$oid) |
|
581 | + protected function deleteTrap($ip, $oid) |
|
582 | 582 | { |
583 | 583 | |
584 | - $db = $this->getDb()->getConnection(); |
|
584 | + $db=$this->getDb()->getConnection(); |
|
585 | 585 | $condition=null; |
586 | 586 | if ($ip != null) |
587 | 587 | { |
@@ -589,10 +589,10 @@ discard block |
||
589 | 589 | } |
590 | 590 | if ($oid != null) |
591 | 591 | { |
592 | - $condition=($condition===null)?'':$condition.' AND '; |
|
592 | + $condition=($condition === null) ? '' : $condition.' AND '; |
|
593 | 593 | $condition.="trap_oid='$oid'"; |
594 | 594 | } |
595 | - if($condition === null) return null; |
|
595 | + if ($condition === null) return null; |
|
596 | 596 | $query=$db->delete( |
597 | 597 | $this->getModuleConfig()->getTrapTableName(), |
598 | 598 | $condition |
@@ -606,10 +606,10 @@ discard block |
||
606 | 606 | * @param $ip string source IP (v4 or v6) |
607 | 607 | * @param $oid string oid |
608 | 608 | */ |
609 | - protected function countTrap($ip,$oid) |
|
609 | + protected function countTrap($ip, $oid) |
|
610 | 610 | { |
611 | 611 | |
612 | - $db = $this->getDb()->getConnection(); |
|
612 | + $db=$this->getDb()->getConnection(); |
|
613 | 613 | $condition=null; |
614 | 614 | if ($ip != null) |
615 | 615 | { |
@@ -617,10 +617,10 @@ discard block |
||
617 | 617 | } |
618 | 618 | if ($oid != null) |
619 | 619 | { |
620 | - $condition=($condition===null)?'':$condition.' AND '; |
|
620 | + $condition=($condition === null) ? '' : $condition.' AND '; |
|
621 | 621 | $condition.="trap_oid='$oid'"; |
622 | 622 | } |
623 | - if($condition === null) return 0; |
|
623 | + if ($condition === null) return 0; |
|
624 | 624 | $query=$db->select() |
625 | 625 | ->from( |
626 | 626 | $this->getModuleConfig()->getTrapTableName(), |
@@ -636,27 +636,27 @@ discard block |
||
636 | 636 | protected function getDBConfigValue($element) |
637 | 637 | { |
638 | 638 | |
639 | - $db = $this->getDb()->getConnection(); |
|
639 | + $db=$this->getDb()->getConnection(); |
|
640 | 640 | |
641 | 641 | $query=$db->select() |
642 | 642 | ->from( |
643 | 643 | $this->getModuleConfig()->getDbConfigTableName(), |
644 | 644 | array('value'=>'value')) |
645 | - ->where('name=?',$element); |
|
645 | + ->where('name=?', $element); |
|
646 | 646 | $return_row=$db->fetchRow($query); |
647 | - if ($return_row==null) // value does not exists |
|
647 | + if ($return_row == null) // value does not exists |
|
648 | 648 | { |
649 | 649 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
650 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
650 | + if (!isset($default[$element])) return null; // no default and not value |
|
651 | 651 | |
652 | - $this->addDBConfigValue($element,$default[$element]); |
|
652 | + $this->addDBConfigValue($element, $default[$element]); |
|
653 | 653 | return $default[$element]; |
654 | 654 | } |
655 | 655 | if ($return_row->value == null) // value id empty |
656 | 656 | { |
657 | 657 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
658 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
659 | - $this->setDBConfigValue($element,$default[$element]); |
|
658 | + if (!isset($default[$element])) return null; // no default and not value |
|
659 | + $this->setDBConfigValue($element, $default[$element]); |
|
660 | 660 | return $default[$element]; |
661 | 661 | } |
662 | 662 | return $return_row->value; |
@@ -667,10 +667,10 @@ discard block |
||
667 | 667 | * @param string $value : value |
668 | 668 | */ |
669 | 669 | |
670 | - protected function addDBConfigValue($element,$value) |
|
670 | + protected function addDBConfigValue($element, $value) |
|
671 | 671 | { |
672 | 672 | |
673 | - $db = $this->getDb()->getConnection(); |
|
673 | + $db=$this->getDb()->getConnection(); |
|
674 | 674 | |
675 | 675 | $query=$db->insert( |
676 | 676 | $this->getModuleConfig()->getDbConfigTableName(), |
@@ -686,10 +686,10 @@ discard block |
||
686 | 686 | * @param string $element : name of config element |
687 | 687 | * @param string $value : value |
688 | 688 | */ |
689 | - protected function setDBConfigValue($element,$value) |
|
689 | + protected function setDBConfigValue($element, $value) |
|
690 | 690 | { |
691 | 691 | |
692 | - $db = $this->getDb()->getConnection(); |
|
692 | + $db=$this->getDb()->getConnection(); |
|
693 | 693 | $query=$db->update( |
694 | 694 | $this->getModuleConfig()->getDbConfigTableName(), |
695 | 695 | array('value'=>$value), |
@@ -704,10 +704,10 @@ discard block |
||
704 | 704 | protected function isDirectorInstalled() |
705 | 705 | { |
706 | 706 | $output=array(); |
707 | - exec('icingacli module list',$output); |
|
707 | + exec('icingacli module list', $output); |
|
708 | 708 | foreach ($output as $line) |
709 | 709 | { |
710 | - if (preg_match('/^director .*enabled/',$line)) |
|
710 | + if (preg_match('/^director .*enabled/', $line)) |
|
711 | 711 | { |
712 | 712 | return true; |
713 | 713 | } |
@@ -87,10 +87,11 @@ discard block |
||
87 | 87 | try |
88 | 88 | { |
89 | 89 | $dbconn = IcingaDbConnection::fromResourceName($DBname); |
90 | - } |
|
91 | - catch (Exception $e) |
|
90 | + } catch (Exception $e) |
|
92 | 91 | { |
93 | - if ($test) return array(2,$DBname); |
|
92 | + if ($test) { |
|
93 | + return array(2,$DBname); |
|
94 | + } |
|
94 | 95 | $this->redirectNow('trapdirector/settings?dberror=2'); |
95 | 96 | return null; |
96 | 97 | } |
@@ -98,10 +99,11 @@ discard block |
||
98 | 99 | try |
99 | 100 | { |
100 | 101 | $db=$dbconn->getConnection(); |
101 | - } |
|
102 | - catch (Exception $e) |
|
102 | + } catch (Exception $e) |
|
103 | 103 | { |
104 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
104 | + if ($test) { |
|
105 | + return array(3,$DBname,$e->getMessage()); |
|
106 | + } |
|
105 | 107 | $this->redirectNow('trapdirector/settings?dberror=3'); |
106 | 108 | return null; |
107 | 109 | } |
@@ -113,25 +115,32 @@ discard block |
||
113 | 115 | $version=$db->fetchRow($query); |
114 | 116 | if ( ($version == null) || ! property_exists($version,'value') ) |
115 | 117 | { |
116 | - if ($test) return array(4,$DBname); |
|
118 | + if ($test) { |
|
119 | + return array(4,$DBname); |
|
120 | + } |
|
117 | 121 | $this->redirectNow('trapdirector/settings?dberror=4'); |
118 | 122 | return null; |
119 | 123 | } |
120 | 124 | if ($version->value < $this->getModuleConfig()->getDbMinVersion()) |
121 | 125 | { |
122 | - if ($test) return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
126 | + if ($test) { |
|
127 | + return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
128 | + } |
|
123 | 129 | $this->redirectNow('trapdirector/settings?dberror=5'); |
124 | 130 | return null; |
125 | 131 | } |
126 | - } |
|
127 | - catch (Exception $e) |
|
132 | + } catch (Exception $e) |
|
128 | 133 | { |
129 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
134 | + if ($test) { |
|
135 | + return array(3,$DBname,$e->getMessage()); |
|
136 | + } |
|
130 | 137 | $this->redirectNow('trapdirector/settings?dberror=4'); |
131 | 138 | return null; |
132 | 139 | } |
133 | 140 | } |
134 | - if ($test) return array(0,''); |
|
141 | + if ($test) { |
|
142 | + return array(0,''); |
|
143 | + } |
|
135 | 144 | return $dbconn; |
136 | 145 | } |
137 | 146 | |
@@ -142,31 +151,41 @@ discard block |
||
142 | 151 | */ |
143 | 152 | public function getDb($test=false) |
144 | 153 | { |
145 | - if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
154 | + if ($this->trapDB != null && $test = false) { |
|
155 | + return $this->trapDB; |
|
156 | + } |
|
146 | 157 | |
147 | 158 | $dbresource=$this->Config()->get('config', 'database'); |
148 | 159 | |
149 | 160 | if ( ! $dbresource ) |
150 | 161 | { |
151 | - if ($test) return array(1,''); |
|
162 | + if ($test) { |
|
163 | + return array(1,''); |
|
164 | + } |
|
152 | 165 | $this->redirectNow('trapdirector/settings?dberror=1'); |
153 | 166 | return null; |
154 | 167 | } |
155 | 168 | $retDB=$this->getDbByName($dbresource,$test,true); |
156 | - if ($test == true) return $retDB; |
|
169 | + if ($test == true) { |
|
170 | + return $retDB; |
|
171 | + } |
|
157 | 172 | $this->trapDB=$retDB; |
158 | 173 | return $this->trapDB; |
159 | 174 | } |
160 | 175 | |
161 | 176 | public function getIdoDb($test=false) |
162 | 177 | { |
163 | - if ($this->icingaDB != null && $test = false) return $this->icingaDB; |
|
178 | + if ($this->icingaDB != null && $test = false) { |
|
179 | + return $this->icingaDB; |
|
180 | + } |
|
164 | 181 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
165 | 182 | $dbresource=$this->Config()->get('config', 'IDOdatabase');; |
166 | 183 | |
167 | 184 | if ( ! $dbresource ) |
168 | 185 | { |
169 | - if ($test) return array(1,'No database in config.ini'); |
|
186 | + if ($test) { |
|
187 | + return array(1,'No database in config.ini'); |
|
188 | + } |
|
170 | 189 | $this->redirectNow('trapdirector/settings?idodberror=1'); |
171 | 190 | return null; |
172 | 191 | } |
@@ -174,10 +193,11 @@ discard block |
||
174 | 193 | try |
175 | 194 | { |
176 | 195 | $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
177 | - } |
|
178 | - catch (Exception $e) |
|
196 | + } catch (Exception $e) |
|
179 | 197 | { |
180 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
198 | + if ($test) { |
|
199 | + return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
200 | + } |
|
181 | 201 | $this->redirectNow('trapdirector/settings?idodberror=2'); |
182 | 202 | return null; |
183 | 203 | } |
@@ -197,8 +217,7 @@ discard block |
||
197 | 217 | { |
198 | 218 | return array(4,"$dbresource does not look like an IDO database"); |
199 | 219 | } |
200 | - } |
|
201 | - catch (Exception $e) |
|
220 | + } catch (Exception $e) |
|
202 | 221 | { |
203 | 222 | return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
204 | 223 | } |
@@ -446,8 +465,7 @@ discard block |
||
446 | 465 | if (isset($common_services[$service->name2]['num'])) |
447 | 466 | { |
448 | 467 | $common_services[$service->name2]['num'] +=1; |
449 | - } |
|
450 | - else |
|
468 | + } else |
|
451 | 469 | { |
452 | 470 | $common_services[$service->name2]['num']=1; |
453 | 471 | $common_services[$service->name2]['name']=$service->name; |
@@ -592,7 +610,9 @@ discard block |
||
592 | 610 | $condition=($condition===null)?'':$condition.' AND '; |
593 | 611 | $condition.="trap_oid='$oid'"; |
594 | 612 | } |
595 | - if($condition === null) return null; |
|
613 | + if($condition === null) { |
|
614 | + return null; |
|
615 | + } |
|
596 | 616 | $query=$db->delete( |
597 | 617 | $this->getModuleConfig()->getTrapTableName(), |
598 | 618 | $condition |
@@ -620,7 +640,9 @@ discard block |
||
620 | 640 | $condition=($condition===null)?'':$condition.' AND '; |
621 | 641 | $condition.="trap_oid='$oid'"; |
622 | 642 | } |
623 | - if($condition === null) return 0; |
|
643 | + if($condition === null) { |
|
644 | + return 0; |
|
645 | + } |
|
624 | 646 | $query=$db->select() |
625 | 647 | ->from( |
626 | 648 | $this->getModuleConfig()->getTrapTableName(), |
@@ -644,18 +666,28 @@ discard block |
||
644 | 666 | array('value'=>'value')) |
645 | 667 | ->where('name=?',$element); |
646 | 668 | $return_row=$db->fetchRow($query); |
647 | - if ($return_row==null) // value does not exists |
|
669 | + if ($return_row==null) { |
|
670 | + // value does not exists |
|
648 | 671 | { |
649 | 672 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
650 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
673 | + } |
|
674 | + if ( ! isset($default[$element])) { |
|
675 | + return null; |
|
676 | + } |
|
677 | + // no default and not value |
|
651 | 678 | |
652 | 679 | $this->addDBConfigValue($element,$default[$element]); |
653 | 680 | return $default[$element]; |
654 | 681 | } |
655 | - if ($return_row->value == null) // value id empty |
|
682 | + if ($return_row->value == null) { |
|
683 | + // value id empty |
|
656 | 684 | { |
657 | 685 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
658 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
686 | + } |
|
687 | + if ( ! isset($default[$element])) { |
|
688 | + return null; |
|
689 | + } |
|
690 | + // no default and not value |
|
659 | 691 | $this->setDBConfigValue($element,$default[$element]); |
660 | 692 | return $default[$element]; |
661 | 693 | } |