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