@@ -15,183 +15,183 @@ |
||
| 15 | 15 | trait TrapConfig |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @return \Trapdirector\Logging */ |
|
| 19 | - abstract public function getLogging(); |
|
| 20 | - /** @return \Trapdirector\TrapApi */ |
|
| 21 | - abstract public function getTrapApi(); |
|
| 18 | + /** @return \Trapdirector\Logging */ |
|
| 19 | + abstract public function getLogging(); |
|
| 20 | + /** @return \Trapdirector\TrapApi */ |
|
| 21 | + abstract public function getTrapApi(); |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Get option from array of ini file, send message if empty |
|
| 25 | - * @param string $option_array Array of ini file |
|
| 26 | - * @param string $option_category category in ini file |
|
| 27 | - * @param string $option_name name of option in category |
|
| 28 | - * @param mixed $option_var variable to fill if found, left untouched if not found |
|
| 29 | - * @param integer $log_level default 2 (warning) |
|
| 30 | - * @param string $message warning message if not found |
|
| 31 | - * @return boolean true if found, or false |
|
| 32 | - */ |
|
| 33 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = WARN, $message = null) |
|
| 34 | - { |
|
| 35 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 36 | - { |
|
| 37 | - if ($message === null) |
|
| 38 | - { |
|
| 39 | - $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
| 40 | - } |
|
| 41 | - $this->getLogging()->log($message,$log_level); |
|
| 42 | - return false; |
|
| 43 | - } |
|
| 44 | - else |
|
| 45 | - { |
|
| 46 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 47 | - return true; |
|
| 48 | - } |
|
| 49 | - } |
|
| 23 | + /** |
|
| 24 | + * Get option from array of ini file, send message if empty |
|
| 25 | + * @param string $option_array Array of ini file |
|
| 26 | + * @param string $option_category category in ini file |
|
| 27 | + * @param string $option_name name of option in category |
|
| 28 | + * @param mixed $option_var variable to fill if found, left untouched if not found |
|
| 29 | + * @param integer $log_level default 2 (warning) |
|
| 30 | + * @param string $message warning message if not found |
|
| 31 | + * @return boolean true if found, or false |
|
| 32 | + */ |
|
| 33 | + protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = WARN, $message = null) |
|
| 34 | + { |
|
| 35 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 36 | + { |
|
| 37 | + if ($message === null) |
|
| 38 | + { |
|
| 39 | + $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
| 40 | + } |
|
| 41 | + $this->getLogging()->log($message,$log_level); |
|
| 42 | + return false; |
|
| 43 | + } |
|
| 44 | + else |
|
| 45 | + { |
|
| 46 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 47 | + return true; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Get options in database |
|
| 53 | - */ |
|
| 54 | - protected function getDatabaseOptions() |
|
| 55 | - { |
|
| 56 | - // Database options |
|
| 57 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 58 | - { |
|
| 59 | - $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
| 60 | - $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
| 61 | - $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
| 62 | - } |
|
| 63 | - } |
|
| 51 | + /** |
|
| 52 | + * Get options in database |
|
| 53 | + */ |
|
| 54 | + protected function getDatabaseOptions() |
|
| 55 | + { |
|
| 56 | + // Database options |
|
| 57 | + if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
| 58 | + { |
|
| 59 | + $this->getDBConfigIfSet('log_level',$this->getLogging()->debugLevel); |
|
| 60 | + $this->getDBConfigIfSet('log_destination',$this->getLogging()->outputMode); |
|
| 61 | + $this->getDBConfigIfSet('log_file',$this->getLogging()->outputFile); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** Set $variable to value if $element found in database config table |
|
| 66 | - * @param string $element |
|
| 67 | - * @param string $variable |
|
| 68 | - */ |
|
| 69 | - protected function getDBConfigIfSet($element,&$variable) |
|
| 70 | - { |
|
| 71 | - $value=$this->getDBConfig($element); |
|
| 72 | - if ($value != null) $variable=$value; |
|
| 73 | - } |
|
| 65 | + /** Set $variable to value if $element found in database config table |
|
| 66 | + * @param string $element |
|
| 67 | + * @param string $variable |
|
| 68 | + */ |
|
| 69 | + protected function getDBConfigIfSet($element,&$variable) |
|
| 70 | + { |
|
| 71 | + $value=$this->getDBConfig($element); |
|
| 72 | + if ($value != null) $variable=$value; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Get data from db_config |
|
| 77 | - * @param $element string name of param |
|
| 78 | - * @return mixed : value (or null) |
|
| 79 | - */ |
|
| 80 | - protected function getDBConfig($element) // TODO : put this in DB class |
|
| 81 | - { |
|
| 82 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 83 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 84 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 85 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 88 | - $value=$ret_code->fetch(); |
|
| 89 | - if ($value != null && isset($value['value'])) |
|
| 90 | - { |
|
| 91 | - return $value['value']; |
|
| 92 | - } |
|
| 93 | - return null; |
|
| 94 | - } |
|
| 75 | + /** |
|
| 76 | + * Get data from db_config |
|
| 77 | + * @param $element string name of param |
|
| 78 | + * @return mixed : value (or null) |
|
| 79 | + */ |
|
| 80 | + protected function getDBConfig($element) // TODO : put this in DB class |
|
| 81 | + { |
|
| 82 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 83 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
| 84 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 85 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | + $value=$ret_code->fetch(); |
|
| 89 | + if ($value != null && isset($value['value'])) |
|
| 90 | + { |
|
| 91 | + return $value['value']; |
|
| 92 | + } |
|
| 93 | + return null; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Get options from ini file |
|
| 98 | - * @param array $trap_config : ini file array |
|
| 99 | - */ |
|
| 100 | - protected function getMainOptions($trapConfig) |
|
| 101 | - { |
|
| 96 | + /** |
|
| 97 | + * Get options from ini file |
|
| 98 | + * @param array $trap_config : ini file array |
|
| 99 | + */ |
|
| 100 | + protected function getMainOptions($trapConfig) |
|
| 101 | + { |
|
| 102 | 102 | |
| 103 | - $nodeStatus=''; |
|
| 104 | - $this->getOptionIfSet($trapConfig,'config','node', $nodeStatus); |
|
| 105 | - if ($this->getTrapApi()->setStatus($nodeStatus) === FALSE) |
|
| 106 | - { |
|
| 107 | - $this->getLogging()->log('Unknown node status '.$nodeStatus.' : setting to MASTER',WARN); |
|
| 108 | - $this->getTrapApi()->setStatusMaster(); |
|
| 109 | - } |
|
| 110 | - else |
|
| 111 | - { |
|
| 112 | - if ($this->getTrapApi()->getStatus() != TrapApi::MASTER) |
|
| 113 | - { |
|
| 114 | - // Get options to connect to API |
|
| 115 | - $IP = $port = $user = $pass = null; |
|
| 116 | - $this->getOptionIfSet($trapConfig,'config','masterIP', $IP, ERROR); |
|
| 117 | - $this->getOptionIfSet($trapConfig,'config','masterPort', $port, ERROR); |
|
| 118 | - $this->getOptionIfSet($trapConfig,'config','masterUser', $user, ERROR); |
|
| 119 | - $this->getOptionIfSet($trapConfig,'config','masterPass', $pass, ERROR); |
|
| 120 | - $this->getTrapApi()->setParams($IP, $port, $user, $pass); |
|
| 121 | - return; |
|
| 122 | - } |
|
| 123 | - } |
|
| 103 | + $nodeStatus=''; |
|
| 104 | + $this->getOptionIfSet($trapConfig,'config','node', $nodeStatus); |
|
| 105 | + if ($this->getTrapApi()->setStatus($nodeStatus) === FALSE) |
|
| 106 | + { |
|
| 107 | + $this->getLogging()->log('Unknown node status '.$nodeStatus.' : setting to MASTER',WARN); |
|
| 108 | + $this->getTrapApi()->setStatusMaster(); |
|
| 109 | + } |
|
| 110 | + else |
|
| 111 | + { |
|
| 112 | + if ($this->getTrapApi()->getStatus() != TrapApi::MASTER) |
|
| 113 | + { |
|
| 114 | + // Get options to connect to API |
|
| 115 | + $IP = $port = $user = $pass = null; |
|
| 116 | + $this->getOptionIfSet($trapConfig,'config','masterIP', $IP, ERROR); |
|
| 117 | + $this->getOptionIfSet($trapConfig,'config','masterPort', $port, ERROR); |
|
| 118 | + $this->getOptionIfSet($trapConfig,'config','masterUser', $user, ERROR); |
|
| 119 | + $this->getOptionIfSet($trapConfig,'config','masterPass', $pass, ERROR); |
|
| 120 | + $this->getTrapApi()->setParams($IP, $port, $user, $pass); |
|
| 121 | + return; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - // Snmptranslate binary path |
|
| 126 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 125 | + // Snmptranslate binary path |
|
| 126 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
| 127 | 127 | |
| 128 | - // mibs path |
|
| 129 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 128 | + // mibs path |
|
| 129 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
| 130 | 130 | |
| 131 | - // icinga2cmd path |
|
| 132 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 131 | + // icinga2cmd path |
|
| 132 | + $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
| 133 | 133 | |
| 134 | - // table prefix |
|
| 135 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
| 134 | + // table prefix |
|
| 135 | + $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
| 136 | 136 | |
| 137 | - // API options |
|
| 138 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
| 139 | - { |
|
| 140 | - $this->apiUse=true; |
|
| 141 | - // Get API options or throw exception as not configured correctly |
|
| 142 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort,ERROR); |
|
| 143 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername,ERROR); |
|
| 144 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword,ERROR); |
|
| 145 | - } |
|
| 146 | - } |
|
| 137 | + // API options |
|
| 138 | + if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
| 139 | + { |
|
| 140 | + $this->apiUse=true; |
|
| 141 | + // Get API options or throw exception as not configured correctly |
|
| 142 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort,ERROR); |
|
| 143 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername,ERROR); |
|
| 144 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword,ERROR); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Create and setup database class for trap & ido (if no api) db |
|
| 150 | - * @param array $trap_config : ini file array |
|
| 151 | - */ |
|
| 152 | - protected function setupDatabase($trapConfig) |
|
| 153 | - { |
|
| 154 | - // Trap database |
|
| 155 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 156 | - { |
|
| 157 | - $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - $dbTrapName=$trapConfig['config']['database']; |
|
| 161 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 148 | + /** |
|
| 149 | + * Create and setup database class for trap & ido (if no api) db |
|
| 150 | + * @param array $trap_config : ini file array |
|
| 151 | + */ |
|
| 152 | + protected function setupDatabase($trapConfig) |
|
| 153 | + { |
|
| 154 | + // Trap database |
|
| 155 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
| 156 | + { |
|
| 157 | + $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + $dbTrapName=$trapConfig['config']['database']; |
|
| 161 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 162 | 162 | |
| 163 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
| 164 | - { |
|
| 165 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
| 166 | - return; |
|
| 167 | - } |
|
| 168 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 169 | - { |
|
| 170 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 171 | - return; |
|
| 172 | - } |
|
| 163 | + if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
| 164 | + { |
|
| 165 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
| 166 | + return; |
|
| 167 | + } |
|
| 168 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 169 | + { |
|
| 170 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 171 | + return; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
| 174 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
| 175 | 175 | |
| 176 | - //TODO enable this again when API queries are all done : $this->logging->log("API Use : ".print_r($this->apiUse,true),INFO ); |
|
| 176 | + //TODO enable this again when API queries are all done : $this->logging->log("API Use : ".print_r($this->apiUse,true),INFO ); |
|
| 177 | 177 | |
| 178 | - if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
| 178 | + if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
| 179 | 179 | |
| 180 | - // IDO Database |
|
| 181 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 182 | - { |
|
| 183 | - $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 184 | - } |
|
| 185 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 180 | + // IDO Database |
|
| 181 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 182 | + { |
|
| 183 | + $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
| 184 | + } |
|
| 185 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 186 | 186 | |
| 187 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 188 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 189 | - { |
|
| 190 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 191 | - return; |
|
| 192 | - } |
|
| 187 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 188 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 189 | + { |
|
| 190 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
| 191 | + return; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 195 | - } |
|
| 194 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | 197 | } |
| 198 | 198 | \ No newline at end of file |