@@ -21,104 +21,104 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class NetworkRule extends PluginTemplate |
| 23 | 23 | { |
| 24 | - /** @var string $description Description of plugin */ |
|
| 25 | - public $description='Network functions to use into rules |
|
| 24 | + /** @var string $description Description of plugin */ |
|
| 25 | + public $description='Network functions to use into rules |
|
| 26 | 26 | test test test'; |
| 27 | 27 | |
| 28 | - /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | - * If no functions are declared, set to empty array |
|
| 30 | - * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | - * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | - * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | - */ |
|
| 34 | - public $functions=array( |
|
| 35 | - 'inNetwork' => array( // The name of the function in rules |
|
| 36 | - 'function' => 'isInNetwork', // Name of the function |
|
| 37 | - 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | - 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 28 | + /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | + * If no functions are declared, set to empty array |
|
| 30 | + * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | + * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | + * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | + */ |
|
| 34 | + public $functions=array( |
|
| 35 | + 'inNetwork' => array( // The name of the function in rules |
|
| 36 | + 'function' => 'isInNetwork', // Name of the function |
|
| 37 | + 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | + 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 39 | 39 | Does not work with IPV6' // Description (can be multiline). |
| 40 | - ), |
|
| 41 | - 'test' => array( // The name of the function in rules |
|
| 42 | - 'function' => 'testParam', // Name of the function |
|
| 43 | - 'params' => '<boolean to return as string>', // parameters description |
|
| 44 | - 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
| 45 | - ) |
|
| 46 | - ); |
|
| 40 | + ), |
|
| 41 | + 'test' => array( // The name of the function in rules |
|
| 42 | + 'function' => 'testParam', // Name of the function |
|
| 43 | + 'params' => '<boolean to return as string>', // parameters description |
|
| 44 | + 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
| 45 | + ) |
|
| 46 | + ); |
|
| 47 | 47 | |
| 48 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 49 | - public $catchAllTraps=false; |
|
| 48 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 49 | + public $catchAllTraps=false; |
|
| 50 | 50 | |
| 51 | - /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 52 | - public $processTraps=false; |
|
| 51 | + /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 52 | + public $processTraps=false; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 56 | - * @throws \Exception |
|
| 57 | - * @return \Trapdirector\Plugins\NetworkRule |
|
| 58 | - */ |
|
| 59 | - function __construct() |
|
| 60 | - { |
|
| 61 | - $this->name=basename(__FILE__,'.php'); |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 54 | + /** |
|
| 55 | + * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 56 | + * @throws \Exception |
|
| 57 | + * @return \Trapdirector\Plugins\NetworkRule |
|
| 58 | + */ |
|
| 59 | + function __construct() |
|
| 60 | + { |
|
| 61 | + $this->name=basename(__FILE__,'.php'); |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Function called by trapdirector if found in rules |
|
| 67 | - * Parameters check has to be done in function. |
|
| 68 | - * @param array $params Function parameters |
|
| 69 | - * @throws Exception |
|
| 70 | - * @return bool Evaluation |
|
| 71 | - */ |
|
| 72 | - public function isInNetwork(array $params) : bool |
|
| 73 | - { |
|
| 74 | - // Check param numbers and thrown exception if not correct. |
|
| 75 | - if (count($params)!=3) |
|
| 76 | - { |
|
| 77 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * Function called by trapdirector if found in rules |
|
| 67 | + * Parameters check has to be done in function. |
|
| 68 | + * @param array $params Function parameters |
|
| 69 | + * @throws Exception |
|
| 70 | + * @return bool Evaluation |
|
| 71 | + */ |
|
| 72 | + public function isInNetwork(array $params) : bool |
|
| 73 | + { |
|
| 74 | + // Check param numbers and thrown exception if not correct. |
|
| 75 | + if (count($params)!=3) |
|
| 76 | + { |
|
| 77 | + throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $ip = $params[0]; |
|
| 81 | - $net = $params[1]; |
|
| 82 | - $masq = $params[2]; |
|
| 80 | + $ip = $params[0]; |
|
| 81 | + $net = $params[1]; |
|
| 82 | + $masq = $params[2]; |
|
| 83 | 83 | |
| 84 | 84 | |
| 85 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 85 | + $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 86 | 86 | |
| 87 | - $ip2 = ip2long($ip); |
|
| 88 | - $net2 = ip2long($net); |
|
| 87 | + $ip2 = ip2long($ip); |
|
| 88 | + $net2 = ip2long($net); |
|
| 89 | 89 | |
| 90 | - if ($ip2 === false ) |
|
| 91 | - { |
|
| 92 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 93 | - throw new Exception('Invalid IP'); |
|
| 94 | - } |
|
| 95 | - if ($net2 === false) |
|
| 96 | - { |
|
| 97 | - $this->log('Invalid network',WARN); |
|
| 98 | - throw new Exception('Invalid net'); |
|
| 99 | - } |
|
| 100 | - if ($masq<1 || $masq > 32) |
|
| 101 | - { |
|
| 102 | - $this->log('Invalid masq',WARN); |
|
| 103 | - throw new Exception('Invalid net masq'); |
|
| 104 | - } |
|
| 105 | - // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 90 | + if ($ip2 === false ) |
|
| 91 | + { |
|
| 92 | + $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 93 | + throw new Exception('Invalid IP'); |
|
| 94 | + } |
|
| 95 | + if ($net2 === false) |
|
| 96 | + { |
|
| 97 | + $this->log('Invalid network',WARN); |
|
| 98 | + throw new Exception('Invalid net'); |
|
| 99 | + } |
|
| 100 | + if ($masq<1 || $masq > 32) |
|
| 101 | + { |
|
| 102 | + $this->log('Invalid masq',WARN); |
|
| 103 | + throw new Exception('Invalid net masq'); |
|
| 104 | + } |
|
| 105 | + // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 106 | 106 | |
| 107 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 108 | - $masq = ~ $masq; |
|
| 109 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 107 | + $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 108 | + $masq = ~ $masq; |
|
| 109 | + return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 110 | 110 | |
| 111 | - } |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - public function testParam(array $param) |
|
| 114 | - { |
|
| 115 | - if (count($param)!=1) |
|
| 116 | - { |
|
| 117 | - throw new Exception('Invalid number of parameters : ' . count($param)); |
|
| 118 | - } |
|
| 119 | - if ($param[0] == 'true') return true; |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 113 | + public function testParam(array $param) |
|
| 114 | + { |
|
| 115 | + if (count($param)!=1) |
|
| 116 | + { |
|
| 117 | + throw new Exception('Invalid number of parameters : ' . count($param)); |
|
| 118 | + } |
|
| 119 | + if ($param[0] == 'true') return true; |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | |
@@ -43,8 +43,8 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | |
| 45 | 45 | /** Get instance of TrapModuleConfig class |
| 46 | - * @return TrapModuleConfig |
|
| 47 | - */ |
|
| 46 | + * @return TrapModuleConfig |
|
| 47 | + */ |
|
| 48 | 48 | public function getModuleConfig() |
| 49 | 49 | { |
| 50 | 50 | if ($this->moduleConfig == Null) |
@@ -73,12 +73,12 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | public function getTrapHostListTable() |
| 75 | 75 | { |
| 76 | - if ($this->trapTableHostList == Null) |
|
| 76 | + if ($this->trapTableHostList == Null) |
|
| 77 | 77 | { |
| 78 | - $this->trapTableHostList = new TrapTableHostList(); |
|
| 79 | - $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
| 80 | - } |
|
| 81 | - return $this->trapTableHostList; |
|
| 78 | + $this->trapTableHostList = new TrapTableHostList(); |
|
| 79 | + $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
| 80 | + } |
|
| 81 | + return $this->trapTableHostList; |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | public function getHandlerListTable() |
@@ -92,11 +92,11 @@ discard block |
||
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | /** Get Database connexion |
| 95 | - * @param $DBname string DB name in resource.ini_ge |
|
| 96 | - * @param $test bool if set to true, returns error code and not database |
|
| 97 | - * @param $test_version bool if set to flase, does not test database version of trapDB |
|
| 98 | - * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 99 | - */ |
|
| 95 | + * @param $DBname string DB name in resource.ini_ge |
|
| 96 | + * @param $test bool if set to true, returns error code and not database |
|
| 97 | + * @param $test_version bool if set to flase, does not test database version of trapDB |
|
| 98 | + * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 99 | + */ |
|
| 100 | 100 | public function getDbByName($DBname,$test=false,$test_version=true) |
| 101 | 101 | { |
| 102 | 102 | try |
@@ -181,20 +181,20 @@ discard block |
||
| 181 | 181 | |
| 182 | 182 | if ( ! $dbresource ) |
| 183 | 183 | { |
| 184 | - if ($test) return array(1,'No database in config.ini'); |
|
| 185 | - $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
| 186 | - return null; |
|
| 184 | + if ($test) return array(1,'No database in config.ini'); |
|
| 185 | + $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
| 186 | + return null; |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | try |
| 190 | 190 | { |
| 191 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 191 | + $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 192 | 192 | } |
| 193 | 193 | catch (Exception $e) |
| 194 | 194 | { |
| 195 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
| 196 | - $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
| 197 | - return null; |
|
| 195 | + if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
| 196 | + $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
| 197 | + return null; |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | if ($test == false) |
@@ -205,13 +205,13 @@ discard block |
||
| 205 | 205 | |
| 206 | 206 | try |
| 207 | 207 | { |
| 208 | - $query = $dbconn->select() |
|
| 209 | - ->from('icinga_dbversion',array('version')); |
|
| 210 | - $version=$dbconn->fetchRow($query); |
|
| 211 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
| 212 | - { |
|
| 213 | - return array(4,"$dbresource does not look like an IDO database"); |
|
| 214 | - } |
|
| 208 | + $query = $dbconn->select() |
|
| 209 | + ->from('icinga_dbversion',array('version')); |
|
| 210 | + $version=$dbconn->fetchRow($query); |
|
| 211 | + if ( ($version == null) || ! property_exists($version,'version') ) |
|
| 212 | + { |
|
| 213 | + return array(4,"$dbresource does not look like an IDO database"); |
|
| 214 | + } |
|
| 215 | 215 | } |
| 216 | 216 | catch (Exception $e) |
| 217 | 217 | { |
@@ -221,15 +221,15 @@ discard block |
||
| 221 | 221 | return array(0,''); |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
| 225 | - { |
|
| 226 | - $limit = $this->params->get('limit', $limit); |
|
| 227 | - $page = $this->params->get('page', $offset); |
|
| 224 | + protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
| 225 | + { |
|
| 226 | + $limit = $this->params->get('limit', $limit); |
|
| 227 | + $page = $this->params->get('page', $offset); |
|
| 228 | 228 | |
| 229 | - $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
| 229 | + $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
| 230 | 230 | |
| 231 | - return $paginatable; |
|
| 232 | - } |
|
| 231 | + return $paginatable; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | 234 | public function displayExitError($source,$message) |
| 235 | 235 | { // TODO : check better ways to transmit data (with POST ?) |
@@ -238,33 +238,33 @@ discard block |
||
| 238 | 238 | |
| 239 | 239 | protected function checkReadPermission() |
| 240 | 240 | { |
| 241 | - if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
| 242 | - $this->displayExitError('Permissions','No permission fo view content'); |
|
| 243 | - } |
|
| 241 | + if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
| 242 | + $this->displayExitError('Permissions','No permission fo view content'); |
|
| 243 | + } |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | protected function checkConfigPermission() |
| 247 | 247 | { |
| 248 | - if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
| 249 | - $this->displayExitError('Permissions','No permission fo configure'); |
|
| 250 | - } |
|
| 248 | + if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
| 249 | + $this->displayExitError('Permissions','No permission fo configure'); |
|
| 250 | + } |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * Check if user has write permission |
|
| 255 | - * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
| 256 | - * @return boolean : user has permission |
|
| 257 | - */ |
|
| 253 | + /** |
|
| 254 | + * Check if user has write permission |
|
| 255 | + * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
| 256 | + * @return boolean : user has permission |
|
| 257 | + */ |
|
| 258 | 258 | protected function checkModuleConfigPermission($check=0) |
| 259 | 259 | { |
| 260 | - if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
| 261 | - if ($check == 0) |
|
| 262 | - { |
|
| 263 | - $this->displayExitError('Permissions','No permission fo configure module'); |
|
| 264 | - } |
|
| 265 | - return false; |
|
| 266 | - } |
|
| 267 | - return true; |
|
| 260 | + if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
| 261 | + if ($check == 0) |
|
| 262 | + { |
|
| 263 | + $this->displayExitError('Permissions','No permission fo configure module'); |
|
| 264 | + } |
|
| 265 | + return false; |
|
| 266 | + } |
|
| 267 | + return true; |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | /************************* Trap class get **********************/ |
@@ -284,8 +284,8 @@ discard block |
||
| 284 | 284 | /************************** MIB related **************************/ |
| 285 | 285 | |
| 286 | 286 | /** Get MIBLoader class |
| 287 | - * @return MIBLoader class |
|
| 288 | - */ |
|
| 287 | + * @return MIBLoader class |
|
| 288 | + */ |
|
| 289 | 289 | protected function getMIB() |
| 290 | 290 | { |
| 291 | 291 | if ($this->MIBData == null) |
@@ -303,9 +303,9 @@ discard block |
||
| 303 | 303 | /************************** Database queries *******************/ |
| 304 | 304 | |
| 305 | 305 | /** Get host(s) by IP (v4 or v6) or by name in IDO database |
| 306 | - * does not catch exceptions |
|
| 307 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 308 | - */ |
|
| 306 | + * does not catch exceptions |
|
| 307 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 308 | + */ |
|
| 309 | 309 | protected function getHostByIP($ip) |
| 310 | 310 | { |
| 311 | 311 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -324,9 +324,9 @@ discard block |
||
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | /** Get host(s) by name in IDO database |
| 327 | - * does not catch exceptions |
|
| 328 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 329 | - */ |
|
| 327 | + * does not catch exceptions |
|
| 328 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 329 | + */ |
|
| 330 | 330 | protected function getHostByName($name) |
| 331 | 331 | { |
| 332 | 332 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -345,9 +345,9 @@ discard block |
||
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /** Get host groups by name in IDO database |
| 348 | - * does not catch exceptions |
|
| 349 | - * @return array of objects ( name, id (object_id), display_name) |
|
| 350 | - */ |
|
| 348 | + * does not catch exceptions |
|
| 349 | + * @return array of objects ( name, id (object_id), display_name) |
|
| 350 | + */ |
|
| 351 | 351 | protected function getHostGroupByName($ip) |
| 352 | 352 | { |
| 353 | 353 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -367,9 +367,9 @@ discard block |
||
| 367 | 367 | |
| 368 | 368 | |
| 369 | 369 | /** Get host IP (v4 and v6) by name in IDO database |
| 370 | - * does not catch exceptions |
|
| 371 | - * @return array ( name, display_name, ip4, ip6) |
|
| 372 | - */ |
|
| 370 | + * does not catch exceptions |
|
| 371 | + * @return array ( name, display_name, ip4, ip6) |
|
| 372 | + */ |
|
| 373 | 373 | protected function getHostInfoByID($id) |
| 374 | 374 | { |
| 375 | 375 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -388,9 +388,9 @@ discard block |
||
| 388 | 388 | |
| 389 | 389 | |
| 390 | 390 | /** Get host by objectid in IDO database |
| 391 | - * does not catch exceptions |
|
| 392 | - * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
| 393 | - */ |
|
| 391 | + * does not catch exceptions |
|
| 392 | + * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
| 393 | + */ |
|
| 394 | 394 | protected function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
| 395 | 395 | { |
| 396 | 396 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -408,10 +408,10 @@ discard block |
||
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | /** Get services from object ( host_object_id) in IDO database |
| 411 | - * does not catch exceptions |
|
| 412 | - * @param $id int object_id |
|
| 413 | - * @return array display_name (of service), service_object_id |
|
| 414 | - */ |
|
| 411 | + * does not catch exceptions |
|
| 412 | + * @param $id int object_id |
|
| 413 | + * @return array display_name (of service), service_object_id |
|
| 414 | + */ |
|
| 415 | 415 | protected function getServicesByHostid($id) |
| 416 | 416 | { |
| 417 | 417 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -430,11 +430,11 @@ discard block |
||
| 430 | 430 | } |
| 431 | 431 | |
| 432 | 432 | /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
| 433 | - * gets all hosts in hostgroup and return common services |
|
| 434 | - * does not catch exceptions |
|
| 435 | - * @param $id int object_id |
|
| 436 | - * @return array display_name (of service), service_object_id |
|
| 437 | - */ |
|
| 433 | + * gets all hosts in hostgroup and return common services |
|
| 434 | + * does not catch exceptions |
|
| 435 | + * @param $id int object_id |
|
| 436 | + * @return array display_name (of service), service_object_id |
|
| 437 | + */ |
|
| 438 | 438 | protected function getServicesByHostGroupid($id) |
| 439 | 439 | { |
| 440 | 440 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -472,7 +472,7 @@ discard block |
||
| 472 | 472 | //print_r($common_services); |
| 473 | 473 | foreach (array_keys($common_services) as $key) |
| 474 | 474 | { |
| 475 | - if ($common_services[$key]['num'] == $num_hosts) |
|
| 475 | + if ($common_services[$key]['num'] == $num_hosts) |
|
| 476 | 476 | { |
| 477 | 477 | array_push($result,array($key,$common_services[$key]['name'])); |
| 478 | 478 | } |
@@ -482,11 +482,11 @@ discard block |
||
| 482 | 482 | } |
| 483 | 483 | |
| 484 | 484 | /** Get services object id by host name / service name in IDO database |
| 485 | - * does not catch exceptions |
|
| 486 | - * @param $hostname string host name |
|
| 487 | - * @param $name string service name |
|
| 488 | - * @return array service id |
|
| 489 | - */ |
|
| 485 | + * does not catch exceptions |
|
| 486 | + * @param $hostname string host name |
|
| 487 | + * @param $name string service name |
|
| 488 | + * @return array service id |
|
| 489 | + */ |
|
| 490 | 490 | protected function getServiceIDByName($hostname,$name) |
| 491 | 491 | { |
| 492 | 492 | $db = $this->getIdoDb()->getConnection(); |
@@ -509,10 +509,10 @@ discard block |
||
| 509 | 509 | } |
| 510 | 510 | |
| 511 | 511 | /** Get object name from object_id in IDO database |
| 512 | - * does not catch exceptions |
|
| 513 | - * @param int $id object_id (default to null, used first if not null) |
|
| 514 | - * @return array name1 (host) name2 (service) |
|
| 515 | - */ |
|
| 512 | + * does not catch exceptions |
|
| 513 | + * @param int $id object_id (default to null, used first if not null) |
|
| 514 | + * @return array name1 (host) name2 (service) |
|
| 515 | + */ |
|
| 516 | 516 | protected function getObjectNameByid($id) |
| 517 | 517 | { |
| 518 | 518 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -528,9 +528,9 @@ discard block |
||
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | /** Add handler rule in traps DB |
| 531 | - * @param array $params : array(<db item>=><value>) |
|
| 532 | - * @return int inserted id |
|
| 533 | - */ |
|
| 531 | + * @param array $params : array(<db item>=><value>) |
|
| 532 | + * @return int inserted id |
|
| 533 | + */ |
|
| 534 | 534 | protected function addHandlerRule($params) |
| 535 | 535 | { |
| 536 | 536 | // TODO Check for rule consistency |
@@ -552,10 +552,10 @@ discard block |
||
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** Update handler rule in traps DB |
| 555 | - * @param array $params : (<db item>=><value>) |
|
| 556 | - * @param integer $ruleID : rule id in db |
|
| 557 | - * @return array affected rows |
|
| 558 | - */ |
|
| 555 | + * @param array $params : (<db item>=><value>) |
|
| 556 | + * @param integer $ruleID : rule id in db |
|
| 557 | + * @return array affected rows |
|
| 558 | + */ |
|
| 559 | 559 | protected function updateHandlerRule($params,$ruleID) |
| 560 | 560 | { |
| 561 | 561 | // TODO Check for rule consistency |
@@ -573,8 +573,8 @@ discard block |
||
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | /** Delete rule by id |
| 576 | - * @param int $ruleID rule id |
|
| 577 | - */ |
|
| 576 | + * @param int $ruleID rule id |
|
| 577 | + */ |
|
| 578 | 578 | protected function deleteRule($ruleID) |
| 579 | 579 | { |
| 580 | 580 | if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
@@ -588,9 +588,9 @@ discard block |
||
| 588 | 588 | } |
| 589 | 589 | |
| 590 | 590 | /** Delete trap by ip & oid |
| 591 | - * @param $ip string source IP (v4 or v6) |
|
| 592 | - * @param $oid string oid |
|
| 593 | - */ |
|
| 591 | + * @param $ip string source IP (v4 or v6) |
|
| 592 | + * @param $oid string oid |
|
| 593 | + */ |
|
| 594 | 594 | protected function deleteTrap($ip,$oid) |
| 595 | 595 | { |
| 596 | 596 | |
@@ -616,9 +616,9 @@ discard block |
||
| 616 | 616 | |
| 617 | 617 | |
| 618 | 618 | /** count trap by ip & oid |
| 619 | - * @param $ip string source IP (v4 or v6) |
|
| 620 | - * @param $oid string oid |
|
| 621 | - */ |
|
| 619 | + * @param $ip string source IP (v4 or v6) |
|
| 620 | + * @param $oid string oid |
|
| 621 | + */ |
|
| 622 | 622 | protected function countTrap($ip,$oid) |
| 623 | 623 | { |
| 624 | 624 | |
@@ -644,8 +644,8 @@ discard block |
||
| 644 | 644 | } |
| 645 | 645 | |
| 646 | 646 | /** get configuration value |
| 647 | - * @param string $element : configuration name in db |
|
| 648 | - */ |
|
| 647 | + * @param string $element : configuration name in db |
|
| 648 | + */ |
|
| 649 | 649 | protected function getDBConfigValue($element) |
| 650 | 650 | { |
| 651 | 651 | |
@@ -676,9 +676,9 @@ discard block |
||
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | /** add configuration value |
| 679 | - * @param string $element : name of config element |
|
| 680 | - * @param string $value : value |
|
| 681 | - */ |
|
| 679 | + * @param string $element : name of config element |
|
| 680 | + * @param string $value : value |
|
| 681 | + */ |
|
| 682 | 682 | |
| 683 | 683 | protected function addDBConfigValue($element,$value) |
| 684 | 684 | { |
@@ -696,9 +696,9 @@ discard block |
||
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | /** set configuration value |
| 699 | - * @param string $element : name of config element |
|
| 700 | - * @param string $value : value |
|
| 701 | - */ |
|
| 699 | + * @param string $element : name of config element |
|
| 700 | + * @param string $value : value |
|
| 701 | + */ |
|
| 702 | 702 | protected function setDBConfigValue($element,$value) |
| 703 | 703 | { |
| 704 | 704 | |
@@ -712,13 +712,13 @@ discard block |
||
| 712 | 712 | } |
| 713 | 713 | |
| 714 | 714 | /** Check if director is installed |
| 715 | - * @return bool true/false |
|
| 716 | - */ |
|
| 715 | + * @return bool true/false |
|
| 716 | + */ |
|
| 717 | 717 | protected function isDirectorInstalled() |
| 718 | 718 | { |
| 719 | - $output=array(); |
|
| 720 | - exec('icingacli module list',$output); |
|
| 721 | - foreach ($output as $line) |
|
| 719 | + $output=array(); |
|
| 720 | + exec('icingacli module list',$output); |
|
| 721 | + foreach ($output as $line) |
|
| 722 | 722 | { |
| 723 | 723 | if (preg_match('/^director .*enabled/',$line)) |
| 724 | 724 | { |
@@ -15,180 +15,180 @@ |
||
| 15 | 15 | class UIDatabase |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @var TrapsController $trapController TrapController 'parent' class */ |
|
| 19 | - private $trapController; |
|
| 18 | + /** @var TrapsController $trapController TrapController 'parent' class */ |
|
| 19 | + private $trapController; |
|
| 20 | 20 | |
| 21 | - /** @var Zend_Db_Adapter_Abstract $trapDB Trap Database*/ |
|
| 22 | - private $trapDB; |
|
| 21 | + /** @var Zend_Db_Adapter_Abstract $trapDB Trap Database*/ |
|
| 22 | + private $trapDB; |
|
| 23 | 23 | |
| 24 | - /** @var Zend_Db_Adapter_Abstract $trapDB Icinga IDO database*/ |
|
| 25 | - private $idoDB; |
|
| 24 | + /** @var Zend_Db_Adapter_Abstract $trapDB Icinga IDO database*/ |
|
| 25 | + private $idoDB; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * |
|
| 29 | - * @param TrapsController $trapCtrl |
|
| 30 | - */ |
|
| 31 | - function __construct(TrapsController $trapCtrl) |
|
| 32 | - { |
|
| 33 | - $this->trapController=$trapCtrl; |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + * @param TrapsController $trapCtrl |
|
| 30 | + */ |
|
| 31 | + function __construct(TrapsController $trapCtrl) |
|
| 32 | + { |
|
| 33 | + $this->trapController=$trapCtrl; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Test if database version >= min database version |
|
| 38 | - * |
|
| 39 | - * @param \Zend_Db_Adapter_Abstract $dbConn |
|
| 40 | - * @param int $min Minimum version |
|
| 41 | - * @param bool $test Test mode |
|
| 42 | - * @param string $DBname Name of DB |
|
| 43 | - * @return NULL|array |
|
| 44 | - */ |
|
| 45 | - protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
| 46 | - { |
|
| 47 | - try |
|
| 48 | - { |
|
| 49 | - $query = $dbAdapter->select() |
|
| 50 | - ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
| 51 | - ->where('name=\'db_version\''); |
|
| 52 | - $version=$dbAdapter->fetchRow($query); |
|
| 53 | - if ( ($version == null) || ! property_exists($version,'value') ) |
|
| 54 | - { |
|
| 55 | - if ($test) return array(4,$DBname); |
|
| 56 | - $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 57 | - return null; |
|
| 58 | - } |
|
| 59 | - if ($version->value < $min) |
|
| 60 | - { |
|
| 61 | - if ($test) return array(5,$version->value,$min); |
|
| 62 | - $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
|
| 63 | - return null; |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - catch (Exception $e) |
|
| 67 | - { |
|
| 68 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
| 69 | - $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 70 | - } |
|
| 71 | - return null; |
|
| 72 | - } |
|
| 36 | + /** |
|
| 37 | + * Test if database version >= min database version |
|
| 38 | + * |
|
| 39 | + * @param \Zend_Db_Adapter_Abstract $dbConn |
|
| 40 | + * @param int $min Minimum version |
|
| 41 | + * @param bool $test Test mode |
|
| 42 | + * @param string $DBname Name of DB |
|
| 43 | + * @return NULL|array |
|
| 44 | + */ |
|
| 45 | + protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname) |
|
| 46 | + { |
|
| 47 | + try |
|
| 48 | + { |
|
| 49 | + $query = $dbAdapter->select() |
|
| 50 | + ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value') |
|
| 51 | + ->where('name=\'db_version\''); |
|
| 52 | + $version=$dbAdapter->fetchRow($query); |
|
| 53 | + if ( ($version == null) || ! property_exists($version,'value') ) |
|
| 54 | + { |
|
| 55 | + if ($test) return array(4,$DBname); |
|
| 56 | + $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 57 | + return null; |
|
| 58 | + } |
|
| 59 | + if ($version->value < $min) |
|
| 60 | + { |
|
| 61 | + if ($test) return array(5,$version->value,$min); |
|
| 62 | + $this->trapController->redirectNow('trapdirector/settings?dberror=5'); |
|
| 63 | + return null; |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + catch (Exception $e) |
|
| 67 | + { |
|
| 68 | + if ($test) return array(3,$DBname,$e->getMessage()); |
|
| 69 | + $this->trapController->redirectNow('trapdirector/settings?dberror=4'); |
|
| 70 | + } |
|
| 71 | + return null; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** Get Database connexion |
|
| 75 | - * @param $DBname string DB name in resource.ini_ge |
|
| 76 | - * @param $test bool if set to true, returns error code and not database |
|
| 77 | - * @param $test_version bool if set to flase, does not test database version of trapDB |
|
| 78 | - * @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 79 | - */ |
|
| 80 | - protected function getDbByName($DBname,$test=false,$test_version=true) |
|
| 81 | - { |
|
| 82 | - try |
|
| 83 | - { |
|
| 84 | - $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
| 85 | - } |
|
| 86 | - catch (Exception $e) |
|
| 87 | - { |
|
| 88 | - if ($test) return array(2,$DBname); |
|
| 89 | - $this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 74 | + /** Get Database connexion |
|
| 75 | + * @param $DBname string DB name in resource.ini_ge |
|
| 76 | + * @param $test bool if set to true, returns error code and not database |
|
| 77 | + * @param $test_version bool if set to flase, does not test database version of trapDB |
|
| 78 | + * @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 79 | + */ |
|
| 80 | + protected function getDbByName($DBname,$test=false,$test_version=true) |
|
| 81 | + { |
|
| 82 | + try |
|
| 83 | + { |
|
| 84 | + $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
| 85 | + } |
|
| 86 | + catch (Exception $e) |
|
| 87 | + { |
|
| 88 | + if ($test) return array(2,$DBname); |
|
| 89 | + $this->trapController->redirectNow('trapdirector/settings?dberror=2'); |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - try |
|
| 94 | - { |
|
| 95 | - $dbAdapter=$dbconn->getDbAdapter(); |
|
| 93 | + try |
|
| 94 | + { |
|
| 95 | + $dbAdapter=$dbconn->getDbAdapter(); |
|
| 96 | 96 | |
| 97 | - } |
|
| 98 | - catch (Exception $e) |
|
| 99 | - { |
|
| 100 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
| 101 | - $this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
|
| 102 | - return null; |
|
| 103 | - } |
|
| 97 | + } |
|
| 98 | + catch (Exception $e) |
|
| 99 | + { |
|
| 100 | + if ($test) return array(3,$DBname,$e->getMessage()); |
|
| 101 | + $this->trapController->redirectNow('trapdirector/settings?dberror=3'); |
|
| 102 | + return null; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - if ($test_version == true) { |
|
| 106 | - $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
|
| 107 | - if ($testRet != null) |
|
| 108 | - { |
|
| 109 | - return $testRet; |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - if ($test) return array(0,''); |
|
| 113 | - return $dbAdapter; |
|
| 114 | - } |
|
| 105 | + if ($test_version == true) { |
|
| 106 | + $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname); |
|
| 107 | + if ($testRet != null) |
|
| 108 | + { |
|
| 109 | + return $testRet; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + if ($test) return array(0,''); |
|
| 113 | + return $dbAdapter; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * Get Trap database |
|
| 118 | - * @param boolean $test |
|
| 119 | - * @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 120 | - */ |
|
| 121 | - public function getDb($test=false) |
|
| 122 | - { |
|
| 123 | - if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
| 116 | + /** |
|
| 117 | + * Get Trap database |
|
| 118 | + * @param boolean $test |
|
| 119 | + * @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 120 | + */ |
|
| 121 | + public function getDb($test=false) |
|
| 122 | + { |
|
| 123 | + if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
| 124 | 124 | |
| 125 | - $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
| 125 | + $dbresource=$this->trapController->Config()->get('config', 'database'); |
|
| 126 | 126 | |
| 127 | - if ( ! $dbresource ) |
|
| 128 | - { |
|
| 129 | - if ($test) return array(1,''); |
|
| 130 | - $this->redirectNow('trapdirector/settings?dberror=1'); |
|
| 131 | - return null; |
|
| 132 | - } |
|
| 133 | - $retDB=$this->getDbByName($dbresource,$test,true); |
|
| 127 | + if ( ! $dbresource ) |
|
| 128 | + { |
|
| 129 | + if ($test) return array(1,''); |
|
| 130 | + $this->redirectNow('trapdirector/settings?dberror=1'); |
|
| 131 | + return null; |
|
| 132 | + } |
|
| 133 | + $retDB=$this->getDbByName($dbresource,$test,true); |
|
| 134 | 134 | |
| 135 | - if ($test === true) return $retDB; |
|
| 135 | + if ($test === true) return $retDB; |
|
| 136 | 136 | |
| 137 | - $this->trapDB=$retDB; |
|
| 138 | - return $this->trapDB; |
|
| 139 | - } |
|
| 137 | + $this->trapDB=$retDB; |
|
| 138 | + return $this->trapDB; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Get IDO Database |
|
| 143 | - * @param boolean $test |
|
| 144 | - * @return Zend_Db_Adapter_Abstract|array]|NULL if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 145 | - */ |
|
| 146 | - public function getIdoDb($test=false) |
|
| 147 | - { |
|
| 148 | - if ($this->idoDB != null && $test = false) return $this->idoDB; |
|
| 149 | - // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
| 150 | - $dbresource=$this->Config()->get('config', 'IDOdatabase');; |
|
| 141 | + /** |
|
| 142 | + * Get IDO Database |
|
| 143 | + * @param boolean $test |
|
| 144 | + * @return Zend_Db_Adapter_Abstract|array]|NULL if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
| 145 | + */ |
|
| 146 | + public function getIdoDb($test=false) |
|
| 147 | + { |
|
| 148 | + if ($this->idoDB != null && $test = false) return $this->idoDB; |
|
| 149 | + // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
|
| 150 | + $dbresource=$this->Config()->get('config', 'IDOdatabase');; |
|
| 151 | 151 | |
| 152 | - if ( ! $dbresource ) |
|
| 153 | - { |
|
| 154 | - if ($test) return array(1,'No database in config.ini'); |
|
| 155 | - $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
| 156 | - return null; |
|
| 157 | - } |
|
| 152 | + if ( ! $dbresource ) |
|
| 153 | + { |
|
| 154 | + if ($test) return array(1,'No database in config.ini'); |
|
| 155 | + $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
| 156 | + return null; |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - try |
|
| 160 | - { |
|
| 161 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 162 | - } |
|
| 163 | - catch (Exception $e) |
|
| 164 | - { |
|
| 165 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
| 166 | - $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
| 167 | - return null; |
|
| 168 | - } |
|
| 159 | + try |
|
| 160 | + { |
|
| 161 | + $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
| 162 | + } |
|
| 163 | + catch (Exception $e) |
|
| 164 | + { |
|
| 165 | + if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
| 166 | + $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
| 167 | + return null; |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - if ($test == false) |
|
| 171 | - { |
|
| 172 | - $this->idoDB = $dbconn->getDbAdapter(); |
|
| 173 | - return $this->idoDB; |
|
| 174 | - } |
|
| 170 | + if ($test == false) |
|
| 171 | + { |
|
| 172 | + $this->idoDB = $dbconn->getDbAdapter(); |
|
| 173 | + return $this->idoDB; |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - try |
|
| 177 | - { |
|
| 178 | - $query = $dbconn->select() |
|
| 179 | - ->from('icinga_dbversion',array('version')); |
|
| 180 | - $version=$dbconn->fetchRow($query); |
|
| 181 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
| 182 | - { |
|
| 183 | - return array(4,"$dbresource does not look like an IDO database"); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - catch (Exception $e) |
|
| 187 | - { |
|
| 188 | - return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
|
| 189 | - } |
|
| 176 | + try |
|
| 177 | + { |
|
| 178 | + $query = $dbconn->select() |
|
| 179 | + ->from('icinga_dbversion',array('version')); |
|
| 180 | + $version=$dbconn->fetchRow($query); |
|
| 181 | + if ( ($version == null) || ! property_exists($version,'version') ) |
|
| 182 | + { |
|
| 183 | + return array(4,"$dbresource does not look like an IDO database"); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + catch (Exception $e) |
|
| 187 | + { |
|
| 188 | + return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - return array(0,''); |
|
| 192 | - } |
|
| 191 | + return array(0,''); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | 194 | } |
| 195 | 195 | \ No newline at end of file |