@@ -15,156 +15,156 @@ |
||
| 15 | 15 | trait TrapConfig |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @return \Trapdirector\Logging */ |
|
| 19 | - abstract public function getLogging(); |
|
| 18 | + /** @return \Trapdirector\Logging */ |
|
| 19 | + abstract public function getLogging(); |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Get option from array of ini file, send message if empty |
|
| 23 | - * @param string $option_array Array of ini file |
|
| 24 | - * @param string $option_category category in ini file |
|
| 25 | - * @param string $option_name name of option in category |
|
| 26 | - * @param mixed $option_var variable to fill if found, left untouched if not found |
|
| 27 | - * @param integer $log_level default 2 (warning) |
|
| 28 | - * @param string $message warning message if not found |
|
| 29 | - * @return boolean true if found, or false |
|
| 30 | - */ |
|
| 31 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
| 32 | - { |
|
| 33 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 34 | - { |
|
| 35 | - if ($message === null) |
|
| 36 | - { |
|
| 37 | - $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
| 38 | - } |
|
| 39 | - $this->getLogging()->log($message,$log_level); |
|
| 40 | - return false; |
|
| 41 | - } |
|
| 42 | - else |
|
| 43 | - { |
|
| 44 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 45 | - return true; |
|
| 46 | - } |
|
| 47 | - } |
|
| 21 | + /** |
|
| 22 | + * Get option from array of ini file, send message if empty |
|
| 23 | + * @param string $option_array Array of ini file |
|
| 24 | + * @param string $option_category category in ini file |
|
| 25 | + * @param string $option_name name of option in category |
|
| 26 | + * @param mixed $option_var variable to fill if found, left untouched if not found |
|
| 27 | + * @param integer $log_level default 2 (warning) |
|
| 28 | + * @param string $message warning message if not found |
|
| 29 | + * @return boolean true if found, or false |
|
| 30 | + */ |
|
| 31 | + protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
| 32 | + { |
|
| 33 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 34 | + { |
|
| 35 | + if ($message === null) |
|
| 36 | + { |
|
| 37 | + $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
| 38 | + } |
|
| 39 | + $this->getLogging()->log($message,$log_level); |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | + else |
|
| 43 | + { |
|
| 44 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 45 | + return true; |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Get options in database |
|
| 51 | - */ |
|
| 52 | - protected function getDatabaseOptions() |
|
| 53 | - { |
|
| 54 | - // Database options |
|
| 55 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 56 | - { |
|
| 57 | - $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
| 58 | - $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
| 59 | - $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
| 60 | - } |
|
| 61 | - } |
|
| 49 | + /** |
|
| 50 | + * Get options in database |
|
| 51 | + */ |
|
| 52 | + protected function getDatabaseOptions() |
|
| 53 | + { |
|
| 54 | + // Database options |
|
| 55 | + if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 56 | + { |
|
| 57 | + $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
| 58 | + $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
| 59 | + $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** Set $variable to value if $element found in database config table |
|
| 64 | - * @param string $element |
|
| 65 | - * @param string $variable |
|
| 66 | - */ |
|
| 67 | - protected function getDBConfigIfSet($element,&$variable) |
|
| 68 | - { |
|
| 69 | - $value=$this->getDBConfig($element); |
|
| 70 | - if ($value != null) $variable=$value; |
|
| 71 | - } |
|
| 63 | + /** Set $variable to value if $element found in database config table |
|
| 64 | + * @param string $element |
|
| 65 | + * @param string $variable |
|
| 66 | + */ |
|
| 67 | + protected function getDBConfigIfSet($element,&$variable) |
|
| 68 | + { |
|
| 69 | + $value=$this->getDBConfig($element); |
|
| 70 | + if ($value != null) $variable=$value; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Get data from db_config |
|
| 75 | - * @param $element string name of param |
|
| 76 | - * @return mixed : value (or null) |
|
| 77 | - */ |
|
| 78 | - protected function getDBConfig($element) // TODO : put this in DB class |
|
| 79 | - { |
|
| 80 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 81 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 82 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 83 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 84 | - return null; |
|
| 85 | - } |
|
| 86 | - $value=$ret_code->fetch(); |
|
| 87 | - if ($value != null && isset($value['value'])) |
|
| 88 | - { |
|
| 89 | - return $value['value']; |
|
| 90 | - } |
|
| 91 | - return null; |
|
| 92 | - } |
|
| 73 | + /** |
|
| 74 | + * Get data from db_config |
|
| 75 | + * @param $element string name of param |
|
| 76 | + * @return mixed : value (or null) |
|
| 77 | + */ |
|
| 78 | + protected function getDBConfig($element) // TODO : put this in DB class |
|
| 79 | + { |
|
| 80 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 81 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 82 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 83 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 84 | + return null; |
|
| 85 | + } |
|
| 86 | + $value=$ret_code->fetch(); |
|
| 87 | + if ($value != null && isset($value['value'])) |
|
| 88 | + { |
|
| 89 | + return $value['value']; |
|
| 90 | + } |
|
| 91 | + return null; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Get options from ini file |
|
| 96 | - * @param array $trap_config : ini file array |
|
| 97 | - */ |
|
| 98 | - protected function getMainOptions($trapConfig) |
|
| 99 | - { |
|
| 94 | + /** |
|
| 95 | + * Get options from ini file |
|
| 96 | + * @param array $trap_config : ini file array |
|
| 97 | + */ |
|
| 98 | + protected function getMainOptions($trapConfig) |
|
| 99 | + { |
|
| 100 | 100 | |
| 101 | - // Snmptranslate binary path |
|
| 102 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 101 | + // Snmptranslate binary path |
|
| 102 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 103 | 103 | |
| 104 | - // mibs path |
|
| 105 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 104 | + // mibs path |
|
| 105 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 106 | 106 | |
| 107 | - // icinga2cmd path |
|
| 108 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 107 | + // icinga2cmd path |
|
| 108 | + $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 109 | 109 | |
| 110 | - // table prefix |
|
| 111 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
| 110 | + // table prefix |
|
| 111 | + $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
| 112 | 112 | |
| 113 | - // API options |
|
| 114 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
| 115 | - { |
|
| 116 | - $this->apiUse=true; |
|
| 117 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort); |
|
| 118 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername); |
|
| 119 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword); |
|
| 120 | - } |
|
| 121 | - } |
|
| 113 | + // API options |
|
| 114 | + if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
| 115 | + { |
|
| 116 | + $this->apiUse=true; |
|
| 117 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort); |
|
| 118 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername); |
|
| 119 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Create and setup database class for trap & ido (if no api) db |
|
| 125 | - * @param array $trap_config : ini file array |
|
| 126 | - */ |
|
| 127 | - protected function setupDatabase($trapConfig) |
|
| 128 | - { |
|
| 129 | - // Trap database |
|
| 130 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 131 | - { |
|
| 132 | - $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 133 | - return; |
|
| 134 | - } |
|
| 135 | - $dbTrapName=$trapConfig['config']['database']; |
|
| 136 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 123 | + /** |
|
| 124 | + * Create and setup database class for trap & ido (if no api) db |
|
| 125 | + * @param array $trap_config : ini file array |
|
| 126 | + */ |
|
| 127 | + protected function setupDatabase($trapConfig) |
|
| 128 | + { |
|
| 129 | + // Trap database |
|
| 130 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
| 131 | + { |
|
| 132 | + $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 133 | + return; |
|
| 134 | + } |
|
| 135 | + $dbTrapName=$trapConfig['config']['database']; |
|
| 136 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 137 | 137 | |
| 138 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
| 139 | - { |
|
| 140 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
| 141 | - return; |
|
| 142 | - } |
|
| 143 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 144 | - { |
|
| 145 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 146 | - return; |
|
| 147 | - } |
|
| 138 | + if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
| 139 | + { |
|
| 140 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
| 141 | + return; |
|
| 142 | + } |
|
| 143 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 144 | + { |
|
| 145 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 146 | + return; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
| 149 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
| 150 | 150 | |
| 151 | - if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
| 151 | + if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
| 152 | 152 | |
| 153 | - // IDO Database |
|
| 154 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 155 | - { |
|
| 156 | - $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 157 | - } |
|
| 158 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 153 | + // IDO Database |
|
| 154 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 155 | + { |
|
| 156 | + $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 157 | + } |
|
| 158 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 159 | 159 | |
| 160 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 161 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 162 | - { |
|
| 163 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 164 | - return; |
|
| 165 | - } |
|
| 160 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 161 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 162 | + { |
|
| 163 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 164 | + return; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 168 | - } |
|
| 167 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | 170 | } |
| 171 | 171 | \ No newline at end of file |
@@ -28,15 +28,15 @@ discard block |
||
| 28 | 28 | * @param string $message warning message if not found |
| 29 | 29 | * @return boolean true if found, or false |
| 30 | 30 | */ |
| 31 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
| 31 | + protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null) |
|
| 32 | 32 | { |
| 33 | 33 | if (!isset($option_array[$option_category][$option_name])) |
| 34 | 34 | { |
| 35 | 35 | if ($message === null) |
| 36 | 36 | { |
| 37 | - $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
| 37 | + $message='No '.$option_name.' in config file: '.$this->trapModuleConfig; |
|
| 38 | 38 | } |
| 39 | - $this->getLogging()->log($message,$log_level); |
|
| 39 | + $this->getLogging()->log($message, $log_level); |
|
| 40 | 40 | return false; |
| 41 | 41 | } |
| 42 | 42 | else |
@@ -54,9 +54,9 @@ discard block |
||
| 54 | 54 | // Database options |
| 55 | 55 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
| 56 | 56 | { |
| 57 | - $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
| 58 | - $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
| 59 | - $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
| 57 | + $this->getDBConfigIfSet('log_level', $this->getLogging()->debugLevel); |
|
| 58 | + $this->getDBConfigIfSet('log_destination', $this->getLogging()->outputMode); |
|
| 59 | + $this->getDBConfigIfSet('log_file', $this->getLogging()->outputFile); |
|
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * @param string $element |
| 65 | 65 | * @param string $variable |
| 66 | 66 | */ |
| 67 | - protected function getDBConfigIfSet($element,&$variable) |
|
| 67 | + protected function getDBConfigIfSet($element, &$variable) |
|
| 68 | 68 | { |
| 69 | 69 | $value=$this->getDBConfig($element); |
| 70 | 70 | if ($value != null) $variable=$value; |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 81 | 81 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
| 82 | 82 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 83 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 83 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 84 | 84 | return null; |
| 85 | 85 | } |
| 86 | 86 | $value=$ret_code->fetch(); |
@@ -99,24 +99,24 @@ discard block |
||
| 99 | 99 | { |
| 100 | 100 | |
| 101 | 101 | // Snmptranslate binary path |
| 102 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 102 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate); |
|
| 103 | 103 | |
| 104 | 104 | // mibs path |
| 105 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 105 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 106 | 106 | |
| 107 | 107 | // icinga2cmd path |
| 108 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 108 | + $this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd); |
|
| 109 | 109 | |
| 110 | 110 | // table prefix |
| 111 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
| 111 | + $this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->dbPrefix); |
|
| 112 | 112 | |
| 113 | 113 | // API options |
| 114 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
| 114 | + if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->apiHostname)) |
|
| 115 | 115 | { |
| 116 | 116 | $this->apiUse=true; |
| 117 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort); |
|
| 118 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername); |
|
| 119 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword); |
|
| 117 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_port', $this->apiPort); |
|
| 118 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_user', $this->apiUsername); |
|
| 119 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_password', $this->apiPassword); |
|
| 120 | 120 | } |
| 121 | 121 | } |
| 122 | 122 | |
@@ -127,40 +127,40 @@ discard block |
||
| 127 | 127 | protected function setupDatabase($trapConfig) |
| 128 | 128 | { |
| 129 | 129 | // Trap database |
| 130 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 130 | + if (!array_key_exists('database', $trapConfig['config'])) |
|
| 131 | 131 | { |
| 132 | - $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 132 | + $this->logging->log("No database in config file: ".$this->trapModuleConfig, ERROR, ''); |
|
| 133 | 133 | return; |
| 134 | 134 | } |
| 135 | 135 | $dbTrapName=$trapConfig['config']['database']; |
| 136 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 136 | + $this->logging->log("Found database in config file: ".$dbTrapName, INFO); |
|
| 137 | 137 | |
| 138 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
| 138 | + if (($dbConfig=parse_ini_file($this->icingaweb2Ressources, true)) === false) |
|
| 139 | 139 | { |
| 140 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
| 140 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources, ERROR, ''); |
|
| 141 | 141 | return; |
| 142 | 142 | } |
| 143 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 143 | + if (!array_key_exists($dbTrapName, $dbConfig)) |
|
| 144 | 144 | { |
| 145 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 145 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources, ERROR, ''); |
|
| 146 | 146 | return; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
| 149 | + $this->trapsDB=new Database($this->logging, $dbConfig[$dbTrapName], $this->dbPrefix); |
|
| 150 | 150 | |
| 151 | 151 | if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
| 152 | 152 | |
| 153 | 153 | // IDO Database |
| 154 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 154 | + if (!array_key_exists('IDOdatabase', $trapConfig['config'])) |
|
| 155 | 155 | { |
| 156 | - $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 156 | + $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig, ERROR, ''); |
|
| 157 | 157 | } |
| 158 | 158 | $dbIdoName=$trapConfig['config']['IDOdatabase']; |
| 159 | 159 | |
| 160 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 161 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 160 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO); |
|
| 161 | + if (!array_key_exists($dbIdoName, $dbConfig)) |
|
| 162 | 162 | { |
| 163 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 163 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources, ERROR, ''); |
|
| 164 | 164 | return; |
| 165 | 165 | } |
| 166 | 166 | |
@@ -38,8 +38,7 @@ discard block |
||
| 38 | 38 | } |
| 39 | 39 | $this->getLogging()->log($message,$log_level); |
| 40 | 40 | return false; |
| 41 | - } |
|
| 42 | - else |
|
| 41 | + } else |
|
| 43 | 42 | { |
| 44 | 43 | $option_var=$option_array[$option_category][$option_name]; |
| 45 | 44 | return true; |
@@ -52,9 +51,11 @@ discard block |
||
| 52 | 51 | protected function getDatabaseOptions() |
| 53 | 52 | { |
| 54 | 53 | // Database options |
| 55 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 54 | + if ($this->logSetup === false) { |
|
| 55 | + // Only if logging was no setup in constructor |
|
| 56 | 56 | { |
| 57 | 57 | $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
| 58 | + } |
|
| 58 | 59 | $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
| 59 | 60 | $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
| 60 | 61 | } |
@@ -67,7 +68,9 @@ discard block |
||
| 67 | 68 | protected function getDBConfigIfSet($element,&$variable) |
| 68 | 69 | { |
| 69 | 70 | $value=$this->getDBConfig($element); |
| 70 | - if ($value != null) $variable=$value; |
|
| 71 | + if ($value != null) { |
|
| 72 | + $variable=$value; |
|
| 73 | + } |
|
| 71 | 74 | } |
| 72 | 75 | |
| 73 | 76 | /** |
@@ -148,7 +151,10 @@ discard block |
||
| 148 | 151 | |
| 149 | 152 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
| 150 | 153 | |
| 151 | - if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
| 154 | + if ($this->apiUse === true) { |
|
| 155 | + return; |
|
| 156 | + } |
|
| 157 | + // In case of API use, no IDO is necessary |
|
| 152 | 158 | |
| 153 | 159 | // IDO Database |
| 154 | 160 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -17,825 +17,825 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Trap |
| 19 | 19 | { |
| 20 | - use TrapConfig; |
|
| 20 | + use TrapConfig; |
|
| 21 | 21 | |
| 22 | - // Configuration files and dirs |
|
| 23 | - /** @var string Icinga etc path */ |
|
| 24 | - protected $icingaweb2Etc; |
|
| 25 | - /** @var string $trapModuleConfig config.ini of module */ |
|
| 26 | - protected $trapModuleConfig; |
|
| 27 | - /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
| 28 | - protected $icingaweb2Ressources; |
|
| 29 | - // Options from config.ini (default values) |
|
| 30 | - /** @var string $snmptranslate */ |
|
| 31 | - protected $snmptranslate='/usr/bin/snmptranslate'; |
|
| 32 | - /** @var string $snmptranslate_dirs */ |
|
| 33 | - protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
| 34 | - /** @var string $icinga2cmd */ |
|
| 35 | - protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
| 36 | - /** @var string $dbPrefix */ |
|
| 37 | - protected $dbPrefix='traps_'; |
|
| 22 | + // Configuration files and dirs |
|
| 23 | + /** @var string Icinga etc path */ |
|
| 24 | + protected $icingaweb2Etc; |
|
| 25 | + /** @var string $trapModuleConfig config.ini of module */ |
|
| 26 | + protected $trapModuleConfig; |
|
| 27 | + /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
| 28 | + protected $icingaweb2Ressources; |
|
| 29 | + // Options from config.ini (default values) |
|
| 30 | + /** @var string $snmptranslate */ |
|
| 31 | + protected $snmptranslate='/usr/bin/snmptranslate'; |
|
| 32 | + /** @var string $snmptranslate_dirs */ |
|
| 33 | + protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
| 34 | + /** @var string $icinga2cmd */ |
|
| 35 | + protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
| 36 | + /** @var string $dbPrefix */ |
|
| 37 | + protected $dbPrefix='traps_'; |
|
| 38 | 38 | |
| 39 | - // API |
|
| 40 | - /** @var boolean $apiUse */ |
|
| 41 | - protected $apiUse=false; |
|
| 42 | - /** @var Icinga2API $icinga2api */ |
|
| 43 | - protected $icinga2api=null; |
|
| 44 | - /** @var string $apiHostname */ |
|
| 45 | - protected $apiHostname=''; |
|
| 46 | - /** @var integer $apiPort */ |
|
| 47 | - protected $apiPort=0; |
|
| 48 | - /** @var string $apiUsername */ |
|
| 49 | - protected $apiUsername=''; |
|
| 50 | - /** @var string $apiPassword */ |
|
| 51 | - protected $apiPassword=''; |
|
| 39 | + // API |
|
| 40 | + /** @var boolean $apiUse */ |
|
| 41 | + protected $apiUse=false; |
|
| 42 | + /** @var Icinga2API $icinga2api */ |
|
| 43 | + protected $icinga2api=null; |
|
| 44 | + /** @var string $apiHostname */ |
|
| 45 | + protected $apiHostname=''; |
|
| 46 | + /** @var integer $apiPort */ |
|
| 47 | + protected $apiPort=0; |
|
| 48 | + /** @var string $apiUsername */ |
|
| 49 | + protected $apiUsername=''; |
|
| 50 | + /** @var string $apiPassword */ |
|
| 51 | + protected $apiPassword=''; |
|
| 52 | 52 | |
| 53 | - // Logs |
|
| 54 | - /** @var Logging Logging class. */ |
|
| 55 | - public $logging; //< Logging class. |
|
| 56 | - /** @var bool true if log was setup in constructor */ |
|
| 57 | - protected $logSetup; //< bool true if log was setup in constructor |
|
| 53 | + // Logs |
|
| 54 | + /** @var Logging Logging class. */ |
|
| 55 | + public $logging; //< Logging class. |
|
| 56 | + /** @var bool true if log was setup in constructor */ |
|
| 57 | + protected $logSetup; //< bool true if log was setup in constructor |
|
| 58 | 58 | |
| 59 | - // Databases |
|
| 60 | - /** @var Database $trapsDB Database class*/ |
|
| 61 | - public $trapsDB = null; |
|
| 59 | + // Databases |
|
| 60 | + /** @var Database $trapsDB Database class*/ |
|
| 61 | + public $trapsDB = null; |
|
| 62 | 62 | |
| 63 | - // Trap received data |
|
| 64 | - protected $receivingHost; |
|
| 65 | - /** @var array Main trap data (oid, source...) */ |
|
| 66 | - public $trapData=array(); |
|
| 67 | - /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
| 68 | - public $trapDataExt=array(); |
|
| 69 | - /** @var int $trapId trap_id after sql insert*/ |
|
| 70 | - public $trapId=null; |
|
| 71 | - /** @var string $trapAction trap action for final write*/ |
|
| 72 | - public $trapAction=null; |
|
| 73 | - /** @var boolean $trapToDb log trap to DB */ |
|
| 74 | - protected $trapToDb=true; |
|
| 63 | + // Trap received data |
|
| 64 | + protected $receivingHost; |
|
| 65 | + /** @var array Main trap data (oid, source...) */ |
|
| 66 | + public $trapData=array(); |
|
| 67 | + /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
| 68 | + public $trapDataExt=array(); |
|
| 69 | + /** @var int $trapId trap_id after sql insert*/ |
|
| 70 | + public $trapId=null; |
|
| 71 | + /** @var string $trapAction trap action for final write*/ |
|
| 72 | + public $trapAction=null; |
|
| 73 | + /** @var boolean $trapToDb log trap to DB */ |
|
| 74 | + protected $trapToDb=true; |
|
| 75 | 75 | |
| 76 | - /** @var Mib mib class */ |
|
| 77 | - public $mibClass = null; |
|
| 76 | + /** @var Mib mib class */ |
|
| 77 | + public $mibClass = null; |
|
| 78 | 78 | |
| 79 | - /** @var Rule rule class */ |
|
| 80 | - public $ruleClass = null; |
|
| 79 | + /** @var Rule rule class */ |
|
| 80 | + public $ruleClass = null; |
|
| 81 | 81 | |
| 82 | - /** @var Plugins plugins manager **/ |
|
| 83 | - public $pluginClass = null; |
|
| 82 | + /** @var Plugins plugins manager **/ |
|
| 83 | + public $pluginClass = null; |
|
| 84 | 84 | |
| 85 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 86 | - { |
|
| 87 | - // Paths of ini files |
|
| 88 | - $this->icingaweb2Etc=$etcDir; |
|
| 89 | - $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
| 90 | - $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
| 91 | - |
|
| 92 | - //************* Setup logging |
|
| 93 | - $this->logging = new Logging(); |
|
| 94 | - if ($baseLogLevel != null) |
|
| 95 | - { |
|
| 96 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 97 | - $this->logSetup=true; |
|
| 98 | - } |
|
| 99 | - else |
|
| 100 | - { |
|
| 101 | - $this->logSetup=false; |
|
| 102 | - } |
|
| 103 | - $this->logging->log('Loggin started', INFO); |
|
| 104 | - |
|
| 105 | - //*************** Get options from ini files |
|
| 106 | - if (! is_file($this->trapModuleConfig)) |
|
| 107 | - { |
|
| 108 | - throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
| 109 | - } |
|
| 110 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
| 111 | - if ($trapConfig == false) |
|
| 112 | - { |
|
| 113 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
| 114 | - throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
| 115 | - } |
|
| 116 | - $this->getMainOptions($trapConfig); // Get main options from ini file |
|
| 117 | - |
|
| 118 | - //*************** Setup database class & get options |
|
| 119 | - $this->setupDatabase($trapConfig); |
|
| 120 | - |
|
| 121 | - $this->getDatabaseOptions(); // Get options in database |
|
| 122 | - |
|
| 123 | - //*************** Setup API |
|
| 124 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
| 125 | - |
|
| 126 | - //*************** Setup MIB |
|
| 127 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 128 | - |
|
| 129 | - //*************** Setup Rule |
|
| 130 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
| 131 | - |
|
| 132 | - $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
| 133 | - 'source_ip' => 'unknown', |
|
| 134 | - 'source_port' => 'unknown', |
|
| 135 | - 'destination_ip' => 'unknown', |
|
| 136 | - 'destination_port' => 'unknown', |
|
| 137 | - 'trap_oid' => 'unknown' |
|
| 138 | - ); |
|
| 139 | - |
|
| 140 | - //*************** Setup Plugins |
|
| 141 | - //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
| 142 | - $this->pluginClass = new Plugins($this); |
|
| 85 | + function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 86 | + { |
|
| 87 | + // Paths of ini files |
|
| 88 | + $this->icingaweb2Etc=$etcDir; |
|
| 89 | + $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
| 90 | + $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
| 91 | + |
|
| 92 | + //************* Setup logging |
|
| 93 | + $this->logging = new Logging(); |
|
| 94 | + if ($baseLogLevel != null) |
|
| 95 | + { |
|
| 96 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 97 | + $this->logSetup=true; |
|
| 98 | + } |
|
| 99 | + else |
|
| 100 | + { |
|
| 101 | + $this->logSetup=false; |
|
| 102 | + } |
|
| 103 | + $this->logging->log('Loggin started', INFO); |
|
| 104 | + |
|
| 105 | + //*************** Get options from ini files |
|
| 106 | + if (! is_file($this->trapModuleConfig)) |
|
| 107 | + { |
|
| 108 | + throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
| 109 | + } |
|
| 110 | + $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
| 111 | + if ($trapConfig == false) |
|
| 112 | + { |
|
| 113 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
| 114 | + throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
| 115 | + } |
|
| 116 | + $this->getMainOptions($trapConfig); // Get main options from ini file |
|
| 117 | + |
|
| 118 | + //*************** Setup database class & get options |
|
| 119 | + $this->setupDatabase($trapConfig); |
|
| 120 | + |
|
| 121 | + $this->getDatabaseOptions(); // Get options in database |
|
| 122 | + |
|
| 123 | + //*************** Setup API |
|
| 124 | + if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
| 125 | + |
|
| 126 | + //*************** Setup MIB |
|
| 127 | + $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 128 | + |
|
| 129 | + //*************** Setup Rule |
|
| 130 | + $this->ruleClass = new Rule($this); //< Create Rule class |
|
| 131 | + |
|
| 132 | + $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
| 133 | + 'source_ip' => 'unknown', |
|
| 134 | + 'source_port' => 'unknown', |
|
| 135 | + 'destination_ip' => 'unknown', |
|
| 136 | + 'destination_port' => 'unknown', |
|
| 137 | + 'trap_oid' => 'unknown' |
|
| 138 | + ); |
|
| 139 | + |
|
| 140 | + //*************** Setup Plugins |
|
| 141 | + //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
| 142 | + $this->pluginClass = new Plugins($this); |
|
| 143 | 143 | |
| 144 | 144 | |
| 145 | - } |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** @return \Trapdirector\Logging */ |
|
| 148 | - public function getLogging() |
|
| 149 | - { |
|
| 150 | - return $this->logging; |
|
| 151 | - } |
|
| 147 | + /** @return \Trapdirector\Logging */ |
|
| 148 | + public function getLogging() |
|
| 149 | + { |
|
| 150 | + return $this->logging; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** @return \Trapdirector\Database */ |
|
| 154 | - public function getTrapsDB() |
|
| 155 | - { |
|
| 156 | - return $this->trapsDB; |
|
| 157 | - } |
|
| 153 | + /** @return \Trapdirector\Database */ |
|
| 154 | + public function getTrapsDB() |
|
| 155 | + { |
|
| 156 | + return $this->trapsDB; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** OBSOLETE Send log. Throws exception on critical error |
|
| 160 | - * @param string $message Message to log |
|
| 161 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 162 | - * @param string $destination file/syslog/display |
|
| 163 | - * @return void |
|
| 164 | - **/ |
|
| 165 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 166 | - { |
|
| 167 | - // TODO : replace ref with $this->logging->log |
|
| 168 | - $this->logging->log($message, $level, $destination); |
|
| 169 | - } |
|
| 159 | + /** OBSOLETE Send log. Throws exception on critical error |
|
| 160 | + * @param string $message Message to log |
|
| 161 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 162 | + * @param string $destination file/syslog/display |
|
| 163 | + * @return void |
|
| 164 | + **/ |
|
| 165 | + public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 166 | + { |
|
| 167 | + // TODO : replace ref with $this->logging->log |
|
| 168 | + $this->logging->log($message, $level, $destination); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 172 | - { |
|
| 173 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 174 | - } |
|
| 171 | + public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 172 | + { |
|
| 173 | + $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * Returns or create new IcingaAPI object |
|
| 178 | - * @return \Icinga\Module\Trapdirector\Icinga2API |
|
| 179 | - */ |
|
| 180 | - protected function getAPI() |
|
| 181 | - { |
|
| 182 | - if ($this->icinga2api == null) |
|
| 183 | - { |
|
| 184 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
| 185 | - } |
|
| 186 | - return $this->icinga2api; |
|
| 187 | - } |
|
| 176 | + /** |
|
| 177 | + * Returns or create new IcingaAPI object |
|
| 178 | + * @return \Icinga\Module\Trapdirector\Icinga2API |
|
| 179 | + */ |
|
| 180 | + protected function getAPI() |
|
| 181 | + { |
|
| 182 | + if ($this->icinga2api == null) |
|
| 183 | + { |
|
| 184 | + $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
| 185 | + } |
|
| 186 | + return $this->icinga2api; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * read data from stream |
|
| 192 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 193 | - * @return mixed array trap data or exception with error |
|
| 194 | - */ |
|
| 195 | - public function read_trap($stream='php://stdin') |
|
| 196 | - { |
|
| 197 | - //Read data from snmptrapd from stdin |
|
| 198 | - $input_stream=fopen($stream, 'r'); |
|
| 199 | - |
|
| 200 | - if ($input_stream === false) |
|
| 201 | - { |
|
| 202 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 203 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 204 | - return null; // note : exception thrown by logging |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // line 1 : host |
|
| 208 | - $this->receivingHost=chop(fgets($input_stream)); |
|
| 209 | - if ($this->receivingHost === false) |
|
| 210 | - { |
|
| 211 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 212 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
| 213 | - } |
|
| 214 | - // line 2 IP:port=>IP:port |
|
| 215 | - $IP=chop(fgets($input_stream)); |
|
| 216 | - if ($IP === false) |
|
| 217 | - { |
|
| 218 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 219 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
| 220 | - } |
|
| 221 | - $matches=array(); |
|
| 222 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 223 | - if ($ret_code===0 || $ret_code===false) |
|
| 224 | - { |
|
| 225 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 226 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 227 | - } |
|
| 228 | - else |
|
| 229 | - { |
|
| 230 | - $this->trapData['source_ip']=$matches[1]; |
|
| 231 | - $this->trapData['destination_ip']=$matches[3]; |
|
| 232 | - $this->trapData['source_port']=$matches[2]; |
|
| 233 | - $this->trapData['destination_port']=$matches[4]; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - while (($vars=fgets($input_stream)) !==false) |
|
| 237 | - { |
|
| 238 | - $vars=chop($vars); |
|
| 239 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 240 | - if ($ret_code===0 || $ret_code===false) |
|
| 241 | - { |
|
| 242 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 243 | - } |
|
| 244 | - else |
|
| 245 | - { |
|
| 246 | - 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')) |
|
| 247 | - { |
|
| 248 | - $this->trapData['trap_oid']=$matches[2]; |
|
| 249 | - } |
|
| 250 | - else |
|
| 251 | - { |
|
| 252 | - $object= new stdClass; |
|
| 253 | - $object->oid =$matches[1]; |
|
| 254 | - $object->value = $matches[2]; |
|
| 255 | - array_push($this->trapDataExt,$object); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - if ($this->trapData['trap_oid']=='unknown') |
|
| 261 | - { |
|
| 262 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
| 263 | - $this->logging->log('no trap oid found',ERROR,''); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - // Translate oids. |
|
| 267 | - |
|
| 268 | - $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
| 269 | - if ($retArray != null) |
|
| 270 | - { |
|
| 271 | - $this->trapData['trap_name']=$retArray['trap_name']; |
|
| 272 | - $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
| 273 | - } |
|
| 274 | - foreach ($this->trapDataExt as $key => $val) |
|
| 275 | - { |
|
| 276 | - $retArray=$this->translateOID($val->oid); |
|
| 277 | - if ($retArray != null) |
|
| 278 | - { |
|
| 279 | - $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
| 280 | - $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - $this->trapData['status']= 'waiting'; |
|
| 286 | - |
|
| 287 | - return $this->trapData; |
|
| 288 | - } |
|
| 190 | + /** |
|
| 191 | + * read data from stream |
|
| 192 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 193 | + * @return mixed array trap data or exception with error |
|
| 194 | + */ |
|
| 195 | + public function read_trap($stream='php://stdin') |
|
| 196 | + { |
|
| 197 | + //Read data from snmptrapd from stdin |
|
| 198 | + $input_stream=fopen($stream, 'r'); |
|
| 199 | + |
|
| 200 | + if ($input_stream === false) |
|
| 201 | + { |
|
| 202 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 203 | + $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 204 | + return null; // note : exception thrown by logging |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // line 1 : host |
|
| 208 | + $this->receivingHost=chop(fgets($input_stream)); |
|
| 209 | + if ($this->receivingHost === false) |
|
| 210 | + { |
|
| 211 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 212 | + $this->logging->log("Error reading Host !",ERROR,''); |
|
| 213 | + } |
|
| 214 | + // line 2 IP:port=>IP:port |
|
| 215 | + $IP=chop(fgets($input_stream)); |
|
| 216 | + if ($IP === false) |
|
| 217 | + { |
|
| 218 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 219 | + $this->logging->log("Error reading IP !",ERROR,''); |
|
| 220 | + } |
|
| 221 | + $matches=array(); |
|
| 222 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 223 | + if ($ret_code===0 || $ret_code===false) |
|
| 224 | + { |
|
| 225 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 226 | + $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 227 | + } |
|
| 228 | + else |
|
| 229 | + { |
|
| 230 | + $this->trapData['source_ip']=$matches[1]; |
|
| 231 | + $this->trapData['destination_ip']=$matches[3]; |
|
| 232 | + $this->trapData['source_port']=$matches[2]; |
|
| 233 | + $this->trapData['destination_port']=$matches[4]; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + while (($vars=fgets($input_stream)) !==false) |
|
| 237 | + { |
|
| 238 | + $vars=chop($vars); |
|
| 239 | + $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 240 | + if ($ret_code===0 || $ret_code===false) |
|
| 241 | + { |
|
| 242 | + $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 243 | + } |
|
| 244 | + else |
|
| 245 | + { |
|
| 246 | + 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')) |
|
| 247 | + { |
|
| 248 | + $this->trapData['trap_oid']=$matches[2]; |
|
| 249 | + } |
|
| 250 | + else |
|
| 251 | + { |
|
| 252 | + $object= new stdClass; |
|
| 253 | + $object->oid =$matches[1]; |
|
| 254 | + $object->value = $matches[2]; |
|
| 255 | + array_push($this->trapDataExt,$object); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + if ($this->trapData['trap_oid']=='unknown') |
|
| 261 | + { |
|
| 262 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
| 263 | + $this->logging->log('no trap oid found',ERROR,''); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + // Translate oids. |
|
| 267 | + |
|
| 268 | + $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
| 269 | + if ($retArray != null) |
|
| 270 | + { |
|
| 271 | + $this->trapData['trap_name']=$retArray['trap_name']; |
|
| 272 | + $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
| 273 | + } |
|
| 274 | + foreach ($this->trapDataExt as $key => $val) |
|
| 275 | + { |
|
| 276 | + $retArray=$this->translateOID($val->oid); |
|
| 277 | + if ($retArray != null) |
|
| 278 | + { |
|
| 279 | + $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
| 280 | + $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + $this->trapData['status']= 'waiting'; |
|
| 286 | + |
|
| 287 | + return $this->trapData; |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - /** |
|
| 291 | - * Translate oid into array(MIB,Name) |
|
| 292 | - * @param $oid string oid to translate |
|
| 293 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 294 | - */ |
|
| 295 | - public function translateOID($oid) |
|
| 296 | - { |
|
| 297 | - // try from database |
|
| 298 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 299 | - |
|
| 300 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
| 301 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 302 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 303 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 304 | - } |
|
| 305 | - $name=$ret_code->fetch(); |
|
| 306 | - if ($name['name'] != null) |
|
| 307 | - { |
|
| 308 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - // Also check if it is an instance of OID |
|
| 312 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 313 | - |
|
| 314 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
| 315 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 316 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 317 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 318 | - } |
|
| 319 | - $name=$ret_code->fetch(); |
|
| 320 | - if ($name['name'] != null) |
|
| 321 | - { |
|
| 322 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // Try to get oid name from snmptranslate |
|
| 326 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 327 | - ' '.$oid); |
|
| 328 | - $matches=array(); |
|
| 329 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 330 | - if ($ret_code===0 || $ret_code === false) { |
|
| 331 | - return NULL; |
|
| 332 | - } else { |
|
| 333 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 334 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 335 | - } |
|
| 336 | - } |
|
| 290 | + /** |
|
| 291 | + * Translate oid into array(MIB,Name) |
|
| 292 | + * @param $oid string oid to translate |
|
| 293 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 294 | + */ |
|
| 295 | + public function translateOID($oid) |
|
| 296 | + { |
|
| 297 | + // try from database |
|
| 298 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 299 | + |
|
| 300 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
| 301 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 302 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 303 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 304 | + } |
|
| 305 | + $name=$ret_code->fetch(); |
|
| 306 | + if ($name['name'] != null) |
|
| 307 | + { |
|
| 308 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + // Also check if it is an instance of OID |
|
| 312 | + $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 313 | + |
|
| 314 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
| 315 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 316 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 317 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 318 | + } |
|
| 319 | + $name=$ret_code->fetch(); |
|
| 320 | + if ($name['name'] != null) |
|
| 321 | + { |
|
| 322 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // Try to get oid name from snmptranslate |
|
| 326 | + $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 327 | + ' '.$oid); |
|
| 328 | + $matches=array(); |
|
| 329 | + $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 330 | + if ($ret_code===0 || $ret_code === false) { |
|
| 331 | + return NULL; |
|
| 332 | + } else { |
|
| 333 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 334 | + return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | 337 | |
| 338 | - /** |
|
| 339 | - * Erase old trap records |
|
| 340 | - * @param integer $days : erase traps when more than $days old |
|
| 341 | - * @return integer : number of lines deleted |
|
| 342 | - **/ |
|
| 343 | - public function eraseOldTraps($days=0) |
|
| 344 | - { |
|
| 345 | - if ($days==0) |
|
| 346 | - { |
|
| 347 | - if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
| 348 | - { |
|
| 349 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 350 | - return; |
|
| 351 | - } |
|
| 352 | - } |
|
| 353 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 354 | - $daysago = strtotime("-".$days." day"); |
|
| 355 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 356 | - if ($db_conn->query($sql) === false) { |
|
| 357 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 358 | - } |
|
| 359 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 360 | - } |
|
| 338 | + /** |
|
| 339 | + * Erase old trap records |
|
| 340 | + * @param integer $days : erase traps when more than $days old |
|
| 341 | + * @return integer : number of lines deleted |
|
| 342 | + **/ |
|
| 343 | + public function eraseOldTraps($days=0) |
|
| 344 | + { |
|
| 345 | + if ($days==0) |
|
| 346 | + { |
|
| 347 | + if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
| 348 | + { |
|
| 349 | + $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 350 | + return; |
|
| 351 | + } |
|
| 352 | + } |
|
| 353 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 354 | + $daysago = strtotime("-".$days." day"); |
|
| 355 | + $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 356 | + if ($db_conn->query($sql) === false) { |
|
| 357 | + $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 358 | + } |
|
| 359 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 360 | + } |
|
| 361 | 361 | |
| 362 | - /** Write error to received trap database |
|
| 363 | - */ |
|
| 364 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 365 | - { |
|
| 366 | - |
|
| 367 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 368 | - |
|
| 369 | - // add date time |
|
| 370 | - $insert_col ='date_received,status'; |
|
| 371 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 372 | - |
|
| 373 | - if ($sourceIP !=null) |
|
| 374 | - { |
|
| 375 | - $insert_col .=',source_ip'; |
|
| 376 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 377 | - } |
|
| 378 | - if ($trapoid !=null) |
|
| 379 | - { |
|
| 380 | - $insert_col .=',trap_oid'; |
|
| 381 | - $insert_val .=",'". $trapoid ."'"; |
|
| 382 | - } |
|
| 383 | - $insert_col .=',status_detail'; |
|
| 384 | - $insert_val .=",'". $message ."'"; |
|
| 385 | - |
|
| 386 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 387 | - |
|
| 388 | - switch ($this->trapsDB->trapDBType) |
|
| 389 | - { |
|
| 390 | - case 'pgsql': |
|
| 391 | - $sql .= ' RETURNING id;'; |
|
| 392 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 393 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 394 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 395 | - } |
|
| 396 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 397 | - // Get last id to insert oid/values in secondary table |
|
| 398 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 362 | + /** Write error to received trap database |
|
| 363 | + */ |
|
| 364 | + public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 365 | + { |
|
| 366 | + |
|
| 367 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 368 | + |
|
| 369 | + // add date time |
|
| 370 | + $insert_col ='date_received,status'; |
|
| 371 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 372 | + |
|
| 373 | + if ($sourceIP !=null) |
|
| 374 | + { |
|
| 375 | + $insert_col .=',source_ip'; |
|
| 376 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 377 | + } |
|
| 378 | + if ($trapoid !=null) |
|
| 379 | + { |
|
| 380 | + $insert_col .=',trap_oid'; |
|
| 381 | + $insert_val .=",'". $trapoid ."'"; |
|
| 382 | + } |
|
| 383 | + $insert_col .=',status_detail'; |
|
| 384 | + $insert_val .=",'". $message ."'"; |
|
| 385 | + |
|
| 386 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 387 | + |
|
| 388 | + switch ($this->trapsDB->trapDBType) |
|
| 389 | + { |
|
| 390 | + case 'pgsql': |
|
| 391 | + $sql .= ' RETURNING id;'; |
|
| 392 | + $this->logging->log('sql : '.$sql,INFO); |
|
| 393 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 394 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 395 | + } |
|
| 396 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 397 | + // Get last id to insert oid/values in secondary table |
|
| 398 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 399 | 399 | |
| 400 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 401 | - } |
|
| 402 | - if (! isset($inserted_id_ret['id'])) { |
|
| 403 | - $this->logging->log('Error getting id',1,''); |
|
| 404 | - } |
|
| 405 | - $this->trapId=$inserted_id_ret['id']; |
|
| 406 | - break; |
|
| 407 | - case 'mysql': |
|
| 408 | - $sql .= ';'; |
|
| 409 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 410 | - if ($db_conn->query($sql) === false) { |
|
| 411 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 412 | - } |
|
| 413 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 414 | - // Get last id to insert oid/values in secondary table |
|
| 415 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 416 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 417 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 418 | - } |
|
| 400 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 401 | + } |
|
| 402 | + if (! isset($inserted_id_ret['id'])) { |
|
| 403 | + $this->logging->log('Error getting id',1,''); |
|
| 404 | + } |
|
| 405 | + $this->trapId=$inserted_id_ret['id']; |
|
| 406 | + break; |
|
| 407 | + case 'mysql': |
|
| 408 | + $sql .= ';'; |
|
| 409 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 410 | + if ($db_conn->query($sql) === false) { |
|
| 411 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 412 | + } |
|
| 413 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 414 | + // Get last id to insert oid/values in secondary table |
|
| 415 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 416 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 417 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 421 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 422 | - $this->trapId=$inserted_id; |
|
| 423 | - break; |
|
| 424 | - default: |
|
| 425 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
| 429 | - } |
|
| 420 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 421 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 422 | + $this->trapId=$inserted_id; |
|
| 423 | + break; |
|
| 424 | + default: |
|
| 425 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + $this->logging->log('id found: '. $this->trapId,INFO ); |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - /** Write trap data to trap database |
|
| 432 | - */ |
|
| 433 | - public function writeTrapToDB() |
|
| 434 | - { |
|
| 435 | - |
|
| 436 | - // If action is ignore -> don't send t DB |
|
| 437 | - if ($this->trapToDb === false) return; |
|
| 438 | - |
|
| 439 | - |
|
| 440 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 441 | - |
|
| 442 | - $insert_col=''; |
|
| 443 | - $insert_val=''; |
|
| 444 | - // add date time |
|
| 445 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
| 446 | - |
|
| 447 | - $firstcol=1; |
|
| 448 | - foreach ($this->trapData as $col => $val) |
|
| 449 | - { |
|
| 450 | - if ($firstcol==0) |
|
| 451 | - { |
|
| 452 | - $insert_col .=','; |
|
| 453 | - $insert_val .=','; |
|
| 454 | - } |
|
| 455 | - $insert_col .= $col ; |
|
| 456 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 457 | - $firstcol=0; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 461 | - switch ($this->trapsDB->trapDBType) |
|
| 462 | - { |
|
| 463 | - case 'pgsql': |
|
| 464 | - $sql .= ' RETURNING id;'; |
|
| 465 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 466 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 467 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 468 | - } |
|
| 469 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 470 | - // Get last id to insert oid/values in secondary table |
|
| 471 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 431 | + /** Write trap data to trap database |
|
| 432 | + */ |
|
| 433 | + public function writeTrapToDB() |
|
| 434 | + { |
|
| 435 | + |
|
| 436 | + // If action is ignore -> don't send t DB |
|
| 437 | + if ($this->trapToDb === false) return; |
|
| 438 | + |
|
| 439 | + |
|
| 440 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 441 | + |
|
| 442 | + $insert_col=''; |
|
| 443 | + $insert_val=''; |
|
| 444 | + // add date time |
|
| 445 | + $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
| 446 | + |
|
| 447 | + $firstcol=1; |
|
| 448 | + foreach ($this->trapData as $col => $val) |
|
| 449 | + { |
|
| 450 | + if ($firstcol==0) |
|
| 451 | + { |
|
| 452 | + $insert_col .=','; |
|
| 453 | + $insert_val .=','; |
|
| 454 | + } |
|
| 455 | + $insert_col .= $col ; |
|
| 456 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 457 | + $firstcol=0; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 461 | + switch ($this->trapsDB->trapDBType) |
|
| 462 | + { |
|
| 463 | + case 'pgsql': |
|
| 464 | + $sql .= ' RETURNING id;'; |
|
| 465 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 466 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 467 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 468 | + } |
|
| 469 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 470 | + // Get last id to insert oid/values in secondary table |
|
| 471 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 472 | 472 | |
| 473 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 474 | - } |
|
| 475 | - if (! isset($inserted_id_ret['id'])) { |
|
| 476 | - $this->logging->log('Error getting id',ERROR,''); |
|
| 477 | - } |
|
| 478 | - $this->trapId=$inserted_id_ret['id']; |
|
| 479 | - break; |
|
| 480 | - case 'mysql': |
|
| 481 | - $sql .= ';'; |
|
| 482 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 483 | - if ($db_conn->query($sql) === false) { |
|
| 484 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 485 | - } |
|
| 486 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 487 | - // Get last id to insert oid/values in secondary table |
|
| 488 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 489 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 490 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 491 | - } |
|
| 473 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 474 | + } |
|
| 475 | + if (! isset($inserted_id_ret['id'])) { |
|
| 476 | + $this->logging->log('Error getting id',ERROR,''); |
|
| 477 | + } |
|
| 478 | + $this->trapId=$inserted_id_ret['id']; |
|
| 479 | + break; |
|
| 480 | + case 'mysql': |
|
| 481 | + $sql .= ';'; |
|
| 482 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 483 | + if ($db_conn->query($sql) === false) { |
|
| 484 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 485 | + } |
|
| 486 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 487 | + // Get last id to insert oid/values in secondary table |
|
| 488 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 489 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 490 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 491 | + } |
|
| 492 | 492 | |
| 493 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 494 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 495 | - $this->trapId=$inserted_id; |
|
| 496 | - break; |
|
| 497 | - default: |
|
| 498 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 499 | - } |
|
| 500 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
| 501 | - |
|
| 502 | - // Fill trap extended data table |
|
| 503 | - foreach ($this->trapDataExt as $value) { |
|
| 504 | - // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
| 505 | - $firstcol=1; |
|
| 506 | - $value->trap_id = $this->trapId; |
|
| 507 | - $insert_col=''; |
|
| 508 | - $insert_val=''; |
|
| 509 | - foreach ($value as $col => $val) |
|
| 510 | - { |
|
| 511 | - if ($firstcol==0) |
|
| 512 | - { |
|
| 513 | - $insert_col .=','; |
|
| 514 | - $insert_val .=','; |
|
| 515 | - } |
|
| 516 | - $insert_col .= $col; |
|
| 517 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 518 | - $firstcol=0; |
|
| 519 | - } |
|
| 493 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 494 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 495 | + $this->trapId=$inserted_id; |
|
| 496 | + break; |
|
| 497 | + default: |
|
| 498 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 499 | + } |
|
| 500 | + $this->logging->log('id found: '.$this->trapId,INFO ); |
|
| 501 | + |
|
| 502 | + // Fill trap extended data table |
|
| 503 | + foreach ($this->trapDataExt as $value) { |
|
| 504 | + // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
| 505 | + $firstcol=1; |
|
| 506 | + $value->trap_id = $this->trapId; |
|
| 507 | + $insert_col=''; |
|
| 508 | + $insert_val=''; |
|
| 509 | + foreach ($value as $col => $val) |
|
| 510 | + { |
|
| 511 | + if ($firstcol==0) |
|
| 512 | + { |
|
| 513 | + $insert_col .=','; |
|
| 514 | + $insert_val .=','; |
|
| 515 | + } |
|
| 516 | + $insert_col .= $col; |
|
| 517 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 518 | + $firstcol=0; |
|
| 519 | + } |
|
| 520 | 520 | |
| 521 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 521 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 522 | 522 | |
| 523 | - if ($db_conn->query($sql) === false) { |
|
| 524 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 525 | - } |
|
| 526 | - } |
|
| 527 | - } |
|
| 523 | + if ($db_conn->query($sql) === false) { |
|
| 524 | + $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 525 | + } |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | 528 | |
| 529 | - /** Get rules from rule database with ip and oid |
|
| 530 | - * @param $ip string ipv4 or ipv6 |
|
| 531 | - * @param $oid string oid in numeric |
|
| 532 | - * @return mixed|boolean : PDO object or false |
|
| 533 | - */ |
|
| 534 | - protected function getRules($ip,$oid) |
|
| 535 | - { |
|
| 536 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 537 | - // fetch rules based on IP in rule and OID |
|
| 538 | - $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
| 539 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 540 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 541 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 542 | - return false; |
|
| 543 | - } |
|
| 544 | - $rules_all=$ret_code->fetchAll(); |
|
| 545 | - //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
| 546 | - $rules_ret=array(); |
|
| 547 | - $rule_ret_key=0; |
|
| 548 | - foreach ($rules_all as $key => $rule) |
|
| 549 | - { |
|
| 550 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 551 | - { |
|
| 552 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 553 | - //TODO : get host name by API (and check if correct in rule). |
|
| 554 | - $rule_ret_key++; |
|
| 555 | - continue; |
|
| 556 | - } |
|
| 557 | - // TODO : get hosts IP by API |
|
| 558 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 559 | - { // get ips of group members by oid |
|
| 560 | - $db_conn2=$this->trapsDB->db_connect_ido(); |
|
| 561 | - $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
| 529 | + /** Get rules from rule database with ip and oid |
|
| 530 | + * @param $ip string ipv4 or ipv6 |
|
| 531 | + * @param $oid string oid in numeric |
|
| 532 | + * @return mixed|boolean : PDO object or false |
|
| 533 | + */ |
|
| 534 | + protected function getRules($ip,$oid) |
|
| 535 | + { |
|
| 536 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 537 | + // fetch rules based on IP in rule and OID |
|
| 538 | + $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
| 539 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 540 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 541 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 542 | + return false; |
|
| 543 | + } |
|
| 544 | + $rules_all=$ret_code->fetchAll(); |
|
| 545 | + //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
| 546 | + $rules_ret=array(); |
|
| 547 | + $rule_ret_key=0; |
|
| 548 | + foreach ($rules_all as $key => $rule) |
|
| 549 | + { |
|
| 550 | + if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 551 | + { |
|
| 552 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 553 | + //TODO : get host name by API (and check if correct in rule). |
|
| 554 | + $rule_ret_key++; |
|
| 555 | + continue; |
|
| 556 | + } |
|
| 557 | + // TODO : get hosts IP by API |
|
| 558 | + if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 559 | + { // get ips of group members by oid |
|
| 560 | + $db_conn2=$this->trapsDB->db_connect_ido(); |
|
| 561 | + $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
| 562 | 562 | FROM icinga_objects as o |
| 563 | 563 | LEFT JOIN icinga_hostgroups as h ON o.object_id=h.hostgroup_object_id |
| 564 | 564 | LEFT JOIN icinga_hostgroup_members as m ON h.hostgroup_id=m.hostgroup_id |
| 565 | 565 | LEFT JOIN icinga_hosts as a ON a.host_object_id = m.host_object_id |
| 566 | 566 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
| 567 | 567 | WHERE o.name1='".$rule['host_group_name']."';"; |
| 568 | - if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
| 569 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 570 | - continue; |
|
| 571 | - } |
|
| 572 | - $grouphosts=$ret_code2->fetchAll(); |
|
| 573 | - //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
| 574 | - foreach ( $grouphosts as $host) |
|
| 575 | - { |
|
| 576 | - //echo $host['ip4']."\n"; |
|
| 577 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 578 | - { |
|
| 579 | - //echo "Rule added \n"; |
|
| 580 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 581 | - $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
| 582 | - $rule_ret_key++; |
|
| 583 | - } |
|
| 584 | - } |
|
| 585 | - } |
|
| 586 | - } |
|
| 587 | - //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
| 588 | - return $rules_ret; |
|
| 589 | - } |
|
| 568 | + if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
| 569 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 570 | + continue; |
|
| 571 | + } |
|
| 572 | + $grouphosts=$ret_code2->fetchAll(); |
|
| 573 | + //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
| 574 | + foreach ( $grouphosts as $host) |
|
| 575 | + { |
|
| 576 | + //echo $host['ip4']."\n"; |
|
| 577 | + if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 578 | + { |
|
| 579 | + //echo "Rule added \n"; |
|
| 580 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
| 581 | + $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
| 582 | + $rule_ret_key++; |
|
| 583 | + } |
|
| 584 | + } |
|
| 585 | + } |
|
| 586 | + } |
|
| 587 | + //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
| 588 | + return $rules_ret; |
|
| 589 | + } |
|
| 590 | 590 | |
| 591 | - /** Add rule match to rule |
|
| 592 | - * @param id int : rule id |
|
| 593 | - * @param set int : value to set |
|
| 594 | - */ |
|
| 595 | - protected function add_rule_match($id, $set) |
|
| 596 | - { |
|
| 597 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 598 | - $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
| 599 | - if ($db_conn->query($sql) === false) { |
|
| 600 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 601 | - } |
|
| 602 | - } |
|
| 591 | + /** Add rule match to rule |
|
| 592 | + * @param id int : rule id |
|
| 593 | + * @param set int : value to set |
|
| 594 | + */ |
|
| 595 | + protected function add_rule_match($id, $set) |
|
| 596 | + { |
|
| 597 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 598 | + $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
| 599 | + if ($db_conn->query($sql) === false) { |
|
| 600 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | 603 | |
| 604 | - /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
| 605 | - * |
|
| 606 | - * @param string $host |
|
| 607 | - * @param string $service |
|
| 608 | - * @param integer $state numerical staus |
|
| 609 | - * @param string $display |
|
| 610 | - * @returnn bool true is service check was sent without error |
|
| 611 | - */ |
|
| 612 | - public function serviceCheckResult($host,$service,$state,$display) |
|
| 613 | - { |
|
| 614 | - if ($this->apiUse === false) |
|
| 615 | - { |
|
| 616 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 617 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 618 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 604 | + /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
| 605 | + * |
|
| 606 | + * @param string $host |
|
| 607 | + * @param string $service |
|
| 608 | + * @param integer $state numerical staus |
|
| 609 | + * @param string $display |
|
| 610 | + * @returnn bool true is service check was sent without error |
|
| 611 | + */ |
|
| 612 | + public function serviceCheckResult($host,$service,$state,$display) |
|
| 613 | + { |
|
| 614 | + if ($this->apiUse === false) |
|
| 615 | + { |
|
| 616 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 617 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 618 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 619 | 619 | |
| 620 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 621 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 622 | - return true; |
|
| 623 | - } |
|
| 624 | - else |
|
| 625 | - { |
|
| 626 | - // Get perfdata if found |
|
| 627 | - $matches=array(); |
|
| 628 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 629 | - { |
|
| 630 | - $display=$matches[1]; |
|
| 631 | - $perfdata=$matches[2]; |
|
| 632 | - } |
|
| 633 | - else |
|
| 634 | - { |
|
| 635 | - $perfdata=''; |
|
| 636 | - } |
|
| 620 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 621 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 622 | + return true; |
|
| 623 | + } |
|
| 624 | + else |
|
| 625 | + { |
|
| 626 | + // Get perfdata if found |
|
| 627 | + $matches=array(); |
|
| 628 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 629 | + { |
|
| 630 | + $display=$matches[1]; |
|
| 631 | + $perfdata=$matches[2]; |
|
| 632 | + } |
|
| 633 | + else |
|
| 634 | + { |
|
| 635 | + $perfdata=''; |
|
| 636 | + } |
|
| 637 | 637 | |
| 638 | - $api = $this->getAPI(); |
|
| 639 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 640 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 641 | - if ($retcode == false) |
|
| 642 | - { |
|
| 643 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 644 | - return false; |
|
| 645 | - } |
|
| 646 | - else |
|
| 647 | - { |
|
| 648 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 649 | - return true; |
|
| 650 | - } |
|
| 651 | - } |
|
| 652 | - } |
|
| 638 | + $api = $this->getAPI(); |
|
| 639 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 640 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 641 | + if ($retcode == false) |
|
| 642 | + { |
|
| 643 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 644 | + return false; |
|
| 645 | + } |
|
| 646 | + else |
|
| 647 | + { |
|
| 648 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 649 | + return true; |
|
| 650 | + } |
|
| 651 | + } |
|
| 652 | + } |
|
| 653 | 653 | |
| 654 | - public function getHostByIP($ip) |
|
| 655 | - { |
|
| 656 | - $api = $this->getAPI(); |
|
| 657 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 658 | - return $api->getHostByIP($ip); |
|
| 659 | - } |
|
| 654 | + public function getHostByIP($ip) |
|
| 655 | + { |
|
| 656 | + $api = $this->getAPI(); |
|
| 657 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
| 658 | + return $api->getHostByIP($ip); |
|
| 659 | + } |
|
| 660 | 660 | |
| 661 | - /** Resolve display. |
|
| 662 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 663 | - * @param $display string |
|
| 664 | - * @return string display |
|
| 665 | - */ |
|
| 666 | - protected function applyDisplay($display) |
|
| 667 | - { |
|
| 668 | - $matches=array(); |
|
| 669 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
| 670 | - { |
|
| 671 | - $oid=$matches[1]; |
|
| 672 | - $found=0; |
|
| 673 | - // Test and transform regexp |
|
| 674 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
| 661 | + /** Resolve display. |
|
| 662 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 663 | + * @param $display string |
|
| 664 | + * @return string display |
|
| 665 | + */ |
|
| 666 | + protected function applyDisplay($display) |
|
| 667 | + { |
|
| 668 | + $matches=array(); |
|
| 669 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
| 670 | + { |
|
| 671 | + $oid=$matches[1]; |
|
| 672 | + $found=0; |
|
| 673 | + // Test and transform regexp |
|
| 674 | + $oidR = $this->ruleClass->regexp_eval($oid); |
|
| 675 | 675 | |
| 676 | - foreach($this->trapDataExt as $val) |
|
| 677 | - { |
|
| 678 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 679 | - { |
|
| 680 | - $val->value=preg_replace('/"/','',$val->value); |
|
| 681 | - $rep=0; |
|
| 682 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 683 | - if ($rep==0) |
|
| 684 | - { |
|
| 685 | - $this->logging->log("Error in display",WARN,''); |
|
| 686 | - return $display; |
|
| 687 | - } |
|
| 688 | - $found=1; |
|
| 689 | - break; |
|
| 690 | - } |
|
| 691 | - } |
|
| 692 | - if ($found==0) |
|
| 693 | - { |
|
| 694 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 695 | - if ($rep==0) |
|
| 696 | - { |
|
| 697 | - $this->logging->log("Error in display",WARN,''); |
|
| 698 | - return $display; |
|
| 699 | - } |
|
| 700 | - } |
|
| 701 | - } |
|
| 702 | - return $display; |
|
| 703 | - } |
|
| 676 | + foreach($this->trapDataExt as $val) |
|
| 677 | + { |
|
| 678 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 679 | + { |
|
| 680 | + $val->value=preg_replace('/"/','',$val->value); |
|
| 681 | + $rep=0; |
|
| 682 | + $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 683 | + if ($rep==0) |
|
| 684 | + { |
|
| 685 | + $this->logging->log("Error in display",WARN,''); |
|
| 686 | + return $display; |
|
| 687 | + } |
|
| 688 | + $found=1; |
|
| 689 | + break; |
|
| 690 | + } |
|
| 691 | + } |
|
| 692 | + if ($found==0) |
|
| 693 | + { |
|
| 694 | + $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 695 | + if ($rep==0) |
|
| 696 | + { |
|
| 697 | + $this->logging->log("Error in display",WARN,''); |
|
| 698 | + return $display; |
|
| 699 | + } |
|
| 700 | + } |
|
| 701 | + } |
|
| 702 | + return $display; |
|
| 703 | + } |
|
| 704 | 704 | |
| 705 | - /** Match rules for current trap and do action |
|
| 706 | - */ |
|
| 707 | - public function applyRules() |
|
| 708 | - { |
|
| 709 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
| 710 | - |
|
| 711 | - if ($rules===false || count($rules)==0) |
|
| 712 | - { |
|
| 713 | - $this->logging->log('No rules found for this trap',INFO ); |
|
| 714 | - $this->trapData['status']='unknown'; |
|
| 715 | - $this->trapToDb=true; |
|
| 716 | - return; |
|
| 717 | - } |
|
| 718 | - //print_r($rules); |
|
| 719 | - // Evaluate all rules in sequence |
|
| 720 | - $this->trapAction=null; |
|
| 721 | - foreach ($rules as $rule) |
|
| 722 | - { |
|
| 705 | + /** Match rules for current trap and do action |
|
| 706 | + */ |
|
| 707 | + public function applyRules() |
|
| 708 | + { |
|
| 709 | + $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
| 710 | + |
|
| 711 | + if ($rules===false || count($rules)==0) |
|
| 712 | + { |
|
| 713 | + $this->logging->log('No rules found for this trap',INFO ); |
|
| 714 | + $this->trapData['status']='unknown'; |
|
| 715 | + $this->trapToDb=true; |
|
| 716 | + return; |
|
| 717 | + } |
|
| 718 | + //print_r($rules); |
|
| 719 | + // Evaluate all rules in sequence |
|
| 720 | + $this->trapAction=null; |
|
| 721 | + foreach ($rules as $rule) |
|
| 722 | + { |
|
| 723 | 723 | |
| 724 | - $host_name=$rule['host_name']; |
|
| 725 | - $service_name=$rule['service_name']; |
|
| 724 | + $host_name=$rule['host_name']; |
|
| 725 | + $service_name=$rule['service_name']; |
|
| 726 | 726 | |
| 727 | - $display=$this->applyDisplay($rule['display']); |
|
| 728 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
| 729 | - try |
|
| 730 | - { |
|
| 731 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 732 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
| 733 | - //->eval_rule($rule['rule']); |
|
| 727 | + $display=$this->applyDisplay($rule['display']); |
|
| 728 | + $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
| 729 | + try |
|
| 730 | + { |
|
| 731 | + $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 732 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
| 733 | + //->eval_rule($rule['rule']); |
|
| 734 | 734 | |
| 735 | - if ($evalr == true) |
|
| 736 | - { |
|
| 737 | - //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
| 738 | - $action=$rule['action_match']; |
|
| 739 | - $this->logging->log('action OK : '.$action,INFO ); |
|
| 740 | - if ($action >= 0) |
|
| 741 | - { |
|
| 742 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 743 | - { |
|
| 744 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
| 745 | - } |
|
| 746 | - else |
|
| 747 | - { |
|
| 748 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 749 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 750 | - } |
|
| 751 | - } |
|
| 752 | - else |
|
| 753 | - { |
|
| 754 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 755 | - } |
|
| 756 | - $this->trapToDb=($action==-2)?false:true; |
|
| 757 | - } |
|
| 758 | - else |
|
| 759 | - { |
|
| 760 | - //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
| 735 | + if ($evalr == true) |
|
| 736 | + { |
|
| 737 | + //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
| 738 | + $action=$rule['action_match']; |
|
| 739 | + $this->logging->log('action OK : '.$action,INFO ); |
|
| 740 | + if ($action >= 0) |
|
| 741 | + { |
|
| 742 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 743 | + { |
|
| 744 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
| 745 | + } |
|
| 746 | + else |
|
| 747 | + { |
|
| 748 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 749 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 750 | + } |
|
| 751 | + } |
|
| 752 | + else |
|
| 753 | + { |
|
| 754 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 755 | + } |
|
| 756 | + $this->trapToDb=($action==-2)?false:true; |
|
| 757 | + } |
|
| 758 | + else |
|
| 759 | + { |
|
| 760 | + //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
| 761 | 761 | |
| 762 | - $action=$rule['action_nomatch']; |
|
| 763 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
| 764 | - if ($action >= 0) |
|
| 765 | - { |
|
| 766 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 767 | - { |
|
| 768 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
| 769 | - } |
|
| 770 | - else |
|
| 771 | - { |
|
| 772 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 773 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 774 | - } |
|
| 775 | - } |
|
| 776 | - else |
|
| 777 | - { |
|
| 778 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 779 | - } |
|
| 780 | - $this->trapToDb=($action==-2)?false:true; |
|
| 781 | - } |
|
| 782 | - // Put name in source_name |
|
| 783 | - if (!isset($this->trapData['source_name'])) |
|
| 784 | - { |
|
| 785 | - $this->trapData['source_name']=$rule['host_name']; |
|
| 786 | - } |
|
| 787 | - else |
|
| 788 | - { |
|
| 789 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
| 790 | - { // only add if not present |
|
| 791 | - $this->trapData['source_name'].=','.$rule['host_name']; |
|
| 792 | - } |
|
| 793 | - } |
|
| 794 | - } |
|
| 795 | - catch (Exception $e) |
|
| 796 | - { |
|
| 797 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 798 | - $this->trapAction.=' ERR : '.$e->getMessage(); |
|
| 799 | - $this->trapData['status']='error'; |
|
| 800 | - } |
|
| 762 | + $action=$rule['action_nomatch']; |
|
| 763 | + $this->logging->log('action NOK : '.$action,INFO ); |
|
| 764 | + if ($action >= 0) |
|
| 765 | + { |
|
| 766 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 767 | + { |
|
| 768 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
| 769 | + } |
|
| 770 | + else |
|
| 771 | + { |
|
| 772 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 773 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 774 | + } |
|
| 775 | + } |
|
| 776 | + else |
|
| 777 | + { |
|
| 778 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 779 | + } |
|
| 780 | + $this->trapToDb=($action==-2)?false:true; |
|
| 781 | + } |
|
| 782 | + // Put name in source_name |
|
| 783 | + if (!isset($this->trapData['source_name'])) |
|
| 784 | + { |
|
| 785 | + $this->trapData['source_name']=$rule['host_name']; |
|
| 786 | + } |
|
| 787 | + else |
|
| 788 | + { |
|
| 789 | + if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
| 790 | + { // only add if not present |
|
| 791 | + $this->trapData['source_name'].=','.$rule['host_name']; |
|
| 792 | + } |
|
| 793 | + } |
|
| 794 | + } |
|
| 795 | + catch (Exception $e) |
|
| 796 | + { |
|
| 797 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 798 | + $this->trapAction.=' ERR : '.$e->getMessage(); |
|
| 799 | + $this->trapData['status']='error'; |
|
| 800 | + } |
|
| 801 | 801 | |
| 802 | - } |
|
| 803 | - if ($this->trapData['status']=='error') |
|
| 804 | - { |
|
| 805 | - $this->trapToDb=true; // Always put errors in DB for the use can see |
|
| 806 | - } |
|
| 807 | - else |
|
| 808 | - { |
|
| 809 | - $this->trapData['status']='done'; |
|
| 810 | - } |
|
| 811 | - } |
|
| 802 | + } |
|
| 803 | + if ($this->trapData['status']=='error') |
|
| 804 | + { |
|
| 805 | + $this->trapToDb=true; // Always put errors in DB for the use can see |
|
| 806 | + } |
|
| 807 | + else |
|
| 808 | + { |
|
| 809 | + $this->trapData['status']='done'; |
|
| 810 | + } |
|
| 811 | + } |
|
| 812 | 812 | |
| 813 | - /** Add Time a action to rule |
|
| 814 | - * @param string $time : time to process to insert in SQL |
|
| 815 | - */ |
|
| 816 | - public function add_rule_final($time) |
|
| 817 | - { |
|
| 818 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 819 | - if ($this->trapAction==null) |
|
| 820 | - { |
|
| 821 | - $this->trapAction='No action'; |
|
| 822 | - } |
|
| 823 | - $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
| 824 | - if ($db_conn->query($sql) === false) { |
|
| 825 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 826 | - } |
|
| 827 | - } |
|
| 813 | + /** Add Time a action to rule |
|
| 814 | + * @param string $time : time to process to insert in SQL |
|
| 815 | + */ |
|
| 816 | + public function add_rule_final($time) |
|
| 817 | + { |
|
| 818 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 819 | + if ($this->trapAction==null) |
|
| 820 | + { |
|
| 821 | + $this->trapAction='No action'; |
|
| 822 | + } |
|
| 823 | + $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
| 824 | + if ($db_conn->query($sql) === false) { |
|
| 825 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 826 | + } |
|
| 827 | + } |
|
| 828 | 828 | |
| 829 | - /*********** UTILITIES *********************/ |
|
| 829 | + /*********** UTILITIES *********************/ |
|
| 830 | 830 | |
| 831 | - /** reset service to OK after time defined in rule |
|
| 832 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 833 | - * @return integer : not in use |
|
| 834 | - **/ |
|
| 835 | - public function reset_services() |
|
| 836 | - { |
|
| 837 | - // Get all services not in 'ok' state |
|
| 838 | - $sql_query="SELECT s.service_object_id, |
|
| 831 | + /** reset service to OK after time defined in rule |
|
| 832 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 833 | + * @return integer : not in use |
|
| 834 | + **/ |
|
| 835 | + public function reset_services() |
|
| 836 | + { |
|
| 837 | + // Get all services not in 'ok' state |
|
| 838 | + $sql_query="SELECT s.service_object_id, |
|
| 839 | 839 | UNIX_TIMESTAMP(s.last_check) AS last_check, |
| 840 | 840 | s.current_state as state, |
| 841 | 841 | v.name1 as host_name, |
@@ -843,43 +843,43 @@ discard block |
||
| 843 | 843 | FROM icinga_servicestatus AS s |
| 844 | 844 | LEFT JOIN icinga_objects as v ON s.service_object_id=v.object_id |
| 845 | 845 | WHERE s.current_state != 0;"; |
| 846 | - $db_conn=$this->trapsDB->db_connect_ido(); |
|
| 847 | - if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
| 848 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 849 | - return 0; |
|
| 850 | - } |
|
| 851 | - $services=$services_db->fetchAll(); |
|
| 852 | - |
|
| 853 | - // Get all rules |
|
| 854 | - $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
| 855 | - $db_conn2=$this->trapsDB->db_connect_trap(); |
|
| 856 | - if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
| 857 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 858 | - return 0; |
|
| 859 | - } |
|
| 860 | - $rules=$rules_db->fetchAll(); |
|
| 861 | - |
|
| 862 | - $now=date('U'); |
|
| 863 | - |
|
| 864 | - $numreset=0; |
|
| 865 | - foreach ($rules as $rule) |
|
| 866 | - { |
|
| 867 | - foreach ($services as $service) |
|
| 868 | - { |
|
| 869 | - if ($service['service_name'] == $rule['service_name'] && |
|
| 870 | - $service['host_name'] == $rule['host_name'] && |
|
| 871 | - ($service['last_check'] + $rule['revert_ok']) < $now) |
|
| 872 | - { |
|
| 873 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 874 | - $numreset++; |
|
| 875 | - } |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - echo "\n"; |
|
| 879 | - echo $numreset . " service(s) reset to OK\n"; |
|
| 880 | - return 0; |
|
| 881 | - |
|
| 882 | - } |
|
| 846 | + $db_conn=$this->trapsDB->db_connect_ido(); |
|
| 847 | + if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
| 848 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 849 | + return 0; |
|
| 850 | + } |
|
| 851 | + $services=$services_db->fetchAll(); |
|
| 852 | + |
|
| 853 | + // Get all rules |
|
| 854 | + $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
| 855 | + $db_conn2=$this->trapsDB->db_connect_trap(); |
|
| 856 | + if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
| 857 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 858 | + return 0; |
|
| 859 | + } |
|
| 860 | + $rules=$rules_db->fetchAll(); |
|
| 861 | + |
|
| 862 | + $now=date('U'); |
|
| 863 | + |
|
| 864 | + $numreset=0; |
|
| 865 | + foreach ($rules as $rule) |
|
| 866 | + { |
|
| 867 | + foreach ($services as $service) |
|
| 868 | + { |
|
| 869 | + if ($service['service_name'] == $rule['service_name'] && |
|
| 870 | + $service['host_name'] == $rule['host_name'] && |
|
| 871 | + ($service['last_check'] + $rule['revert_ok']) < $now) |
|
| 872 | + { |
|
| 873 | + $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 874 | + $numreset++; |
|
| 875 | + } |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + echo "\n"; |
|
| 879 | + echo $numreset . " service(s) reset to OK\n"; |
|
| 880 | + return 0; |
|
| 881 | + |
|
| 882 | + } |
|
| 883 | 883 | |
| 884 | 884 | |
| 885 | 885 | } |
| 886 | 886 | \ No newline at end of file |
@@ -52,13 +52,13 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | // Logs |
| 54 | 54 | /** @var Logging Logging class. */ |
| 55 | - public $logging; //< Logging class. |
|
| 55 | + public $logging; //< Logging class. |
|
| 56 | 56 | /** @var bool true if log was setup in constructor */ |
| 57 | - protected $logSetup; //< bool true if log was setup in constructor |
|
| 57 | + protected $logSetup; //< bool true if log was setup in constructor |
|
| 58 | 58 | |
| 59 | 59 | // Databases |
| 60 | 60 | /** @var Database $trapsDB Database class*/ |
| 61 | - public $trapsDB = null; |
|
| 61 | + public $trapsDB=null; |
|
| 62 | 62 | |
| 63 | 63 | // Trap received data |
| 64 | 64 | protected $receivingHost; |
@@ -74,15 +74,15 @@ discard block |
||
| 74 | 74 | protected $trapToDb=true; |
| 75 | 75 | |
| 76 | 76 | /** @var Mib mib class */ |
| 77 | - public $mibClass = null; |
|
| 77 | + public $mibClass=null; |
|
| 78 | 78 | |
| 79 | 79 | /** @var Rule rule class */ |
| 80 | - public $ruleClass = null; |
|
| 80 | + public $ruleClass=null; |
|
| 81 | 81 | |
| 82 | 82 | /** @var Plugins plugins manager **/ |
| 83 | - public $pluginClass = null; |
|
| 83 | + public $pluginClass=null; |
|
| 84 | 84 | |
| 85 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
| 85 | + function __construct($etcDir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='') |
|
| 86 | 86 | { |
| 87 | 87 | // Paths of ini files |
| 88 | 88 | $this->icingaweb2Etc=$etcDir; |
@@ -90,10 +90,10 @@ discard block |
||
| 90 | 90 | $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
| 91 | 91 | |
| 92 | 92 | //************* Setup logging |
| 93 | - $this->logging = new Logging(); |
|
| 93 | + $this->logging=new Logging(); |
|
| 94 | 94 | if ($baseLogLevel != null) |
| 95 | 95 | { |
| 96 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 96 | + $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile); |
|
| 97 | 97 | $this->logSetup=true; |
| 98 | 98 | } |
| 99 | 99 | else |
@@ -103,14 +103,14 @@ discard block |
||
| 103 | 103 | $this->logging->log('Loggin started', INFO); |
| 104 | 104 | |
| 105 | 105 | //*************** Get options from ini files |
| 106 | - if (! is_file($this->trapModuleConfig)) |
|
| 106 | + if (!is_file($this->trapModuleConfig)) |
|
| 107 | 107 | { |
| 108 | 108 | throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
| 109 | 109 | } |
| 110 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
| 110 | + $trapConfig=parse_ini_file($this->trapModuleConfig, true); |
|
| 111 | 111 | if ($trapConfig == false) |
| 112 | 112 | { |
| 113 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
| 113 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig, ERROR, 'syslog'); |
|
| 114 | 114 | throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
| 115 | 115 | } |
| 116 | 116 | $this->getMainOptions($trapConfig); // Get main options from ini file |
@@ -124,10 +124,10 @@ discard block |
||
| 124 | 124 | if ($this->apiUse === true) $this->getAPI(); // Setup API |
| 125 | 125 | |
| 126 | 126 | //*************** Setup MIB |
| 127 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
| 127 | + $this->mibClass=new Mib($this->logging, $this->trapsDB, $this->snmptranslate, $this->snmptranslate_dirs); // Create Mib class |
|
| 128 | 128 | |
| 129 | 129 | //*************** Setup Rule |
| 130 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
| 130 | + $this->ruleClass=new Rule($this); //< Create Rule class |
|
| 131 | 131 | |
| 132 | 132 | $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
| 133 | 133 | 'source_ip' => 'unknown', |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | |
| 140 | 140 | //*************** Setup Plugins |
| 141 | 141 | //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
| 142 | - $this->pluginClass = new Plugins($this); |
|
| 142 | + $this->pluginClass=new Plugins($this); |
|
| 143 | 143 | |
| 144 | 144 | |
| 145 | 145 | } |
@@ -162,15 +162,15 @@ discard block |
||
| 162 | 162 | * @param string $destination file/syslog/display |
| 163 | 163 | * @return void |
| 164 | 164 | **/ |
| 165 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
| 165 | + public function trapLog($message, $level, $destination='') // OBSOLETE |
|
| 166 | 166 | { |
| 167 | 167 | // TODO : replace ref with $this->logging->log |
| 168 | 168 | $this->logging->log($message, $level, $destination); |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
| 171 | + public function setLogging($debugLvl, $outputType, $outputOption=null) // OBSOLETE |
|
| 172 | 172 | { |
| 173 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
| 173 | + $this->logging->setLogging($debugLvl, $outputType, $outputOption); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | { |
| 182 | 182 | if ($this->icinga2api == null) |
| 183 | 183 | { |
| 184 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
| 184 | + $this->icinga2api=new Icinga2API($this->apiHostname, $this->apiPort); |
|
| 185 | 185 | } |
| 186 | 186 | return $this->icinga2api; |
| 187 | 187 | } |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | if ($input_stream === false) |
| 201 | 201 | { |
| 202 | 202 | $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
| 203 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
| 203 | + $this->logging->log("Error reading stdin !", ERROR, ''); |
|
| 204 | 204 | return null; // note : exception thrown by logging |
| 205 | 205 | } |
| 206 | 206 | |
@@ -209,21 +209,21 @@ discard block |
||
| 209 | 209 | if ($this->receivingHost === false) |
| 210 | 210 | { |
| 211 | 211 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
| 212 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
| 212 | + $this->logging->log("Error reading Host !", ERROR, ''); |
|
| 213 | 213 | } |
| 214 | 214 | // line 2 IP:port=>IP:port |
| 215 | 215 | $IP=chop(fgets($input_stream)); |
| 216 | 216 | if ($IP === false) |
| 217 | 217 | { |
| 218 | 218 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
| 219 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
| 219 | + $this->logging->log("Error reading IP !", ERROR, ''); |
|
| 220 | 220 | } |
| 221 | 221 | $matches=array(); |
| 222 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
| 223 | - if ($ret_code===0 || $ret_code===false) |
|
| 222 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches); |
|
| 223 | + if ($ret_code === 0 || $ret_code === false) |
|
| 224 | 224 | { |
| 225 | 225 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 226 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
| 226 | + $this->logging->log('Error parsing IP : '.$IP, ERROR, ''); |
|
| 227 | 227 | } |
| 228 | 228 | else |
| 229 | 229 | { |
@@ -233,34 +233,34 @@ discard block |
||
| 233 | 233 | $this->trapData['destination_port']=$matches[4]; |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | - while (($vars=fgets($input_stream)) !==false) |
|
| 236 | + while (($vars=fgets($input_stream)) !== false) |
|
| 237 | 237 | { |
| 238 | 238 | $vars=chop($vars); |
| 239 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
| 240 | - if ($ret_code===0 || $ret_code===false) |
|
| 239 | + $ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches); |
|
| 240 | + if ($ret_code === 0 || $ret_code === false) |
|
| 241 | 241 | { |
| 242 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
| 242 | + $this->logging->log('No match on trap data : '.$vars, WARN, ''); |
|
| 243 | 243 | } |
| 244 | 244 | else |
| 245 | 245 | { |
| 246 | - 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')) |
|
| 246 | + 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')) |
|
| 247 | 247 | { |
| 248 | 248 | $this->trapData['trap_oid']=$matches[2]; |
| 249 | 249 | } |
| 250 | 250 | else |
| 251 | 251 | { |
| 252 | - $object= new stdClass; |
|
| 253 | - $object->oid =$matches[1]; |
|
| 254 | - $object->value = $matches[2]; |
|
| 255 | - array_push($this->trapDataExt,$object); |
|
| 252 | + $object=new stdClass; |
|
| 253 | + $object->oid=$matches[1]; |
|
| 254 | + $object->value=$matches[2]; |
|
| 255 | + array_push($this->trapDataExt, $object); |
|
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | - if ($this->trapData['trap_oid']=='unknown') |
|
| 260 | + if ($this->trapData['trap_oid'] == 'unknown') |
|
| 261 | 261 | { |
| 262 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
| 263 | - $this->logging->log('no trap oid found',ERROR,''); |
|
| 262 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trapData['source_ip']); |
|
| 263 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | // Translate oids. |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | |
| 285 | - $this->trapData['status']= 'waiting'; |
|
| 285 | + $this->trapData['status']='waiting'; |
|
| 286 | 286 | |
| 287 | 287 | return $this->trapData; |
| 288 | 288 | } |
@@ -298,40 +298,40 @@ discard block |
||
| 298 | 298 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 299 | 299 | |
| 300 | 300 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
| 301 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 301 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 302 | 302 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 303 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 303 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 304 | 304 | } |
| 305 | 305 | $name=$ret_code->fetch(); |
| 306 | 306 | if ($name['name'] != null) |
| 307 | 307 | { |
| 308 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 308 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | // Also check if it is an instance of OID |
| 312 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
| 312 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
| 313 | 313 | |
| 314 | 314 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
| 315 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 315 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 316 | 316 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 317 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 317 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 318 | 318 | } |
| 319 | 319 | $name=$ret_code->fetch(); |
| 320 | 320 | if ($name['name'] != null) |
| 321 | 321 | { |
| 322 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
| 322 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | // Try to get oid name from snmptranslate |
| 326 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 326 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 327 | 327 | ' '.$oid); |
| 328 | 328 | $matches=array(); |
| 329 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
| 330 | - if ($ret_code===0 || $ret_code === false) { |
|
| 329 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
| 330 | + if ($ret_code === 0 || $ret_code === false) { |
|
| 331 | 331 | return NULL; |
| 332 | 332 | } else { |
| 333 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
| 334 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
| 333 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
| 334 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | 337 | |
@@ -342,90 +342,90 @@ discard block |
||
| 342 | 342 | **/ |
| 343 | 343 | public function eraseOldTraps($days=0) |
| 344 | 344 | { |
| 345 | - if ($days==0) |
|
| 345 | + if ($days == 0) |
|
| 346 | 346 | { |
| 347 | 347 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
| 348 | 348 | { |
| 349 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
| 349 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
| 350 | 350 | return; |
| 351 | 351 | } |
| 352 | 352 | } |
| 353 | 353 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 354 | - $daysago = strtotime("-".$days." day"); |
|
| 355 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
| 354 | + $daysago=strtotime("-".$days." day"); |
|
| 355 | + $sql='delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
| 356 | 356 | if ($db_conn->query($sql) === false) { |
| 357 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
| 357 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
| 358 | 358 | } |
| 359 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
| 359 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | /** Write error to received trap database |
| 363 | 363 | */ |
| 364 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
| 364 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
| 365 | 365 | { |
| 366 | 366 | |
| 367 | 367 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 368 | 368 | |
| 369 | 369 | // add date time |
| 370 | - $insert_col ='date_received,status'; |
|
| 371 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 370 | + $insert_col='date_received,status'; |
|
| 371 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
| 372 | 372 | |
| 373 | - if ($sourceIP !=null) |
|
| 373 | + if ($sourceIP != null) |
|
| 374 | 374 | { |
| 375 | - $insert_col .=',source_ip'; |
|
| 376 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 375 | + $insert_col.=',source_ip'; |
|
| 376 | + $insert_val.=",'".$sourceIP."'"; |
|
| 377 | 377 | } |
| 378 | - if ($trapoid !=null) |
|
| 378 | + if ($trapoid != null) |
|
| 379 | 379 | { |
| 380 | - $insert_col .=',trap_oid'; |
|
| 381 | - $insert_val .=",'". $trapoid ."'"; |
|
| 380 | + $insert_col.=',trap_oid'; |
|
| 381 | + $insert_val.=",'".$trapoid."'"; |
|
| 382 | 382 | } |
| 383 | - $insert_col .=',status_detail'; |
|
| 384 | - $insert_val .=",'". $message ."'"; |
|
| 383 | + $insert_col.=',status_detail'; |
|
| 384 | + $insert_val.=",'".$message."'"; |
|
| 385 | 385 | |
| 386 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 386 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
| 387 | 387 | |
| 388 | 388 | switch ($this->trapsDB->trapDBType) |
| 389 | 389 | { |
| 390 | 390 | case 'pgsql': |
| 391 | - $sql .= ' RETURNING id;'; |
|
| 392 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 391 | + $sql.=' RETURNING id;'; |
|
| 392 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 393 | 393 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 394 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 394 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
| 395 | 395 | } |
| 396 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 396 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 397 | 397 | // Get last id to insert oid/values in secondary table |
| 398 | 398 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
| 399 | 399 | |
| 400 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 400 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
| 401 | 401 | } |
| 402 | - if (! isset($inserted_id_ret['id'])) { |
|
| 403 | - $this->logging->log('Error getting id',1,''); |
|
| 402 | + if (!isset($inserted_id_ret['id'])) { |
|
| 403 | + $this->logging->log('Error getting id', 1, ''); |
|
| 404 | 404 | } |
| 405 | 405 | $this->trapId=$inserted_id_ret['id']; |
| 406 | 406 | break; |
| 407 | 407 | case 'mysql': |
| 408 | - $sql .= ';'; |
|
| 409 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 408 | + $sql.=';'; |
|
| 409 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 410 | 410 | if ($db_conn->query($sql) === false) { |
| 411 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 411 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
| 412 | 412 | } |
| 413 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 413 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 414 | 414 | // Get last id to insert oid/values in secondary table |
| 415 | 415 | $sql='SELECT LAST_INSERT_ID();'; |
| 416 | 416 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 417 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 417 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 421 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 421 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 422 | 422 | $this->trapId=$inserted_id; |
| 423 | 423 | break; |
| 424 | 424 | default: |
| 425 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 425 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
| 426 | 426 | } |
| 427 | 427 | |
| 428 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
| 428 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | /** Write trap data to trap database |
@@ -442,86 +442,86 @@ discard block |
||
| 442 | 442 | $insert_col=''; |
| 443 | 443 | $insert_val=''; |
| 444 | 444 | // add date time |
| 445 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
| 445 | + $this->trapData['date_received']=date("Y-m-d H:i:s"); |
|
| 446 | 446 | |
| 447 | 447 | $firstcol=1; |
| 448 | 448 | foreach ($this->trapData as $col => $val) |
| 449 | 449 | { |
| 450 | - if ($firstcol==0) |
|
| 450 | + if ($firstcol == 0) |
|
| 451 | 451 | { |
| 452 | - $insert_col .=','; |
|
| 453 | - $insert_val .=','; |
|
| 452 | + $insert_col.=','; |
|
| 453 | + $insert_val.=','; |
|
| 454 | 454 | } |
| 455 | - $insert_col .= $col ; |
|
| 456 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 455 | + $insert_col.=$col; |
|
| 456 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
| 457 | 457 | $firstcol=0; |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 460 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
| 461 | 461 | switch ($this->trapsDB->trapDBType) |
| 462 | 462 | { |
| 463 | 463 | case 'pgsql': |
| 464 | - $sql .= ' RETURNING id;'; |
|
| 465 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 464 | + $sql.=' RETURNING id;'; |
|
| 465 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 466 | 466 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 467 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 467 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
| 468 | 468 | } |
| 469 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 469 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 470 | 470 | // Get last id to insert oid/values in secondary table |
| 471 | 471 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
| 472 | 472 | |
| 473 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 473 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
| 474 | 474 | } |
| 475 | - if (! isset($inserted_id_ret['id'])) { |
|
| 476 | - $this->logging->log('Error getting id',ERROR,''); |
|
| 475 | + if (!isset($inserted_id_ret['id'])) { |
|
| 476 | + $this->logging->log('Error getting id', ERROR, ''); |
|
| 477 | 477 | } |
| 478 | 478 | $this->trapId=$inserted_id_ret['id']; |
| 479 | 479 | break; |
| 480 | 480 | case 'mysql': |
| 481 | - $sql .= ';'; |
|
| 482 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 481 | + $sql.=';'; |
|
| 482 | + $this->logging->log('sql : '.$sql, INFO); |
|
| 483 | 483 | if ($db_conn->query($sql) === false) { |
| 484 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
| 484 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
| 485 | 485 | } |
| 486 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 486 | + $this->logging->log('SQL insertion OK', INFO); |
|
| 487 | 487 | // Get last id to insert oid/values in secondary table |
| 488 | 488 | $sql='SELECT LAST_INSERT_ID();'; |
| 489 | 489 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 490 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
| 490 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 494 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 494 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 495 | 495 | $this->trapId=$inserted_id; |
| 496 | 496 | break; |
| 497 | 497 | default: |
| 498 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
| 498 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
| 499 | 499 | } |
| 500 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
| 500 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
| 501 | 501 | |
| 502 | 502 | // Fill trap extended data table |
| 503 | 503 | foreach ($this->trapDataExt as $value) { |
| 504 | 504 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
| 505 | 505 | $firstcol=1; |
| 506 | - $value->trap_id = $this->trapId; |
|
| 506 | + $value->trap_id=$this->trapId; |
|
| 507 | 507 | $insert_col=''; |
| 508 | 508 | $insert_val=''; |
| 509 | 509 | foreach ($value as $col => $val) |
| 510 | 510 | { |
| 511 | - if ($firstcol==0) |
|
| 511 | + if ($firstcol == 0) |
|
| 512 | 512 | { |
| 513 | - $insert_col .=','; |
|
| 514 | - $insert_val .=','; |
|
| 513 | + $insert_col.=','; |
|
| 514 | + $insert_val.=','; |
|
| 515 | 515 | } |
| 516 | - $insert_col .= $col; |
|
| 517 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
| 516 | + $insert_col.=$col; |
|
| 517 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
| 518 | 518 | $firstcol=0; |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
| 521 | + $sql='INSERT INTO '.$this->dbPrefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
| 522 | 522 | |
| 523 | 523 | if ($db_conn->query($sql) === false) { |
| 524 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
| 524 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
| 525 | 525 | } |
| 526 | 526 | } |
| 527 | 527 | } |
@@ -531,14 +531,14 @@ discard block |
||
| 531 | 531 | * @param $oid string oid in numeric |
| 532 | 532 | * @return mixed|boolean : PDO object or false |
| 533 | 533 | */ |
| 534 | - protected function getRules($ip,$oid) |
|
| 534 | + protected function getRules($ip, $oid) |
|
| 535 | 535 | { |
| 536 | 536 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 537 | 537 | // fetch rules based on IP in rule and OID |
| 538 | 538 | $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
| 539 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 539 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 540 | 540 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 541 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 541 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 542 | 542 | return false; |
| 543 | 543 | } |
| 544 | 544 | $rules_all=$ret_code->fetchAll(); |
@@ -547,7 +547,7 @@ discard block |
||
| 547 | 547 | $rule_ret_key=0; |
| 548 | 548 | foreach ($rules_all as $key => $rule) |
| 549 | 549 | { |
| 550 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
| 550 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
| 551 | 551 | { |
| 552 | 552 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
| 553 | 553 | //TODO : get host name by API (and check if correct in rule). |
@@ -555,7 +555,7 @@ discard block |
||
| 555 | 555 | continue; |
| 556 | 556 | } |
| 557 | 557 | // TODO : get hosts IP by API |
| 558 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
| 558 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
| 559 | 559 | { // get ips of group members by oid |
| 560 | 560 | $db_conn2=$this->trapsDB->db_connect_ido(); |
| 561 | 561 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -566,15 +566,15 @@ discard block |
||
| 566 | 566 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
| 567 | 567 | WHERE o.name1='".$rule['host_group_name']."';"; |
| 568 | 568 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
| 569 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 569 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
| 570 | 570 | continue; |
| 571 | 571 | } |
| 572 | 572 | $grouphosts=$ret_code2->fetchAll(); |
| 573 | 573 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
| 574 | - foreach ( $grouphosts as $host) |
|
| 574 | + foreach ($grouphosts as $host) |
|
| 575 | 575 | { |
| 576 | 576 | //echo $host['ip4']."\n"; |
| 577 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
| 577 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
| 578 | 578 | { |
| 579 | 579 | //echo "Rule added \n"; |
| 580 | 580 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -597,7 +597,7 @@ discard block |
||
| 597 | 597 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 598 | 598 | $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
| 599 | 599 | if ($db_conn->query($sql) === false) { |
| 600 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 600 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
| 601 | 601 | } |
| 602 | 602 | } |
| 603 | 603 | |
@@ -609,23 +609,23 @@ discard block |
||
| 609 | 609 | * @param string $display |
| 610 | 610 | * @returnn bool true is service check was sent without error |
| 611 | 611 | */ |
| 612 | - public function serviceCheckResult($host,$service,$state,$display) |
|
| 612 | + public function serviceCheckResult($host, $service, $state, $display) |
|
| 613 | 613 | { |
| 614 | 614 | if ($this->apiUse === false) |
| 615 | 615 | { |
| 616 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 617 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 618 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 616 | + $send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'. |
|
| 617 | + $host.';'.$service.';'.$state.';'.$display; |
|
| 618 | + $this->logging->log($send." : to : ".$this->icinga2cmd, INFO); |
|
| 619 | 619 | |
| 620 | 620 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 621 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 621 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
| 622 | 622 | return true; |
| 623 | 623 | } |
| 624 | 624 | else |
| 625 | 625 | { |
| 626 | 626 | // Get perfdata if found |
| 627 | 627 | $matches=array(); |
| 628 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 628 | + if (preg_match('/(.*)\|(.*)/', $display, $matches) == 1) |
|
| 629 | 629 | { |
| 630 | 630 | $display=$matches[1]; |
| 631 | 631 | $perfdata=$matches[2]; |
@@ -635,17 +635,17 @@ discard block |
||
| 635 | 635 | $perfdata=''; |
| 636 | 636 | } |
| 637 | 637 | |
| 638 | - $api = $this->getAPI(); |
|
| 638 | + $api=$this->getAPI(); |
|
| 639 | 639 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
| 640 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 640 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display, $perfdata); |
|
| 641 | 641 | if ($retcode == false) |
| 642 | 642 | { |
| 643 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 643 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
| 644 | 644 | return false; |
| 645 | 645 | } |
| 646 | 646 | else |
| 647 | 647 | { |
| 648 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 648 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
| 649 | 649 | return true; |
| 650 | 650 | } |
| 651 | 651 | } |
@@ -653,7 +653,7 @@ discard block |
||
| 653 | 653 | |
| 654 | 654 | public function getHostByIP($ip) |
| 655 | 655 | { |
| 656 | - $api = $this->getAPI(); |
|
| 656 | + $api=$this->getAPI(); |
|
| 657 | 657 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
| 658 | 658 | return $api->getHostByIP($ip); |
| 659 | 659 | } |
@@ -666,35 +666,35 @@ discard block |
||
| 666 | 666 | protected function applyDisplay($display) |
| 667 | 667 | { |
| 668 | 668 | $matches=array(); |
| 669 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
| 669 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $display, $matches) == 1) |
|
| 670 | 670 | { |
| 671 | 671 | $oid=$matches[1]; |
| 672 | 672 | $found=0; |
| 673 | 673 | // Test and transform regexp |
| 674 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
| 674 | + $oidR=$this->ruleClass->regexp_eval($oid); |
|
| 675 | 675 | |
| 676 | - foreach($this->trapDataExt as $val) |
|
| 676 | + foreach ($this->trapDataExt as $val) |
|
| 677 | 677 | { |
| 678 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 678 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
| 679 | 679 | { |
| 680 | - $val->value=preg_replace('/"/','',$val->value); |
|
| 680 | + $val->value=preg_replace('/"/', '', $val->value); |
|
| 681 | 681 | $rep=0; |
| 682 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
| 683 | - if ($rep==0) |
|
| 682 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
| 683 | + if ($rep == 0) |
|
| 684 | 684 | { |
| 685 | - $this->logging->log("Error in display",WARN,''); |
|
| 685 | + $this->logging->log("Error in display", WARN, ''); |
|
| 686 | 686 | return $display; |
| 687 | 687 | } |
| 688 | 688 | $found=1; |
| 689 | 689 | break; |
| 690 | 690 | } |
| 691 | 691 | } |
| 692 | - if ($found==0) |
|
| 692 | + if ($found == 0) |
|
| 693 | 693 | { |
| 694 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
| 695 | - if ($rep==0) |
|
| 694 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
| 695 | + if ($rep == 0) |
|
| 696 | 696 | { |
| 697 | - $this->logging->log("Error in display",WARN,''); |
|
| 697 | + $this->logging->log("Error in display", WARN, ''); |
|
| 698 | 698 | return $display; |
| 699 | 699 | } |
| 700 | 700 | } |
@@ -706,11 +706,11 @@ discard block |
||
| 706 | 706 | */ |
| 707 | 707 | public function applyRules() |
| 708 | 708 | { |
| 709 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
| 709 | + $rules=$this->getRules($this->trapData['source_ip'], $this->trapData['trap_oid']); |
|
| 710 | 710 | |
| 711 | - if ($rules===false || count($rules)==0) |
|
| 711 | + if ($rules === false || count($rules) == 0) |
|
| 712 | 712 | { |
| 713 | - $this->logging->log('No rules found for this trap',INFO ); |
|
| 713 | + $this->logging->log('No rules found for this trap', INFO); |
|
| 714 | 714 | $this->trapData['status']='unknown'; |
| 715 | 715 | $this->trapToDb=true; |
| 716 | 716 | return; |
@@ -725,59 +725,59 @@ discard block |
||
| 725 | 725 | $service_name=$rule['service_name']; |
| 726 | 726 | |
| 727 | 727 | $display=$this->applyDisplay($rule['display']); |
| 728 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
| 728 | + $this->trapAction=($this->trapAction == null) ? '' : $this->trapAction.', '; |
|
| 729 | 729 | try |
| 730 | 730 | { |
| 731 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
| 732 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
| 731 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
| 732 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt); |
|
| 733 | 733 | //->eval_rule($rule['rule']); |
| 734 | 734 | |
| 735 | 735 | if ($evalr == true) |
| 736 | 736 | { |
| 737 | 737 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
| 738 | 738 | $action=$rule['action_match']; |
| 739 | - $this->logging->log('action OK : '.$action,INFO ); |
|
| 739 | + $this->logging->log('action OK : '.$action, INFO); |
|
| 740 | 740 | if ($action >= 0) |
| 741 | 741 | { |
| 742 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
| 742 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
| 743 | 743 | { |
| 744 | 744 | $this->trapAction.='Error sending status : check cmd/API'; |
| 745 | 745 | } |
| 746 | 746 | else |
| 747 | 747 | { |
| 748 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 748 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 749 | 749 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 750 | 750 | } |
| 751 | 751 | } |
| 752 | 752 | else |
| 753 | 753 | { |
| 754 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 754 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 755 | 755 | } |
| 756 | - $this->trapToDb=($action==-2)?false:true; |
|
| 756 | + $this->trapToDb=($action == -2) ?false:true; |
|
| 757 | 757 | } |
| 758 | 758 | else |
| 759 | 759 | { |
| 760 | 760 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 761 | 761 | |
| 762 | 762 | $action=$rule['action_nomatch']; |
| 763 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
| 763 | + $this->logging->log('action NOK : '.$action, INFO); |
|
| 764 | 764 | if ($action >= 0) |
| 765 | 765 | { |
| 766 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 766 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
| 767 | 767 | { |
| 768 | 768 | $this->trapAction.='Error sending status : check cmd/API'; |
| 769 | 769 | } |
| 770 | 770 | else |
| 771 | 771 | { |
| 772 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 772 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 773 | 773 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 774 | 774 | } |
| 775 | 775 | } |
| 776 | 776 | else |
| 777 | 777 | { |
| 778 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 778 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
| 779 | 779 | } |
| 780 | - $this->trapToDb=($action==-2)?false:true; |
|
| 780 | + $this->trapToDb=($action == -2) ?false:true; |
|
| 781 | 781 | } |
| 782 | 782 | // Put name in source_name |
| 783 | 783 | if (!isset($this->trapData['source_name'])) |
@@ -786,7 +786,7 @@ discard block |
||
| 786 | 786 | } |
| 787 | 787 | else |
| 788 | 788 | { |
| 789 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
| 789 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trapData['source_name'])) |
|
| 790 | 790 | { // only add if not present |
| 791 | 791 | $this->trapData['source_name'].=','.$rule['host_name']; |
| 792 | 792 | } |
@@ -794,13 +794,13 @@ discard block |
||
| 794 | 794 | } |
| 795 | 795 | catch (Exception $e) |
| 796 | 796 | { |
| 797 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 797 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
| 798 | 798 | $this->trapAction.=' ERR : '.$e->getMessage(); |
| 799 | 799 | $this->trapData['status']='error'; |
| 800 | 800 | } |
| 801 | 801 | |
| 802 | 802 | } |
| 803 | - if ($this->trapData['status']=='error') |
|
| 803 | + if ($this->trapData['status'] == 'error') |
|
| 804 | 804 | { |
| 805 | 805 | $this->trapToDb=true; // Always put errors in DB for the use can see |
| 806 | 806 | } |
@@ -816,13 +816,13 @@ discard block |
||
| 816 | 816 | public function add_rule_final($time) |
| 817 | 817 | { |
| 818 | 818 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 819 | - if ($this->trapAction==null) |
|
| 819 | + if ($this->trapAction == null) |
|
| 820 | 820 | { |
| 821 | 821 | $this->trapAction='No action'; |
| 822 | 822 | } |
| 823 | 823 | $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
| 824 | 824 | if ($db_conn->query($sql) === false) { |
| 825 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
| 825 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
| 826 | 826 | } |
| 827 | 827 | } |
| 828 | 828 | |
@@ -845,7 +845,7 @@ discard block |
||
| 845 | 845 | WHERE s.current_state != 0;"; |
| 846 | 846 | $db_conn=$this->trapsDB->db_connect_ido(); |
| 847 | 847 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
| 848 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 848 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
| 849 | 849 | return 0; |
| 850 | 850 | } |
| 851 | 851 | $services=$services_db->fetchAll(); |
@@ -854,7 +854,7 @@ discard block |
||
| 854 | 854 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
| 855 | 855 | $db_conn2=$this->trapsDB->db_connect_trap(); |
| 856 | 856 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
| 857 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
| 857 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
| 858 | 858 | return 0; |
| 859 | 859 | } |
| 860 | 860 | $rules=$rules_db->fetchAll(); |
@@ -870,13 +870,13 @@ discard block |
||
| 870 | 870 | $service['host_name'] == $rule['host_name'] && |
| 871 | 871 | ($service['last_check'] + $rule['revert_ok']) < $now) |
| 872 | 872 | { |
| 873 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 873 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
| 874 | 874 | $numreset++; |
| 875 | 875 | } |
| 876 | 876 | } |
| 877 | 877 | } |
| 878 | 878 | echo "\n"; |
| 879 | - echo $numreset . " service(s) reset to OK\n"; |
|
| 879 | + echo $numreset." service(s) reset to OK\n"; |
|
| 880 | 880 | return 0; |
| 881 | 881 | |
| 882 | 882 | } |
@@ -95,8 +95,7 @@ discard block |
||
| 95 | 95 | { |
| 96 | 96 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
| 97 | 97 | $this->logSetup=true; |
| 98 | - } |
|
| 99 | - else |
|
| 98 | + } else |
|
| 100 | 99 | { |
| 101 | 100 | $this->logSetup=false; |
| 102 | 101 | } |
@@ -121,7 +120,10 @@ discard block |
||
| 121 | 120 | $this->getDatabaseOptions(); // Get options in database |
| 122 | 121 | |
| 123 | 122 | //*************** Setup API |
| 124 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
| 123 | + if ($this->apiUse === true) { |
|
| 124 | + $this->getAPI(); |
|
| 125 | + } |
|
| 126 | + // Setup API |
|
| 125 | 127 | |
| 126 | 128 | //*************** Setup MIB |
| 127 | 129 | $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
@@ -224,8 +226,7 @@ discard block |
||
| 224 | 226 | { |
| 225 | 227 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
| 226 | 228 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 227 | - } |
|
| 228 | - else |
|
| 229 | + } else |
|
| 229 | 230 | { |
| 230 | 231 | $this->trapData['source_ip']=$matches[1]; |
| 231 | 232 | $this->trapData['destination_ip']=$matches[3]; |
@@ -240,14 +241,12 @@ discard block |
||
| 240 | 241 | if ($ret_code===0 || $ret_code===false) |
| 241 | 242 | { |
| 242 | 243 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
| 243 | - } |
|
| 244 | - else |
|
| 244 | + } else |
|
| 245 | 245 | { |
| 246 | 246 | 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')) |
| 247 | 247 | { |
| 248 | 248 | $this->trapData['trap_oid']=$matches[2]; |
| 249 | - } |
|
| 250 | - else |
|
| 249 | + } else |
|
| 251 | 250 | { |
| 252 | 251 | $object= new stdClass; |
| 253 | 252 | $object->oid =$matches[1]; |
@@ -418,7 +417,9 @@ discard block |
||
| 418 | 417 | } |
| 419 | 418 | |
| 420 | 419 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 421 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 420 | + if ($inserted_id==false) { |
|
| 421 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 422 | + } |
|
| 422 | 423 | $this->trapId=$inserted_id; |
| 423 | 424 | break; |
| 424 | 425 | default: |
@@ -434,7 +435,9 @@ discard block |
||
| 434 | 435 | { |
| 435 | 436 | |
| 436 | 437 | // If action is ignore -> don't send t DB |
| 437 | - if ($this->trapToDb === false) return; |
|
| 438 | + if ($this->trapToDb === false) { |
|
| 439 | + return; |
|
| 440 | + } |
|
| 438 | 441 | |
| 439 | 442 | |
| 440 | 443 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -491,7 +494,9 @@ discard block |
||
| 491 | 494 | } |
| 492 | 495 | |
| 493 | 496 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 494 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 497 | + if ($inserted_id==false) { |
|
| 498 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 499 | + } |
|
| 495 | 500 | $this->trapId=$inserted_id; |
| 496 | 501 | break; |
| 497 | 502 | default: |
@@ -620,8 +625,7 @@ discard block |
||
| 620 | 625 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
| 621 | 626 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
| 622 | 627 | return true; |
| 623 | - } |
|
| 624 | - else |
|
| 628 | + } else |
|
| 625 | 629 | { |
| 626 | 630 | // Get perfdata if found |
| 627 | 631 | $matches=array(); |
@@ -629,8 +633,7 @@ discard block |
||
| 629 | 633 | { |
| 630 | 634 | $display=$matches[1]; |
| 631 | 635 | $perfdata=$matches[2]; |
| 632 | - } |
|
| 633 | - else |
|
| 636 | + } else |
|
| 634 | 637 | { |
| 635 | 638 | $perfdata=''; |
| 636 | 639 | } |
@@ -642,8 +645,7 @@ discard block |
||
| 642 | 645 | { |
| 643 | 646 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
| 644 | 647 | return false; |
| 645 | - } |
|
| 646 | - else |
|
| 648 | + } else |
|
| 647 | 649 | { |
| 648 | 650 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
| 649 | 651 | return true; |
@@ -742,20 +744,17 @@ discard block |
||
| 742 | 744 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 743 | 745 | { |
| 744 | 746 | $this->trapAction.='Error sending status : check cmd/API'; |
| 745 | - } |
|
| 746 | - else |
|
| 747 | + } else |
|
| 747 | 748 | { |
| 748 | 749 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 749 | 750 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 750 | 751 | } |
| 751 | - } |
|
| 752 | - else |
|
| 752 | + } else |
|
| 753 | 753 | { |
| 754 | 754 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 755 | 755 | } |
| 756 | 756 | $this->trapToDb=($action==-2)?false:true; |
| 757 | - } |
|
| 758 | - else |
|
| 757 | + } else |
|
| 759 | 758 | { |
| 760 | 759 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
| 761 | 760 | |
@@ -766,14 +765,12 @@ discard block |
||
| 766 | 765 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
| 767 | 766 | { |
| 768 | 767 | $this->trapAction.='Error sending status : check cmd/API'; |
| 769 | - } |
|
| 770 | - else |
|
| 768 | + } else |
|
| 771 | 769 | { |
| 772 | 770 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 773 | 771 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
| 774 | 772 | } |
| 775 | - } |
|
| 776 | - else |
|
| 773 | + } else |
|
| 777 | 774 | { |
| 778 | 775 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
| 779 | 776 | } |
@@ -783,16 +780,14 @@ discard block |
||
| 783 | 780 | if (!isset($this->trapData['source_name'])) |
| 784 | 781 | { |
| 785 | 782 | $this->trapData['source_name']=$rule['host_name']; |
| 786 | - } |
|
| 787 | - else |
|
| 783 | + } else |
|
| 788 | 784 | { |
| 789 | 785 | if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
| 790 | 786 | { // only add if not present |
| 791 | 787 | $this->trapData['source_name'].=','.$rule['host_name']; |
| 792 | 788 | } |
| 793 | 789 | } |
| 794 | - } |
|
| 795 | - catch (Exception $e) |
|
| 790 | + } catch (Exception $e) |
|
| 796 | 791 | { |
| 797 | 792 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
| 798 | 793 | $this->trapAction.=' ERR : '.$e->getMessage(); |
@@ -803,8 +798,7 @@ discard block |
||
| 803 | 798 | if ($this->trapData['status']=='error') |
| 804 | 799 | { |
| 805 | 800 | $this->trapToDb=true; // Always put errors in DB for the use can see |
| 806 | - } |
|
| 807 | - else |
|
| 801 | + } else |
|
| 808 | 802 | { |
| 809 | 803 | $this->trapData['status']='done'; |
| 810 | 804 | } |
@@ -17,13 +17,13 @@ discard block |
||
| 17 | 17 | try |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - $trap = new Trap($icingaweb2Etc); |
|
| 21 | - //$Trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
|
| 22 | - //$Trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
|
| 23 | - //$Trap->setLogging(4,'syslog'); |
|
| 20 | + $trap = new Trap($icingaweb2Etc); |
|
| 21 | + //$Trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
|
| 22 | + //$Trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
|
| 23 | + //$Trap->setLogging(4,'syslog'); |
|
| 24 | 24 | |
| 25 | - // TODO : tranfer this to reset_trap cli command |
|
| 26 | - $trap->eraseOldTraps(); |
|
| 25 | + // TODO : tranfer this to reset_trap cli command |
|
| 26 | + $trap->eraseOldTraps(); |
|
| 27 | 27 | |
| 28 | 28 | $trap->read_trap('php://stdin'); |
| 29 | 29 | |
@@ -36,15 +36,15 @@ discard block |
||
| 36 | 36 | } |
| 37 | 37 | catch (Exception $e) |
| 38 | 38 | { |
| 39 | - if ($trap == null) |
|
| 40 | - { // Exception in trap creation : log in display & syslog |
|
| 41 | - $logging = new Logging(); |
|
| 42 | - $logging->log("Caught exception creating Trap class",2); |
|
| 43 | - } |
|
| 44 | - else |
|
| 45 | - { |
|
| 39 | + if ($trap == null) |
|
| 40 | + { // Exception in trap creation : log in display & syslog |
|
| 41 | + $logging = new Logging(); |
|
| 42 | + $logging->log("Caught exception creating Trap class",2); |
|
| 43 | + } |
|
| 44 | + else |
|
| 45 | + { |
|
| 46 | 46 | $trap->logging->log("Exception : ". $e->getMessage(),2,0); |
| 47 | - } |
|
| 47 | + } |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | //end |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | use Trapdirector\Trap; |
| 5 | 5 | |
| 6 | 6 | // start |
| 7 | -$time1 = microtime(true); |
|
| 7 | +$time1=microtime(true); |
|
| 8 | 8 | |
| 9 | 9 | require_once ('trap_class.php'); |
| 10 | 10 | |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | try |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - $trap = new Trap($icingaweb2Etc); |
|
| 20 | + $trap=new Trap($icingaweb2Etc); |
|
| 21 | 21 | //$Trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
| 22 | 22 | //$Trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
| 23 | 23 | //$Trap->setLogging(4,'syslog'); |
@@ -38,12 +38,12 @@ discard block |
||
| 38 | 38 | { |
| 39 | 39 | if ($trap == null) |
| 40 | 40 | { // Exception in trap creation : log in display & syslog |
| 41 | - $logging = new Logging(); |
|
| 42 | - $logging->log("Caught exception creating Trap class",2); |
|
| 41 | + $logging=new Logging(); |
|
| 42 | + $logging->log("Caught exception creating Trap class", 2); |
|
| 43 | 43 | } |
| 44 | 44 | else |
| 45 | 45 | { |
| 46 | - $trap->logging->log("Exception : ". $e->getMessage(),2,0); |
|
| 46 | + $trap->logging->log("Exception : ".$e->getMessage(), 2, 0); |
|
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | |
@@ -33,15 +33,13 @@ |
||
| 33 | 33 | |
| 34 | 34 | $trap->add_rule_final(microtime(true) - $time1); |
| 35 | 35 | |
| 36 | -} |
|
| 37 | -catch (Exception $e) |
|
| 36 | +} catch (Exception $e) |
|
| 38 | 37 | { |
| 39 | 38 | if ($trap == null) |
| 40 | 39 | { // Exception in trap creation : log in display & syslog |
| 41 | 40 | $logging = new Logging(); |
| 42 | 41 | $logging->log("Caught exception creating Trap class",2); |
| 43 | - } |
|
| 44 | - else |
|
| 42 | + } else |
|
| 45 | 43 | { |
| 46 | 44 | $trap->logging->log("Exception : ". $e->getMessage(),2,0); |
| 47 | 45 | } |
@@ -22,77 +22,77 @@ discard block |
||
| 22 | 22 | class MibCommand extends Command |
| 23 | 23 | { |
| 24 | 24 | /** |
| 25 | - * Update mib database |
|
| 26 | - * |
|
| 27 | - * USAGE |
|
| 28 | - * |
|
| 29 | - * icingli trapdirector mib update |
|
| 30 | - * |
|
| 31 | - * OPTIONS |
|
| 32 | - * |
|
| 33 | - * --pid <file> : run in background with pid in <file> |
|
| 34 | - * |
|
| 35 | - * --verb : Set output log to verbose |
|
| 36 | - * |
|
| 37 | - * --force-check : force check of all traps & objects for change. (NOT IMPLEMENTED) |
|
| 38 | - */ |
|
| 25 | + * Update mib database |
|
| 26 | + * |
|
| 27 | + * USAGE |
|
| 28 | + * |
|
| 29 | + * icingli trapdirector mib update |
|
| 30 | + * |
|
| 31 | + * OPTIONS |
|
| 32 | + * |
|
| 33 | + * --pid <file> : run in background with pid in <file> |
|
| 34 | + * |
|
| 35 | + * --verb : Set output log to verbose |
|
| 36 | + * |
|
| 37 | + * --force-check : force check of all traps & objects for change. (NOT IMPLEMENTED) |
|
| 38 | + */ |
|
| 39 | 39 | public function updateAction() |
| 40 | 40 | { |
| 41 | - $background = $this->params->get('pid', null); |
|
| 42 | - $logLevel= $this->params->has('verb') ? 4 : 2; |
|
| 43 | - if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
| 44 | - $forceCheck=$this->params->has('force-check')?True:False; |
|
| 45 | - $pid=1; |
|
| 46 | - if ($background != null) |
|
| 47 | - { |
|
| 48 | - $file=@fopen($background,'w'); |
|
| 49 | - if ($file == false) |
|
| 50 | - { |
|
| 51 | - echo 'Error : cannot open pid file '.$background; |
|
| 52 | - return 1; |
|
| 53 | - } |
|
| 54 | - $pid = pcntl_fork(); |
|
| 55 | - if ($pid == -1) { |
|
| 56 | - echo 'Error : Cannot fork process'; |
|
| 57 | - return 1; |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
|
| 41 | + $background = $this->params->get('pid', null); |
|
| 42 | + $logLevel= $this->params->has('verb') ? 4 : 2; |
|
| 43 | + if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
| 44 | + $forceCheck=$this->params->has('force-check')?True:False; |
|
| 45 | + $pid=1; |
|
| 46 | + if ($background != null) |
|
| 47 | + { |
|
| 48 | + $file=@fopen($background,'w'); |
|
| 49 | + if ($file == false) |
|
| 50 | + { |
|
| 51 | + echo 'Error : cannot open pid file '.$background; |
|
| 52 | + return 1; |
|
| 53 | + } |
|
| 54 | + $pid = pcntl_fork(); |
|
| 55 | + if ($pid == -1) { |
|
| 56 | + echo 'Error : Cannot fork process'; |
|
| 57 | + return 1; |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
|
| 61 | 61 | require_once($module->getBaseDir() .'/bin/trap_class.php'); |
| 62 | 62 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 63 | 63 | $trap = new Trap($icingaweb2_etc); |
| 64 | 64 | if ($pid == 1) |
| 65 | 65 | { |
| 66 | - $trap->setLogging($logLevel,'display'); |
|
| 66 | + $trap->setLogging($logLevel,'display'); |
|
| 67 | 67 | } |
| 68 | 68 | else |
| 69 | 69 | { // use default display TODO : if default is 'display' son process will be killed at first output.... |
| 70 | - if ($pid != 0) |
|
| 71 | - { |
|
| 72 | - // father process |
|
| 73 | - fwrite($file,$pid); |
|
| 74 | - fclose($file); |
|
| 75 | - echo "OK : process $pid in bckground"; |
|
| 76 | - return 0; |
|
| 77 | - } |
|
| 78 | - else |
|
| 79 | - { // son process : close all file descriptors and go to a new session |
|
| 80 | - fclose($file); |
|
| 70 | + if ($pid != 0) |
|
| 71 | + { |
|
| 72 | + // father process |
|
| 73 | + fwrite($file,$pid); |
|
| 74 | + fclose($file); |
|
| 75 | + echo "OK : process $pid in bckground"; |
|
| 76 | + return 0; |
|
| 77 | + } |
|
| 78 | + else |
|
| 79 | + { // son process : close all file descriptors and go to a new session |
|
| 80 | + fclose($file); |
|
| 81 | 81 | // $sid = posix_setsid(); |
| 82 | - fclose(STDIN); |
|
| 83 | - fclose(STDOUT); |
|
| 84 | - fclose(STDERR); |
|
| 85 | - try |
|
| 86 | - { |
|
| 87 | - $trap->mibClass->update_mib_database(false,$forceCheck); |
|
| 88 | - } |
|
| 89 | - catch (Exception $e) |
|
| 90 | - { |
|
| 91 | - $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
|
| 92 | - } |
|
| 93 | - unlink($background); |
|
| 94 | - return 0; |
|
| 95 | - } |
|
| 82 | + fclose(STDIN); |
|
| 83 | + fclose(STDOUT); |
|
| 84 | + fclose(STDERR); |
|
| 85 | + try |
|
| 86 | + { |
|
| 87 | + $trap->mibClass->update_mib_database(false,$forceCheck); |
|
| 88 | + } |
|
| 89 | + catch (Exception $e) |
|
| 90 | + { |
|
| 91 | + $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
|
| 92 | + } |
|
| 93 | + unlink($background); |
|
| 94 | + return 0; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | 97 | } |
| 98 | 98 | |
@@ -110,28 +110,28 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | if ($pid != 1) |
| 112 | 112 | { |
| 113 | - unlink($background); |
|
| 113 | + unlink($background); |
|
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | 116 | /** |
| 117 | - * purge all mib database NOT IMPLEMENTED |
|
| 118 | - * |
|
| 119 | - * USAGE |
|
| 120 | - * |
|
| 121 | - * icingli trapdirector mib purge --confirm |
|
| 122 | - * |
|
| 123 | - * OPTIONS |
|
| 124 | - * |
|
| 125 | - * --confirm : needed to execute purge |
|
| 126 | - */ |
|
| 117 | + * purge all mib database NOT IMPLEMENTED |
|
| 118 | + * |
|
| 119 | + * USAGE |
|
| 120 | + * |
|
| 121 | + * icingli trapdirector mib purge --confirm |
|
| 122 | + * |
|
| 123 | + * OPTIONS |
|
| 124 | + * |
|
| 125 | + * --confirm : needed to execute purge |
|
| 126 | + */ |
|
| 127 | 127 | public function purgeAction() |
| 128 | 128 | { |
| 129 | 129 | $db_prefix=$this->Config()->get('config', 'database_prefix'); |
| 130 | 130 | |
| 131 | 131 | if (!$this->params->has('confirm')) |
| 132 | 132 | { |
| 133 | - echo "This needs confirmation with '--confirm'\n"; |
|
| 134 | - return; |
|
| 133 | + echo "This needs confirmation with '--confirm'\n"; |
|
| 134 | + return; |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | $Config = new TrapModuleConfig($db_prefix); |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | $db = IcingaDbConnection::fromResourceName($dbresource)->getConnection(); |
| 145 | 145 | |
| 146 | 146 | $query = $db->delete( |
| 147 | - $Config->getMIBCacheTableName(), |
|
| 148 | - 'id>0'); |
|
| 149 | - echo 'Deleted '. $query . " traps and objects\n"; |
|
| 147 | + $Config->getMIBCacheTableName(), |
|
| 148 | + 'id>0'); |
|
| 149 | + echo 'Deleted '. $query . " traps and objects\n"; |
|
| 150 | 150 | } |
| 151 | 151 | catch (Exception $e) |
| 152 | 152 | { |
@@ -38,39 +38,39 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function updateAction() |
| 40 | 40 | { |
| 41 | - $background = $this->params->get('pid', null); |
|
| 42 | - $logLevel= $this->params->has('verb') ? 4 : 2; |
|
| 43 | - if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
| 44 | - $forceCheck=$this->params->has('force-check')?True:False; |
|
| 41 | + $background=$this->params->get('pid', null); |
|
| 42 | + $logLevel=$this->params->has('verb') ? 4 : 2; |
|
| 43 | + if ($this->params->has('force-check')) { echo "Not implemented"; return; } |
|
| 44 | + $forceCheck=$this->params->has('force-check') ?True:False; |
|
| 45 | 45 | $pid=1; |
| 46 | 46 | if ($background != null) |
| 47 | 47 | { |
| 48 | - $file=@fopen($background,'w'); |
|
| 48 | + $file=@fopen($background, 'w'); |
|
| 49 | 49 | if ($file == false) |
| 50 | 50 | { |
| 51 | 51 | echo 'Error : cannot open pid file '.$background; |
| 52 | 52 | return 1; |
| 53 | 53 | } |
| 54 | - $pid = pcntl_fork(); |
|
| 54 | + $pid=pcntl_fork(); |
|
| 55 | 55 | if ($pid == -1) { |
| 56 | 56 | echo 'Error : Cannot fork process'; |
| 57 | 57 | return 1; |
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
| 61 | - require_once($module->getBaseDir() .'/bin/trap_class.php'); |
|
| 61 | + require_once($module->getBaseDir().'/bin/trap_class.php'); |
|
| 62 | 62 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 63 | - $trap = new Trap($icingaweb2_etc); |
|
| 63 | + $trap=new Trap($icingaweb2_etc); |
|
| 64 | 64 | if ($pid == 1) |
| 65 | 65 | { |
| 66 | - $trap->setLogging($logLevel,'display'); |
|
| 66 | + $trap->setLogging($logLevel, 'display'); |
|
| 67 | 67 | } |
| 68 | 68 | else |
| 69 | 69 | { // use default display TODO : if default is 'display' son process will be killed at first output.... |
| 70 | 70 | if ($pid != 0) |
| 71 | 71 | { |
| 72 | 72 | // father process |
| 73 | - fwrite($file,$pid); |
|
| 73 | + fwrite($file, $pid); |
|
| 74 | 74 | fclose($file); |
| 75 | 75 | echo "OK : process $pid in bckground"; |
| 76 | 76 | return 0; |
@@ -84,11 +84,11 @@ discard block |
||
| 84 | 84 | fclose(STDERR); |
| 85 | 85 | try |
| 86 | 86 | { |
| 87 | - $trap->mibClass->update_mib_database(false,$forceCheck); |
|
| 87 | + $trap->mibClass->update_mib_database(false, $forceCheck); |
|
| 88 | 88 | } |
| 89 | 89 | catch (Exception $e) |
| 90 | 90 | { |
| 91 | - $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
|
| 91 | + $trap->logging->log('Error in updating : '.$e->getMessage(), 2); |
|
| 92 | 92 | } |
| 93 | 93 | unlink($background); |
| 94 | 94 | return 0; |
@@ -100,13 +100,13 @@ discard block |
||
| 100 | 100 | { |
| 101 | 101 | echo "Update main mib database : \n"; |
| 102 | 102 | echo "# (trap found) C (trap already processed) . (every 2 seconds) : \n"; |
| 103 | - $trap->mibClass->update_mib_database(true,$forceCheck); |
|
| 103 | + $trap->mibClass->update_mib_database(true, $forceCheck); |
|
| 104 | 104 | echo "Done\n"; |
| 105 | 105 | |
| 106 | 106 | } |
| 107 | 107 | catch (Exception $e) |
| 108 | 108 | { |
| 109 | - echo 'Error in updating : ' . $e->getMessage(); |
|
| 109 | + echo 'Error in updating : '.$e->getMessage(); |
|
| 110 | 110 | } |
| 111 | 111 | if ($pid != 1) |
| 112 | 112 | { |
@@ -134,23 +134,23 @@ discard block |
||
| 134 | 134 | return; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - $Config = new TrapModuleConfig($db_prefix); |
|
| 137 | + $Config=new TrapModuleConfig($db_prefix); |
|
| 138 | 138 | |
| 139 | 139 | try |
| 140 | 140 | { |
| 141 | 141 | |
| 142 | 142 | $dbresource=$this->Config()->get('config', 'database'); |
| 143 | 143 | echo "DB name : $dbresource\n"; |
| 144 | - $db = IcingaDbConnection::fromResourceName($dbresource)->getConnection(); |
|
| 144 | + $db=IcingaDbConnection::fromResourceName($dbresource)->getConnection(); |
|
| 145 | 145 | |
| 146 | - $query = $db->delete( |
|
| 146 | + $query=$db->delete( |
|
| 147 | 147 | $Config->getMIBCacheTableName(), |
| 148 | 148 | 'id>0'); |
| 149 | - echo 'Deleted '. $query . " traps and objects\n"; |
|
| 149 | + echo 'Deleted '.$query." traps and objects\n"; |
|
| 150 | 150 | } |
| 151 | 151 | catch (Exception $e) |
| 152 | 152 | { |
| 153 | - echo 'Error in DB : ' . $e->getMessage(); |
|
| 153 | + echo 'Error in DB : '.$e->getMessage(); |
|
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
@@ -64,8 +64,7 @@ discard block |
||
| 64 | 64 | if ($pid == 1) |
| 65 | 65 | { |
| 66 | 66 | $trap->setLogging($logLevel,'display'); |
| 67 | - } |
|
| 68 | - else |
|
| 67 | + } else |
|
| 69 | 68 | { // use default display TODO : if default is 'display' son process will be killed at first output.... |
| 70 | 69 | if ($pid != 0) |
| 71 | 70 | { |
@@ -74,8 +73,7 @@ discard block |
||
| 74 | 73 | fclose($file); |
| 75 | 74 | echo "OK : process $pid in bckground"; |
| 76 | 75 | return 0; |
| 77 | - } |
|
| 78 | - else |
|
| 76 | + } else |
|
| 79 | 77 | { // son process : close all file descriptors and go to a new session |
| 80 | 78 | fclose($file); |
| 81 | 79 | // $sid = posix_setsid(); |
@@ -85,8 +83,7 @@ discard block |
||
| 85 | 83 | try |
| 86 | 84 | { |
| 87 | 85 | $trap->mibClass->update_mib_database(false,$forceCheck); |
| 88 | - } |
|
| 89 | - catch (Exception $e) |
|
| 86 | + } catch (Exception $e) |
|
| 90 | 87 | { |
| 91 | 88 | $trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
| 92 | 89 | } |
@@ -103,8 +100,7 @@ discard block |
||
| 103 | 100 | $trap->mibClass->update_mib_database(true,$forceCheck); |
| 104 | 101 | echo "Done\n"; |
| 105 | 102 | |
| 106 | - } |
|
| 107 | - catch (Exception $e) |
|
| 103 | + } catch (Exception $e) |
|
| 108 | 104 | { |
| 109 | 105 | echo 'Error in updating : ' . $e->getMessage(); |
| 110 | 106 | } |
@@ -147,8 +143,7 @@ discard block |
||
| 147 | 143 | $Config->getMIBCacheTableName(), |
| 148 | 144 | 'id>0'); |
| 149 | 145 | echo 'Deleted '. $query . " traps and objects\n"; |
| 150 | - } |
|
| 151 | - catch (Exception $e) |
|
| 146 | + } catch (Exception $e) |
|
| 152 | 147 | { |
| 153 | 148 | echo 'Error in DB : ' . $e->getMessage(); |
| 154 | 149 | } |
@@ -52,8 +52,7 @@ discard block |
||
| 52 | 52 | $this->Config()->saveIni(); |
| 53 | 53 | $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
| 54 | 54 | //$emptyConfig=1; |
| 55 | - } |
|
| 56 | - catch (Exception $e) |
|
| 55 | + } catch (Exception $e) |
|
| 57 | 56 | { |
| 58 | 57 | $this->view->configErrorDetected=$e->getMessage(); |
| 59 | 58 | } |
@@ -141,8 +140,7 @@ discard block |
||
| 141 | 140 | $this->view->apimessage='API config : ' . $e->getMessage(); |
| 142 | 141 | $this->view->apimessageError=true; |
| 143 | 142 | } |
| 144 | - } |
|
| 145 | - else |
|
| 143 | + } else |
|
| 146 | 144 | { |
| 147 | 145 | $this->view->apimessage='API parameters not configured'; |
| 148 | 146 | $this->view->apimessageError=true; |
@@ -208,7 +206,9 @@ discard block |
||
| 208 | 206 | $input="154865134987aaaa"; |
| 209 | 207 | exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
| 210 | 208 | |
| 211 | - if (! isset($output[0])) $output[0]="NO OUT"; |
|
| 209 | + if (! isset($output[0])) { |
|
| 210 | + $output[0]="NO OUT"; |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | 213 | if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
| 214 | 214 | { |
@@ -310,8 +310,7 @@ discard block |
||
| 310 | 310 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
| 311 | 311 | printf('Schema already exists'); |
| 312 | 312 | |
| 313 | - } |
|
| 314 | - catch (DBException $e) |
|
| 313 | + } catch (DBException $e) |
|
| 315 | 314 | { |
| 316 | 315 | |
| 317 | 316 | printf('Creating schema : <br>'); |
@@ -367,8 +366,7 @@ discard block |
||
| 367 | 366 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
| 368 | 367 | echo 'Schema already exists and is up to date<br>'; |
| 369 | 368 | return; |
| 370 | - } |
|
| 371 | - catch (DBException $e) |
|
| 369 | + } catch (DBException $e) |
|
| 372 | 370 | { |
| 373 | 371 | $dberror=$e->getArray(); |
| 374 | 372 | } |
@@ -435,12 +433,15 @@ discard block |
||
| 435 | 433 | } |
| 436 | 434 | // Assume there is only one line... TODO : see if there is a better way to do this |
| 437 | 435 | $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
| 438 | - if (!preg_match('/-n/',$line)) |
|
| 439 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
| 440 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
| 441 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
| 442 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
| 443 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 436 | + if (!preg_match('/-n/',$line)) { |
|
| 437 | + return array(1,'snmptrapd has no -n option : '.$line); |
|
| 438 | + } |
|
| 439 | + if (!preg_match('/-O[^ ]*n/',$line)) { |
|
| 440 | + return array(1,'snmptrapd has no -On option : '.$line); |
|
| 441 | + } |
|
| 442 | + if (!preg_match('/-O[^ ]*e/',$line)) { |
|
| 443 | + return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 444 | + } |
|
| 444 | 445 | |
| 445 | 446 | return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
| 446 | 447 | } |
@@ -25,16 +25,16 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | private function get_param() |
| 27 | 27 | { |
| 28 | - $dberrorMsg=$this->params->get('dberror'); |
|
| 29 | - if ($dberrorMsg != '') |
|
| 30 | - { |
|
| 31 | - $this->view->errorDetected=$dberrorMsg; |
|
| 32 | - } |
|
| 33 | - $dberrorMsg=$this->params->get('idodberror'); |
|
| 34 | - if ($dberrorMsg != '') |
|
| 35 | - { |
|
| 36 | - $this->view->errorDetected=$dberrorMsg; |
|
| 37 | - } |
|
| 28 | + $dberrorMsg=$this->params->get('dberror'); |
|
| 29 | + if ($dberrorMsg != '') |
|
| 30 | + { |
|
| 31 | + $this->view->errorDetected=$dberrorMsg; |
|
| 32 | + } |
|
| 33 | + $dberrorMsg=$this->params->get('idodberror'); |
|
| 34 | + if ($dberrorMsg != '') |
|
| 35 | + { |
|
| 36 | + $this->view->errorDetected=$dberrorMsg; |
|
| 37 | + } |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -43,22 +43,22 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | private function check_empty_config() |
| 45 | 45 | { |
| 46 | - $this->view->configErrorDetected == NULL; // Displayed error on various conifugration errors. |
|
| 47 | - if ($this->Config()->isEmpty() == true) |
|
| 48 | - { |
|
| 49 | - $this->Config()->setSection('config'); // Set base config section. |
|
| 50 | - try |
|
| 51 | - { |
|
| 52 | - $this->Config()->saveIni(); |
|
| 53 | - $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
|
| 54 | - //$emptyConfig=1; |
|
| 55 | - } |
|
| 56 | - catch (Exception $e) |
|
| 57 | - { |
|
| 58 | - $this->view->configErrorDetected=$e->getMessage(); |
|
| 59 | - } |
|
| 46 | + $this->view->configErrorDetected == NULL; // Displayed error on various conifugration errors. |
|
| 47 | + if ($this->Config()->isEmpty() == true) |
|
| 48 | + { |
|
| 49 | + $this->Config()->setSection('config'); // Set base config section. |
|
| 50 | + try |
|
| 51 | + { |
|
| 52 | + $this->Config()->saveIni(); |
|
| 53 | + $this->view->configErrorDetected='Configuration is empty : you can run install script with parameters (see Automatic installation below)'; |
|
| 54 | + //$emptyConfig=1; |
|
| 55 | + } |
|
| 56 | + catch (Exception $e) |
|
| 57 | + { |
|
| 58 | + $this->view->configErrorDetected=$e->getMessage(); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - } |
|
| 61 | + } |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -71,56 +71,56 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | private function check_db() |
| 73 | 73 | { |
| 74 | - $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
|
| 75 | - 0 => array('Database configuration OK','',''), |
|
| 76 | - 1 => array('Database set in config.ini','No database in config.ini',''), |
|
| 77 | - 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
|
| 78 | - Url::fromPath('config/resource')), |
|
| 79 | - 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
|
| 80 | - Url::fromPath('trapdirector/settings/createschema')), |
|
| 81 | - 4 => array('Schema is set','Schema is not set for ', |
|
| 82 | - Url::fromPath('trapdirector/settings/createschema')), |
|
| 83 | - 5 => array('Schema is up to date','Schema is outdated :', |
|
| 84 | - Url::fromPath('trapdirector/settings/updateschema')), |
|
| 85 | - ); |
|
| 74 | + $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
|
| 75 | + 0 => array('Database configuration OK','',''), |
|
| 76 | + 1 => array('Database set in config.ini','No database in config.ini',''), |
|
| 77 | + 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
|
| 78 | + Url::fromPath('config/resource')), |
|
| 79 | + 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
|
| 80 | + Url::fromPath('trapdirector/settings/createschema')), |
|
| 81 | + 4 => array('Schema is set','Schema is not set for ', |
|
| 82 | + Url::fromPath('trapdirector/settings/createschema')), |
|
| 83 | + 5 => array('Schema is up to date','Schema is outdated :', |
|
| 84 | + Url::fromPath('trapdirector/settings/updateschema')), |
|
| 85 | + ); |
|
| 86 | 86 | |
| 87 | - try { |
|
| 88 | - $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
| 89 | - $dberror=array(0,''); |
|
| 90 | - } catch (DBException $e) { |
|
| 91 | - $dberror = $e->getArray(); |
|
| 92 | - } |
|
| 87 | + try { |
|
| 88 | + $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
| 89 | + $dberror=array(0,''); |
|
| 90 | + } catch (DBException $e) { |
|
| 91 | + $dberror = $e->getArray(); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - $this->view->db_error=$dberror[0]; |
|
| 95 | - switch ($dberror[0]) |
|
| 96 | - { |
|
| 97 | - case 2: |
|
| 98 | - case 4: |
|
| 99 | - $db_message[$dberror[0]][1] .= $dberror[1]; |
|
| 100 | - break; |
|
| 101 | - case 3: |
|
| 102 | - $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
|
| 103 | - break; |
|
| 104 | - case 5: |
|
| 105 | - $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
|
| 106 | - break; |
|
| 107 | - case 0: |
|
| 108 | - case 1: |
|
| 109 | - break; |
|
| 110 | - default: |
|
| 111 | - new ProgrammingError('Out of bond result from database test'); |
|
| 112 | - } |
|
| 113 | - $this->view->message=$db_message; |
|
| 94 | + $this->view->db_error=$dberror[0]; |
|
| 95 | + switch ($dberror[0]) |
|
| 96 | + { |
|
| 97 | + case 2: |
|
| 98 | + case 4: |
|
| 99 | + $db_message[$dberror[0]][1] .= $dberror[1]; |
|
| 100 | + break; |
|
| 101 | + case 3: |
|
| 102 | + $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
|
| 103 | + break; |
|
| 104 | + case 5: |
|
| 105 | + $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
|
| 106 | + break; |
|
| 107 | + case 0: |
|
| 108 | + case 1: |
|
| 109 | + break; |
|
| 110 | + default: |
|
| 111 | + new ProgrammingError('Out of bond result from database test'); |
|
| 112 | + } |
|
| 113 | + $this->view->message=$db_message; |
|
| 114 | 114 | |
| 115 | - try { |
|
| 116 | - $this->getUIDatabase()->testGetIdoDb(); // Get DB in test mode |
|
| 117 | - $dberror=array(0,''); |
|
| 118 | - } catch (DBException $e) { |
|
| 119 | - $dberror = $e->getArray(); |
|
| 120 | - } |
|
| 115 | + try { |
|
| 116 | + $this->getUIDatabase()->testGetIdoDb(); // Get DB in test mode |
|
| 117 | + $dberror=array(0,''); |
|
| 118 | + } catch (DBException $e) { |
|
| 119 | + $dberror = $e->getArray(); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - $this->view->ido_db_error=$dberror[0]; |
|
| 123 | - $this->view->ido_message='IDO Database : ' . $dberror[1]; |
|
| 122 | + $this->view->ido_db_error=$dberror[0]; |
|
| 123 | + $this->view->ido_message='IDO Database : ' . $dberror[1]; |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
@@ -130,23 +130,23 @@ discard block |
||
| 130 | 130 | */ |
| 131 | 131 | private function check_api() |
| 132 | 132 | { |
| 133 | - if ($this->Config()->get('config', 'icingaAPI_host') != '') |
|
| 134 | - { |
|
| 135 | - $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
| 136 | - $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
|
| 137 | - try { |
|
| 138 | - list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 139 | - //$this->view->apimessageError=false; |
|
| 140 | - } catch (RuntimeException $e) { |
|
| 141 | - $this->view->apimessage='API config : ' . $e->getMessage(); |
|
| 142 | - $this->view->apimessageError=true; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - else |
|
| 146 | - { |
|
| 147 | - $this->view->apimessage='API parameters not configured'; |
|
| 148 | - $this->view->apimessageError=true; |
|
| 149 | - } |
|
| 133 | + if ($this->Config()->get('config', 'icingaAPI_host') != '') |
|
| 134 | + { |
|
| 135 | + $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
| 136 | + $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
|
| 137 | + try { |
|
| 138 | + list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 139 | + //$this->view->apimessageError=false; |
|
| 140 | + } catch (RuntimeException $e) { |
|
| 141 | + $this->view->apimessage='API config : ' . $e->getMessage(); |
|
| 142 | + $this->view->apimessageError=true; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + else |
|
| 146 | + { |
|
| 147 | + $this->view->apimessage='API parameters not configured'; |
|
| 148 | + $this->view->apimessageError=true; |
|
| 149 | + } |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | /** |
@@ -157,21 +157,21 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | private function check_icingaweb_path() |
| 159 | 159 | { |
| 160 | - $this->view->icingaEtcWarn=0; |
|
| 161 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 162 | - if ($icingaweb2_etc != "/etc/icingaweb2/" && $icingaweb2_etc != '') |
|
| 163 | - { |
|
| 164 | - $output=array(); |
|
| 160 | + $this->view->icingaEtcWarn=0; |
|
| 161 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
| 162 | + if ($icingaweb2_etc != "/etc/icingaweb2/" && $icingaweb2_etc != '') |
|
| 163 | + { |
|
| 164 | + $output=array(); |
|
| 165 | 165 | |
| 166 | - exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2Etc=" ',$output); |
|
| 166 | + exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2Etc=" ',$output); |
|
| 167 | 167 | |
| 168 | 168 | |
| 169 | - if (! isset($output[0]) || ! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
| 170 | - { |
|
| 171 | - $this->view->icingaEtcWarn=1; |
|
| 172 | - $this->view->icingaweb2_etc=$icingaweb2_etc; |
|
| 173 | - } |
|
| 174 | - } |
|
| 169 | + if (! isset($output[0]) || ! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
| 170 | + { |
|
| 171 | + $this->view->icingaEtcWarn=1; |
|
| 172 | + $this->view->icingaweb2_etc=$icingaweb2_etc; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | } |
| 177 | 177 | |
@@ -182,15 +182,15 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | private function get_db_list($allowed) |
| 184 | 184 | { |
| 185 | - $resources = array(); |
|
| 186 | - foreach (ResourceFactory::getResourceConfigs() as $name => $resource) |
|
| 187 | - { |
|
| 188 | - if ($resource->get('type') === 'db' && in_array($resource->get('db'), $allowed)) |
|
| 189 | - { |
|
| 190 | - $resources[$name] = $name; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - return $resources; |
|
| 185 | + $resources = array(); |
|
| 186 | + foreach (ResourceFactory::getResourceConfigs() as $name => $resource) |
|
| 187 | + { |
|
| 188 | + if ($resource->get('type') === 'db' && in_array($resource->get('db'), $allowed)) |
|
| 189 | + { |
|
| 190 | + $resources[$name] = $name; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + return $resources; |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /** |
@@ -199,23 +199,23 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | private function get_php_binary() |
| 201 | 201 | { |
| 202 | - $phpBinary= array( PHP_BINARY, PHP_BINDIR . "/php", '/usr/bin/php'); |
|
| 202 | + $phpBinary= array( PHP_BINARY, PHP_BINDIR . "/php", '/usr/bin/php'); |
|
| 203 | 203 | |
| 204 | - foreach ($phpBinary as $phpBin ) |
|
| 205 | - { |
|
| 206 | - $output=array(); |
|
| 207 | - $retCode=255; |
|
| 208 | - $input="154865134987aaaa"; |
|
| 209 | - exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
|
| 204 | + foreach ($phpBinary as $phpBin ) |
|
| 205 | + { |
|
| 206 | + $output=array(); |
|
| 207 | + $retCode=255; |
|
| 208 | + $input="154865134987aaaa"; |
|
| 209 | + exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
|
| 210 | 210 | |
| 211 | - if (! isset($output[0])) $output[0]="NO OUT"; |
|
| 211 | + if (! isset($output[0])) $output[0]="NO OUT"; |
|
| 212 | 212 | |
| 213 | - if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
|
| 214 | - { |
|
| 215 | - return $phpBin; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - return NULL; |
|
| 213 | + if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
|
| 214 | + { |
|
| 215 | + return $phpBin; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + return NULL; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | public function indexAction() |
| 235 | 235 | { |
| 236 | 236 | |
| 237 | - // CHeck permissions : display tests in any case, but no configuration. |
|
| 237 | + // CHeck permissions : display tests in any case, but no configuration. |
|
| 238 | 238 | $this->view->configPermission=$this->checkModuleConfigPermission(1); |
| 239 | 239 | // But check read permission |
| 240 | 240 | $this->checkReadPermission(); |
@@ -242,16 +242,16 @@ discard block |
||
| 242 | 242 | $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
| 243 | 243 | |
| 244 | 244 | // Get message : sent on configuration problems detected by controllers |
| 245 | - $this->get_param(); |
|
| 245 | + $this->get_param(); |
|
| 246 | 246 | |
| 247 | - // Test if configuration exists, if not create for installer script |
|
| 247 | + // Test if configuration exists, if not create for installer script |
|
| 248 | 248 | $this->check_empty_config(); |
| 249 | 249 | |
| 250 | 250 | // Test Database |
| 251 | - $this->check_db(); |
|
| 251 | + $this->check_db(); |
|
| 252 | 252 | |
| 253 | 253 | //********* Test API |
| 254 | - $this->check_api(); |
|
| 254 | + $this->check_api(); |
|
| 255 | 255 | |
| 256 | 256 | //*********** Test snmptrapd alive and options |
| 257 | 257 | list ($this->view->snmptrapdError, $this->view->snmptrapdMessage) = $this->checkSnmpTrapd(); |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | $phpBinary = $this->get_php_binary(); |
| 266 | 266 | if ($phpBinary == null) |
| 267 | 267 | { |
| 268 | - $phpBinary = ' PHP BINARY NOT FOUND '; |
|
| 268 | + $phpBinary = ' PHP BINARY NOT FOUND '; |
|
| 269 | 269 | |
| 270 | 270 | } |
| 271 | 271 | |
@@ -273,11 +273,11 @@ discard block |
||
| 273 | 273 | $this->view->traps_in_config= $phpBinary . ' ' . $this->Module()->getBaseDir() . '/bin/trap_in.php'; |
| 274 | 274 | |
| 275 | 275 | $this->view->installer= $this->Module()->getBaseDir() . '/bin/installer.sh ' |
| 276 | - . ' -c all ' |
|
| 277 | - . ' -d ' . $this->Module()->getBaseDir() |
|
| 278 | - . ' -p ' . $phpBinary |
|
| 279 | - . ' -a ' . exec('whoami') |
|
| 280 | - . ' -w ' . Icinga::app()->getConfigDir(); |
|
| 276 | + . ' -c all ' |
|
| 277 | + . ' -d ' . $this->Module()->getBaseDir() |
|
| 278 | + . ' -p ' . $phpBinary |
|
| 279 | + . ' -a ' . exec('whoami') |
|
| 280 | + . ' -w ' . Icinga::app()->getConfigDir(); |
|
| 281 | 281 | |
| 282 | 282 | // ******************* configuration form setup******************* |
| 283 | 283 | $this->view->form = $form = new TrapsConfigForm(); |
@@ -307,8 +307,8 @@ discard block |
||
| 307 | 307 | |
| 308 | 308 | try |
| 309 | 309 | { |
| 310 | - $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
| 311 | - printf('Schema already exists'); |
|
| 310 | + $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
| 311 | + printf('Schema already exists'); |
|
| 312 | 312 | |
| 313 | 313 | } |
| 314 | 314 | catch (DBException $e) |
@@ -319,19 +319,19 @@ discard block |
||
| 319 | 319 | // Get module database name |
| 320 | 320 | $dbName=$this->Config()->get('config', 'database'); |
| 321 | 321 | |
| 322 | - $dbResource = ResourceFactory::getResourceConfig($dbName); |
|
| 323 | - $dbType=$dbResource->get('db'); |
|
| 324 | - switch ($dbType) { |
|
| 325 | - case 'mysql': |
|
| 326 | - $dbFileExt='sql'; |
|
| 327 | - break; |
|
| 328 | - case 'pgsql': |
|
| 329 | - $dbFileExt='pgsql'; |
|
| 330 | - break; |
|
| 331 | - default: |
|
| 332 | - printf("Database configuration error : Unsuported DB"); |
|
| 333 | - return; |
|
| 334 | - } |
|
| 322 | + $dbResource = ResourceFactory::getResourceConfig($dbName); |
|
| 323 | + $dbType=$dbResource->get('db'); |
|
| 324 | + switch ($dbType) { |
|
| 325 | + case 'mysql': |
|
| 326 | + $dbFileExt='sql'; |
|
| 327 | + break; |
|
| 328 | + case 'pgsql': |
|
| 329 | + $dbFileExt='pgsql'; |
|
| 330 | + break; |
|
| 331 | + default: |
|
| 332 | + printf("Database configuration error : Unsuported DB"); |
|
| 333 | + return; |
|
| 334 | + } |
|
| 335 | 335 | |
| 336 | 336 | printf('<pre>'); |
| 337 | 337 | require_once $this->Module()->getBaseDir() .'/bin/trap_class.php'; |
@@ -355,32 +355,32 @@ discard block |
||
| 355 | 355 | public function updateschemaAction() |
| 356 | 356 | { |
| 357 | 357 | $this->checkModuleConfigPermission(); |
| 358 | - $this->getTabs()->add('get',array( |
|
| 359 | - 'active' => true, |
|
| 360 | - 'label' => $this->translate('Update Schema'), |
|
| 361 | - 'url' => Url::fromRequest() |
|
| 362 | - )); |
|
| 358 | + $this->getTabs()->add('get',array( |
|
| 359 | + 'active' => true, |
|
| 360 | + 'label' => $this->translate('Update Schema'), |
|
| 361 | + 'url' => Url::fromRequest() |
|
| 362 | + )); |
|
| 363 | 363 | // check if needed |
| 364 | 364 | $dberror=array(); |
| 365 | - try |
|
| 366 | - { |
|
| 367 | - $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
| 368 | - echo 'Schema already exists and is up to date<br>'; |
|
| 369 | - return; |
|
| 370 | - } |
|
| 371 | - catch (DBException $e) |
|
| 372 | - { |
|
| 373 | - $dberror=$e->getArray(); |
|
| 374 | - } |
|
| 365 | + try |
|
| 366 | + { |
|
| 367 | + $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
|
| 368 | + echo 'Schema already exists and is up to date<br>'; |
|
| 369 | + return; |
|
| 370 | + } |
|
| 371 | + catch (DBException $e) |
|
| 372 | + { |
|
| 373 | + $dberror=$e->getArray(); |
|
| 374 | + } |
|
| 375 | 375 | |
| 376 | 376 | echo 'Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a><br><br>'; |
| 377 | 377 | |
| 378 | 378 | if ($dberror[0] != 5) |
| 379 | 379 | { |
| 380 | - echo 'Database does not exists or is not setup correctly<br>'; |
|
| 381 | - return; |
|
| 380 | + echo 'Database does not exists or is not setup correctly<br>'; |
|
| 381 | + return; |
|
| 382 | 382 | } |
| 383 | - // setup |
|
| 383 | + // setup |
|
| 384 | 384 | require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
| 385 | 385 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 386 | 386 | $debug_level=4; |
@@ -393,20 +393,20 @@ discard block |
||
| 393 | 393 | $target_version=$dberror[2]; |
| 394 | 394 | |
| 395 | 395 | if ($this->params->get('msgok') == null) { |
| 396 | - // Check for messages and display if any |
|
| 397 | - echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
|
| 398 | - $Trap->setLogging(2,'syslog'); |
|
| 399 | - $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
| 400 | - if ($message != '') |
|
| 401 | - { |
|
| 402 | - echo 'Note :<br><pre>'; |
|
| 403 | - echo $message; |
|
| 404 | - echo '</pre>'; |
|
| 405 | - echo '<br>'; |
|
| 406 | - 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>'; |
|
| 407 | - echo '<br>'; |
|
| 408 | - return; |
|
| 409 | - } |
|
| 396 | + // Check for messages and display if any |
|
| 397 | + echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
|
| 398 | + $Trap->setLogging(2,'syslog'); |
|
| 399 | + $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
| 400 | + if ($message != '') |
|
| 401 | + { |
|
| 402 | + echo 'Note :<br><pre>'; |
|
| 403 | + echo $message; |
|
| 404 | + echo '</pre>'; |
|
| 405 | + echo '<br>'; |
|
| 406 | + 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>'; |
|
| 407 | + echo '<br>'; |
|
| 408 | + return; |
|
| 409 | + } |
|
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | $Trap->setLogging($debug_level,'display'); |
@@ -420,38 +420,38 @@ discard block |
||
| 420 | 420 | |
| 421 | 421 | private function checkSnmpTrapd() |
| 422 | 422 | { |
| 423 | - $psOutput=array(); |
|
| 424 | - // First check is someone is listening to port 162. As not root, we can't have pid... |
|
| 425 | - $sspath = exec('which ss 2>/dev/null'); |
|
| 426 | - if(empty($sspath)) |
|
| 427 | - { |
|
| 428 | - // RHEL based systems |
|
| 429 | - $sspath = '/usr/sbin/ss'; |
|
| 430 | - } |
|
| 431 | - if(!is_executable("$sspath")) |
|
| 432 | - { |
|
| 433 | - return array(1,"Can not execute $sspath"); |
|
| 434 | - } |
|
| 435 | - exec("$sspath -lun | grep ':162 '",$psOutput); |
|
| 436 | - if (count($psOutput) == 0) |
|
| 437 | - { |
|
| 438 | - return array(1,'Port UDP/162 is not open : is snmptrapd running?'); |
|
| 439 | - } |
|
| 440 | - $psOutput=array(); |
|
| 441 | - exec('ps --no-headers -o command -C snmptrapd',$psOutput); |
|
| 442 | - if (count($psOutput) == 0) |
|
| 443 | - { |
|
| 444 | - return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 445 | - } |
|
| 446 | - // Assume there is only one line... TODO : see if there is a better way to do this |
|
| 447 | - $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
| 448 | - if (!preg_match('/-n/',$line)) |
|
| 449 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
| 450 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
| 451 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
| 452 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
| 453 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 423 | + $psOutput=array(); |
|
| 424 | + // First check is someone is listening to port 162. As not root, we can't have pid... |
|
| 425 | + $sspath = exec('which ss 2>/dev/null'); |
|
| 426 | + if(empty($sspath)) |
|
| 427 | + { |
|
| 428 | + // RHEL based systems |
|
| 429 | + $sspath = '/usr/sbin/ss'; |
|
| 430 | + } |
|
| 431 | + if(!is_executable("$sspath")) |
|
| 432 | + { |
|
| 433 | + return array(1,"Can not execute $sspath"); |
|
| 434 | + } |
|
| 435 | + exec("$sspath -lun | grep ':162 '",$psOutput); |
|
| 436 | + if (count($psOutput) == 0) |
|
| 437 | + { |
|
| 438 | + return array(1,'Port UDP/162 is not open : is snmptrapd running?'); |
|
| 439 | + } |
|
| 440 | + $psOutput=array(); |
|
| 441 | + exec('ps --no-headers -o command -C snmptrapd',$psOutput); |
|
| 442 | + if (count($psOutput) == 0) |
|
| 443 | + { |
|
| 444 | + return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 445 | + } |
|
| 446 | + // Assume there is only one line... TODO : see if there is a better way to do this |
|
| 447 | + $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
| 448 | + if (!preg_match('/-n/',$line)) |
|
| 449 | + return array(1,'snmptrapd has no -n option : '.$line); |
|
| 450 | + if (!preg_match('/-O[^ ]*n/',$line)) |
|
| 451 | + return array(1,'snmptrapd has no -On option : '.$line); |
|
| 452 | + if (!preg_match('/-O[^ ]*e/',$line)) |
|
| 453 | + return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 454 | 454 | |
| 455 | - return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
| 455 | + return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
| 456 | 456 | } |
| 457 | 457 | } |
@@ -72,23 +72,23 @@ discard block |
||
| 72 | 72 | private function check_db() |
| 73 | 73 | { |
| 74 | 74 | $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
| 75 | - 0 => array('Database configuration OK','',''), |
|
| 76 | - 1 => array('Database set in config.ini','No database in config.ini',''), |
|
| 77 | - 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
|
| 75 | + 0 => array('Database configuration OK', '', ''), |
|
| 76 | + 1 => array('Database set in config.ini', 'No database in config.ini', ''), |
|
| 77 | + 2 => array('Database exists in Icingaweb2 config', 'Database does not exist in Icingaweb2 : ', |
|
| 78 | 78 | Url::fromPath('config/resource')), |
| 79 | - 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
|
| 79 | + 3 => array('Database credentials OK', 'Database does not exist/invalid credentials/no schema : ', |
|
| 80 | 80 | Url::fromPath('trapdirector/settings/createschema')), |
| 81 | - 4 => array('Schema is set','Schema is not set for ', |
|
| 81 | + 4 => array('Schema is set', 'Schema is not set for ', |
|
| 82 | 82 | Url::fromPath('trapdirector/settings/createschema')), |
| 83 | - 5 => array('Schema is up to date','Schema is outdated :', |
|
| 83 | + 5 => array('Schema is up to date', 'Schema is outdated :', |
|
| 84 | 84 | Url::fromPath('trapdirector/settings/updateschema')), |
| 85 | 85 | ); |
| 86 | 86 | |
| 87 | 87 | try { |
| 88 | 88 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
| 89 | - $dberror=array(0,''); |
|
| 89 | + $dberror=array(0, ''); |
|
| 90 | 90 | } catch (DBException $e) { |
| 91 | - $dberror = $e->getArray(); |
|
| 91 | + $dberror=$e->getArray(); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | $this->view->db_error=$dberror[0]; |
@@ -96,13 +96,13 @@ discard block |
||
| 96 | 96 | { |
| 97 | 97 | case 2: |
| 98 | 98 | case 4: |
| 99 | - $db_message[$dberror[0]][1] .= $dberror[1]; |
|
| 99 | + $db_message[$dberror[0]][1].=$dberror[1]; |
|
| 100 | 100 | break; |
| 101 | 101 | case 3: |
| 102 | - $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
|
| 102 | + $db_message[$dberror[0]][1].=$dberror[1].', Message : '.$dberror[2]; |
|
| 103 | 103 | break; |
| 104 | 104 | case 5: |
| 105 | - $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
|
| 105 | + $db_message[$dberror[0]][1].=' version '.$dberror[1].', version needed : '.$dberror[2]; |
|
| 106 | 106 | break; |
| 107 | 107 | case 0: |
| 108 | 108 | case 1: |
@@ -114,13 +114,13 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | try { |
| 116 | 116 | $this->getUIDatabase()->testGetIdoDb(); // Get DB in test mode |
| 117 | - $dberror=array(0,''); |
|
| 117 | + $dberror=array(0, ''); |
|
| 118 | 118 | } catch (DBException $e) { |
| 119 | - $dberror = $e->getArray(); |
|
| 119 | + $dberror=$e->getArray(); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | $this->view->ido_db_error=$dberror[0]; |
| 123 | - $this->view->ido_message='IDO Database : ' . $dberror[1]; |
|
| 123 | + $this->view->ido_message='IDO Database : '.$dberror[1]; |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
@@ -132,13 +132,13 @@ discard block |
||
| 132 | 132 | { |
| 133 | 133 | if ($this->Config()->get('config', 'icingaAPI_host') != '') |
| 134 | 134 | { |
| 135 | - $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
|
| 135 | + $apitest=new Icinga2Api($this->Config()->get('config', 'icingaAPI_host'), $this->Config()->get('config', 'icingaAPI_port')); |
|
| 136 | 136 | $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
| 137 | 137 | try { |
| 138 | - list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 138 | + list($this->view->apimessageError, $this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
|
| 139 | 139 | //$this->view->apimessageError=false; |
| 140 | 140 | } catch (RuntimeException $e) { |
| 141 | - $this->view->apimessage='API config : ' . $e->getMessage(); |
|
| 141 | + $this->view->apimessage='API config : '.$e->getMessage(); |
|
| 142 | 142 | $this->view->apimessageError=true; |
| 143 | 143 | } |
| 144 | 144 | } |
@@ -163,10 +163,10 @@ discard block |
||
| 163 | 163 | { |
| 164 | 164 | $output=array(); |
| 165 | 165 | |
| 166 | - exec('cat ' . $this->module->getBaseDir() .'/bin/trap_in.php | grep "\$icingaweb2Etc=" ',$output); |
|
| 166 | + exec('cat '.$this->module->getBaseDir().'/bin/trap_in.php | grep "\$icingaweb2Etc=" ', $output); |
|
| 167 | 167 | |
| 168 | 168 | |
| 169 | - if (! isset($output[0]) || ! preg_match('#"'. $icingaweb2_etc .'"#',$output[0])) |
|
| 169 | + if (!isset($output[0]) || !preg_match('#"'.$icingaweb2_etc.'"#', $output[0])) |
|
| 170 | 170 | { |
| 171 | 171 | $this->view->icingaEtcWarn=1; |
| 172 | 172 | $this->view->icingaweb2_etc=$icingaweb2_etc; |
@@ -182,12 +182,12 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | private function get_db_list($allowed) |
| 184 | 184 | { |
| 185 | - $resources = array(); |
|
| 185 | + $resources=array(); |
|
| 186 | 186 | foreach (ResourceFactory::getResourceConfigs() as $name => $resource) |
| 187 | 187 | { |
| 188 | 188 | if ($resource->get('type') === 'db' && in_array($resource->get('db'), $allowed)) |
| 189 | 189 | { |
| 190 | - $resources[$name] = $name; |
|
| 190 | + $resources[$name]=$name; |
|
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | return $resources; |
@@ -199,18 +199,18 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | private function get_php_binary() |
| 201 | 201 | { |
| 202 | - $phpBinary= array( PHP_BINARY, PHP_BINDIR . "/php", '/usr/bin/php'); |
|
| 202 | + $phpBinary=array(PHP_BINARY, PHP_BINDIR."/php", '/usr/bin/php'); |
|
| 203 | 203 | |
| 204 | - foreach ($phpBinary as $phpBin ) |
|
| 204 | + foreach ($phpBinary as $phpBin) |
|
| 205 | 205 | { |
| 206 | 206 | $output=array(); |
| 207 | 207 | $retCode=255; |
| 208 | 208 | $input="154865134987aaaa"; |
| 209 | - exec("$phpBin -r \"echo '$input';\"",$output,$retCode); |
|
| 209 | + exec("$phpBin -r \"echo '$input';\"", $output, $retCode); |
|
| 210 | 210 | |
| 211 | - if (! isset($output[0])) $output[0]="NO OUT"; |
|
| 211 | + if (!isset($output[0])) $output[0]="NO OUT"; |
|
| 212 | 212 | |
| 213 | - if ($retCode == 0 && preg_match("/$input/",$output[0]) == 1) |
|
| 213 | + if ($retCode == 0 && preg_match("/$input/", $output[0]) == 1) |
|
| 214 | 214 | { |
| 215 | 215 | return $phpBin; |
| 216 | 216 | } |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | // But check read permission |
| 240 | 240 | $this->checkReadPermission(); |
| 241 | 241 | |
| 242 | - $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
|
| 242 | + $this->view->tabs=$this->Module()->getConfigTabs()->activate('config'); |
|
| 243 | 243 | |
| 244 | 244 | // Get message : sent on configuration problems detected by controllers |
| 245 | 245 | $this->get_param(); |
@@ -254,39 +254,39 @@ discard block |
||
| 254 | 254 | $this->check_api(); |
| 255 | 255 | |
| 256 | 256 | //*********** Test snmptrapd alive and options |
| 257 | - list ($this->view->snmptrapdError, $this->view->snmptrapdMessage) = $this->checkSnmpTrapd(); |
|
| 257 | + list ($this->view->snmptrapdError, $this->view->snmptrapdMessage)=$this->checkSnmpTrapd(); |
|
| 258 | 258 | |
| 259 | 259 | // List DB in $ressources |
| 260 | - $resources = $this->get_db_list(array('mysql', 'pgsql')); |
|
| 260 | + $resources=$this->get_db_list(array('mysql', 'pgsql')); |
|
| 261 | 261 | |
| 262 | 262 | // Check standard Icingaweb2 path |
| 263 | 263 | $this->check_icingaweb_path(); |
| 264 | 264 | |
| 265 | - $phpBinary = $this->get_php_binary(); |
|
| 265 | + $phpBinary=$this->get_php_binary(); |
|
| 266 | 266 | if ($phpBinary == null) |
| 267 | 267 | { |
| 268 | - $phpBinary = ' PHP BINARY NOT FOUND '; |
|
| 268 | + $phpBinary=' PHP BINARY NOT FOUND '; |
|
| 269 | 269 | |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | // Setup path for mini documentation |
| 273 | - $this->view->traps_in_config= $phpBinary . ' ' . $this->Module()->getBaseDir() . '/bin/trap_in.php'; |
|
| 273 | + $this->view->traps_in_config=$phpBinary.' '.$this->Module()->getBaseDir().'/bin/trap_in.php'; |
|
| 274 | 274 | |
| 275 | - $this->view->installer= $this->Module()->getBaseDir() . '/bin/installer.sh ' |
|
| 275 | + $this->view->installer=$this->Module()->getBaseDir().'/bin/installer.sh ' |
|
| 276 | 276 | . ' -c all ' |
| 277 | - . ' -d ' . $this->Module()->getBaseDir() |
|
| 278 | - . ' -p ' . $phpBinary |
|
| 279 | - . ' -a ' . exec('whoami') |
|
| 280 | - . ' -w ' . Icinga::app()->getConfigDir(); |
|
| 277 | + . ' -d '.$this->Module()->getBaseDir() |
|
| 278 | + . ' -p '.$phpBinary |
|
| 279 | + . ' -a '.exec('whoami') |
|
| 280 | + . ' -w '.Icinga::app()->getConfigDir(); |
|
| 281 | 281 | |
| 282 | 282 | // ******************* configuration form setup******************* |
| 283 | - $this->view->form = $form = new TrapsConfigForm(); |
|
| 283 | + $this->view->form=$form=new TrapsConfigForm(); |
|
| 284 | 284 | |
| 285 | 285 | // set default paths; |
| 286 | - $this->view->form->setPaths($this->Module()->getBaseDir(),Icinga::app()->getConfigDir()); |
|
| 286 | + $this->view->form->setPaths($this->Module()->getBaseDir(), Icinga::app()->getConfigDir()); |
|
| 287 | 287 | |
| 288 | 288 | // set default ido database |
| 289 | - $this->view->form->setDefaultIDODB($this->Config()->module('monitoring','backends')->get('icinga','resource')); |
|
| 289 | + $this->view->form->setDefaultIDODB($this->Config()->module('monitoring', 'backends')->get('icinga', 'resource')); |
|
| 290 | 290 | |
| 291 | 291 | // Make form handle request. |
| 292 | 292 | $form->setIniConfig($this->Config()) |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | public function createschemaAction() |
| 299 | 299 | { |
| 300 | 300 | $this->checkModuleConfigPermission(); |
| 301 | - $this->getTabs()->add('create_schema',array( |
|
| 301 | + $this->getTabs()->add('create_schema', array( |
|
| 302 | 302 | 'active' => true, |
| 303 | 303 | 'label' => $this->translate('Create Schema'), |
| 304 | 304 | 'url' => Url::fromRequest() |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | // Get module database name |
| 320 | 320 | $dbName=$this->Config()->get('config', 'database'); |
| 321 | 321 | |
| 322 | - $dbResource = ResourceFactory::getResourceConfig($dbName); |
|
| 322 | + $dbResource=ResourceFactory::getResourceConfig($dbName); |
|
| 323 | 323 | $dbType=$dbResource->get('db'); |
| 324 | 324 | switch ($dbType) { |
| 325 | 325 | case 'mysql': |
@@ -334,28 +334,28 @@ discard block |
||
| 334 | 334 | } |
| 335 | 335 | |
| 336 | 336 | printf('<pre>'); |
| 337 | - require_once $this->Module()->getBaseDir() .'/bin/trap_class.php'; |
|
| 337 | + require_once $this->Module()->getBaseDir().'/bin/trap_class.php'; |
|
| 338 | 338 | |
| 339 | 339 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 340 | 340 | $debug_level=4; |
| 341 | - $Trap = new Trap($icingaweb2_etc); |
|
| 342 | - $Trap->setLogging($debug_level,'display'); |
|
| 341 | + $Trap=new Trap($icingaweb2_etc); |
|
| 342 | + $Trap->setLogging($debug_level, 'display'); |
|
| 343 | 343 | |
| 344 | 344 | $prefix=$this->Config()->get('config', 'database_prefix'); |
| 345 | 345 | // schema file : <path>/SQL/schema_v<verion>.<dbtype> |
| 346 | - $schema=$this->Module()->getBaseDir() . |
|
| 347 | - '/SQL/schema_v'. $this->getModuleConfig()->getDbCurVersion() . '.' . $dbFileExt; |
|
| 346 | + $schema=$this->Module()->getBaseDir(). |
|
| 347 | + '/SQL/schema_v'.$this->getModuleConfig()->getDbCurVersion().'.'.$dbFileExt; |
|
| 348 | 348 | |
| 349 | - $Trap->trapsDB->create_schema($schema,$prefix); |
|
| 349 | + $Trap->trapsDB->create_schema($schema, $prefix); |
|
| 350 | 350 | echo '</pre>'; |
| 351 | 351 | } |
| 352 | - echo '<br><br>Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a>'; |
|
| 352 | + echo '<br><br>Return to <a href="'.Url::fromPath('trapdirector/settings').'" class="link-button icon-wrench"> settings page </a>'; |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | public function updateschemaAction() |
| 356 | 356 | { |
| 357 | 357 | $this->checkModuleConfigPermission(); |
| 358 | - $this->getTabs()->add('get',array( |
|
| 358 | + $this->getTabs()->add('get', array( |
|
| 359 | 359 | 'active' => true, |
| 360 | 360 | 'label' => $this->translate('Update Schema'), |
| 361 | 361 | 'url' => Url::fromRequest() |
@@ -373,7 +373,7 @@ discard block |
||
| 373 | 373 | $dberror=$e->getArray(); |
| 374 | 374 | } |
| 375 | 375 | |
| 376 | - echo 'Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a><br><br>'; |
|
| 376 | + echo 'Return to <a href="'.Url::fromPath('trapdirector/settings').'" class="link-button icon-wrench"> settings page </a><br><br>'; |
|
| 377 | 377 | |
| 378 | 378 | if ($dberror[0] != 5) |
| 379 | 379 | { |
@@ -381,40 +381,40 @@ discard block |
||
| 381 | 381 | return; |
| 382 | 382 | } |
| 383 | 383 | // setup |
| 384 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
| 384 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
| 385 | 385 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 386 | 386 | $debug_level=4; |
| 387 | - $Trap = new Trap($icingaweb2_etc); |
|
| 387 | + $Trap=new Trap($icingaweb2_etc); |
|
| 388 | 388 | |
| 389 | 389 | |
| 390 | 390 | $prefix=$this->Config()->get('config', 'database_prefix'); |
| 391 | - $updateSchema=$this->Module()->getBaseDir() . '/SQL/'; |
|
| 391 | + $updateSchema=$this->Module()->getBaseDir().'/SQL/'; |
|
| 392 | 392 | |
| 393 | 393 | $target_version=$dberror[2]; |
| 394 | 394 | |
| 395 | 395 | if ($this->params->get('msgok') == null) { |
| 396 | 396 | // Check for messages and display if any |
| 397 | 397 | echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
| 398 | - $Trap->setLogging(2,'syslog'); |
|
| 399 | - $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
|
| 398 | + $Trap->setLogging(2, 'syslog'); |
|
| 399 | + $message=$Trap->trapsDB->update_schema($updateSchema, $target_version, $prefix, true); |
|
| 400 | 400 | if ($message != '') |
| 401 | 401 | { |
| 402 | 402 | echo 'Note :<br><pre>'; |
| 403 | 403 | echo $message; |
| 404 | 404 | echo '</pre>'; |
| 405 | 405 | echo '<br>'; |
| 406 | - 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>'; |
|
| 406 | + 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>'; |
|
| 407 | 407 | echo '<br>'; |
| 408 | 408 | return; |
| 409 | 409 | } |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | - $Trap->setLogging($debug_level,'display'); |
|
| 412 | + $Trap->setLogging($debug_level, 'display'); |
|
| 413 | 413 | |
| 414 | - echo 'Updating schema to '. $target_version . ': <br>'; |
|
| 414 | + echo 'Updating schema to '.$target_version.': <br>'; |
|
| 415 | 415 | echo '<pre>'; |
| 416 | 416 | |
| 417 | - $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix); |
|
| 417 | + $Trap->trapsDB->update_schema($updateSchema, $target_version, $prefix); |
|
| 418 | 418 | echo '</pre>'; |
| 419 | 419 | } |
| 420 | 420 | |
@@ -422,36 +422,36 @@ discard block |
||
| 422 | 422 | { |
| 423 | 423 | $psOutput=array(); |
| 424 | 424 | // First check is someone is listening to port 162. As not root, we can't have pid... |
| 425 | - $sspath = exec('which ss 2>/dev/null'); |
|
| 426 | - if(empty($sspath)) |
|
| 425 | + $sspath=exec('which ss 2>/dev/null'); |
|
| 426 | + if (empty($sspath)) |
|
| 427 | 427 | { |
| 428 | 428 | // RHEL based systems |
| 429 | - $sspath = '/usr/sbin/ss'; |
|
| 429 | + $sspath='/usr/sbin/ss'; |
|
| 430 | 430 | } |
| 431 | - if(!is_executable("$sspath")) |
|
| 431 | + if (!is_executable("$sspath")) |
|
| 432 | 432 | { |
| 433 | - return array(1,"Can not execute $sspath"); |
|
| 433 | + return array(1, "Can not execute $sspath"); |
|
| 434 | 434 | } |
| 435 | - exec("$sspath -lun | grep ':162 '",$psOutput); |
|
| 435 | + exec("$sspath -lun | grep ':162 '", $psOutput); |
|
| 436 | 436 | if (count($psOutput) == 0) |
| 437 | 437 | { |
| 438 | - return array(1,'Port UDP/162 is not open : is snmptrapd running?'); |
|
| 438 | + return array(1, 'Port UDP/162 is not open : is snmptrapd running?'); |
|
| 439 | 439 | } |
| 440 | 440 | $psOutput=array(); |
| 441 | - exec('ps --no-headers -o command -C snmptrapd',$psOutput); |
|
| 441 | + exec('ps --no-headers -o command -C snmptrapd', $psOutput); |
|
| 442 | 442 | if (count($psOutput) == 0) |
| 443 | 443 | { |
| 444 | - return array(1,"UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 444 | + return array(1, "UDP/162 : OK, but no snmptrapd process (?)"); |
|
| 445 | 445 | } |
| 446 | 446 | // Assume there is only one line... TODO : see if there is a better way to do this |
| 447 | - $line = preg_replace('/^.*snmptrapd /','',$psOutput[0]); |
|
| 448 | - if (!preg_match('/-n/',$line)) |
|
| 449 | - return array(1,'snmptrapd has no -n option : '.$line); |
|
| 450 | - if (!preg_match('/-O[^ ]*n/',$line)) |
|
| 451 | - return array(1,'snmptrapd has no -On option : '.$line); |
|
| 452 | - if (!preg_match('/-O[^ ]*e/',$line)) |
|
| 453 | - return array(1,'snmptrapd has no -Oe option : '.$line); |
|
| 447 | + $line=preg_replace('/^.*snmptrapd /', '', $psOutput[0]); |
|
| 448 | + if (!preg_match('/-n/', $line)) |
|
| 449 | + return array(1, 'snmptrapd has no -n option : '.$line); |
|
| 450 | + if (!preg_match('/-O[^ ]*n/', $line)) |
|
| 451 | + return array(1, 'snmptrapd has no -On option : '.$line); |
|
| 452 | + if (!preg_match('/-O[^ ]*e/', $line)) |
|
| 453 | + return array(1, 'snmptrapd has no -Oe option : '.$line); |
|
| 454 | 454 | |
| 455 | - return array(0,'snmptrapd listening to UDP/162, options : '.$line); |
|
| 455 | + return array(0, 'snmptrapd listening to UDP/162, options : '.$line); |
|
| 456 | 456 | } |
| 457 | 457 | } |