1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Icinga\Module\Trapdirector\Controllers; |
4
|
|
|
|
5
|
|
|
//use Icinga\Web\Controller; |
6
|
|
|
//use Icinga\Web\Url; |
7
|
|
|
use Exception; |
8
|
|
|
use Icinga\Module\Trapdirector\TrapsController; |
9
|
|
|
use Trapdirector\Trap; |
10
|
|
|
|
11
|
|
|
class HelperController extends TrapsController |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** Get host list with filter (IP or name) : host=<filter> |
15
|
|
|
* returns in JSON : status=>OK/NOK hosts=>array of hosts |
16
|
|
|
*/ |
17
|
|
|
public function gethostsAction() |
18
|
|
|
{ |
19
|
|
|
$postData=$this->getRequest()->getPost(); |
20
|
|
|
|
21
|
|
|
$hostFilter = $this->checkPostVar($postData, 'hostFilter', '.*'); |
22
|
|
|
|
23
|
|
|
$retHosts=array('status'=>'OK','hosts' => array()); |
24
|
|
|
$this->getIdoConn(); // set apiMode to correct val |
25
|
|
|
if ($this->apiMode === TRUE) |
26
|
|
|
{ |
27
|
|
|
$hosts=$this->getIdoConn()->getHostByNameOrIP($hostFilter); |
|
|
|
|
28
|
|
|
$retHosts['test']=count($hosts); |
29
|
|
|
} |
30
|
|
|
else |
31
|
|
|
{ |
32
|
|
|
$hosts=$this->getIdoConn()->getHostByIP($hostFilter); |
33
|
|
|
} |
34
|
|
|
foreach ($hosts as $val) |
35
|
|
|
{ |
36
|
|
|
array_push($retHosts['hosts'],$val->name); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->_helper->json($retHosts); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** Get hostgroup list with filter (name) : hostgroup=<hostFilter> |
43
|
|
|
* returns in JSON : status=>OK/NOK hosts=>array of hosts |
44
|
|
|
*/ |
45
|
|
|
public function gethostgroupsAction() |
46
|
|
|
{ |
47
|
|
|
$postData=$this->getRequest()->getPost(); |
48
|
|
|
|
49
|
|
|
$hostFilter = $this->checkPostVar($postData, 'hostFilter', '.*'); |
50
|
|
|
|
51
|
|
|
$retHosts=array('status'=>'OK','hosts' => array()); |
52
|
|
|
|
53
|
|
|
$hosts=$this->getIdoConn()->getHostGroupByName($hostFilter); |
54
|
|
|
foreach ($hosts as $val) |
55
|
|
|
{ |
56
|
|
|
array_push($retHosts['hosts'],$val->name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->_helper->json($retHosts); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** Get service list by host name ( host=<host> ) |
63
|
|
|
* returns in JSON : |
64
|
|
|
* status=>OK/No services found/More than one host matches |
65
|
|
|
* services=>array of services (name) |
66
|
|
|
* hostid = host object id or -1 if not found. |
67
|
|
|
*/ |
68
|
|
|
public function getservicesAction() |
69
|
|
|
{ |
70
|
|
|
$postData=$this->getRequest()->getPost(); |
71
|
|
|
|
72
|
|
|
$host=$this->checkPostVar($postData, 'host', '.*'); |
|
|
|
|
73
|
|
|
if (isset($postData['host'])) |
74
|
|
|
{ |
75
|
|
|
$host=$postData['host']; |
76
|
|
|
} |
77
|
|
|
else |
78
|
|
|
{ |
79
|
|
|
$this->_helper->json(array('status'=>'No Hosts','hostid' => -1)); |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$hostArray=$this->getIdoConn()->getHostByName($host); |
84
|
|
|
if (count($hostArray) > 1) |
85
|
|
|
{ |
86
|
|
|
$this->_helper->json(array('status'=>'More than one host matches','hostid' => -1)); |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
else if (count($hostArray) == 0) |
90
|
|
|
{ |
91
|
|
|
$this->_helper->json(array('status'=>'No host matches','hostid' => -1)); |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
$services=$this->getIdoConn()->getServicesByHostid($hostArray[0]->id); |
95
|
|
|
if (count($services) < 1) |
96
|
|
|
{ |
97
|
|
|
$this->_helper->json(array('status'=>'No services found for host','hostid' => $hostArray[0]->id)); |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
$retServices=array('status'=>'OK','services' => array(),'hostid' => $hostArray[0]->id); |
101
|
|
|
foreach ($services as $val) |
102
|
|
|
{ |
103
|
|
|
array_push($retServices['services'],array($val->id , $val->name)); |
104
|
|
|
} |
105
|
|
|
$this->_helper->json($retServices); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** Get service list by host group ( name=<host> ) |
109
|
|
|
* returns in JSON : |
110
|
|
|
* status=>OK/No services found/More than one host matches |
111
|
|
|
* services=>array of services (name) |
112
|
|
|
* groupid = group object id or -1 if not found. |
113
|
|
|
*/ |
114
|
|
|
public function gethostgroupservicesAction() |
115
|
|
|
{ |
116
|
|
|
$postData=$this->getRequest()->getPost(); |
117
|
|
|
|
118
|
|
|
$host = $this->checkPostVar($postData, 'host', '.+'); |
119
|
|
|
|
120
|
|
|
$hostArray=$this->getIdoConn()->getHostGroupByName($host); |
121
|
|
|
if (count($hostArray) > 1) |
122
|
|
|
{ |
123
|
|
|
$this->_helper->json(array('status'=>'More than one hostgroup matches','hostid' => -1)); |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
else if (count($hostArray) == 0) |
127
|
|
|
{ |
128
|
|
|
$this->_helper->json(array('status'=>'No hostgroup matches','hostid' => -1)); |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
$services=$this->getIdoConn()->getServicesByHostGroupid($hostArray[0]->id); |
132
|
|
|
if (count($services) < 1) |
133
|
|
|
{ |
134
|
|
|
$this->_helper->json(array('status'=>'No services found for hostgroup','hostid' => $hostArray[0]->id)); |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
$retServices=array('status'=>'OK','services' => $services,'hostid' => $hostArray[0]->id); |
138
|
|
|
|
139
|
|
|
$this->_helper->json($retServices); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** Get traps from mib : entry : mib=<mib> |
143
|
|
|
* returns in JSON : |
144
|
|
|
* status=>OK/No mib/Error getting mibs |
145
|
|
|
* traps=>array of array( oid -> name) |
146
|
|
|
*/ |
147
|
|
|
public function gettrapsAction() |
148
|
|
|
{ |
149
|
|
|
$postData=$this->getRequest()->getPost(); |
150
|
|
|
|
151
|
|
|
$mib = $this->checkPostVar($postData, 'mib', '.*'); |
152
|
|
|
|
153
|
|
|
try |
154
|
|
|
{ |
155
|
|
|
$traplist=$this->getMIB()->getTrapList($mib); |
156
|
|
|
$retTraps=array('status'=>'OK','traps' => $traplist); |
157
|
|
|
} |
158
|
|
|
catch (Exception $e) |
159
|
|
|
{ |
160
|
|
|
$retTraps=array('status' => 'Error getting mibs'); |
161
|
|
|
} |
162
|
|
|
$this->_helper->json($retTraps); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** Get trap objects from mib : entry : trap=<oid> |
166
|
|
|
* returns in JSON : |
167
|
|
|
* status=>OK/no trap/not found |
168
|
|
|
* objects=>array of array( oid -> name, oid->mib) |
169
|
|
|
*/ |
170
|
|
|
public function gettrapobjectsAction() |
171
|
|
|
{ |
172
|
|
|
$postData=$this->getRequest()->getPost(); |
173
|
|
|
|
174
|
|
|
$trap = $this->checkPostVar($postData, 'trap', '.*'); |
175
|
|
|
|
176
|
|
|
try |
177
|
|
|
{ |
178
|
|
|
$objectlist=$this->getMIB()->getObjectList($trap); |
179
|
|
|
$retObjects=array('status'=>'OK','objects' => $objectlist); |
180
|
|
|
} |
181
|
|
|
catch (Exception $e) |
182
|
|
|
{ |
183
|
|
|
$retObjects=array('status' => 'not found'); |
184
|
|
|
} |
185
|
|
|
$this->_helper->json($retObjects); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** Get list of all loaded mibs : entry : none |
189
|
|
|
* return : array of strings. |
190
|
|
|
*/ |
191
|
|
|
public function getmiblistAction() |
192
|
|
|
{ |
193
|
|
|
try |
194
|
|
|
{ |
195
|
|
|
$miblist=$this->getMIB()->getMIBList(); |
196
|
|
|
} |
197
|
|
|
catch (Exception $e) |
198
|
|
|
{ |
199
|
|
|
$miblist=array('Error getting mibs'); |
200
|
|
|
} |
201
|
|
|
$this->_helper->json($miblist); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** Get MIB::Name from OID : entry : oid |
205
|
|
|
* status=>OK/No oid/not found |
206
|
|
|
* mib=>string |
207
|
|
|
* name=>string |
208
|
|
|
*/ |
209
|
|
|
public function translateoidAction() |
210
|
|
|
{ |
211
|
|
|
$postData=$this->getRequest()->getPost(); |
212
|
|
|
|
213
|
|
|
$oid = $this->checkPostVar($postData, 'oid', '.*'); |
214
|
|
|
|
215
|
|
|
// Try to get oid name from snmptranslate |
216
|
|
|
if (($object=$this->getMIB()->translateOID($oid)) == null) |
217
|
|
|
{ |
218
|
|
|
$this->_helper->json(array('status'=>'Not found')); |
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
else |
222
|
|
|
{ |
223
|
|
|
$this->_helper->json( |
224
|
|
|
array('status'=>'OK', |
225
|
|
|
'mib' => $object['mib'], |
226
|
|
|
'name' => $object['name'], |
227
|
|
|
'type' => $object['type'], |
228
|
|
|
'type_enum' => $object['type_enum'], |
229
|
|
|
'description' => $object['description'] |
230
|
|
|
) |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** Save or execute database purge of <n> days |
237
|
|
|
* days=>int |
238
|
|
|
* action=>save/execute |
239
|
|
|
* return : status=>OK/Message error |
240
|
|
|
*/ |
241
|
|
|
public function dbmaintenanceAction() |
242
|
|
|
{ |
243
|
|
|
|
244
|
|
|
$postData=$this->getRequest()->getPost(); |
245
|
|
|
|
246
|
|
|
$days = $this->checkPostVar($postData, 'days', '^[0-9]+$'); |
247
|
|
|
$days=intval($days); |
248
|
|
|
|
249
|
|
|
$action = $this->checkPostVar($postData, 'action', 'save|execute'); |
250
|
|
|
|
251
|
|
|
if ($action == 'save') |
252
|
|
|
{ |
253
|
|
|
try |
254
|
|
|
{ |
255
|
|
|
$this->getUIDatabase()->setDBConfigValue('db_remove_days',$days); |
256
|
|
|
} |
257
|
|
|
catch (Exception $e) |
258
|
|
|
{ |
259
|
|
|
$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
260
|
|
|
return; |
261
|
|
|
} |
262
|
|
|
$this->_helper->json(array('status'=>'OK')); |
263
|
|
|
return; |
264
|
|
|
} |
265
|
|
|
if ($action == 'execute') |
266
|
|
|
{ |
267
|
|
|
try |
268
|
|
|
{ |
269
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
270
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
271
|
|
|
$debug_level=4; |
272
|
|
|
$trap = new Trap($icingaweb2_etc); |
273
|
|
|
$trap->setLogging($debug_level,'syslog'); |
274
|
|
|
$trap->eraseOldTraps($days); |
275
|
|
|
} |
276
|
|
|
catch (Exception $e) |
277
|
|
|
{ |
278
|
|
|
$this->_helper->json(array('status'=>'execute error : '.$e->getMessage() )); |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
$this->_helper->json(array('status'=>'OK')); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** Save snmp configuration to db |
287
|
|
|
* use=>1 (true) or 0 |
288
|
|
|
* oid=>specific oid or '' for default |
289
|
|
|
* return : status=>OK/Message error |
290
|
|
|
*/ |
291
|
|
|
public function snmpconfigAction() |
292
|
|
|
{ |
293
|
|
|
$postData=$this->getRequest()->getPost(); |
294
|
|
|
|
295
|
|
|
$snmpUse = $this->checkPostVar($postData, 'useTrapAddr', '0|1'); |
296
|
|
|
|
297
|
|
|
$snmpOID = $this->checkPostVar($postData, 'trapAddrOID', '^[\.0-9]+$'); |
298
|
|
|
|
299
|
|
|
try |
300
|
|
|
{ |
301
|
|
|
$this->getUIDatabase()->setDBConfigValue('use_SnmpTrapAddess',$snmpUse); |
302
|
|
|
$this->getUIDatabase()->setDBConfigValue('SnmpTrapAddess_oid',$snmpOID); |
303
|
|
|
} |
304
|
|
|
catch (Exception $e) |
305
|
|
|
{ |
306
|
|
|
$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
307
|
|
|
return; |
308
|
|
|
} |
309
|
|
|
$this->_helper->json(array('status'=>'OK')); |
310
|
|
|
return; |
311
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** Save log output to db |
315
|
|
|
* destination=>log destination |
316
|
|
|
* file=>file name |
317
|
|
|
* level => int |
318
|
|
|
* return : status=>OK/Message error |
319
|
|
|
*/ |
320
|
|
|
public function logdestinationAction() |
321
|
|
|
{ |
322
|
|
|
$postData=$this->getRequest()->getPost(); |
323
|
|
|
|
324
|
|
|
$destination = $this->checkPostVar($postData, 'destination', '.*'); |
325
|
|
|
$logDest=$this->getModuleConfig()->getLogDestinations(); |
326
|
|
|
if (!isset($logDest[$destination])) |
327
|
|
|
{ |
328
|
|
|
$this->_helper->json(array('status'=>'invalid destination : '.$destination)); |
329
|
|
|
return; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
if (isset($postData['file'])) |
333
|
|
|
{ |
334
|
|
|
$file=$postData['file']; |
335
|
|
|
$fileHandler=@fopen($file,'w'); |
336
|
|
|
if ($fileHandler == false) |
337
|
|
|
{ // File os note writabe / cannot create |
338
|
|
|
$this->_helper->json(array('status'=>'File not writable : '.$file)); |
339
|
|
|
return; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
else |
343
|
|
|
{ |
344
|
|
|
if ($destination != 'file') |
345
|
|
|
{ |
346
|
|
|
$file=null; |
347
|
|
|
} |
348
|
|
|
else |
349
|
|
|
{ |
350
|
|
|
$this->_helper->json(array('status'=>'No file')); |
351
|
|
|
return; |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
$level = $this->checkPostVar($postData, 'level', '[0-9]'); |
356
|
|
|
|
357
|
|
|
try |
358
|
|
|
{ |
359
|
|
|
$this->getUIDatabase()->setDBConfigValue('log_destination',$destination); |
360
|
|
|
$this->getUIDatabase()->setDBConfigValue('log_file',$file); |
361
|
|
|
$this->getUIDatabase()->setDBConfigValue('log_level',$level); |
362
|
|
|
} |
363
|
|
|
catch (Exception $e) |
364
|
|
|
{ |
365
|
|
|
$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
366
|
|
|
return; |
367
|
|
|
} |
368
|
|
|
$this->_helper->json(array('status'=>'OK')); |
369
|
|
|
return; |
370
|
|
|
|
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** Test a rule evaluation |
374
|
|
|
* rule=>rule to evaluate |
375
|
|
|
* action=>'evaluate' |
376
|
|
|
* return : status=>OK/Message error & message : return of evaluation |
377
|
|
|
*/ |
378
|
|
|
public function testruleAction() |
379
|
|
|
{ |
380
|
|
|
|
381
|
|
|
$postData=$this->getRequest()->getPost(); |
382
|
|
|
|
383
|
|
|
$rule = $this->checkPostVar($postData, 'rule', '.*'); |
384
|
|
|
|
385
|
|
|
$action = $this->checkPostVar($postData, 'action', 'evaluate'); |
386
|
|
|
|
387
|
|
|
if ($action == 'evaluate') |
388
|
|
|
{ |
389
|
|
|
try |
390
|
|
|
{ |
391
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
392
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
393
|
|
|
$trap = new Trap($icingaweb2_etc); |
394
|
|
|
// Cleanup spaces before eval |
395
|
|
|
$rule=$trap->ruleClass->eval_cleanup($rule); |
396
|
|
|
// Eval |
397
|
|
|
$item=0; |
398
|
|
|
$rule=$trap->ruleClass->evaluation($rule,$item); |
399
|
|
|
} |
400
|
|
|
catch (Exception $e) |
401
|
|
|
{ |
402
|
|
|
$this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
403
|
|
|
return; |
404
|
|
|
} |
405
|
|
|
$return=($rule==true)?'true':'false'; |
406
|
|
|
$this->_helper->json(array('status'=>'OK', 'message' => $return)); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** Test a rule evaluation |
412
|
|
|
* name => name of plugin |
413
|
|
|
* action => enable | disable |
414
|
|
|
* return : status=>OK/Message error |
415
|
|
|
*/ |
416
|
|
|
public function pluginAction() |
417
|
|
|
{ |
418
|
|
|
$postData=$this->getRequest()->getPost(); |
419
|
|
|
|
420
|
|
|
$pluginName = $this->checkPostVar($postData, 'name', '.*'); |
421
|
|
|
|
422
|
|
|
$action = $this->checkPostVar($postData, 'action', 'enable|disable'); |
423
|
|
|
|
424
|
|
|
try |
425
|
|
|
{ |
426
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
427
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
428
|
|
|
$trap = new Trap($icingaweb2_etc); |
429
|
|
|
// Enable plugin. |
430
|
|
|
$action=($action == 'enable') ? true : false; |
431
|
|
|
$retVal=$trap->pluginClass->enablePlugin($pluginName, $action); |
432
|
|
|
|
433
|
|
|
} |
434
|
|
|
catch (Exception $e) |
435
|
|
|
{ |
436
|
|
|
$this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
437
|
|
|
return; |
438
|
|
|
} |
439
|
|
|
if ($retVal === true) |
440
|
|
|
{ |
441
|
|
|
$this->_helper->json(array('status'=>'OK')); |
442
|
|
|
} |
443
|
|
|
else |
444
|
|
|
{ |
445
|
|
|
$this->_helper->json(array('status'=>'Error, see logs')); |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** Function evaluation |
450
|
|
|
* function => name of function |
451
|
|
|
* action => evaluate |
452
|
|
|
* return : status=>OK/Message error & message : return of evaluation ('true' or 'false' ) |
453
|
|
|
*/ |
454
|
|
|
public function functionAction() |
455
|
|
|
{ |
456
|
|
|
$postData=$this->getRequest()->getPost(); |
457
|
|
|
|
458
|
|
|
$functionString = $this->checkPostVar($postData, 'function', '.*'); |
459
|
|
|
|
460
|
|
|
$this->checkPostVar($postData, 'action', 'evaluate'); |
461
|
|
|
|
462
|
|
|
// Only one action possible for now, no tests on action. |
463
|
|
|
try |
464
|
|
|
{ |
465
|
|
|
require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
466
|
|
|
$icingaweb2Etc=$this->Config()->get('config', 'icingaweb2_etc'); |
467
|
|
|
$trap = new Trap($icingaweb2Etc); |
468
|
|
|
// load all plugins in case tested function is not enabled. |
469
|
|
|
$trap->pluginClass->registerAllPlugins(false); |
470
|
|
|
// Clean all spaces |
471
|
|
|
$functionString = $trap->ruleClass->eval_cleanup($functionString); |
472
|
|
|
// Eval functions |
473
|
|
|
$result = $trap->pluginClass->evaluateFunctionString($functionString); |
474
|
|
|
} |
475
|
|
|
catch (Exception $e) |
476
|
|
|
{ |
477
|
|
|
$this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
478
|
|
|
return; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$result = ($result === true)?'True':'False'; |
482
|
|
|
$this->_helper->json(array('status'=>'OK','message' => $result)); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/************** Utilities **********************/ |
486
|
|
|
|
487
|
|
|
private function checkPostVar(array $postData,string $postVar, string $validRegexp) : string |
488
|
|
|
{ |
489
|
|
|
if (!isset ($postData[$postVar])) |
490
|
|
|
{ |
491
|
|
|
$this->_helper->json(array('status'=>'No ' . $postVar)); |
492
|
|
|
return ''; |
493
|
|
|
} |
494
|
|
|
if (preg_match('/'.$validRegexp.'/', $postData[$postVar]) != 1) |
495
|
|
|
{ |
496
|
|
|
$this->_helper->json(array('status'=>'Unknown ' . $postVar . ' value '.$postData[$postVar])); |
497
|
|
|
return ''; |
498
|
|
|
} |
499
|
|
|
return $postData[$postVar]; |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.