@@ -8,363 +8,363 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Icinga2API |
| 10 | 10 | { |
| 11 | - protected $version = 'v1'; //< icinga2 api version |
|
| 11 | + protected $version = 'v1'; //< icinga2 api version |
|
| 12 | 12 | |
| 13 | - protected $host; //< icinga2 host name or IP |
|
| 14 | - protected $port; //< icinga2 api port |
|
| 13 | + protected $host; //< icinga2 host name or IP |
|
| 14 | + protected $port; //< icinga2 api port |
|
| 15 | 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' |
|
| 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 | 20 | |
| 21 | - protected $curl; |
|
| 22 | - // http://php.net/manual/de/function.json-last-error.php#119985 |
|
| 23 | - protected $errorReference = [ |
|
| 24 | - JSON_ERROR_NONE => 'No error has occurred.', |
|
| 25 | - JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
|
| 26 | - JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
|
| 27 | - JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.', |
|
| 28 | - JSON_ERROR_SYNTAX => 'Syntax error.', |
|
| 29 | - JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.', |
|
| 30 | - JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded.', |
|
| 31 | - JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.', |
|
| 32 | - JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.', |
|
| 33 | - ]; |
|
| 34 | - const JSON_UNKNOWN_ERROR = 'Unknown error.'; |
|
| 21 | + protected $curl; |
|
| 22 | + // http://php.net/manual/de/function.json-last-error.php#119985 |
|
| 23 | + protected $errorReference = [ |
|
| 24 | + JSON_ERROR_NONE => 'No error has occurred.', |
|
| 25 | + JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
|
| 26 | + JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
|
| 27 | + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.', |
|
| 28 | + JSON_ERROR_SYNTAX => 'Syntax error.', |
|
| 29 | + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.', |
|
| 30 | + JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded.', |
|
| 31 | + JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.', |
|
| 32 | + JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.', |
|
| 33 | + ]; |
|
| 34 | + const JSON_UNKNOWN_ERROR = 'Unknown error.'; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Creates Icinga2API object |
|
| 38 | - * |
|
| 39 | - * @param string $host host name or IP |
|
| 40 | - * @param number $port API port |
|
| 41 | - */ |
|
| 42 | - public function __construct($host, $port = 5665) |
|
| 43 | - { |
|
| 44 | - $this->host=$host; |
|
| 45 | - $this->port=$port; |
|
| 46 | - } |
|
| 47 | - /** |
|
| 48 | - * Set user & pass |
|
| 49 | - * @param string $user |
|
| 50 | - * @param string $pass |
|
| 51 | - */ |
|
| 52 | - public function setCredentials($user,$pass) |
|
| 53 | - { |
|
| 54 | - $this->user=$user; |
|
| 55 | - $this->pass=$pass; |
|
| 56 | - $this->authmethod='pass'; |
|
| 57 | - } |
|
| 36 | + /** |
|
| 37 | + * Creates Icinga2API object |
|
| 38 | + * |
|
| 39 | + * @param string $host host name or IP |
|
| 40 | + * @param number $port API port |
|
| 41 | + */ |
|
| 42 | + public function __construct($host, $port = 5665) |
|
| 43 | + { |
|
| 44 | + $this->host=$host; |
|
| 45 | + $this->port=$port; |
|
| 46 | + } |
|
| 47 | + /** |
|
| 48 | + * Set user & pass |
|
| 49 | + * @param string $user |
|
| 50 | + * @param string $pass |
|
| 51 | + */ |
|
| 52 | + public function setCredentials($user,$pass) |
|
| 53 | + { |
|
| 54 | + $this->user=$user; |
|
| 55 | + $this->pass=$pass; |
|
| 56 | + $this->authmethod='pass'; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Set user & certificate (NOT IMPLEMENTED @throws RuntimeException) |
|
| 61 | - * @param string $user |
|
| 62 | - * @param string $usercert |
|
| 63 | - */ |
|
| 64 | - public function setCredentialskey($user,$usercert) |
|
| 65 | - { |
|
| 66 | - $this->user=$user; |
|
| 67 | - $this->usercert=$usercert; |
|
| 68 | - $this->authmethod='cert'; |
|
| 69 | - throw new RuntimeException('Certificate auth not implemented'); |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * Set user & certificate (NOT IMPLEMENTED @throws RuntimeException) |
|
| 61 | + * @param string $user |
|
| 62 | + * @param string $usercert |
|
| 63 | + */ |
|
| 64 | + public function setCredentialskey($user,$usercert) |
|
| 65 | + { |
|
| 66 | + $this->user=$user; |
|
| 67 | + $this->usercert=$usercert; |
|
| 68 | + $this->authmethod='cert'; |
|
| 69 | + throw new RuntimeException('Certificate auth not implemented'); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - public function test(array $permissions) |
|
| 73 | - { |
|
| 74 | - try |
|
| 75 | - { |
|
| 76 | - $result=$this->request('GET', "", NULL, NULL); |
|
| 77 | - } |
|
| 78 | - catch (Exception $e) |
|
| 79 | - { |
|
| 80 | - return array(true, 'Error with API : '.$e->getMessage()); |
|
| 81 | - } |
|
| 82 | - //var_dump($result); |
|
| 83 | - $permOk=1; |
|
| 84 | - $permMissing=''; |
|
| 85 | - if (property_exists($result, 'results') && property_exists($result->results[0], 'permissions')) |
|
| 86 | - { |
|
| 72 | + public function test(array $permissions) |
|
| 73 | + { |
|
| 74 | + try |
|
| 75 | + { |
|
| 76 | + $result=$this->request('GET', "", NULL, NULL); |
|
| 77 | + } |
|
| 78 | + catch (Exception $e) |
|
| 79 | + { |
|
| 80 | + return array(true, 'Error with API : '.$e->getMessage()); |
|
| 81 | + } |
|
| 82 | + //var_dump($result); |
|
| 83 | + $permOk=1; |
|
| 84 | + $permMissing=''; |
|
| 85 | + if (property_exists($result, 'results') && property_exists($result->results[0], 'permissions')) |
|
| 86 | + { |
|
| 87 | 87 | |
| 88 | - foreach ( $permissions as $mustPermission) |
|
| 89 | - { |
|
| 90 | - $curPermOK=0; |
|
| 91 | - foreach ( $result->results[0]->permissions as $curPermission) |
|
| 92 | - { |
|
| 93 | - $curPermission=preg_replace('/\*/','.*',$curPermission); // put * as .* to created a regexp |
|
| 94 | - if (preg_match('#'.$curPermission.'#',$mustPermission)) |
|
| 95 | - { |
|
| 96 | - $curPermOK=1; |
|
| 97 | - break; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - if ($curPermOK == 0) |
|
| 101 | - { |
|
| 102 | - $permOk=0; |
|
| 103 | - $permMissing=$mustPermission; |
|
| 104 | - break; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - if ($permOk == 0) |
|
| 108 | - { |
|
| 109 | - return array(true,'API connection OK, but missing permission : '.$permMissing); |
|
| 110 | - } |
|
| 111 | - return array(false,'API connection OK'); |
|
| 88 | + foreach ( $permissions as $mustPermission) |
|
| 89 | + { |
|
| 90 | + $curPermOK=0; |
|
| 91 | + foreach ( $result->results[0]->permissions as $curPermission) |
|
| 92 | + { |
|
| 93 | + $curPermission=preg_replace('/\*/','.*',$curPermission); // put * as .* to created a regexp |
|
| 94 | + if (preg_match('#'.$curPermission.'#',$mustPermission)) |
|
| 95 | + { |
|
| 96 | + $curPermOK=1; |
|
| 97 | + break; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + if ($curPermOK == 0) |
|
| 101 | + { |
|
| 102 | + $permOk=0; |
|
| 103 | + $permMissing=$mustPermission; |
|
| 104 | + break; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + if ($permOk == 0) |
|
| 108 | + { |
|
| 109 | + return array(true,'API connection OK, but missing permission : '.$permMissing); |
|
| 110 | + } |
|
| 111 | + return array(false,'API connection OK'); |
|
| 112 | 112 | |
| 113 | - } |
|
| 114 | - return array(true,'API connection OK, but cannot get permissions'); |
|
| 115 | - } |
|
| 113 | + } |
|
| 114 | + return array(true,'API connection OK, but cannot get permissions'); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - protected function url($url) { |
|
| 119 | - return sprintf('https://%s:%d/%s/%s', $this->host, $this->port, $this->version, $url); |
|
| 120 | - } |
|
| 118 | + protected function url($url) { |
|
| 119 | + return sprintf('https://%s:%d/%s/%s', $this->host, $this->port, $this->version, $url); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Create or return curl ressource |
|
| 124 | - * @throws Exception |
|
| 125 | - * @return resource |
|
| 126 | - */ |
|
| 127 | - protected function curl() { |
|
| 128 | - if ($this->curl === null) { |
|
| 129 | - $this->curl = curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
| 130 | - if (!$this->curl) { |
|
| 131 | - throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl)); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return $this->curl; |
|
| 135 | - } |
|
| 122 | + /** |
|
| 123 | + * Create or return curl ressource |
|
| 124 | + * @throws Exception |
|
| 125 | + * @return resource |
|
| 126 | + */ |
|
| 127 | + protected function curl() { |
|
| 128 | + if ($this->curl === null) { |
|
| 129 | + $this->curl = curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
| 130 | + if (!$this->curl) { |
|
| 131 | + throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl)); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return $this->curl; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * Send a passive service check |
|
| 139 | - * @param string $host : host name |
|
| 140 | - * @param string $service : service name |
|
| 141 | - * @param int $state : state of service |
|
| 142 | - * @param string $display : service passive check output |
|
| 143 | - * @param string $perfdata : performance data as string |
|
| 144 | - * @return array (status = true (oK) or false (nok), string message) |
|
| 145 | - */ |
|
| 146 | - public function serviceCheckResult($host,$service,$state,$display,$perfdata='') |
|
| 147 | - { |
|
| 148 | - //Send a POST request to the URL endpoint /v1/actions/process-check-result |
|
| 149 | - //actions/process-check-result?service=example.localdomain!passive-ping6 |
|
| 150 | - $url='actions/process-check-result'; |
|
| 151 | - $body=array( |
|
| 152 | - "filter" => 'service.name=="'.$service.'" && service.host_name=="'.$host.'"', |
|
| 153 | - 'type' => 'Service', |
|
| 154 | - "exit_status" => $state, |
|
| 155 | - "plugin_output" => $display, |
|
| 156 | - "performance_data" => $perfdata |
|
| 157 | - ); |
|
| 158 | - try |
|
| 159 | - { |
|
| 160 | - $result=$this->request('POST', $url, null, $body); |
|
| 161 | - } catch (Exception $e) |
|
| 162 | - { |
|
| 163 | - return array(false, $e->getMessage()); |
|
| 164 | - } |
|
| 165 | - if (property_exists($result,'error') ) |
|
| 166 | - { |
|
| 167 | - if (property_exists($result,'status')) |
|
| 168 | - { |
|
| 169 | - $message=$result->status; |
|
| 170 | - } |
|
| 171 | - else |
|
| 172 | - { |
|
| 173 | - $message="Unkown status"; |
|
| 174 | - } |
|
| 175 | - return array(false , 'Ret code ' .$result->error.' : '.$message); |
|
| 176 | - } |
|
| 177 | - if (property_exists($result, 'results')) |
|
| 178 | - { |
|
| 179 | - if (isset($result->results[0])) |
|
| 180 | - { |
|
| 181 | - return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
| 182 | - } |
|
| 183 | - else |
|
| 184 | - { |
|
| 185 | - return array(false,'Service not found'); |
|
| 186 | - } |
|
| 137 | + /** |
|
| 138 | + * Send a passive service check |
|
| 139 | + * @param string $host : host name |
|
| 140 | + * @param string $service : service name |
|
| 141 | + * @param int $state : state of service |
|
| 142 | + * @param string $display : service passive check output |
|
| 143 | + * @param string $perfdata : performance data as string |
|
| 144 | + * @return array (status = true (oK) or false (nok), string message) |
|
| 145 | + */ |
|
| 146 | + public function serviceCheckResult($host,$service,$state,$display,$perfdata='') |
|
| 147 | + { |
|
| 148 | + //Send a POST request to the URL endpoint /v1/actions/process-check-result |
|
| 149 | + //actions/process-check-result?service=example.localdomain!passive-ping6 |
|
| 150 | + $url='actions/process-check-result'; |
|
| 151 | + $body=array( |
|
| 152 | + "filter" => 'service.name=="'.$service.'" && service.host_name=="'.$host.'"', |
|
| 153 | + 'type' => 'Service', |
|
| 154 | + "exit_status" => $state, |
|
| 155 | + "plugin_output" => $display, |
|
| 156 | + "performance_data" => $perfdata |
|
| 157 | + ); |
|
| 158 | + try |
|
| 159 | + { |
|
| 160 | + $result=$this->request('POST', $url, null, $body); |
|
| 161 | + } catch (Exception $e) |
|
| 162 | + { |
|
| 163 | + return array(false, $e->getMessage()); |
|
| 164 | + } |
|
| 165 | + if (property_exists($result,'error') ) |
|
| 166 | + { |
|
| 167 | + if (property_exists($result,'status')) |
|
| 168 | + { |
|
| 169 | + $message=$result->status; |
|
| 170 | + } |
|
| 171 | + else |
|
| 172 | + { |
|
| 173 | + $message="Unkown status"; |
|
| 174 | + } |
|
| 175 | + return array(false , 'Ret code ' .$result->error.' : '.$message); |
|
| 176 | + } |
|
| 177 | + if (property_exists($result, 'results')) |
|
| 178 | + { |
|
| 179 | + if (isset($result->results[0])) |
|
| 180 | + { |
|
| 181 | + return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
| 182 | + } |
|
| 183 | + else |
|
| 184 | + { |
|
| 185 | + return array(false,'Service not found'); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - } |
|
| 189 | - return array(false,'Unkown result, open issue with this : '.print_r($result,true)); |
|
| 190 | - } |
|
| 188 | + } |
|
| 189 | + return array(false,'Unkown result, open issue with this : '.print_r($result,true)); |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * return array of host by IP (4 or 6) |
|
| 194 | - * @param string $ip |
|
| 195 | - * @throws Exception |
|
| 196 | - * @return array objects : array('__name','name','display_name') |
|
| 197 | - */ |
|
| 198 | - public function getHostByIP($ip) |
|
| 199 | - { |
|
| 200 | - /* |
|
| 192 | + /** |
|
| 193 | + * return array of host by IP (4 or 6) |
|
| 194 | + * @param string $ip |
|
| 195 | + * @throws Exception |
|
| 196 | + * @return array objects : array('__name','name','display_name') |
|
| 197 | + */ |
|
| 198 | + public function getHostByIP($ip) |
|
| 199 | + { |
|
| 200 | + /* |
|
| 201 | 201 | * curl -k -s -u trapdirector:trapdirector -H 'X-HTTP-Method-Override: GET' -X POST 'https://localhost:5665/v1/objects/hosts' |
| 202 | 202 | * -d '{"filter":"host.group==\"test_trap\"","attrs": ["address" ,"address6"]}' |
| 203 | 203 | |
| 204 | 204 | {"results":[{"attrs":{"__name":"Icinga host","address":"127.0.0.1","display_name":"Icinga host","name":"Icinga host"},"joins":{},"meta":{},"name":"Icinga host","type":"Host"}]} |
| 205 | 205 | */ |
| 206 | 206 | |
| 207 | - $url='objects/hosts'; |
|
| 208 | - $body=array( |
|
| 209 | - "filter" => 'host.address=="'.$ip.'" || host.address6=="'.$ip.'"', |
|
| 210 | - "attrs" => array('__name','name','display_name') |
|
| 211 | - ); |
|
| 212 | - try |
|
| 213 | - { |
|
| 214 | - $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
| 215 | - } catch (Exception $e) |
|
| 216 | - { |
|
| 217 | - throw new Exception($e->getMessage()); |
|
| 218 | - } |
|
| 207 | + $url='objects/hosts'; |
|
| 208 | + $body=array( |
|
| 209 | + "filter" => 'host.address=="'.$ip.'" || host.address6=="'.$ip.'"', |
|
| 210 | + "attrs" => array('__name','name','display_name') |
|
| 211 | + ); |
|
| 212 | + try |
|
| 213 | + { |
|
| 214 | + $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
| 215 | + } catch (Exception $e) |
|
| 216 | + { |
|
| 217 | + throw new Exception($e->getMessage()); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - if (property_exists($result,'error') ) |
|
| 221 | - { |
|
| 222 | - if (property_exists($result,'status')) |
|
| 223 | - { |
|
| 224 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
| 225 | - } |
|
| 226 | - else |
|
| 227 | - { |
|
| 228 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - if (property_exists($result, 'results')) |
|
| 232 | - { |
|
| 233 | - $numHost=0; |
|
| 234 | - $hostArray=array(); |
|
| 235 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
| 236 | - { |
|
| 237 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
| 238 | - $numHost++; |
|
| 239 | - } |
|
| 240 | - return $hostArray; |
|
| 241 | - } |
|
| 242 | - throw new Exception('Unkown result'); |
|
| 243 | - } |
|
| 220 | + if (property_exists($result,'error') ) |
|
| 221 | + { |
|
| 222 | + if (property_exists($result,'status')) |
|
| 223 | + { |
|
| 224 | + throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
| 225 | + } |
|
| 226 | + else |
|
| 227 | + { |
|
| 228 | + throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + if (property_exists($result, 'results')) |
|
| 232 | + { |
|
| 233 | + $numHost=0; |
|
| 234 | + $hostArray=array(); |
|
| 235 | + while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
| 236 | + { |
|
| 237 | + $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
| 238 | + $numHost++; |
|
| 239 | + } |
|
| 240 | + return $hostArray; |
|
| 241 | + } |
|
| 242 | + throw new Exception('Unkown result'); |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * Get all host and IP from hostgroup |
|
| 247 | - * @param string $hostGroup |
|
| 248 | - * @throws Exception |
|
| 249 | - * @return array : attributes : address, address6, name |
|
| 250 | - */ |
|
| 251 | - public function getHostsIPByHostGroup($hostGroup) |
|
| 252 | - { |
|
| 253 | - $url='objects/hosts'; |
|
| 254 | - $body=array( |
|
| 255 | - "filter" => '\"'.$hostGroup.'\" in host.groups', |
|
| 256 | - "attrs" => array('address','address','name') |
|
| 257 | - ); |
|
| 258 | - try |
|
| 259 | - { |
|
| 260 | - $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
| 261 | - } catch (Exception $e) |
|
| 262 | - { |
|
| 263 | - throw new Exception($e->getMessage()); |
|
| 264 | - } |
|
| 245 | + /** |
|
| 246 | + * Get all host and IP from hostgroup |
|
| 247 | + * @param string $hostGroup |
|
| 248 | + * @throws Exception |
|
| 249 | + * @return array : attributes : address, address6, name |
|
| 250 | + */ |
|
| 251 | + public function getHostsIPByHostGroup($hostGroup) |
|
| 252 | + { |
|
| 253 | + $url='objects/hosts'; |
|
| 254 | + $body=array( |
|
| 255 | + "filter" => '\"'.$hostGroup.'\" in host.groups', |
|
| 256 | + "attrs" => array('address','address','name') |
|
| 257 | + ); |
|
| 258 | + try |
|
| 259 | + { |
|
| 260 | + $result=$this->request('POST', $url, array('X-HTTP-Method-Override: GET'), $body); |
|
| 261 | + } catch (Exception $e) |
|
| 262 | + { |
|
| 263 | + throw new Exception($e->getMessage()); |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - if (property_exists($result,'error') ) |
|
| 267 | - { |
|
| 268 | - if (property_exists($result,'status')) |
|
| 269 | - { |
|
| 270 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
| 271 | - } |
|
| 272 | - else |
|
| 273 | - { |
|
| 274 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - if (property_exists($result, 'results')) |
|
| 278 | - { |
|
| 279 | - $numHost=0; |
|
| 280 | - $hostArray=array(); |
|
| 281 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
| 282 | - { |
|
| 283 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
| 284 | - $hostArray[$numHost]->name = $result->results[$numHost]->name; |
|
| 285 | - $numHost++; |
|
| 286 | - } |
|
| 287 | - return $hostArray; |
|
| 288 | - } |
|
| 289 | - throw new Exception('Unkown result'); |
|
| 290 | - } |
|
| 266 | + if (property_exists($result,'error') ) |
|
| 267 | + { |
|
| 268 | + if (property_exists($result,'status')) |
|
| 269 | + { |
|
| 270 | + throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
| 271 | + } |
|
| 272 | + else |
|
| 273 | + { |
|
| 274 | + throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + if (property_exists($result, 'results')) |
|
| 278 | + { |
|
| 279 | + $numHost=0; |
|
| 280 | + $hostArray=array(); |
|
| 281 | + while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
| 282 | + { |
|
| 283 | + $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
| 284 | + $hostArray[$numHost]->name = $result->results[$numHost]->name; |
|
| 285 | + $numHost++; |
|
| 286 | + } |
|
| 287 | + return $hostArray; |
|
| 288 | + } |
|
| 289 | + throw new Exception('Unkown result'); |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - /** |
|
| 293 | - * Send request to API |
|
| 294 | - * @param string $method get/post/... |
|
| 295 | - * @param string $url (after /v1/ ) |
|
| 296 | - * @param array $headers |
|
| 297 | - * @param array $body |
|
| 298 | - * @throws Exception |
|
| 299 | - * @return array |
|
| 300 | - */ |
|
| 301 | - public function request($method, $url, $headers, $body) { |
|
| 302 | - $auth = sprintf('%s:%s', $this->user, $this->pass); |
|
| 303 | - $curlHeaders = array("Accept: application/json"); |
|
| 304 | - if ($body !== null) { |
|
| 305 | - $body = json_encode($body); |
|
| 306 | - array_push($curlHeaders, 'Content-Type: application/json'); |
|
| 307 | - //array_push($curlHeaders, 'X-HTTP-Method-Override: GET'); |
|
| 308 | - } |
|
| 309 | - //var_dump($body); |
|
| 310 | - //var_dump($this->url($url)); |
|
| 311 | - if ($headers !== null) { |
|
| 312 | - $curlFinalHeaders = array_merge($curlHeaders, $headers); |
|
| 313 | - } else |
|
| 314 | - { |
|
| 315 | - $curlFinalHeaders=$curlHeaders; |
|
| 316 | - } |
|
| 317 | - $curl = $this->curl(); |
|
| 318 | - $opts = array( |
|
| 319 | - CURLOPT_URL => $this->url($url), |
|
| 320 | - CURLOPT_HTTPHEADER => $curlFinalHeaders, |
|
| 321 | - CURLOPT_USERPWD => $auth, |
|
| 322 | - CURLOPT_CUSTOMREQUEST => strtoupper($method), |
|
| 323 | - CURLOPT_RETURNTRANSFER => true, |
|
| 324 | - CURLOPT_CONNECTTIMEOUT => 10, |
|
| 325 | - CURLOPT_SSL_VERIFYHOST => false, |
|
| 326 | - CURLOPT_SSL_VERIFYPEER => false, |
|
| 327 | - ); |
|
| 328 | - if ($body !== null) { |
|
| 329 | - $opts[CURLOPT_POSTFIELDS] = $body; |
|
| 330 | - } |
|
| 331 | - curl_setopt_array($curl, $opts); |
|
| 332 | - $res = curl_exec($curl); |
|
| 333 | - if ($res === false) { |
|
| 334 | - throw new Exception('CURL ERROR: ' . curl_error($curl)); |
|
| 335 | - } |
|
| 336 | - $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
| 337 | - if ($statusCode === 401) { |
|
| 338 | - throw new Exception('Unable to authenticate, please check your API credentials'); |
|
| 339 | - } |
|
| 340 | - return $this->fromJsonResult($res); |
|
| 341 | - } |
|
| 292 | + /** |
|
| 293 | + * Send request to API |
|
| 294 | + * @param string $method get/post/... |
|
| 295 | + * @param string $url (after /v1/ ) |
|
| 296 | + * @param array $headers |
|
| 297 | + * @param array $body |
|
| 298 | + * @throws Exception |
|
| 299 | + * @return array |
|
| 300 | + */ |
|
| 301 | + public function request($method, $url, $headers, $body) { |
|
| 302 | + $auth = sprintf('%s:%s', $this->user, $this->pass); |
|
| 303 | + $curlHeaders = array("Accept: application/json"); |
|
| 304 | + if ($body !== null) { |
|
| 305 | + $body = json_encode($body); |
|
| 306 | + array_push($curlHeaders, 'Content-Type: application/json'); |
|
| 307 | + //array_push($curlHeaders, 'X-HTTP-Method-Override: GET'); |
|
| 308 | + } |
|
| 309 | + //var_dump($body); |
|
| 310 | + //var_dump($this->url($url)); |
|
| 311 | + if ($headers !== null) { |
|
| 312 | + $curlFinalHeaders = array_merge($curlHeaders, $headers); |
|
| 313 | + } else |
|
| 314 | + { |
|
| 315 | + $curlFinalHeaders=$curlHeaders; |
|
| 316 | + } |
|
| 317 | + $curl = $this->curl(); |
|
| 318 | + $opts = array( |
|
| 319 | + CURLOPT_URL => $this->url($url), |
|
| 320 | + CURLOPT_HTTPHEADER => $curlFinalHeaders, |
|
| 321 | + CURLOPT_USERPWD => $auth, |
|
| 322 | + CURLOPT_CUSTOMREQUEST => strtoupper($method), |
|
| 323 | + CURLOPT_RETURNTRANSFER => true, |
|
| 324 | + CURLOPT_CONNECTTIMEOUT => 10, |
|
| 325 | + CURLOPT_SSL_VERIFYHOST => false, |
|
| 326 | + CURLOPT_SSL_VERIFYPEER => false, |
|
| 327 | + ); |
|
| 328 | + if ($body !== null) { |
|
| 329 | + $opts[CURLOPT_POSTFIELDS] = $body; |
|
| 330 | + } |
|
| 331 | + curl_setopt_array($curl, $opts); |
|
| 332 | + $res = curl_exec($curl); |
|
| 333 | + if ($res === false) { |
|
| 334 | + throw new Exception('CURL ERROR: ' . curl_error($curl)); |
|
| 335 | + } |
|
| 336 | + $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
| 337 | + if ($statusCode === 401) { |
|
| 338 | + throw new Exception('Unable to authenticate, please check your API credentials'); |
|
| 339 | + } |
|
| 340 | + return $this->fromJsonResult($res); |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - /** |
|
| 344 | - * |
|
| 345 | - * @param string $json json encoded |
|
| 346 | - * @throws Exception |
|
| 347 | - * @return array json decoded |
|
| 348 | - */ |
|
| 349 | - protected function fromJsonResult($json) { |
|
| 350 | - $result = @json_decode($json); |
|
| 351 | - //var_dump($json); |
|
| 352 | - if ($result === null) { |
|
| 353 | - throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error())); |
|
| 354 | - } |
|
| 355 | - return $result; |
|
| 356 | - } |
|
| 343 | + /** |
|
| 344 | + * |
|
| 345 | + * @param string $json json encoded |
|
| 346 | + * @throws Exception |
|
| 347 | + * @return array json decoded |
|
| 348 | + */ |
|
| 349 | + protected function fromJsonResult($json) { |
|
| 350 | + $result = @json_decode($json); |
|
| 351 | + //var_dump($json); |
|
| 352 | + if ($result === null) { |
|
| 353 | + throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error())); |
|
| 354 | + } |
|
| 355 | + return $result; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Return text error no json error |
|
| 360 | - * @param string $errorCode |
|
| 361 | - * @return string |
|
| 362 | - */ |
|
| 363 | - protected function getLastJsonErrorMessage($errorCode) { |
|
| 364 | - if (!array_key_exists($errorCode, $this->errorReference)) { |
|
| 365 | - return self::JSON_UNKNOWN_ERROR; |
|
| 366 | - } |
|
| 367 | - return $this->errorReference[$errorCode]; |
|
| 368 | - } |
|
| 358 | + /** |
|
| 359 | + * Return text error no json error |
|
| 360 | + * @param string $errorCode |
|
| 361 | + * @return string |
|
| 362 | + */ |
|
| 363 | + protected function getLastJsonErrorMessage($errorCode) { |
|
| 364 | + if (!array_key_exists($errorCode, $this->errorReference)) { |
|
| 365 | + return self::JSON_UNKNOWN_ERROR; |
|
| 366 | + } |
|
| 367 | + return $this->errorReference[$errorCode]; |
|
| 368 | + } |
|
| 369 | 369 | } |
| 370 | 370 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | |
| 59 | 59 | function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
| 60 | 60 | { |
| 61 | - // Paths of ini files |
|
| 61 | + // Paths of ini files |
|
| 62 | 62 | $this->icingaweb2_etc=$etc_dir; |
| 63 | 63 | $this->trap_module_config=$this->icingaweb2_etc."/modules/trapdirector/config.ini"; |
| 64 | 64 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
@@ -67,18 +67,18 @@ discard block |
||
| 67 | 67 | $this->logging = new Logging(); |
| 68 | 68 | if ($baseLogLevel != null) |
| 69 | 69 | { |
| 70 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 71 | - $this->logSetup=true; |
|
| 70 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 71 | + $this->logSetup=true; |
|
| 72 | 72 | } |
| 73 | 73 | else |
| 74 | - $this->logSetup=false; |
|
| 74 | + $this->logSetup=false; |
|
| 75 | 75 | $this->logging->log('Loggin started', INFO); |
| 76 | 76 | |
| 77 | 77 | // Get options from ini files |
| 78 | 78 | $trapConfig=parse_ini_file($this->trap_module_config,true); |
| 79 | 79 | if ($trapConfig == false) |
| 80 | 80 | { |
| 81 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 81 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 82 | 82 | } |
| 83 | 83 | $this->getMainOptions($trapConfig); // Get main options from ini file |
| 84 | 84 | $this->setupDatabase($trapConfig); // Setup database class |
@@ -112,26 +112,26 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
| 114 | 114 | { |
| 115 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 116 | - { |
|
| 117 | - if ($message === null) |
|
| 118 | - { |
|
| 119 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 120 | - } |
|
| 121 | - $this->logging->log($message,$log_level,'syslog'); |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - else |
|
| 125 | - { |
|
| 126 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 127 | - return true; |
|
| 128 | - } |
|
| 115 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 116 | + { |
|
| 117 | + if ($message === null) |
|
| 118 | + { |
|
| 119 | + $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 120 | + } |
|
| 121 | + $this->logging->log($message,$log_level,'syslog'); |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + else |
|
| 125 | + { |
|
| 126 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 127 | + return true; |
|
| 128 | + } |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * Get options from ini file |
| 133 | 133 | * @param array $trap_config : ini file array |
| 134 | - */ |
|
| 134 | + */ |
|
| 135 | 135 | protected function getMainOptions($trapConfig) |
| 136 | 136 | { |
| 137 | 137 | |
@@ -150,10 +150,10 @@ discard block |
||
| 150 | 150 | // API options |
| 151 | 151 | if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
| 152 | 152 | { |
| 153 | - $this->api_use=true; |
|
| 154 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 155 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 156 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 153 | + $this->api_use=true; |
|
| 154 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 155 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 156 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 157 | 157 | } |
| 158 | 158 | } |
| 159 | 159 | |
@@ -163,45 +163,45 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | protected function setupDatabase($trapConfig) |
| 165 | 165 | { |
| 166 | - // Trap database |
|
| 167 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 168 | - { |
|
| 169 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 170 | - return; |
|
| 171 | - } |
|
| 172 | - $dbTrapName=$trapConfig['config']['database']; |
|
| 173 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 166 | + // Trap database |
|
| 167 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
| 168 | + { |
|
| 169 | + $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 170 | + return; |
|
| 171 | + } |
|
| 172 | + $dbTrapName=$trapConfig['config']['database']; |
|
| 173 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 174 | 174 | |
| 175 | 175 | if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
| 176 | - { |
|
| 177 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 178 | - return; |
|
| 179 | - } |
|
| 180 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 181 | - { |
|
| 182 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 183 | - return; |
|
| 184 | - } |
|
| 176 | + { |
|
| 177 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 178 | + return; |
|
| 179 | + } |
|
| 180 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 181 | + { |
|
| 182 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 183 | + return; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 186 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 187 | 187 | |
| 188 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 188 | + if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 189 | 189 | |
| 190 | - // IDO Database |
|
| 191 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 192 | - { |
|
| 193 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 194 | - } |
|
| 195 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 196 | - |
|
| 197 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 198 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 199 | - { |
|
| 200 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 201 | - return; |
|
| 202 | - } |
|
| 190 | + // IDO Database |
|
| 191 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 192 | + { |
|
| 193 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 194 | + } |
|
| 195 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 196 | + |
|
| 197 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 198 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 199 | + { |
|
| 200 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 201 | + return; |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 204 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -212,9 +212,9 @@ discard block |
||
| 212 | 212 | // Database options |
| 213 | 213 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
| 214 | 214 | { |
| 215 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 216 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 217 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 215 | + $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 216 | + $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 217 | + $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
@@ -225,10 +225,10 @@ discard block |
||
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
| 228 | - * Get data from db_config |
|
| 229 | - * @param $element string name of param |
|
| 230 | - * @return mixed : value (or null) |
|
| 231 | - */ |
|
| 228 | + * Get data from db_config |
|
| 229 | + * @param $element string name of param |
|
| 230 | + * @return mixed : value (or null) |
|
| 231 | + */ |
|
| 232 | 232 | protected function getDBConfig($element) |
| 233 | 233 | { |
| 234 | 234 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -246,15 +246,15 @@ discard block |
||
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | /** OBSOLETE Send log. Throws exception on critical error |
| 249 | - * @param string $message Message to log |
|
| 250 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 251 | - * @param string $destination file/syslog/display |
|
| 252 | - * @return void |
|
| 253 | - **/ |
|
| 249 | + * @param string $message Message to log |
|
| 250 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 251 | + * @param string $destination file/syslog/display |
|
| 252 | + * @return void |
|
| 253 | + **/ |
|
| 254 | 254 | public function trapLog( $message, $level, $destination ='') // OBSOLETE |
| 255 | 255 | { |
| 256 | 256 | // TODO : replace ref with $this->logging->log |
| 257 | - $this->logging->log($message, $level, $destination); |
|
| 257 | + $this->logging->log($message, $level, $destination); |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
@@ -264,19 +264,19 @@ discard block |
||
| 264 | 264 | |
| 265 | 265 | protected function getAPI() |
| 266 | 266 | { |
| 267 | - if ($this->icinga2api == null) |
|
| 268 | - { |
|
| 269 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 270 | - } |
|
| 271 | - return $this->icinga2api; |
|
| 267 | + if ($this->icinga2api == null) |
|
| 268 | + { |
|
| 269 | + $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 270 | + } |
|
| 271 | + return $this->icinga2api; |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | |
| 275 | 275 | /** |
| 276 | 276 | * read data from stream |
| 277 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 278 | - * @return mixed array trap data or exception with error |
|
| 279 | - */ |
|
| 277 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 278 | + * @return mixed array trap data or exception with error |
|
| 279 | + */ |
|
| 280 | 280 | public function read_trap($stream='php://stdin') |
| 281 | 281 | { |
| 282 | 282 | //Read data from snmptrapd from stdin |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | |
| 285 | 285 | if ($input_stream === false) |
| 286 | 286 | { |
| 287 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 287 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 288 | 288 | $this->logging->log("Error reading stdin !",ERROR,''); |
| 289 | 289 | return null; // note : exception thrown by logging |
| 290 | 290 | } |
@@ -293,21 +293,21 @@ discard block |
||
| 293 | 293 | $this->receivingHost=chop(fgets($input_stream)); |
| 294 | 294 | if ($this->receivingHost === false) |
| 295 | 295 | { |
| 296 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 296 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 297 | 297 | $this->logging->log("Error reading Host !",ERROR,''); |
| 298 | 298 | } |
| 299 | 299 | // line 2 IP:port=>IP:port |
| 300 | 300 | $IP=chop(fgets($input_stream)); |
| 301 | 301 | if ($IP === false) |
| 302 | 302 | { |
| 303 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 303 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 304 | 304 | $this->logging->log("Error reading IP !",ERROR,''); |
| 305 | 305 | } |
| 306 | 306 | $matches=array(); |
| 307 | 307 | $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
| 308 | 308 | if ($ret_code===0 || $ret_code===false) |
| 309 | 309 | { |
| 310 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 310 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 311 | 311 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 312 | 312 | } |
| 313 | 313 | else |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | } |
| 329 | 329 | else |
| 330 | 330 | { |
| 331 | - 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')) |
|
| 331 | + 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')) |
|
| 332 | 332 | { |
| 333 | 333 | $this->trap_data['trap_oid']=$matches[2]; |
| 334 | 334 | } |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | |
| 345 | 345 | if ($this->trap_data['trap_oid']=='unknown') |
| 346 | 346 | { |
| 347 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 347 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 348 | 348 | $this->logging->log('no trap oid found',ERROR,''); |
| 349 | 349 | } |
| 350 | 350 | |
@@ -374,9 +374,9 @@ discard block |
||
| 374 | 374 | |
| 375 | 375 | /** |
| 376 | 376 | * Translate oid into array(MIB,Name) |
| 377 | - * @param $oid string oid to translate |
|
| 378 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 379 | - */ |
|
| 377 | + * @param $oid string oid to translate |
|
| 378 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 379 | + */ |
|
| 380 | 380 | public function translateOID($oid) |
| 381 | 381 | { |
| 382 | 382 | // try from database |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | |
| 410 | 410 | // Try to get oid name from snmptranslate |
| 411 | 411 | $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
| 412 | - ' '.$oid); |
|
| 412 | + ' '.$oid); |
|
| 413 | 413 | $matches=array(); |
| 414 | 414 | $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
| 415 | 415 | if ($ret_code===0 || $ret_code === false) { |
@@ -422,9 +422,9 @@ discard block |
||
| 422 | 422 | |
| 423 | 423 | /** |
| 424 | 424 | * Erase old trap records |
| 425 | - * @param integer $days : erase traps when more than $days old |
|
| 426 | - * @return integer : number of lines deleted |
|
| 427 | - **/ |
|
| 425 | + * @param integer $days : erase traps when more than $days old |
|
| 426 | + * @return integer : number of lines deleted |
|
| 427 | + **/ |
|
| 428 | 428 | public function eraseOldTraps($days=0) |
| 429 | 429 | { |
| 430 | 430 | if ($days==0) |
@@ -449,72 +449,72 @@ discard block |
||
| 449 | 449 | public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
| 450 | 450 | { |
| 451 | 451 | |
| 452 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 452 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 453 | 453 | |
| 454 | - // add date time |
|
| 455 | - $insert_col ='date_received,status'; |
|
| 456 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 454 | + // add date time |
|
| 455 | + $insert_col ='date_received,status'; |
|
| 456 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 457 | 457 | |
| 458 | - if ($sourceIP !=null) |
|
| 459 | - { |
|
| 460 | - $insert_col .=',source_ip'; |
|
| 461 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 462 | - } |
|
| 463 | - if ($trapoid !=null) |
|
| 464 | - { |
|
| 465 | - $insert_col .=',trap_oid'; |
|
| 466 | - $insert_val .=",'". $trapoid ."'"; |
|
| 467 | - } |
|
| 468 | - $insert_col .=',status_detail'; |
|
| 469 | - $insert_val .=",'". $message ."'"; |
|
| 458 | + if ($sourceIP !=null) |
|
| 459 | + { |
|
| 460 | + $insert_col .=',source_ip'; |
|
| 461 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 462 | + } |
|
| 463 | + if ($trapoid !=null) |
|
| 464 | + { |
|
| 465 | + $insert_col .=',trap_oid'; |
|
| 466 | + $insert_val .=",'". $trapoid ."'"; |
|
| 467 | + } |
|
| 468 | + $insert_col .=',status_detail'; |
|
| 469 | + $insert_val .=",'". $message ."'"; |
|
| 470 | 470 | |
| 471 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 471 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 472 | 472 | |
| 473 | - switch ($this->trapsDB->trapDBType) |
|
| 474 | - { |
|
| 475 | - case 'pgsql': |
|
| 476 | - $sql .= ' RETURNING id;'; |
|
| 477 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 478 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 479 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 480 | - } |
|
| 481 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 482 | - // Get last id to insert oid/values in secondary table |
|
| 483 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 473 | + switch ($this->trapsDB->trapDBType) |
|
| 474 | + { |
|
| 475 | + case 'pgsql': |
|
| 476 | + $sql .= ' RETURNING id;'; |
|
| 477 | + $this->logging->log('sql : '.$sql,INFO); |
|
| 478 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 479 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 480 | + } |
|
| 481 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 482 | + // Get last id to insert oid/values in secondary table |
|
| 483 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 484 | 484 | |
| 485 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 486 | - } |
|
| 487 | - if (! isset($inserted_id_ret['id'])) { |
|
| 488 | - $this->logging->log('Error getting id',1,''); |
|
| 489 | - } |
|
| 490 | - $this->trap_id=$inserted_id_ret['id']; |
|
| 491 | - break; |
|
| 492 | - case 'mysql': |
|
| 493 | - $sql .= ';'; |
|
| 494 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 495 | - if ($db_conn->query($sql) === false) { |
|
| 496 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 497 | - } |
|
| 498 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 499 | - // Get last id to insert oid/values in secondary table |
|
| 500 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 501 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 502 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 503 | - } |
|
| 485 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 486 | + } |
|
| 487 | + if (! isset($inserted_id_ret['id'])) { |
|
| 488 | + $this->logging->log('Error getting id',1,''); |
|
| 489 | + } |
|
| 490 | + $this->trap_id=$inserted_id_ret['id']; |
|
| 491 | + break; |
|
| 492 | + case 'mysql': |
|
| 493 | + $sql .= ';'; |
|
| 494 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 495 | + if ($db_conn->query($sql) === false) { |
|
| 496 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 497 | + } |
|
| 498 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 499 | + // Get last id to insert oid/values in secondary table |
|
| 500 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 501 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 502 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 503 | + } |
|
| 504 | 504 | |
| 505 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 506 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 507 | - $this->trap_id=$inserted_id; |
|
| 508 | - break; |
|
| 509 | - default: |
|
| 510 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 511 | - } |
|
| 505 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 506 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 507 | + $this->trap_id=$inserted_id; |
|
| 508 | + break; |
|
| 509 | + default: |
|
| 510 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 511 | + } |
|
| 512 | 512 | |
| 513 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 513 | + $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | /** Write trap data to trap database |
| 517 | - */ |
|
| 517 | + */ |
|
| 518 | 518 | public function writeTrapToDB() |
| 519 | 519 | { |
| 520 | 520 | |
@@ -612,10 +612,10 @@ discard block |
||
| 612 | 612 | } |
| 613 | 613 | |
| 614 | 614 | /** Get rules from rule database with ip and oid |
| 615 | - * @param $ip string ipv4 or ipv6 |
|
| 616 | - * @param $oid string oid in numeric |
|
| 617 | - * @return mixed|boolean : PDO object or false |
|
| 618 | - */ |
|
| 615 | + * @param $ip string ipv4 or ipv6 |
|
| 616 | + * @param $oid string oid in numeric |
|
| 617 | + * @return mixed|boolean : PDO object or false |
|
| 618 | + */ |
|
| 619 | 619 | protected function getRules($ip,$oid) |
| 620 | 620 | { |
| 621 | 621 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -674,9 +674,9 @@ discard block |
||
| 674 | 674 | } |
| 675 | 675 | |
| 676 | 676 | /** Add rule match to rule |
| 677 | - * @param id int : rule id |
|
| 678 | - * @param set int : value to set |
|
| 679 | - */ |
|
| 677 | + * @param id int : rule id |
|
| 678 | + * @param set int : value to set |
|
| 679 | + */ |
|
| 680 | 680 | protected function add_rule_match($id, $set) |
| 681 | 681 | { |
| 682 | 682 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -693,65 +693,65 @@ discard block |
||
| 693 | 693 | * @param integer $state numerical staus |
| 694 | 694 | * @param string $display |
| 695 | 695 | * @returnn bool true is service check was sent without error |
| 696 | - */ |
|
| 696 | + */ |
|
| 697 | 697 | public function serviceCheckResult($host,$service,$state,$display) |
| 698 | 698 | { |
| 699 | - if ($this->api_use === false) |
|
| 700 | - { |
|
| 701 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 702 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 703 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 699 | + if ($this->api_use === false) |
|
| 700 | + { |
|
| 701 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 702 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 703 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 704 | 704 | |
| 705 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 706 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 707 | - return true; |
|
| 708 | - } |
|
| 709 | - else |
|
| 710 | - { |
|
| 711 | - // Get perfdata if found |
|
| 712 | - $matches=array(); |
|
| 713 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 714 | - { |
|
| 715 | - $display=$matches[1]; |
|
| 716 | - $perfdata=$matches[2]; |
|
| 717 | - } |
|
| 718 | - else |
|
| 719 | - { |
|
| 720 | - $perfdata=''; |
|
| 721 | - } |
|
| 705 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 706 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 707 | + return true; |
|
| 708 | + } |
|
| 709 | + else |
|
| 710 | + { |
|
| 711 | + // Get perfdata if found |
|
| 712 | + $matches=array(); |
|
| 713 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
| 714 | + { |
|
| 715 | + $display=$matches[1]; |
|
| 716 | + $perfdata=$matches[2]; |
|
| 717 | + } |
|
| 718 | + else |
|
| 719 | + { |
|
| 720 | + $perfdata=''; |
|
| 721 | + } |
|
| 722 | 722 | |
| 723 | - $api = $this->getAPI(); |
|
| 724 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 725 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 726 | - if ($retcode == false) |
|
| 727 | - { |
|
| 728 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 729 | - return false; |
|
| 730 | - } |
|
| 731 | - else |
|
| 732 | - { |
|
| 733 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 734 | - return true; |
|
| 735 | - } |
|
| 736 | - } |
|
| 723 | + $api = $this->getAPI(); |
|
| 724 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 725 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
| 726 | + if ($retcode == false) |
|
| 727 | + { |
|
| 728 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 729 | + return false; |
|
| 730 | + } |
|
| 731 | + else |
|
| 732 | + { |
|
| 733 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 734 | + return true; |
|
| 735 | + } |
|
| 736 | + } |
|
| 737 | 737 | } |
| 738 | 738 | |
| 739 | 739 | public function getHostByIP($ip) |
| 740 | 740 | { |
| 741 | - $api = $this->getAPI(); |
|
| 742 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 743 | - return $api->getHostByIP($ip); |
|
| 741 | + $api = $this->getAPI(); |
|
| 742 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 743 | + return $api->getHostByIP($ip); |
|
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | /** Resolve display. |
| 747 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 748 | - * @param $display string |
|
| 749 | - * @return string display |
|
| 750 | - */ |
|
| 747 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
| 748 | + * @param $display string |
|
| 749 | + * @return string display |
|
| 750 | + */ |
|
| 751 | 751 | protected function applyDisplay($display) |
| 752 | 752 | { |
| 753 | - $matches=array(); |
|
| 754 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 753 | + $matches=array(); |
|
| 754 | + while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 755 | 755 | { |
| 756 | 756 | $oid=$matches[1]; |
| 757 | 757 | $found=0; |
@@ -785,7 +785,7 @@ discard block |
||
| 785 | 785 | } |
| 786 | 786 | |
| 787 | 787 | /** Match rules for current trap and do action |
| 788 | - */ |
|
| 788 | + */ |
|
| 789 | 789 | public function applyRules() |
| 790 | 790 | { |
| 791 | 791 | $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
@@ -823,12 +823,12 @@ discard block |
||
| 823 | 823 | { |
| 824 | 824 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 825 | 825 | { |
| 826 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 826 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 827 | 827 | } |
| 828 | 828 | else |
| 829 | 829 | { |
| 830 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 831 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 830 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 831 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 832 | 832 | } |
| 833 | 833 | } |
| 834 | 834 | else |
@@ -845,15 +845,15 @@ discard block |
||
| 845 | 845 | $this->logging->log('action NOK : '.$action,INFO ); |
| 846 | 846 | if ($action >= 0) |
| 847 | 847 | { |
| 848 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 849 | - { |
|
| 850 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 851 | - } |
|
| 852 | - else |
|
| 853 | - { |
|
| 854 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 855 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 856 | - } |
|
| 848 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 849 | + { |
|
| 850 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 851 | + } |
|
| 852 | + else |
|
| 853 | + { |
|
| 854 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 855 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 856 | + } |
|
| 857 | 857 | } |
| 858 | 858 | else |
| 859 | 859 | { |
@@ -876,9 +876,9 @@ discard block |
||
| 876 | 876 | } |
| 877 | 877 | catch (Exception $e) |
| 878 | 878 | { |
| 879 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 880 | - $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 881 | - $this->trap_data['status']='error'; |
|
| 879 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 880 | + $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 881 | + $this->trap_data['status']='error'; |
|
| 882 | 882 | } |
| 883 | 883 | |
| 884 | 884 | } |
@@ -893,8 +893,8 @@ discard block |
||
| 893 | 893 | } |
| 894 | 894 | |
| 895 | 895 | /** Add Time a action to rule |
| 896 | - * @param string $time : time to process to insert in SQL |
|
| 897 | - */ |
|
| 896 | + * @param string $time : time to process to insert in SQL |
|
| 897 | + */ |
|
| 898 | 898 | public function add_rule_final($time) |
| 899 | 899 | { |
| 900 | 900 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -911,9 +911,9 @@ discard block |
||
| 911 | 911 | /*********** UTILITIES *********************/ |
| 912 | 912 | |
| 913 | 913 | /** reset service to OK after time defined in rule |
| 914 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 915 | - * @return integer : not in use |
|
| 916 | - **/ |
|
| 914 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 915 | + * @return integer : not in use |
|
| 916 | + **/ |
|
| 917 | 917 | public function reset_services() |
| 918 | 918 | { |
| 919 | 919 | // Get all services not in 'ok' state |