1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Icinga\Module\Trapdirector\Clicommands; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Icinga\Application\Icinga; |
8
|
|
|
use Icinga\Data\Db\DbConnection as IcingaDbConnection; |
9
|
|
|
use Icinga\Cli\Command; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
use Icinga\Module\Trapdirector\Config\TrapModuleConfig; |
14
|
|
|
|
15
|
|
|
use Trapdirector\Trap; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* MIB related actions |
19
|
|
|
* |
20
|
|
|
* Databse update, remove old traps |
21
|
|
|
*/ |
22
|
|
|
class MibCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Update mib database |
26
|
|
|
* |
27
|
|
|
* USAGE |
28
|
|
|
* |
29
|
|
|
* icingli trapdirector mib update |
30
|
|
|
* |
31
|
|
|
* OPTIONS |
32
|
|
|
* |
33
|
|
|
* --pid <file> : run in background with pid in <file> |
34
|
|
|
* |
35
|
|
|
* --verb : Set output log to verbose |
36
|
|
|
* |
37
|
|
|
* --force-check : force check of all traps & objects for change. (NOT IMPLEMENTED) |
38
|
|
|
*/ |
39
|
|
|
public function updateAction() |
40
|
|
|
{ |
41
|
|
|
$background = $this->params->get('pid', null); |
42
|
|
|
$logLevel= $this->params->has('verb') ? 4 : 2; |
43
|
|
|
if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
44
|
|
|
$forceCheck=$this->params->has('force-check')?True:False; |
45
|
|
|
$pid=1; |
46
|
|
|
if ($background != null) |
47
|
|
|
{ |
48
|
|
|
$file=@fopen($background,'w'); |
49
|
|
|
if ($file == false) |
50
|
|
|
{ |
51
|
|
|
echo 'Error : cannot open pid file '.$background; |
52
|
|
|
return 1; |
53
|
|
|
} |
54
|
|
|
$pid = pcntl_fork(); |
55
|
|
|
if ($pid == -1) { |
56
|
|
|
echo 'Error : Cannot fork process'; |
57
|
|
|
return 1; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
$module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
61
|
|
|
require_once($module->getBaseDir() .'/bin/trap_class.php'); |
62
|
|
|
$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
63
|
|
|
$trap = new Trap($icingaweb2_etc); |
64
|
|
|
if ($pid == 1) |
65
|
|
|
{ |
66
|
|
|
$trap->setLogging($logLevel,'display'); |
67
|
|
|
} |
68
|
|
|
else |
69
|
|
|
{ // use default display TODO : if default is 'display' son process will be killed at first output.... |
70
|
|
|
if ($pid != 0) |
71
|
|
|
{ |
72
|
|
|
// father process |
73
|
|
|
fwrite($file,$pid); |
|
|
|
|
74
|
|
|
fclose($file); |
75
|
|
|
echo "OK : process $pid in bckground"; |
76
|
|
|
return 0; |
77
|
|
|
} |
78
|
|
|
else |
79
|
|
|
{ // son process : close all file descriptors and go to a new session |
80
|
|
|
fclose($file); |
81
|
|
|
// $sid = posix_setsid(); |
82
|
|
|
fclose(STDIN); |
83
|
|
|
fclose(STDOUT); |
84
|
|
|
fclose(STDERR); |
85
|
|
|
try |
86
|
|
|
{ |
87
|
|
|
$trap->mibClass->update_mib_database(false,$forceCheck); |
88
|
|
|
} |
89
|
|
|
catch (Exception $e) |
90
|
|
|
{ |
91
|
|
|
$trap->logging->log('Error in updating : ' . $e->getMessage(),2); |
92
|
|
|
} |
93
|
|
|
unlink($background); |
94
|
|
|
return 0; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
try |
100
|
|
|
{ |
101
|
|
|
echo "Update main mib database : \n"; |
102
|
|
|
echo "# (trap found) C (trap already processed) . (every 2 seconds) : \n"; |
103
|
|
|
$trap->mibClass->update_mib_database(true,$forceCheck); |
104
|
|
|
echo "Done\n"; |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
catch (Exception $e) |
108
|
|
|
{ |
109
|
|
|
echo 'Error in updating : ' . $e->getMessage(); |
110
|
|
|
} |
111
|
|
|
if ($pid != 1) |
112
|
|
|
{ |
113
|
|
|
unlink($background); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
/** |
117
|
|
|
* purge all mib database NOT IMPLEMENTED |
118
|
|
|
* |
119
|
|
|
* USAGE |
120
|
|
|
* |
121
|
|
|
* icingli trapdirector mib purge --confirm |
122
|
|
|
* |
123
|
|
|
* OPTIONS |
124
|
|
|
* |
125
|
|
|
* --confirm : needed to execute purge |
126
|
|
|
*/ |
127
|
|
|
public function purgeAction() |
128
|
|
|
{ |
129
|
|
|
$db_prefix=$this->Config()->get('config', 'database_prefix'); |
130
|
|
|
|
131
|
|
|
if (!$this->params->has('confirm')) |
132
|
|
|
{ |
133
|
|
|
echo "This needs confirmation with '--confirm'\n"; |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$Config = new TrapModuleConfig($db_prefix); |
138
|
|
|
|
139
|
|
|
try |
140
|
|
|
{ |
141
|
|
|
|
142
|
|
|
$dbresource=$this->Config()->get('config', 'database'); |
143
|
|
|
echo "DB name : $dbresource\n"; |
144
|
|
|
$db = IcingaDbConnection::fromResourceName($dbresource)->getConnection(); |
145
|
|
|
|
146
|
|
|
$query = $db->delete( |
147
|
|
|
$Config->getMIBCacheTableName(), |
148
|
|
|
'id>0'); |
149
|
|
|
echo 'Deleted '. $query . " traps and objects\n"; |
150
|
|
|
} |
151
|
|
|
catch (Exception $e) |
152
|
|
|
{ |
153
|
|
|
echo 'Error in DB : ' . $e->getMessage(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|