Passed
Push — master ( 42d7f2...df2595 )
by Patrick
02:12
created

HelperController   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 453
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 207
c 1
b 0
f 0
dl 0
loc 453
rs 6.96
wmc 53

11 Methods

Rating   Name   Duplication   Size   Complexity  
B getservicesAction() 0 36 6
A getmiblistAction() 0 11 2
A gethostgroupsAction() 0 22 3
A gettrapobjectsAction() 0 22 3
A gethostgroupservicesAction() 0 33 5
A translateoidAction() 0 28 3
B dbmaintenanceAction() 0 63 10
B testruleAction() 0 46 7
A gethostsAction() 0 22 3
B logdestinationAction() 0 63 8
A gettrapsAction() 0 22 3

How to fix   Complexity   

Complex Class

Complex classes like HelperController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use HelperController, and based on these observations, apply Extract Interface, too.

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 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
		if (isset($postData['hostFilter']))
21
		{
22
			$hostFilter=$postData['hostFilter'];
23
		}
24
		else
25
		{
26
			$this->_helper->json(array('status'=>'KO'));
27
			return;
28
		}
29
30
		$retHosts=array('status'=>'OK','hosts' => array());
31
32
		$hosts=$this->getHostByIP($hostFilter);
33
		foreach ($hosts as $val)
34
		{
35
			array_push($retHosts['hosts'],$val->name);
36
		}
37
		
38
		$this->_helper->json($retHosts);
39
	}
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
		if (isset($postData['hostFilter']))
49
		{
50
			$hostFilter=$postData['hostFilter'];
51
		}
52
		else
53
		{
54
			$this->_helper->json(array('status'=>'Error : no filter'));
55
			return;
56
		}
57
58
		$retHosts=array('status'=>'OK','hosts' => array());
59
60
		$hosts=$this->getHostGroupByName($hostFilter);
61
		foreach ($hosts as $val)
62
		{
63
			array_push($retHosts['hosts'],$val->name);
64
		}
65
		
66
		$this->_helper->json($retHosts);
67
	}
68
69
	
70
	/** Get service list by host name ( host=<host> )
71
	*	returns in JSON : 
72
	*		status=>OK/No services found/More than one host matches
73
	*		services=>array of services (name)
74
	*		hostid = host object id or -1 if not found.
75
	*/
76
	public function getservicesAction()
77
	{
78
		$postData=$this->getRequest()->getPost();
79
		if (isset($postData['host']))
80
		{
81
			$host=$postData['host'];
82
		}
83
		else
84
		{
85
			$this->_helper->json(array('status'=>'No Hosts','hostid' => -1));
86
			return;
87
		}
88
		
89
		$hostArray=$this->getHostByName($host);
90
		if (count($hostArray) > 1)
91
		{	
92
			$this->_helper->json(array('status'=>'More than one host matches','hostid' => -1));
93
			return;
94
		}
95
		else if (count($hostArray) == 0)
96
		{
97
			$this->_helper->json(array('status'=>'No host matches','hostid' => -1));
98
			return;
99
		}
100
		$services=$this->getServicesByHostid($hostArray[0]->id);
101
		if (count($services) < 1)
102
		{
103
			$this->_helper->json(array('status'=>'No services found for host','hostid' => $hostArray[0]->id));
104
			return;
105
		}
106
		$retServices=array('status'=>'OK','services' => array(),'hostid' => $hostArray[0]->id);
107
		foreach ($services as $val)
108
		{
109
			array_push($retServices['services'],array($val->id , $val->name));
110
		}
111
		$this->_helper->json($retServices);
112
	}
113
	
114
	/** Get service list by host group ( name=<host> )
115
	*	returns in JSON : 
116
	*		status=>OK/No services found/More than one host matches
117
	*		services=>array of services (name)
118
	*		groupid = group object id or -1 if not found.
119
	*/
120
	public function gethostgroupservicesAction()
121
	{
122
		$postData=$this->getRequest()->getPost();
123
		if (isset($postData['host']))
124
		{
125
			$host=$postData['host'];
126
		}
127
		else
128
		{
129
			$this->_helper->json(array('status'=>'No Hosts','hostid' => -1));
130
			return;
131
		}
132
		
133
		$hostArray=$this->getHostGroupByName($host);
134
		if (count($hostArray) > 1)
135
		{	
136
			$this->_helper->json(array('status'=>'More than one hostgroup matches','hostid' => -1));
137
			return;
138
		}
139
		else if (count($hostArray) == 0)
140
		{
141
			$this->_helper->json(array('status'=>'No hostgroup matches','hostid' => -1));
142
			return;
143
		}
144
		$services=$this->getServicesByHostGroupid($hostArray[0]->id);
145
		if (count($services) < 1)
146
		{
147
			$this->_helper->json(array('status'=>'No services found for hostgroup','hostid' => $hostArray[0]->id));
148
			return;
149
		}
150
		$retServices=array('status'=>'OK','services' => $services,'hostid' => $hostArray[0]->id);
151
		
152
		$this->_helper->json($retServices);
153
	}
