| Total Complexity | 45 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like IcingaApiBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IcingaApiBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class IcingaApiBase |
||
| 10 | { |
||
| 11 | protected $version = 'v1'; //< icinga2 api version |
||
| 12 | |||
| 13 | protected $host; //< icinga2 host name or IP |
||
| 14 | protected $port; //< icinga2 api port |
||
| 15 | |||
| 16 | protected $user; //< user name |
||
| 17 | protected $pass; //< user password |
||
| 18 | protected $usercert; //< user key for certificate auth (NOT IMPLEMENTED) |
||
| 19 | protected $authmethod='pass'; //< Authentication : 'pass' or 'cert' |
||
| 20 | |||
| 21 | protected $queryURL=array( |
||
| 22 | 'host' => 'objects/hosts', |
||
| 23 | 'hostgroup' => 'objects/hostgroups', |
||
| 24 | 'service' => 'objects/services' |
||
| 25 | ); |
||
| 26 | |||
| 27 | protected $curl; |
||
| 28 | // http://php.net/manual/de/function.json-last-error.php#119985 |
||
| 29 | protected $errorReference = [ |
||
| 30 | JSON_ERROR_NONE => 'No error has occurred.', |
||
| 31 | JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
||
| 32 | JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
||
| 33 | JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.', |
||
| 34 | JSON_ERROR_SYNTAX => 'Syntax error.', |
||
| 35 | JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.', |
||
| 36 | JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded.', |
||
| 37 | JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.', |
||
| 38 | JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.', |
||
| 39 | ]; |
||
| 40 | const JSON_UNKNOWN_ERROR = 'Unknown error.'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Creates Icinga2API object |
||
| 44 | * |
||
| 45 | * @param string $host host name or IP |
||
| 46 | * @param number $port API port |
||
| 47 | */ |
||
| 48 | public function __construct($host, $port = 5665) |
||
| 52 | } |
||
| 53 | /** |
||
| 54 | * Set user & pass |
||
| 55 | * @param string $user |
||
| 56 | * @param string $pass |
||
| 57 | */ |
||
| 58 | public function setCredentials($user,$pass) |
||
| 59 | { |
||
| 60 | $this->user=$user; |
||
| 61 | $this->pass=$pass; |
||
| 62 | $this->authmethod='pass'; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set user & certificate (NOT IMPLEMENTED @throws RuntimeException) |
||
| 67 | * @param string $user |
||
| 68 | * @param string $usercert |
||
| 69 | */ |
||
| 70 | public function setCredentialskey($user,$usercert) |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Test API connection |
||
| 80 | * @param array $permissions : check permissions if not null or empty |
||
| 81 | * @return array (bool,string) : bool = false is all OK, else true with message |
||
| 82 | * */ |
||
| 83 | public function test(array $permissions) |
||
| 84 | { |
||
| 85 | try |
||
| 86 | { |
||
| 87 | $result=$this->request('GET', "", NULL, NULL); |
||
| 88 | } |
||
| 89 | catch (Exception $e) |
||
| 90 | { |
||
| 91 | return array(true, 'Error with API : '.$e->getMessage()); |
||
| 92 | } |
||
| 93 | //var_dump($result); |
||
| 94 | $permOk=1; |
||
| 95 | $permMissing=''; |
||
| 96 | if ($permissions === NULL || count($permissions) == 0) // If no permission check return OK after connexion |
||
| 97 | { |
||
| 98 | return array(false,'OK'); |
||
| 99 | } |
||
| 100 | if (property_exists($result, 'results') && property_exists($result->results[0], 'permissions')) |
||
| 101 | { |
||
| 102 | |||
| 103 | foreach ( $permissions as $mustPermission) |
||
| 104 | { |
||
| 105 | $curPermOK=0; |
||
| 106 | foreach ( $result->results[0]->permissions as $curPermission) |
||
| 107 | { |
||
| 108 | $curPermission=preg_replace('/\*/','.*',$curPermission); // put * as .* to created a regexp |
||
| 109 | if (preg_match('#'.$curPermission.'#',$mustPermission)) |
||
| 110 | { |
||
| 111 | $curPermOK=1; |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | if ($curPermOK == 0) |
||
| 116 | { |
||
| 117 | $permOk=0; |
||
| 118 | $permMissing=$mustPermission; |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | if ($permOk == 0) |
||
| 123 | { |
||
| 124 | return array(true,'API connection OK, but missing permission : '.$permMissing); |
||
| 125 | } |
||
| 126 | return array(false,'API connection OK'); |
||
| 127 | |||
| 128 | } |
||
| 129 | return array(true,'API connection OK, but cannot get permissions'); |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | protected function url($url) { |
||
| 134 | return sprintf('https://%s:%d/%s/%s', $this->host, $this->port, $this->version, $url); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Create or return curl ressource |
||
| 139 | * @throws Exception |
||
| 140 | * @return resource |
||
| 141 | */ |
||
| 142 | protected function curl() { |
||
| 143 | if ($this->curl === null) { |
||
| 144 | $this->curl = curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
||
| 145 | if ($this->curl === false) { |
||
| 146 | throw new Exception('CURL INIT ERROR'); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | return $this->curl; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Send a passive service check |
||
| 154 | * @param string $host : host name |
||
| 155 | * @param string $service : service name |
||
| 156 | * @param int $state : state of service |
||
| 157 | * @param string $display : service passive check output |
||
| 158 | * @param string $perfdata : performance data as string |
||
| 159 | * @return array (status = true (oK) or false (nok), string message) |
||
| 160 | */ |
||
| 161 | public function serviceCheckResult($host,$service,$state,$display,$perfdata='') |
||
| 162 | { |
||
| 163 | //Send a POST request to the URL endpoint /v1/actions/process-check-result |
||
| 164 | //actions/process-check-result?service=example.localdomain!passive-ping6 |
||
| 165 | $url='actions/process-check-result'; |
||
| 166 | $body=array( |
||
| 167 | "filter" => 'service.name=="'.$service.'" && service.host_name=="'.$host.'"', |
||
| 168 | 'type' => 'Service', |
||
| 169 | "exit_status" => $state, |
||
| 170 | "plugin_output" => $display, |
||
| 171 | "performance_data" => $perfdata |
||
| 172 | ); |
||
| 173 | try |
||
| 174 | { |
||
| 175 | $result=$this->request('POST', $url, null, $body); |
||
| 176 | } catch (Exception $e) |
||
| 177 | { |
||
| 178 | return array(false, $e->getMessage()); |
||
| 179 | } |
||
| 180 | if (property_exists($result,'error') ) |
||
| 181 | { |
||
| 182 | if (property_exists($result,'status')) |
||
| 183 | { |
||
| 184 | $message=$result->status; |
||
| 185 | } |
||
| 186 | else |
||
| 187 | { |
||
| 188 | $message="Unkown status"; |
||
| 189 | } |
||
| 190 | return array(false , 'Ret code ' .$result->error.' : '.$message); |
||
| 191 | } |
||
| 192 | if (property_exists($result, 'results')) |
||
| 193 | { |
||
| 194 | if (isset($result->results[0])) |
||
| 195 | { |
||
| 196 | return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
||
| 197 | } |
||
| 198 | else |
||
| 199 | { |
||
| 200 | return array(false,'Service not found'); |
||
| 201 | } |
||
| 202 | |||
| 203 | } |
||
| 204 | return array(false,'Unkown result, open issue with this : '.print_r($result,true)); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Check 'result' exists and check for errors/status |
||
| 209 | * @param \stdClass $result |
||
| 210 | * @throws Exception |
||
| 211 | */ |
||
| 212 | public function checkResultStatus(\stdClass $result) |
||
| 213 | { |
||
| 214 | if (property_exists($result,'error') ) |
||
| 215 | { |
||
| 216 | if (property_exists($result,'status')) |
||
| 217 | { |
||
| 218 | throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
||
| 219 | } |
||
| 220 | else |
||
| 221 | { |
||
| 222 | throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | if ( ! property_exists($result, 'results')) |
||
| 226 | { |
||
| 227 | throw new Exception('Unkown result'); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Send request to API |
||
| 233 | * @param string $method get/post/... |
||
| 234 | * @param string $url (after /v1/ ) |
||
| 235 | * @param array $headers |
||
| 236 | * @param array $body |
||
| 237 | * @throws Exception |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function request($method, $url, $headers, $body) { |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * |
||
| 284 | * @param string $json json encoded |
||
| 285 | * @throws Exception |
||
| 286 | * @return array json decoded |
||
| 287 | */ |
||
| 288 | protected function fromJsonResult($json) { |
||
| 289 | $result = @json_decode($json); |
||
| 290 | //var_dump($json); |
||
| 291 | if ($result === null) { |
||
| 292 | throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error())); |
||
| 293 | } |
||
| 294 | return $result; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return text error no json error |
||
| 299 | * @param string $errorCode |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | protected function getLastJsonErrorMessage($errorCode) { |
||
| 303 | if (!array_key_exists($errorCode, $this->errorReference)) { |
||
| 304 | return self::JSON_UNKNOWN_ERROR; |
||
| 305 | } |
||
| 306 | return $this->errorReference[$errorCode]; |
||
| 307 | } |
||
| 308 | |||
| 309 | public function standardQuery(string $urlType , string $filter, array $attributes) |
||
| 351 | } |
||
| 352 | |||
| 353 | } |
||
| 354 | |||
| 355 |