@@ -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 |
@@ -8,19 +8,19 @@ discard block |
||
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 | 21 | protected $curl; |
22 | 22 | // http://php.net/manual/de/function.json-last-error.php#119985 |
23 | - protected $errorReference = [ |
|
23 | + protected $errorReference=[ |
|
24 | 24 | JSON_ERROR_NONE => 'No error has occurred.', |
25 | 25 | JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.', |
26 | 26 | JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.', |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.', |
32 | 32 | JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.', |
33 | 33 | ]; |
34 | - const JSON_UNKNOWN_ERROR = 'Unknown error.'; |
|
34 | + const JSON_UNKNOWN_ERROR='Unknown error.'; |
|
35 | 35 | |
36 | 36 | /** |
37 | 37 | * Creates Icinga2API object |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * @param string $host host name or IP |
40 | 40 | * @param number $port API port |
41 | 41 | */ |
42 | - public function __construct($host, $port = 5665) |
|
42 | + public function __construct($host, $port=5665) |
|
43 | 43 | { |
44 | 44 | $this->host=$host; |
45 | 45 | $this->port=$port; |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @param string $user |
50 | 50 | * @param string $pass |
51 | 51 | */ |
52 | - public function setCredentials($user,$pass) |
|
52 | + public function setCredentials($user, $pass) |
|
53 | 53 | { |
54 | 54 | $this->user=$user; |
55 | 55 | $this->pass=$pass; |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * @param string $user |
62 | 62 | * @param string $usercert |
63 | 63 | */ |
64 | - public function setCredentialskey($user,$usercert) |
|
64 | + public function setCredentialskey($user, $usercert) |
|
65 | 65 | { |
66 | 66 | $this->user=$user; |
67 | 67 | $this->usercert=$usercert; |
@@ -85,13 +85,13 @@ discard block |
||
85 | 85 | if (property_exists($result, 'results') && property_exists($result->results[0], 'permissions')) |
86 | 86 | { |
87 | 87 | |
88 | - foreach ( $permissions as $mustPermission) |
|
88 | + foreach ($permissions as $mustPermission) |
|
89 | 89 | { |
90 | 90 | $curPermOK=0; |
91 | - foreach ( $result->results[0]->permissions as $curPermission) |
|
91 | + foreach ($result->results[0]->permissions as $curPermission) |
|
92 | 92 | { |
93 | - $curPermission=preg_replace('/\*/','.*',$curPermission); // put * as .* to created a regexp |
|
94 | - if (preg_match('#'.$curPermission.'#',$mustPermission)) |
|
93 | + $curPermission=preg_replace('/\*/', '.*', $curPermission); // put * as .* to created a regexp |
|
94 | + if (preg_match('#'.$curPermission.'#', $mustPermission)) |
|
95 | 95 | { |
96 | 96 | $curPermOK=1; |
97 | 97 | break; |
@@ -106,12 +106,12 @@ discard block |
||
106 | 106 | } |
107 | 107 | if ($permOk == 0) |
108 | 108 | { |
109 | - return array(true,'API connection OK, but missing permission : '.$permMissing); |
|
109 | + return array(true, 'API connection OK, but missing permission : '.$permMissing); |
|
110 | 110 | } |
111 | - return array(false,'API connection OK'); |
|
111 | + return array(false, 'API connection OK'); |
|
112 | 112 | |
113 | 113 | } |
114 | - return array(true,'API connection OK, but cannot get permissions'); |
|
114 | + return array(true, 'API connection OK, but cannot get permissions'); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | |
@@ -126,9 +126,9 @@ discard block |
||
126 | 126 | */ |
127 | 127 | protected function curl() { |
128 | 128 | if ($this->curl === null) { |
129 | - $this->curl = curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
129 | + $this->curl=curl_init(sprintf('https://%s:%d', $this->host, $this->port)); |
|
130 | 130 | if (!$this->curl) { |
131 | - throw new Exception('CURL INIT ERROR: ' . curl_error($this->curl)); |
|
131 | + throw new Exception('CURL INIT ERROR: '.curl_error($this->curl)); |
|
132 | 132 | } |
133 | 133 | } |
134 | 134 | return $this->curl; |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | * @param string $perfdata : performance data as string |
144 | 144 | * @return array (status = true (oK) or false (nok), string message) |
145 | 145 | */ |
146 | - public function serviceCheckResult($host,$service,$state,$display,$perfdata='') |
|
146 | + public function serviceCheckResult($host, $service, $state, $display, $perfdata='') |
|
147 | 147 | { |
148 | 148 | //Send a POST request to the URL endpoint /v1/actions/process-check-result |
149 | 149 | //actions/process-check-result?service=example.localdomain!passive-ping6 |
@@ -162,9 +162,9 @@ discard block |
||
162 | 162 | { |
163 | 163 | return array(false, $e->getMessage()); |
164 | 164 | } |
165 | - if (property_exists($result,'error') ) |
|
165 | + if (property_exists($result, 'error')) |
|
166 | 166 | { |
167 | - if (property_exists($result,'status')) |
|
167 | + if (property_exists($result, 'status')) |
|
168 | 168 | { |
169 | 169 | $message=$result->status; |
170 | 170 | } |
@@ -172,21 +172,21 @@ discard block |
||
172 | 172 | { |
173 | 173 | $message="Unkown status"; |
174 | 174 | } |
175 | - return array(false , 'Ret code ' .$result->error.' : '.$message); |
|
175 | + return array(false, 'Ret code '.$result->error.' : '.$message); |
|
176 | 176 | } |
177 | 177 | if (property_exists($result, 'results')) |
178 | 178 | { |
179 | 179 | if (isset($result->results[0])) |
180 | 180 | { |
181 | - return array(true,'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
181 | + return array(true, 'code '.$result->results[0]->code.' : '.$result->results[0]->status); |
|
182 | 182 | } |
183 | 183 | else |
184 | 184 | { |
185 | - return array(false,'Service not found'); |
|
185 | + return array(false, 'Service not found'); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | } |
189 | - return array(false,'Unkown result, open issue with this : '.print_r($result,true)); |
|
189 | + return array(false, 'Unkown result, open issue with this : '.print_r($result, true)); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | $url='objects/hosts'; |
208 | 208 | $body=array( |
209 | 209 | "filter" => 'host.address=="'.$ip.'" || host.address6=="'.$ip.'"', |
210 | - "attrs" => array('__name','name','display_name') |
|
210 | + "attrs" => array('__name', 'name', 'display_name') |
|
211 | 211 | ); |
212 | 212 | try |
213 | 213 | { |
@@ -217,24 +217,24 @@ discard block |
||
217 | 217 | throw new Exception($e->getMessage()); |
218 | 218 | } |
219 | 219 | |
220 | - if (property_exists($result,'error') ) |
|
220 | + if (property_exists($result, 'error')) |
|
221 | 221 | { |
222 | - if (property_exists($result,'status')) |
|
222 | + if (property_exists($result, 'status')) |
|
223 | 223 | { |
224 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
224 | + throw new Exception('Ret code '.$result->error.' : '.$result->status); |
|
225 | 225 | } |
226 | 226 | else |
227 | 227 | { |
228 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
228 | + throw new Exception('Ret code '.$result->error.' : Unkown status'); |
|
229 | 229 | } |
230 | 230 | } |
231 | 231 | if (property_exists($result, 'results')) |
232 | 232 | { |
233 | 233 | $numHost=0; |
234 | 234 | $hostArray=array(); |
235 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
235 | + while (isset($result->results[$numHost]) && property_exists($result->results[$numHost], 'attrs')) |
|
236 | 236 | { |
237 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
237 | + $hostArray[$numHost]=$result->results[$numHost]->attrs; |
|
238 | 238 | $numHost++; |
239 | 239 | } |
240 | 240 | return $hostArray; |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | $url='objects/hosts'; |
254 | 254 | $body=array( |
255 | 255 | "filter" => '\"'.$hostGroup.'\" in host.groups', |
256 | - "attrs" => array('address','address','name') |
|
256 | + "attrs" => array('address', 'address', 'name') |
|
257 | 257 | ); |
258 | 258 | try |
259 | 259 | { |
@@ -263,25 +263,25 @@ discard block |
||
263 | 263 | throw new Exception($e->getMessage()); |
264 | 264 | } |
265 | 265 | |
266 | - if (property_exists($result,'error') ) |
|
266 | + if (property_exists($result, 'error')) |
|
267 | 267 | { |
268 | - if (property_exists($result,'status')) |
|
268 | + if (property_exists($result, 'status')) |
|
269 | 269 | { |
270 | - throw new Exception('Ret code ' .$result->error.' : ' . $result->status); |
|
270 | + throw new Exception('Ret code '.$result->error.' : '.$result->status); |
|
271 | 271 | } |
272 | 272 | else |
273 | 273 | { |
274 | - throw new Exception('Ret code ' .$result->error.' : Unkown status'); |
|
274 | + throw new Exception('Ret code '.$result->error.' : Unkown status'); |
|
275 | 275 | } |
276 | 276 | } |
277 | 277 | if (property_exists($result, 'results')) |
278 | 278 | { |
279 | 279 | $numHost=0; |
280 | 280 | $hostArray=array(); |
281 | - while (isset($result->results[$numHost]) && property_exists ($result->results[$numHost],'attrs')) |
|
281 | + while (isset($result->results[$numHost]) && property_exists($result->results[$numHost], 'attrs')) |
|
282 | 282 | { |
283 | - $hostArray[$numHost] = $result->results[$numHost]->attrs; |
|
284 | - $hostArray[$numHost]->name = $result->results[$numHost]->name; |
|
283 | + $hostArray[$numHost]=$result->results[$numHost]->attrs; |
|
284 | + $hostArray[$numHost]->name=$result->results[$numHost]->name; |
|
285 | 285 | $numHost++; |
286 | 286 | } |
287 | 287 | return $hostArray; |
@@ -299,23 +299,23 @@ discard block |
||
299 | 299 | * @return array |
300 | 300 | */ |
301 | 301 | public function request($method, $url, $headers, $body) { |
302 | - $auth = sprintf('%s:%s', $this->user, $this->pass); |
|
303 | - $curlHeaders = array("Accept: application/json"); |
|
302 | + $auth=sprintf('%s:%s', $this->user, $this->pass); |
|
303 | + $curlHeaders=array("Accept: application/json"); |
|
304 | 304 | if ($body !== null) { |
305 | - $body = json_encode($body); |
|
305 | + $body=json_encode($body); |
|
306 | 306 | array_push($curlHeaders, 'Content-Type: application/json'); |
307 | 307 | //array_push($curlHeaders, 'X-HTTP-Method-Override: GET'); |
308 | 308 | } |
309 | 309 | //var_dump($body); |
310 | 310 | //var_dump($this->url($url)); |
311 | 311 | if ($headers !== null) { |
312 | - $curlFinalHeaders = array_merge($curlHeaders, $headers); |
|
312 | + $curlFinalHeaders=array_merge($curlHeaders, $headers); |
|
313 | 313 | } else |
314 | 314 | { |
315 | 315 | $curlFinalHeaders=$curlHeaders; |
316 | 316 | } |
317 | - $curl = $this->curl(); |
|
318 | - $opts = array( |
|
317 | + $curl=$this->curl(); |
|
318 | + $opts=array( |
|
319 | 319 | CURLOPT_URL => $this->url($url), |
320 | 320 | CURLOPT_HTTPHEADER => $curlFinalHeaders, |
321 | 321 | CURLOPT_USERPWD => $auth, |
@@ -326,14 +326,14 @@ discard block |
||
326 | 326 | CURLOPT_SSL_VERIFYPEER => false, |
327 | 327 | ); |
328 | 328 | if ($body !== null) { |
329 | - $opts[CURLOPT_POSTFIELDS] = $body; |
|
329 | + $opts[CURLOPT_POSTFIELDS]=$body; |
|
330 | 330 | } |
331 | 331 | curl_setopt_array($curl, $opts); |
332 | - $res = curl_exec($curl); |
|
332 | + $res=curl_exec($curl); |
|
333 | 333 | if ($res === false) { |
334 | - throw new Exception('CURL ERROR: ' . curl_error($curl)); |
|
334 | + throw new Exception('CURL ERROR: '.curl_error($curl)); |
|
335 | 335 | } |
336 | - $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
336 | + $statusCode=curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
337 | 337 | if ($statusCode === 401) { |
338 | 338 | throw new Exception('Unable to authenticate, please check your API credentials'); |
339 | 339 | } |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | * @return array json decoded |
348 | 348 | */ |
349 | 349 | protected function fromJsonResult($json) { |
350 | - $result = @json_decode($json); |
|
350 | + $result=@json_decode($json); |
|
351 | 351 | //var_dump($json); |
352 | 352 | if ($result === null) { |
353 | 353 | throw new Exception('Parsing JSON failed: '.$this->getLastJsonErrorMessage(json_last_error())); |
@@ -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 |
@@ -36,8 +36,8 @@ discard block |
||
36 | 36 | protected $api_password=''; |
37 | 37 | |
38 | 38 | // Logs |
39 | - protected $logging; //< Logging class. |
|
40 | - protected $logSetup; //< bool true if log was setup in constructor |
|
39 | + protected $logging; //< Logging class. |
|
40 | + protected $logSetup; //< bool true if log was setup in constructor |
|
41 | 41 | |
42 | 42 | // Databases |
43 | 43 | public $trapsDB; //< Database class |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | // Rule evaluation |
57 | 57 | public $ruleClass; // Rule class |
58 | 58 | |
59 | - function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
59 | + function __construct($etc_dir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='') |
|
60 | 60 | { |
61 | 61 | // Paths of ini files |
62 | 62 | $this->icingaweb2_etc=$etc_dir; |
@@ -64,10 +64,10 @@ discard block |
||
64 | 64 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
65 | 65 | |
66 | 66 | // Setup logging |
67 | - $this->logging = new Logging(); |
|
67 | + $this->logging=new Logging(); |
|
68 | 68 | if ($baseLogLevel != null) |
69 | 69 | { |
70 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
70 | + $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile); |
|
71 | 71 | $this->logSetup=true; |
72 | 72 | } |
73 | 73 | else |
@@ -75,10 +75,10 @@ discard block |
||
75 | 75 | $this->logging->log('Loggin started', INFO); |
76 | 76 | |
77 | 77 | // Get options from ini files |
78 | - $trapConfig=parse_ini_file($this->trap_module_config,true); |
|
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 |
@@ -86,9 +86,9 @@ discard block |
||
86 | 86 | $this->getDatabaseOptions(); // Get options in database |
87 | 87 | if ($this->api_use === true) $this->getAPI(); // Setup API |
88 | 88 | |
89 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
89 | + $this->mibClass=new Mib($this->logging, $this->trapsDB, $this->snmptranslate, $this->snmptranslate_dirs); // Create Mib class |
|
90 | 90 | |
91 | - $this->ruleClass = new Rule($this->logging); //< Create Rule class |
|
91 | + $this->ruleClass=new Rule($this->logging); //< Create Rule class |
|
92 | 92 | |
93 | 93 | $this->trap_data=array( |
94 | 94 | 'source_ip' => 'unknown', |
@@ -110,15 +110,15 @@ discard block |
||
110 | 110 | * @param string $message warning message if not found |
111 | 111 | * @return boolean true if found, or false |
112 | 112 | */ |
113 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
113 | + protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null) |
|
114 | 114 | { |
115 | 115 | if (!isset($option_array[$option_category][$option_name])) |
116 | 116 | { |
117 | 117 | if ($message === null) |
118 | 118 | { |
119 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
119 | + $message='No '.$option_name.' in config file: '.$this->trap_module_config; |
|
120 | 120 | } |
121 | - $this->logging->log($message,$log_level,'syslog'); |
|
121 | + $this->logging->log($message, $log_level, 'syslog'); |
|
122 | 122 | return false; |
123 | 123 | } |
124 | 124 | else |
@@ -136,24 +136,24 @@ discard block |
||
136 | 136 | { |
137 | 137 | |
138 | 138 | // Snmptranslate binary path |
139 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
139 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate); |
|
140 | 140 | |
141 | 141 | // mibs path |
142 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
142 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs); |
|
143 | 143 | |
144 | 144 | // icinga2cmd path |
145 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
145 | + $this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd); |
|
146 | 146 | |
147 | 147 | // table prefix |
148 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->db_prefix); |
|
148 | + $this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->db_prefix); |
|
149 | 149 | |
150 | 150 | // API options |
151 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
|
151 | + if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->api_hostname)) |
|
152 | 152 | { |
153 | 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); |
|
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 | |
@@ -164,40 +164,40 @@ discard block |
||
164 | 164 | protected function setupDatabase($trapConfig) |
165 | 165 | { |
166 | 166 | // Trap database |
167 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
167 | + if (!array_key_exists('database', $trapConfig['config'])) |
|
168 | 168 | { |
169 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
169 | + $this->logging->log("No database in config file: ".$this->trap_module_config, ERROR, ''); |
|
170 | 170 | return; |
171 | 171 | } |
172 | 172 | $dbTrapName=$trapConfig['config']['database']; |
173 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
173 | + $this->logging->log("Found database in config file: ".$dbTrapName, INFO); |
|
174 | 174 | |
175 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
|
175 | + if (($dbConfig=parse_ini_file($this->icingaweb2_ressources, true)) === false) |
|
176 | 176 | { |
177 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
177 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources, ERROR, ''); |
|
178 | 178 | return; |
179 | 179 | } |
180 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
180 | + if (!array_key_exists($dbTrapName, $dbConfig)) |
|
181 | 181 | { |
182 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
182 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources, ERROR, ''); |
|
183 | 183 | return; |
184 | 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 | 188 | if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
189 | 189 | |
190 | 190 | // IDO Database |
191 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
191 | + if (!array_key_exists('IDOdatabase', $trapConfig['config'])) |
|
192 | 192 | { |
193 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
193 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config, ERROR, ''); |
|
194 | 194 | } |
195 | 195 | $dbIdoName=$trapConfig['config']['IDOdatabase']; |
196 | 196 | |
197 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
198 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
197 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO); |
|
198 | + if (!array_key_exists($dbIdoName, $dbConfig)) |
|
199 | 199 | { |
200 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
200 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources, ERROR, ''); |
|
201 | 201 | return; |
202 | 202 | } |
203 | 203 | |
@@ -212,13 +212,13 @@ 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 | |
221 | - protected function getDBConfigIfSet($element,&$variable) |
|
221 | + protected function getDBConfigIfSet($element, &$variable) |
|
222 | 222 | { |
223 | 223 | $value=$this->getDBConfig($element); |
224 | 224 | if ($value != 'null') $variable=$value; |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | $db_conn=$this->trapsDB->db_connect_trap(); |
235 | 235 | $sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )'; |
236 | 236 | if (($ret_code=$db_conn->query($sql)) === false) { |
237 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
237 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
238 | 238 | return null; |
239 | 239 | } |
240 | 240 | $value=$ret_code->fetch(); |
@@ -251,22 +251,22 @@ discard block |
||
251 | 251 | * @param string $destination file/syslog/display |
252 | 252 | * @return void |
253 | 253 | **/ |
254 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
254 | + public function trapLog($message, $level, $destination='') // OBSOLETE |
|
255 | 255 | { |
256 | 256 | // TODO : replace ref with $this->logging->log |
257 | 257 | $this->logging->log($message, $level, $destination); |
258 | 258 | } |
259 | 259 | |
260 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
260 | + public function setLogging($debugLvl, $outputType, $outputOption=null) // OBSOLETE |
|
261 | 261 | { |
262 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
262 | + $this->logging->setLogging($debugLvl, $outputType, $outputOption); |
|
263 | 263 | } |
264 | 264 | |
265 | 265 | protected function getAPI() |
266 | 266 | { |
267 | 267 | if ($this->icinga2api == null) |
268 | 268 | { |
269 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
269 | + $this->icinga2api=new Icinga2API($this->api_hostname, $this->api_port); |
|
270 | 270 | } |
271 | 271 | return $this->icinga2api; |
272 | 272 | } |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | if ($input_stream === false) |
286 | 286 | { |
287 | 287 | $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
288 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
288 | + $this->logging->log("Error reading stdin !", ERROR, ''); |
|
289 | 289 | return null; // note : exception thrown by logging |
290 | 290 | } |
291 | 291 | |
@@ -294,21 +294,21 @@ discard block |
||
294 | 294 | if ($this->receivingHost === false) |
295 | 295 | { |
296 | 296 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
297 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
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 | 303 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
304 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
304 | + $this->logging->log("Error reading IP !", ERROR, ''); |
|
305 | 305 | } |
306 | 306 | $matches=array(); |
307 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
308 | - if ($ret_code===0 || $ret_code===false) |
|
307 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches); |
|
308 | + if ($ret_code === 0 || $ret_code === false) |
|
309 | 309 | { |
310 | 310 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
311 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
311 | + $this->logging->log('Error parsing IP : '.$IP, ERROR, ''); |
|
312 | 312 | } |
313 | 313 | else |
314 | 314 | { |
@@ -318,34 +318,34 @@ discard block |
||
318 | 318 | $this->trap_data['destination_port']=$matches[4]; |
319 | 319 | } |
320 | 320 | |
321 | - while (($vars=fgets($input_stream)) !==false) |
|
321 | + while (($vars=fgets($input_stream)) !== false) |
|
322 | 322 | { |
323 | 323 | $vars=chop($vars); |
324 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
325 | - if ($ret_code===0 || $ret_code===false) |
|
324 | + $ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches); |
|
325 | + if ($ret_code === 0 || $ret_code === false) |
|
326 | 326 | { |
327 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
327 | + $this->logging->log('No match on trap data : '.$vars, WARN, ''); |
|
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 | } |
335 | 335 | else |
336 | 336 | { |
337 | - $object= new stdClass; |
|
338 | - $object->oid =$matches[1]; |
|
339 | - $object->value = $matches[2]; |
|
340 | - array_push($this->trap_data_ext,$object); |
|
337 | + $object=new stdClass; |
|
338 | + $object->oid=$matches[1]; |
|
339 | + $object->value=$matches[2]; |
|
340 | + array_push($this->trap_data_ext, $object); |
|
341 | 341 | } |
342 | 342 | } |
343 | 343 | } |
344 | 344 | |
345 | - if ($this->trap_data['trap_oid']=='unknown') |
|
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']); |
|
348 | - $this->logging->log('no trap oid found',ERROR,''); |
|
347 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trap_data['source_ip']); |
|
348 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
349 | 349 | } |
350 | 350 | |
351 | 351 | // Translate oids. |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | } |
368 | 368 | |
369 | 369 | |
370 | - $this->trap_data['status']= 'waiting'; |
|
370 | + $this->trap_data['status']='waiting'; |
|
371 | 371 | |
372 | 372 | return $this->trap_data; |
373 | 373 | } |
@@ -383,40 +383,40 @@ discard block |
||
383 | 383 | $db_conn=$this->trapsDB->db_connect_trap(); |
384 | 384 | |
385 | 385 | $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
386 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
386 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
387 | 387 | if (($ret_code=$db_conn->query($sql)) === false) { |
388 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
388 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
389 | 389 | } |
390 | 390 | $name=$ret_code->fetch(); |
391 | 391 | if ($name['name'] != null) |
392 | 392 | { |
393 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
393 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
394 | 394 | } |
395 | 395 | |
396 | 396 | // Also check if it is an instance of OID |
397 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
397 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
398 | 398 | |
399 | 399 | $sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
400 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
400 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
401 | 401 | if (($ret_code=$db_conn->query($sql)) === false) { |
402 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
402 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
403 | 403 | } |
404 | 404 | $name=$ret_code->fetch(); |
405 | 405 | if ($name['name'] != null) |
406 | 406 | { |
407 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
407 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
408 | 408 | } |
409 | 409 | |
410 | 410 | // Try to get oid name from snmptranslate |
411 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
411 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
412 | 412 | ' '.$oid); |
413 | 413 | $matches=array(); |
414 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
415 | - if ($ret_code===0 || $ret_code === false) { |
|
414 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
415 | + if ($ret_code === 0 || $ret_code === false) { |
|
416 | 416 | return NULL; |
417 | 417 | } else { |
418 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
419 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
418 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
419 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
420 | 420 | } |
421 | 421 | } |
422 | 422 | |
@@ -427,90 +427,90 @@ discard block |
||
427 | 427 | **/ |
428 | 428 | public function eraseOldTraps($days=0) |
429 | 429 | { |
430 | - if ($days==0) |
|
430 | + if ($days == 0) |
|
431 | 431 | { |
432 | 432 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
433 | 433 | { |
434 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
434 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
435 | 435 | return; |
436 | 436 | } |
437 | 437 | } |
438 | 438 | $db_conn=$this->trapsDB->db_connect_trap(); |
439 | - $daysago = strtotime("-".$days." day"); |
|
440 | - $sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
439 | + $daysago=strtotime("-".$days." day"); |
|
440 | + $sql='delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
441 | 441 | if ($db_conn->query($sql) === false) { |
442 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
442 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
443 | 443 | } |
444 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
444 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
445 | 445 | } |
446 | 446 | |
447 | 447 | /** Write error to received trap database |
448 | 448 | */ |
449 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
449 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
450 | 450 | { |
451 | 451 | |
452 | 452 | $db_conn=$this->trapsDB->db_connect_trap(); |
453 | 453 | |
454 | 454 | // add date time |
455 | - $insert_col ='date_received,status'; |
|
456 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
455 | + $insert_col='date_received,status'; |
|
456 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
457 | 457 | |
458 | - if ($sourceIP !=null) |
|
458 | + if ($sourceIP != null) |
|
459 | 459 | { |
460 | - $insert_col .=',source_ip'; |
|
461 | - $insert_val .=",'". $sourceIP ."'"; |
|
460 | + $insert_col.=',source_ip'; |
|
461 | + $insert_val.=",'".$sourceIP."'"; |
|
462 | 462 | } |
463 | - if ($trapoid !=null) |
|
463 | + if ($trapoid != null) |
|
464 | 464 | { |
465 | - $insert_col .=',trap_oid'; |
|
466 | - $insert_val .=",'". $trapoid ."'"; |
|
465 | + $insert_col.=',trap_oid'; |
|
466 | + $insert_val.=",'".$trapoid."'"; |
|
467 | 467 | } |
468 | - $insert_col .=',status_detail'; |
|
469 | - $insert_val .=",'". $message ."'"; |
|
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 | 473 | switch ($this->trapsDB->trapDBType) |
474 | 474 | { |
475 | 475 | case 'pgsql': |
476 | - $sql .= ' RETURNING id;'; |
|
477 | - $this->logging->log('sql : '.$sql,INFO); |
|
476 | + $sql.=' RETURNING id;'; |
|
477 | + $this->logging->log('sql : '.$sql, INFO); |
|
478 | 478 | if (($ret_code=$db_conn->query($sql)) === false) { |
479 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
479 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
480 | 480 | } |
481 | - $this->logging->log('SQL insertion OK',INFO ); |
|
481 | + $this->logging->log('SQL insertion OK', INFO); |
|
482 | 482 | // Get last id to insert oid/values in secondary table |
483 | 483 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
484 | 484 | |
485 | - $this->logging->log('Erreur recuperation id',1,''); |
|
485 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
486 | 486 | } |
487 | - if (! isset($inserted_id_ret['id'])) { |
|
488 | - $this->logging->log('Error getting id',1,''); |
|
487 | + if (!isset($inserted_id_ret['id'])) { |
|
488 | + $this->logging->log('Error getting id', 1, ''); |
|
489 | 489 | } |
490 | 490 | $this->trap_id=$inserted_id_ret['id']; |
491 | 491 | break; |
492 | 492 | case 'mysql': |
493 | - $sql .= ';'; |
|
494 | - $this->logging->log('sql : '.$sql,INFO ); |
|
493 | + $sql.=';'; |
|
494 | + $this->logging->log('sql : '.$sql, INFO); |
|
495 | 495 | if ($db_conn->query($sql) === false) { |
496 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
496 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
497 | 497 | } |
498 | - $this->logging->log('SQL insertion OK',INFO ); |
|
498 | + $this->logging->log('SQL insertion OK', INFO); |
|
499 | 499 | // Get last id to insert oid/values in secondary table |
500 | 500 | $sql='SELECT LAST_INSERT_ID();'; |
501 | 501 | if (($ret_code=$db_conn->query($sql)) === false) { |
502 | - $this->logging->log('Erreur recuperation id',1,''); |
|
502 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
503 | 503 | } |
504 | 504 | |
505 | 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"); |
|
506 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
507 | 507 | $this->trap_id=$inserted_id; |
508 | 508 | break; |
509 | 509 | default: |
510 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
510 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
511 | 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 |
@@ -527,86 +527,86 @@ discard block |
||
527 | 527 | $insert_col=''; |
528 | 528 | $insert_val=''; |
529 | 529 | // add date time |
530 | - $this->trap_data['date_received'] = date("Y-m-d H:i:s"); |
|
530 | + $this->trap_data['date_received']=date("Y-m-d H:i:s"); |
|
531 | 531 | |
532 | 532 | $firstcol=1; |
533 | 533 | foreach ($this->trap_data as $col => $val) |
534 | 534 | { |
535 | - if ($firstcol==0) |
|
535 | + if ($firstcol == 0) |
|
536 | 536 | { |
537 | - $insert_col .=','; |
|
538 | - $insert_val .=','; |
|
537 | + $insert_col.=','; |
|
538 | + $insert_val.=','; |
|
539 | 539 | } |
540 | - $insert_col .= $col ; |
|
541 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
540 | + $insert_col.=$col; |
|
541 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
542 | 542 | $firstcol=0; |
543 | 543 | } |
544 | 544 | |
545 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
545 | + $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
546 | 546 | switch ($this->trapsDB->trapDBType) |
547 | 547 | { |
548 | 548 | case 'pgsql': |
549 | - $sql .= ' RETURNING id;'; |
|
550 | - $this->logging->log('sql : '.$sql,INFO ); |
|
549 | + $sql.=' RETURNING id;'; |
|
550 | + $this->logging->log('sql : '.$sql, INFO); |
|
551 | 551 | if (($ret_code=$db_conn->query($sql)) === false) { |
552 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
552 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
553 | 553 | } |
554 | - $this->logging->log('SQL insertion OK',INFO ); |
|
554 | + $this->logging->log('SQL insertion OK', INFO); |
|
555 | 555 | // Get last id to insert oid/values in secondary table |
556 | 556 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
557 | 557 | |
558 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
558 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
559 | 559 | } |
560 | - if (! isset($inserted_id_ret['id'])) { |
|
561 | - $this->logging->log('Error getting id',ERROR,''); |
|
560 | + if (!isset($inserted_id_ret['id'])) { |
|
561 | + $this->logging->log('Error getting id', ERROR, ''); |
|
562 | 562 | } |
563 | 563 | $this->trap_id=$inserted_id_ret['id']; |
564 | 564 | break; |
565 | 565 | case 'mysql': |
566 | - $sql .= ';'; |
|
567 | - $this->logging->log('sql : '.$sql,INFO ); |
|
566 | + $sql.=';'; |
|
567 | + $this->logging->log('sql : '.$sql, INFO); |
|
568 | 568 | if ($db_conn->query($sql) === false) { |
569 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
569 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
570 | 570 | } |
571 | - $this->logging->log('SQL insertion OK',INFO ); |
|
571 | + $this->logging->log('SQL insertion OK', INFO); |
|
572 | 572 | // Get last id to insert oid/values in secondary table |
573 | 573 | $sql='SELECT LAST_INSERT_ID();'; |
574 | 574 | if (($ret_code=$db_conn->query($sql)) === false) { |
575 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
575 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
576 | 576 | } |
577 | 577 | |
578 | 578 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
579 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
579 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
580 | 580 | $this->trap_id=$inserted_id; |
581 | 581 | break; |
582 | 582 | default: |
583 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
583 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
584 | 584 | } |
585 | - $this->logging->log('id found: '.$this->trap_id,INFO ); |
|
585 | + $this->logging->log('id found: '.$this->trap_id, INFO); |
|
586 | 586 | |
587 | 587 | // Fill trap extended data table |
588 | 588 | foreach ($this->trap_data_ext as $value) { |
589 | 589 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
590 | 590 | $firstcol=1; |
591 | - $value->trap_id = $this->trap_id; |
|
591 | + $value->trap_id=$this->trap_id; |
|
592 | 592 | $insert_col=''; |
593 | 593 | $insert_val=''; |
594 | 594 | foreach ($value as $col => $val) |
595 | 595 | { |
596 | - if ($firstcol==0) |
|
596 | + if ($firstcol == 0) |
|
597 | 597 | { |
598 | - $insert_col .=','; |
|
599 | - $insert_val .=','; |
|
598 | + $insert_col.=','; |
|
599 | + $insert_val.=','; |
|
600 | 600 | } |
601 | - $insert_col .= $col; |
|
602 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
601 | + $insert_col.=$col; |
|
602 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
603 | 603 | $firstcol=0; |
604 | 604 | } |
605 | 605 | |
606 | - $sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
606 | + $sql='INSERT INTO '.$this->db_prefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
607 | 607 | |
608 | 608 | if ($db_conn->query($sql) === false) { |
609 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
609 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
610 | 610 | } |
611 | 611 | } |
612 | 612 | } |
@@ -616,14 +616,14 @@ discard block |
||
616 | 616 | * @param $oid string oid in numeric |
617 | 617 | * @return mixed|boolean : PDO object or false |
618 | 618 | */ |
619 | - protected function getRules($ip,$oid) |
|
619 | + protected function getRules($ip, $oid) |
|
620 | 620 | { |
621 | 621 | $db_conn=$this->trapsDB->db_connect_trap(); |
622 | 622 | // fetch rules based on IP in rule and OID |
623 | 623 | $sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
624 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
624 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
625 | 625 | if (($ret_code=$db_conn->query($sql)) === false) { |
626 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
626 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
627 | 627 | return false; |
628 | 628 | } |
629 | 629 | $rules_all=$ret_code->fetchAll(); |
@@ -632,7 +632,7 @@ discard block |
||
632 | 632 | $rule_ret_key=0; |
633 | 633 | foreach ($rules_all as $key => $rule) |
634 | 634 | { |
635 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
635 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
636 | 636 | { |
637 | 637 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
638 | 638 | //TODO : get host name by API (and check if correct in rule). |
@@ -640,7 +640,7 @@ discard block |
||
640 | 640 | continue; |
641 | 641 | } |
642 | 642 | // TODO : get hosts IP by API |
643 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
643 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
644 | 644 | { // get ips of group members by oid |
645 | 645 | $db_conn2=$this->trapsDB->db_connect_ido(); |
646 | 646 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -651,15 +651,15 @@ discard block |
||
651 | 651 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
652 | 652 | WHERE o.name1='".$rule['host_group_name']."';"; |
653 | 653 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
654 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
654 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
655 | 655 | continue; |
656 | 656 | } |
657 | 657 | $grouphosts=$ret_code2->fetchAll(); |
658 | 658 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
659 | - foreach ( $grouphosts as $host) |
|
659 | + foreach ($grouphosts as $host) |
|
660 | 660 | { |
661 | 661 | //echo $host['ip4']."\n"; |
662 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
662 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
663 | 663 | { |
664 | 664 | //echo "Rule added \n"; |
665 | 665 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -682,7 +682,7 @@ discard block |
||
682 | 682 | $db_conn=$this->trapsDB->db_connect_trap(); |
683 | 683 | $sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
684 | 684 | if ($db_conn->query($sql) === false) { |
685 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
685 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
686 | 686 | } |
687 | 687 | } |
688 | 688 | |
@@ -694,23 +694,23 @@ discard block |
||
694 | 694 | * @param string $display |
695 | 695 | * @returnn bool true is service check was sent without error |
696 | 696 | */ |
697 | - public function serviceCheckResult($host,$service,$state,$display) |
|
697 | + public function serviceCheckResult($host, $service, $state, $display) |
|
698 | 698 | { |
699 | 699 | if ($this->api_use === false) |
700 | 700 | { |
701 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
702 | - $host.';' .$service .';' . $state . ';'.$display; |
|
703 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
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 | 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); |
|
706 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
707 | 707 | return true; |
708 | 708 | } |
709 | 709 | else |
710 | 710 | { |
711 | 711 | // Get perfdata if found |
712 | 712 | $matches=array(); |
713 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
713 | + if (preg_match('/(.*)\|(.*)/', $display, $matches) == 1) |
|
714 | 714 | { |
715 | 715 | $display=$matches[1]; |
716 | 716 | $perfdata=$matches[2]; |
@@ -720,17 +720,17 @@ discard block |
||
720 | 720 | $perfdata=''; |
721 | 721 | } |
722 | 722 | |
723 | - $api = $this->getAPI(); |
|
723 | + $api=$this->getAPI(); |
|
724 | 724 | $api->setCredentials($this->api_username, $this->api_password); |
725 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
725 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display, $perfdata); |
|
726 | 726 | if ($retcode == false) |
727 | 727 | { |
728 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
728 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
729 | 729 | return false; |
730 | 730 | } |
731 | 731 | else |
732 | 732 | { |
733 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
733 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
734 | 734 | return true; |
735 | 735 | } |
736 | 736 | } |
@@ -738,7 +738,7 @@ discard block |
||
738 | 738 | |
739 | 739 | public function getHostByIP($ip) |
740 | 740 | { |
741 | - $api = $this->getAPI(); |
|
741 | + $api=$this->getAPI(); |
|
742 | 742 | $api->setCredentials($this->api_username, $this->api_password); |
743 | 743 | return $api->getHostByIP($ip); |
744 | 744 | } |
@@ -751,32 +751,32 @@ discard block |
||
751 | 751 | protected function applyDisplay($display) |
752 | 752 | { |
753 | 753 | $matches=array(); |
754 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
754 | + while (preg_match('/_OID\(([0-9\.]+)\)/', $display, $matches) == 1) |
|
755 | 755 | { |
756 | 756 | $oid=$matches[1]; |
757 | 757 | $found=0; |
758 | - foreach($this->trap_data_ext as $val) |
|
758 | + foreach ($this->trap_data_ext as $val) |
|
759 | 759 | { |
760 | 760 | if ($oid == $val->oid) |
761 | 761 | { |
762 | - $val->value=preg_replace('/"/','',$val->value); |
|
762 | + $val->value=preg_replace('/"/', '', $val->value); |
|
763 | 763 | $rep=0; |
764 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
765 | - if ($rep==0) |
|
764 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
765 | + if ($rep == 0) |
|
766 | 766 | { |
767 | - $this->logging->log("Error in display",WARN,''); |
|
767 | + $this->logging->log("Error in display", WARN, ''); |
|
768 | 768 | return $display; |
769 | 769 | } |
770 | 770 | $found=1; |
771 | 771 | break; |
772 | 772 | } |
773 | 773 | } |
774 | - if ($found==0) |
|
774 | + if ($found == 0) |
|
775 | 775 | { |
776 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
777 | - if ($rep==0) |
|
776 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
777 | + if ($rep == 0) |
|
778 | 778 | { |
779 | - $this->logging->log("Error in display",WARN,''); |
|
779 | + $this->logging->log("Error in display", WARN, ''); |
|
780 | 780 | return $display; |
781 | 781 | } |
782 | 782 | } |
@@ -788,11 +788,11 @@ discard block |
||
788 | 788 | */ |
789 | 789 | public function applyRules() |
790 | 790 | { |
791 | - $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
|
791 | + $rules=$this->getRules($this->trap_data['source_ip'], $this->trap_data['trap_oid']); |
|
792 | 792 | |
793 | - if ($rules===false || count($rules)==0) |
|
793 | + if ($rules === false || count($rules) == 0) |
|
794 | 794 | { |
795 | - $this->logging->log('No rules found for this trap',INFO ); |
|
795 | + $this->logging->log('No rules found for this trap', INFO); |
|
796 | 796 | $this->trap_data['status']='unknown'; |
797 | 797 | $this->trap_to_db=true; |
798 | 798 | return; |
@@ -807,59 +807,59 @@ discard block |
||
807 | 807 | $service_name=$rule['service_name']; |
808 | 808 | |
809 | 809 | $display=$this->applyDisplay($rule['display']); |
810 | - $this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', '; |
|
810 | + $this->trap_action=($this->trap_action == null) ? '' : $this->trap_action.', '; |
|
811 | 811 | try |
812 | 812 | { |
813 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
814 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trap_data_ext) ; |
|
813 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
814 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trap_data_ext); |
|
815 | 815 | //->eval_rule($rule['rule']); |
816 | 816 | |
817 | 817 | if ($evalr == true) |
818 | 818 | { |
819 | 819 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
820 | 820 | $action=$rule['action_match']; |
821 | - $this->logging->log('action OK : '.$action,INFO ); |
|
821 | + $this->logging->log('action OK : '.$action, INFO); |
|
822 | 822 | if ($action >= 0) |
823 | 823 | { |
824 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
824 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
825 | 825 | { |
826 | 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); |
|
830 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
831 | 831 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
832 | 832 | } |
833 | 833 | } |
834 | 834 | else |
835 | 835 | { |
836 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
836 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
837 | 837 | } |
838 | - $this->trap_to_db=($action==-2)?false:true; |
|
838 | + $this->trap_to_db=($action == -2) ?false:true; |
|
839 | 839 | } |
840 | 840 | else |
841 | 841 | { |
842 | 842 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
843 | 843 | |
844 | 844 | $action=$rule['action_nomatch']; |
845 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
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) |
|
848 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
849 | 849 | { |
850 | 850 | $this->trap_action.='Error sending status : check cmd/API'; |
851 | 851 | } |
852 | 852 | else |
853 | 853 | { |
854 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
854 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
855 | 855 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
856 | 856 | } |
857 | 857 | } |
858 | 858 | else |
859 | 859 | { |
860 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
860 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
861 | 861 | } |
862 | - $this->trap_to_db=($action==-2)?false:true; |
|
862 | + $this->trap_to_db=($action == -2) ?false:true; |
|
863 | 863 | } |
864 | 864 | // Put name in source_name |
865 | 865 | if (!isset($this->trap_data['source_name'])) |
@@ -868,7 +868,7 @@ discard block |
||
868 | 868 | } |
869 | 869 | else |
870 | 870 | { |
871 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
|
871 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trap_data['source_name'])) |
|
872 | 872 | { // only add if not present |
873 | 873 | $this->trap_data['source_name'].=','.$rule['host_name']; |
874 | 874 | } |
@@ -876,13 +876,13 @@ discard block |
||
876 | 876 | } |
877 | 877 | catch (Exception $e) |
878 | 878 | { |
879 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
879 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
880 | 880 | $this->trap_action.=' ERR : '.$e->getMessage(); |
881 | 881 | $this->trap_data['status']='error'; |
882 | 882 | } |
883 | 883 | |
884 | 884 | } |
885 | - if ($this->trap_data['status']=='error') |
|
885 | + if ($this->trap_data['status'] == 'error') |
|
886 | 886 | { |
887 | 887 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
888 | 888 | } |
@@ -898,13 +898,13 @@ discard block |
||
898 | 898 | public function add_rule_final($time) |
899 | 899 | { |
900 | 900 | $db_conn=$this->trapsDB->db_connect_trap(); |
901 | - if ($this->trap_action==null) |
|
901 | + if ($this->trap_action == null) |
|
902 | 902 | { |
903 | 903 | $this->trap_action='No action'; |
904 | 904 | } |
905 | 905 | $sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."' WHERE (id = '".$this->trap_id."');"; |
906 | 906 | if ($db_conn->query($sql) === false) { |
907 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
907 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
908 | 908 | } |
909 | 909 | } |
910 | 910 | |
@@ -927,7 +927,7 @@ discard block |
||
927 | 927 | WHERE s.current_state != 0;"; |
928 | 928 | $db_conn=$this->trapsDB->db_connect_ido(); |
929 | 929 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
930 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
930 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
931 | 931 | return 0; |
932 | 932 | } |
933 | 933 | $services=$services_db->fetchAll(); |
@@ -936,7 +936,7 @@ discard block |
||
936 | 936 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;"; |
937 | 937 | $db_conn2=$this->trapsDB->db_connect_trap(); |
938 | 938 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
939 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
939 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
940 | 940 | return 0; |
941 | 941 | } |
942 | 942 | $rules=$rules_db->fetchAll(); |
@@ -952,13 +952,13 @@ discard block |
||
952 | 952 | $service['host_name'] == $rule['host_name'] && |
953 | 953 | ($service['last_check'] + $rule['revert_ok']) < $now) |
954 | 954 | { |
955 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
955 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
956 | 956 | $numreset++; |
957 | 957 | } |
958 | 958 | } |
959 | 959 | } |
960 | 960 | echo "\n"; |
961 | - echo $numreset . " service(s) reset to OK\n"; |
|
961 | + echo $numreset." service(s) reset to OK\n"; |
|
962 | 962 | return 0; |
963 | 963 | |
964 | 964 | } |
@@ -69,9 +69,9 @@ discard block |
||
69 | 69 | { |
70 | 70 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
71 | 71 | $this->logSetup=true; |
72 | + } else { |
|
73 | + $this->logSetup=false; |
|
72 | 74 | } |
73 | - else |
|
74 | - $this->logSetup=false; |
|
75 | 75 | $this->logging->log('Loggin started', INFO); |
76 | 76 | |
77 | 77 | // Get options from ini files |
@@ -84,7 +84,10 @@ discard block |
||
84 | 84 | $this->setupDatabase($trapConfig); // Setup database class |
85 | 85 | |
86 | 86 | $this->getDatabaseOptions(); // Get options in database |
87 | - if ($this->api_use === true) $this->getAPI(); // Setup API |
|
87 | + if ($this->api_use === true) { |
|
88 | + $this->getAPI(); |
|
89 | + } |
|
90 | + // Setup API |
|
88 | 91 | |
89 | 92 | $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
90 | 93 | |
@@ -120,8 +123,7 @@ discard block |
||
120 | 123 | } |
121 | 124 | $this->logging->log($message,$log_level,'syslog'); |
122 | 125 | return false; |
123 | - } |
|
124 | - else |
|
126 | + } else |
|
125 | 127 | { |
126 | 128 | $option_var=$option_array[$option_category][$option_name]; |
127 | 129 | return true; |
@@ -185,7 +187,10 @@ discard block |
||
185 | 187 | |
186 | 188 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
187 | 189 | |
188 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
190 | + if ($this->api_use === true) { |
|
191 | + return; |
|
192 | + } |
|
193 | + // In case of API use, no IDO is necessary |
|
189 | 194 | |
190 | 195 | // IDO Database |
191 | 196 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -210,9 +215,11 @@ discard block |
||
210 | 215 | protected function getDatabaseOptions() |
211 | 216 | { |
212 | 217 | // Database options |
213 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
218 | + if ($this->logSetup === false) { |
|
219 | + // Only if logging was no setup in constructor |
|
214 | 220 | { |
215 | 221 | $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
222 | + } |
|
216 | 223 | $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
217 | 224 | $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
218 | 225 | } |
@@ -221,7 +228,9 @@ discard block |
||
221 | 228 | protected function getDBConfigIfSet($element,&$variable) |
222 | 229 | { |
223 | 230 | $value=$this->getDBConfig($element); |
224 | - if ($value != 'null') $variable=$value; |
|
231 | + if ($value != 'null') { |
|
232 | + $variable=$value; |
|
233 | + } |
|
225 | 234 | } |
226 | 235 | |
227 | 236 | /** |
@@ -309,8 +318,7 @@ discard block |
||
309 | 318 | { |
310 | 319 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
311 | 320 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
312 | - } |
|
313 | - else |
|
321 | + } else |
|
314 | 322 | { |
315 | 323 | $this->trap_data['source_ip']=$matches[1]; |
316 | 324 | $this->trap_data['destination_ip']=$matches[3]; |
@@ -325,14 +333,12 @@ discard block |
||
325 | 333 | if ($ret_code===0 || $ret_code===false) |
326 | 334 | { |
327 | 335 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
328 | - } |
|
329 | - else |
|
336 | + } else |
|
330 | 337 | { |
331 | 338 | 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 | 339 | { |
333 | 340 | $this->trap_data['trap_oid']=$matches[2]; |
334 | - } |
|
335 | - else |
|
341 | + } else |
|
336 | 342 | { |
337 | 343 | $object= new stdClass; |
338 | 344 | $object->oid =$matches[1]; |
@@ -503,7 +509,9 @@ discard block |
||
503 | 509 | } |
504 | 510 | |
505 | 511 | $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"); |
|
512 | + if ($inserted_id==false) { |
|
513 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
514 | + } |
|
507 | 515 | $this->trap_id=$inserted_id; |
508 | 516 | break; |
509 | 517 | default: |
@@ -519,7 +527,9 @@ discard block |
||
519 | 527 | { |
520 | 528 | |
521 | 529 | // If action is ignore -> don't send t DB |
522 | - if ($this->trap_to_db === false) return; |
|
530 | + if ($this->trap_to_db === false) { |
|
531 | + return; |
|
532 | + } |
|
523 | 533 | |
524 | 534 | |
525 | 535 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -576,7 +586,9 @@ discard block |
||
576 | 586 | } |
577 | 587 | |
578 | 588 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
579 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
589 | + if ($inserted_id==false) { |
|
590 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
591 | + } |
|
580 | 592 | $this->trap_id=$inserted_id; |
581 | 593 | break; |
582 | 594 | default: |
@@ -705,8 +717,7 @@ discard block |
||
705 | 717 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
706 | 718 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
707 | 719 | return true; |
708 | - } |
|
709 | - else |
|
720 | + } else |
|
710 | 721 | { |
711 | 722 | // Get perfdata if found |
712 | 723 | $matches=array(); |
@@ -714,8 +725,7 @@ discard block |
||
714 | 725 | { |
715 | 726 | $display=$matches[1]; |
716 | 727 | $perfdata=$matches[2]; |
717 | - } |
|
718 | - else |
|
728 | + } else |
|
719 | 729 | { |
720 | 730 | $perfdata=''; |
721 | 731 | } |
@@ -727,8 +737,7 @@ discard block |
||
727 | 737 | { |
728 | 738 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
729 | 739 | return false; |
730 | - } |
|
731 | - else |
|
740 | + } else |
|
732 | 741 | { |
733 | 742 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
734 | 743 | return true; |
@@ -824,20 +833,17 @@ discard block |
||
824 | 833 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
825 | 834 | { |
826 | 835 | $this->trap_action.='Error sending status : check cmd/API'; |
827 | - } |
|
828 | - else |
|
836 | + } else |
|
829 | 837 | { |
830 | 838 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
831 | 839 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
832 | 840 | } |
833 | - } |
|
834 | - else |
|
841 | + } else |
|
835 | 842 | { |
836 | 843 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
837 | 844 | } |
838 | 845 | $this->trap_to_db=($action==-2)?false:true; |
839 | - } |
|
840 | - else |
|
846 | + } else |
|
841 | 847 | { |
842 | 848 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
843 | 849 | |
@@ -848,14 +854,12 @@ discard block |
||
848 | 854 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
849 | 855 | { |
850 | 856 | $this->trap_action.='Error sending status : check cmd/API'; |
851 | - } |
|
852 | - else |
|
857 | + } else |
|
853 | 858 | { |
854 | 859 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
855 | 860 | $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
856 | 861 | } |
857 | - } |
|
858 | - else |
|
862 | + } else |
|
859 | 863 | { |
860 | 864 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
861 | 865 | } |
@@ -865,16 +869,14 @@ discard block |
||
865 | 869 | if (!isset($this->trap_data['source_name'])) |
866 | 870 | { |
867 | 871 | $this->trap_data['source_name']=$rule['host_name']; |
868 | - } |
|
869 | - else |
|
872 | + } else |
|
870 | 873 | { |
871 | 874 | if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name'])) |
872 | 875 | { // only add if not present |
873 | 876 | $this->trap_data['source_name'].=','.$rule['host_name']; |
874 | 877 | } |
875 | 878 | } |
876 | - } |
|
877 | - catch (Exception $e) |
|
879 | + } catch (Exception $e) |
|
878 | 880 | { |
879 | 881 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
880 | 882 | $this->trap_action.=' ERR : '.$e->getMessage(); |
@@ -885,8 +887,7 @@ discard block |
||
885 | 887 | if ($this->trap_data['status']=='error') |
886 | 888 | { |
887 | 889 | $this->trap_to_db=true; // Always put errors in DB for the use can see |
888 | - } |
|
889 | - else |
|
890 | + } else |
|
890 | 891 | { |
891 | 892 | $this->trap_data['status']='done'; |
892 | 893 | } |