154
155
	/** Get traps from mib  : entry : mib=<mib>
156
	*	returns in JSON : 
157
	*		status=>OK/No mib/Error getting mibs
158
	*		traps=>array of array( oid -> name)
159
	*/
160
	public function gettrapsAction()
161
	{
162
		$postData=$this->getRequest()->getPost();
163
		if (isset($postData['mib']))
164
		{
165
			$mib=$postData['mib'];
166
		}
167
		else
168
		{
169
			$this->_helper->json(array('status'=>'No mib'));
170
			return;
171
		}
172
		try
173
		{
174
			$traplist=$this->getMIB()->getTrapList($mib);
175
			$retTraps=array('status'=>'OK','traps' => $traplist);
176
		} 
177
		catch (Exception $e) 
178
		{ 
179
			$retTraps=array('status' => 'Error getting mibs');
180
		}
181
		$this->_helper->json($retTraps);
182
	}	
183
184
	/** Get trap objects from mib  : entry : trap=<oid>
185
	*	returns in JSON : 
186
	*		status=>OK/no trap/not found
187
	*		objects=>array of array( oid -> name, oid->mib)
188
	*/
189
	public function gettrapobjectsAction()
190
	{
191
		$postData=$this->getRequest()->getPost();
192
		if (isset($postData['trap']))
193
		{
194
			$trap=$postData['trap'];
195
		}
196
		else
197
		{
198
			$this->_helper->json(array('status'=>'No trap'));
199
			return;
200
		}
201
		try
202
		{
203
			$objectlist=$this->getMIB()->getObjectList($trap);
204
			$retObjects=array('status'=>'OK','objects' => $objectlist);
205
		} 
206
		catch (Exception $e) 
207
		{ 
208
			$retObjects=array('status' => 'not found');
209
		}
210
		$this->_helper->json($retObjects);
211
	}	
212
	
213
	/** Get list of all loaded mibs : entry : none
214
	*	return : array of strings.
215
	*/
216
	public function getmiblistAction()
217
	{
218
		try
219
		{
220
			$miblist=$this->getMIB()->getMIBList();
221
		} 
222
		catch (Exception $e) 
223
		{ 
224
			$miblist=array('Error getting mibs');
225
		}
226
		$this->_helper->json($miblist);
227
	}
228
	
229
	/** Get MIB::Name from OID : entry : oid
230
	*		status=>OK/No oid/not found
231
	*		mib=>string
232
	*		name=>string
233
	*/	
234
	public function translateoidAction()
235
	{
236
		$postData=$this->getRequest()->getPost();
237
		if (isset($postData['oid']))
238
		{
239
			$oid=$postData['oid'];
240
		}
241
		else
242
		{
243
			$this->_helper->json(array('status'=>'No oid'));
244
			return;
245
		}
246
		
247
		// Try to get oid name from snmptranslate
248
		if (($object=$this->getMIB()->translateOID($oid)) == null)
249
		{
250
			$this->_helper->json(array('status'=>'Not found'));
251
			return;
252
		}
253
		else
254
		{
255
			$this->_helper->json(
256
				array('status'=>'OK',
257
					'mib' => $object['mib'], 
258
					'name' => $object['name'],
259
					'type' => $object['type'],
260
					'type_enum' => $object['type_enum'],
261
				    'description' => $object['description']
262
				)
263
			);
264
		}
265
266
	}
267
268
	
269
	/** Save or execute database purge of <n> days
270
	*	days=>int 
271
	*	action=>save/execute
272
	*	return : status=>OK/Message error
273
	*/
274
	public function dbmaintenanceAction()
275
	{
276
		
277
		$postData=$this->getRequest()->getPost();
278
		if (isset($postData['days']))
279
		{
280
			$days=$postData['days'];
281
			if (!preg_match('/^[0-9]+$/',$days))
282
			{
283
				$this->_helper->json(array('status'=>'invalid days : '.$days));
284
				return;
285
			}
286
		}
287
		else
288
		{
289
			$this->_helper->json(array('status'=>'No days'));
290
			return;
291
		}
292
		if (isset($postData['action']))
293
		{
294
			$action=$postData['action'];
295
			if ($action != 'save' && $action !='execute')
296
			{
297
				$this->_helper->json(array('status'=>'unknown action '.$action));
298
				return;
299
			}
300
		}
301
		else
302
		{
303
			$this->_helper->json(array('status'=>'No action'));
304
			return;
305
		}
306
		if ($action == 'save')
307
		{
308
			try
309
			{
310
				$this->setDBConfigValue('db_remove_days',$days);
311
			}
312
			catch (Exception $e)
313
			{
314
				$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() ));
315
				return;
316
			}
