@@ -22,37 +22,37 @@ discard block |
||
| 22 | 22 | public function indexAction() |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - // CHeck permissions : display tests in any case, but no configuration. |
|
| 25 | + // CHeck permissions : display tests in any case, but no configuration. |
|
| 26 | 26 | $this->view->configPermission=$this->checkModuleConfigPermission(1); |
| 27 | 27 | // But check read permission |
| 28 | 28 | $this->checkReadPermission(); |
| 29 | 29 | // Get message : sent on configuration problems detected by controllers |
| 30 | 30 | $dberrorMsg=$this->params->get('dberror'); |
| 31 | 31 | if ($dberrorMsg != '') |
| 32 | - $this->view->errorDetected=$dberrorMsg; |
|
| 33 | - $dberrorMsg=$this->params->get('idodberror'); |
|
| 34 | - if ($dberrorMsg != '') |
|
| 35 | - $this->view->errorDetected=$dberrorMsg; |
|
| 32 | + $this->view->errorDetected=$dberrorMsg; |
|
| 33 | + $dberrorMsg=$this->params->get('idodberror'); |
|
| 34 | + if ($dberrorMsg != '') |
|
| 35 | + $this->view->errorDetected=$dberrorMsg; |
|
| 36 | 36 | |
| 37 | - $this->view->configErrorDetected == NULL; // Displayed error on various conifugration errors. |
|
| 37 | + $this->view->configErrorDetected == NULL; // Displayed error on various conifugration errors. |
|
| 38 | 38 | |
| 39 | - // Test if configuration exists, if not create for installer script |
|
| 39 | + // Test if configuration exists, if not create for installer script |
|
| 40 | 40 | //$emptyConfig=0; |
| 41 | - if ($this->Config()->isEmpty() == true) |
|
| 42 | - { |
|
| 43 | - $this->Config()->setSection('config'); // Set base config section. |
|
| 44 | - try |
|
| 45 | - { |
|
| 46 | - $this->Config()->saveIni(); |
|
| 47 | - $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
|
| 41 | + if ($this->Config()->isEmpty() == true) |
|
| 42 | + { |
|
| 43 | + $this->Config()->setSection('config'); // Set base config section. |
|
| 44 | + try |
|
| 45 | + { |
|
| 46 | + $this->Config()->saveIni(); |
|
| 47 | + $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
|
| 48 | 48 | //$emptyConfig=1; |
| 49 | - } |
|
| 50 | - catch (Exception $e) |
|
| 51 | - { |
|
| 52 | - $this->view->configErrorDetected=$e->getMessage(); |
|
| 53 | - } |
|
| 49 | + } |
|
| 50 | + catch (Exception $e) |
|
| 51 | + { |
|
| 52 | + $this->view->configErrorDetected=$e->getMessage(); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - } |
|
| 55 | + } |
|
| 56 | 56 | // Test Database |
| 57 | 57 | $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
| 58 | 58 | 0 => array('Database configuration OK','',''), |
@@ -97,20 +97,20 @@ discard block |
||
| 97 | 97 | //********* Test API |
| 98 | 98 | if ($this->Config()->get('config', 'icingaAPI_host') != '') |
| 99 | 99 | { |
| 100 | - $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
| 101 | - $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
|
| 102 | - try { |
|
| 103 | - list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 104 | - //$this->view->apimessageError=false; |
|
| 105 | - } catch (RuntimeException $e) { |
|
| 106 | - $this->view->apimessage='API config : ' . $e->getMessage(); |
|
| 107 | - $this->view->apimessageError=true; |
|
| 108 | - } |
|
| 100 | + $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
| 101 | + $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
|
| 102 | + try { |
|
| 103 | + list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 104 | + //$this->view->apimessageError=false; |
|
| 105 | + } catch (RuntimeException $e) { |
|
| 106 | + $this->view->apimessage='API config : ' . $e->getMessage(); |
|
| 107 | + $this->view->apimessageError=true; |
|
| 108 | + } |
|
| 109 | 109 | } |
| 110 | 110 | else |
| 111 | 111 | { |
| 112 | - $this->view->apimessage='API parameters not configured'; |
|
| 113 | - $this->view->apimessageError=true; |
|
| 112 | + $this->view->apimessage='API parameters not configured'; |
|
| 113 | + $this->view->apimessageError=true; |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | //*********** Test snmptrapd alive and options |
@@ -125,33 +125,33 @@ discard block |
||
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
|
| 128 | + $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
|
| 129 | 129 | |
| 130 | 130 | // Check standard Icingaweb2 path |
| 131 | 131 | $this->view->icingaEtcWarn=0; |
| 132 | 132 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 133 | 133 | if ($icingaweb2_etc != "/etc/icingaweb2/" && $icingaweb2_etc != '') |
| 134 | 134 | { |
| 135 | - $output=array(); |
|
| 135 | + $output=array(); |
|
| 136 | 136 | |
| 137 | - exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2_etc=" ',$output); |
|
| 137 | + exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2_etc=" ',$output); |
|
| 138 | 138 | |
| 139 | - if (! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
| 140 | - { |
|
| 141 | - $this->view->icingaEtcWarn=1; |
|
| 142 | - $this->view->icingaweb2_etc=$icingaweb2_etc; |
|
| 143 | - } |
|
| 139 | + if (! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
| 140 | + { |
|
| 141 | + $this->view->icingaEtcWarn=1; |
|
| 142 | + $this->view->icingaweb2_etc=$icingaweb2_etc; |
|
| 143 | + } |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Setup path for mini documentation |
| 147 | 147 | $this->view->traps_in_config= PHP_BINARY . ' ' . $this->Module()->getBaseDir() . '/bin/trap_in.php'; |
| 148 | 148 | |
| 149 | 149 | $this->view->installer= $this->Module()->getBaseDir() . '/bin/installer.sh ' |
| 150 | - . ' -c all ' |
|
| 151 | - . ' -d ' . $this->Module()->getBaseDir() |
|
| 152 | - . ' -p ' . PHP_BINARY |
|
| 153 | - . ' -a ' . exec('whoami') |
|
| 154 | - . ' -w ' . Icinga::app()->getConfigDir(); |
|
| 150 | + . ' -c all ' |
|
| 151 | + . ' -d ' . $this->Module()->getBaseDir() |
|
| 152 | + . ' -p ' . PHP_BINARY |
|
| 153 | + . ' -a ' . exec('whoami') |
|
| 154 | + . ' -w ' . Icinga::app()->getConfigDir(); |
|
| 155 | 155 | |
| 156 | 156 | // ******************* configuration form setup******************* |
| 157 | 157 | $this->view->form = $form = new TrapsConfigForm(); |
@@ -196,19 +196,19 @@ discard block |
||
| 196 | 196 | $dbResource = ResourceFactory::getResourceConfig($dbName); |
| 197 | 197 | $dbType=$dbResource->get('db'); |
| 198 | 198 | switch ($dbType) { |
| 199 | - case 'mysql': |
|
| 200 | - $dbFileExt='sql'; |
|
| 201 | - break; |
|
| 202 | - case 'pgsql': |
|
| 203 | - $dbFileExt='pgsql'; |
|
| 204 | - break; |
|
| 205 | - default: |
|
| 206 | - throw new ConfigurationError('Unsuported database : '.$dbType); |
|
| 199 | + case 'mysql': |
|
| 200 | + $dbFileExt='sql'; |
|
| 201 | + break; |
|
| 202 | + case 'pgsql': |
|
| 203 | + $dbFileExt='pgsql'; |
|
| 204 | + break; |
|
| 205 | + default: |
|
| 206 | + throw new ConfigurationError('Unsuported database : '.$dbType); |
|
| 207 | 207 | } |
| 208 | 208 | } catch (ConfigurationError $e ) |
| 209 | 209 | { |
| 210 | - printf("Database configuration error : %s",$e->getMessage()); |
|
| 211 | - return; |
|
| 210 | + printf("Database configuration error : %s",$e->getMessage()); |
|
| 211 | + return; |
|
| 212 | 212 | } |
| 213 | 213 | printf('<pre>'); |
| 214 | 214 | require_once $this->Module()->getBaseDir() .'/bin/trap_class.php'; |
@@ -232,11 +232,11 @@ discard block |
||
| 232 | 232 | public function updateschemaAction() |
| 233 | 233 | { |
| 234 | 234 | $this->checkModuleConfigPermission(); |
| 235 | - $this->getTabs()->add('get',array( |
|
| 236 | - 'active' => true, |
|
| 237 | - 'label' => $this->translate('Update Schema'), |
|
| 238 | - 'url' => Url::fromRequest() |
|
| 239 | - )); |
|
| 235 | + $this->getTabs()->add('get',array( |
|
| 236 | + 'active' => true, |
|
| 237 | + 'label' => $this->translate('Update Schema'), |
|
| 238 | + 'url' => Url::fromRequest() |
|
| 239 | + )); |
|
| 240 | 240 | // check if needed |
| 241 | 241 | |
| 242 | 242 | $dberror=$this->getDb(true); // Get DB in test mode |
@@ -245,15 +245,15 @@ discard block |
||
| 245 | 245 | |
| 246 | 246 | if ($dberror[0] == 0) |
| 247 | 247 | { |
| 248 | - echo 'Schema already exists and is up to date<br>'; |
|
| 249 | - return; |
|
| 248 | + echo 'Schema already exists and is up to date<br>'; |
|
| 249 | + return; |
|
| 250 | 250 | } |
| 251 | 251 | if ($dberror[0] != 5) |
| 252 | 252 | { |
| 253 | - echo 'Database does not exists or is not setup correctly<br>'; |
|
| 254 | - return; |
|
| 253 | + echo 'Database does not exists or is not setup correctly<br>'; |
|
| 254 | + return; |
|
| 255 | 255 | } |
| 256 | - // setup |
|
| 256 | + // setup |
|
| 257 | 257 | require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
| 258 | 258 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 259 | 259 | $debug_level=4; |
@@ -266,20 +266,20 @@ discard block |
||
| 266 | 266 | $target_version=$dberror[2]; |
| 267 | 267 | |
| 268 | 268 | if ($this->params->get('msgok') == null) { |
| 269 | - // Check for messages and display if any |
|
| 270 | - echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
|
| 271 | - $Trap->setLogging(2,'syslog'); |
|
| 272 | - $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
| 273 | - if ($message != '') |
|
| 274 | - { |
|
| 275 | - echo 'Note :<br><pre>'; |
|
| 276 | - echo $message; |
|
| 277 | - echo '</pre>'; |
|
| 278 | - echo '<br>'; |
|
| 279 | - echo '<a class="link-button" style="font-size:large;font-weight:bold" href="' . Url::fromPath('trapdirector/settings/updateschema') .'?msgok=1">Click here to update</a>'; |
|
| 280 | - echo '<br>'; |
|
| 281 | - return; |
|
| 282 | - } |
|
| 269 | + // Check for messages and display if any |
|
| 270 | + echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
|
| 271 | + $Trap->setLogging(2,'syslog'); |
|
| 272 | + $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
| 273 | + if ($message != '') |
|
| 274 | + { |
|
| 275 | + echo 'Note :<br><pre>'; |
|
| 276 | + echo $message; |
|
| 277 | + echo '</pre>'; |
|
| 278 | + echo '<br>'; |
|
| 279 | + echo '<a class="link-button" style="font-size:large;font-weight:bold" href="' . Url::fromPath('trapdirector/settings/updateschema') .'?msgok=1">Click here to update</a>'; |
|
| 280 | + echo '<br>'; |
|
| 281 | + return; |
|
| 282 | + } |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | $Trap->setLogging($debug_level,'display'); |
@@ -293,28 +293,28 @@ discard block |
||
| 293 | 293 | |
| 294 | 294 | private function checkSnmpTrapd() |
| 295 | 295 | { |
| 296 | - $psOutput=array(); |
|
| 297 | - // First check is someone is listening to port 162. As not root, we can't have pid... |
|
| 298 | - exec('netstat -an |grep -E "udp.*:162"',$psOutput); |
|
| 299 | - if (count($psOutput) == 0) |
|
| 300 | - { |
|
| 301 | - return array(1,'Port UDP/162 is not open : snmptrapd must not be started'); |
|
| 302 | - } |
|
| 303 | - $psOutput=array(); |
|
| 304 | - exec('ps fax |grep snmptrapd |grep -v grep',$psOutput); |
|
| 305 | - if (count($psOutput) == 0) |
|
| 306 | - { |
|
| 307 | - return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 308 | - } |
|
| 309 | - // Assume there is only one line... TODO : see if there is a better way to do this |
|
| 310 | - $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
| 311 | - if (!preg_match('/-n/',$line)) |
|
| 312 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
| 313 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
| 314 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
| 315 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
| 316 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 296 | + $psOutput=array(); |
|
| 297 | + // First check is someone is listening to port 162. As not root, we can't have pid... |
|
| 298 | + exec('netstat -an |grep -E "udp.*:162"',$psOutput); |
|
| 299 | + if (count($psOutput) == 0) |
|
| 300 | + { |
|
| 301 | + return array(1,'Port UDP/162 is not open : snmptrapd must not be started'); |
|
| 302 | + } |
|
| 303 | + $psOutput=array(); |
|
| 304 | + exec('ps fax |grep snmptrapd |grep -v grep',$psOutput); |
|
| 305 | + if (count($psOutput) == 0) |
|
| 306 | + { |
|
| 307 | + return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 308 | + } |
|
| 309 | + // Assume there is only one line... TODO : see if there is a better way to do this |
|
| 310 | + $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
| 311 | + if (!preg_match('/-n/',$line)) |
|
| 312 | + return array(1,'snmptrapd has no -n option : '.$line); |
|
| 313 | + if (!preg_match('/-O[^ ]*n/',$line)) |
|
| 314 | + return array(1,'snmptrapd has no -On option : '.$line); |
|
| 315 | + if (!preg_match('/-O[^ ]*e/',$line)) |
|
| 316 | + return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 317 | 317 | |
| 318 | - return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
| 318 | + return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
| 319 | 319 | } |
| 320 | 320 | } |
@@ -55,15 +55,15 @@ discard block |
||
| 55 | 55 | } |
| 56 | 56 | // Test Database |
| 57 | 57 | $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
| 58 | - 0 => array('Database configuration OK','',''), |
|
| 59 | - 1 => array('Database set in config.ini','No database in config.ini',''), |
|
| 60 | - 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
|
| 58 | + 0 => array('Database configuration OK', '', ''), |
|
| 59 | + 1 => array('Database set in config.ini', 'No database in config.ini', ''), |
|
| 60 | + 2 => array('Database exists in Icingaweb2 config', 'Database does not exist in Icingaweb2 : ', |
|
| 61 | 61 | Url::fromPath('config/resource')), |
| 62 | - 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
|
| 62 | + 3 => array('Database credentials OK', 'Database does not exist/invalid credentials/no schema : ', |
|
| 63 | 63 | Url::fromPath('trapdirector/settings/createschema')), |
| 64 | - 4 => array('Schema is set','Schema is not set for ', |
|
| 64 | + 4 => array('Schema is set', 'Schema is not set for ', |
|
| 65 | 65 | Url::fromPath('trapdirector/settings/createschema')), |
| 66 | - 5 => array('Schema is up to date','Schema is outdated :', |
|
| 66 | + 5 => array('Schema is up to date', 'Schema is outdated :', |
|
| 67 | 67 | Url::fromPath('trapdirector/settings/updateschema')), |
| 68 | 68 | ); |
| 69 | 69 | |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | { |
| 75 | 75 | case 2: |
| 76 | 76 | case 4: |
| 77 | - $db_message[$dberror[0]][1] .= $dberror[1]; |
|
| 77 | + $db_message[$dberror[0]][1].=$dberror[1]; |
|
| 78 | 78 | break; |
| 79 | 79 | case 3: |
| 80 | - $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
|
| 80 | + $db_message[$dberror[0]][1].=$dberror[1].', Message : '.$dberror[2]; |
|
| 81 | 81 | break; |
| 82 | 82 | case 5: |
| 83 | - $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
|
| 83 | + $db_message[$dberror[0]][1].=' version '.$dberror[1].', version needed : '.$dberror[2]; |
|
| 84 | 84 | break; |
| 85 | 85 | case 0: |
| 86 | 86 | case 1: |
@@ -92,18 +92,18 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | $dberror=$this->getIdoDb(true); // Get IDO DB in test mode |
| 94 | 94 | $this->view->ido_db_error=$dberror[0]; |
| 95 | - $this->view->ido_message='IDO Database : ' . $dberror[1]; |
|
| 95 | + $this->view->ido_message='IDO Database : '.$dberror[1]; |
|
| 96 | 96 | |
| 97 | 97 | //********* Test API |
| 98 | 98 | if ($this->Config()->get('config', 'icingaAPI_host') != '') |
| 99 | 99 | { |
| 100 | - $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
| 100 | + $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'), $this->Config()->get('config', 'icingaAPI_port')); |
|
| 101 | 101 | $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
| 102 | 102 | try { |
| 103 | - list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 103 | + list($this->view->apimessageError, $this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 104 | 104 | //$this->view->apimessageError=false; |
| 105 | 105 | } catch (RuntimeException $e) { |
| 106 | - $this->view->apimessage='API config : ' . $e->getMessage(); |
|
| 106 | + $this->view->apimessage='API config : '.$e->getMessage(); |
|
| 107 | 107 | $this->view->apimessageError=true; |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -114,18 +114,18 @@ discard block |
||
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | //*********** Test snmptrapd alive and options |
| 117 | - list ($this->view->snmptrapdError, $this->view->snmptrapdMessage) = $this->checkSnmpTrapd(); |
|
| 117 | + list ($this->view->snmptrapdError, $this->view->snmptrapdMessage)=$this->checkSnmpTrapd(); |
|
| 118 | 118 | |
| 119 | 119 | // List DB in $ressources |
| 120 | - $resources = array(); |
|
| 121 | - $allowed = array('mysql', 'pgsql'); |
|
| 120 | + $resources=array(); |
|
| 121 | + $allowed=array('mysql', 'pgsql'); |
|
| 122 | 122 | foreach (ResourceFactory::getResourceConfigs() as $name => $resource) { |
| 123 | 123 | if ($resource->get('type') === 'db' && in_array($resource->get('db'), $allowed)) { |
| 124 | - $resources[$name] = $name; |
|
| 124 | + $resources[$name]=$name; |
|
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
|
| 128 | + $this->view->tabs=$this->Module()->getConfigTabs()->activate('config'); |
|
| 129 | 129 | |
| 130 | 130 | // Check standard Icingaweb2 path |
| 131 | 131 | $this->view->icingaEtcWarn=0; |
@@ -134,9 +134,9 @@ discard block |
||
| 134 | 134 | { |
| 135 | 135 | $output=array(); |
| 136 | 136 | |
| 137 | - exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2_etc=" ',$output); |
|
| 137 | + exec('cat '.$this->module->getBaseDir().'/bin/trap_in.php | grep "\$icingaweb2_etc=" ', $output); |
|
| 138 | 138 | |
| 139 | - if (! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
| 139 | + if (!preg_match('#"'.$icingaweb2_etc.'"#', $output[0])) |
|
| 140 | 140 | { |
| 141 | 141 | $this->view->icingaEtcWarn=1; |
| 142 | 142 | $this->view->icingaweb2_etc=$icingaweb2_etc; |
@@ -144,23 +144,23 @@ discard block |
||
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Setup path for mini documentation |
| 147 | - $this->view->traps_in_config= PHP_BINARY . ' ' . $this->Module()->getBaseDir() . '/bin/trap_in.php'; |
|
| 147 | + $this->view->traps_in_config=PHP_BINARY.' '.$this->Module()->getBaseDir().'/bin/trap_in.php'; |
|
| 148 | 148 | |
| 149 | - $this->view->installer= $this->Module()->getBaseDir() . '/bin/installer.sh ' |
|
| 149 | + $this->view->installer=$this->Module()->getBaseDir().'/bin/installer.sh ' |
|
| 150 | 150 | . ' -c all ' |
| 151 | - . ' -d ' . $this->Module()->getBaseDir() |
|
| 152 | - . ' -p ' . PHP_BINARY |
|
| 153 | - . ' -a ' . exec('whoami') |
|
| 154 | - . ' -w ' . Icinga::app()->getConfigDir(); |
|
| 151 | + . ' -d '.$this->Module()->getBaseDir() |
|
| 152 | + . ' -p '.PHP_BINARY |
|
| 153 | + . ' -a '.exec('whoami') |
|
| 154 | + . ' -w '.Icinga::app()->getConfigDir(); |
|
| 155 | 155 | |
| 156 | 156 | // ******************* configuration form setup******************* |
| 157 | - $this->view->form = $form = new TrapsConfigForm(); |
|
| 157 | + $this->view->form=$form=new TrapsConfigForm(); |
|
| 158 | 158 | |
| 159 | 159 | // set default paths; |
| 160 | - $this->view->form->setPaths($this->Module()->getBaseDir(),Icinga::app()->getConfigDir()); |
|
| 160 | + $this->view->form->setPaths($this->Module()->getBaseDir(), Icinga::app()->getConfigDir()); |
|
| 161 | 161 | |
| 162 | 162 | // set default ido database |
| 163 | - $this->view->form->setDefaultIDODB($this->Config()->module('monitoring','backends')->get('icinga','resource')); |
|
| 163 | + $this->view->form->setDefaultIDODB($this->Config()->module('monitoring', 'backends')->get('icinga', 'resource')); |
|
| 164 | 164 | |
| 165 | 165 | // Make form handle request. |
| 166 | 166 | $form->setIniConfig($this->Config()) |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | public function createschemaAction() |
| 173 | 173 | { |
| 174 | 174 | $this->checkModuleConfigPermission(); |
| 175 | - $this->getTabs()->add('create_schema',array( |
|
| 175 | + $this->getTabs()->add('create_schema', array( |
|
| 176 | 176 | 'active' => true, |
| 177 | 177 | 'label' => $this->translate('Create Schema'), |
| 178 | 178 | 'url' => Url::fromRequest() |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | $dbName=$this->Config()->get('config', 'database'); |
| 194 | 194 | |
| 195 | 195 | try { |
| 196 | - $dbResource = ResourceFactory::getResourceConfig($dbName); |
|
| 196 | + $dbResource=ResourceFactory::getResourceConfig($dbName); |
|
| 197 | 197 | $dbType=$dbResource->get('db'); |
| 198 | 198 | switch ($dbType) { |
| 199 | 199 | case 'mysql': |
@@ -205,34 +205,34 @@ discard block |
||
| 205 | 205 | default: |
| 206 | 206 | throw new ConfigurationError('Unsuported database : '.$dbType); |
| 207 | 207 | } |
| 208 | - } catch (ConfigurationError $e ) |
|
| 208 | + } catch (ConfigurationError $e) |
|
| 209 | 209 | { |
| 210 | - printf("Database configuration error : %s",$e->getMessage()); |
|
| 210 | + printf("Database configuration error : %s", $e->getMessage()); |
|
| 211 | 211 | return; |
| 212 | 212 | } |
| 213 | 213 | printf('<pre>'); |
| 214 | - require_once $this->Module()->getBaseDir() .'/bin/trap_class.php'; |
|
| 214 | + require_once $this->Module()->getBaseDir().'/bin/trap_class.php'; |
|
| 215 | 215 | |
| 216 | 216 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 217 | 217 | $debug_level=4; |
| 218 | - $Trap = new Trap($icingaweb2_etc); |
|
| 219 | - $Trap->setLogging($debug_level,'display'); |
|
| 218 | + $Trap=new Trap($icingaweb2_etc); |
|
| 219 | + $Trap->setLogging($debug_level, 'display'); |
|
| 220 | 220 | |
| 221 | 221 | $prefix=$this->Config()->get('config', 'database_prefix'); |
| 222 | 222 | // schema file : <path>/SQL/schema_v<verion>.<dbtype> |
| 223 | - $schema=$this->Module()->getBaseDir() . |
|
| 224 | - '/SQL/schema_v'. $this->getModuleConfig()->getDbCurVersion() . '.' . $dbFileExt; |
|
| 223 | + $schema=$this->Module()->getBaseDir(). |
|
| 224 | + '/SQL/schema_v'.$this->getModuleConfig()->getDbCurVersion().'.'.$dbFileExt; |
|
| 225 | 225 | |
| 226 | - $Trap->trapsDB->create_schema($schema,$prefix); |
|
| 226 | + $Trap->trapsDB->create_schema($schema, $prefix); |
|
| 227 | 227 | echo '</pre>'; |
| 228 | 228 | } |
| 229 | - echo '<br><br>Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a>'; |
|
| 229 | + echo '<br><br>Return to <a href="'.Url::fromPath('trapdirector/settings').'" class="link-button icon-wrench"> settings page </a>'; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | public function updateschemaAction() |
| 233 | 233 | { |
| 234 | 234 | $this->checkModuleConfigPermission(); |
| 235 | - $this->getTabs()->add('get',array( |
|
| 235 | + $this->getTabs()->add('get', array( |
|
| 236 | 236 | 'active' => true, |
| 237 | 237 | 'label' => $this->translate('Update Schema'), |
| 238 | 238 | 'url' => Url::fromRequest() |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | |
| 242 | 242 | $dberror=$this->getDb(true); // Get DB in test mode |
| 243 | 243 | |
| 244 | - echo 'Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a><br><br>'; |
|
| 244 | + echo 'Return to <a href="'.Url::fromPath('trapdirector/settings').'" class="link-button icon-wrench"> settings page </a><br><br>'; |
|
| 245 | 245 | |
| 246 | 246 | if ($dberror[0] == 0) |
| 247 | 247 | { |
@@ -254,40 +254,40 @@ discard block |
||
| 254 | 254 | return; |
| 255 | 255 | } |
| 256 | 256 | // setup |
| 257 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 257 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
| 258 | 258 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 259 | 259 | $debug_level=4; |
| 260 | - $Trap = new Trap($icingaweb2_etc); |
|
| 260 | + $Trap=new Trap($icingaweb2_etc); |
|
| 261 | 261 | |
| 262 | 262 | |
| 263 | 263 | $prefix=$this->Config()->get('config', 'database_prefix'); |
| 264 | - $updateSchema=$this->Module()->getBaseDir() . '/SQL/'; |
|
| 264 | + $updateSchema=$this->Module()->getBaseDir().'/SQL/'; |
|
| 265 | 265 | |
| 266 | 266 | $target_version=$dberror[2]; |
| 267 | 267 | |
| 268 | 268 | if ($this->params->get('msgok') == null) { |
| 269 | 269 | // Check for messages and display if any |
| 270 | 270 | echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
| 271 | - $Trap->setLogging(2,'syslog'); |
|
| 272 | - $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
| 271 | + $Trap->setLogging(2, 'syslog'); |
|
| 272 | + $message=$Trap->trapsDB->update_schema($updateSchema, $target_version, $prefix, true); |
|
| 273 | 273 | if ($message != '') |
| 274 | 274 | { |
| 275 | 275 | echo 'Note :<br><pre>'; |
| 276 | 276 | echo $message; |
| 277 | 277 | echo '</pre>'; |
| 278 | 278 | echo '<br>'; |
| 279 | - echo '<a class="link-button" style="font-size:large;font-weight:bold" href="' . Url::fromPath('trapdirector/settings/updateschema') .'?msgok=1">Click here to update</a>'; |
|
| 279 | + echo '<a class="link-button" style="font-size:large;font-weight:bold" href="'.Url::fromPath('trapdirector/settings/updateschema').'?msgok=1">Click here to update</a>'; |
|
| 280 | 280 | echo '<br>'; |
| 281 | 281 | return; |
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | - $Trap->setLogging($debug_level,'display'); |
|
| 285 | + $Trap->setLogging($debug_level, 'display'); |
|
| 286 | 286 | |
| 287 | - echo 'Updating schema to '. $target_version . ': <br>'; |
|
| 287 | + echo 'Updating schema to '.$target_version.': <br>'; |
|
| 288 | 288 | echo '<pre>'; |
| 289 | 289 | |
| 290 | - $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix); |
|
| 290 | + $Trap->trapsDB->update_schema($updateSchema, $target_version, $prefix); |
|
| 291 | 291 | echo '</pre>'; |
| 292 | 292 | } |
| 293 | 293 | |
@@ -295,26 +295,26 @@ discard block |
||
| 295 | 295 | { |
| 296 | 296 | $psOutput=array(); |
| 297 | 297 | // First check is someone is listening to port 162. As not root, we can't have pid... |
| 298 | - exec('netstat -an |grep -E "udp.*:162"',$psOutput); |
|
| 298 | + exec('netstat -an |grep -E "udp.*:162"', $psOutput); |
|
| 299 | 299 | if (count($psOutput) == 0) |
| 300 | 300 | { |
| 301 | - return array(1,'Port UDP/162 is not open : snmptrapd must not be started'); |
|
| 301 | + return array(1, 'Port UDP/162 is not open : snmptrapd must not be started'); |
|
| 302 | 302 | } |
| 303 | 303 | $psOutput=array(); |
| 304 | - exec('ps fax |grep snmptrapd |grep -v grep',$psOutput); |
|
| 304 | + exec('ps fax |grep snmptrapd |grep -v grep', $psOutput); |
|
| 305 | 305 | if (count($psOutput) == 0) |
| 306 | 306 | { |
| 307 | - return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 307 | + return array(1, "UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 308 | 308 | } |
| 309 | 309 | // Assume there is only one line... TODO : see if there is a better way to do this |
| 310 | - $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
| 311 | - if (!preg_match('/-n/',$line)) |
|
| 312 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
| 313 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
| 314 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
| 315 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
| 316 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 310 | + $line=preg_replace('/^.*snmptrapd /', '', $psOutput[0]); |
|
| 311 | + if (!preg_match('/-n/', $line)) |
|
| 312 | + return array(1, 'snmptrapd has no -n option : '.$line); |
|
| 313 | + if (!preg_match('/-O[^ ]*n/', $line)) |
|
| 314 | + return array(1, 'snmptrapd has no -On option : '.$line); |
|
| 315 | + if (!preg_match('/-O[^ ]*e/', $line)) |
|
| 316 | + return array(1, 'snmptrapd has no -Oe option : '.$line); |
|
| 317 | 317 | |
| 318 | - return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
| 318 | + return array(0, 'snmptrapd listening to UDP/162, options : '.$line); |
|
| 319 | 319 | } |
| 320 | 320 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
| 59 | 59 | { |
| 60 | - // Paths of ini files |
|
| 60 | + // Paths of ini files |
|
| 61 | 61 | $this->icingaweb2_etc=$etc_dir; |
| 62 | 62 | $this->trap_module_config=$this->icingaweb2_etc."/modules/trapdirector/config.ini"; |
| 63 | 63 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
@@ -66,18 +66,18 @@ discard block |
||
| 66 | 66 | $this->logging = new Logging(); |
| 67 | 67 | if ($baseLogLevel != null) |
| 68 | 68 | { |
| 69 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 70 | - $this->logSetup=true; |
|
| 69 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 70 | + $this->logSetup=true; |
|
| 71 | 71 | } |
| 72 | 72 | else |
| 73 | - $this->logSetup=false; |
|
| 73 | + $this->logSetup=false; |
|
| 74 | 74 | $this->logging->log('Loggin started', INFO); |
| 75 | 75 | |
| 76 | 76 | // Get options from ini files |
| 77 | 77 | $trapConfig=parse_ini_file($this->trap_module_config,true); |
| 78 | 78 | if ($trapConfig == false) |
| 79 | 79 | { |
| 80 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 80 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 81 | 81 | } |
| 82 | 82 | $this->getMainOptions($trapConfig); // Get main options from ini file |
| 83 | 83 | $this->setupDatabase($trapConfig); // Setup database class |
@@ -109,26 +109,26 @@ discard block |
||
| 109 | 109 | */ |
| 110 | 110 | protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
| 111 | 111 | { |
| 112 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 113 | - { |
|
| 114 | - if ($message === null) |
|
| 115 | - { |
|
| 116 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 117 | - } |
|
| 118 | - $this->logging->log($message,$log_level,'syslog'); |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - else |
|
| 122 | - { |
|
| 123 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 124 | - return true; |
|
| 125 | - } |
|
| 112 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 113 | + { |
|
| 114 | + if ($message === null) |
|
| 115 | + { |
|
| 116 | + $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 117 | + } |
|
| 118 | + $this->logging->log($message,$log_level,'syslog'); |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + else |
|
| 122 | + { |
|
| 123 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 124 | + return true; |
|
| 125 | + } |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
| 129 | 129 | * Get options from ini file |
| 130 | 130 | * @param array $trap_config : ini file array |
| 131 | - */ |
|
| 131 | + */ |
|
| 132 | 132 | protected function getMainOptions($trapConfig) |
| 133 | 133 | { |
| 134 | 134 | |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | // API options |
| 148 | 148 | if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
| 149 | 149 | { |
| 150 | - $this->api_use=true; |
|
| 151 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 152 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 153 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 150 | + $this->api_use=true; |
|
| 151 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 152 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 153 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
@@ -160,45 +160,45 @@ discard block |
||
| 160 | 160 | */ |
| 161 | 161 | protected function setupDatabase($trapConfig) |
| 162 | 162 | { |
| 163 | - // Trap database |
|
| 164 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 165 | - { |
|
| 166 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 167 | - return; |
|
| 168 | - } |
|
| 169 | - $dbTrapName=$trapConfig['config']['database']; |
|
| 170 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 163 | + // Trap database |
|
| 164 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
| 165 | + { |
|
| 166 | + $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 167 | + return; |
|
| 168 | + } |
|
| 169 | + $dbTrapName=$trapConfig['config']['database']; |
|
| 170 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 171 | 171 | |
| 172 | 172 | if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
| 173 | - { |
|
| 174 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 175 | - return; |
|
| 176 | - } |
|
| 177 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 178 | - { |
|
| 179 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 180 | - return; |
|
| 181 | - } |
|
| 173 | + { |
|
| 174 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 175 | + return; |
|
| 176 | + } |
|
| 177 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 178 | + { |
|
| 179 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 183 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 184 | 184 | |
| 185 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 185 | + if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 186 | 186 | |
| 187 | - // IDO Database |
|
| 188 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 189 | - { |
|
| 190 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 191 | - } |
|
| 192 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 187 | + // IDO Database |
|
| 188 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 189 | + { |
|
| 190 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 191 | + } |
|
| 192 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 193 | 193 | |
| 194 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 195 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 196 | - { |
|
| 197 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 198 | - return; |
|
| 199 | - } |
|
| 194 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 195 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 196 | + { |
|
| 197 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 198 | + return; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 201 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | /** |
@@ -209,9 +209,9 @@ discard block |
||
| 209 | 209 | // Database options |
| 210 | 210 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
| 211 | 211 | { |
| 212 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 213 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 214 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 212 | + $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 213 | + $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 214 | + $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 215 | 215 | } |
| 216 | 216 | } |
| 217 | 217 | |
@@ -222,10 +222,10 @@ discard block |
||
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | - * Get data from db_config |
|
| 226 | - * @param $element string name of param |
|
| 227 | - * @return mixed : value (or null) |
|
| 228 | - */ |
|
| 225 | + * Get data from db_config |
|
| 226 | + * @param $element string name of param |
|
| 227 | + * @return mixed : value (or null) |
|
| 228 | + */ |
|
| 229 | 229 | protected function getDBConfig($element) |
| 230 | 230 | { |
| 231 | 231 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -243,15 +243,15 @@ discard block |
||
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** OBSOLETE Send log. Throws exception on critical error |
| 246 | - * @param string $message Message to log |
|
| 247 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 248 | - * @param string $destination file/syslog/display |
|
| 249 | - * @return void |
|
| 250 | - **/ |
|
| 246 | + * @param string $message Message to log |
|
| 247 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 248 | + * @param string $destination file/syslog/display |
|
| 249 | + * @return void |
|
| 250 | + **/ |
|
| 251 | 251 | public function trapLog( $message, $level, $destination ='') // OBSOLETE |
| 252 | 252 | { |
| 253 | 253 | // TODO : replace ref with $this->logging->log |
| 254 | - $this->logging->log($message, $level, $destination); |
|
| 254 | + $this->logging->log($message, $level, $destination); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
@@ -261,19 +261,19 @@ discard block |
||
| 261 | 261 | |
| 262 | 262 | protected function getAPI() |
| 263 | 263 | { |
| 264 | - if ($this->icinga2api == null) |
|
| 265 | - { |
|
| 266 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 267 | - } |
|
| 268 | - return $this->icinga2api; |
|
| 264 | + if ($this->icinga2api == null) |
|
| 265 | + { |
|
| 266 | + $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 267 | + } |
|
| 268 | + return $this->icinga2api; |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | |
| 272 | 272 | /** |
| 273 | 273 | * read data from stream |
| 274 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 275 | - * @return mixed array trap data or exception with error |
|
| 276 | - */ |
|
| 274 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 275 | + * @return mixed array trap data or exception with error |
|
| 276 | + */ |
|
| 277 | 277 | public function read_trap($stream='php://stdin') |
| 278 | 278 | { |
| 279 | 279 | //Read data from snmptrapd from stdin |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | |
| 282 | 282 | if ($input_stream === false) |
| 283 | 283 | { |
| 284 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 284 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 285 | 285 | $this->logging->log("Error reading stdin !",ERROR,''); |
| 286 | 286 | return null; // note : exception thrown by logging |
| 287 | 287 | } |
@@ -290,21 +290,21 @@ discard block |
||
| 290 | 290 | $this->receivingHost=chop(fgets($input_stream)); |
| 291 | 291 | if ($this->receivingHost === false) |
| 292 | 292 | { |
| 293 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 293 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 294 | 294 | $this->logging->log("Error reading Host !",ERROR,''); |
| 295 | 295 | } |
| 296 | 296 | // line 2 IP:port=>IP:port |
| 297 | 297 | $IP=chop(fgets($input_stream)); |
| 298 | 298 | if ($IP === false) |
| 299 | 299 | { |
| 300 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 300 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 301 | 301 | $this->logging->log("Error reading IP !",ERROR,''); |
| 302 | 302 | } |
| 303 | 303 | $matches=array(); |
| 304 | 304 | $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
| 305 | 305 | if ($ret_code===0 || $ret_code===false) |
| 306 | 306 | { |
| 307 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 307 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 308 | 308 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 309 | 309 | } |
| 310 | 310 | else |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | } |
| 326 | 326 | else |
| 327 | 327 | { |
| 328 | - 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')) |
|
| 328 | + 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')) |
|
| 329 | 329 | { |
| 330 | 330 | $this->trap_data['trap_oid']=$matches[2]; |
| 331 | 331 | } |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | |
| 342 | 342 | if ($this->trap_data['trap_oid']=='unknown') |
| 343 | 343 | { |
| 344 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 344 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 345 | 345 | $this->logging->log('no trap oid found',ERROR,''); |
| 346 | 346 | } |
| 347 | 347 | |
@@ -371,9 +371,9 @@ discard block |
||
| 371 | 371 | |
| 372 | 372 | /** |
| 373 | 373 | * Translate oid into array(MIB,Name) |
| 374 | - * @param $oid string oid to translate |
|
| 375 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 376 | - */ |
|
| 374 | + * @param $oid string oid to translate |
|
| 375 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 376 | + */ |
|
| 377 | 377 | public function translateOID($oid) |
| 378 | 378 | { |
| 379 | 379 | // try from database |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | |
| 407 | 407 | // Try to get oid name from snmptranslate |
| 408 | 408 | $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
| 409 | - ' '.$oid); |
|
| 409 | + ' '.$oid); |
|
| 410 | 410 | $matches=array(); |
| 411 | 411 | $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
| 412 | 412 | if ($ret_code===0 || $ret_code === false) { |
@@ -419,9 +419,9 @@ discard block |
||
| 419 | 419 | |
| 420 | 420 | /** |
| 421 | 421 | * Erase old trap records |
| 422 | - * @param integer $days : erase traps when more than $days old |
|
| 423 | - * @return integer : number of lines deleted |
|
| 424 | - **/ |
|
| 422 | + * @param integer $days : erase traps when more than $days old |
|
| 423 | + * @return integer : number of lines deleted |
|
| 424 | + **/ |
|
| 425 | 425 | public function eraseOldTraps($days=0) |
| 426 | 426 | { |
| 427 | 427 | if ($days==0) |
@@ -446,72 +446,72 @@ discard block |
||
| 446 | 446 | public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
| 447 | 447 | { |
| 448 | 448 | |
| 449 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 449 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 450 | 450 | |
| 451 | - // add date time |
|
| 452 | - $insert_col ='date_received,status'; |
|
| 453 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 451 | + // add date time |
|
| 452 | + $insert_col ='date_received,status'; |
|
| 453 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 454 | 454 | |
| 455 | - if ($sourceIP !=null) |
|
| 456 | - { |
|
| 457 | - $insert_col .=',source_ip'; |
|
| 458 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 459 | - } |
|
| 460 | - if ($trapoid !=null) |
|
| 461 | - { |
|
| 462 | - $insert_col .=',trap_oid'; |
|
| 463 | - $insert_val .=",'". $trapoid ."'"; |
|
| 464 | - } |
|
| 465 | - $insert_col .=',status_detail'; |
|
| 466 | - $insert_val .=",'". $message ."'"; |
|
| 455 | + if ($sourceIP !=null) |
|
| 456 | + { |
|
| 457 | + $insert_col .=',source_ip'; |
|
| 458 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 459 | + } |
|
| 460 | + if ($trapoid !=null) |
|
| 461 | + { |
|
| 462 | + $insert_col .=',trap_oid'; |
|
| 463 | + $insert_val .=",'". $trapoid ."'"; |
|
| 464 | + } |
|
| 465 | + $insert_col .=',status_detail'; |
|
| 466 | + $insert_val .=",'". $message ."'"; |
|
| 467 | 467 | |
| 468 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 468 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 469 | 469 | |
| 470 | - switch ($this->trapsDB->trapDBType) |
|
| 471 | - { |
|
| 472 | - case 'pgsql': |
|
| 473 | - $sql .= ' RETURNING id;'; |
|
| 474 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 475 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 476 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 477 | - } |
|
| 478 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 479 | - // Get last id to insert oid/values in secondary table |
|
| 480 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 470 | + switch ($this->trapsDB->trapDBType) |
|
| 471 | + { |
|
| 472 | + case 'pgsql': |
|
| 473 | + $sql .= ' RETURNING id;'; |
|
| 474 | + $this->logging->log('sql : '.$sql,INFO); |
|
| 475 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 476 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 477 | + } |
|
| 478 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 479 | + // Get last id to insert oid/values in secondary table |
|
| 480 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 481 | 481 | |
| 482 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 483 | - } |
|
| 484 | - if (! isset($inserted_id_ret['id'])) { |
|
| 485 | - $this->logging->log('Error getting id',1,''); |
|
| 486 | - } |
|
| 487 | - $this->trap_id=$inserted_id_ret['id']; |
|
| 488 | - break; |
|
| 489 | - case 'mysql': |
|
| 490 | - $sql .= ';'; |
|
| 491 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 492 | - if ($db_conn->query($sql) === false) { |
|
| 493 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 494 | - } |
|
| 495 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 496 | - // Get last id to insert oid/values in secondary table |
|
| 497 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 498 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 499 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 500 | - } |
|
| 482 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 483 | + } |
|
| 484 | + if (! isset($inserted_id_ret['id'])) { |
|
| 485 | + $this->logging->log('Error getting id',1,''); |
|
| 486 | + } |
|
| 487 | + $this->trap_id=$inserted_id_ret['id']; |
|
| 488 | + break; |
|
| 489 | + case 'mysql': |
|
| 490 | + $sql .= ';'; |
|
| 491 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 492 | + if ($db_conn->query($sql) === false) { |
|
| 493 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 494 | + } |
|
| 495 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 496 | + // Get last id to insert oid/values in secondary table |
|
| 497 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 498 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 499 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 503 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 504 | - $this->trap_id=$inserted_id; |
|
| 505 | - break; |
|
| 506 | - default: |
|
| 507 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 508 | - } |
|
| 502 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 503 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 504 | + $this->trap_id=$inserted_id; |
|
| 505 | + break; |
|
| 506 | + default: |
|
| 507 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 510 | + $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | /** Write trap data to trap database |
| 514 | - */ |
|
| 514 | + */ |
|
| 515 | 515 | public function writeTrapToDB() |
| 516 | 516 | { |
| 517 | 517 | |
@@ -609,10 +609,10 @@ discard block |
||
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | /** Get rules from rule database with ip and oid |
| 612 | - * @param $ip string ipv4 or ipv6 |
|
| 613 | - * @param $oid string oid in numeric |
|
| 614 | - * @return mixed : PDO object or false |
|
| 615 | - */ |
|
| 612 | + * @param $ip string ipv4 or ipv6 |
|
| 613 | + * @param $oid string oid in numeric |
|
| 614 | + * @return mixed : PDO object or false |
|
| 615 | + */ |
|
| 616 | 616 | protected function getRules($ip,$oid) |
| 617 | 617 | { |
| 618 | 618 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -671,9 +671,9 @@ discard block |
||
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | /** Add rule match to rule |
| 674 | - * @param id int : rule id |
|
| 675 | - * @param set int : value to set |
|
| 676 | - */ |
|
| 674 | + * @param id int : rule id |
|
| 675 | + * @param set int : value to set |
|
| 676 | + */ |
|
| 677 | 677 | protected function add_rule_match($id, $set) |
| 678 | 678 | { |
| 679 | 679 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -690,53 +690,53 @@ discard block |
||
| 690 | 690 | * @param integer $state numerical staus |
| 691 | 691 | * @param string $display |
| 692 | 692 | * @returnn bool true is service check was sent without error |
| 693 | - */ |
|
| 693 | + */ |
|
| 694 | 694 | public function serviceCheckResult($host,$service,$state,$display) |
| 695 | 695 | { |
| 696 | - if ($this->api_use === false) |
|
| 697 | - { |
|
| 698 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 699 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 700 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 696 | + if ($this->api_use === false) |
|
| 697 | + { |
|
| 698 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 699 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 700 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 701 | 701 | |
| 702 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 703 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 704 | - return true; |
|
| 705 | - } |
|
| 706 | - else |
|
| 707 | - { |
|
| 708 | - $api = $this->getAPI(); |
|
| 709 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 710 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
| 711 | - if ($retcode == false) |
|
| 712 | - { |
|
| 713 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 714 | - return false; |
|
| 715 | - } |
|
| 716 | - else |
|
| 717 | - { |
|
| 718 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 719 | - return true; |
|
| 720 | - } |
|
| 721 | - } |
|
| 702 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 703 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 704 | + return true; |
|
| 705 | + } |
|
| 706 | + else |
|
| 707 | + { |
|
| 708 | + $api = $this->getAPI(); |
|
| 709 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 710 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
| 711 | + if ($retcode == false) |
|
| 712 | + { |
|
| 713 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 714 | + return false; |
|
| 715 | + } |
|
| 716 | + else |
|
| 717 | + { |
|
| 718 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 719 | + return true; |
|
| 720 | + } |
|
| 721 | + } |
|
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | public function getHostByIP($ip) |
| 725 | 725 | { |
| 726 | - $api = $this->getAPI(); |
|
| 727 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 728 | - return $api->getHostByIP($ip); |
|
| 726 | + $api = $this->getAPI(); |
|
| 727 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 728 | + return $api->getHostByIP($ip); |
|
| 729 | 729 | } |
| 730 | 730 | |
| 731 | 731 | /** Resolve display. |
| 732 | - * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
| 733 | - * @param $display string |
|
| 734 | - * @return string display |
|
| 735 | - */ |
|
| 732 | + * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
| 733 | + * @param $display string |
|
| 734 | + * @return string display |
|
| 735 | + */ |
|
| 736 | 736 | protected function applyDisplay($display) |
| 737 | 737 | { |
| 738 | - $matches=array(); |
|
| 739 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 738 | + $matches=array(); |
|
| 739 | + while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 740 | 740 | { |
| 741 | 741 | $oid=$matches[1]; |
| 742 | 742 | $found=0; |
@@ -796,11 +796,11 @@ discard block |
||
| 796 | 796 | |
| 797 | 797 | if ($rule[$item] == '(') |
| 798 | 798 | { // grouping |
| 799 | - $item++; |
|
| 799 | + $item++; |
|
| 800 | 800 | $start=$item; |
| 801 | 801 | $parenthesis_count=0; |
| 802 | 802 | while (($item < strlen($rule)) // Not end of string AND |
| 803 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 803 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 804 | 804 | { |
| 805 | 805 | if ($rule[$item] == '"' ) |
| 806 | 806 | { // pass through string |
@@ -808,14 +808,14 @@ discard block |
||
| 808 | 808 | $item=$this->eval_getNext($rule,$item,'"'); |
| 809 | 809 | } |
| 810 | 810 | else{ |
| 811 | - if ($rule[$item] == '(') |
|
| 812 | - { |
|
| 813 | - $parenthesis_count++; |
|
| 814 | - } |
|
| 815 | - if ($rule[$item] == ')') |
|
| 816 | - { |
|
| 817 | - $parenthesis_count--; |
|
| 818 | - } |
|
| 811 | + if ($rule[$item] == '(') |
|
| 812 | + { |
|
| 813 | + $parenthesis_count++; |
|
| 814 | + } |
|
| 815 | + if ($rule[$item] == ')') |
|
| 816 | + { |
|
| 817 | + $parenthesis_count--; |
|
| 818 | + } |
|
| 819 | 819 | $item++; |
| 820 | 820 | } |
| 821 | 821 | } |
@@ -867,24 +867,24 @@ discard block |
||
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | /** Evaluation : makes token and evaluate. |
| 870 | - * Public function for expressions testing |
|
| 871 | - * accepts : < > = <= >= != (typec = 0) |
|
| 872 | - * operators : & | (typec=1) |
|
| 873 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 874 | - * comparison int vs strings will return null (error) |
|
| 875 | - * return : bool or null on error |
|
| 876 | - */ |
|
| 870 | + * Public function for expressions testing |
|
| 871 | + * accepts : < > = <= >= != (typec = 0) |
|
| 872 | + * operators : & | (typec=1) |
|
| 873 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 874 | + * comparison int vs strings will return null (error) |
|
| 875 | + * return : bool or null on error |
|
| 876 | + */ |
|
| 877 | 877 | public function evaluation($rule,&$item) |
| 878 | 878 | { |
| 879 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 879 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 880 | 880 | if ( $rule[$item] == '!') // If '!' found, negate next expression. |
| 881 | 881 | { |
| 882 | - $negate=true; |
|
| 883 | - $item++; |
|
| 882 | + $negate=true; |
|
| 883 | + $item++; |
|
| 884 | 884 | } |
| 885 | 885 | else |
| 886 | 886 | { |
| 887 | - $negate=false; |
|
| 887 | + $negate=false; |
|
| 888 | 888 | } |
| 889 | 889 | // First element : number, string or () |
| 890 | 890 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
@@ -904,24 +904,24 @@ discard block |
||
| 904 | 904 | // Third element : number, string or () |
| 905 | 905 | if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
| 906 | 906 | { |
| 907 | - $item++; |
|
| 908 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 909 | - $val2= ! $this->evaluation($rule,$item); |
|
| 910 | - $type2=2; // result is a boolean |
|
| 907 | + $item++; |
|
| 908 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 909 | + $val2= ! $this->evaluation($rule,$item); |
|
| 910 | + $type2=2; // result is a boolean |
|
| 911 | 911 | } |
| 912 | 912 | else |
| 913 | 913 | { |
| 914 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 914 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 915 | 915 | } |
| 916 | 916 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 917 | 917 | |
| 918 | 918 | if ($type1!=$type2) // cannot compare different types |
| 919 | 919 | { |
| 920 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
| 920 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
| 921 | 921 | } |
| 922 | 922 | if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
| 923 | 923 | { |
| 924 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 924 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 925 | 925 | } |
| 926 | 926 | |
| 927 | 927 | switch ($comp){ |
@@ -979,9 +979,9 @@ discard block |
||
| 979 | 979 | } |
| 980 | 980 | |
| 981 | 981 | /** Evaluation rule (uses eval_* functions recursively) |
| 982 | - * @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) ) |
|
| 983 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 984 | - */ |
|
| 982 | + * @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) ) |
|
| 983 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 984 | + */ |
|
| 985 | 985 | |
| 986 | 986 | protected function eval_rule($rule) |
| 987 | 987 | { |
@@ -1026,7 +1026,7 @@ discard block |
||
| 1026 | 1026 | } |
| 1027 | 1027 | if ($found==0) |
| 1028 | 1028 | { // OID not found : throw error |
| 1029 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
| 1029 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
| 1030 | 1030 | } |
| 1031 | 1031 | } |
| 1032 | 1032 | $item=0; |
@@ -1037,7 +1037,7 @@ discard block |
||
| 1037 | 1037 | } |
| 1038 | 1038 | |
| 1039 | 1039 | /** Match rules for current trap and do action |
| 1040 | - */ |
|
| 1040 | + */ |
|
| 1041 | 1041 | public function applyRules() |
| 1042 | 1042 | { |
| 1043 | 1043 | $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
@@ -1074,12 +1074,12 @@ discard block |
||
| 1074 | 1074 | { |
| 1075 | 1075 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 1076 | 1076 | { |
| 1077 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1077 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1078 | 1078 | } |
| 1079 | 1079 | else |
| 1080 | 1080 | { |
| 1081 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1082 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1081 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1082 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1083 | 1083 | } |
| 1084 | 1084 | } |
| 1085 | 1085 | else |
@@ -1096,15 +1096,15 @@ discard block |
||
| 1096 | 1096 | $this->logging->log('action NOK : '.$action,INFO ); |
| 1097 | 1097 | if ($action >= 0) |
| 1098 | 1098 | { |
| 1099 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 1100 | - { |
|
| 1101 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1102 | - } |
|
| 1103 | - else |
|
| 1104 | - { |
|
| 1105 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1106 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1107 | - } |
|
| 1099 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 1100 | + { |
|
| 1101 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1102 | + } |
|
| 1103 | + else |
|
| 1104 | + { |
|
| 1105 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1106 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1107 | + } |
|
| 1108 | 1108 | } |
| 1109 | 1109 | else |
| 1110 | 1110 | { |
@@ -1127,9 +1127,9 @@ discard block |
||
| 1127 | 1127 | } |
| 1128 | 1128 | catch (Exception $e) |
| 1129 | 1129 | { |
| 1130 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 1131 | - $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 1132 | - $this->trap_data['status']='error'; |
|
| 1130 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 1131 | + $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 1132 | + $this->trap_data['status']='error'; |
|
| 1133 | 1133 | } |
| 1134 | 1134 | |
| 1135 | 1135 | } |
@@ -1144,8 +1144,8 @@ discard block |
||
| 1144 | 1144 | } |
| 1145 | 1145 | |
| 1146 | 1146 | /** Add Time a action to rule |
| 1147 | - * @param string $time : time to process to insert in SQL |
|
| 1148 | - */ |
|
| 1147 | + * @param string $time : time to process to insert in SQL |
|
| 1148 | + */ |
|
| 1149 | 1149 | public function add_rule_final($time) |
| 1150 | 1150 | { |
| 1151 | 1151 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -1162,9 +1162,9 @@ discard block |
||
| 1162 | 1162 | /*********** UTILITIES *********************/ |
| 1163 | 1163 | |
| 1164 | 1164 | /** reset service to OK after time defined in rule |
| 1165 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 1166 | - * @return integer : not in use |
|
| 1167 | - **/ |
|
| 1165 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 1166 | + * @return integer : not in use |
|
| 1167 | + **/ |
|
| 1168 | 1168 | public function reset_services() |
| 1169 | 1169 | { |
| 1170 | 1170 | // Get all services not in 'ok' state |
@@ -1236,105 +1236,105 @@ discard block |
||
| 1236 | 1236 | $description=$db_conn->quote($description); |
| 1237 | 1237 | if (isset($this->dbOidIndex[$oid])) |
| 1238 | 1238 | { |
| 1239 | - if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 1240 | - { // newly created. |
|
| 1241 | - return 0; |
|
| 1242 | - } |
|
| 1239 | + if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 1240 | + { // newly created. |
|
| 1241 | + return 0; |
|
| 1242 | + } |
|
| 1243 | 1243 | if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
| 1244 | - $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 1245 | - $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 1246 | - //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 1247 | - //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 1248 | - //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 1249 | - //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 1250 | - //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 1251 | - ) |
|
| 1244 | + $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 1245 | + $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 1246 | + //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 1247 | + //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 1248 | + //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 1249 | + //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 1250 | + //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 1251 | + ) |
|
| 1252 | 1252 | { // Do update |
| 1253 | - $sql='UPDATE '.$this->db_prefix.'mib_cache SET '. |
|
| 1254 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 1255 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 1256 | - ' WHERE id= :id'; |
|
| 1257 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 1253 | + $sql='UPDATE '.$this->db_prefix.'mib_cache SET '. |
|
| 1254 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 1255 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 1256 | + ' WHERE id= :id'; |
|
| 1257 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 1258 | 1258 | |
| 1259 | - $sqlParam=array( |
|
| 1260 | - ':name' => $name, |
|
| 1261 | - ':type' => $type, |
|
| 1262 | - ':mib' => $mib, |
|
| 1263 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1264 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1265 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1266 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1267 | - ':description' => ($description==null)?'null':$description, |
|
| 1268 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 1269 | - ); |
|
| 1259 | + $sqlParam=array( |
|
| 1260 | + ':name' => $name, |
|
| 1261 | + ':type' => $type, |
|
| 1262 | + ':mib' => $mib, |
|
| 1263 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1264 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1265 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1266 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1267 | + ':description' => ($description==null)?'null':$description, |
|
| 1268 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 1269 | + ); |
|
| 1270 | 1270 | |
| 1271 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1272 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 1273 | - } |
|
| 1274 | - $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1271 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1272 | + $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 1273 | + } |
|
| 1274 | + $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1275 | 1275 | return 1; |
| 1276 | 1276 | } |
| 1277 | 1277 | else |
| 1278 | 1278 | { |
| 1279 | - $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1280 | - return 0; |
|
| 1279 | + $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1280 | + return 0; |
|
| 1281 | 1281 | } |
| 1282 | 1282 | } |
| 1283 | - // create new OID. |
|
| 1283 | + // create new OID. |
|
| 1284 | 1284 | |
| 1285 | 1285 | // Insert data |
| 1286 | 1286 | |
| 1287 | 1287 | $sql='INSERT INTO '.$this->db_prefix.'mib_cache '. |
| 1288 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 1289 | - ', syntax, type_enum , description ) ' . |
|
| 1290 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 1291 | - ', :syntax, :type_enum, :description )'; |
|
| 1288 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 1289 | + ', syntax, type_enum , description ) ' . |
|
| 1290 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 1291 | + ', :syntax, :type_enum, :description )'; |
|
| 1292 | 1292 | |
| 1293 | 1293 | if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
| 1294 | 1294 | |
| 1295 | 1295 | $sqlQuery=$db_conn->prepare($sql); |
| 1296 | 1296 | |
| 1297 | 1297 | $sqlParam=array( |
| 1298 | - ':oid' => $oid, |
|
| 1299 | - ':name' => $name, |
|
| 1300 | - ':type' => $type, |
|
| 1301 | - ':mib' => $mib, |
|
| 1302 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1303 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1304 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1305 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1306 | - ':description' => ($description==null)?'null':$description |
|
| 1298 | + ':oid' => $oid, |
|
| 1299 | + ':name' => $name, |
|
| 1300 | + ':type' => $type, |
|
| 1301 | + ':mib' => $mib, |
|
| 1302 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1303 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1304 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1305 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1306 | + ':description' => ($description==null)?'null':$description |
|
| 1307 | 1307 | ); |
| 1308 | 1308 | |
| 1309 | 1309 | if ($sqlQuery->execute($sqlParam) === false) { |
| 1310 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 1310 | + $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 1311 | 1311 | } |
| 1312 | 1312 | |
| 1313 | 1313 | switch ($this->trapsDB->trapDBType) |
| 1314 | 1314 | { |
| 1315 | - case 'pgsql': |
|
| 1316 | - // Get last id to insert oid/values in secondary table |
|
| 1317 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 1318 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 1319 | - } |
|
| 1320 | - if (! isset($inserted_id_ret['id'])) { |
|
| 1321 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 1322 | - } |
|
| 1323 | - $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 1324 | - break; |
|
| 1325 | - case 'mysql': |
|
| 1326 | - // Get last id to insert oid/values in secondary table |
|
| 1327 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 1328 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1329 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 1330 | - } |
|
| 1315 | + case 'pgsql': |
|
| 1316 | + // Get last id to insert oid/values in secondary table |
|
| 1317 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 1318 | + $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 1319 | + } |
|
| 1320 | + if (! isset($inserted_id_ret['id'])) { |
|
| 1321 | + $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 1322 | + } |
|
| 1323 | + $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 1324 | + break; |
|
| 1325 | + case 'mysql': |
|
| 1326 | + // Get last id to insert oid/values in secondary table |
|
| 1327 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 1328 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1329 | + $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 1330 | + } |
|
| 1331 | 1331 | |
| 1332 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 1333 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1334 | - $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 1335 | - break; |
|
| 1336 | - default: |
|
| 1337 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 1332 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 1333 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1334 | + $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 1335 | + break; |
|
| 1336 | + default: |
|
| 1337 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 1338 | 1338 | } |
| 1339 | 1339 | |
| 1340 | 1340 | // Set as newly created. |
@@ -1342,148 +1342,148 @@ discard block |
||
| 1342 | 1342 | return 2; |
| 1343 | 1343 | } |
| 1344 | 1344 | |
| 1345 | - /** |
|
| 1346 | - * create or update (with check_existing = true) objects of trap |
|
| 1347 | - * @param string $trapOID : trap oid |
|
| 1348 | - * @param string $trapmib : mib of trap |
|
| 1349 | - * @param array $objects : array of objects name (without MIB) |
|
| 1350 | - * @param bool $check_existing : check instead of create |
|
| 1351 | - */ |
|
| 1345 | + /** |
|
| 1346 | + * create or update (with check_existing = true) objects of trap |
|
| 1347 | + * @param string $trapOID : trap oid |
|
| 1348 | + * @param string $trapmib : mib of trap |
|
| 1349 | + * @param array $objects : array of objects name (without MIB) |
|
| 1350 | + * @param bool $check_existing : check instead of create |
|
| 1351 | + */ |
|
| 1352 | 1352 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
| 1353 | 1353 | { |
| 1354 | - $dbObjects=null; // cache of objects for trap in db |
|
| 1355 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 1354 | + $dbObjects=null; // cache of objects for trap in db |
|
| 1355 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 1356 | 1356 | |
| 1357 | - // Get id of trapmib. |
|
| 1357 | + // Get id of trapmib. |
|
| 1358 | 1358 | |
| 1359 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 1360 | - if ($check_existing === true) |
|
| 1361 | - { |
|
| 1362 | - // Get all objects |
|
| 1363 | - $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 1364 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 1365 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1366 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 1367 | - } |
|
| 1368 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 1359 | + $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 1360 | + if ($check_existing === true) |
|
| 1361 | + { |
|
| 1362 | + // Get all objects |
|
| 1363 | + $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 1364 | + $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 1365 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 1366 | + $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 1367 | + } |
|
| 1368 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 1369 | 1369 | |
| 1370 | - foreach ($dbObjectsRaw as $val) |
|
| 1371 | - { |
|
| 1372 | - $dbObjects[$val['object_id']]=1; |
|
| 1373 | - } |
|
| 1374 | - } |
|
| 1375 | - foreach ($objects as $object) |
|
| 1376 | - { |
|
| 1377 | - $match=$snmptrans=array(); |
|
| 1378 | - $retVal=0; |
|
| 1379 | - $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 1380 | - $tmpdesc='';$indesc=false; |
|
| 1370 | + foreach ($dbObjectsRaw as $val) |
|
| 1371 | + { |
|
| 1372 | + $dbObjects[$val['object_id']]=1; |
|
| 1373 | + } |
|
| 1374 | + } |
|
| 1375 | + foreach ($objects as $object) |
|
| 1376 | + { |
|
| 1377 | + $match=$snmptrans=array(); |
|
| 1378 | + $retVal=0; |
|
| 1379 | + $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 1380 | + $tmpdesc='';$indesc=false; |
|
| 1381 | 1381 | |
| 1382 | - $objMib=$trapmib; |
|
| 1383 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1384 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1385 | - if ($retVal!=0) |
|
| 1386 | - { |
|
| 1387 | - // Maybe not trap mib, search with IR |
|
| 1388 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1389 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1390 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 1391 | - { // Not found -> continue with warning |
|
| 1392 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 1393 | - continue; |
|
| 1394 | - } |
|
| 1395 | - $objMib=$match[1]; |
|
| 1382 | + $objMib=$trapmib; |
|
| 1383 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1384 | + ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1385 | + if ($retVal!=0) |
|
| 1386 | + { |
|
| 1387 | + // Maybe not trap mib, search with IR |
|
| 1388 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1389 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1390 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 1391 | + { // Not found -> continue with warning |
|
| 1392 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 1393 | + continue; |
|
| 1394 | + } |
|
| 1395 | + $objMib=$match[1]; |
|
| 1396 | 1396 | |
| 1397 | - // Do the snmptranslate again. |
|
| 1398 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1399 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 1400 | - if ($retVal!=0) { |
|
| 1401 | - $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 1402 | - } |
|
| 1397 | + // Do the snmptranslate again. |
|
| 1398 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1399 | + ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 1400 | + if ($retVal!=0) { |
|
| 1401 | + $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 1402 | + } |
|
| 1403 | 1403 | |
| 1404 | - } |
|
| 1405 | - foreach ($snmptrans as $line) |
|
| 1406 | - { |
|
| 1407 | - if ($indesc===true) |
|
| 1408 | - { |
|
| 1409 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 1410 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 1411 | - { |
|
| 1412 | - $objDesc = $tmpdesc . $match[1]; |
|
| 1413 | - $indesc=false; |
|
| 1414 | - } |
|
| 1415 | - $tmpdesc.=$line; |
|
| 1416 | - continue; |
|
| 1417 | - } |
|
| 1418 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 1419 | - { |
|
| 1420 | - $objOid=$line; |
|
| 1421 | - continue; |
|
| 1422 | - } |
|
| 1423 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 1424 | - { |
|
| 1425 | - $objSyntax=$match[1]; |
|
| 1426 | - $objEnum=$match[2]; |
|
| 1427 | - continue; |
|
| 1428 | - } |
|
| 1429 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 1430 | - { |
|
| 1431 | - $objSyntax=$match[1]; |
|
| 1432 | - continue; |
|
| 1433 | - } |
|
| 1434 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 1435 | - { |
|
| 1436 | - $objDispHint=$match[1]; |
|
| 1437 | - continue; |
|
| 1438 | - } |
|
| 1439 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 1440 | - { |
|
| 1441 | - $objDesc=$match[1]; |
|
| 1442 | - continue; |
|
| 1443 | - } |
|
| 1444 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 1445 | - { |
|
| 1446 | - $tmpdesc=$match[1]; |
|
| 1447 | - $indesc=true; |
|
| 1448 | - continue; |
|
| 1449 | - } |
|
| 1450 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 1451 | - { |
|
| 1452 | - $objTc=$match[1]; |
|
| 1453 | - continue; |
|
| 1454 | - } |
|
| 1455 | - } |
|
| 1456 | - $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 1457 | - //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 1458 | - // Update |
|
| 1459 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 1404 | + } |
|
| 1405 | + foreach ($snmptrans as $line) |
|
| 1406 | + { |
|
| 1407 | + if ($indesc===true) |
|
| 1408 | + { |
|
| 1409 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 1410 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
| 1411 | + { |
|
| 1412 | + $objDesc = $tmpdesc . $match[1]; |
|
| 1413 | + $indesc=false; |
|
| 1414 | + } |
|
| 1415 | + $tmpdesc.=$line; |
|
| 1416 | + continue; |
|
| 1417 | + } |
|
| 1418 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 1419 | + { |
|
| 1420 | + $objOid=$line; |
|
| 1421 | + continue; |
|
| 1422 | + } |
|
| 1423 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 1424 | + { |
|
| 1425 | + $objSyntax=$match[1]; |
|
| 1426 | + $objEnum=$match[2]; |
|
| 1427 | + continue; |
|
| 1428 | + } |
|
| 1429 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 1430 | + { |
|
| 1431 | + $objSyntax=$match[1]; |
|
| 1432 | + continue; |
|
| 1433 | + } |
|
| 1434 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 1435 | + { |
|
| 1436 | + $objDispHint=$match[1]; |
|
| 1437 | + continue; |
|
| 1438 | + } |
|
| 1439 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 1440 | + { |
|
| 1441 | + $objDesc=$match[1]; |
|
| 1442 | + continue; |
|
| 1443 | + } |
|
| 1444 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 1445 | + { |
|
| 1446 | + $tmpdesc=$match[1]; |
|
| 1447 | + $indesc=true; |
|
| 1448 | + continue; |
|
| 1449 | + } |
|
| 1450 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 1451 | + { |
|
| 1452 | + $objTc=$match[1]; |
|
| 1453 | + continue; |
|
| 1454 | + } |
|
| 1455 | + } |
|
| 1456 | + $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 1457 | + //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 1458 | + // Update |
|
| 1459 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 1460 | 1460 | |
| 1461 | - if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 1462 | - { // if link exists, continue |
|
| 1463 | - $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 1464 | - continue; |
|
| 1465 | - } |
|
| 1466 | - if ($check_existing === true) |
|
| 1467 | - { |
|
| 1468 | - // TODO : check link trap - objects exists, mark them. |
|
| 1469 | - } |
|
| 1470 | - // Associate in object table |
|
| 1471 | - $sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 1472 | - 'values (:trap_id, :object_id)'; |
|
| 1473 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 1474 | - $sqlParam=array( |
|
| 1475 | - ':trap_id' => $trapId, |
|
| 1476 | - ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 1477 | - ); |
|
| 1461 | + if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 1462 | + { // if link exists, continue |
|
| 1463 | + $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 1464 | + continue; |
|
| 1465 | + } |
|
| 1466 | + if ($check_existing === true) |
|
| 1467 | + { |
|
| 1468 | + // TODO : check link trap - objects exists, mark them. |
|
| 1469 | + } |
|
| 1470 | + // Associate in object table |
|
| 1471 | + $sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 1472 | + 'values (:trap_id, :object_id)'; |
|
| 1473 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 1474 | + $sqlParam=array( |
|
| 1475 | + ':trap_id' => $trapId, |
|
| 1476 | + ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 1477 | + ); |
|
| 1478 | 1478 | |
| 1479 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1480 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 1481 | - } |
|
| 1482 | - } |
|
| 1483 | - if ($check_existing === true) |
|
| 1484 | - { |
|
| 1485 | - // TODO : remove link trap - objects that wasn't marked. |
|
| 1486 | - } |
|
| 1479 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 1480 | + $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 1481 | + } |
|
| 1482 | + } |
|
| 1483 | + if ($check_existing === true) |
|
| 1484 | + { |
|
| 1485 | + // TODO : remove link trap - objects that wasn't marked. |
|
| 1486 | + } |
|
| 1487 | 1487 | |
| 1488 | 1488 | } |
| 1489 | 1489 | |
@@ -1493,7 +1493,7 @@ discard block |
||
| 1493 | 1493 | * @param boolean $check_change : Force check of trap params & objects |
| 1494 | 1494 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
| 1495 | 1495 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
| 1496 | - */ |
|
| 1496 | + */ |
|
| 1497 | 1497 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
| 1498 | 1498 | { |
| 1499 | 1499 | // Timing |
@@ -1545,7 +1545,7 @@ discard block |
||
| 1545 | 1545 | |
| 1546 | 1546 | for ($curElement=0;$curElement < $numElements;$curElement++) |
| 1547 | 1547 | { |
| 1548 | - $time_1= microtime(true); |
|
| 1548 | + $time_1= microtime(true); |
|
| 1549 | 1549 | if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
| 1550 | 1550 | { // echo a . every 2 sec |
| 1551 | 1551 | echo '.'; |
@@ -1563,8 +1563,8 @@ discard block |
||
| 1563 | 1563 | // Get oid or pass if not found |
| 1564 | 1564 | if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
| 1565 | 1565 | { |
| 1566 | - $time_parse1 += microtime(true) - $time_1; |
|
| 1567 | - $time_parse1N ++; |
|
| 1566 | + $time_parse1 += microtime(true) - $time_1; |
|
| 1567 | + $time_parse1N ++; |
|
| 1568 | 1568 | continue; |
| 1569 | 1569 | } |
| 1570 | 1570 | $oid=$this->objectsAll[$curElement]; |
@@ -1575,7 +1575,7 @@ discard block |
||
| 1575 | 1575 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
| 1576 | 1576 | $this->objectsAll[$curElement],$match)) |
| 1577 | 1577 | { |
| 1578 | - $time_check1 += microtime(true) - $time_1; |
|
| 1578 | + $time_check1 += microtime(true) - $time_1; |
|
| 1579 | 1579 | $time_check1N++; |
| 1580 | 1580 | continue; |
| 1581 | 1581 | } |
@@ -1588,8 +1588,8 @@ discard block |
||
| 1588 | 1588 | // Check if next is suboid -> in that case is cannot be a trap |
| 1589 | 1589 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
| 1590 | 1590 | { |
| 1591 | - $time_check2 += microtime(true) - $time_1; |
|
| 1592 | - $time_check2N++; |
|
| 1591 | + $time_check2 += microtime(true) - $time_1; |
|
| 1592 | + $time_check2N++; |
|
| 1593 | 1593 | continue; |
| 1594 | 1594 | } |
| 1595 | 1595 | unset($snmptrans); |
@@ -1597,8 +1597,8 @@ discard block |
||
| 1597 | 1597 | ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
| 1598 | 1598 | if ($retVal!=0) |
| 1599 | 1599 | { |
| 1600 | - $time_check2 += microtime(true) - $time_1; |
|
| 1601 | - $time_check2N++; |
|
| 1600 | + $time_check2 += microtime(true) - $time_1; |
|
| 1601 | + $time_check2N++; |
|
| 1602 | 1602 | continue; |
| 1603 | 1603 | } |
| 1604 | 1604 | //echo "\n v1 trap found : $oid \n"; |
@@ -1607,7 +1607,7 @@ discard block |
||
| 1607 | 1607 | } |
| 1608 | 1608 | if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
| 1609 | 1609 | { |
| 1610 | - $time_check3 += microtime(true) - $time_1; |
|
| 1610 | + $time_check3 += microtime(true) - $time_1; |
|
| 1611 | 1611 | $time_check3N++; |
| 1612 | 1612 | continue; |
| 1613 | 1613 | } |
@@ -1628,7 +1628,7 @@ discard block |
||
| 1628 | 1628 | |
| 1629 | 1629 | if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
| 1630 | 1630 | { |
| 1631 | - $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 1631 | + $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 1632 | 1632 | } |
| 1633 | 1633 | $trapMib=$match[1]; |
| 1634 | 1634 | |
@@ -1636,17 +1636,17 @@ discard block |
||
| 1636 | 1636 | while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
| 1637 | 1637 | if (isset($snmptrans[$numLine])) |
| 1638 | 1638 | { |
| 1639 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 1639 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 1640 | 1640 | |
| 1641 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 1642 | - { |
|
| 1643 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 1644 | - $numLine++; |
|
| 1645 | - } |
|
| 1646 | - if (isset($snmptrans[$numLine])) { |
|
| 1647 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 1648 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 1649 | - } |
|
| 1641 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 1642 | + { |
|
| 1643 | + $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 1644 | + $numLine++; |
|
| 1645 | + } |
|
| 1646 | + if (isset($snmptrans[$numLine])) { |
|
| 1647 | + $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 1648 | + $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 1649 | + } |
|
| 1650 | 1650 | |
| 1651 | 1651 | } |
| 1652 | 1652 | $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
@@ -1654,23 +1654,23 @@ discard block |
||
| 1654 | 1654 | |
| 1655 | 1655 | if (($update==0) && ($check_change===false)) |
| 1656 | 1656 | { // Trapd didn't change & force check disabled |
| 1657 | - $time_objects += microtime(true) - $time_1; |
|
| 1658 | - if ($display_progress) echo "C"; |
|
| 1659 | - continue; |
|
| 1657 | + $time_objects += microtime(true) - $time_1; |
|
| 1658 | + if ($display_progress) echo "C"; |
|
| 1659 | + continue; |
|
| 1660 | 1660 | } |
| 1661 | 1661 | |
| 1662 | 1662 | $synt=null; |
| 1663 | 1663 | foreach ($snmptrans as $line) |
| 1664 | 1664 | { |
| 1665 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 1666 | - { |
|
| 1667 | - $synt=$match[1]; |
|
| 1668 | - } |
|
| 1665 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 1666 | + { |
|
| 1667 | + $synt=$match[1]; |
|
| 1668 | + } |
|
| 1669 | 1669 | } |
| 1670 | 1670 | if ($synt == null) |
| 1671 | 1671 | { |
| 1672 | 1672 | //echo "No objects for $trapOID\n"; |
| 1673 | - $time_objects += microtime(true) - $time_1; |
|
| 1673 | + $time_objects += microtime(true) - $time_1; |
|
| 1674 | 1674 | continue; |
| 1675 | 1675 | } |
| 1676 | 1676 | //echo "$synt \n"; |
@@ -1689,18 +1689,18 @@ discard block |
||
| 1689 | 1689 | |
| 1690 | 1690 | if ($display_progress) |
| 1691 | 1691 | { |
| 1692 | - echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 1693 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 1694 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 1695 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 1696 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 1692 | + echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 1693 | + echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 1694 | + echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 1695 | + echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 1696 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 1697 | 1697 | } |
| 1698 | 1698 | |
| 1699 | 1699 | // Timing ends |
| 1700 | 1700 | $timeTaken=microtime(true) - $timeTaken; |
| 1701 | 1701 | if ($display_progress) |
| 1702 | 1702 | { |
| 1703 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 1703 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 1704 | 1704 | } |
| 1705 | 1705 | |
| 1706 | 1706 | } |
@@ -34,8 +34,8 @@ discard block |
||
| 34 | 34 | protected $api_password=''; |
| 35 | 35 | |
| 36 | 36 | // Logs |
| 37 | - protected $logging; //< Logging class. |
|
| 38 | - protected $logSetup; //< bool true if log was setup in constructor |
|
| 37 | + protected $logging; //< Logging class. |
|
| 38 | + protected $logSetup; //< bool true if log was setup in constructor |
|
| 39 | 39 | |
| 40 | 40 | // Databases |
| 41 | 41 | public $trapsDB; //< Database class |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | private $objectsAll; //< output lines of snmptranslate list |
| 56 | 56 | private $trapObjectsIndex; //< array of traps objects (as OID) |
| 57 | 57 | |
| 58 | - function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 58 | + function __construct($etc_dir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='') |
|
| 59 | 59 | { |
| 60 | 60 | // Paths of ini files |
| 61 | 61 | $this->icingaweb2_etc=$etc_dir; |
@@ -63,10 +63,10 @@ discard block |
||
| 63 | 63 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
| 64 | 64 | |
| 65 | 65 | // Setup logging |
| 66 | - $this->logging = new Logging(); |
|
| 66 | + $this->logging=new Logging(); |
|
| 67 | 67 | if ($baseLogLevel != null) |
| 68 | 68 | { |
| 69 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 69 | + $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile); |
|
| 70 | 70 | $this->logSetup=true; |
| 71 | 71 | } |
| 72 | 72 | else |
@@ -74,10 +74,10 @@ discard block |
||
| 74 | 74 | $this->logging->log('Loggin started', INFO); |
| 75 | 75 | |
| 76 | 76 | // Get options from ini files |
| 77 | - $trapConfig=parse_ini_file($this->trap_module_config,true); |
|
| 77 | + $trapConfig=parse_ini_file($this->trap_module_config, true); |
|
| 78 | 78 | if ($trapConfig == false) |
| 79 | 79 | { |
| 80 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 80 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config, ERROR, 'syslog'); |
|
| 81 | 81 | } |
| 82 | 82 | $this->getMainOptions($trapConfig); // Get main options from ini file |
| 83 | 83 | $this->setupDatabase($trapConfig); // Setup database class |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | $this->getDatabaseOptions(); // Get options in database |
| 86 | 86 | if ($this->api_use === true) $this->getAPI(); // Setup API |
| 87 | 87 | |
| 88 | - $this->mibClass = new Mib($this->logging,$this->trapsDB); // Create Mib class |
|
| 88 | + $this->mibClass=new Mib($this->logging, $this->trapsDB); // Create Mib class |
|
| 89 | 89 | |
| 90 | 90 | $this->trap_data=array( |
| 91 | 91 | 'source_ip' => 'unknown', |
@@ -107,15 +107,15 @@ discard block |
||
| 107 | 107 | * @param string $message warning message if not found |
| 108 | 108 | * @return boolean true if found, or false |
| 109 | 109 | */ |
| 110 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
| 110 | + protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null) |
|
| 111 | 111 | { |
| 112 | 112 | if (!isset($option_array[$option_category][$option_name])) |
| 113 | 113 | { |
| 114 | 114 | if ($message === null) |
| 115 | 115 | { |
| 116 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 116 | + $message='No '.$option_name.' in config file: '.$this->trap_module_config; |
|
| 117 | 117 | } |
| 118 | - $this->logging->log($message,$log_level,'syslog'); |
|
| 118 | + $this->logging->log($message, $log_level, 'syslog'); |
|
| 119 | 119 | return false; |
| 120 | 120 | } |
| 121 | 121 | else |
@@ -133,24 +133,24 @@ discard block |
||
| 133 | 133 | { |
| 134 | 134 | |
| 135 | 135 | // Snmptranslate binary path |
| 136 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 136 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate); |
|
| 137 | 137 | |
| 138 | 138 | // mibs path |
| 139 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 139 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 140 | 140 | |
| 141 | 141 | // icinga2cmd path |
| 142 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 142 | + $this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd); |
|
| 143 | 143 | |
| 144 | 144 | // table prefix |
| 145 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->db_prefix); |
|
| 145 | + $this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->db_prefix); |
|
| 146 | 146 | |
| 147 | 147 | // API options |
| 148 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
|
| 148 | + if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->api_hostname)) |
|
| 149 | 149 | { |
| 150 | 150 | $this->api_use=true; |
| 151 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 152 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 153 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 151 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_port', $this->api_port); |
|
| 152 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_user', $this->api_username); |
|
| 153 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_password', $this->api_password); |
|
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
@@ -161,40 +161,40 @@ discard block |
||
| 161 | 161 | protected function setupDatabase($trapConfig) |
| 162 | 162 | { |
| 163 | 163 | // Trap database |
| 164 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 164 | + if (!array_key_exists('database', $trapConfig['config'])) |
|
| 165 | 165 | { |
| 166 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 166 | + $this->logging->log("No database in config file: ".$this->trap_module_config, ERROR, ''); |
|
| 167 | 167 | return; |
| 168 | 168 | } |
| 169 | 169 | $dbTrapName=$trapConfig['config']['database']; |
| 170 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 170 | + $this->logging->log("Found database in config file: ".$dbTrapName, INFO); |
|
| 171 | 171 | |
| 172 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
|
| 172 | + if (($dbConfig=parse_ini_file($this->icingaweb2_ressources, true)) === false) |
|
| 173 | 173 | { |
| 174 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 174 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources, ERROR, ''); |
|
| 175 | 175 | return; |
| 176 | 176 | } |
| 177 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 177 | + if (!array_key_exists($dbTrapName, $dbConfig)) |
|
| 178 | 178 | { |
| 179 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 179 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources, ERROR, ''); |
|
| 180 | 180 | return; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 183 | + $this->trapsDB=new Database($this->logging, $dbConfig[$dbTrapName], $this->db_prefix); |
|
| 184 | 184 | |
| 185 | 185 | if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
| 186 | 186 | |
| 187 | 187 | // IDO Database |
| 188 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 188 | + if (!array_key_exists('IDOdatabase', $trapConfig['config'])) |
|
| 189 | 189 | { |
| 190 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 190 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config, ERROR, ''); |
|
| 191 | 191 | } |
| 192 | 192 | $dbIdoName=$trapConfig['config']['IDOdatabase']; |
| 193 | 193 | |
| 194 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 195 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 194 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO); |
|
| 195 | + if (!array_key_exists($dbIdoName, $dbConfig)) |
|
| 196 | 196 | { |
| 197 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 197 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources, ERROR, ''); |
|
| 198 | 198 | return; |
| 199 | 199 | } |
| 200 | 200 | |
@@ -209,13 +209,13 @@ discard block |
||
| 209 | 209 | // Database options |
| 210 | 210 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
| 211 | 211 | { |
| 212 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 213 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 214 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 212 | + $this->getDBConfigIfSet('log_level', $this->logging->debugLevel); |
|
| 213 | + $this->getDBConfigIfSet('log_destination', $this->logging->outputMode); |
|
| 214 | + $this->getDBConfigIfSet('log_file', $this->logging->outputFile); |
|
| 215 | 215 | } |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - protected function getDBConfigIfSet($element,&$variable) |
|
| 218 | + protected function getDBConfigIfSet($element, &$variable) |
|
| 219 | 219 | { |
| 220 | 220 | $value=$this->getDBConfig($element); |
| 221 | 221 | if ($value != 'null') $variable=$value; |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 232 | 232 | $sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )'; |
| 233 | 233 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 234 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 234 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 235 | 235 | return null; |
| 236 | 236 | } |
| 237 | 237 | $value=$ret_code->fetch(); |
@@ -248,22 +248,22 @@ discard block |
||
| 248 | 248 | * @param string $destination file/syslog/display |
| 249 | 249 | * @return void |
| 250 | 250 | **/ |
| 251 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 251 | + public function trapLog($message, $level, $destination='') // OBSOLETE |
|
| 252 | 252 | { |
| 253 | 253 | // TODO : replace ref with $this->logging->log |
| 254 | 254 | $this->logging->log($message, $level, $destination); |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 257 | + public function setLogging($debugLvl, $outputType, $outputOption=null) // OBSOLETE |
|
| 258 | 258 | { |
| 259 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 259 | + $this->logging->setLogging($debugLvl, $outputType, $outputOption); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | protected function getAPI() |
| 263 | 263 | { |
| 264 | 264 | if ($this->icinga2api == null) |
| 265 | 265 | { |
| 266 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 266 | + $this->icinga2api=new Icinga2API($this->api_hostname, $this->api_port); |
|
| 267 | 267 | } |
| 268 | 268 | return $this->icinga2api; |
| 269 | 269 | } |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | if ($input_stream === false) |
| 283 | 283 | { |
| 284 | 284 | $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
| 285 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 285 | + $this->logging->log("Error reading stdin !", ERROR, ''); |
|
| 286 | 286 | return null; // note : exception thrown by logging |
| 287 | 287 | } |
| 288 | 288 | |
@@ -291,21 +291,21 @@ discard block |
||
| 291 | 291 | if ($this->receivingHost === false) |
| 292 | 292 | { |
| 293 | 293 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
| 294 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
| 294 | + $this->logging->log("Error reading Host !", ERROR, ''); |
|
| 295 | 295 | } |
| 296 | 296 | // line 2 IP:port=>IP:port |
| 297 | 297 | $IP=chop(fgets($input_stream)); |
| 298 | 298 | if ($IP === false) |
| 299 | 299 | { |
| 300 | 300 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
| 301 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
| 301 | + $this->logging->log("Error reading IP !", ERROR, ''); |
|
| 302 | 302 | } |
| 303 | 303 | $matches=array(); |
| 304 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 305 | - if ($ret_code===0 || $ret_code===false) |
|
| 304 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches); |
|
| 305 | + if ($ret_code === 0 || $ret_code === false) |
|
| 306 | 306 | { |
| 307 | 307 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 308 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 308 | + $this->logging->log('Error parsing IP : '.$IP, ERROR, ''); |
|
| 309 | 309 | } |
| 310 | 310 | else |
| 311 | 311 | { |
@@ -315,34 +315,34 @@ discard block |
||
| 315 | 315 | $this->trap_data['destination_port']=$matches[4]; |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | - while (($vars=fgets($input_stream)) !==false) |
|
| 318 | + while (($vars=fgets($input_stream)) !== false) |
|
| 319 | 319 | { |
| 320 | 320 | $vars=chop($vars); |
| 321 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 322 | - if ($ret_code===0 || $ret_code===false) |
|
| 321 | + $ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches); |
|
| 322 | + if ($ret_code === 0 || $ret_code === false) |
|
| 323 | 323 | { |
| 324 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 324 | + $this->logging->log('No match on trap data : '.$vars, WARN, ''); |
|
| 325 | 325 | } |
| 326 | 326 | else |
| 327 | 327 | { |
| 328 | - 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')) |
|
| 328 | + 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')) |
|
| 329 | 329 | { |
| 330 | 330 | $this->trap_data['trap_oid']=$matches[2]; |
| 331 | 331 | } |
| 332 | 332 | else |
| 333 | 333 | { |
| 334 | - $object= new stdClass; |
|
| 335 | - $object->oid =$matches[1]; |
|
| 336 | - $object->value = $matches[2]; |
|
| 337 | - array_push($this->trap_data_ext,$object); |
|
| 334 | + $object=new stdClass; |
|
| 335 | + $object->oid=$matches[1]; |
|
| 336 | + $object->value=$matches[2]; |
|
| 337 | + array_push($this->trap_data_ext, $object); |
|
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - if ($this->trap_data['trap_oid']=='unknown') |
|
| 342 | + if ($this->trap_data['trap_oid'] == 'unknown') |
|
| 343 | 343 | { |
| 344 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 345 | - $this->logging->log('no trap oid found',ERROR,''); |
|
| 344 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trap_data['source_ip']); |
|
| 345 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | // Translate oids. |
@@ -364,7 +364,7 @@ discard block |
||
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | |
| 367 | - $this->trap_data['status']= 'waiting'; |
|
| 367 | + $this->trap_data['status']='waiting'; |
|
| 368 | 368 | |
| 369 | 369 | return $this->trap_data; |
| 370 | 370 | } |
@@ -380,40 +380,40 @@ discard block |
||
| 380 | 380 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 381 | 381 | |
| 382 | 382 | $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
| 383 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 383 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 384 | 384 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 385 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 385 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 386 | 386 | } |
| 387 | 387 | $name=$ret_code->fetch(); |
| 388 | 388 | if ($name['name'] != null) |
| 389 | 389 | { |
| 390 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 390 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | // Also check if it is an instance of OID |
| 394 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 394 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
| 395 | 395 | |
| 396 | 396 | $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
| 397 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 397 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 398 | 398 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 399 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 399 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 400 | 400 | } |
| 401 | 401 | $name=$ret_code->fetch(); |
| 402 | 402 | if ($name['name'] != null) |
| 403 | 403 | { |
| 404 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 404 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | // Try to get oid name from snmptranslate |
| 408 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 408 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 409 | 409 | ' '.$oid); |
| 410 | 410 | $matches=array(); |
| 411 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 412 | - if ($ret_code===0 || $ret_code === false) { |
|
| 411 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
| 412 | + if ($ret_code === 0 || $ret_code === false) { |
|
| 413 | 413 | return NULL; |
| 414 | 414 | } else { |
| 415 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 416 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 415 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
| 416 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
| 417 | 417 | } |
| 418 | 418 | } |
| 419 | 419 | |
@@ -424,90 +424,90 @@ discard block |
||
| 424 | 424 | **/ |
| 425 | 425 | public function eraseOldTraps($days=0) |
| 426 | 426 | { |
| 427 | - if ($days==0) |
|
| 427 | + if ($days == 0) |
|
| 428 | 428 | { |
| 429 | 429 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
| 430 | 430 | { |
| 431 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 431 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
| 432 | 432 | return; |
| 433 | 433 | } |
| 434 | 434 | } |
| 435 | 435 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 436 | - $daysago = strtotime("-".$days." day"); |
|
| 437 | - $sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 436 | + $daysago=strtotime("-".$days." day"); |
|
| 437 | + $sql='delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
| 438 | 438 | if ($db_conn->query($sql) === false) { |
| 439 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 439 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
| 440 | 440 | } |
| 441 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 441 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | /** Write error to received trap database |
| 445 | 445 | */ |
| 446 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 446 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
| 447 | 447 | { |
| 448 | 448 | |
| 449 | 449 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 450 | 450 | |
| 451 | 451 | // add date time |
| 452 | - $insert_col ='date_received,status'; |
|
| 453 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 452 | + $insert_col='date_received,status'; |
|
| 453 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
| 454 | 454 | |
| 455 | - if ($sourceIP !=null) |
|
| 455 | + if ($sourceIP != null) |
|
| 456 | 456 | { |
| 457 | - $insert_col .=',source_ip'; |
|
| 458 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 457 | + $insert_col.=',source_ip'; |
|
| 458 | + $insert_val.=",'".$sourceIP."'"; |
|
| 459 | 459 | } |
| 460 | - if ($trapoid !=null) |
|
| 460 | + if ($trapoid != null) |
|
| 461 | 461 | { |
| 462 | - $insert_col .=',trap_oid'; |
|
| 463 | - $insert_val .=",'". $trapoid ."'"; |
|
| 462 | + $insert_col.=',trap_oid'; |
|
| 463 | + $insert_val.=",'".$trapoid."'"; |
|
| 464 | 464 | } |
| 465 | - $insert_col .=',status_detail'; |
|
| 466 | - $insert_val .=",'". $message ."'"; |
|
| 465 | + $insert_col.=',status_detail'; |
|
| 466 | + $insert_val.=",'".$message."'"; |
|
| 467 | 467 | |
| 468 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 468 | + $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
| 469 | 469 | |
| 470 | 470 | switch ($this->trapsDB->trapDBType) |
| 471 | 471 | { |
| 472 | 472 | case 'pgsql': |
| 473 | - $sql .= ' RETURNING id;'; |
|
| 474 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 473 | + $sql.=' RETURNING id;'; |
|
| 474 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 475 | 475 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 476 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 476 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
| 477 | 477 | } |
| 478 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 478 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 479 | 479 | // Get last id to insert oid/values in secondary table |
| 480 | 480 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
| 481 | 481 | |
| 482 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 482 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
| 483 | 483 | } |
| 484 | - if (! isset($inserted_id_ret['id'])) { |
|
| 485 | - $this->logging->log('Error getting id',1,''); |
|
| 484 | + if (!isset($inserted_id_ret['id'])) { |
|
| 485 | + $this->logging->log('Error getting id', 1, ''); |
|
| 486 | 486 | } |
| 487 | 487 | $this->trap_id=$inserted_id_ret['id']; |
| 488 | 488 | break; |
| 489 | 489 | case 'mysql': |
| 490 | - $sql .= ';'; |
|
| 491 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 490 | + $sql.=';'; |
|
| 491 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 492 | 492 | if ($db_conn->query($sql) === false) { |
| 493 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 493 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
| 494 | 494 | } |
| 495 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 495 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 496 | 496 | // Get last id to insert oid/values in secondary table |
| 497 | 497 | $sql='SELECT LAST_INSERT_ID();'; |
| 498 | 498 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 499 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 499 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 503 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 503 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 504 | 504 | $this->trap_id=$inserted_id; |
| 505 | 505 | break; |
| 506 | 506 | default: |
| 507 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 507 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 510 | + $this->logging->log('id found: '.$this->trap_id, INFO); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | /** Write trap data to trap database |
@@ -524,86 +524,86 @@ discard block |
||
| 524 | 524 | $insert_col=''; |
| 525 | 525 | $insert_val=''; |
| 526 | 526 | // add date time |
| 527 | - $this->trap_data['date_received'] = date("Y-m-d H:i:s"); |
|
| 527 | + $this->trap_data['date_received']=date("Y-m-d H:i:s"); |
|
| 528 | 528 | |
| 529 | 529 | $firstcol=1; |
| 530 | 530 | foreach ($this->trap_data as $col => $val) |
| 531 | 531 | { |
| 532 | - if ($firstcol==0) |
|
| 532 | + if ($firstcol == 0) |
|
| 533 | 533 | { |
| 534 | - $insert_col .=','; |
|
| 535 | - $insert_val .=','; |
|
| 534 | + $insert_col.=','; |
|
| 535 | + $insert_val.=','; |
|
| 536 | 536 | } |
| 537 | - $insert_col .= $col ; |
|
| 538 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 537 | + $insert_col.=$col; |
|
| 538 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
| 539 | 539 | $firstcol=0; |
| 540 | 540 | } |
| 541 | 541 | |
| 542 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 542 | + $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
| 543 | 543 | switch ($this->trapsDB->trapDBType) |
| 544 | 544 | { |
| 545 | 545 | case 'pgsql': |
| 546 | - $sql .= ' RETURNING id;'; |
|
| 547 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 546 | + $sql.=' RETURNING id;'; |
|
| 547 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 548 | 548 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 549 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 549 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
| 550 | 550 | } |
| 551 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 551 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 552 | 552 | // Get last id to insert oid/values in secondary table |
| 553 | 553 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
| 554 | 554 | |
| 555 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 555 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
| 556 | 556 | } |
| 557 | - if (! isset($inserted_id_ret['id'])) { |
|
| 558 | - $this->logging->log('Error getting id',ERROR,''); |
|
| 557 | + if (!isset($inserted_id_ret['id'])) { |
|
| 558 | + $this->logging->log('Error getting id', ERROR, ''); |
|
| 559 | 559 | } |
| 560 | 560 | $this->trap_id=$inserted_id_ret['id']; |
| 561 | 561 | break; |
| 562 | 562 | case 'mysql': |
| 563 | - $sql .= ';'; |
|
| 564 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 563 | + $sql.=';'; |
|
| 564 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 565 | 565 | if ($db_conn->query($sql) === false) { |
| 566 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 566 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
| 567 | 567 | } |
| 568 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 568 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 569 | 569 | // Get last id to insert oid/values in secondary table |
| 570 | 570 | $sql='SELECT LAST_INSERT_ID();'; |
| 571 | 571 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 572 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 572 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 576 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 576 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 577 | 577 | $this->trap_id=$inserted_id; |
| 578 | 578 | break; |
| 579 | 579 | default: |
| 580 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 580 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
| 581 | 581 | } |
| 582 | - $this->logging->log('id found: '.$this->trap_id,INFO ); |
|
| 582 | + $this->logging->log('id found: '.$this->trap_id, INFO); |
|
| 583 | 583 | |
| 584 | 584 | // Fill trap extended data table |
| 585 | 585 | foreach ($this->trap_data_ext as $value) { |
| 586 | 586 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
| 587 | 587 | $firstcol=1; |
| 588 | - $value->trap_id = $this->trap_id; |
|
| 588 | + $value->trap_id=$this->trap_id; |
|
| 589 | 589 | $insert_col=''; |
| 590 | 590 | $insert_val=''; |
| 591 | 591 | foreach ($value as $col => $val) |
| 592 | 592 | { |
| 593 | - if ($firstcol==0) |
|
| 593 | + if ($firstcol == 0) |
|
| 594 | 594 | { |
| 595 | - $insert_col .=','; |
|
| 596 | - $insert_val .=','; |
|
| 595 | + $insert_col.=','; |
|
| 596 | + $insert_val.=','; |
|
| 597 | 597 | } |
| 598 | - $insert_col .= $col; |
|
| 599 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 598 | + $insert_col.=$col; |
|
| 599 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
| 600 | 600 | $firstcol=0; |
| 601 | 601 | } |
| 602 | 602 | |
| 603 | - $sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 603 | + $sql='INSERT INTO '.$this->db_prefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
| 604 | 604 | |
| 605 | 605 | if ($db_conn->query($sql) === false) { |
| 606 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 606 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
| 607 | 607 | } |
| 608 | 608 | } |
| 609 | 609 | } |
@@ -613,14 +613,14 @@ discard block |
||
| 613 | 613 | * @param $oid string oid in numeric |
| 614 | 614 | * @return mixed : PDO object or false |
| 615 | 615 | */ |
| 616 | - protected function getRules($ip,$oid) |
|
| 616 | + protected function getRules($ip, $oid) |
|
| 617 | 617 | { |
| 618 | 618 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 619 | 619 | // fetch rules based on IP in rule and OID |
| 620 | 620 | $sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
| 621 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 621 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 622 | 622 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 623 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 623 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 624 | 624 | return false; |
| 625 | 625 | } |
| 626 | 626 | $rules_all=$ret_code->fetchAll(); |
@@ -629,7 +629,7 @@ discard block |
||
| 629 | 629 | $rule_ret_key=0; |
| 630 | 630 | foreach ($rules_all as $key => $rule) |
| 631 | 631 | { |
| 632 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 632 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
| 633 | 633 | { |
| 634 | 634 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
| 635 | 635 | //TODO : get host name by API (and check if correct in rule). |
@@ -637,7 +637,7 @@ discard block |
||
| 637 | 637 | continue; |
| 638 | 638 | } |
| 639 | 639 | // TODO : get hosts IP by API |
| 640 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 640 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
| 641 | 641 | { // get ips of group members by oid |
| 642 | 642 | $db_conn2=$this->trapsDB->db_connect_ido(); |
| 643 | 643 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -648,15 +648,15 @@ discard block |
||
| 648 | 648 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
| 649 | 649 | WHERE o.name1='".$rule['host_group_name']."';"; |
| 650 | 650 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
| 651 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 651 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 652 | 652 | continue; |
| 653 | 653 | } |
| 654 | 654 | $grouphosts=$ret_code2->fetchAll(); |
| 655 | 655 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
| 656 | - foreach ( $grouphosts as $host) |
|
| 656 | + foreach ($grouphosts as $host) |
|
| 657 | 657 | { |
| 658 | 658 | //echo $host['ip4']."\n"; |
| 659 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 659 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
| 660 | 660 | { |
| 661 | 661 | //echo "Rule added \n"; |
| 662 | 662 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -679,7 +679,7 @@ discard block |
||
| 679 | 679 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 680 | 680 | $sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
| 681 | 681 | if ($db_conn->query($sql) === false) { |
| 682 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 682 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
| 683 | 683 | } |
| 684 | 684 | } |
| 685 | 685 | |
@@ -691,31 +691,31 @@ discard block |
||
| 691 | 691 | * @param string $display |
| 692 | 692 | * @returnn bool true is service check was sent without error |
| 693 | 693 | */ |
| 694 | - public function serviceCheckResult($host,$service,$state,$display) |
|
| 694 | + public function serviceCheckResult($host, $service, $state, $display) |
|
| 695 | 695 | { |
| 696 | 696 | if ($this->api_use === false) |
| 697 | 697 | { |
| 698 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 699 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 700 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 698 | + $send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'. |
|
| 699 | + $host.';'.$service.';'.$state.';'.$display; |
|
| 700 | + $this->logging->log($send." : to : ".$this->icinga2cmd, INFO); |
|
| 701 | 701 | |
| 702 | 702 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 703 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 703 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
| 704 | 704 | return true; |
| 705 | 705 | } |
| 706 | 706 | else |
| 707 | 707 | { |
| 708 | - $api = $this->getAPI(); |
|
| 708 | + $api=$this->getAPI(); |
|
| 709 | 709 | $api->setCredentials($this->api_username, $this->api_password); |
| 710 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
| 710 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display); |
|
| 711 | 711 | if ($retcode == false) |
| 712 | 712 | { |
| 713 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 713 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
| 714 | 714 | return false; |
| 715 | 715 | } |
| 716 | 716 | else |
| 717 | 717 | { |
| 718 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 718 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
| 719 | 719 | return true; |
| 720 | 720 | } |
| 721 | 721 | } |
@@ -723,7 +723,7 @@ discard block |
||
| 723 | 723 | |
| 724 | 724 | public function getHostByIP($ip) |
| 725 | 725 | { |
| 726 | - $api = $this->getAPI(); |
|
| 726 | + $api=$this->getAPI(); |
|
| 727 | 727 | $api->setCredentials($this->api_username, $this->api_password); |
| 728 | 728 | return $api->getHostByIP($ip); |
| 729 | 729 | } |
@@ -736,32 +736,32 @@ discard block |
||
| 736 | 736 | protected function applyDisplay($display) |
| 737 | 737 | { |
| 738 | 738 | $matches=array(); |
| 739 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 739 | + while (preg_match('/_OID\(([0-9\.]+)\)/', $display, $matches) == 1) |
|
| 740 | 740 | { |
| 741 | 741 | $oid=$matches[1]; |
| 742 | 742 | $found=0; |
| 743 | - foreach($this->trap_data_ext as $val) |
|
| 743 | + foreach ($this->trap_data_ext as $val) |
|
| 744 | 744 | { |
| 745 | 745 | if ($oid == $val->oid) |
| 746 | 746 | { |
| 747 | - $val->value=preg_replace('/"/','',$val->value); |
|
| 747 | + $val->value=preg_replace('/"/', '', $val->value); |
|
| 748 | 748 | $rep=0; |
| 749 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 750 | - if ($rep==0) |
|
| 749 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
| 750 | + if ($rep == 0) |
|
| 751 | 751 | { |
| 752 | - $this->logging->log("Error in display",WARN,''); |
|
| 752 | + $this->logging->log("Error in display", WARN, ''); |
|
| 753 | 753 | return $display; |
| 754 | 754 | } |
| 755 | 755 | $found=1; |
| 756 | 756 | break; |
| 757 | 757 | } |
| 758 | 758 | } |
| 759 | - if ($found==0) |
|
| 759 | + if ($found == 0) |
|
| 760 | 760 | { |
| 761 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 762 | - if ($rep==0) |
|
| 761 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
| 762 | + if ($rep == 0) |
|
| 763 | 763 | { |
| 764 | - $this->logging->log("Error in display",WARN,''); |
|
| 764 | + $this->logging->log("Error in display", WARN, ''); |
|
| 765 | 765 | return $display; |
| 766 | 766 | } |
| 767 | 767 | } |
@@ -771,27 +771,27 @@ discard block |
||
| 771 | 771 | |
| 772 | 772 | |
| 773 | 773 | /***************** Eval & tokenizer functions ****************/ |
| 774 | - protected function eval_getElement($rule,&$item) |
|
| 774 | + protected function eval_getElement($rule, &$item) |
|
| 775 | 775 | { |
| 776 | - while ($rule[$item]==' ') $item++; |
|
| 777 | - if (preg_match('/[0-9\.]/',$rule[$item])) |
|
| 776 | + while ($rule[$item] == ' ') $item++; |
|
| 777 | + if (preg_match('/[0-9\.]/', $rule[$item])) |
|
| 778 | 778 | { // number |
| 779 | 779 | |
| 780 | - $item2=$item+1; |
|
| 781 | - while (($item2!=strlen($rule)) && (preg_match('/[0-9\.]/',$rule[$item2]))) { $item2++ ;} |
|
| 782 | - $val=substr($rule,$item,$item2-$item); |
|
| 780 | + $item2=$item + 1; |
|
| 781 | + while (($item2 != strlen($rule)) && (preg_match('/[0-9\.]/', $rule[$item2]))) { $item2++; } |
|
| 782 | + $val=substr($rule, $item, $item2 - $item); |
|
| 783 | 783 | $item=$item2; |
| 784 | 784 | //echo "number ".$val."\n"; |
| 785 | - return array(0,$val); |
|
| 785 | + return array(0, $val); |
|
| 786 | 786 | } |
| 787 | 787 | if ($rule[$item] == '"') |
| 788 | 788 | { // string |
| 789 | 789 | $item++; |
| 790 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 791 | - $val=substr($rule,$item,$item2-$item-1); |
|
| 790 | + $item2=$this->eval_getNext($rule, $item, '"'); |
|
| 791 | + $val=substr($rule, $item, $item2 - $item - 1); |
|
| 792 | 792 | $item=$item2; |
| 793 | 793 | //echo "string : ".$val."\n"; |
| 794 | - return array(1,$val); |
|
| 794 | + return array(1, $val); |
|
| 795 | 795 | } |
| 796 | 796 | |
| 797 | 797 | if ($rule[$item] == '(') |
@@ -800,14 +800,14 @@ discard block |
||
| 800 | 800 | $start=$item; |
| 801 | 801 | $parenthesis_count=0; |
| 802 | 802 | while (($item < strlen($rule)) // Not end of string AND |
| 803 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 803 | + && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded () |
|
| 804 | 804 | { |
| 805 | - if ($rule[$item] == '"' ) |
|
| 805 | + if ($rule[$item] == '"') |
|
| 806 | 806 | { // pass through string |
| 807 | 807 | $item++; |
| 808 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
| 808 | + $item=$this->eval_getNext($rule, $item, '"'); |
|
| 809 | 809 | } |
| 810 | - else{ |
|
| 810 | + else { |
|
| 811 | 811 | if ($rule[$item] == '(') |
| 812 | 812 | { |
| 813 | 813 | $parenthesis_count++; |
@@ -820,49 +820,49 @@ discard block |
||
| 820 | 820 | } |
| 821 | 821 | } |
| 822 | 822 | |
| 823 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 824 | - $val=substr($rule,$start,$item-$start); |
|
| 823 | + if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); } |
|
| 824 | + $val=substr($rule, $start, $item - $start); |
|
| 825 | 825 | $item++; |
| 826 | 826 | $start=0; |
| 827 | 827 | //echo "group : ".$val."\n"; |
| 828 | 828 | // returns evaluation of group as type 2 (boolean) |
| 829 | - return array(2,$this->evaluation($val,$start)); |
|
| 829 | + return array(2, $this->evaluation($val, $start)); |
|
| 830 | 830 | } |
| 831 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 831 | + throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]); |
|
| 832 | 832 | |
| 833 | 833 | } |
| 834 | 834 | |
| 835 | - protected function eval_getNext($rule,$item,$tok) |
|
| 835 | + protected function eval_getNext($rule, $item, $tok) |
|
| 836 | 836 | { |
| 837 | - while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;} |
|
| 838 | - if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 839 | - return $item+1; |
|
| 837 | + while (($rule[$item] != $tok) && ($item < strlen($rule))) { $item++; } |
|
| 838 | + if ($item == strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item); |
|
| 839 | + return $item + 1; |
|
| 840 | 840 | } |
| 841 | 841 | |
| 842 | - protected function eval_getOper($rule,&$item) |
|
| 842 | + protected function eval_getOper($rule, &$item) |
|
| 843 | 843 | { |
| 844 | - while ($rule[$item]==' ') $item++; |
|
| 844 | + while ($rule[$item] == ' ') $item++; |
|
| 845 | 845 | switch ($rule[$item]) |
| 846 | 846 | { |
| 847 | 847 | case '<': |
| 848 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 849 | - $item++; return array(0,"<"); |
|
| 848 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); } |
|
| 849 | + $item++; return array(0, "<"); |
|
| 850 | 850 | case '>': |
| 851 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 852 | - $item++; return array(0,">"); |
|
| 851 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); } |
|
| 852 | + $item++; return array(0, ">"); |
|
| 853 | 853 | case '=': |
| 854 | - $item++; return array(0,"="); |
|
| 854 | + $item++; return array(0, "="); |
|
| 855 | 855 | case '!': |
| 856 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
| 857 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
| 856 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); } |
|
| 857 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule." at ".$item); |
|
| 858 | 858 | case '~': |
| 859 | - $item++; return array(0,"~"); |
|
| 859 | + $item++; return array(0, "~"); |
|
| 860 | 860 | case '|': |
| 861 | - $item++; return array(1,"|"); |
|
| 861 | + $item++; return array(1, "|"); |
|
| 862 | 862 | case '&': |
| 863 | - $item++; return array(1,"&"); |
|
| 863 | + $item++; return array(1, "&"); |
|
| 864 | 864 | default : |
| 865 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 865 | + throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item); |
|
| 866 | 866 | } |
| 867 | 867 | } |
| 868 | 868 | |
@@ -874,10 +874,10 @@ discard block |
||
| 874 | 874 | * comparison int vs strings will return null (error) |
| 875 | 875 | * return : bool or null on error |
| 876 | 876 | */ |
| 877 | - public function evaluation($rule,&$item) |
|
| 877 | + public function evaluation($rule, &$item) |
|
| 878 | 878 | { |
| 879 | 879 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
| 880 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 880 | + if ($rule[$item] == '!') // If '!' found, negate next expression. |
|
| 881 | 881 | { |
| 882 | 882 | $negate=true; |
| 883 | 883 | $item++; |
@@ -887,63 +887,63 @@ discard block |
||
| 887 | 887 | $negate=false; |
| 888 | 888 | } |
| 889 | 889 | // First element : number, string or () |
| 890 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 890 | + list($type1, $val1)=$this->eval_getElement($rule, $item); |
|
| 891 | 891 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 892 | 892 | |
| 893 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 893 | + if ($item == strlen($rule)) // If only element, return value, but only boolean |
|
| 894 | 894 | { |
| 895 | 895 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 896 | - if ($negate === true) $val1= ! $val1; |
|
| 896 | + if ($negate === true) $val1=!$val1; |
|
| 897 | 897 | return $val1; |
| 898 | 898 | } |
| 899 | 899 | |
| 900 | 900 | // Second element : operator |
| 901 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 901 | + list($typec, $comp)=$this->eval_getOper($rule, $item); |
|
| 902 | 902 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 903 | 903 | |
| 904 | 904 | // Third element : number, string or () |
| 905 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 905 | + if ($rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 906 | 906 | { |
| 907 | 907 | $item++; |
| 908 | 908 | if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
| 909 | - $val2= ! $this->evaluation($rule,$item); |
|
| 909 | + $val2=!$this->evaluation($rule, $item); |
|
| 910 | 910 | $type2=2; // result is a boolean |
| 911 | 911 | } |
| 912 | 912 | else |
| 913 | 913 | { |
| 914 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 914 | + list($type2, $val2)=$this->eval_getElement($rule, $item); |
|
| 915 | 915 | } |
| 916 | 916 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 917 | 917 | |
| 918 | - if ($type1!=$type2) // cannot compare different types |
|
| 918 | + if ($type1 != $type2) // cannot compare different types |
|
| 919 | 919 | { |
| 920 | 920 | throw new Exception("Cannot compare string & number : ".$rule); |
| 921 | 921 | } |
| 922 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 922 | + if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number |
|
| 923 | 923 | { |
| 924 | 924 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 925 | 925 | } |
| 926 | 926 | |
| 927 | - switch ($comp){ |
|
| 928 | - case '<': $retVal= ($val1 < $val2); break; |
|
| 929 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
| 930 | - case '>': $retVal= ($val1 > $val2); break; |
|
| 931 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
| 932 | - case '=': $retVal= ($val1 == $val2); break; |
|
| 933 | - case '!=': $retVal= ($val1 != $val2); break; |
|
| 934 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
| 935 | - case '|': $retVal= ($val1 || $val2); break; |
|
| 936 | - case '&': $retVal= ($val1 && $val2); break; |
|
| 927 | + switch ($comp) { |
|
| 928 | + case '<': $retVal=($val1 < $val2); break; |
|
| 929 | + case '<=': $retVal=($val1 <= $val2); break; |
|
| 930 | + case '>': $retVal=($val1 > $val2); break; |
|
| 931 | + case '>=': $retVal=($val1 >= $val2); break; |
|
| 932 | + case '=': $retVal=($val1 == $val2); break; |
|
| 933 | + case '!=': $retVal=($val1 != $val2); break; |
|
| 934 | + case '~': $retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break; |
|
| 935 | + case '|': $retVal=($val1 || $val2); break; |
|
| 936 | + case '&': $retVal=($val1 && $val2); break; |
|
| 937 | 937 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 938 | 938 | } |
| 939 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 939 | + if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression |
|
| 940 | 940 | |
| 941 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 941 | + if ($item == strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 942 | 942 | // check for logical operator : |
| 943 | 943 | switch ($rule[$item]) |
| 944 | 944 | { |
| 945 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 946 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 945 | + case '|': $item++; return ($retVal || $this->evaluation($rule, $item)); |
|
| 946 | + case '&': $item++; return ($retVal && $this->evaluation($rule, $item)); |
|
| 947 | 947 | |
| 948 | 948 | default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
| 949 | 949 | } |
@@ -955,17 +955,17 @@ discard block |
||
| 955 | 955 | $rule2=''; |
| 956 | 956 | while ($item < strlen($rule)) |
| 957 | 957 | { |
| 958 | - if ($rule[$item]==' ') { $item++; continue; } |
|
| 959 | - if ($rule[$item]=='"') |
|
| 958 | + if ($rule[$item] == ' ') { $item++; continue; } |
|
| 959 | + if ($rule[$item] == '"') |
|
| 960 | 960 | { |
| 961 | 961 | $rule2.=$rule[$item]; |
| 962 | 962 | $item++; |
| 963 | - while (($rule[$item]!='"') && ($item < strlen($rule))) |
|
| 963 | + while (($rule[$item] != '"') && ($item < strlen($rule))) |
|
| 964 | 964 | { |
| 965 | 965 | $rule2.=$rule[$item]; |
| 966 | 966 | $item++; |
| 967 | 967 | } |
| 968 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 968 | + if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item); |
|
| 969 | 969 | $rule2.=$rule[$item]; |
| 970 | 970 | $item++; |
| 971 | 971 | continue; |
@@ -985,12 +985,12 @@ discard block |
||
| 985 | 985 | |
| 986 | 986 | protected function eval_rule($rule) |
| 987 | 987 | { |
| 988 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 988 | + if ($rule == null || $rule == '') // Empty rule is always true |
|
| 989 | 989 | { |
| 990 | 990 | return true; |
| 991 | 991 | } |
| 992 | 992 | $matches=array(); |
| 993 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 993 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1) |
|
| 994 | 994 | { |
| 995 | 995 | $oid=$matches[1]; |
| 996 | 996 | $found=0; |
@@ -1002,49 +1002,49 @@ discard block |
||
| 1002 | 1002 | // replace * with \* in oid for preg_replace |
| 1003 | 1003 | $oid=preg_replace('/\*/', '\*', $oid); |
| 1004 | 1004 | |
| 1005 | - $this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG ); |
|
| 1005 | + $this->logging->log('OID in rule : '.$oid.' / '.$oidR, DEBUG); |
|
| 1006 | 1006 | |
| 1007 | - foreach($this->trap_data_ext as $val) |
|
| 1007 | + foreach ($this->trap_data_ext as $val) |
|
| 1008 | 1008 | { |
| 1009 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 1009 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
| 1010 | 1010 | { |
| 1011 | - if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 1011 | + if (!preg_match('/^[0-9]*\.?[0-9]+$/', $val->value)) |
|
| 1012 | 1012 | { // If not a number, change " to ' and put " around it |
| 1013 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
| 1013 | + $val->value=preg_replace('/"/', "'", $val->value); |
|
| 1014 | 1014 | $val->value='"'.$val->value.'"'; |
| 1015 | 1015 | } |
| 1016 | 1016 | $rep=0; |
| 1017 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 1018 | - if ($rep==0) |
|
| 1017 | + $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep); |
|
| 1018 | + if ($rep == 0) |
|
| 1019 | 1019 | { |
| 1020 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
| 1020 | + $this->logging->log("Error in rule_eval", WARN, ''); |
|
| 1021 | 1021 | return false; |
| 1022 | 1022 | } |
| 1023 | 1023 | $found=1; |
| 1024 | 1024 | break; |
| 1025 | 1025 | } |
| 1026 | 1026 | } |
| 1027 | - if ($found==0) |
|
| 1027 | + if ($found == 0) |
|
| 1028 | 1028 | { // OID not found : throw error |
| 1029 | 1029 | throw new Exception('OID '.$oid.' not found in trap'); |
| 1030 | 1030 | } |
| 1031 | 1031 | } |
| 1032 | 1032 | $item=0; |
| 1033 | 1033 | $rule=$this->eval_cleanup($rule); |
| 1034 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 1034 | + $this->logging->log('Rule after clenup: '.$rule, INFO); |
|
| 1035 | 1035 | |
| 1036 | - return $this->evaluation($rule,$item); |
|
| 1036 | + return $this->evaluation($rule, $item); |
|
| 1037 | 1037 | } |
| 1038 | 1038 | |
| 1039 | 1039 | /** Match rules for current trap and do action |
| 1040 | 1040 | */ |
| 1041 | 1041 | public function applyRules() |
| 1042 | 1042 | { |
| 1043 | - $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
|
| 1043 | + $rules=$this->getRules($this->trap_data['source_ip'], $this->trap_data['trap_oid']); |
|
| 1044 | 1044 | |
| 1045 | - if ($rules===false || count($rules)==0) |
|
| 1045 | + if ($rules === false || count($rules) == 0) |
|
| 1046 | 1046 | { |
| 1047 | - $this->logging->log('No rules found for this trap',INFO ); |
|
| 1047 | + $this->logging->log('No rules found for this trap', INFO); |
|
| 1048 | 1048 | $this->trap_data['status']='unknown'; |
| 1049 | 1049 | $this->trap_to_db=true; |
| 1050 | 1050 | return; |
@@ -1059,58 +1059,58 @@ discard block |
||
| 1059 | 1059 | $service_name=$rule['service_name']; |
| 1060 | 1060 | |
| 1061 | 1061 | $display=$this->applyDisplay($rule['display']); |
| 1062 | - $this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', '; |
|
| 1062 | + $this->trap_action=($this->trap_action == null) ? '' : $this->trap_action.', '; |
|
| 1063 | 1063 | try |
| 1064 | 1064 | { |
| 1065 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 1065 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
| 1066 | 1066 | $evalr=$this->eval_rule($rule['rule']); |
| 1067 | 1067 | |
| 1068 | 1068 | if ($evalr == true) |
| 1069 | 1069 | { |
| 1070 | 1070 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
| 1071 | 1071 | $action=$rule['action_match']; |
| 1072 | - $this->logging->log('action OK : '.$action,INFO ); |
|
| 1072 | + $this->logging->log('action OK : '.$action, INFO); |
|
| 1073 | 1073 | if ($action >= 0) |
| 1074 | 1074 | { |
| 1075 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 1075 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
| 1076 | 1076 | { |
| 1077 | 1077 | $this->trap_action.='Error sending status : check cmd/API'; |
| 1078 | 1078 | } |
| 1079 | 1079 | else |
| 1080 | 1080 | { |
| 1081 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1081 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 1082 | 1082 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 1083 | 1083 | } |
| 1084 | 1084 | } |
| 1085 | 1085 | else |
| 1086 | 1086 | { |
| 1087 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1087 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 1088 | 1088 | } |
| 1089 | - $this->trap_to_db=($action==-2)?false:true; |
|
| 1089 | + $this->trap_to_db=($action == -2) ?false:true; |
|
| 1090 | 1090 | } |
| 1091 | 1091 | else |
| 1092 | 1092 | { |
| 1093 | 1093 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 1094 | 1094 | |
| 1095 | 1095 | $action=$rule['action_nomatch']; |
| 1096 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
| 1096 | + $this->logging->log('action NOK : '.$action, INFO); |
|
| 1097 | 1097 | if ($action >= 0) |
| 1098 | 1098 | { |
| 1099 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 1099 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
| 1100 | 1100 | { |
| 1101 | 1101 | $this->trap_action.='Error sending status : check cmd/API'; |
| 1102 | 1102 | } |
| 1103 | 1103 | else |
| 1104 | 1104 | { |
| 1105 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1105 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 1106 | 1106 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 1107 | 1107 | } |
| 1108 | 1108 | } |
| 1109 | 1109 | else |
| 1110 | 1110 | { |
| 1111 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1111 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 1112 | 1112 | } |
| 1113 | - $this->trap_to_db=($action==-2)?false:true; |
|
| 1113 | + $this->trap_to_db=($action == -2) ?false:true; |
|
| 1114 | 1114 | } |
| 1115 | 1115 | // Put name in source_name |
| 1116 | 1116 | if (!isset($this->trap_data['source_name'])) |
@@ -1119,7 +1119,7 @@ discard block |
||
| 1119 | 1119 | } |
| 1120 | 1120 | else |
| 1121 | 1121 | { |
| 1122 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
|
| 1122 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trap_data['source_name'])) |
|
| 1123 | 1123 | { // only add if not present |
| 1124 | 1124 | $this->trap_data['source_name'].=','.$rule['host_name']; |
| 1125 | 1125 | } |
@@ -1127,13 +1127,13 @@ discard block |
||
| 1127 | 1127 | } |
| 1128 | 1128 | catch (Exception $e) |
| 1129 | 1129 | { |
| 1130 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 1130 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
| 1131 | 1131 | $this->trap_action.=' ERR : '.$e->getMessage(); |
| 1132 | 1132 | $this->trap_data['status']='error'; |
| 1133 | 1133 | } |
| 1134 | 1134 | |
| 1135 | 1135 | } |
| 1136 | - if ($this->trap_data['status']=='error') |
|
| 1136 | + if ($this->trap_data['status'] == 'error') |
|
| 1137 | 1137 | { |
| 1138 | 1138 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
| 1139 | 1139 | } |
@@ -1149,13 +1149,13 @@ discard block |
||
| 1149 | 1149 | public function add_rule_final($time) |
| 1150 | 1150 | { |
| 1151 | 1151 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 1152 | - if ($this->trap_action==null) |
|
| 1152 | + if ($this->trap_action == null) |
|
| 1153 | 1153 | { |
| 1154 | 1154 | $this->trap_action='No action'; |
| 1155 | 1155 | } |
| 1156 | 1156 | $sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."' WHERE (id = '".$this->trap_id."');"; |
| 1157 | 1157 | if ($db_conn->query($sql) === false) { |
| 1158 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 1158 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
| 1159 | 1159 | } |
| 1160 | 1160 | } |
| 1161 | 1161 | |
@@ -1178,7 +1178,7 @@ discard block |
||
| 1178 | 1178 | WHERE s.current_state != 0;"; |
| 1179 | 1179 | $db_conn=$this->trapsDB->db_connect_ido(); |
| 1180 | 1180 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
| 1181 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 1181 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
| 1182 | 1182 | return 0; |
| 1183 | 1183 | } |
| 1184 | 1184 | $services=$services_db->fetchAll(); |
@@ -1187,7 +1187,7 @@ discard block |
||
| 1187 | 1187 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;"; |
| 1188 | 1188 | $db_conn2=$this->trapsDB->db_connect_trap(); |
| 1189 | 1189 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
| 1190 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 1190 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
| 1191 | 1191 | return 0; |
| 1192 | 1192 | } |
| 1193 | 1193 | $rules=$rules_db->fetchAll(); |
@@ -1203,13 +1203,13 @@ discard block |
||
| 1203 | 1203 | $service['host_name'] == $rule['host_name'] && |
| 1204 | 1204 | ($service['last_check'] + $rule['revert_ok']) < $now) |
| 1205 | 1205 | { |
| 1206 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 1206 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 1207 | 1207 | $numreset++; |
| 1208 | 1208 | } |
| 1209 | 1209 | } |
| 1210 | 1210 | } |
| 1211 | 1211 | echo "\n"; |
| 1212 | - echo $numreset . " service(s) reset to OK\n"; |
|
| 1212 | + echo $numreset." service(s) reset to OK\n"; |
|
| 1213 | 1213 | return 0; |
| 1214 | 1214 | |
| 1215 | 1215 | } |
@@ -1230,7 +1230,7 @@ discard block |
||
| 1230 | 1230 | * @param string $description |
| 1231 | 1231 | * @return number : 0=unchanged, 1 = changed, 2=created |
| 1232 | 1232 | */ |
| 1233 | - public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
|
| 1233 | + public function update_oid($oid, $mib, $name, $type, $textConv, $dispHint, $syntax, $type_enum, $description=NULL) |
|
| 1234 | 1234 | { |
| 1235 | 1235 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 1236 | 1236 | $description=$db_conn->quote($description); |
@@ -1240,7 +1240,7 @@ discard block |
||
| 1240 | 1240 | { // newly created. |
| 1241 | 1241 | return 0; |
| 1242 | 1242 | } |
| 1243 | - if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 1243 | + if ($name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 1244 | 1244 | $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
| 1245 | 1245 | $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
| 1246 | 1246 | //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
@@ -1260,23 +1260,23 @@ discard block |
||
| 1260 | 1260 | ':name' => $name, |
| 1261 | 1261 | ':type' => $type, |
| 1262 | 1262 | ':mib' => $mib, |
| 1263 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1264 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1265 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1266 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1267 | - ':description' => ($description==null)?'null':$description, |
|
| 1263 | + ':tc' => ($textConv == null) ? 'null' : $textConv, |
|
| 1264 | + ':display_hint' => ($dispHint == null) ? 'null' : $dispHint, |
|
| 1265 | + ':syntax' => ($syntax == null) ? 'null' : $syntax, |
|
| 1266 | + ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, |
|
| 1267 | + ':description' => ($description == null) ? 'null' : $description, |
|
| 1268 | 1268 | ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
| 1269 | 1269 | ); |
| 1270 | 1270 | |
| 1271 | 1271 | if ($sqlQuery->execute($sqlParam) === false) { |
| 1272 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 1272 | + $this->logging->log('Error in query : '.$sql, ERROR, ''); |
|
| 1273 | 1273 | } |
| 1274 | - $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1274 | + $this->logging->log('Trap updated : '.$name.' / OID : '.$oid, DEBUG); |
|
| 1275 | 1275 | return 1; |
| 1276 | 1276 | } |
| 1277 | 1277 | else |
| 1278 | 1278 | { |
| 1279 | - $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 1279 | + $this->logging->log('Trap unchanged : '.$name.' / OID : '.$oid, DEBUG); |
|
| 1280 | 1280 | return 0; |
| 1281 | 1281 | } |
| 1282 | 1282 | } |
@@ -1286,11 +1286,11 @@ discard block |
||
| 1286 | 1286 | |
| 1287 | 1287 | $sql='INSERT INTO '.$this->db_prefix.'mib_cache '. |
| 1288 | 1288 | '(oid, name, type , mib, textual_convention, display_hint '. |
| 1289 | - ', syntax, type_enum , description ) ' . |
|
| 1289 | + ', syntax, type_enum , description ) '. |
|
| 1290 | 1290 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 1291 | 1291 | ', :syntax, :type_enum, :description )'; |
| 1292 | 1292 | |
| 1293 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 1293 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql.='RETURNING id'; |
|
| 1294 | 1294 | |
| 1295 | 1295 | $sqlQuery=$db_conn->prepare($sql); |
| 1296 | 1296 | |
@@ -1299,15 +1299,15 @@ discard block |
||
| 1299 | 1299 | ':name' => $name, |
| 1300 | 1300 | ':type' => $type, |
| 1301 | 1301 | ':mib' => $mib, |
| 1302 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 1303 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 1304 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 1305 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 1306 | - ':description' => ($description==null)?'null':$description |
|
| 1302 | + ':tc' => ($textConv == null) ? 'null' : $textConv, |
|
| 1303 | + ':display_hint' => ($dispHint == null) ? 'null' : $dispHint, |
|
| 1304 | + ':syntax' => ($syntax == null) ? 'null' : $syntax, |
|
| 1305 | + ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, |
|
| 1306 | + ':description' => ($description == null) ? 'null' : $description |
|
| 1307 | 1307 | ); |
| 1308 | 1308 | |
| 1309 | 1309 | if ($sqlQuery->execute($sqlParam) === false) { |
| 1310 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 1310 | + $this->logging->log('Error in query : '.$sql, 1, ''); |
|
| 1311 | 1311 | } |
| 1312 | 1312 | |
| 1313 | 1313 | switch ($this->trapsDB->trapDBType) |
@@ -1315,10 +1315,10 @@ discard block |
||
| 1315 | 1315 | case 'pgsql': |
| 1316 | 1316 | // Get last id to insert oid/values in secondary table |
| 1317 | 1317 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
| 1318 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 1318 | + $this->logging->log('Error getting id - pgsql - ', 1, ''); |
|
| 1319 | 1319 | } |
| 1320 | - if (! isset($inserted_id_ret['id'])) { |
|
| 1321 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 1320 | + if (!isset($inserted_id_ret['id'])) { |
|
| 1321 | + $this->logging->log('Error getting id - pgsql - empty.', 1, ''); |
|
| 1322 | 1322 | } |
| 1323 | 1323 | $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
| 1324 | 1324 | break; |
@@ -1326,15 +1326,15 @@ discard block |
||
| 1326 | 1326 | // Get last id to insert oid/values in secondary table |
| 1327 | 1327 | $sql='SELECT LAST_INSERT_ID();'; |
| 1328 | 1328 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 1329 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 1329 | + $this->logging->log('Erreur getting id - mysql - ', 1, ''); |
|
| 1330 | 1330 | } |
| 1331 | 1331 | |
| 1332 | 1332 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 1333 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1333 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1334 | 1334 | $this->dbOidIndex[$oid]['id']=$inserted_id; |
| 1335 | 1335 | break; |
| 1336 | 1336 | default: |
| 1337 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 1337 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
| 1338 | 1338 | } |
| 1339 | 1339 | |
| 1340 | 1340 | // Set as newly created. |
@@ -1349,21 +1349,21 @@ discard block |
||
| 1349 | 1349 | * @param array $objects : array of objects name (without MIB) |
| 1350 | 1350 | * @param bool $check_existing : check instead of create |
| 1351 | 1351 | */ |
| 1352 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 1352 | + public function trap_objects($trapOID, $trapmib, $objects, $check_existing) |
|
| 1353 | 1353 | { |
| 1354 | 1354 | $dbObjects=null; // cache of objects for trap in db |
| 1355 | 1355 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 1356 | 1356 | |
| 1357 | 1357 | // Get id of trapmib. |
| 1358 | 1358 | |
| 1359 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 1359 | + $trapId=$this->dbOidIndex[$trapOID]['id']; |
|
| 1360 | 1360 | if ($check_existing === true) |
| 1361 | 1361 | { |
| 1362 | 1362 | // Get all objects |
| 1363 | 1363 | $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
| 1364 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 1364 | + $this->logging->log('SQL query get all traps: '.$sql, DEBUG); |
|
| 1365 | 1365 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 1366 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 1366 | + $this->logging->log('No result in query : '.$sql, 1, ''); |
|
| 1367 | 1367 | } |
| 1368 | 1368 | $dbObjectsRaw=$ret_code->fetchAll(); |
| 1369 | 1369 | |
@@ -1377,39 +1377,39 @@ discard block |
||
| 1377 | 1377 | $match=$snmptrans=array(); |
| 1378 | 1378 | $retVal=0; |
| 1379 | 1379 | $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
| 1380 | - $tmpdesc='';$indesc=false; |
|
| 1380 | + $tmpdesc=''; $indesc=false; |
|
| 1381 | 1381 | |
| 1382 | 1382 | $objMib=$trapmib; |
| 1383 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1384 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1385 | - if ($retVal!=0) |
|
| 1383 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1384 | + ' -On -Td '.$objMib.'::'.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 1385 | + if ($retVal != 0) |
|
| 1386 | 1386 | { |
| 1387 | 1387 | // Maybe not trap mib, search with IR |
| 1388 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1389 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 1390 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 1388 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1389 | + ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 1390 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match)) |
|
| 1391 | 1391 | { // Not found -> continue with warning |
| 1392 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 1392 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, ''); |
|
| 1393 | 1393 | continue; |
| 1394 | 1394 | } |
| 1395 | 1395 | $objMib=$match[1]; |
| 1396 | 1396 | |
| 1397 | 1397 | // Do the snmptranslate again. |
| 1398 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1399 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 1400 | - if ($retVal!=0) { |
|
| 1401 | - $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 1398 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1399 | + ' -On -Td '.$objMib.'::'.$object, $snmptrans, $retVal); |
|
| 1400 | + if ($retVal != 0) { |
|
| 1401 | + $this->logging->log('Error finding trap object : '.$objMib.'::'.$object, 2, ''); |
|
| 1402 | 1402 | } |
| 1403 | 1403 | |
| 1404 | 1404 | } |
| 1405 | 1405 | foreach ($snmptrans as $line) |
| 1406 | 1406 | { |
| 1407 | - if ($indesc===true) |
|
| 1407 | + if ($indesc === true) |
|
| 1408 | 1408 | { |
| 1409 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 1410 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 1409 | + $line=preg_replace('/[\t ]+/', ' ', $line); |
|
| 1410 | + if (preg_match('/(.*)"$/', $line, $match)) |
|
| 1411 | 1411 | { |
| 1412 | - $objDesc = $tmpdesc . $match[1]; |
|
| 1412 | + $objDesc=$tmpdesc.$match[1]; |
|
| 1413 | 1413 | $indesc=false; |
| 1414 | 1414 | } |
| 1415 | 1415 | $tmpdesc.=$line; |
@@ -1420,43 +1420,43 @@ discard block |
||
| 1420 | 1420 | $objOid=$line; |
| 1421 | 1421 | continue; |
| 1422 | 1422 | } |
| 1423 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 1423 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match)) |
|
| 1424 | 1424 | { |
| 1425 | 1425 | $objSyntax=$match[1]; |
| 1426 | 1426 | $objEnum=$match[2]; |
| 1427 | 1427 | continue; |
| 1428 | 1428 | } |
| 1429 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 1429 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match)) |
|
| 1430 | 1430 | { |
| 1431 | 1431 | $objSyntax=$match[1]; |
| 1432 | 1432 | continue; |
| 1433 | 1433 | } |
| 1434 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 1434 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match)) |
|
| 1435 | 1435 | { |
| 1436 | 1436 | $objDispHint=$match[1]; |
| 1437 | 1437 | continue; |
| 1438 | 1438 | } |
| 1439 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 1439 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match)) |
|
| 1440 | 1440 | { |
| 1441 | 1441 | $objDesc=$match[1]; |
| 1442 | 1442 | continue; |
| 1443 | 1443 | } |
| 1444 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 1444 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match)) |
|
| 1445 | 1445 | { |
| 1446 | 1446 | $tmpdesc=$match[1]; |
| 1447 | 1447 | $indesc=true; |
| 1448 | 1448 | continue; |
| 1449 | 1449 | } |
| 1450 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 1450 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match)) |
|
| 1451 | 1451 | { |
| 1452 | 1452 | $objTc=$match[1]; |
| 1453 | 1453 | continue; |
| 1454 | 1454 | } |
| 1455 | 1455 | } |
| 1456 | - $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 1456 | + $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc", DEBUG); |
|
| 1457 | 1457 | //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
| 1458 | 1458 | // Update |
| 1459 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 1459 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum, $objDesc); |
|
| 1460 | 1460 | |
| 1461 | 1461 | if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
| 1462 | 1462 | { // if link exists, continue |
@@ -1477,7 +1477,7 @@ discard block |
||
| 1477 | 1477 | ); |
| 1478 | 1478 | |
| 1479 | 1479 | if ($sqlQuery->execute($sqlParam) === false) { |
| 1480 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 1480 | + $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$objOid]['id'], 1, ''); |
|
| 1481 | 1481 | } |
| 1482 | 1482 | } |
| 1483 | 1483 | if ($check_existing === true) |
@@ -1494,19 +1494,19 @@ discard block |
||
| 1494 | 1494 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
| 1495 | 1495 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
| 1496 | 1496 | */ |
| 1497 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 1497 | + public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1') |
|
| 1498 | 1498 | { |
| 1499 | 1499 | // Timing |
| 1500 | - $timeTaken = microtime(true); |
|
| 1500 | + $timeTaken=microtime(true); |
|
| 1501 | 1501 | $retVal=0; |
| 1502 | 1502 | // Get all mib objects from all mibs |
| 1503 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 1504 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 1503 | + $snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 1504 | + $this->logging->log('Getting all traps : '.$snmpCommand, DEBUG); |
|
| 1505 | 1505 | unset($this->objectsAll); |
| 1506 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 1507 | - if ($retVal!=0) |
|
| 1506 | + exec($snmpCommand, $this->objectsAll, $retVal); |
|
| 1507 | + if ($retVal != 0) |
|
| 1508 | 1508 | { |
| 1509 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 1509 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
| 1510 | 1510 | } |
| 1511 | 1511 | |
| 1512 | 1512 | // Get all mibs from databse to have a memory index |
@@ -1514,14 +1514,14 @@ discard block |
||
| 1514 | 1514 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 1515 | 1515 | |
| 1516 | 1516 | $sql='SELECT * from '.$this->db_prefix.'mib_cache;'; |
| 1517 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 1517 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 1518 | 1518 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 1519 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 1519 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 1520 | 1520 | } |
| 1521 | 1521 | $this->dbOidAll=$ret_code->fetchAll(); |
| 1522 | 1522 | $this->dbOidIndex=array(); |
| 1523 | 1523 | // Create the index for db; |
| 1524 | - foreach($this->dbOidAll as $key=>$val) |
|
| 1524 | + foreach ($this->dbOidAll as $key=>$val) |
|
| 1525 | 1525 | { |
| 1526 | 1526 | $this->dbOidIndex[$val['oid']]['key']=$key; |
| 1527 | 1527 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
@@ -1529,11 +1529,11 @@ discard block |
||
| 1529 | 1529 | |
| 1530 | 1530 | // Count elements to show progress |
| 1531 | 1531 | $numElements=count($this->objectsAll); |
| 1532 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 1532 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO); |
|
| 1533 | 1533 | |
| 1534 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 1534 | + $step=$basestep=$numElements / 10; // output display of % done |
|
| 1535 | 1535 | $num_step=0; |
| 1536 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 1536 | + $timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds |
|
| 1537 | 1537 | |
| 1538 | 1538 | // Create index for trap objects |
| 1539 | 1539 | $this->trapObjectsIndex=array(); |
@@ -1543,28 +1543,28 @@ discard block |
||
| 1543 | 1543 | $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
| 1544 | 1544 | $time_num_traps=0; |
| 1545 | 1545 | |
| 1546 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 1546 | + for ($curElement=0; $curElement < $numElements; $curElement++) |
|
| 1547 | 1547 | { |
| 1548 | - $time_1= microtime(true); |
|
| 1549 | - if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
|
| 1548 | + $time_1=microtime(true); |
|
| 1549 | + if ((microtime(true) - $timeFiveSec) > 2 && $display_progress) |
|
| 1550 | 1550 | { // echo a . every 2 sec |
| 1551 | 1551 | echo '.'; |
| 1552 | - $timeFiveSec = microtime(true); |
|
| 1552 | + $timeFiveSec=microtime(true); |
|
| 1553 | 1553 | } |
| 1554 | - if ($curElement>$step) |
|
| 1554 | + if ($curElement > $step) |
|
| 1555 | 1555 | { // display progress |
| 1556 | 1556 | $num_step++; |
| 1557 | 1557 | $step+=$basestep; |
| 1558 | 1558 | if ($display_progress) |
| 1559 | 1559 | { |
| 1560 | - echo "\n" . ($num_step*10). '% : '; |
|
| 1560 | + echo "\n".($num_step * 10).'% : '; |
|
| 1561 | 1561 | } |
| 1562 | 1562 | } |
| 1563 | 1563 | // Get oid or pass if not found |
| 1564 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 1564 | + if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement])) |
|
| 1565 | 1565 | { |
| 1566 | - $time_parse1 += microtime(true) - $time_1; |
|
| 1567 | - $time_parse1N ++; |
|
| 1566 | + $time_parse1+=microtime(true) - $time_1; |
|
| 1567 | + $time_parse1N++; |
|
| 1568 | 1568 | continue; |
| 1569 | 1569 | } |
| 1570 | 1570 | $oid=$this->objectsAll[$curElement]; |
@@ -1573,9 +1573,9 @@ discard block |
||
| 1573 | 1573 | $curElement++; |
| 1574 | 1574 | $match=$snmptrans=array(); |
| 1575 | 1575 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
| 1576 | - $this->objectsAll[$curElement],$match)) |
|
| 1576 | + $this->objectsAll[$curElement], $match)) |
|
| 1577 | 1577 | { |
| 1578 | - $time_check1 += microtime(true) - $time_1; |
|
| 1578 | + $time_check1+=microtime(true) - $time_1; |
|
| 1579 | 1579 | $time_check1N++; |
| 1580 | 1580 | continue; |
| 1581 | 1581 | } |
@@ -1583,21 +1583,21 @@ discard block |
||
| 1583 | 1583 | $name=$match[1]; // Name |
| 1584 | 1584 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 1585 | 1585 | |
| 1586 | - if ($type==0) // object type=0 : check if v1 trap |
|
| 1586 | + if ($type == 0) // object type=0 : check if v1 trap |
|
| 1587 | 1587 | { |
| 1588 | 1588 | // Check if next is suboid -> in that case is cannot be a trap |
| 1589 | - if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
|
| 1589 | + if (preg_match("/^$oid/", $this->objectsAll[$curElement + 1])) |
|
| 1590 | 1590 | { |
| 1591 | - $time_check2 += microtime(true) - $time_1; |
|
| 1591 | + $time_check2+=microtime(true) - $time_1; |
|
| 1592 | 1592 | $time_check2N++; |
| 1593 | 1593 | continue; |
| 1594 | 1594 | } |
| 1595 | 1595 | unset($snmptrans); |
| 1596 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1597 | - ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 1598 | - if ($retVal!=0) |
|
| 1596 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1597 | + ' -Td '.$oid.' | grep OBJECTS ', $snmptrans, $retVal); |
|
| 1598 | + if ($retVal != 0) |
|
| 1599 | 1599 | { |
| 1600 | - $time_check2 += microtime(true) - $time_1; |
|
| 1600 | + $time_check2+=microtime(true) - $time_1; |
|
| 1601 | 1601 | $time_check2N++; |
| 1602 | 1602 | continue; |
| 1603 | 1603 | } |
@@ -1605,56 +1605,56 @@ discard block |
||
| 1605 | 1605 | // Force as trap. |
| 1606 | 1606 | $type=21; |
| 1607 | 1607 | } |
| 1608 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 1608 | + if ($onlyTraps === true && $type != 21) // if only traps and not a trap, continue |
|
| 1609 | 1609 | { |
| 1610 | - $time_check3 += microtime(true) - $time_1; |
|
| 1610 | + $time_check3+=microtime(true) - $time_1; |
|
| 1611 | 1611 | $time_check3N++; |
| 1612 | 1612 | continue; |
| 1613 | 1613 | } |
| 1614 | 1614 | |
| 1615 | 1615 | $time_num_traps++; |
| 1616 | 1616 | |
| 1617 | - $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
|
| 1617 | + $this->logging->log('Found trap : '.$match[1].' / OID : '.$oid, INFO); |
|
| 1618 | 1618 | if ($display_progress) echo '#'; // echo a # when trap found |
| 1619 | 1619 | |
| 1620 | 1620 | // get trap objects & source MIB |
| 1621 | 1621 | unset($snmptrans); |
| 1622 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1623 | - ' -Td '.$oid,$snmptrans,$retVal); |
|
| 1624 | - if ($retVal!=0) |
|
| 1622 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 1623 | + ' -Td '.$oid, $snmptrans, $retVal); |
|
| 1624 | + if ($retVal != 0) |
|
| 1625 | 1625 | { |
| 1626 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 1626 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
| 1627 | 1627 | } |
| 1628 | 1628 | |
| 1629 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 1629 | + if (!preg_match('/^(.*)::/', $snmptrans[0], $match)) |
|
| 1630 | 1630 | { |
| 1631 | - $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 1631 | + $this->logging->log('Error getting mib from trap '.$oid.' : '.$snmptrans[0], 1, ''); |
|
| 1632 | 1632 | } |
| 1633 | 1633 | $trapMib=$match[1]; |
| 1634 | 1634 | |
| 1635 | - $numLine=1;$trapDesc=''; |
|
| 1636 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 1635 | + $numLine=1; $trapDesc=''; |
|
| 1636 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++; |
|
| 1637 | 1637 | if (isset($snmptrans[$numLine])) |
| 1638 | 1638 | { |
| 1639 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 1639 | + $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]); |
|
| 1640 | 1640 | |
| 1641 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 1641 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine])) |
|
| 1642 | 1642 | { |
| 1643 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 1643 | + $trapDesc.=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]); |
|
| 1644 | 1644 | $numLine++; |
| 1645 | 1645 | } |
| 1646 | 1646 | if (isset($snmptrans[$numLine])) { |
| 1647 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 1648 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 1647 | + $trapDesc.=preg_replace('/".*/', '', $snmptrans[$numLine]); |
|
| 1648 | + $trapDesc=preg_replace('/[\t ]+/', ' ', $trapDesc); |
|
| 1649 | 1649 | } |
| 1650 | 1650 | |
| 1651 | 1651 | } |
| 1652 | - $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
|
| 1653 | - $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
|
| 1652 | + $update=$this->update_oid($oid, $trapMib, $name, $type, NULL, NULL, NULL, NULL, $trapDesc); |
|
| 1653 | + $time_update+=microtime(true) - $time_1; $time_1=microtime(true); |
|
| 1654 | 1654 | |
| 1655 | - if (($update==0) && ($check_change===false)) |
|
| 1655 | + if (($update == 0) && ($check_change === false)) |
|
| 1656 | 1656 | { // Trapd didn't change & force check disabled |
| 1657 | - $time_objects += microtime(true) - $time_1; |
|
| 1657 | + $time_objects+=microtime(true) - $time_1; |
|
| 1658 | 1658 | if ($display_progress) echo "C"; |
| 1659 | 1659 | continue; |
| 1660 | 1660 | } |
@@ -1662,7 +1662,7 @@ discard block |
||
| 1662 | 1662 | $synt=null; |
| 1663 | 1663 | foreach ($snmptrans as $line) |
| 1664 | 1664 | { |
| 1665 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 1665 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match)) |
|
| 1666 | 1666 | { |
| 1667 | 1667 | $synt=$match[1]; |
| 1668 | 1668 | } |
@@ -1670,30 +1670,30 @@ discard block |
||
| 1670 | 1670 | if ($synt == null) |
| 1671 | 1671 | { |
| 1672 | 1672 | //echo "No objects for $trapOID\n"; |
| 1673 | - $time_objects += microtime(true) - $time_1; |
|
| 1673 | + $time_objects+=microtime(true) - $time_1; |
|
| 1674 | 1674 | continue; |
| 1675 | 1675 | } |
| 1676 | 1676 | //echo "$synt \n"; |
| 1677 | 1677 | $trapObjects=array(); |
| 1678 | - while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 1678 | + while (preg_match('/ *([^ ,]+) *,* */', $synt, $match)) |
|
| 1679 | 1679 | { |
| 1680 | - array_push($trapObjects,$match[1]); |
|
| 1681 | - $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 1680 | + array_push($trapObjects, $match[1]); |
|
| 1681 | + $synt=preg_replace('/'.$match[0].'/', '', $synt); |
|
| 1682 | 1682 | } |
| 1683 | 1683 | |
| 1684 | 1684 | $this->trap_objects($oid, $trapMib, $trapObjects, false); |
| 1685 | 1685 | |
| 1686 | - $time_objects += microtime(true) - $time_1; |
|
| 1686 | + $time_objects+=microtime(true) - $time_1; |
|
| 1687 | 1687 | $time_objectsN++; |
| 1688 | 1688 | } |
| 1689 | 1689 | |
| 1690 | 1690 | if ($display_progress) |
| 1691 | 1691 | { |
| 1692 | 1692 | echo "\nNumber of processed traps : $time_num_traps \n"; |
| 1693 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 1694 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 1695 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 1696 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 1693 | + echo "\nParsing : ".number_format($time_parse1 + $time_check1, 1)." sec / ".($time_parse1N + $time_check1N)." occurences\n"; |
|
| 1694 | + echo "Detecting traps : ".number_format($time_check2 + $time_check3, 1)." sec / ".($time_check2N + $time_check3N)." occurences\n"; |
|
| 1695 | + echo "Trap processing ($time_updateN): ".number_format($time_update, 1)." sec , "; |
|
| 1696 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects, 1)." sec \n"; |
|
| 1697 | 1697 | } |
| 1698 | 1698 | |
| 1699 | 1699 | // Timing ends |
@@ -68,9 +68,9 @@ discard block |
||
| 68 | 68 | { |
| 69 | 69 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
| 70 | 70 | $this->logSetup=true; |
| 71 | + } else { |
|
| 72 | + $this->logSetup=false; |
|
| 71 | 73 | } |
| 72 | - else |
|
| 73 | - $this->logSetup=false; |
|
| 74 | 74 | $this->logging->log('Loggin started', INFO); |
| 75 | 75 | |
| 76 | 76 | // Get options from ini files |
@@ -83,7 +83,10 @@ discard block |
||
| 83 | 83 | $this->setupDatabase($trapConfig); // Setup database class |
| 84 | 84 | |
| 85 | 85 | $this->getDatabaseOptions(); // Get options in database |
| 86 | - if ($this->api_use === true) $this->getAPI(); // Setup API |
|
| 86 | + if ($this->api_use === true) { |
|
| 87 | + $this->getAPI(); |
|
| 88 | + } |
|
| 89 | + // Setup API |
|
| 87 | 90 | |
| 88 | 91 | $this->mibClass = new Mib($this->logging,$this->trapsDB); // Create Mib class |
| 89 | 92 | |
@@ -117,8 +120,7 @@ discard block |
||
| 117 | 120 | } |
| 118 | 121 | $this->logging->log($message,$log_level,'syslog'); |
| 119 | 122 | return false; |
| 120 | - } |
|
| 121 | - else |
|
| 123 | + } else |
|
| 122 | 124 | { |
| 123 | 125 | $option_var=$option_array[$option_category][$option_name]; |
| 124 | 126 | return true; |
@@ -182,7 +184,10 @@ discard block |
||
| 182 | 184 | |
| 183 | 185 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
| 184 | 186 | |
| 185 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 187 | + if ($this->api_use === true) { |
|
| 188 | + return; |
|
| 189 | + } |
|
| 190 | + // In case of API use, no IDO is necessary |
|
| 186 | 191 | |
| 187 | 192 | // IDO Database |
| 188 | 193 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -207,9 +212,11 @@ discard block |
||
| 207 | 212 | protected function getDatabaseOptions() |
| 208 | 213 | { |
| 209 | 214 | // Database options |
| 210 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 215 | + if ($this->logSetup === false) { |
|
| 216 | + // Only if logging was no setup in constructor |
|
| 211 | 217 | { |
| 212 | 218 | $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
| 219 | + } |
|
| 213 | 220 | $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
| 214 | 221 | $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
| 215 | 222 | } |
@@ -218,7 +225,9 @@ discard block |
||
| 218 | 225 | protected function getDBConfigIfSet($element,&$variable) |
| 219 | 226 | { |
| 220 | 227 | $value=$this->getDBConfig($element); |
| 221 | - if ($value != 'null') $variable=$value; |
|
| 228 | + if ($value != 'null') { |
|
| 229 | + $variable=$value; |
|
| 230 | + } |
|
| 222 | 231 | } |
| 223 | 232 | |
| 224 | 233 | /** |
@@ -306,8 +315,7 @@ discard block |
||
| 306 | 315 | { |
| 307 | 316 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 308 | 317 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 309 | - } |
|
| 310 | - else |
|
| 318 | + } else |
|
| 311 | 319 | { |
| 312 | 320 | $this->trap_data['source_ip']=$matches[1]; |
| 313 | 321 | $this->trap_data['destination_ip']=$matches[3]; |
@@ -322,14 +330,12 @@ discard block |
||
| 322 | 330 | if ($ret_code===0 || $ret_code===false) |
| 323 | 331 | { |
| 324 | 332 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
| 325 | - } |
|
| 326 | - else |
|
| 333 | + } else |
|
| 327 | 334 | { |
| 328 | 335 | 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')) |
| 329 | 336 | { |
| 330 | 337 | $this->trap_data['trap_oid']=$matches[2]; |
| 331 | - } |
|
| 332 | - else |
|
| 338 | + } else |
|
| 333 | 339 | { |
| 334 | 340 | $object= new stdClass; |
| 335 | 341 | $object->oid =$matches[1]; |
@@ -500,7 +506,9 @@ discard block |
||
| 500 | 506 | } |
| 501 | 507 | |
| 502 | 508 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 503 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 509 | + if ($inserted_id==false) { |
|
| 510 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 511 | + } |
|
| 504 | 512 | $this->trap_id=$inserted_id; |
| 505 | 513 | break; |
| 506 | 514 | default: |
@@ -516,7 +524,9 @@ discard block |
||
| 516 | 524 | { |
| 517 | 525 | |
| 518 | 526 | // If action is ignore -> don't send t DB |
| 519 | - if ($this->trap_to_db === false) return; |
|
| 527 | + if ($this->trap_to_db === false) { |
|
| 528 | + return; |
|
| 529 | + } |
|
| 520 | 530 | |
| 521 | 531 | |
| 522 | 532 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -573,7 +583,9 @@ discard block |
||
| 573 | 583 | } |
| 574 | 584 | |
| 575 | 585 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 576 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 586 | + if ($inserted_id==false) { |
|
| 587 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 588 | + } |
|
| 577 | 589 | $this->trap_id=$inserted_id; |
| 578 | 590 | break; |
| 579 | 591 | default: |
@@ -702,8 +714,7 @@ discard block |
||
| 702 | 714 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 703 | 715 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
| 704 | 716 | return true; |
| 705 | - } |
|
| 706 | - else |
|
| 717 | + } else |
|
| 707 | 718 | { |
| 708 | 719 | $api = $this->getAPI(); |
| 709 | 720 | $api->setCredentials($this->api_username, $this->api_password); |
@@ -712,8 +723,7 @@ discard block |
||
| 712 | 723 | { |
| 713 | 724 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
| 714 | 725 | return false; |
| 715 | - } |
|
| 716 | - else |
|
| 726 | + } else |
|
| 717 | 727 | { |
| 718 | 728 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
| 719 | 729 | return true; |
@@ -773,7 +783,9 @@ discard block |
||
| 773 | 783 | /***************** Eval & tokenizer functions ****************/ |
| 774 | 784 | protected function eval_getElement($rule,&$item) |
| 775 | 785 | { |
| 776 | - while ($rule[$item]==' ') $item++; |
|
| 786 | + while ($rule[$item]==' ') { |
|
| 787 | + $item++; |
|
| 788 | + } |
|
| 777 | 789 | if (preg_match('/[0-9\.]/',$rule[$item])) |
| 778 | 790 | { // number |
| 779 | 791 | |
@@ -800,14 +812,15 @@ discard block |
||
| 800 | 812 | $start=$item; |
| 801 | 813 | $parenthesis_count=0; |
| 802 | 814 | while (($item < strlen($rule)) // Not end of string AND |
| 803 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 815 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) { |
|
| 816 | + // Closing ')' or embeded () |
|
| 804 | 817 | { |
| 805 | 818 | if ($rule[$item] == '"' ) |
| 806 | 819 | { // pass through string |
| 807 | 820 | $item++; |
| 821 | + } |
|
| 808 | 822 | $item=$this->eval_getNext($rule,$item,'"'); |
| 809 | - } |
|
| 810 | - else{ |
|
| 823 | + } else{ |
|
| 811 | 824 | if ($rule[$item] == '(') |
| 812 | 825 | { |
| 813 | 826 | $parenthesis_count++; |
@@ -835,13 +848,17 @@ discard block |
||
| 835 | 848 | protected function eval_getNext($rule,$item,$tok) |
| 836 | 849 | { |
| 837 | 850 | while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;} |
| 838 | - if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 851 | + if ($item==strlen($rule)) { |
|
| 852 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 853 | + } |
|
| 839 | 854 | return $item+1; |
| 840 | 855 | } |
| 841 | 856 | |
| 842 | 857 | protected function eval_getOper($rule,&$item) |
| 843 | 858 | { |
| 844 | - while ($rule[$item]==' ') $item++; |
|
| 859 | + while ($rule[$item]==' ') { |
|
| 860 | + $item++; |
|
| 861 | + } |
|
| 845 | 862 | switch ($rule[$item]) |
| 846 | 863 | { |
| 847 | 864 | case '<': |
@@ -877,12 +894,13 @@ discard block |
||
| 877 | 894 | public function evaluation($rule,&$item) |
| 878 | 895 | { |
| 879 | 896 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
| 880 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 897 | + if ( $rule[$item] == '!') { |
|
| 898 | + // If '!' found, negate next expression. |
|
| 881 | 899 | { |
| 882 | 900 | $negate=true; |
| 883 | - $item++; |
|
| 884 | 901 | } |
| 885 | - else |
|
| 902 | + $item++; |
|
| 903 | + } else |
|
| 886 | 904 | { |
| 887 | 905 | $negate=false; |
| 888 | 906 | } |
@@ -890,10 +908,14 @@ discard block |
||
| 890 | 908 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
| 891 | 909 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 892 | 910 | |
| 893 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 911 | + if ($item==strlen($rule)) { |
|
| 912 | + // If only element, return value, but only boolean |
|
| 894 | 913 | { |
| 895 | 914 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 896 | - if ($negate === true) $val1= ! $val1; |
|
| 915 | + } |
|
| 916 | + if ($negate === true) { |
|
| 917 | + $val1= ! $val1; |
|
| 918 | + } |
|
| 897 | 919 | return $val1; |
| 898 | 920 | } |
| 899 | 921 | |
@@ -902,27 +924,34 @@ discard block |
||
| 902 | 924 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 903 | 925 | |
| 904 | 926 | // Third element : number, string or () |
| 905 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 927 | + if ( $rule[$item] == '!') { |
|
| 928 | + // starts with a ! so evaluate whats next |
|
| 906 | 929 | { |
| 907 | 930 | $item++; |
| 908 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 931 | + } |
|
| 932 | + if ($typec != 1) { |
|
| 933 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 934 | + } |
|
| 909 | 935 | $val2= ! $this->evaluation($rule,$item); |
| 910 | 936 | $type2=2; // result is a boolean |
| 911 | - } |
|
| 912 | - else |
|
| 937 | + } else |
|
| 913 | 938 | { |
| 914 | 939 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
| 915 | 940 | } |
| 916 | 941 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 917 | 942 | |
| 918 | - if ($type1!=$type2) // cannot compare different types |
|
| 943 | + if ($type1!=$type2) { |
|
| 944 | + // cannot compare different types |
|
| 919 | 945 | { |
| 920 | 946 | throw new Exception("Cannot compare string & number : ".$rule); |
| 921 | 947 | } |
| 922 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 948 | + } |
|
| 949 | + if ($typec==1 && $type1 !=2) { |
|
| 950 | + // cannot use & or | with string/number |
|
| 923 | 951 | { |
| 924 | 952 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 925 | 953 | } |
| 954 | + } |
|
| 926 | 955 | |
| 927 | 956 | switch ($comp){ |
| 928 | 957 | case '<': $retVal= ($val1 < $val2); break; |
@@ -936,9 +965,15 @@ discard block |
||
| 936 | 965 | case '&': $retVal= ($val1 && $val2); break; |
| 937 | 966 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 938 | 967 | } |
| 939 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 968 | + if ($negate === true) { |
|
| 969 | + $retVal = ! $retVal; |
|
| 970 | + } |
|
| 971 | + // Inverse result if negate before expression |
|
| 940 | 972 | |
| 941 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 973 | + if ($item==strlen($rule)) { |
|
| 974 | + return $retVal; |
|
| 975 | + } |
|
| 976 | + // End of string : return evaluation |
|
| 942 | 977 | // check for logical operator : |
| 943 | 978 | switch ($rule[$item]) |
| 944 | 979 | { |
@@ -965,7 +1000,9 @@ discard block |
||
| 965 | 1000 | $rule2.=$rule[$item]; |
| 966 | 1001 | $item++; |
| 967 | 1002 | } |
| 968 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 1003 | + if ($item == strlen ($rule)) { |
|
| 1004 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 1005 | + } |
|
| 969 | 1006 | $rule2.=$rule[$item]; |
| 970 | 1007 | $item++; |
| 971 | 1008 | continue; |
@@ -985,10 +1022,12 @@ discard block |
||
| 985 | 1022 | |
| 986 | 1023 | protected function eval_rule($rule) |
| 987 | 1024 | { |
| 988 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 1025 | + if ($rule==null || $rule == '') { |
|
| 1026 | + // Empty rule is always true |
|
| 989 | 1027 | { |
| 990 | 1028 | return true; |
| 991 | 1029 | } |
| 1030 | + } |
|
| 992 | 1031 | $matches=array(); |
| 993 | 1032 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
| 994 | 1033 | { |
@@ -1075,20 +1114,17 @@ discard block |
||
| 1075 | 1114 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 1076 | 1115 | { |
| 1077 | 1116 | $this->trap_action.='Error sending status : check cmd/API'; |
| 1078 | - } |
|
| 1079 | - else |
|
| 1117 | + } else |
|
| 1080 | 1118 | { |
| 1081 | 1119 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1082 | 1120 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 1083 | 1121 | } |
| 1084 | - } |
|
| 1085 | - else |
|
| 1122 | + } else |
|
| 1086 | 1123 | { |
| 1087 | 1124 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1088 | 1125 | } |
| 1089 | 1126 | $this->trap_to_db=($action==-2)?false:true; |
| 1090 | - } |
|
| 1091 | - else |
|
| 1127 | + } else |
|
| 1092 | 1128 | { |
| 1093 | 1129 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 1094 | 1130 | |
@@ -1099,14 +1135,12 @@ discard block |
||
| 1099 | 1135 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
| 1100 | 1136 | { |
| 1101 | 1137 | $this->trap_action.='Error sending status : check cmd/API'; |
| 1102 | - } |
|
| 1103 | - else |
|
| 1138 | + } else |
|
| 1104 | 1139 | { |
| 1105 | 1140 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1106 | 1141 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 1107 | 1142 | } |
| 1108 | - } |
|
| 1109 | - else |
|
| 1143 | + } else |
|
| 1110 | 1144 | { |
| 1111 | 1145 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 1112 | 1146 | } |
@@ -1116,16 +1150,14 @@ discard block |
||
| 1116 | 1150 | if (!isset($this->trap_data['source_name'])) |
| 1117 | 1151 | { |
| 1118 | 1152 | $this->trap_data['source_name']=$rule['host_name']; |
| 1119 | - } |
|
| 1120 | - else |
|
| 1153 | + } else |
|
| 1121 | 1154 | { |
| 1122 | 1155 | if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
| 1123 | 1156 | { // only add if not present |
| 1124 | 1157 | $this->trap_data['source_name'].=','.$rule['host_name']; |
| 1125 | 1158 | } |
| 1126 | 1159 | } |
| 1127 | - } |
|
| 1128 | - catch (Exception $e) |
|
| 1160 | + } catch (Exception $e) |
|
| 1129 | 1161 | { |
| 1130 | 1162 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
| 1131 | 1163 | $this->trap_action.=' ERR : '.$e->getMessage(); |
@@ -1136,8 +1168,7 @@ discard block |
||
| 1136 | 1168 | if ($this->trap_data['status']=='error') |
| 1137 | 1169 | { |
| 1138 | 1170 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
| 1139 | - } |
|
| 1140 | - else |
|
| 1171 | + } else |
|
| 1141 | 1172 | { |
| 1142 | 1173 | $this->trap_data['status']='done'; |
| 1143 | 1174 | } |
@@ -1273,8 +1304,7 @@ discard block |
||
| 1273 | 1304 | } |
| 1274 | 1305 | $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
| 1275 | 1306 | return 1; |
| 1276 | - } |
|
| 1277 | - else |
|
| 1307 | + } else |
|
| 1278 | 1308 | { |
| 1279 | 1309 | $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
| 1280 | 1310 | return 0; |
@@ -1290,7 +1320,9 @@ discard block |
||
| 1290 | 1320 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 1291 | 1321 | ', :syntax, :type_enum, :description )'; |
| 1292 | 1322 | |
| 1293 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 1323 | + if ($this->trapsDB->trapDBType == 'pgsql') { |
|
| 1324 | + $sql .= 'RETURNING id'; |
|
| 1325 | + } |
|
| 1294 | 1326 | |
| 1295 | 1327 | $sqlQuery=$db_conn->prepare($sql); |
| 1296 | 1328 | |
@@ -1330,7 +1362,9 @@ discard block |
||
| 1330 | 1362 | } |
| 1331 | 1363 | |
| 1332 | 1364 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 1333 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1365 | + if ($inserted_id==false) { |
|
| 1366 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 1367 | + } |
|
| 1334 | 1368 | $this->dbOidIndex[$oid]['id']=$inserted_id; |
| 1335 | 1369 | break; |
| 1336 | 1370 | default: |
@@ -1583,12 +1617,14 @@ discard block |
||
| 1583 | 1617 | $name=$match[1]; // Name |
| 1584 | 1618 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 1585 | 1619 | |
| 1586 | - if ($type==0) // object type=0 : check if v1 trap |
|
| 1620 | + if ($type==0) { |
|
| 1621 | + // object type=0 : check if v1 trap |
|
| 1587 | 1622 | { |
| 1588 | 1623 | // Check if next is suboid -> in that case is cannot be a trap |
| 1589 | 1624 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
| 1590 | 1625 | { |
| 1591 | 1626 | $time_check2 += microtime(true) - $time_1; |
| 1627 | + } |
|
| 1592 | 1628 | $time_check2N++; |
| 1593 | 1629 | continue; |
| 1594 | 1630 | } |
@@ -1605,9 +1641,11 @@ discard block |
||
| 1605 | 1641 | // Force as trap. |
| 1606 | 1642 | $type=21; |
| 1607 | 1643 | } |
| 1608 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 1644 | + if ($onlyTraps===true && $type!=21) { |
|
| 1645 | + // if only traps and not a trap, continue |
|
| 1609 | 1646 | { |
| 1610 | 1647 | $time_check3 += microtime(true) - $time_1; |
| 1648 | + } |
|
| 1611 | 1649 | $time_check3N++; |
| 1612 | 1650 | continue; |
| 1613 | 1651 | } |
@@ -1615,7 +1653,10 @@ discard block |
||
| 1615 | 1653 | $time_num_traps++; |
| 1616 | 1654 | |
| 1617 | 1655 | $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
| 1618 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 1656 | + if ($display_progress) { |
|
| 1657 | + echo '#'; |
|
| 1658 | + } |
|
| 1659 | + // echo a # when trap found |
|
| 1619 | 1660 | |
| 1620 | 1661 | // get trap objects & source MIB |
| 1621 | 1662 | unset($snmptrans); |
@@ -1633,7 +1674,9 @@ discard block |
||
| 1633 | 1674 | $trapMib=$match[1]; |
| 1634 | 1675 | |
| 1635 | 1676 | $numLine=1;$trapDesc=''; |
| 1636 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 1677 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) { |
|
| 1678 | + $numLine++; |
|
| 1679 | + } |
|
| 1637 | 1680 | if (isset($snmptrans[$numLine])) |
| 1638 | 1681 | { |
| 1639 | 1682 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
@@ -1655,7 +1698,9 @@ discard block |
||
| 1655 | 1698 | if (($update==0) && ($check_change===false)) |
| 1656 | 1699 | { // Trapd didn't change & force check disabled |
| 1657 | 1700 | $time_objects += microtime(true) - $time_1; |
| 1658 | - if ($display_progress) echo "C"; |
|
| 1701 | + if ($display_progress) { |
|
| 1702 | + echo "C"; |
|
| 1703 | + } |
|
| 1659 | 1704 | continue; |
| 1660 | 1705 | } |
| 1661 | 1706 | |
@@ -11,299 +11,299 @@ |
||
| 11 | 11 | class Database |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - // Databases |
|
| 15 | - protected $trapDB=null; //< trap database |
|
| 16 | - protected $idoDB=null; //< ido database |
|
| 17 | - public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 18 | - public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 14 | + // Databases |
|
| 15 | + protected $trapDB=null; //< trap database |
|
| 16 | + protected $idoDB=null; //< ido database |
|
| 17 | + public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 18 | + public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 19 | 19 | |
| 20 | - protected $trapDSN; //< trap database connection params |
|
| 21 | - protected $trapUsername; //< trap database connection params |
|
| 22 | - protected $trapPass; //< trap database connection params |
|
| 23 | - public $dbPrefix; //< database tables prefix |
|
| 20 | + protected $trapDSN; //< trap database connection params |
|
| 21 | + protected $trapUsername; //< trap database connection params |
|
| 22 | + protected $trapPass; //< trap database connection params |
|
| 23 | + public $dbPrefix; //< database tables prefix |
|
| 24 | 24 | |
| 25 | - protected $idoSet; //< bool true is ido database set |
|
| 26 | - protected $idoDSN; //< trap database connection params |
|
| 27 | - protected $idoUsername; //< trap database connection params |
|
| 28 | - protected $idoPass; //< trap database connection params |
|
| 25 | + protected $idoSet; //< bool true is ido database set |
|
| 26 | + protected $idoDSN; //< trap database connection params |
|
| 27 | + protected $idoUsername; //< trap database connection params |
|
| 28 | + protected $idoPass; //< trap database connection params |
|
| 29 | 29 | |
| 30 | - // Logging function |
|
| 30 | + // Logging function |
|
| 31 | 31 | |
| 32 | - protected $logging; //< logging class |
|
| 32 | + protected $logging; //< logging class |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param Logging $logClass : where to log |
|
| 36 | - * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 37 | - */ |
|
| 38 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
| 39 | - { |
|
| 40 | - $this->logging=$logClass; |
|
| 41 | - $this->dbPrefix=$dbPrefix; |
|
| 34 | + /** |
|
| 35 | + * @param Logging $logClass : where to log |
|
| 36 | + * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 37 | + */ |
|
| 38 | + function __construct($logClass,$dbParam,$dbPrefix) |
|
| 39 | + { |
|
| 40 | + $this->logging=$logClass; |
|
| 41 | + $this->dbPrefix=$dbPrefix; |
|
| 42 | 42 | |
| 43 | - $this->trapDSN=$this->setupDSN($dbParam); |
|
| 44 | - $this->trapUsername = $dbParam['username']; |
|
| 45 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 46 | - $this->trapDBType=$dbParam['db']; |
|
| 47 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 48 | - $this->db_connect_trap(); |
|
| 43 | + $this->trapDSN=$this->setupDSN($dbParam); |
|
| 44 | + $this->trapUsername = $dbParam['username']; |
|
| 45 | + $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 46 | + $this->trapDBType=$dbParam['db']; |
|
| 47 | + $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 48 | + $this->db_connect_trap(); |
|
| 49 | 49 | |
| 50 | - } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Setup and connect to IDO database |
|
| 54 | - * @param array $dbParam : array of named params |
|
| 55 | - */ |
|
| 56 | - public function setupIDO($dbParam) |
|
| 57 | - { |
|
| 58 | - $this->idoDSN=$this->setupDSN($dbParam); |
|
| 59 | - $this->idoUsername = $dbParam['username']; |
|
| 60 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 61 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 62 | - $this->idoDBType=$dbParam['db']; |
|
| 63 | - $this->db_connect_ido(); |
|
| 64 | - } |
|
| 52 | + /** |
|
| 53 | + * Setup and connect to IDO database |
|
| 54 | + * @param array $dbParam : array of named params |
|
| 55 | + */ |
|
| 56 | + public function setupIDO($dbParam) |
|
| 57 | + { |
|
| 58 | + $this->idoDSN=$this->setupDSN($dbParam); |
|
| 59 | + $this->idoUsername = $dbParam['username']; |
|
| 60 | + $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 61 | + $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 62 | + $this->idoDBType=$dbParam['db']; |
|
| 63 | + $this->db_connect_ido(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Connect to IDO database |
|
| 68 | - * @return \PDO |
|
| 69 | - */ |
|
| 70 | - public function db_connect_ido() |
|
| 71 | - { |
|
| 72 | - if ($this->idoDB != null) { |
|
| 73 | - // Check if connection is still alive |
|
| 74 | - try { |
|
| 75 | - $this->idoDB->query('select 1')->fetchColumn(); |
|
| 76 | - return $this->idoDB; |
|
| 77 | - } catch (Exception $e) { |
|
| 78 | - // select 1 failed, try to reconnect. |
|
| 79 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - try { |
|
| 83 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 84 | - } catch (PDOException $e) { |
|
| 85 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 86 | - } |
|
| 87 | - return $this->idoDB; |
|
| 88 | - } |
|
| 66 | + /** |
|
| 67 | + * Connect to IDO database |
|
| 68 | + * @return \PDO |
|
| 69 | + */ |
|
| 70 | + public function db_connect_ido() |
|
| 71 | + { |
|
| 72 | + if ($this->idoDB != null) { |
|
| 73 | + // Check if connection is still alive |
|
| 74 | + try { |
|
| 75 | + $this->idoDB->query('select 1')->fetchColumn(); |
|
| 76 | + return $this->idoDB; |
|
| 77 | + } catch (Exception $e) { |
|
| 78 | + // select 1 failed, try to reconnect. |
|
| 79 | + $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + try { |
|
| 83 | + $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 84 | + } catch (PDOException $e) { |
|
| 85 | + $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 86 | + } |
|
| 87 | + return $this->idoDB; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Connect to Trap database |
|
| 92 | - * @return \PDO |
|
| 93 | - */ |
|
| 94 | - public function db_connect_trap() |
|
| 95 | - { |
|
| 90 | + /** |
|
| 91 | + * Connect to Trap database |
|
| 92 | + * @return \PDO |
|
| 93 | + */ |
|
| 94 | + public function db_connect_trap() |
|
| 95 | + { |
|
| 96 | 96 | |
| 97 | - if ($this->trapDB != null) { |
|
| 98 | - // Check if connection is still alive |
|
| 99 | - try { |
|
| 100 | - $this->trapDB->query('select 1')->fetchColumn(); |
|
| 101 | - return $this->trapDB; |
|
| 102 | - } catch (Exception $e) { |
|
| 103 | - // select 1 failed, try to reconnect. |
|
| 104 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - try { |
|
| 108 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 109 | - } catch (PDOException $e) { |
|
| 110 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 111 | - } |
|
| 112 | - return $this->trapDB; |
|
| 113 | - } |
|
| 97 | + if ($this->trapDB != null) { |
|
| 98 | + // Check if connection is still alive |
|
| 99 | + try { |
|
| 100 | + $this->trapDB->query('select 1')->fetchColumn(); |
|
| 101 | + return $this->trapDB; |
|
| 102 | + } catch (Exception $e) { |
|
| 103 | + // select 1 failed, try to reconnect. |
|
| 104 | + $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + try { |
|
| 108 | + $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 109 | + } catch (PDOException $e) { |
|
| 110 | + $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 111 | + } |
|
| 112 | + return $this->trapDB; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Setup dsn and check parameters |
|
| 117 | - * @param array $configElmt |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - protected function setupDSN($configElmt) |
|
| 121 | - { |
|
| 122 | - if (!array_key_exists('db',$configElmt) || |
|
| 123 | - !array_key_exists('host',$configElmt) || |
|
| 124 | - !array_key_exists('dbname',$configElmt) || |
|
| 125 | - !array_key_exists('username',$configElmt)) |
|
| 126 | - { |
|
| 127 | - $this->logging->log('Missing DB params',ERROR); |
|
| 128 | - return ''; |
|
| 129 | - } |
|
| 115 | + /** |
|
| 116 | + * Setup dsn and check parameters |
|
| 117 | + * @param array $configElmt |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + protected function setupDSN($configElmt) |
|
| 121 | + { |
|
| 122 | + if (!array_key_exists('db',$configElmt) || |
|
| 123 | + !array_key_exists('host',$configElmt) || |
|
| 124 | + !array_key_exists('dbname',$configElmt) || |
|
| 125 | + !array_key_exists('username',$configElmt)) |
|
| 126 | + { |
|
| 127 | + $this->logging->log('Missing DB params',ERROR); |
|
| 128 | + return ''; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 132 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 131 | + // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 132 | + $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 133 | 133 | |
| 134 | - if (array_key_exists('port', $configElmt)) |
|
| 135 | - { |
|
| 136 | - $dsn .= ';port='.$configElmt['port']; |
|
| 137 | - } |
|
| 138 | - return $dsn; |
|
| 139 | - } |
|
| 134 | + if (array_key_exists('port', $configElmt)) |
|
| 135 | + { |
|
| 136 | + $dsn .= ';port='.$configElmt['port']; |
|
| 137 | + } |
|
| 138 | + return $dsn; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | 141 | |
| 142 | - /** Create database schema |
|
| 143 | - * @param $schema_file string File to read schema from |
|
| 144 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 145 | - */ |
|
| 146 | - public function create_schema($schema_file,$table_prefix) |
|
| 147 | - { |
|
| 148 | - //Read data from snmptrapd from stdin |
|
| 149 | - $input_stream=fopen($schema_file, 'r'); |
|
| 142 | + /** Create database schema |
|
| 143 | + * @param $schema_file string File to read schema from |
|
| 144 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 145 | + */ |
|
| 146 | + public function create_schema($schema_file,$table_prefix) |
|
| 147 | + { |
|
| 148 | + //Read data from snmptrapd from stdin |
|
| 149 | + $input_stream=fopen($schema_file, 'r'); |
|
| 150 | 150 | |
| 151 | - if ($input_stream=== false) |
|
| 152 | - { |
|
| 153 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - $newline=''; |
|
| 157 | - $cur_table=''; |
|
| 158 | - $cur_table_array=array(); |
|
| 159 | - $db_conn=$this->db_connect_trap(); |
|
| 151 | + if ($input_stream=== false) |
|
| 152 | + { |
|
| 153 | + $this->logging->log("Error reading schema !",ERROR,''); |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + $newline=''; |
|
| 157 | + $cur_table=''; |
|
| 158 | + $cur_table_array=array(); |
|
| 159 | + $db_conn=$this->db_connect_trap(); |
|
| 160 | 160 | |
| 161 | - while (($line=fgets($input_stream)) !== false) |
|
| 162 | - { |
|
| 163 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 164 | - if (preg_match('/; *$/', $newline)) |
|
| 165 | - { |
|
| 166 | - $sql= $newline; |
|
| 167 | - if ($db_conn->query($sql) === false) { |
|
| 168 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 169 | - return; |
|
| 170 | - } |
|
| 171 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 172 | - { |
|
| 173 | - $cur_table='table '.$cur_table_array[1]; |
|
| 174 | - } |
|
| 175 | - else |
|
| 176 | - { |
|
| 177 | - $cur_table='secret SQL stuff :-)'; |
|
| 178 | - } |
|
| 179 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 180 | - $newline=''; |
|
| 181 | - } |
|
| 182 | - } |
|
| 161 | + while (($line=fgets($input_stream)) !== false) |
|
| 162 | + { |
|
| 163 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 164 | + if (preg_match('/; *$/', $newline)) |
|
| 165 | + { |
|
| 166 | + $sql= $newline; |
|
| 167 | + if ($db_conn->query($sql) === false) { |
|
| 168 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 169 | + return; |
|
| 170 | + } |
|
| 171 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 172 | + { |
|
| 173 | + $cur_table='table '.$cur_table_array[1]; |
|
| 174 | + } |
|
| 175 | + else |
|
| 176 | + { |
|
| 177 | + $cur_table='secret SQL stuff :-)'; |
|
| 178 | + } |
|
| 179 | + $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 180 | + $newline=''; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - $sql= $newline; |
|
| 185 | - if ($sql != '' ) |
|
| 186 | - { |
|
| 187 | - if ($db_conn->query($sql) === false) { |
|
| 188 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 189 | - return; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - $this->logging->log('Schema created',INFO); |
|
| 193 | - } |
|
| 184 | + $sql= $newline; |
|
| 185 | + if ($sql != '' ) |
|
| 186 | + { |
|
| 187 | + if ($db_conn->query($sql) === false) { |
|
| 188 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 189 | + return; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + $this->logging->log('Schema created',INFO); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Update database schema from current (as set in db) to $target_version |
|
| 197 | - * @param $prefix string file prefix of sql update File |
|
| 198 | - * @param $target_version int target db version number |
|
| 199 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 200 | - * @param bool $getmsg : only get messages from version upgrades |
|
| 201 | - * @return string : if $getmsg=true, return messages. |
|
| 202 | - */ |
|
| 203 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 204 | - { |
|
| 205 | - // Get current db number |
|
| 206 | - $db_conn=$this->db_connect_trap(); |
|
| 207 | - $sql='SELECT id,value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 208 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 209 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 210 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 211 | - return 'ERROR'; |
|
| 212 | - } |
|
| 213 | - $version=$ret_code->fetchAll(); |
|
| 214 | - $cur_version=$version[0]['value']; |
|
| 215 | - $db_version_id=$version[0]['id']; |
|
| 195 | + /** |
|
| 196 | + * Update database schema from current (as set in db) to $target_version |
|
| 197 | + * @param $prefix string file prefix of sql update File |
|
| 198 | + * @param $target_version int target db version number |
|
| 199 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 200 | + * @param bool $getmsg : only get messages from version upgrades |
|
| 201 | + * @return string : if $getmsg=true, return messages. |
|
| 202 | + */ |
|
| 203 | + public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 204 | + { |
|
| 205 | + // Get current db number |
|
| 206 | + $db_conn=$this->db_connect_trap(); |
|
| 207 | + $sql='SELECT id,value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 208 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 209 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 210 | + $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 211 | + return 'ERROR'; |
|
| 212 | + } |
|
| 213 | + $version=$ret_code->fetchAll(); |
|
| 214 | + $cur_version=$version[0]['value']; |
|
| 215 | + $db_version_id=$version[0]['id']; |
|
| 216 | 216 | |
| 217 | - if ($this->trapDBType == 'pgsql') |
|
| 218 | - { |
|
| 219 | - $prefix .= 'update_pgsql/schema_'; |
|
| 220 | - } |
|
| 221 | - else |
|
| 222 | - { |
|
| 223 | - $prefix .= 'update_sql/schema_'; |
|
| 224 | - } |
|
| 225 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 226 | - if ($getmsg === true) |
|
| 227 | - { |
|
| 228 | - $message=''; |
|
| 229 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 230 | - while($cur_version<$target_version) |
|
| 231 | - { |
|
| 232 | - $cur_version++; |
|
| 233 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 234 | - $input_stream=fopen($updateFile, 'r'); |
|
| 235 | - if ($input_stream=== false) |
|
| 236 | - { |
|
| 237 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 238 | - return 'ERROR'; |
|
| 239 | - } |
|
| 240 | - do { $line=fgets($input_stream); } |
|
| 241 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 242 | - if ($line === false) |
|
| 243 | - { |
|
| 244 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 245 | - return 'ERROR'; |
|
| 246 | - } |
|
| 247 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 248 | - } |
|
| 249 | - return $message; |
|
| 250 | - } |
|
| 251 | - while($cur_version<$target_version) |
|
| 252 | - { // tODO : execute pre & post scripts |
|
| 253 | - $cur_version++; |
|
| 254 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 255 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 256 | - $input_stream=fopen($updateFile, 'r'); |
|
| 257 | - if ($input_stream=== false) |
|
| 258 | - { |
|
| 259 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 260 | - return 'ERROR'; |
|
| 261 | - } |
|
| 262 | - $newline=''; |
|
| 263 | - $db_conn=$this->db_connect_trap(); |
|
| 264 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 265 | - while (($line=fgets($input_stream)) !== false) |
|
| 266 | - { |
|
| 267 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 268 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 269 | - if (preg_match('/; *$/', $newline)) |
|
| 270 | - { |
|
| 271 | - $sql_req=$db_conn->prepare($newline); |
|
| 272 | - if ($sql_req->execute() === false) { |
|
| 273 | - $this->logging->log('Error create schema : '.$newline,1,''); |
|
| 274 | - } |
|
| 275 | - $cur_table_array=array(); |
|
| 276 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 277 | - { |
|
| 278 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 279 | - } |
|
| 280 | - else |
|
| 281 | - { |
|
| 282 | - $cur_table='secret SQL stuff :-)'; |
|
| 283 | - //$cur_table=$newline; |
|
| 284 | - } |
|
| 285 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 217 | + if ($this->trapDBType == 'pgsql') |
|
| 218 | + { |
|
| 219 | + $prefix .= 'update_pgsql/schema_'; |
|
| 220 | + } |
|
| 221 | + else |
|
| 222 | + { |
|
| 223 | + $prefix .= 'update_sql/schema_'; |
|
| 224 | + } |
|
| 225 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 226 | + if ($getmsg === true) |
|
| 227 | + { |
|
| 228 | + $message=''; |
|
| 229 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 230 | + while($cur_version<$target_version) |
|
| 231 | + { |
|
| 232 | + $cur_version++; |
|
| 233 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 234 | + $input_stream=fopen($updateFile, 'r'); |
|
| 235 | + if ($input_stream=== false) |
|
| 236 | + { |
|
| 237 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 238 | + return 'ERROR'; |
|
| 239 | + } |
|
| 240 | + do { $line=fgets($input_stream); } |
|
| 241 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 242 | + if ($line === false) |
|
| 243 | + { |
|
| 244 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 245 | + return 'ERROR'; |
|
| 246 | + } |
|
| 247 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 248 | + } |
|
| 249 | + return $message; |
|
| 250 | + } |
|
| 251 | + while($cur_version<$target_version) |
|
| 252 | + { // tODO : execute pre & post scripts |
|
| 253 | + $cur_version++; |
|
| 254 | + $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 255 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 256 | + $input_stream=fopen($updateFile, 'r'); |
|
| 257 | + if ($input_stream=== false) |
|
| 258 | + { |
|
| 259 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 260 | + return 'ERROR'; |
|
| 261 | + } |
|
| 262 | + $newline=''; |
|
| 263 | + $db_conn=$this->db_connect_trap(); |
|
| 264 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 265 | + while (($line=fgets($input_stream)) !== false) |
|
| 266 | + { |
|
| 267 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 268 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 269 | + if (preg_match('/; *$/', $newline)) |
|
| 270 | + { |
|
| 271 | + $sql_req=$db_conn->prepare($newline); |
|
| 272 | + if ($sql_req->execute() === false) { |
|
| 273 | + $this->logging->log('Error create schema : '.$newline,1,''); |
|
| 274 | + } |
|
| 275 | + $cur_table_array=array(); |
|
| 276 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 277 | + { |
|
| 278 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 279 | + } |
|
| 280 | + else |
|
| 281 | + { |
|
| 282 | + $cur_table='secret SQL stuff :-)'; |
|
| 283 | + //$cur_table=$newline; |
|
| 284 | + } |
|
| 285 | + $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 286 | 286 | |
| 287 | - $newline=''; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - fclose($input_stream); |
|
| 287 | + $newline=''; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + fclose($input_stream); |
|
| 291 | 291 | |
| 292 | - //$sql= $newline; |
|
| 293 | - //if ($db_conn->query($sql) === false) { |
|
| 294 | - // $this->logging->log('Error updating schema : '.$sql,1,''); |
|
| 295 | - //} |
|
| 292 | + //$sql= $newline; |
|
| 293 | + //if ($db_conn->query($sql) === false) { |
|
| 294 | + // $this->logging->log('Error updating schema : '.$sql,1,''); |
|
| 295 | + //} |
|
| 296 | 296 | |
| 297 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
|
| 298 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 299 | - if ($db_conn->query($sql) === false) { |
|
| 300 | - $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
|
| 301 | - return 'ERROR'; |
|
| 302 | - } |
|
| 297 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
|
| 298 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 299 | + if ($db_conn->query($sql) === false) { |
|
| 300 | + $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
|
| 301 | + return 'ERROR'; |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 305 | - } |
|
| 306 | - } |
|
| 304 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | 308 | |
| 309 | 309 | } |
| 310 | 310 | \ No newline at end of file |
@@ -35,16 +35,16 @@ discard block |
||
| 35 | 35 | * @param Logging $logClass : where to log |
| 36 | 36 | * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
| 37 | 37 | */ |
| 38 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
| 38 | + function __construct($logClass, $dbParam, $dbPrefix) |
|
| 39 | 39 | { |
| 40 | 40 | $this->logging=$logClass; |
| 41 | 41 | $this->dbPrefix=$dbPrefix; |
| 42 | 42 | |
| 43 | 43 | $this->trapDSN=$this->setupDSN($dbParam); |
| 44 | - $this->trapUsername = $dbParam['username']; |
|
| 45 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 44 | + $this->trapUsername=$dbParam['username']; |
|
| 45 | + $this->trapPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : ''; |
|
| 46 | 46 | $this->trapDBType=$dbParam['db']; |
| 47 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 47 | + $this->logging->log('DSN : '.$this->trapDSN.';user '.$this->trapUsername.' / prefix : '.$this->dbPrefix, INFO); |
|
| 48 | 48 | $this->db_connect_trap(); |
| 49 | 49 | |
| 50 | 50 | } |
@@ -56,9 +56,9 @@ discard block |
||
| 56 | 56 | public function setupIDO($dbParam) |
| 57 | 57 | { |
| 58 | 58 | $this->idoDSN=$this->setupDSN($dbParam); |
| 59 | - $this->idoUsername = $dbParam['username']; |
|
| 60 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 61 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 59 | + $this->idoUsername=$dbParam['username']; |
|
| 60 | + $this->idoPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : ''; |
|
| 61 | + $this->logging->log('DSN : '.$this->idoDSN.';user '.$this->idoUsername, INFO); |
|
| 62 | 62 | $this->idoDBType=$dbParam['db']; |
| 63 | 63 | $this->db_connect_ido(); |
| 64 | 64 | } |
@@ -76,13 +76,13 @@ discard block |
||
| 76 | 76 | return $this->idoDB; |
| 77 | 77 | } catch (Exception $e) { |
| 78 | 78 | // select 1 failed, try to reconnect. |
| 79 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 79 | + $this->logging->log('Database IDO connection lost, reconnecting', WARN); |
|
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | 82 | try { |
| 83 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 83 | + $this->idoDB=new PDO($this->idoDSN, $this->idoUsername, $this->idoPass); |
|
| 84 | 84 | } catch (PDOException $e) { |
| 85 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 85 | + $this->logging->log('Connection failed to IDO : '.$e->getMessage(), ERROR, ''); |
|
| 86 | 86 | } |
| 87 | 87 | return $this->idoDB; |
| 88 | 88 | } |
@@ -101,13 +101,13 @@ discard block |
||
| 101 | 101 | return $this->trapDB; |
| 102 | 102 | } catch (Exception $e) { |
| 103 | 103 | // select 1 failed, try to reconnect. |
| 104 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 104 | + $this->logging->log('Database connection lost, reconnecting', WARN); |
|
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | try { |
| 108 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 108 | + $this->trapDB=new PDO($this->trapDSN, $this->trapUsername, $this->trapPass); |
|
| 109 | 109 | } catch (PDOException $e) { |
| 110 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 110 | + $this->logging->log('Connection failed : '.$e->getMessage(), ERROR, ''); |
|
| 111 | 111 | } |
| 112 | 112 | return $this->trapDB; |
| 113 | 113 | } |
@@ -119,21 +119,21 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | protected function setupDSN($configElmt) |
| 121 | 121 | { |
| 122 | - if (!array_key_exists('db',$configElmt) || |
|
| 123 | - !array_key_exists('host',$configElmt) || |
|
| 124 | - !array_key_exists('dbname',$configElmt) || |
|
| 125 | - !array_key_exists('username',$configElmt)) |
|
| 122 | + if (!array_key_exists('db', $configElmt) || |
|
| 123 | + !array_key_exists('host', $configElmt) || |
|
| 124 | + !array_key_exists('dbname', $configElmt) || |
|
| 125 | + !array_key_exists('username', $configElmt)) |
|
| 126 | 126 | { |
| 127 | - $this->logging->log('Missing DB params',ERROR); |
|
| 127 | + $this->logging->log('Missing DB params', ERROR); |
|
| 128 | 128 | return ''; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
| 132 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 132 | + $dsn=$configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 133 | 133 | |
| 134 | 134 | if (array_key_exists('port', $configElmt)) |
| 135 | 135 | { |
| 136 | - $dsn .= ';port='.$configElmt['port']; |
|
| 136 | + $dsn.=';port='.$configElmt['port']; |
|
| 137 | 137 | } |
| 138 | 138 | return $dsn; |
| 139 | 139 | } |
@@ -143,14 +143,14 @@ discard block |
||
| 143 | 143 | * @param $schema_file string File to read schema from |
| 144 | 144 | * @param $table_prefix string to replace #PREFIX# in schema file by this |
| 145 | 145 | */ |
| 146 | - public function create_schema($schema_file,$table_prefix) |
|
| 146 | + public function create_schema($schema_file, $table_prefix) |
|
| 147 | 147 | { |
| 148 | 148 | //Read data from snmptrapd from stdin |
| 149 | 149 | $input_stream=fopen($schema_file, 'r'); |
| 150 | 150 | |
| 151 | - if ($input_stream=== false) |
|
| 151 | + if ($input_stream === false) |
|
| 152 | 152 | { |
| 153 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
| 153 | + $this->logging->log("Error reading schema !", ERROR, ''); |
|
| 154 | 154 | return; |
| 155 | 155 | } |
| 156 | 156 | $newline=''; |
@@ -160,15 +160,15 @@ discard block |
||
| 160 | 160 | |
| 161 | 161 | while (($line=fgets($input_stream)) !== false) |
| 162 | 162 | { |
| 163 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 163 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
| 164 | 164 | if (preg_match('/; *$/', $newline)) |
| 165 | 165 | { |
| 166 | - $sql= $newline; |
|
| 166 | + $sql=$newline; |
|
| 167 | 167 | if ($db_conn->query($sql) === false) { |
| 168 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 168 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
| 169 | 169 | return; |
| 170 | 170 | } |
| 171 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 171 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
| 172 | 172 | { |
| 173 | 173 | $cur_table='table '.$cur_table_array[1]; |
| 174 | 174 | } |
@@ -176,20 +176,20 @@ discard block |
||
| 176 | 176 | { |
| 177 | 177 | $cur_table='secret SQL stuff :-)'; |
| 178 | 178 | } |
| 179 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 179 | + $this->logging->log('Creating : '.$cur_table, INFO); |
|
| 180 | 180 | $newline=''; |
| 181 | 181 | } |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - $sql= $newline; |
|
| 185 | - if ($sql != '' ) |
|
| 184 | + $sql=$newline; |
|
| 185 | + if ($sql != '') |
|
| 186 | 186 | { |
| 187 | 187 | if ($db_conn->query($sql) === false) { |
| 188 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 188 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
| 189 | 189 | return; |
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | - $this->logging->log('Schema created',INFO); |
|
| 192 | + $this->logging->log('Schema created', INFO); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | /** |
@@ -200,14 +200,14 @@ discard block |
||
| 200 | 200 | * @param bool $getmsg : only get messages from version upgrades |
| 201 | 201 | * @return string : if $getmsg=true, return messages. |
| 202 | 202 | */ |
| 203 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 203 | + public function update_schema($prefix, $target_version, $table_prefix, $getmsg=false) |
|
| 204 | 204 | { |
| 205 | 205 | // Get current db number |
| 206 | 206 | $db_conn=$this->db_connect_trap(); |
| 207 | 207 | $sql='SELECT id,value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
| 208 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 208 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 209 | 209 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 210 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 210 | + $this->logging->log('Cannot get db version. Query : '.$sql, 2, ''); |
|
| 211 | 211 | return 'ERROR'; |
| 212 | 212 | } |
| 213 | 213 | $version=$ret_code->fetchAll(); |
@@ -216,47 +216,47 @@ discard block |
||
| 216 | 216 | |
| 217 | 217 | if ($this->trapDBType == 'pgsql') |
| 218 | 218 | { |
| 219 | - $prefix .= 'update_pgsql/schema_'; |
|
| 219 | + $prefix.='update_pgsql/schema_'; |
|
| 220 | 220 | } |
| 221 | 221 | else |
| 222 | 222 | { |
| 223 | - $prefix .= 'update_sql/schema_'; |
|
| 223 | + $prefix.='update_sql/schema_'; |
|
| 224 | 224 | } |
| 225 | 225 | //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
| 226 | 226 | if ($getmsg === true) |
| 227 | 227 | { |
| 228 | 228 | $message=''; |
| 229 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 230 | - while($cur_version<$target_version) |
|
| 229 | + $this->logging->log('getting message for upgrade', DEBUG); |
|
| 230 | + while ($cur_version < $target_version) |
|
| 231 | 231 | { |
| 232 | 232 | $cur_version++; |
| 233 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 233 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
| 234 | 234 | $input_stream=fopen($updateFile, 'r'); |
| 235 | - if ($input_stream=== false) |
|
| 235 | + if ($input_stream === false) |
|
| 236 | 236 | { |
| 237 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 237 | + $this->logging->log("Error reading update file ".$updateFile, 2, ''); |
|
| 238 | 238 | return 'ERROR'; |
| 239 | 239 | } |
| 240 | 240 | do { $line=fgets($input_stream); } |
| 241 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 241 | + while ($line !== false && !preg_match('/#MESSAGE/', $line)); |
|
| 242 | 242 | if ($line === false) |
| 243 | 243 | { |
| 244 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 244 | + $this->logging->log("No message in file ".$updateFile, 2, ''); |
|
| 245 | 245 | return 'ERROR'; |
| 246 | 246 | } |
| 247 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 247 | + $message.=($cur_version - 1).'->'.$cur_version.' : '.preg_replace('/#MESSAGE : /', '', $line)."\n"; |
|
| 248 | 248 | } |
| 249 | 249 | return $message; |
| 250 | 250 | } |
| 251 | - while($cur_version<$target_version) |
|
| 251 | + while ($cur_version < $target_version) |
|
| 252 | 252 | { // tODO : execute pre & post scripts |
| 253 | 253 | $cur_version++; |
| 254 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 255 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 254 | + $this->logging->log('Updating to version : '.$cur_version, INFO); |
|
| 255 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
| 256 | 256 | $input_stream=fopen($updateFile, 'r'); |
| 257 | - if ($input_stream=== false) |
|
| 257 | + if ($input_stream === false) |
|
| 258 | 258 | { |
| 259 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 259 | + $this->logging->log("Error reading update file ".$updateFile, 2, ''); |
|
| 260 | 260 | return 'ERROR'; |
| 261 | 261 | } |
| 262 | 262 | $newline=''; |
@@ -265,24 +265,24 @@ discard block |
||
| 265 | 265 | while (($line=fgets($input_stream)) !== false) |
| 266 | 266 | { |
| 267 | 267 | if (preg_match('/^#/', $line)) continue; // ignore comment lines |
| 268 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 268 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
| 269 | 269 | if (preg_match('/; *$/', $newline)) |
| 270 | 270 | { |
| 271 | 271 | $sql_req=$db_conn->prepare($newline); |
| 272 | 272 | if ($sql_req->execute() === false) { |
| 273 | - $this->logging->log('Error create schema : '.$newline,1,''); |
|
| 273 | + $this->logging->log('Error create schema : '.$newline, 1, ''); |
|
| 274 | 274 | } |
| 275 | 275 | $cur_table_array=array(); |
| 276 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 276 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
| 277 | 277 | { |
| 278 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 278 | + $cur_table=$cur_table_array[1].' SQL table '.$cur_table_array[2]; |
|
| 279 | 279 | } |
| 280 | 280 | else |
| 281 | 281 | { |
| 282 | 282 | $cur_table='secret SQL stuff :-)'; |
| 283 | 283 | //$cur_table=$newline; |
| 284 | 284 | } |
| 285 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 285 | + $this->logging->log('Doing : '.$cur_table, INFO); |
|
| 286 | 286 | |
| 287 | 287 | $newline=''; |
| 288 | 288 | } |
@@ -295,13 +295,13 @@ discard block |
||
| 295 | 295 | //} |
| 296 | 296 | |
| 297 | 297 | $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; |
| 298 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 298 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 299 | 299 | if ($db_conn->query($sql) === false) { |
| 300 | - $this->logging->log('Cannot update db version. Query : ' . $sql,2); |
|
| 300 | + $this->logging->log('Cannot update db version. Query : '.$sql, 2); |
|
| 301 | 301 | return 'ERROR'; |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 304 | + $this->logging->log('Schema updated to version : '.$cur_version, INFO); |
|
| 305 | 305 | } |
| 306 | 306 | } |
| 307 | 307 | |
@@ -171,8 +171,7 @@ discard block |
||
| 171 | 171 | if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
| 172 | 172 | { |
| 173 | 173 | $cur_table='table '.$cur_table_array[1]; |
| 174 | - } |
|
| 175 | - else |
|
| 174 | + } else |
|
| 176 | 175 | { |
| 177 | 176 | $cur_table='secret SQL stuff :-)'; |
| 178 | 177 | } |
@@ -217,8 +216,7 @@ discard block |
||
| 217 | 216 | if ($this->trapDBType == 'pgsql') |
| 218 | 217 | { |
| 219 | 218 | $prefix .= 'update_pgsql/schema_'; |
| 220 | - } |
|
| 221 | - else |
|
| 219 | + } else |
|
| 222 | 220 | { |
| 223 | 221 | $prefix .= 'update_sql/schema_'; |
| 224 | 222 | } |
@@ -264,7 +262,10 @@ discard block |
||
| 264 | 262 | $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 265 | 263 | while (($line=fgets($input_stream)) !== false) |
| 266 | 264 | { |
| 267 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 265 | + if (preg_match('/^#/', $line)) { |
|
| 266 | + continue; |
|
| 267 | + } |
|
| 268 | + // ignore comment lines |
|
| 268 | 269 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
| 269 | 270 | if (preg_match('/; *$/', $newline)) |
| 270 | 271 | { |
@@ -276,8 +277,7 @@ discard block |
||
| 276 | 277 | if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
| 277 | 278 | { |
| 278 | 279 | $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
| 279 | - } |
|
| 280 | - else |
|
| 280 | + } else |
|
| 281 | 281 | { |
| 282 | 282 | $cur_table='secret SQL stuff :-)'; |
| 283 | 283 | //$cur_table=$newline; |
@@ -10,509 +10,509 @@ |
||
| 10 | 10 | class Mib |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - protected $logging; //< logging class |
|
| 14 | - protected $trapsDB; //< Database class |
|
| 13 | + protected $logging; //< logging class |
|
| 14 | + protected $trapsDB; //< Database class |
|
| 15 | 15 | |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Setup Mib Class |
|
| 19 | - * @param Logging $logClass : where to log |
|
| 20 | - * @param Database $dbClass : Database |
|
| 21 | - */ |
|
| 22 | - function __construct($logClass,$dbClass) |
|
| 23 | - { |
|
| 24 | - $this->logging=$logClass; |
|
| 25 | - $this->trapsDB=$dbClass; |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Setup Mib Class |
|
| 19 | + * @param Logging $logClass : where to log |
|
| 20 | + * @param Database $dbClass : Database |
|
| 21 | + */ |
|
| 22 | + function __construct($logClass,$dbClass) |
|
| 23 | + { |
|
| 24 | + $this->logging=$logClass; |
|
| 25 | + $this->trapsDB=$dbClass; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 31 | - * @param string $oid |
|
| 32 | - * @param string $mib |
|
| 33 | - * @param string $name |
|
| 34 | - * @param string $type |
|
| 35 | - * @param string $textConv |
|
| 36 | - * @param string $dispHint |
|
| 37 | - * @param string $syntax |
|
| 38 | - * @param string $type_enum |
|
| 39 | - * @param string $description |
|
| 40 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 41 | - */ |
|
| 42 | - public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
|
| 43 | - { |
|
| 44 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 45 | - $description=$db_conn->quote($description); |
|
| 46 | - if (isset($this->dbOidIndex[$oid])) |
|
| 47 | - { |
|
| 48 | - if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 49 | - { // newly created. |
|
| 50 | - return 0; |
|
| 51 | - } |
|
| 52 | - if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 53 | - $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 54 | - $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 55 | - //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 56 | - //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 57 | - //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 58 | - //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 59 | - //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 60 | - ) |
|
| 61 | - { // Do update |
|
| 62 | - $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
| 63 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 64 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 65 | - ' WHERE id= :id'; |
|
| 66 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 29 | + /** |
|
| 30 | + * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 31 | + * @param string $oid |
|
| 32 | + * @param string $mib |
|
| 33 | + * @param string $name |
|
| 34 | + * @param string $type |
|
| 35 | + * @param string $textConv |
|
| 36 | + * @param string $dispHint |
|
| 37 | + * @param string $syntax |
|
| 38 | + * @param string $type_enum |
|
| 39 | + * @param string $description |
|
| 40 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 41 | + */ |
|
| 42 | + public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
|
| 43 | + { |
|
| 44 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 45 | + $description=$db_conn->quote($description); |
|
| 46 | + if (isset($this->dbOidIndex[$oid])) |
|
| 47 | + { |
|
| 48 | + if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 49 | + { // newly created. |
|
| 50 | + return 0; |
|
| 51 | + } |
|
| 52 | + if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 53 | + $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 54 | + $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 55 | + //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 56 | + //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 57 | + //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 58 | + //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 59 | + //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 60 | + ) |
|
| 61 | + { // Do update |
|
| 62 | + $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
| 63 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 64 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 65 | + ' WHERE id= :id'; |
|
| 66 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 67 | 67 | |
| 68 | - $sqlParam=array( |
|
| 69 | - ':name' => $name, |
|
| 70 | - ':type' => $type, |
|
| 71 | - ':mib' => $mib, |
|
| 72 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 73 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 74 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 75 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 76 | - ':description' => ($description==null)?'null':$description, |
|
| 77 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 78 | - ); |
|
| 68 | + $sqlParam=array( |
|
| 69 | + ':name' => $name, |
|
| 70 | + ':type' => $type, |
|
| 71 | + ':mib' => $mib, |
|
| 72 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 73 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 74 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 75 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 76 | + ':description' => ($description==null)?'null':$description, |
|
| 77 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 78 | + ); |
|
| 79 | 79 | |
| 80 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 81 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 82 | - } |
|
| 83 | - $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 84 | - return 1; |
|
| 85 | - } |
|
| 86 | - else |
|
| 87 | - { |
|
| 88 | - $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 89 | - return 0; |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - // create new OID. |
|
| 80 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 81 | + $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 82 | + } |
|
| 83 | + $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 84 | + return 1; |
|
| 85 | + } |
|
| 86 | + else |
|
| 87 | + { |
|
| 88 | + $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 89 | + return 0; |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + // create new OID. |
|
| 93 | 93 | |
| 94 | - // Insert data |
|
| 94 | + // Insert data |
|
| 95 | 95 | |
| 96 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
| 97 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 98 | - ', syntax, type_enum , description ) ' . |
|
| 99 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 100 | - ', :syntax, :type_enum, :description )'; |
|
| 96 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
| 97 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 98 | + ', syntax, type_enum , description ) ' . |
|
| 99 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 100 | + ', :syntax, :type_enum, :description )'; |
|
| 101 | 101 | |
| 102 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 102 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 103 | 103 | |
| 104 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 104 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 105 | 105 | |
| 106 | - $sqlParam=array( |
|
| 107 | - ':oid' => $oid, |
|
| 108 | - ':name' => $name, |
|
| 109 | - ':type' => $type, |
|
| 110 | - ':mib' => $mib, |
|
| 111 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 112 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 113 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 114 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 115 | - ':description' => ($description==null)?'null':$description |
|
| 116 | - ); |
|
| 106 | + $sqlParam=array( |
|
| 107 | + ':oid' => $oid, |
|
| 108 | + ':name' => $name, |
|
| 109 | + ':type' => $type, |
|
| 110 | + ':mib' => $mib, |
|
| 111 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 112 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 113 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 114 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 115 | + ':description' => ($description==null)?'null':$description |
|
| 116 | + ); |
|
| 117 | 117 | |
| 118 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 119 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 120 | - } |
|
| 118 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 119 | + $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - switch ($this->trapsDB->trapDBType) |
|
| 123 | - { |
|
| 124 | - case 'pgsql': |
|
| 125 | - // Get last id to insert oid/values in secondary table |
|
| 126 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 127 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 128 | - } |
|
| 129 | - if (! isset($inserted_id_ret['id'])) { |
|
| 130 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 131 | - } |
|
| 132 | - $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 133 | - break; |
|
| 134 | - case 'mysql': |
|
| 135 | - // Get last id to insert oid/values in secondary table |
|
| 136 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 137 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 138 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 139 | - } |
|
| 122 | + switch ($this->trapsDB->trapDBType) |
|
| 123 | + { |
|
| 124 | + case 'pgsql': |
|
| 125 | + // Get last id to insert oid/values in secondary table |
|
| 126 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 127 | + $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 128 | + } |
|
| 129 | + if (! isset($inserted_id_ret['id'])) { |
|
| 130 | + $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 131 | + } |
|
| 132 | + $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 133 | + break; |
|
| 134 | + case 'mysql': |
|
| 135 | + // Get last id to insert oid/values in secondary table |
|
| 136 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 137 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 138 | + $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 142 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 143 | - $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 144 | - break; |
|
| 145 | - default: |
|
| 146 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 147 | - } |
|
| 141 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 142 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 143 | + $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 144 | + break; |
|
| 145 | + default: |
|
| 146 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - // Set as newly created. |
|
| 150 | - $this->dbOidIndex[$oid]['key']=-1; |
|
| 151 | - return 2; |
|
| 152 | - } |
|
| 149 | + // Set as newly created. |
|
| 150 | + $this->dbOidIndex[$oid]['key']=-1; |
|
| 151 | + return 2; |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * create or update (with check_existing = true) objects of trap |
|
| 156 | - * @param string $trapOID : trap oid |
|
| 157 | - * @param string $trapmib : mib of trap |
|
| 158 | - * @param array $objects : array of objects name (without MIB) |
|
| 159 | - * @param bool $check_existing : check instead of create |
|
| 160 | - */ |
|
| 161 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 162 | - { |
|
| 163 | - $dbObjects=null; // cache of objects for trap in db |
|
| 164 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 154 | + /** |
|
| 155 | + * create or update (with check_existing = true) objects of trap |
|
| 156 | + * @param string $trapOID : trap oid |
|
| 157 | + * @param string $trapmib : mib of trap |
|
| 158 | + * @param array $objects : array of objects name (without MIB) |
|
| 159 | + * @param bool $check_existing : check instead of create |
|
| 160 | + */ |
|
| 161 | + public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 162 | + { |
|
| 163 | + $dbObjects=null; // cache of objects for trap in db |
|
| 164 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 165 | 165 | |
| 166 | - // Get id of trapmib. |
|
| 166 | + // Get id of trapmib. |
|
| 167 | 167 | |
| 168 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 169 | - if ($check_existing === true) |
|
| 170 | - { |
|
| 171 | - // Get all objects |
|
| 172 | - $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 173 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 174 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 175 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 176 | - } |
|
| 177 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 168 | + $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 169 | + if ($check_existing === true) |
|
| 170 | + { |
|
| 171 | + // Get all objects |
|
| 172 | + $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 173 | + $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 174 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 175 | + $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 176 | + } |
|
| 177 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 178 | 178 | |
| 179 | - foreach ($dbObjectsRaw as $val) |
|
| 180 | - { |
|
| 181 | - $dbObjects[$val['object_id']]=1; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - foreach ($objects as $object) |
|
| 185 | - { |
|
| 186 | - $match=$snmptrans=array(); |
|
| 187 | - $retVal=0; |
|
| 188 | - $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 189 | - $tmpdesc='';$indesc=false; |
|
| 179 | + foreach ($dbObjectsRaw as $val) |
|
| 180 | + { |
|
| 181 | + $dbObjects[$val['object_id']]=1; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + foreach ($objects as $object) |
|
| 185 | + { |
|
| 186 | + $match=$snmptrans=array(); |
|
| 187 | + $retVal=0; |
|
| 188 | + $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 189 | + $tmpdesc='';$indesc=false; |
|
| 190 | 190 | |
| 191 | - $objMib=$trapmib; |
|
| 192 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 193 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 194 | - if ($retVal!=0) |
|
| 195 | - { |
|
| 196 | - // Maybe not trap mib, search with IR |
|
| 197 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 198 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 199 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 200 | - { // Not found -> continue with warning |
|
| 201 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 202 | - continue; |
|
| 203 | - } |
|
| 204 | - $objMib=$match[1]; |
|
| 191 | + $objMib=$trapmib; |
|
| 192 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 193 | + ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 194 | + if ($retVal!=0) |
|
| 195 | + { |
|
| 196 | + // Maybe not trap mib, search with IR |
|
| 197 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 198 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 199 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 200 | + { // Not found -> continue with warning |
|
| 201 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 202 | + continue; |
|
| 203 | + } |
|
| 204 | + $objMib=$match[1]; |
|
| 205 | 205 | |
| 206 | - // Do the snmptranslate again. |
|
| 207 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 208 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 209 | - if ($retVal!=0) { |
|
| 210 | - $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 211 | - } |
|
| 206 | + // Do the snmptranslate again. |
|
| 207 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 208 | + ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 209 | + if ($retVal!=0) { |
|
| 210 | + $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - } |
|
| 214 | - foreach ($snmptrans as $line) |
|
| 215 | - { |
|
| 216 | - if ($indesc===true) |
|
| 217 | - { |
|
| 218 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 219 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 220 | - { |
|
| 221 | - $objDesc = $tmpdesc . $match[1]; |
|
| 222 | - $indesc=false; |
|
| 223 | - } |
|
| 224 | - $tmpdesc.=$line; |
|
| 225 | - continue; |
|
| 226 | - } |
|
| 227 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 228 | - { |
|
| 229 | - $objOid=$line; |
|
| 230 | - continue; |
|
| 231 | - } |
|
| 232 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 233 | - { |
|
| 234 | - $objSyntax=$match[1]; |
|
| 235 | - $objEnum=$match[2]; |
|
| 236 | - continue; |
|
| 237 | - } |
|
| 238 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 239 | - { |
|
| 240 | - $objSyntax=$match[1]; |
|
| 241 | - continue; |
|
| 242 | - } |
|
| 243 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 244 | - { |
|
| 245 | - $objDispHint=$match[1]; |
|
| 246 | - continue; |
|
| 247 | - } |
|
| 248 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 249 | - { |
|
| 250 | - $objDesc=$match[1]; |
|
| 251 | - continue; |
|
| 252 | - } |
|
| 253 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 254 | - { |
|
| 255 | - $tmpdesc=$match[1]; |
|
| 256 | - $indesc=true; |
|
| 257 | - continue; |
|
| 258 | - } |
|
| 259 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 260 | - { |
|
| 261 | - $objTc=$match[1]; |
|
| 262 | - continue; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 266 | - //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 267 | - // Update |
|
| 268 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 213 | + } |
|
| 214 | + foreach ($snmptrans as $line) |
|
| 215 | + { |
|
| 216 | + if ($indesc===true) |
|
| 217 | + { |
|
| 218 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 219 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
| 220 | + { |
|
| 221 | + $objDesc = $tmpdesc . $match[1]; |
|
| 222 | + $indesc=false; |
|
| 223 | + } |
|
| 224 | + $tmpdesc.=$line; |
|
| 225 | + continue; |
|
| 226 | + } |
|
| 227 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 228 | + { |
|
| 229 | + $objOid=$line; |
|
| 230 | + continue; |
|
| 231 | + } |
|
| 232 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 233 | + { |
|
| 234 | + $objSyntax=$match[1]; |
|
| 235 | + $objEnum=$match[2]; |
|
| 236 | + continue; |
|
| 237 | + } |
|
| 238 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 239 | + { |
|
| 240 | + $objSyntax=$match[1]; |
|
| 241 | + continue; |
|
| 242 | + } |
|
| 243 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 244 | + { |
|
| 245 | + $objDispHint=$match[1]; |
|
| 246 | + continue; |
|
| 247 | + } |
|
| 248 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 249 | + { |
|
| 250 | + $objDesc=$match[1]; |
|
| 251 | + continue; |
|
| 252 | + } |
|
| 253 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 254 | + { |
|
| 255 | + $tmpdesc=$match[1]; |
|
| 256 | + $indesc=true; |
|
| 257 | + continue; |
|
| 258 | + } |
|
| 259 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 260 | + { |
|
| 261 | + $objTc=$match[1]; |
|
| 262 | + continue; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 266 | + //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 267 | + // Update |
|
| 268 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 269 | 269 | |
| 270 | - if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 271 | - { // if link exists, continue |
|
| 272 | - $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 273 | - continue; |
|
| 274 | - } |
|
| 275 | - if ($check_existing === true) |
|
| 276 | - { |
|
| 277 | - // TODO : check link trap - objects exists, mark them. |
|
| 278 | - } |
|
| 279 | - // Associate in object table |
|
| 280 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 281 | - 'values (:trap_id, :object_id)'; |
|
| 282 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 283 | - $sqlParam=array( |
|
| 284 | - ':trap_id' => $trapId, |
|
| 285 | - ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 286 | - ); |
|
| 270 | + if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 271 | + { // if link exists, continue |
|
| 272 | + $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 273 | + continue; |
|
| 274 | + } |
|
| 275 | + if ($check_existing === true) |
|
| 276 | + { |
|
| 277 | + // TODO : check link trap - objects exists, mark them. |
|
| 278 | + } |
|
| 279 | + // Associate in object table |
|
| 280 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 281 | + 'values (:trap_id, :object_id)'; |
|
| 282 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 283 | + $sqlParam=array( |
|
| 284 | + ':trap_id' => $trapId, |
|
| 285 | + ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 286 | + ); |
|
| 287 | 287 | |
| 288 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 289 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - if ($check_existing === true) |
|
| 293 | - { |
|
| 294 | - // TODO : remove link trap - objects that wasn't marked. |
|
| 295 | - } |
|
| 288 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 289 | + $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + if ($check_existing === true) |
|
| 293 | + { |
|
| 294 | + // TODO : remove link trap - objects that wasn't marked. |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - } |
|
| 297 | + } |
|
| 298 | 298 | |
| 299 | - /** |
|
| 300 | - * Cache mib in database |
|
| 301 | - * @param boolean $display_progress : Display progress on standard output |
|
| 302 | - * @param boolean $check_change : Force check of trap params & objects |
|
| 303 | - * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 304 | - * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 305 | - */ |
|
| 306 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 307 | - { |
|
| 308 | - // Timing |
|
| 309 | - $timeTaken = microtime(true); |
|
| 310 | - $retVal=0; |
|
| 311 | - // Get all mib objects from all mibs |
|
| 312 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 313 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 314 | - unset($this->objectsAll); |
|
| 315 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 316 | - if ($retVal!=0) |
|
| 317 | - { |
|
| 318 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 319 | - } |
|
| 299 | + /** |
|
| 300 | + * Cache mib in database |
|
| 301 | + * @param boolean $display_progress : Display progress on standard output |
|
| 302 | + * @param boolean $check_change : Force check of trap params & objects |
|
| 303 | + * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 304 | + * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 305 | + */ |
|
| 306 | + public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 307 | + { |
|
| 308 | + // Timing |
|
| 309 | + $timeTaken = microtime(true); |
|
| 310 | + $retVal=0; |
|
| 311 | + // Get all mib objects from all mibs |
|
| 312 | + $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 313 | + $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 314 | + unset($this->objectsAll); |
|
| 315 | + exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 316 | + if ($retVal!=0) |
|
| 317 | + { |
|
| 318 | + $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - // Get all mibs from databse to have a memory index |
|
| 321 | + // Get all mibs from databse to have a memory index |
|
| 322 | 322 | |
| 323 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 323 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 324 | 324 | |
| 325 | - $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 326 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 327 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 328 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 329 | - } |
|
| 330 | - $this->dbOidAll=$ret_code->fetchAll(); |
|
| 331 | - $this->dbOidIndex=array(); |
|
| 332 | - // Create the index for db; |
|
| 333 | - foreach($this->dbOidAll as $key=>$val) |
|
| 334 | - { |
|
| 335 | - $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 336 | - $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 337 | - } |
|
| 325 | + $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 326 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 327 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 328 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 329 | + } |
|
| 330 | + $this->dbOidAll=$ret_code->fetchAll(); |
|
| 331 | + $this->dbOidIndex=array(); |
|
| 332 | + // Create the index for db; |
|
| 333 | + foreach($this->dbOidAll as $key=>$val) |
|
| 334 | + { |
|
| 335 | + $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 336 | + $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - // Count elements to show progress |
|
| 340 | - $numElements=count($this->objectsAll); |
|
| 341 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 339 | + // Count elements to show progress |
|
| 340 | + $numElements=count($this->objectsAll); |
|
| 341 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 342 | 342 | |
| 343 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 344 | - $num_step=0; |
|
| 345 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 343 | + $step=$basestep=$numElements/10; // output display of % done |
|
| 344 | + $num_step=0; |
|
| 345 | + $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 346 | 346 | |
| 347 | - // Create index for trap objects |
|
| 348 | - $this->trapObjectsIndex=array(); |
|
| 347 | + // Create index for trap objects |
|
| 348 | + $this->trapObjectsIndex=array(); |
|
| 349 | 349 | |
| 350 | - // detailed timing (time_* vars) |
|
| 351 | - $time_parse1=$time_check1=$time_check2=$time_check3=$time_update=$time_objects=0; |
|
| 352 | - $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
|
| 353 | - $time_num_traps=0; |
|
| 350 | + // detailed timing (time_* vars) |
|
| 351 | + $time_parse1=$time_check1=$time_check2=$time_check3=$time_update=$time_objects=0; |
|
| 352 | + $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
|
| 353 | + $time_num_traps=0; |
|
| 354 | 354 | |
| 355 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 356 | - { |
|
| 357 | - $time_1= microtime(true); |
|
| 358 | - if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
|
| 359 | - { // echo a . every 2 sec |
|
| 360 | - echo '.'; |
|
| 361 | - $timeFiveSec = microtime(true); |
|
| 362 | - } |
|
| 363 | - if ($curElement>$step) |
|
| 364 | - { // display progress |
|
| 365 | - $num_step++; |
|
| 366 | - $step+=$basestep; |
|
| 367 | - if ($display_progress) |
|
| 368 | - { |
|
| 369 | - echo "\n" . ($num_step*10). '% : '; |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - // Get oid or pass if not found |
|
| 373 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 374 | - { |
|
| 375 | - $time_parse1 += microtime(true) - $time_1; |
|
| 376 | - $time_parse1N ++; |
|
| 377 | - continue; |
|
| 378 | - } |
|
| 379 | - $oid=$this->objectsAll[$curElement]; |
|
| 355 | + for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 356 | + { |
|
| 357 | + $time_1= microtime(true); |
|
| 358 | + if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
|
| 359 | + { // echo a . every 2 sec |
|
| 360 | + echo '.'; |
|
| 361 | + $timeFiveSec = microtime(true); |
|
| 362 | + } |
|
| 363 | + if ($curElement>$step) |
|
| 364 | + { // display progress |
|
| 365 | + $num_step++; |
|
| 366 | + $step+=$basestep; |
|
| 367 | + if ($display_progress) |
|
| 368 | + { |
|
| 369 | + echo "\n" . ($num_step*10). '% : '; |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + // Get oid or pass if not found |
|
| 373 | + if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 374 | + { |
|
| 375 | + $time_parse1 += microtime(true) - $time_1; |
|
| 376 | + $time_parse1N ++; |
|
| 377 | + continue; |
|
| 378 | + } |
|
| 379 | + $oid=$this->objectsAll[$curElement]; |
|
| 380 | 380 | |
| 381 | - // get next line |
|
| 382 | - $curElement++; |
|
| 383 | - $match=$snmptrans=array(); |
|
| 384 | - if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 385 | - $this->objectsAll[$curElement],$match)) |
|
| 386 | - { |
|
| 387 | - $time_check1 += microtime(true) - $time_1; |
|
| 388 | - $time_check1N++; |
|
| 389 | - continue; |
|
| 390 | - } |
|
| 381 | + // get next line |
|
| 382 | + $curElement++; |
|
| 383 | + $match=$snmptrans=array(); |
|
| 384 | + if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 385 | + $this->objectsAll[$curElement],$match)) |
|
| 386 | + { |
|
| 387 | + $time_check1 += microtime(true) - $time_1; |
|
| 388 | + $time_check1N++; |
|
| 389 | + continue; |
|
| 390 | + } |
|
| 391 | 391 | |
| 392 | - $name=$match[1]; // Name |
|
| 393 | - $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 392 | + $name=$match[1]; // Name |
|
| 393 | + $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 394 | 394 | |
| 395 | - if ($type==0) // object type=0 : check if v1 trap |
|
| 396 | - { |
|
| 397 | - // Check if next is suboid -> in that case is cannot be a trap |
|
| 398 | - if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
|
| 399 | - { |
|
| 400 | - $time_check2 += microtime(true) - $time_1; |
|
| 401 | - $time_check2N++; |
|
| 402 | - continue; |
|
| 403 | - } |
|
| 404 | - unset($snmptrans); |
|
| 405 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 406 | - ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 407 | - if ($retVal!=0) |
|
| 408 | - { |
|
| 409 | - $time_check2 += microtime(true) - $time_1; |
|
| 410 | - $time_check2N++; |
|
| 411 | - continue; |
|
| 412 | - } |
|
| 413 | - //echo "\n v1 trap found : $oid \n"; |
|
| 414 | - // Force as trap. |
|
| 415 | - $type=21; |
|
| 416 | - } |
|
| 417 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 418 | - { |
|
| 419 | - $time_check3 += microtime(true) - $time_1; |
|
| 420 | - $time_check3N++; |
|
| 421 | - continue; |
|
| 422 | - } |
|
| 395 | + if ($type==0) // object type=0 : check if v1 trap |
|
| 396 | + { |
|
| 397 | + // Check if next is suboid -> in that case is cannot be a trap |
|
| 398 | + if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
|
| 399 | + { |
|
| 400 | + $time_check2 += microtime(true) - $time_1; |
|
| 401 | + $time_check2N++; |
|
| 402 | + continue; |
|
| 403 | + } |
|
| 404 | + unset($snmptrans); |
|
| 405 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 406 | + ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 407 | + if ($retVal!=0) |
|
| 408 | + { |
|
| 409 | + $time_check2 += microtime(true) - $time_1; |
|
| 410 | + $time_check2N++; |
|
| 411 | + continue; |
|
| 412 | + } |
|
| 413 | + //echo "\n v1 trap found : $oid \n"; |
|
| 414 | + // Force as trap. |
|
| 415 | + $type=21; |
|
| 416 | + } |
|
| 417 | + if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 418 | + { |
|
| 419 | + $time_check3 += microtime(true) - $time_1; |
|
| 420 | + $time_check3N++; |
|
| 421 | + continue; |
|
| 422 | + } |
|
| 423 | 423 | |
| 424 | - $time_num_traps++; |
|
| 424 | + $time_num_traps++; |
|
| 425 | 425 | |
| 426 | - $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
|
| 427 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 426 | + $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
|
| 427 | + if ($display_progress) echo '#'; // echo a # when trap found |
|
| 428 | 428 | |
| 429 | - // get trap objects & source MIB |
|
| 430 | - unset($snmptrans); |
|
| 431 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 432 | - ' -Td '.$oid,$snmptrans,$retVal); |
|
| 433 | - if ($retVal!=0) |
|
| 434 | - { |
|
| 435 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 436 | - } |
|
| 429 | + // get trap objects & source MIB |
|
| 430 | + unset($snmptrans); |
|
| 431 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 432 | + ' -Td '.$oid,$snmptrans,$retVal); |
|
| 433 | + if ($retVal!=0) |
|
| 434 | + { |
|
| 435 | + $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 436 | + } |
|
| 437 | 437 | |
| 438 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 439 | - { |
|
| 440 | - $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 441 | - } |
|
| 442 | - $trapMib=$match[1]; |
|
| 438 | + if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 439 | + { |
|
| 440 | + $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 441 | + } |
|
| 442 | + $trapMib=$match[1]; |
|
| 443 | 443 | |
| 444 | - $numLine=1;$trapDesc=''; |
|
| 445 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 446 | - if (isset($snmptrans[$numLine])) |
|
| 447 | - { |
|
| 448 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 444 | + $numLine=1;$trapDesc=''; |
|
| 445 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 446 | + if (isset($snmptrans[$numLine])) |
|
| 447 | + { |
|
| 448 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 449 | 449 | |
| 450 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 451 | - { |
|
| 452 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 453 | - $numLine++; |
|
| 454 | - } |
|
| 455 | - if (isset($snmptrans[$numLine])) { |
|
| 456 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 457 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 458 | - } |
|
| 450 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 451 | + { |
|
| 452 | + $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 453 | + $numLine++; |
|
| 454 | + } |
|
| 455 | + if (isset($snmptrans[$numLine])) { |
|
| 456 | + $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 457 | + $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 458 | + } |
|
| 459 | 459 | |
| 460 | - } |
|
| 461 | - $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
|
| 462 | - $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
|
| 460 | + } |
|
| 461 | + $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
|
| 462 | + $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
|
| 463 | 463 | |
| 464 | - if (($update==0) && ($check_change===false)) |
|
| 465 | - { // Trapd didn't change & force check disabled |
|
| 466 | - $time_objects += microtime(true) - $time_1; |
|
| 467 | - if ($display_progress) echo "C"; |
|
| 468 | - continue; |
|
| 469 | - } |
|
| 464 | + if (($update==0) && ($check_change===false)) |
|
| 465 | + { // Trapd didn't change & force check disabled |
|
| 466 | + $time_objects += microtime(true) - $time_1; |
|
| 467 | + if ($display_progress) echo "C"; |
|
| 468 | + continue; |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | - $synt=null; |
|
| 472 | - foreach ($snmptrans as $line) |
|
| 473 | - { |
|
| 474 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 475 | - { |
|
| 476 | - $synt=$match[1]; |
|
| 477 | - } |
|
| 478 | - } |
|
| 479 | - if ($synt == null) |
|
| 480 | - { |
|
| 481 | - //echo "No objects for $trapOID\n"; |
|
| 482 | - $time_objects += microtime(true) - $time_1; |
|
| 483 | - continue; |
|
| 484 | - } |
|
| 485 | - //echo "$synt \n"; |
|
| 486 | - $trapObjects=array(); |
|
| 487 | - while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 488 | - { |
|
| 489 | - array_push($trapObjects,$match[1]); |
|
| 490 | - $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 491 | - } |
|
| 471 | + $synt=null; |
|
| 472 | + foreach ($snmptrans as $line) |
|
| 473 | + { |
|
| 474 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 475 | + { |
|
| 476 | + $synt=$match[1]; |
|
| 477 | + } |
|
| 478 | + } |
|
| 479 | + if ($synt == null) |
|
| 480 | + { |
|
| 481 | + //echo "No objects for $trapOID\n"; |
|
| 482 | + $time_objects += microtime(true) - $time_1; |
|
| 483 | + continue; |
|
| 484 | + } |
|
| 485 | + //echo "$synt \n"; |
|
| 486 | + $trapObjects=array(); |
|
| 487 | + while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 488 | + { |
|
| 489 | + array_push($trapObjects,$match[1]); |
|
| 490 | + $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - $this->trap_objects($oid, $trapMib, $trapObjects, false); |
|
| 493 | + $this->trap_objects($oid, $trapMib, $trapObjects, false); |
|
| 494 | 494 | |
| 495 | - $time_objects += microtime(true) - $time_1; |
|
| 496 | - $time_objectsN++; |
|
| 497 | - } |
|
| 495 | + $time_objects += microtime(true) - $time_1; |
|
| 496 | + $time_objectsN++; |
|
| 497 | + } |
|
| 498 | 498 | |
| 499 | - if ($display_progress) |
|
| 500 | - { |
|
| 501 | - echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 502 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 503 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 504 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 505 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 506 | - } |
|
| 499 | + if ($display_progress) |
|
| 500 | + { |
|
| 501 | + echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 502 | + echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 503 | + echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 504 | + echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 505 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 506 | + } |
|
| 507 | 507 | |
| 508 | - // Timing ends |
|
| 509 | - $timeTaken=microtime(true) - $timeTaken; |
|
| 510 | - if ($display_progress) |
|
| 511 | - { |
|
| 512 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 513 | - } |
|
| 508 | + // Timing ends |
|
| 509 | + $timeTaken=microtime(true) - $timeTaken; |
|
| 510 | + if ($display_progress) |
|
| 511 | + { |
|
| 512 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 513 | + } |
|
| 514 | 514 | |
| 515 | - } |
|
| 515 | + } |
|
| 516 | 516 | |
| 517 | 517 | |
| 518 | 518 | } |
| 519 | 519 | \ No newline at end of file |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | * @param Logging $logClass : where to log |
| 20 | 20 | * @param Database $dbClass : Database |
| 21 | 21 | */ |
| 22 | - function __construct($logClass,$dbClass) |
|
| 22 | + function __construct($logClass, $dbClass) |
|
| 23 | 23 | { |
| 24 | 24 | $this->logging=$logClass; |
| 25 | 25 | $this->trapsDB=$dbClass; |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * @param string $description |
| 40 | 40 | * @return number : 0=unchanged, 1 = changed, 2=created |
| 41 | 41 | */ |
| 42 | - public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
|
| 42 | + public function update_oid($oid, $mib, $name, $type, $textConv, $dispHint, $syntax, $type_enum, $description=NULL) |
|
| 43 | 43 | { |
| 44 | 44 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 45 | 45 | $description=$db_conn->quote($description); |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | { // newly created. |
| 50 | 50 | return 0; |
| 51 | 51 | } |
| 52 | - if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 52 | + if ($name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 53 | 53 | $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
| 54 | 54 | $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
| 55 | 55 | //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
@@ -69,23 +69,23 @@ discard block |
||
| 69 | 69 | ':name' => $name, |
| 70 | 70 | ':type' => $type, |
| 71 | 71 | ':mib' => $mib, |
| 72 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 73 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 74 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 75 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 76 | - ':description' => ($description==null)?'null':$description, |
|
| 72 | + ':tc' => ($textConv == null) ? 'null' : $textConv, |
|
| 73 | + ':display_hint' => ($dispHint == null) ? 'null' : $dispHint, |
|
| 74 | + ':syntax' => ($syntax == null) ? 'null' : $syntax, |
|
| 75 | + ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, |
|
| 76 | + ':description' => ($description == null) ? 'null' : $description, |
|
| 77 | 77 | ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | if ($sqlQuery->execute($sqlParam) === false) { |
| 81 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 81 | + $this->logging->log('Error in query : '.$sql, ERROR, ''); |
|
| 82 | 82 | } |
| 83 | - $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 83 | + $this->logging->log('Trap updated : '.$name.' / OID : '.$oid, DEBUG); |
|
| 84 | 84 | return 1; |
| 85 | 85 | } |
| 86 | 86 | else |
| 87 | 87 | { |
| 88 | - $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 88 | + $this->logging->log('Trap unchanged : '.$name.' / OID : '.$oid, DEBUG); |
|
| 89 | 89 | return 0; |
| 90 | 90 | } |
| 91 | 91 | } |
@@ -95,11 +95,11 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
| 97 | 97 | '(oid, name, type , mib, textual_convention, display_hint '. |
| 98 | - ', syntax, type_enum , description ) ' . |
|
| 98 | + ', syntax, type_enum , description ) '. |
|
| 99 | 99 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 100 | 100 | ', :syntax, :type_enum, :description )'; |
| 101 | 101 | |
| 102 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 102 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql.='RETURNING id'; |
|
| 103 | 103 | |
| 104 | 104 | $sqlQuery=$db_conn->prepare($sql); |
| 105 | 105 | |
@@ -108,15 +108,15 @@ discard block |
||
| 108 | 108 | ':name' => $name, |
| 109 | 109 | ':type' => $type, |
| 110 | 110 | ':mib' => $mib, |
| 111 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 112 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 113 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 114 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 115 | - ':description' => ($description==null)?'null':$description |
|
| 111 | + ':tc' => ($textConv == null) ? 'null' : $textConv, |
|
| 112 | + ':display_hint' => ($dispHint == null) ? 'null' : $dispHint, |
|
| 113 | + ':syntax' => ($syntax == null) ? 'null' : $syntax, |
|
| 114 | + ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, |
|
| 115 | + ':description' => ($description == null) ? 'null' : $description |
|
| 116 | 116 | ); |
| 117 | 117 | |
| 118 | 118 | if ($sqlQuery->execute($sqlParam) === false) { |
| 119 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 119 | + $this->logging->log('Error in query : '.$sql, 1, ''); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | switch ($this->trapsDB->trapDBType) |
@@ -124,10 +124,10 @@ discard block |
||
| 124 | 124 | case 'pgsql': |
| 125 | 125 | // Get last id to insert oid/values in secondary table |
| 126 | 126 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
| 127 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 127 | + $this->logging->log('Error getting id - pgsql - ', 1, ''); |
|
| 128 | 128 | } |
| 129 | - if (! isset($inserted_id_ret['id'])) { |
|
| 130 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 129 | + if (!isset($inserted_id_ret['id'])) { |
|
| 130 | + $this->logging->log('Error getting id - pgsql - empty.', 1, ''); |
|
| 131 | 131 | } |
| 132 | 132 | $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
| 133 | 133 | break; |
@@ -135,15 +135,15 @@ discard block |
||
| 135 | 135 | // Get last id to insert oid/values in secondary table |
| 136 | 136 | $sql='SELECT LAST_INSERT_ID();'; |
| 137 | 137 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 138 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 138 | + $this->logging->log('Erreur getting id - mysql - ', 1, ''); |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 142 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 142 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 143 | 143 | $this->dbOidIndex[$oid]['id']=$inserted_id; |
| 144 | 144 | break; |
| 145 | 145 | default: |
| 146 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 146 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | // Set as newly created. |
@@ -158,21 +158,21 @@ discard block |
||
| 158 | 158 | * @param array $objects : array of objects name (without MIB) |
| 159 | 159 | * @param bool $check_existing : check instead of create |
| 160 | 160 | */ |
| 161 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 161 | + public function trap_objects($trapOID, $trapmib, $objects, $check_existing) |
|
| 162 | 162 | { |
| 163 | 163 | $dbObjects=null; // cache of objects for trap in db |
| 164 | 164 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 165 | 165 | |
| 166 | 166 | // Get id of trapmib. |
| 167 | 167 | |
| 168 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 168 | + $trapId=$this->dbOidIndex[$trapOID]['id']; |
|
| 169 | 169 | if ($check_existing === true) |
| 170 | 170 | { |
| 171 | 171 | // Get all objects |
| 172 | 172 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
| 173 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 173 | + $this->logging->log('SQL query get all traps: '.$sql, DEBUG); |
|
| 174 | 174 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 175 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 175 | + $this->logging->log('No result in query : '.$sql, 1, ''); |
|
| 176 | 176 | } |
| 177 | 177 | $dbObjectsRaw=$ret_code->fetchAll(); |
| 178 | 178 | |
@@ -186,39 +186,39 @@ discard block |
||
| 186 | 186 | $match=$snmptrans=array(); |
| 187 | 187 | $retVal=0; |
| 188 | 188 | $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
| 189 | - $tmpdesc='';$indesc=false; |
|
| 189 | + $tmpdesc=''; $indesc=false; |
|
| 190 | 190 | |
| 191 | 191 | $objMib=$trapmib; |
| 192 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 193 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 194 | - if ($retVal!=0) |
|
| 192 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 193 | + ' -On -Td '.$objMib.'::'.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 194 | + if ($retVal != 0) |
|
| 195 | 195 | { |
| 196 | 196 | // Maybe not trap mib, search with IR |
| 197 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 198 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 199 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 197 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 198 | + ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 199 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match)) |
|
| 200 | 200 | { // Not found -> continue with warning |
| 201 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 201 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, ''); |
|
| 202 | 202 | continue; |
| 203 | 203 | } |
| 204 | 204 | $objMib=$match[1]; |
| 205 | 205 | |
| 206 | 206 | // Do the snmptranslate again. |
| 207 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 208 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 209 | - if ($retVal!=0) { |
|
| 210 | - $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 207 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 208 | + ' -On -Td '.$objMib.'::'.$object, $snmptrans, $retVal); |
|
| 209 | + if ($retVal != 0) { |
|
| 210 | + $this->logging->log('Error finding trap object : '.$objMib.'::'.$object, 2, ''); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | } |
| 214 | 214 | foreach ($snmptrans as $line) |
| 215 | 215 | { |
| 216 | - if ($indesc===true) |
|
| 216 | + if ($indesc === true) |
|
| 217 | 217 | { |
| 218 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 219 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 218 | + $line=preg_replace('/[\t ]+/', ' ', $line); |
|
| 219 | + if (preg_match('/(.*)"$/', $line, $match)) |
|
| 220 | 220 | { |
| 221 | - $objDesc = $tmpdesc . $match[1]; |
|
| 221 | + $objDesc=$tmpdesc.$match[1]; |
|
| 222 | 222 | $indesc=false; |
| 223 | 223 | } |
| 224 | 224 | $tmpdesc.=$line; |
@@ -229,43 +229,43 @@ discard block |
||
| 229 | 229 | $objOid=$line; |
| 230 | 230 | continue; |
| 231 | 231 | } |
| 232 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 232 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match)) |
|
| 233 | 233 | { |
| 234 | 234 | $objSyntax=$match[1]; |
| 235 | 235 | $objEnum=$match[2]; |
| 236 | 236 | continue; |
| 237 | 237 | } |
| 238 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 238 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match)) |
|
| 239 | 239 | { |
| 240 | 240 | $objSyntax=$match[1]; |
| 241 | 241 | continue; |
| 242 | 242 | } |
| 243 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 243 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match)) |
|
| 244 | 244 | { |
| 245 | 245 | $objDispHint=$match[1]; |
| 246 | 246 | continue; |
| 247 | 247 | } |
| 248 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 248 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match)) |
|
| 249 | 249 | { |
| 250 | 250 | $objDesc=$match[1]; |
| 251 | 251 | continue; |
| 252 | 252 | } |
| 253 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 253 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match)) |
|
| 254 | 254 | { |
| 255 | 255 | $tmpdesc=$match[1]; |
| 256 | 256 | $indesc=true; |
| 257 | 257 | continue; |
| 258 | 258 | } |
| 259 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 259 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match)) |
|
| 260 | 260 | { |
| 261 | 261 | $objTc=$match[1]; |
| 262 | 262 | continue; |
| 263 | 263 | } |
| 264 | 264 | } |
| 265 | - $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 265 | + $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc", DEBUG); |
|
| 266 | 266 | //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
| 267 | 267 | // Update |
| 268 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 268 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum, $objDesc); |
|
| 269 | 269 | |
| 270 | 270 | if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
| 271 | 271 | { // if link exists, continue |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | ); |
| 287 | 287 | |
| 288 | 288 | if ($sqlQuery->execute($sqlParam) === false) { |
| 289 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 289 | + $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$objOid]['id'], 1, ''); |
|
| 290 | 290 | } |
| 291 | 291 | } |
| 292 | 292 | if ($check_existing === true) |
@@ -303,19 +303,19 @@ discard block |
||
| 303 | 303 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
| 304 | 304 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
| 305 | 305 | */ |
| 306 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 306 | + public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1') |
|
| 307 | 307 | { |
| 308 | 308 | // Timing |
| 309 | - $timeTaken = microtime(true); |
|
| 309 | + $timeTaken=microtime(true); |
|
| 310 | 310 | $retVal=0; |
| 311 | 311 | // Get all mib objects from all mibs |
| 312 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 313 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 312 | + $snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 313 | + $this->logging->log('Getting all traps : '.$snmpCommand, DEBUG); |
|
| 314 | 314 | unset($this->objectsAll); |
| 315 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 316 | - if ($retVal!=0) |
|
| 315 | + exec($snmpCommand, $this->objectsAll, $retVal); |
|
| 316 | + if ($retVal != 0) |
|
| 317 | 317 | { |
| 318 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 318 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | // Get all mibs from databse to have a memory index |
@@ -323,14 +323,14 @@ discard block |
||
| 323 | 323 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 324 | 324 | |
| 325 | 325 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
| 326 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 326 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 327 | 327 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 328 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 328 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 329 | 329 | } |
| 330 | 330 | $this->dbOidAll=$ret_code->fetchAll(); |
| 331 | 331 | $this->dbOidIndex=array(); |
| 332 | 332 | // Create the index for db; |
| 333 | - foreach($this->dbOidAll as $key=>$val) |
|
| 333 | + foreach ($this->dbOidAll as $key=>$val) |
|
| 334 | 334 | { |
| 335 | 335 | $this->dbOidIndex[$val['oid']]['key']=$key; |
| 336 | 336 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
@@ -338,11 +338,11 @@ discard block |
||
| 338 | 338 | |
| 339 | 339 | // Count elements to show progress |
| 340 | 340 | $numElements=count($this->objectsAll); |
| 341 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 341 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO); |
|
| 342 | 342 | |
| 343 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 343 | + $step=$basestep=$numElements / 10; // output display of % done |
|
| 344 | 344 | $num_step=0; |
| 345 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 345 | + $timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds |
|
| 346 | 346 | |
| 347 | 347 | // Create index for trap objects |
| 348 | 348 | $this->trapObjectsIndex=array(); |
@@ -352,28 +352,28 @@ discard block |
||
| 352 | 352 | $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
| 353 | 353 | $time_num_traps=0; |
| 354 | 354 | |
| 355 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 355 | + for ($curElement=0; $curElement < $numElements; $curElement++) |
|
| 356 | 356 | { |
| 357 | - $time_1= microtime(true); |
|
| 358 | - if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
|
| 357 | + $time_1=microtime(true); |
|
| 358 | + if ((microtime(true) - $timeFiveSec) > 2 && $display_progress) |
|
| 359 | 359 | { // echo a . every 2 sec |
| 360 | 360 | echo '.'; |
| 361 | - $timeFiveSec = microtime(true); |
|
| 361 | + $timeFiveSec=microtime(true); |
|
| 362 | 362 | } |
| 363 | - if ($curElement>$step) |
|
| 363 | + if ($curElement > $step) |
|
| 364 | 364 | { // display progress |
| 365 | 365 | $num_step++; |
| 366 | 366 | $step+=$basestep; |
| 367 | 367 | if ($display_progress) |
| 368 | 368 | { |
| 369 | - echo "\n" . ($num_step*10). '% : '; |
|
| 369 | + echo "\n".($num_step * 10).'% : '; |
|
| 370 | 370 | } |
| 371 | 371 | } |
| 372 | 372 | // Get oid or pass if not found |
| 373 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 373 | + if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement])) |
|
| 374 | 374 | { |
| 375 | - $time_parse1 += microtime(true) - $time_1; |
|
| 376 | - $time_parse1N ++; |
|
| 375 | + $time_parse1+=microtime(true) - $time_1; |
|
| 376 | + $time_parse1N++; |
|
| 377 | 377 | continue; |
| 378 | 378 | } |
| 379 | 379 | $oid=$this->objectsAll[$curElement]; |
@@ -382,9 +382,9 @@ discard block |
||
| 382 | 382 | $curElement++; |
| 383 | 383 | $match=$snmptrans=array(); |
| 384 | 384 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
| 385 | - $this->objectsAll[$curElement],$match)) |
|
| 385 | + $this->objectsAll[$curElement], $match)) |
|
| 386 | 386 | { |
| 387 | - $time_check1 += microtime(true) - $time_1; |
|
| 387 | + $time_check1+=microtime(true) - $time_1; |
|
| 388 | 388 | $time_check1N++; |
| 389 | 389 | continue; |
| 390 | 390 | } |
@@ -392,21 +392,21 @@ discard block |
||
| 392 | 392 | $name=$match[1]; // Name |
| 393 | 393 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 394 | 394 | |
| 395 | - if ($type==0) // object type=0 : check if v1 trap |
|
| 395 | + if ($type == 0) // object type=0 : check if v1 trap |
|
| 396 | 396 | { |
| 397 | 397 | // Check if next is suboid -> in that case is cannot be a trap |
| 398 | - if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
|
| 398 | + if (preg_match("/^$oid/", $this->objectsAll[$curElement + 1])) |
|
| 399 | 399 | { |
| 400 | - $time_check2 += microtime(true) - $time_1; |
|
| 400 | + $time_check2+=microtime(true) - $time_1; |
|
| 401 | 401 | $time_check2N++; |
| 402 | 402 | continue; |
| 403 | 403 | } |
| 404 | 404 | unset($snmptrans); |
| 405 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 406 | - ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 407 | - if ($retVal!=0) |
|
| 405 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 406 | + ' -Td '.$oid.' | grep OBJECTS ', $snmptrans, $retVal); |
|
| 407 | + if ($retVal != 0) |
|
| 408 | 408 | { |
| 409 | - $time_check2 += microtime(true) - $time_1; |
|
| 409 | + $time_check2+=microtime(true) - $time_1; |
|
| 410 | 410 | $time_check2N++; |
| 411 | 411 | continue; |
| 412 | 412 | } |
@@ -414,56 +414,56 @@ discard block |
||
| 414 | 414 | // Force as trap. |
| 415 | 415 | $type=21; |
| 416 | 416 | } |
| 417 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 417 | + if ($onlyTraps === true && $type != 21) // if only traps and not a trap, continue |
|
| 418 | 418 | { |
| 419 | - $time_check3 += microtime(true) - $time_1; |
|
| 419 | + $time_check3+=microtime(true) - $time_1; |
|
| 420 | 420 | $time_check3N++; |
| 421 | 421 | continue; |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | $time_num_traps++; |
| 425 | 425 | |
| 426 | - $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
|
| 426 | + $this->logging->log('Found trap : '.$match[1].' / OID : '.$oid, INFO); |
|
| 427 | 427 | if ($display_progress) echo '#'; // echo a # when trap found |
| 428 | 428 | |
| 429 | 429 | // get trap objects & source MIB |
| 430 | 430 | unset($snmptrans); |
| 431 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 432 | - ' -Td '.$oid,$snmptrans,$retVal); |
|
| 433 | - if ($retVal!=0) |
|
| 431 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 432 | + ' -Td '.$oid, $snmptrans, $retVal); |
|
| 433 | + if ($retVal != 0) |
|
| 434 | 434 | { |
| 435 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 435 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
| 436 | 436 | } |
| 437 | 437 | |
| 438 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 438 | + if (!preg_match('/^(.*)::/', $snmptrans[0], $match)) |
|
| 439 | 439 | { |
| 440 | - $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 440 | + $this->logging->log('Error getting mib from trap '.$oid.' : '.$snmptrans[0], 1, ''); |
|
| 441 | 441 | } |
| 442 | 442 | $trapMib=$match[1]; |
| 443 | 443 | |
| 444 | - $numLine=1;$trapDesc=''; |
|
| 445 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 444 | + $numLine=1; $trapDesc=''; |
|
| 445 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++; |
|
| 446 | 446 | if (isset($snmptrans[$numLine])) |
| 447 | 447 | { |
| 448 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 448 | + $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]); |
|
| 449 | 449 | |
| 450 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 450 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine])) |
|
| 451 | 451 | { |
| 452 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 452 | + $trapDesc.=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]); |
|
| 453 | 453 | $numLine++; |
| 454 | 454 | } |
| 455 | 455 | if (isset($snmptrans[$numLine])) { |
| 456 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 457 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 456 | + $trapDesc.=preg_replace('/".*/', '', $snmptrans[$numLine]); |
|
| 457 | + $trapDesc=preg_replace('/[\t ]+/', ' ', $trapDesc); |
|
| 458 | 458 | } |
| 459 | 459 | |
| 460 | 460 | } |
| 461 | - $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
|
| 462 | - $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
|
| 461 | + $update=$this->update_oid($oid, $trapMib, $name, $type, NULL, NULL, NULL, NULL, $trapDesc); |
|
| 462 | + $time_update+=microtime(true) - $time_1; $time_1=microtime(true); |
|
| 463 | 463 | |
| 464 | - if (($update==0) && ($check_change===false)) |
|
| 464 | + if (($update == 0) && ($check_change === false)) |
|
| 465 | 465 | { // Trapd didn't change & force check disabled |
| 466 | - $time_objects += microtime(true) - $time_1; |
|
| 466 | + $time_objects+=microtime(true) - $time_1; |
|
| 467 | 467 | if ($display_progress) echo "C"; |
| 468 | 468 | continue; |
| 469 | 469 | } |
@@ -471,7 +471,7 @@ discard block |
||
| 471 | 471 | $synt=null; |
| 472 | 472 | foreach ($snmptrans as $line) |
| 473 | 473 | { |
| 474 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 474 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match)) |
|
| 475 | 475 | { |
| 476 | 476 | $synt=$match[1]; |
| 477 | 477 | } |
@@ -479,30 +479,30 @@ discard block |
||
| 479 | 479 | if ($synt == null) |
| 480 | 480 | { |
| 481 | 481 | //echo "No objects for $trapOID\n"; |
| 482 | - $time_objects += microtime(true) - $time_1; |
|
| 482 | + $time_objects+=microtime(true) - $time_1; |
|
| 483 | 483 | continue; |
| 484 | 484 | } |
| 485 | 485 | //echo "$synt \n"; |
| 486 | 486 | $trapObjects=array(); |
| 487 | - while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 487 | + while (preg_match('/ *([^ ,]+) *,* */', $synt, $match)) |
|
| 488 | 488 | { |
| 489 | - array_push($trapObjects,$match[1]); |
|
| 490 | - $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 489 | + array_push($trapObjects, $match[1]); |
|
| 490 | + $synt=preg_replace('/'.$match[0].'/', '', $synt); |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | $this->trap_objects($oid, $trapMib, $trapObjects, false); |
| 494 | 494 | |
| 495 | - $time_objects += microtime(true) - $time_1; |
|
| 495 | + $time_objects+=microtime(true) - $time_1; |
|
| 496 | 496 | $time_objectsN++; |
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | if ($display_progress) |
| 500 | 500 | { |
| 501 | 501 | echo "\nNumber of processed traps : $time_num_traps \n"; |
| 502 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 503 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 504 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 505 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 502 | + echo "\nParsing : ".number_format($time_parse1 + $time_check1, 1)." sec / ".($time_parse1N + $time_check1N)." occurences\n"; |
|
| 503 | + echo "Detecting traps : ".number_format($time_check2 + $time_check3, 1)." sec / ".($time_check2N + $time_check3N)." occurences\n"; |
|
| 504 | + echo "Trap processing ($time_updateN): ".number_format($time_update, 1)." sec , "; |
|
| 505 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects, 1)." sec \n"; |
|
| 506 | 506 | } |
| 507 | 507 | |
| 508 | 508 | // Timing ends |
@@ -82,8 +82,7 @@ discard block |
||
| 82 | 82 | } |
| 83 | 83 | $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
| 84 | 84 | return 1; |
| 85 | - } |
|
| 86 | - else |
|
| 85 | + } else |
|
| 87 | 86 | { |
| 88 | 87 | $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
| 89 | 88 | return 0; |
@@ -99,7 +98,9 @@ discard block |
||
| 99 | 98 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 100 | 99 | ', :syntax, :type_enum, :description )'; |
| 101 | 100 | |
| 102 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 101 | + if ($this->trapsDB->trapDBType == 'pgsql') { |
|
| 102 | + $sql .= 'RETURNING id'; |
|
| 103 | + } |
|
| 103 | 104 | |
| 104 | 105 | $sqlQuery=$db_conn->prepare($sql); |
| 105 | 106 | |
@@ -139,7 +140,9 @@ discard block |
||
| 139 | 140 | } |
| 140 | 141 | |
| 141 | 142 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 142 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 143 | + if ($inserted_id==false) { |
|
| 144 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 145 | + } |
|
| 143 | 146 | $this->dbOidIndex[$oid]['id']=$inserted_id; |
| 144 | 147 | break; |
| 145 | 148 | default: |
@@ -392,12 +395,14 @@ discard block |
||
| 392 | 395 | $name=$match[1]; // Name |
| 393 | 396 | $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 394 | 397 | |
| 395 | - if ($type==0) // object type=0 : check if v1 trap |
|
| 398 | + if ($type==0) { |
|
| 399 | + // object type=0 : check if v1 trap |
|
| 396 | 400 | { |
| 397 | 401 | // Check if next is suboid -> in that case is cannot be a trap |
| 398 | 402 | if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
| 399 | 403 | { |
| 400 | 404 | $time_check2 += microtime(true) - $time_1; |
| 405 | + } |
|
| 401 | 406 | $time_check2N++; |
| 402 | 407 | continue; |
| 403 | 408 | } |
@@ -414,9 +419,11 @@ discard block |
||
| 414 | 419 | // Force as trap. |
| 415 | 420 | $type=21; |
| 416 | 421 | } |
| 417 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 422 | + if ($onlyTraps===true && $type!=21) { |
|
| 423 | + // if only traps and not a trap, continue |
|
| 418 | 424 | { |
| 419 | 425 | $time_check3 += microtime(true) - $time_1; |
| 426 | + } |
|
| 420 | 427 | $time_check3N++; |
| 421 | 428 | continue; |
| 422 | 429 | } |
@@ -424,7 +431,10 @@ discard block |
||
| 424 | 431 | $time_num_traps++; |
| 425 | 432 | |
| 426 | 433 | $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
| 427 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 434 | + if ($display_progress) { |
|
| 435 | + echo '#'; |
|
| 436 | + } |
|
| 437 | + // echo a # when trap found |
|
| 428 | 438 | |
| 429 | 439 | // get trap objects & source MIB |
| 430 | 440 | unset($snmptrans); |
@@ -442,7 +452,9 @@ discard block |
||
| 442 | 452 | $trapMib=$match[1]; |
| 443 | 453 | |
| 444 | 454 | $numLine=1;$trapDesc=''; |
| 445 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 455 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) { |
|
| 456 | + $numLine++; |
|
| 457 | + } |
|
| 446 | 458 | if (isset($snmptrans[$numLine])) |
| 447 | 459 | { |
| 448 | 460 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
@@ -464,7 +476,9 @@ discard block |
||
| 464 | 476 | if (($update==0) && ($check_change===false)) |
| 465 | 477 | { // Trapd didn't change & force check disabled |
| 466 | 478 | $time_objects += microtime(true) - $time_1; |
| 467 | - if ($display_progress) echo "C"; |
|
| 479 | + if ($display_progress) { |
|
| 480 | + echo "C"; |
|
| 481 | + } |
|
| 468 | 482 | continue; |
| 469 | 483 | } |
| 470 | 484 | |