317
			$this->_helper->json(array('status'=>'OK'));
318
			return;
319
		}
320
		if ($action == 'execute')
321
		{
322
			try
323
			{
324
				require_once($this->Module()->getBaseDir() .'/bin/trap_class.php');
325
				$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc');
326
				$debug_level=4;
327
				$Trap = new Trap($icingaweb2_etc);
328
				$Trap->setLogging($debug_level,'syslog');
329
				$Trap->eraseOldTraps($days);
330
			}
331
			catch (Exception $e)
332
			{
333
				$this->_helper->json(array('status'=>'execute error : '.$e->getMessage() ));
334
				return;
335
			}			
336
			$this->_helper->json(array('status'=>'OK'));
337
		}
338
			
339
	}	
340
341
	/** Save log output to db
342
	*	destination=>log destination 
343
	*	file=>file name
344
	*	level => int 
345
	*	return : status=>OK/Message error
346
	*/
347
	public function logdestinationAction()
348
	{
349
		$postData=$this->getRequest()->getPost();
350
		if (isset($postData['destination']))
351
		{
352
			$destination=$postData['destination'];
353
			$logDest=$this->getModuleConfig()->getLogDestinations();
354
			if (!isset($logDest[$destination]))
355
			{
356
				$this->_helper->json(array('status'=>'invalid destination : '.$destination));
357
				return;
358
			}
359
		}
360
		else
361
		{
362
			$this->_helper->json(array('status'=>'No destination'));
363
		}
364
		if (isset($postData['file']))
365
		{ 
366
			$file=$postData['file'];
367
			$fileHandler=@fopen($file,'w');
368
			if ($fileHandler == false)
369
			{   // File os note writabe / cannot create
370
			    $this->_helper->json(array('status'=>'File not writable :  '.$file));
371
			    return;
372
			}
373
		}
374
		else
375
		{
376
			if ($destination != 'file')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $destination does not seem to be defined for all execution paths leading up to this point.
Loading history...
377
			{
378
				$file=null;
379
			}
380
			else
381
			{
382
				$this->_helper->json(array('status'=>'No file'));
383
				return;
384
			}
385
		}
386
387
		if (isset($postData['level']))
388
		{ 
389
			$level=$postData['level'];
390
		}
391
		else
392
		{
393
			$this->_helper->json(array('status'=>'No level'));
394
			return;
395
		}
396
		
397
		try
398
		{
399
			$this->setDBConfigValue('log_destination',$destination);
400
			$this->setDBConfigValue('log_file',$file);
401
			$this->setDBConfigValue('log_level',$level);
402
		}
403
		catch (Exception $e)
404
		{
405
			$this->_helper->json(array('status'=>'Save error : '.$e->getMessage() ));
406
			return;
407
		}
408
		$this->_helper->json(array('status'=>'OK'));
409
		return;
410
			
411
	}	
412
	
413
	/** Test a rule evaluation
414
	 *	rule=>rule to evaluate
415
	 *	action=>'evaluate'
416
	 *	return : status=>OK/Message error & message : return of evaluation
417
	 */
418
	public function testruleAction()
419
	{
420
	    
421
	    $postData=$this->getRequest()->getPost();
422
	    if (isset($postData['rule']))
423
	    {
424
	        $rule=$postData['rule'];
425
	    }
426
	    else
427
	    {
428
	        $this->_helper->json(array('status'=>'No Rule'));
429
	    }
430
	    if (isset($postData['action']))
431
	    {
432
	        $action=$postData['action'];
433
	        if ($action != 'evaluate')
434
	        {
435
	            $this->_helper->json(array('status'=>'unknown action '.$action));
436
	            return;
437
	        }
438
	    }
439
	    else
440
	    {
441
	        $this->_helper->json(array('status'=>'No action'));
442
	        return;
443
	    }
444
	    if ($action == 'evaluate')
445
	    {
446
	        try
447
	        {
448
	            require_once($this->Module()->getBaseDir() .'/bin/trap_class.php');
449
	            $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc');
450
	            $Trap = new Trap($icingaweb2_etc);
451
	            // Cleanup spaces before eval
452
	            $rule=$Trap->eval_cleanup($rule);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rule does not seem to be defined for all execution paths leading up to this point.
Loading history...
453
	            // Eval
454
	            $item=0;
455
	            $rule=$Trap->evaluation($rule,$item);
456
	        }
457
	        catch (Exception $e)
458
	        {
459
	            $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() ));
460
	            return;
461
	        }
462
	        $return=($rule==true)?'true':'false';
463
	        $this->_helper->json(array('status'=>'OK', 'message' => $return));
464
	    }
465
	    
466
	}	
467
	
468
}
469