@@ -9,383 +9,383 @@ |
||
9 | 9 | class Database |
10 | 10 | { |
11 | 11 | |
12 | - // Databases |
|
13 | - /** @var \PDO $trapDB trap database */ |
|
14 | - protected $trapDB=null; |
|
15 | - protected $idoDB=null; //< ido database |
|
16 | - public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
17 | - public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
12 | + // Databases |
|
13 | + /** @var \PDO $trapDB trap database */ |
|
14 | + protected $trapDB=null; |
|
15 | + protected $idoDB=null; //< ido database |
|
16 | + public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
17 | + public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
18 | 18 | |
19 | - protected $trapDSN; //< trap database connection params |
|
20 | - protected $trapUsername; //< trap database connection params |
|
21 | - protected $trapPass; //< trap database connection params |
|
22 | - public $dbPrefix; //< database tables prefix |
|
19 | + protected $trapDSN; //< trap database connection params |
|
20 | + protected $trapUsername; //< trap database connection params |
|
21 | + protected $trapPass; //< trap database connection params |
|
22 | + public $dbPrefix; //< database tables prefix |
|
23 | 23 | |
24 | - protected $idoSet; //< bool true is ido database set |
|
25 | - protected $idoDSN; //< trap database connection params |
|
26 | - protected $idoUsername; //< trap database connection params |
|
27 | - protected $idoPass; //< trap database connection params |
|
24 | + protected $idoSet; //< bool true is ido database set |
|
25 | + protected $idoDSN; //< trap database connection params |
|
26 | + protected $idoUsername; //< trap database connection params |
|
27 | + protected $idoPass; //< trap database connection params |
|
28 | 28 | |
29 | - // Logging function |
|
29 | + // Logging function |
|
30 | 30 | |
31 | - protected $logging; //< logging class |
|
31 | + protected $logging; //< logging class |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param Logging $logClass : where to log |
|
35 | - * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
36 | - */ |
|
37 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
38 | - { |
|
39 | - $this->logging=$logClass; |
|
40 | - $this->dbPrefix=$dbPrefix; |
|
33 | + /** |
|
34 | + * @param Logging $logClass : where to log |
|
35 | + * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
36 | + */ |
|
37 | + function __construct($logClass,$dbParam,$dbPrefix) |
|
38 | + { |
|
39 | + $this->logging=$logClass; |
|
40 | + $this->dbPrefix=$dbPrefix; |
|
41 | 41 | |
42 | - $this->trapDSN=$this->setupDSN($dbParam); |
|
43 | - $this->trapUsername = $dbParam['username']; |
|
44 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
45 | - $this->trapDBType=$dbParam['db']; |
|
46 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
47 | - $this->db_connect_trap(); |
|
42 | + $this->trapDSN=$this->setupDSN($dbParam); |
|
43 | + $this->trapUsername = $dbParam['username']; |
|
44 | + $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
45 | + $this->trapDBType=$dbParam['db']; |
|
46 | + $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
47 | + $this->db_connect_trap(); |
|
48 | 48 | |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Setup and connect to IDO database |
|
53 | - * @param array $dbParam : array of named params |
|
54 | - */ |
|
55 | - public function setupIDO($dbParam) |
|
56 | - { |
|
57 | - $this->idoDSN=$this->setupDSN($dbParam); |
|
58 | - $this->idoUsername = $dbParam['username']; |
|
59 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
60 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
61 | - $this->idoDBType=$dbParam['db']; |
|
62 | - $this->db_connect_ido(); |
|
63 | - } |
|
51 | + /** |
|
52 | + * Setup and connect to IDO database |
|
53 | + * @param array $dbParam : array of named params |
|
54 | + */ |
|
55 | + public function setupIDO($dbParam) |
|
56 | + { |
|
57 | + $this->idoDSN=$this->setupDSN($dbParam); |
|
58 | + $this->idoUsername = $dbParam['username']; |
|
59 | + $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
60 | + $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
61 | + $this->idoDBType=$dbParam['db']; |
|
62 | + $this->db_connect_ido(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Connect to IDO database |
|
67 | - * @return \PDO |
|
68 | - */ |
|
69 | - public function db_connect_ido() |
|
70 | - { |
|
71 | - if ($this->idoDB != null) { |
|
72 | - // Check if connection is still alive |
|
73 | - try { |
|
74 | - $this->idoDB->query('select 1')->fetchColumn(); |
|
75 | - return $this->idoDB; |
|
76 | - } catch (Exception $e) { |
|
77 | - // select 1 failed, try to reconnect. |
|
78 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
79 | - } |
|
80 | - } |
|
81 | - try { |
|
82 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
83 | - } catch (PDOException $e) { |
|
84 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
85 | - } |
|
86 | - return $this->idoDB; |
|
87 | - } |
|
65 | + /** |
|
66 | + * Connect to IDO database |
|
67 | + * @return \PDO |
|
68 | + */ |
|
69 | + public function db_connect_ido() |
|
70 | + { |
|
71 | + if ($this->idoDB != null) { |
|
72 | + // Check if connection is still alive |
|
73 | + try { |
|
74 | + $this->idoDB->query('select 1')->fetchColumn(); |
|
75 | + return $this->idoDB; |
|
76 | + } catch (Exception $e) { |
|
77 | + // select 1 failed, try to reconnect. |
|
78 | + $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
79 | + } |
|
80 | + } |
|
81 | + try { |
|
82 | + $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
83 | + } catch (PDOException $e) { |
|
84 | + $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
85 | + } |
|
86 | + return $this->idoDB; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Connect to Trap database |
|
91 | - * @return \PDO |
|
92 | - */ |
|
93 | - public function db_connect_trap() |
|
94 | - { |
|
95 | - if ($this->trapDB != null) { |
|
96 | - // Check if connection is still alive |
|
97 | - try { |
|
98 | - $this->trapDB->query('select 1')->fetchColumn(); |
|
99 | - return $this->trapDB; |
|
100 | - } catch (Exception $e) { |
|
101 | - // select 1 failed, try to reconnect. |
|
102 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
103 | - } |
|
104 | - } |
|
105 | - try { |
|
106 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
107 | - } catch (PDOException $e) { |
|
108 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
109 | - } |
|
110 | - return $this->trapDB; |
|
111 | - } |
|
89 | + /** |
|
90 | + * Connect to Trap database |
|
91 | + * @return \PDO |
|
92 | + */ |
|
93 | + public function db_connect_trap() |
|
94 | + { |
|
95 | + if ($this->trapDB != null) { |
|
96 | + // Check if connection is still alive |
|
97 | + try { |
|
98 | + $this->trapDB->query('select 1')->fetchColumn(); |
|
99 | + return $this->trapDB; |
|
100 | + } catch (Exception $e) { |
|
101 | + // select 1 failed, try to reconnect. |
|
102 | + $this->logging->log('Database connection lost, reconnecting',WARN); |
|
103 | + } |
|
104 | + } |
|
105 | + try { |
|
106 | + $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
107 | + } catch (PDOException $e) { |
|
108 | + $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
109 | + } |
|
110 | + return $this->trapDB; |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Setup dsn and check parameters |
|
115 | - * @param array $configElmt |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - protected function setupDSN($configElmt) |
|
119 | - { |
|
120 | - if (!array_key_exists('db',$configElmt) || |
|
121 | - !array_key_exists('host',$configElmt) || |
|
122 | - !array_key_exists('dbname',$configElmt) || |
|
123 | - !array_key_exists('username',$configElmt)) |
|
124 | - { |
|
125 | - $this->logging->log('Missing DB params',ERROR); |
|
126 | - return ''; |
|
127 | - } |
|
113 | + /** |
|
114 | + * Setup dsn and check parameters |
|
115 | + * @param array $configElmt |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + protected function setupDSN($configElmt) |
|
119 | + { |
|
120 | + if (!array_key_exists('db',$configElmt) || |
|
121 | + !array_key_exists('host',$configElmt) || |
|
122 | + !array_key_exists('dbname',$configElmt) || |
|
123 | + !array_key_exists('username',$configElmt)) |
|
124 | + { |
|
125 | + $this->logging->log('Missing DB params',ERROR); |
|
126 | + return ''; |
|
127 | + } |
|
128 | 128 | |
129 | - // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
130 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
129 | + // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
130 | + $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
131 | 131 | |
132 | - if (array_key_exists('port', $configElmt)) |
|
133 | - { |
|
134 | - $dsn .= ';port='.$configElmt['port']; |
|
135 | - } |
|
136 | - return $dsn; |
|
137 | - } |
|
132 | + if (array_key_exists('port', $configElmt)) |
|
133 | + { |
|
134 | + $dsn .= ';port='.$configElmt['port']; |
|
135 | + } |
|
136 | + return $dsn; |
|
137 | + } |
|
138 | 138 | |
139 | - /** Set name=element in database config table |
|
140 | - * @param string $name |
|
141 | - * @param string $element |
|
142 | - * @return boolean true on success, else false (error logged) |
|
143 | - */ |
|
144 | - public function setDBConfig($name,$element) |
|
145 | - { |
|
146 | - $db_conn=$this->db_connect_trap(); |
|
147 | - $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
148 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
149 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
150 | - return false; |
|
151 | - } |
|
152 | - $value=$ret_code->fetch(); |
|
153 | - if ($value != null && isset($value['id'])) |
|
154 | - { // Entry exists -> update |
|
155 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
156 | - } |
|
157 | - else |
|
158 | - { // Entry does no exists -> create |
|
159 | - $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
160 | - } |
|
161 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
162 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
163 | - return false; |
|
164 | - } |
|
165 | - $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
166 | - return true; |
|
167 | - } |
|
139 | + /** Set name=element in database config table |
|
140 | + * @param string $name |
|
141 | + * @param string $element |
|
142 | + * @return boolean true on success, else false (error logged) |
|
143 | + */ |
|
144 | + public function setDBConfig($name,$element) |
|
145 | + { |
|
146 | + $db_conn=$this->db_connect_trap(); |
|
147 | + $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
|
148 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
149 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
150 | + return false; |
|
151 | + } |
|
152 | + $value=$ret_code->fetch(); |
|
153 | + if ($value != null && isset($value['id'])) |
|
154 | + { // Entry exists -> update |
|
155 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
|
156 | + } |
|
157 | + else |
|
158 | + { // Entry does no exists -> create |
|
159 | + $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
|
160 | + } |
|
161 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
162 | + $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
163 | + return false; |
|
164 | + } |
|
165 | + $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
166 | + return true; |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Get data from db_config |
|
171 | - * @param $element string name of param |
|
172 | - * @return mixed : value (or null) |
|
173 | - */ |
|
174 | - public function getDBConfig($element) |
|
175 | - { |
|
176 | - $db_conn=$this->db_connect_trap(); |
|
177 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
178 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
179 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
180 | - return null; |
|
181 | - } |
|
182 | - $value=$ret_code->fetch(); |
|
183 | - if ($value != null && isset($value['value'])) |
|
184 | - { |
|
185 | - return $value['value']; |
|
186 | - } |
|
187 | - return null; |
|
188 | - } |
|
169 | + /** |
|
170 | + * Get data from db_config |
|
171 | + * @param $element string name of param |
|
172 | + * @return mixed : value (or null) |
|
173 | + */ |
|
174 | + public function getDBConfig($element) |
|
175 | + { |
|
176 | + $db_conn=$this->db_connect_trap(); |
|
177 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
178 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
179 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
180 | + return null; |
|
181 | + } |
|
182 | + $value=$ret_code->fetch(); |
|
183 | + if ($value != null && isset($value['value'])) |
|
184 | + { |
|
185 | + return $value['value']; |
|
186 | + } |
|
187 | + return null; |
|
188 | + } |
|
189 | 189 | |
190 | 190 | |
191 | - //********* Schema Management *********************/ |
|
191 | + //********* Schema Management *********************/ |
|
192 | 192 | |
193 | - /** Create database schema |
|
194 | - * @param $schema_file string File to read schema from |
|
195 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
196 | - */ |
|
197 | - public function create_schema($schema_file,$table_prefix) |
|
198 | - { |
|
199 | - //Read data from snmptrapd from stdin |
|
200 | - $input_stream=fopen($schema_file, 'r'); |
|
193 | + /** Create database schema |
|
194 | + * @param $schema_file string File to read schema from |
|
195 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
196 | + */ |
|
197 | + public function create_schema($schema_file,$table_prefix) |
|
198 | + { |
|
199 | + //Read data from snmptrapd from stdin |
|
200 | + $input_stream=fopen($schema_file, 'r'); |
|
201 | 201 | |
202 | - if ($input_stream=== false) |
|
203 | - { |
|
204 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
205 | - return; |
|
206 | - } |
|
207 | - $newline=''; |
|
208 | - $cur_table=''; |
|
209 | - $cur_table_array=array(); |
|
210 | - $db_conn=$this->db_connect_trap(); |
|
202 | + if ($input_stream=== false) |
|
203 | + { |
|
204 | + $this->logging->log("Error reading schema !",ERROR,''); |
|
205 | + return; |
|
206 | + } |
|
207 | + $newline=''; |
|
208 | + $cur_table=''; |
|
209 | + $cur_table_array=array(); |
|
210 | + $db_conn=$this->db_connect_trap(); |
|
211 | 211 | |
212 | - while (($line=fgets($input_stream)) !== false) |
|
213 | - { |
|
214 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
215 | - if (preg_match('/; *$/', $newline)) |
|
216 | - { |
|
217 | - $sql= $newline; |
|
218 | - if ($db_conn->query($sql) === false) { |
|
219 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
220 | - return; |
|
221 | - } |
|
222 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
223 | - { |
|
224 | - $cur_table='table '.$cur_table_array[1]; |
|
225 | - } |
|
226 | - else |
|
227 | - { |
|
228 | - $cur_table='secret SQL stuff :-)'; |
|
229 | - } |
|
230 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
231 | - $newline=''; |
|
232 | - } |
|
233 | - } |
|
212 | + while (($line=fgets($input_stream)) !== false) |
|
213 | + { |
|
214 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
215 | + if (preg_match('/; *$/', $newline)) |
|
216 | + { |
|
217 | + $sql= $newline; |
|
218 | + if ($db_conn->query($sql) === false) { |
|
219 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
220 | + return; |
|
221 | + } |
|
222 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
223 | + { |
|
224 | + $cur_table='table '.$cur_table_array[1]; |
|
225 | + } |
|
226 | + else |
|
227 | + { |
|
228 | + $cur_table='secret SQL stuff :-)'; |
|
229 | + } |
|
230 | + $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
231 | + $newline=''; |
|
232 | + } |
|
233 | + } |
|
234 | 234 | |
235 | - $sql= $newline; |
|
236 | - if ($sql != '' ) |
|
237 | - { |
|
238 | - if ($db_conn->query($sql) === false) { |
|
239 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
240 | - return; |
|
241 | - } |
|
242 | - } |
|
243 | - $this->logging->log('Schema created',INFO); |
|
244 | - } |
|
235 | + $sql= $newline; |
|
236 | + if ($sql != '' ) |
|
237 | + { |
|
238 | + if ($db_conn->query($sql) === false) { |
|
239 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
240 | + return; |
|
241 | + } |
|
242 | + } |
|
243 | + $this->logging->log('Schema created',INFO); |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * Update database schema from current (as set in db) to $target_version |
|
248 | - * @param $prefix string file prefix of sql update File |
|
249 | - * @param $target_version int target db version number |
|
250 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
251 | - * @param bool $getmsg : only get messages from version upgrades |
|
252 | - * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
253 | - */ |
|
254 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
255 | - { |
|
256 | - // Get current db number |
|
257 | - $db_conn=$this->db_connect_trap(); |
|
258 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
259 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
260 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
261 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
262 | - return 'ERROR'; |
|
263 | - } |
|
264 | - $version=$ret_code->fetchAll(); |
|
265 | - $cur_version=$version[0]['value']; |
|
246 | + /** |
|
247 | + * Update database schema from current (as set in db) to $target_version |
|
248 | + * @param $prefix string file prefix of sql update File |
|
249 | + * @param $target_version int target db version number |
|
250 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
251 | + * @param bool $getmsg : only get messages from version upgrades |
|
252 | + * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
253 | + */ |
|
254 | + public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
255 | + { |
|
256 | + // Get current db number |
|
257 | + $db_conn=$this->db_connect_trap(); |
|
258 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
259 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
260 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
261 | + $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
262 | + return 'ERROR'; |
|
263 | + } |
|
264 | + $version=$ret_code->fetchAll(); |
|
265 | + $cur_version=$version[0]['value']; |
|
266 | 266 | |
267 | - if ($this->trapDBType == 'pgsql') |
|
268 | - { |
|
269 | - $prefix .= 'update_pgsql/schema_'; |
|
270 | - } |
|
271 | - else |
|
272 | - { |
|
273 | - $prefix .= 'update_sql/schema_'; |
|
274 | - } |
|
275 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
276 | - if ($getmsg === true) |
|
277 | - { |
|
278 | - return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
279 | - } |
|
267 | + if ($this->trapDBType == 'pgsql') |
|
268 | + { |
|
269 | + $prefix .= 'update_pgsql/schema_'; |
|
270 | + } |
|
271 | + else |
|
272 | + { |
|
273 | + $prefix .= 'update_sql/schema_'; |
|
274 | + } |
|
275 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
276 | + if ($getmsg === true) |
|
277 | + { |
|
278 | + return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
279 | + } |
|
280 | 280 | |
281 | - if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
282 | - { |
|
283 | - return 'ERROR'; |
|
284 | - } |
|
285 | - return ''; |
|
281 | + if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
282 | + { |
|
283 | + return 'ERROR'; |
|
284 | + } |
|
285 | + return ''; |
|
286 | 286 | |
287 | - } |
|
287 | + } |
|
288 | 288 | |
289 | - /** |
|
290 | - * Update database schema from current (as set in db) to $target_version |
|
291 | - * @param string $prefix file prefix of sql update File |
|
292 | - * @param int $cur_version current db version number |
|
293 | - * @param int $target_version target db version number |
|
294 | - * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
295 | - * @return bool : true on error |
|
296 | - */ |
|
297 | - public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
298 | - { |
|
299 | - while($cur_version<$target_version) |
|
300 | - { // TODO : execute pre & post scripts |
|
301 | - $cur_version++; |
|
302 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
303 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
304 | - $input_stream=fopen($updateFile, 'r'); |
|
305 | - if ($input_stream=== false) |
|
306 | - { |
|
307 | - $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
308 | - return true; |
|
309 | - } |
|
310 | - $newline=''; |
|
311 | - $db_conn=$this->db_connect_trap(); |
|
312 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
313 | - while (($line=fgets($input_stream)) !== false) |
|
314 | - { |
|
315 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
316 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
317 | - if (preg_match('/; *$/', $newline)) |
|
318 | - { |
|
319 | - $sql_req=$db_conn->prepare($newline); |
|
320 | - if ($sql_req->execute() === false) { |
|
321 | - $this->logging->log('Error create schema : '.$newline,ERROR); |
|
322 | - return true; |
|
323 | - } |
|
324 | - $cur_table_array=array(); |
|
325 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
326 | - { |
|
327 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
328 | - } |
|
329 | - else |
|
330 | - { |
|
331 | - $cur_table='secret SQL stuff :-)'; |
|
332 | - //$cur_table=$newline; |
|
333 | - } |
|
334 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
289 | + /** |
|
290 | + * Update database schema from current (as set in db) to $target_version |
|
291 | + * @param string $prefix file prefix of sql update File |
|
292 | + * @param int $cur_version current db version number |
|
293 | + * @param int $target_version target db version number |
|
294 | + * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
295 | + * @return bool : true on error |
|
296 | + */ |
|
297 | + public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
298 | + { |
|
299 | + while($cur_version<$target_version) |
|
300 | + { // TODO : execute pre & post scripts |
|
301 | + $cur_version++; |
|
302 | + $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
303 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
304 | + $input_stream=fopen($updateFile, 'r'); |
|
305 | + if ($input_stream=== false) |
|
306 | + { |
|
307 | + $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
308 | + return true; |
|
309 | + } |
|
310 | + $newline=''; |
|
311 | + $db_conn=$this->db_connect_trap(); |
|
312 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
313 | + while (($line=fgets($input_stream)) !== false) |
|
314 | + { |
|
315 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
316 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
317 | + if (preg_match('/; *$/', $newline)) |
|
318 | + { |
|
319 | + $sql_req=$db_conn->prepare($newline); |
|
320 | + if ($sql_req->execute() === false) { |
|
321 | + $this->logging->log('Error create schema : '.$newline,ERROR); |
|
322 | + return true; |
|
323 | + } |
|
324 | + $cur_table_array=array(); |
|
325 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
326 | + { |
|
327 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
328 | + } |
|
329 | + else |
|
330 | + { |
|
331 | + $cur_table='secret SQL stuff :-)'; |
|
332 | + //$cur_table=$newline; |
|
333 | + } |
|
334 | + $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
335 | 335 | |
336 | - $newline=''; |
|
337 | - } |
|
338 | - } |
|
339 | - fclose($input_stream); |
|
336 | + $newline=''; |
|
337 | + } |
|
338 | + } |
|
339 | + fclose($input_stream); |
|
340 | 340 | |
341 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
342 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
343 | - if ($db_conn->query($sql) === false) { |
|
344 | - $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
345 | - return true; |
|
346 | - } |
|
341 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
342 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
343 | + if ($db_conn->query($sql) === false) { |
|
344 | + $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
345 | + return true; |
|
346 | + } |
|
347 | 347 | |
348 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
349 | - } |
|
350 | - return false; |
|
351 | - } |
|
348 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
349 | + } |
|
350 | + return false; |
|
351 | + } |
|
352 | 352 | |
353 | - /** |
|
354 | - * Get database message for update to $target_version |
|
355 | - * @param string $prefix file prefix of sql update File |
|
356 | - * @param int $cur_version current db version number |
|
357 | - * @param int $target_version target db version number |
|
358 | - * @return string : return messages or 'ERROR'. |
|
359 | - */ |
|
360 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
361 | - { |
|
353 | + /** |
|
354 | + * Get database message for update to $target_version |
|
355 | + * @param string $prefix file prefix of sql update File |
|
356 | + * @param int $cur_version current db version number |
|
357 | + * @param int $target_version target db version number |
|
358 | + * @return string : return messages or 'ERROR'. |
|
359 | + */ |
|
360 | + private function update_schema_message($prefix,$cur_version,$target_version) |
|
361 | + { |
|
362 | 362 | |
363 | - $message=''; |
|
364 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
365 | - while($cur_version<$target_version) |
|
366 | - { |
|
367 | - $cur_version++; |
|
368 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
369 | - $input_stream=fopen($updateFile, 'r'); |
|
370 | - if ($input_stream=== false) |
|
371 | - { |
|
372 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
373 | - return 'ERROR'; |
|
374 | - } |
|
375 | - do |
|
376 | - { |
|
377 | - $line=fgets($input_stream); |
|
378 | - } |
|
379 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
380 | - fclose($input_stream); |
|
381 | - if ($line === false) |
|
382 | - { |
|
383 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
384 | - return ''; |
|
385 | - } |
|
386 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
387 | - } |
|
388 | - return $message; |
|
389 | - } |
|
363 | + $message=''; |
|
364 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
365 | + while($cur_version<$target_version) |
|
366 | + { |
|
367 | + $cur_version++; |
|
368 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
369 | + $input_stream=fopen($updateFile, 'r'); |
|
370 | + if ($input_stream=== false) |
|
371 | + { |
|
372 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
373 | + return 'ERROR'; |
|
374 | + } |
|
375 | + do |
|
376 | + { |
|
377 | + $line=fgets($input_stream); |
|
378 | + } |
|
379 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
380 | + fclose($input_stream); |
|
381 | + if ($line === false) |
|
382 | + { |
|
383 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
384 | + return ''; |
|
385 | + } |
|
386 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
387 | + } |
|
388 | + return $message; |
|
389 | + } |
|
390 | 390 | |
391 | 391 | } |
392 | 392 | \ No newline at end of file |
@@ -34,16 +34,16 @@ discard block |
||
34 | 34 | * @param Logging $logClass : where to log |
35 | 35 | * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
36 | 36 | */ |
37 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
37 | + function __construct($logClass, $dbParam, $dbPrefix) |
|
38 | 38 | { |
39 | 39 | $this->logging=$logClass; |
40 | 40 | $this->dbPrefix=$dbPrefix; |
41 | 41 | |
42 | 42 | $this->trapDSN=$this->setupDSN($dbParam); |
43 | - $this->trapUsername = $dbParam['username']; |
|
44 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
43 | + $this->trapUsername=$dbParam['username']; |
|
44 | + $this->trapPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : ''; |
|
45 | 45 | $this->trapDBType=$dbParam['db']; |
46 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
46 | + $this->logging->log('DSN : '.$this->trapDSN.';user '.$this->trapUsername.' / prefix : '.$this->dbPrefix, INFO); |
|
47 | 47 | $this->db_connect_trap(); |
48 | 48 | |
49 | 49 | } |
@@ -55,9 +55,9 @@ discard block |
||
55 | 55 | public function setupIDO($dbParam) |
56 | 56 | { |
57 | 57 | $this->idoDSN=$this->setupDSN($dbParam); |
58 | - $this->idoUsername = $dbParam['username']; |
|
59 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
60 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
58 | + $this->idoUsername=$dbParam['username']; |
|
59 | + $this->idoPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : ''; |
|
60 | + $this->logging->log('DSN : '.$this->idoDSN.';user '.$this->idoUsername, INFO); |
|
61 | 61 | $this->idoDBType=$dbParam['db']; |
62 | 62 | $this->db_connect_ido(); |
63 | 63 | } |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | return $this->idoDB; |
76 | 76 | } catch (Exception $e) { |
77 | 77 | // select 1 failed, try to reconnect. |
78 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
78 | + $this->logging->log('Database IDO connection lost, reconnecting', WARN); |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 | try { |
82 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
82 | + $this->idoDB=new PDO($this->idoDSN, $this->idoUsername, $this->idoPass); |
|
83 | 83 | } catch (PDOException $e) { |
84 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
84 | + $this->logging->log('Connection failed to IDO : '.$e->getMessage(), ERROR, ''); |
|
85 | 85 | } |
86 | 86 | return $this->idoDB; |
87 | 87 | } |
@@ -99,13 +99,13 @@ discard block |
||
99 | 99 | return $this->trapDB; |
100 | 100 | } catch (Exception $e) { |
101 | 101 | // select 1 failed, try to reconnect. |
102 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
102 | + $this->logging->log('Database connection lost, reconnecting', WARN); |
|
103 | 103 | } |
104 | 104 | } |
105 | 105 | try { |
106 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
106 | + $this->trapDB=new PDO($this->trapDSN, $this->trapUsername, $this->trapPass); |
|
107 | 107 | } catch (PDOException $e) { |
108 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
108 | + $this->logging->log('Connection failed : '.$e->getMessage(), ERROR, ''); |
|
109 | 109 | } |
110 | 110 | return $this->trapDB; |
111 | 111 | } |
@@ -117,21 +117,21 @@ discard block |
||
117 | 117 | */ |
118 | 118 | protected function setupDSN($configElmt) |
119 | 119 | { |
120 | - if (!array_key_exists('db',$configElmt) || |
|
121 | - !array_key_exists('host',$configElmt) || |
|
122 | - !array_key_exists('dbname',$configElmt) || |
|
123 | - !array_key_exists('username',$configElmt)) |
|
120 | + if (!array_key_exists('db', $configElmt) || |
|
121 | + !array_key_exists('host', $configElmt) || |
|
122 | + !array_key_exists('dbname', $configElmt) || |
|
123 | + !array_key_exists('username', $configElmt)) |
|
124 | 124 | { |
125 | - $this->logging->log('Missing DB params',ERROR); |
|
125 | + $this->logging->log('Missing DB params', ERROR); |
|
126 | 126 | return ''; |
127 | 127 | } |
128 | 128 | |
129 | 129 | // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
130 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
130 | + $dsn=$configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
131 | 131 | |
132 | 132 | if (array_key_exists('port', $configElmt)) |
133 | 133 | { |
134 | - $dsn .= ';port='.$configElmt['port']; |
|
134 | + $dsn.=';port='.$configElmt['port']; |
|
135 | 135 | } |
136 | 136 | return $dsn; |
137 | 137 | } |
@@ -141,12 +141,12 @@ discard block |
||
141 | 141 | * @param string $element |
142 | 142 | * @return boolean true on success, else false (error logged) |
143 | 143 | */ |
144 | - public function setDBConfig($name,$element) |
|
144 | + public function setDBConfig($name, $element) |
|
145 | 145 | { |
146 | 146 | $db_conn=$this->db_connect_trap(); |
147 | 147 | $sql='SELECT id from '.$this->dbPrefix.'db_config WHERE ( name=\''.$name.'\' )'; |
148 | 148 | if (($ret_code=$db_conn->query($sql)) === false) { |
149 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
149 | + $this->logging->log('Error setting config element : '.$sql, WARN, ''); |
|
150 | 150 | return false; |
151 | 151 | } |
152 | 152 | $value=$ret_code->fetch(); |
@@ -159,10 +159,10 @@ discard block |
||
159 | 159 | $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
160 | 160 | } |
161 | 161 | if (($ret_code=$db_conn->query($sql)) === false) { |
162 | - $this->logging->log('Error setting config element : ' . $sql,WARN,''); |
|
162 | + $this->logging->log('Error setting config element : '.$sql, WARN, ''); |
|
163 | 163 | return false; |
164 | 164 | } |
165 | - $this->logging->log('Setting config '.$name.' = '.$element.' in database',INFO); |
|
165 | + $this->logging->log('Setting config '.$name.' = '.$element.' in database', INFO); |
|
166 | 166 | return true; |
167 | 167 | } |
168 | 168 | |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | $db_conn=$this->db_connect_trap(); |
177 | 177 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
178 | 178 | if (($ret_code=$db_conn->query($sql)) === false) { |
179 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
179 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
180 | 180 | return null; |
181 | 181 | } |
182 | 182 | $value=$ret_code->fetch(); |
@@ -194,14 +194,14 @@ discard block |
||
194 | 194 | * @param $schema_file string File to read schema from |
195 | 195 | * @param $table_prefix string to replace #PREFIX# in schema file by this |
196 | 196 | */ |
197 | - public function create_schema($schema_file,$table_prefix) |
|
197 | + public function create_schema($schema_file, $table_prefix) |
|
198 | 198 | { |
199 | 199 | //Read data from snmptrapd from stdin |
200 | 200 | $input_stream=fopen($schema_file, 'r'); |
201 | 201 | |
202 | - if ($input_stream=== false) |
|
202 | + if ($input_stream === false) |
|
203 | 203 | { |
204 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
204 | + $this->logging->log("Error reading schema !", ERROR, ''); |
|
205 | 205 | return; |
206 | 206 | } |
207 | 207 | $newline=''; |
@@ -211,15 +211,15 @@ discard block |
||
211 | 211 | |
212 | 212 | while (($line=fgets($input_stream)) !== false) |
213 | 213 | { |
214 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
214 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
215 | 215 | if (preg_match('/; *$/', $newline)) |
216 | 216 | { |
217 | - $sql= $newline; |
|
217 | + $sql=$newline; |
|
218 | 218 | if ($db_conn->query($sql) === false) { |
219 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
219 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
220 | 220 | return; |
221 | 221 | } |
222 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
222 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
223 | 223 | { |
224 | 224 | $cur_table='table '.$cur_table_array[1]; |
225 | 225 | } |
@@ -227,20 +227,20 @@ discard block |
||
227 | 227 | { |
228 | 228 | $cur_table='secret SQL stuff :-)'; |
229 | 229 | } |
230 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
230 | + $this->logging->log('Creating : '.$cur_table, INFO); |
|
231 | 231 | $newline=''; |
232 | 232 | } |
233 | 233 | } |
234 | 234 | |
235 | - $sql= $newline; |
|
236 | - if ($sql != '' ) |
|
235 | + $sql=$newline; |
|
236 | + if ($sql != '') |
|
237 | 237 | { |
238 | 238 | if ($db_conn->query($sql) === false) { |
239 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
239 | + $this->logging->log('Error create schema : '.$sql, ERROR, ''); |
|
240 | 240 | return; |
241 | 241 | } |
242 | 242 | } |
243 | - $this->logging->log('Schema created',INFO); |
|
243 | + $this->logging->log('Schema created', INFO); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | /** |
@@ -251,14 +251,14 @@ discard block |
||
251 | 251 | * @param bool $getmsg : only get messages from version upgrades |
252 | 252 | * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
253 | 253 | */ |
254 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
254 | + public function update_schema($prefix, $target_version, $table_prefix, $getmsg=false) |
|
255 | 255 | { |
256 | 256 | // Get current db number |
257 | 257 | $db_conn=$this->db_connect_trap(); |
258 | 258 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
259 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
259 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
260 | 260 | if (($ret_code=$db_conn->query($sql)) === false) { |
261 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
261 | + $this->logging->log('Cannot get db version. Query : '.$sql, 2, ''); |
|
262 | 262 | return 'ERROR'; |
263 | 263 | } |
264 | 264 | $version=$ret_code->fetchAll(); |
@@ -266,11 +266,11 @@ discard block |
||
266 | 266 | |
267 | 267 | if ($this->trapDBType == 'pgsql') |
268 | 268 | { |
269 | - $prefix .= 'update_pgsql/schema_'; |
|
269 | + $prefix.='update_pgsql/schema_'; |
|
270 | 270 | } |
271 | 271 | else |
272 | 272 | { |
273 | - $prefix .= 'update_sql/schema_'; |
|
273 | + $prefix.='update_sql/schema_'; |
|
274 | 274 | } |
275 | 275 | //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
276 | 276 | if ($getmsg === true) |
@@ -294,17 +294,17 @@ discard block |
||
294 | 294 | * @param string $table_prefix to replace #PREFIX# in schema file by this |
295 | 295 | * @return bool : true on error |
296 | 296 | */ |
297 | - public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
297 | + public function update_schema_do($prefix, $cur_version, $target_version, $table_prefix) |
|
298 | 298 | { |
299 | - while($cur_version<$target_version) |
|
299 | + while ($cur_version < $target_version) |
|
300 | 300 | { // TODO : execute pre & post scripts |
301 | 301 | $cur_version++; |
302 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
303 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
302 | + $this->logging->log('Updating to version : '.$cur_version, INFO); |
|
303 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
304 | 304 | $input_stream=fopen($updateFile, 'r'); |
305 | - if ($input_stream=== false) |
|
305 | + if ($input_stream === false) |
|
306 | 306 | { |
307 | - $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
307 | + $this->logging->log("Error reading update file ".$updateFile, ERROR); |
|
308 | 308 | return true; |
309 | 309 | } |
310 | 310 | $newline=''; |
@@ -313,25 +313,25 @@ discard block |
||
313 | 313 | while (($line=fgets($input_stream)) !== false) |
314 | 314 | { |
315 | 315 | if (preg_match('/^#/', $line)) continue; // ignore comment lines |
316 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
316 | + $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line)); |
|
317 | 317 | if (preg_match('/; *$/', $newline)) |
318 | 318 | { |
319 | 319 | $sql_req=$db_conn->prepare($newline); |
320 | 320 | if ($sql_req->execute() === false) { |
321 | - $this->logging->log('Error create schema : '.$newline,ERROR); |
|
321 | + $this->logging->log('Error create schema : '.$newline, ERROR); |
|
322 | 322 | return true; |
323 | 323 | } |
324 | 324 | $cur_table_array=array(); |
325 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
325 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/', $newline, $cur_table_array)) |
|
326 | 326 | { |
327 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
327 | + $cur_table=$cur_table_array[1].' SQL table '.$cur_table_array[2]; |
|
328 | 328 | } |
329 | 329 | else |
330 | 330 | { |
331 | 331 | $cur_table='secret SQL stuff :-)'; |
332 | 332 | //$cur_table=$newline; |
333 | 333 | } |
334 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
334 | + $this->logging->log('Doing : '.$cur_table, INFO); |
|
335 | 335 | |
336 | 336 | $newline=''; |
337 | 337 | } |
@@ -339,13 +339,13 @@ discard block |
||
339 | 339 | fclose($input_stream); |
340 | 340 | |
341 | 341 | $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
342 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
342 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
343 | 343 | if ($db_conn->query($sql) === false) { |
344 | - $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
344 | + $this->logging->log('Cannot update db version. Query : '.$sql, WARN); |
|
345 | 345 | return true; |
346 | 346 | } |
347 | 347 | |
348 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
348 | + $this->logging->log('Schema updated to version : '.$cur_version, INFO); |
|
349 | 349 | } |
350 | 350 | return false; |
351 | 351 | } |
@@ -357,33 +357,33 @@ discard block |
||
357 | 357 | * @param int $target_version target db version number |
358 | 358 | * @return string : return messages or 'ERROR'. |
359 | 359 | */ |
360 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
360 | + private function update_schema_message($prefix, $cur_version, $target_version) |
|
361 | 361 | { |
362 | 362 | |
363 | 363 | $message=''; |
364 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
365 | - while($cur_version<$target_version) |
|
364 | + $this->logging->log('getting message for upgrade', DEBUG); |
|
365 | + while ($cur_version < $target_version) |
|
366 | 366 | { |
367 | 367 | $cur_version++; |
368 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
368 | + $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql'; |
|
369 | 369 | $input_stream=fopen($updateFile, 'r'); |
370 | - if ($input_stream=== false) |
|
370 | + if ($input_stream === false) |
|
371 | 371 | { |
372 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
372 | + $this->logging->log("Error reading update file ".$updateFile, 2, ''); |
|
373 | 373 | return 'ERROR'; |
374 | 374 | } |
375 | 375 | do |
376 | 376 | { |
377 | 377 | $line=fgets($input_stream); |
378 | 378 | } |
379 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
379 | + while ($line !== false && !preg_match('/#MESSAGE/', $line)); |
|
380 | 380 | fclose($input_stream); |
381 | 381 | if ($line === false) |
382 | 382 | { |
383 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
383 | + $this->logging->log("No message in file ".$updateFile, 2, ''); |
|
384 | 384 | return ''; |
385 | 385 | } |
386 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
386 | + $message.=($cur_version - 1).'->'.$cur_version.' : '.preg_replace('/#MESSAGE : /', '', $line)."\n"; |
|
387 | 387 | } |
388 | 388 | return $message; |
389 | 389 | } |
@@ -153,8 +153,7 @@ discard block |
||
153 | 153 | if ($value != null && isset($value['id'])) |
154 | 154 | { // Entry exists -> update |
155 | 155 | $sql='UPDATE '.$this->dbPrefix.'db_config SET value = \''.$element.'\' WHERE (id = '.$value['id'].')'; |
156 | - } |
|
157 | - else |
|
156 | + } else |
|
158 | 157 | { // Entry does no exists -> create |
159 | 158 | $sql='INSERT INTO '.$this->dbPrefix.'db_config (name,value) VALUES (\''.$name.'\' , \''.$element.'\' )'; |
160 | 159 | } |
@@ -222,8 +221,7 @@ discard block |
||
222 | 221 | if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
223 | 222 | { |
224 | 223 | $cur_table='table '.$cur_table_array[1]; |
225 | - } |
|
226 | - else |
|
224 | + } else |
|
227 | 225 | { |
228 | 226 | $cur_table='secret SQL stuff :-)'; |
229 | 227 | } |
@@ -267,8 +265,7 @@ discard block |
||
267 | 265 | if ($this->trapDBType == 'pgsql') |
268 | 266 | { |
269 | 267 | $prefix .= 'update_pgsql/schema_'; |
270 | - } |
|
271 | - else |
|
268 | + } else |
|
272 | 269 | { |
273 | 270 | $prefix .= 'update_sql/schema_'; |
274 | 271 | } |
@@ -312,7 +309,10 @@ discard block |
||
312 | 309 | $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
313 | 310 | while (($line=fgets($input_stream)) !== false) |
314 | 311 | { |
315 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
312 | + if (preg_match('/^#/', $line)) { |
|
313 | + continue; |
|
314 | + } |
|
315 | + // ignore comment lines |
|
316 | 316 | $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
317 | 317 | if (preg_match('/; *$/', $newline)) |
318 | 318 | { |
@@ -325,8 +325,7 @@ discard block |
||
325 | 325 | if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
326 | 326 | { |
327 | 327 | $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
328 | - } |
|
329 | - else |
|
328 | + } else |
|
330 | 329 | { |
331 | 330 | $cur_table='secret SQL stuff :-)'; |
332 | 331 | //$cur_table=$newline; |
@@ -87,10 +87,11 @@ discard block |
||
87 | 87 | try |
88 | 88 | { |
89 | 89 | $dbconn = IcingaDbConnection::fromResourceName($DBname); |
90 | - } |
|
91 | - catch (Exception $e) |
|
90 | + } catch (Exception $e) |
|
92 | 91 | { |
93 | - if ($test) return array(2,$DBname); |
|
92 | + if ($test) { |
|
93 | + return array(2,$DBname); |
|
94 | + } |
|
94 | 95 | $this->redirectNow('trapdirector/settings?dberror=2'); |
95 | 96 | return null; |
96 | 97 | } |
@@ -98,10 +99,11 @@ discard block |
||
98 | 99 | try |
99 | 100 | { |
100 | 101 | $db=$dbconn->getConnection(); |
101 | - } |
|
102 | - catch (Exception $e) |
|
102 | + } catch (Exception $e) |
|
103 | 103 | { |
104 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
104 | + if ($test) { |
|
105 | + return array(3,$DBname,$e->getMessage()); |
|
106 | + } |
|
105 | 107 | $this->redirectNow('trapdirector/settings?dberror=3'); |
106 | 108 | return null; |
107 | 109 | } |
@@ -113,25 +115,32 @@ discard block |
||
113 | 115 | $version=$db->fetchRow($query); |
114 | 116 | if ( ($version == null) || ! property_exists($version,'value') ) |
115 | 117 | { |
116 | - if ($test) return array(4,$DBname); |
|
118 | + if ($test) { |
|
119 | + return array(4,$DBname); |
|
120 | + } |
|
117 | 121 | $this->redirectNow('trapdirector/settings?dberror=4'); |
118 | 122 | return null; |
119 | 123 | } |
120 | 124 | if ($version->value < $this->getModuleConfig()->getDbMinVersion()) |
121 | 125 | { |
122 | - if ($test) return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
126 | + if ($test) { |
|
127 | + return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
128 | + } |
|
123 | 129 | $this->redirectNow('trapdirector/settings?dberror=5'); |
124 | 130 | return null; |
125 | 131 | } |
126 | - } |
|
127 | - catch (Exception $e) |
|
132 | + } catch (Exception $e) |
|
128 | 133 | { |
129 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
134 | + if ($test) { |
|
135 | + return array(3,$DBname,$e->getMessage()); |
|
136 | + } |
|
130 | 137 | $this->redirectNow('trapdirector/settings?dberror=4'); |
131 | 138 | return null; |
132 | 139 | } |
133 | 140 | } |
134 | - if ($test) return array(0,''); |
|
141 | + if ($test) { |
|
142 | + return array(0,''); |
|
143 | + } |
|
135 | 144 | return $dbconn; |
136 | 145 | } |
137 | 146 | |
@@ -142,31 +151,41 @@ discard block |
||
142 | 151 | */ |
143 | 152 | public function getDb($test=false) |
144 | 153 | { |
145 | - if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
154 | + if ($this->trapDB != null && $test = false) { |
|
155 | + return $this->trapDB; |
|
156 | + } |
|
146 | 157 | |
147 | 158 | $dbresource=$this->Config()->get('config', 'database'); |
148 | 159 | |
149 | 160 | if ( ! $dbresource ) |
150 | 161 | { |
151 | - if ($test) return array(1,''); |
|
162 | + if ($test) { |
|
163 | + return array(1,''); |
|
164 | + } |
|
152 | 165 | $this->redirectNow('trapdirector/settings?dberror=1'); |
153 | 166 | return null; |
154 | 167 | } |
155 | 168 | $retDB=$this->getDbByName($dbresource,$test,true); |
156 | - if ($test === true) return $retDB; |
|
169 | + if ($test === true) { |
|
170 | + return $retDB; |
|
171 | + } |
|
157 | 172 | $this->trapDB=$retDB; |
158 | 173 | return $this->trapDB; |
159 | 174 | } |
160 | 175 | |
161 | 176 | public function getIdoDb($test=false) |
162 | 177 | { |
163 | - if ($this->icingaDB != null && $test = false) return $this->icingaDB; |
|
178 | + if ($this->icingaDB != null && $test = false) { |
|
179 | + return $this->icingaDB; |
|
180 | + } |
|
164 | 181 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
165 | 182 | $dbresource=$this->Config()->get('config', 'IDOdatabase');; |
166 | 183 | |
167 | 184 | if ( ! $dbresource ) |
168 | 185 | { |
169 | - if ($test) return array(1,'No database in config.ini'); |
|
186 | + if ($test) { |
|
187 | + return array(1,'No database in config.ini'); |
|
188 | + } |
|
170 | 189 | $this->redirectNow('trapdirector/settings?idodberror=1'); |
171 | 190 | return null; |
172 | 191 | } |
@@ -174,10 +193,11 @@ discard block |
||
174 | 193 | try |
175 | 194 | { |
176 | 195 | $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
177 | - } |
|
178 | - catch (Exception $e) |
|
196 | + } catch (Exception $e) |
|
179 | 197 | { |
180 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
198 | + if ($test) { |
|
199 | + return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
200 | + } |
|
181 | 201 | $this->redirectNow('trapdirector/settings?idodberror=2'); |
182 | 202 | return null; |
183 | 203 | } |
@@ -197,8 +217,7 @@ discard block |
||
197 | 217 | { |
198 | 218 | return array(4,"$dbresource does not look like an IDO database"); |
199 | 219 | } |
200 | - } |
|
201 | - catch (Exception $e) |
|
220 | + } catch (Exception $e) |
|
202 | 221 | { |
203 | 222 | return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
204 | 223 | } |
@@ -446,8 +465,7 @@ discard block |
||
446 | 465 | if (isset($common_services[$service->name2]['num'])) |
447 | 466 | { |
448 | 467 | $common_services[$service->name2]['num'] +=1; |
449 | - } |
|
450 | - else |
|
468 | + } else |
|
451 | 469 | { |
452 | 470 | $common_services[$service->name2]['num']=1; |
453 | 471 | $common_services[$service->name2]['name']=$service->name; |
@@ -592,7 +610,9 @@ discard block |
||
592 | 610 | $condition=($condition===null)?'':$condition.' AND '; |
593 | 611 | $condition.="trap_oid='$oid'"; |
594 | 612 | } |
595 | - if($condition === null) return null; |
|
613 | + if($condition === null) { |
|
614 | + return null; |
|
615 | + } |
|
596 | 616 | $query=$db->delete( |
597 | 617 | $this->getModuleConfig()->getTrapTableName(), |
598 | 618 | $condition |
@@ -620,7 +640,9 @@ discard block |
||
620 | 640 | $condition=($condition===null)?'':$condition.' AND '; |
621 | 641 | $condition.="trap_oid='$oid'"; |
622 | 642 | } |
623 | - if($condition === null) return 0; |
|
643 | + if($condition === null) { |
|
644 | + return 0; |
|
645 | + } |
|
624 | 646 | $query=$db->select() |
625 | 647 | ->from( |
626 | 648 | $this->getModuleConfig()->getTrapTableName(), |
@@ -644,18 +666,28 @@ discard block |
||
644 | 666 | array('value'=>'value')) |
645 | 667 | ->where('name=?',$element); |
646 | 668 | $return_row=$db->fetchRow($query); |
647 | - if ($return_row==null) // value does not exists |
|
669 | + if ($return_row==null) { |
|
670 | + // value does not exists |
|
648 | 671 | { |
649 | 672 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
650 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
673 | + } |
|
674 | + if ( ! isset($default[$element])) { |
|
675 | + return null; |
|
676 | + } |
|
677 | + // no default and not value |
|
651 | 678 | |
652 | 679 | $this->addDBConfigValue($element,$default[$element]); |
653 | 680 | return $default[$element]; |
654 | 681 | } |
655 | - if ($return_row->value == null) // value id empty |
|
682 | + if ($return_row->value == null) { |
|
683 | + // value id empty |
|
656 | 684 | { |
657 | 685 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
658 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
686 | + } |
|
687 | + if ( ! isset($default[$element])) { |
|
688 | + return null; |
|
689 | + } |
|
690 | + // no default and not value |
|
659 | 691 | $this->setDBConfigValue($element,$default[$element]); |
660 | 692 | return $default[$element]; |
661 | 693 | } |
@@ -43,8 +43,8 @@ discard block |
||
43 | 43 | |
44 | 44 | |
45 | 45 | /** Get instance of TrapModuleConfig class |
46 | - * @return TrapModuleConfig |
|
47 | - */ |
|
46 | + * @return TrapModuleConfig |
|
47 | + */ |
|
48 | 48 | public function getModuleConfig() |
49 | 49 | { |
50 | 50 | if ($this->moduleConfig == Null) |
@@ -73,12 +73,12 @@ discard block |
||
73 | 73 | |
74 | 74 | public function getTrapHostListTable() |
75 | 75 | { |
76 | - if ($this->trapTableHostList == Null) |
|
76 | + if ($this->trapTableHostList == Null) |
|
77 | 77 | { |
78 | - $this->trapTableHostList = new TrapTableHostList(); |
|
79 | - $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
80 | - } |
|
81 | - return $this->trapTableHostList; |
|
78 | + $this->trapTableHostList = new TrapTableHostList(); |
|
79 | + $this->trapTableHostList->setConfig($this->getModuleConfig()); |
|
80 | + } |
|
81 | + return $this->trapTableHostList; |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | public function getHandlerListTable() |
@@ -92,11 +92,11 @@ discard block |
||
92 | 92 | } |
93 | 93 | |
94 | 94 | /** Get Database connexion |
95 | - * @param $DBname string DB name in resource.ini_ge |
|
96 | - * @param $test bool if set to true, returns error code and not database |
|
97 | - * @param $test_version bool if set to flase, does not test database version of trapDB |
|
98 | - * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
99 | - */ |
|
95 | + * @param $DBname string DB name in resource.ini_ge |
|
96 | + * @param $test bool if set to true, returns error code and not database |
|
97 | + * @param $test_version bool if set to flase, does not test database version of trapDB |
|
98 | + * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
|
99 | + */ |
|
100 | 100 | public function getDbByName($DBname,$test=false,$test_version=true) |
101 | 101 | { |
102 | 102 | try |
@@ -181,20 +181,20 @@ discard block |
||
181 | 181 | |
182 | 182 | if ( ! $dbresource ) |
183 | 183 | { |
184 | - if ($test) return array(1,'No database in config.ini'); |
|
185 | - $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
186 | - return null; |
|
184 | + if ($test) return array(1,'No database in config.ini'); |
|
185 | + $this->redirectNow('trapdirector/settings?idodberror=1'); |
|
186 | + return null; |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | try |
190 | 190 | { |
191 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
191 | + $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
192 | 192 | } |
193 | 193 | catch (Exception $e) |
194 | 194 | { |
195 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
196 | - $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
197 | - return null; |
|
195 | + if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
196 | + $this->redirectNow('trapdirector/settings?idodberror=2'); |
|
197 | + return null; |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | if ($test == false) |
@@ -205,13 +205,13 @@ discard block |
||
205 | 205 | |
206 | 206 | try |
207 | 207 | { |
208 | - $query = $dbconn->select() |
|
209 | - ->from('icinga_dbversion',array('version')); |
|
210 | - $version=$dbconn->fetchRow($query); |
|
211 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
212 | - { |
|
213 | - return array(4,"$dbresource does not look like an IDO database"); |
|
214 | - } |
|
208 | + $query = $dbconn->select() |
|
209 | + ->from('icinga_dbversion',array('version')); |
|
210 | + $version=$dbconn->fetchRow($query); |
|
211 | + if ( ($version == null) || ! property_exists($version,'version') ) |
|
212 | + { |
|
213 | + return array(4,"$dbresource does not look like an IDO database"); |
|
214 | + } |
|
215 | 215 | } |
216 | 216 | catch (Exception $e) |
217 | 217 | { |
@@ -221,15 +221,15 @@ discard block |
||
221 | 221 | return array(0,''); |
222 | 222 | } |
223 | 223 | |
224 | - protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
225 | - { |
|
226 | - $limit = $this->params->get('limit', $limit); |
|
227 | - $page = $this->params->get('page', $offset); |
|
224 | + protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
225 | + { |
|
226 | + $limit = $this->params->get('limit', $limit); |
|
227 | + $page = $this->params->get('page', $offset); |
|
228 | 228 | |
229 | - $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
229 | + $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
|
230 | 230 | |
231 | - return $paginatable; |
|
232 | - } |
|
231 | + return $paginatable; |
|
232 | + } |
|
233 | 233 | |
234 | 234 | public function displayExitError($source,$message) |
235 | 235 | { // TODO : check better ways to transmit data (with POST ?) |
@@ -238,33 +238,33 @@ discard block |
||
238 | 238 | |
239 | 239 | protected function checkReadPermission() |
240 | 240 | { |
241 | - if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
242 | - $this->displayExitError('Permissions','No permission fo view content'); |
|
243 | - } |
|
241 | + if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
242 | + $this->displayExitError('Permissions','No permission fo view content'); |
|
243 | + } |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | protected function checkConfigPermission() |
247 | 247 | { |
248 | - if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
249 | - $this->displayExitError('Permissions','No permission fo configure'); |
|
250 | - } |
|
248 | + if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
249 | + $this->displayExitError('Permissions','No permission fo configure'); |
|
250 | + } |
|
251 | 251 | } |
252 | 252 | |
253 | - /** |
|
254 | - * Check if user has write permission |
|
255 | - * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
256 | - * @return boolean : user has permission |
|
257 | - */ |
|
253 | + /** |
|
254 | + * Check if user has write permission |
|
255 | + * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
|
256 | + * @return boolean : user has permission |
|
257 | + */ |
|
258 | 258 | protected function checkModuleConfigPermission($check=0) |
259 | 259 | { |
260 | - if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
261 | - if ($check == 0) |
|
262 | - { |
|
263 | - $this->displayExitError('Permissions','No permission fo configure module'); |
|
264 | - } |
|
265 | - return false; |
|
266 | - } |
|
267 | - return true; |
|
260 | + if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
261 | + if ($check == 0) |
|
262 | + { |
|
263 | + $this->displayExitError('Permissions','No permission fo configure module'); |
|
264 | + } |
|
265 | + return false; |
|
266 | + } |
|
267 | + return true; |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | /************************* Trap class get **********************/ |
@@ -284,8 +284,8 @@ discard block |
||
284 | 284 | /************************** MIB related **************************/ |
285 | 285 | |
286 | 286 | /** Get MIBLoader class |
287 | - * @return MIBLoader class |
|
288 | - */ |
|
287 | + * @return MIBLoader class |
|
288 | + */ |
|
289 | 289 | protected function getMIB() |
290 | 290 | { |
291 | 291 | if ($this->MIBData == null) |
@@ -303,9 +303,9 @@ discard block |
||
303 | 303 | /************************** Database queries *******************/ |
304 | 304 | |
305 | 305 | /** Get host(s) by IP (v4 or v6) or by name in IDO database |
306 | - * does not catch exceptions |
|
307 | - * @return array of objects ( name, id (object_id), display_name) |
|
308 | - */ |
|
306 | + * does not catch exceptions |
|
307 | + * @return array of objects ( name, id (object_id), display_name) |
|
308 | + */ |
|
309 | 309 | protected function getHostByIP($ip) |
310 | 310 | { |
311 | 311 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -324,9 +324,9 @@ discard block |
||
324 | 324 | } |
325 | 325 | |
326 | 326 | /** Get host(s) by name in IDO database |
327 | - * does not catch exceptions |
|
328 | - * @return array of objects ( name, id (object_id), display_name) |
|
329 | - */ |
|
327 | + * does not catch exceptions |
|
328 | + * @return array of objects ( name, id (object_id), display_name) |
|
329 | + */ |
|
330 | 330 | protected function getHostByName($name) |
331 | 331 | { |
332 | 332 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -345,9 +345,9 @@ discard block |
||
345 | 345 | } |
346 | 346 | |
347 | 347 | /** Get host groups by name in IDO database |
348 | - * does not catch exceptions |
|
349 | - * @return array of objects ( name, id (object_id), display_name) |
|
350 | - */ |
|
348 | + * does not catch exceptions |
|
349 | + * @return array of objects ( name, id (object_id), display_name) |
|
350 | + */ |
|
351 | 351 | protected function getHostGroupByName($ip) |
352 | 352 | { |
353 | 353 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -367,9 +367,9 @@ discard block |
||
367 | 367 | |
368 | 368 | |
369 | 369 | /** Get host IP (v4 and v6) by name in IDO database |
370 | - * does not catch exceptions |
|
371 | - * @return array ( name, display_name, ip4, ip6) |
|
372 | - */ |
|
370 | + * does not catch exceptions |
|
371 | + * @return array ( name, display_name, ip4, ip6) |
|
372 | + */ |
|
373 | 373 | protected function getHostInfoByID($id) |
374 | 374 | { |
375 | 375 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -388,9 +388,9 @@ discard block |
||
388 | 388 | |
389 | 389 | |
390 | 390 | /** Get host by objectid in IDO database |
391 | - * does not catch exceptions |
|
392 | - * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
393 | - */ |
|
391 | + * does not catch exceptions |
|
392 | + * @return array of objects ( id, name, display_name, ip, ip6, ) |
|
393 | + */ |
|
394 | 394 | protected function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
395 | 395 | { |
396 | 396 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -408,10 +408,10 @@ discard block |
||
408 | 408 | } |
409 | 409 | |
410 | 410 | /** Get services from object ( host_object_id) in IDO database |
411 | - * does not catch exceptions |
|
412 | - * @param $id int object_id |
|
413 | - * @return array display_name (of service), service_object_id |
|
414 | - */ |
|
411 | + * does not catch exceptions |
|
412 | + * @param $id int object_id |
|
413 | + * @return array display_name (of service), service_object_id |
|
414 | + */ |
|
415 | 415 | protected function getServicesByHostid($id) |
416 | 416 | { |
417 | 417 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -430,11 +430,11 @@ discard block |
||
430 | 430 | } |
431 | 431 | |
432 | 432 | /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
433 | - * gets all hosts in hostgroup and return common services |
|
434 | - * does not catch exceptions |
|
435 | - * @param $id int object_id |
|
436 | - * @return array display_name (of service), service_object_id |
|
437 | - */ |
|
433 | + * gets all hosts in hostgroup and return common services |
|
434 | + * does not catch exceptions |
|
435 | + * @param $id int object_id |
|
436 | + * @return array display_name (of service), service_object_id |
|
437 | + */ |
|
438 | 438 | protected function getServicesByHostGroupid($id) |
439 | 439 | { |
440 | 440 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
@@ -472,7 +472,7 @@ discard block |
||
472 | 472 | //print_r($common_services); |
473 | 473 | foreach (array_keys($common_services) as $key) |
474 | 474 | { |
475 | - if ($common_services[$key]['num'] == $num_hosts) |
|
475 | + if ($common_services[$key]['num'] == $num_hosts) |
|
476 | 476 | { |
477 | 477 | array_push($result,array($key,$common_services[$key]['name'])); |
478 | 478 | } |
@@ -482,11 +482,11 @@ discard block |
||
482 | 482 | } |
483 | 483 | |
484 | 484 | /** Get services object id by host name / service name in IDO database |
485 | - * does not catch exceptions |
|
486 | - * @param $hostname string host name |
|
487 | - * @param $name string service name |
|
488 | - * @return array service id |
|
489 | - */ |
|
485 | + * does not catch exceptions |
|
486 | + * @param $hostname string host name |
|
487 | + * @param $name string service name |
|
488 | + * @return array service id |
|
489 | + */ |
|
490 | 490 | protected function getServiceIDByName($hostname,$name) |
491 | 491 | { |
492 | 492 | $db = $this->getIdoDb()->getConnection(); |
@@ -509,10 +509,10 @@ discard block |
||
509 | 509 | } |
510 | 510 | |
511 | 511 | /** Get object name from object_id in IDO database |
512 | - * does not catch exceptions |
|
513 | - * @param int $id object_id (default to null, used first if not null) |
|
514 | - * @return array name1 (host) name2 (service) |
|
515 | - */ |
|
512 | + * does not catch exceptions |
|
513 | + * @param int $id object_id (default to null, used first if not null) |
|
514 | + * @return array name1 (host) name2 (service) |
|
515 | + */ |
|
516 | 516 | protected function getObjectNameByid($id) |
517 | 517 | { |
518 | 518 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
@@ -528,9 +528,9 @@ discard block |
||
528 | 528 | } |
529 | 529 | |
530 | 530 | /** Add handler rule in traps DB |
531 | - * @param array $params : array(<db item>=><value>) |
|
532 | - * @return int inserted id |
|
533 | - */ |
|
531 | + * @param array $params : array(<db item>=><value>) |
|
532 | + * @return int inserted id |
|
533 | + */ |
|
534 | 534 | protected function addHandlerRule($params) |
535 | 535 | { |
536 | 536 | // TODO Check for rule consistency |
@@ -552,10 +552,10 @@ discard block |
||
552 | 552 | } |
553 | 553 | |
554 | 554 | /** Update handler rule in traps DB |
555 | - * @param array $params : (<db item>=><value>) |
|
556 | - * @param integer $ruleID : rule id in db |
|
557 | - * @return array affected rows |
|
558 | - */ |
|
555 | + * @param array $params : (<db item>=><value>) |
|
556 | + * @param integer $ruleID : rule id in db |
|
557 | + * @return array affected rows |
|
558 | + */ |
|
559 | 559 | protected function updateHandlerRule($params,$ruleID) |
560 | 560 | { |
561 | 561 | // TODO Check for rule consistency |
@@ -573,8 +573,8 @@ discard block |
||
573 | 573 | } |
574 | 574 | |
575 | 575 | /** Delete rule by id |
576 | - * @param int $ruleID rule id |
|
577 | - */ |
|
576 | + * @param int $ruleID rule id |
|
577 | + */ |
|
578 | 578 | protected function deleteRule($ruleID) |
579 | 579 | { |
580 | 580 | if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
@@ -588,9 +588,9 @@ discard block |
||
588 | 588 | } |
589 | 589 | |
590 | 590 | /** Delete trap by ip & oid |
591 | - * @param $ip string source IP (v4 or v6) |
|
592 | - * @param $oid string oid |
|
593 | - */ |
|
591 | + * @param $ip string source IP (v4 or v6) |
|
592 | + * @param $oid string oid |
|
593 | + */ |
|
594 | 594 | protected function deleteTrap($ip,$oid) |
595 | 595 | { |
596 | 596 | |
@@ -616,9 +616,9 @@ discard block |
||
616 | 616 | |
617 | 617 | |
618 | 618 | /** count trap by ip & oid |
619 | - * @param $ip string source IP (v4 or v6) |
|
620 | - * @param $oid string oid |
|
621 | - */ |
|
619 | + * @param $ip string source IP (v4 or v6) |
|
620 | + * @param $oid string oid |
|
621 | + */ |
|
622 | 622 | protected function countTrap($ip,$oid) |
623 | 623 | { |
624 | 624 | |
@@ -644,8 +644,8 @@ discard block |
||
644 | 644 | } |
645 | 645 | |
646 | 646 | /** get configuration value |
647 | - * @param string $element : configuration name in db |
|
648 | - */ |
|
647 | + * @param string $element : configuration name in db |
|
648 | + */ |
|
649 | 649 | protected function getDBConfigValue($element) |
650 | 650 | { |
651 | 651 | |
@@ -676,9 +676,9 @@ discard block |
||
676 | 676 | } |
677 | 677 | |
678 | 678 | /** add configuration value |
679 | - * @param string $element : name of config element |
|
680 | - * @param string $value : value |
|
681 | - */ |
|
679 | + * @param string $element : name of config element |
|
680 | + * @param string $value : value |
|
681 | + */ |
|
682 | 682 | |
683 | 683 | protected function addDBConfigValue($element,$value) |
684 | 684 | { |
@@ -696,9 +696,9 @@ discard block |
||
696 | 696 | } |
697 | 697 | |
698 | 698 | /** set configuration value |
699 | - * @param string $element : name of config element |
|
700 | - * @param string $value : value |
|
701 | - */ |
|
699 | + * @param string $element : name of config element |
|
700 | + * @param string $value : value |
|
701 | + */ |
|
702 | 702 | protected function setDBConfigValue($element,$value) |
703 | 703 | { |
704 | 704 | |
@@ -712,13 +712,13 @@ discard block |
||
712 | 712 | } |
713 | 713 | |
714 | 714 | /** Check if director is installed |
715 | - * @return bool true/false |
|
716 | - */ |
|
715 | + * @return bool true/false |
|
716 | + */ |
|
717 | 717 | protected function isDirectorInstalled() |
718 | 718 | { |
719 | - $output=array(); |
|
720 | - exec('icingacli module list',$output); |
|
721 | - foreach ($output as $line) |
|
719 | + $output=array(); |
|
720 | + exec('icingacli module list',$output); |
|
721 | + foreach ($output as $line) |
|
722 | 722 | { |
723 | 723 | if (preg_match('/^director .*enabled/',$line)) |
724 | 724 | { |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | { |
55 | 55 | $this->redirectNow('trapdirector/settings?message=No database prefix'); |
56 | 56 | } |
57 | - $this->moduleConfig = new TrapModuleConfig($db_prefix); |
|
57 | + $this->moduleConfig=new TrapModuleConfig($db_prefix); |
|
58 | 58 | } |
59 | 59 | return $this->moduleConfig; |
60 | 60 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function getTrapListTable() { |
67 | 67 | if ($this->trapTableList == Null) { |
68 | - $this->trapTableList = new TrapTableList(); |
|
68 | + $this->trapTableList=new TrapTableList(); |
|
69 | 69 | $this->trapTableList->setConfig($this->getModuleConfig()); |
70 | 70 | } |
71 | 71 | return $this->trapTableList; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | { |
76 | 76 | if ($this->trapTableHostList == Null) |
77 | 77 | { |
78 | - $this->trapTableHostList = new TrapTableHostList(); |
|
78 | + $this->trapTableHostList=new TrapTableHostList(); |
|
79 | 79 | $this->trapTableHostList->setConfig($this->getModuleConfig()); |
80 | 80 | } |
81 | 81 | return $this->trapTableHostList; |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | { |
86 | 86 | if ($this->handlerTableList == Null) |
87 | 87 | { |
88 | - $this->handlerTableList = new HandlerTableList(); |
|
88 | + $this->handlerTableList=new HandlerTableList(); |
|
89 | 89 | $this->handlerTableList->setConfig($this->getModuleConfig()); |
90 | 90 | } |
91 | 91 | return $this->handlerTableList; |
@@ -97,15 +97,15 @@ discard block |
||
97 | 97 | * @param $test_version bool if set to flase, does not test database version of trapDB |
98 | 98 | * @return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error. |
99 | 99 | */ |
100 | - public function getDbByName($DBname,$test=false,$test_version=true) |
|
100 | + public function getDbByName($DBname, $test=false, $test_version=true) |
|
101 | 101 | { |
102 | 102 | try |
103 | 103 | { |
104 | - $dbconn = IcingaDbConnection::fromResourceName($DBname); |
|
104 | + $dbconn=IcingaDbConnection::fromResourceName($DBname); |
|
105 | 105 | } |
106 | 106 | catch (Exception $e) |
107 | 107 | { |
108 | - if ($test) return array(2,$DBname); |
|
108 | + if ($test) return array(2, $DBname); |
|
109 | 109 | $this->redirectNow('trapdirector/settings?dberror=2'); |
110 | 110 | return null; |
111 | 111 | } |
@@ -116,37 +116,37 @@ discard block |
||
116 | 116 | } |
117 | 117 | catch (Exception $e) |
118 | 118 | { |
119 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
119 | + if ($test) return array(3, $DBname, $e->getMessage()); |
|
120 | 120 | $this->redirectNow('trapdirector/settings?dberror=3'); |
121 | 121 | return null; |
122 | 122 | } |
123 | 123 | try |
124 | 124 | { |
125 | - $query = $db->select() |
|
126 | - ->from($this->getModuleConfig()->getDbConfigTableName(),'value') |
|
125 | + $query=$db->select() |
|
126 | + ->from($this->getModuleConfig()->getDbConfigTableName(), 'value') |
|
127 | 127 | ->where('name=\'db_version\''); |
128 | 128 | $version=$db->fetchRow($query); |
129 | - if ( ($version == null) || ! property_exists($version,'value') ) |
|
129 | + if (($version == null) || !property_exists($version, 'value')) |
|
130 | 130 | { |
131 | - if ($test) return array(4,$DBname); |
|
131 | + if ($test) return array(4, $DBname); |
|
132 | 132 | $this->redirectNow('trapdirector/settings?dberror=4'); |
133 | 133 | return null; |
134 | 134 | } |
135 | 135 | if ($version->value < $this->getModuleConfig()->getDbMinVersion()) |
136 | 136 | { |
137 | - if ($test) return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion()); |
|
137 | + if ($test) return array(5, $version->value, $this->getModuleConfig()->getDbMinVersion()); |
|
138 | 138 | $this->redirectNow('trapdirector/settings?dberror=5'); |
139 | 139 | return null; |
140 | 140 | } |
141 | 141 | } |
142 | 142 | catch (Exception $e) |
143 | 143 | { |
144 | - if ($test) return array(3,$DBname,$e->getMessage()); |
|
144 | + if ($test) return array(3, $DBname, $e->getMessage()); |
|
145 | 145 | $this->redirectNow('trapdirector/settings?dberror=4'); |
146 | 146 | return null; |
147 | 147 | } |
148 | 148 | } |
149 | - if ($test) return array(0,''); |
|
149 | + if ($test) return array(0, ''); |
|
150 | 150 | return $dbconn; |
151 | 151 | } |
152 | 152 | |
@@ -157,17 +157,17 @@ discard block |
||
157 | 157 | */ |
158 | 158 | public function getDb($test=false) |
159 | 159 | { |
160 | - if ($this->trapDB != null && $test = false) return $this->trapDB; |
|
160 | + if ($this->trapDB != null && $test=false) return $this->trapDB; |
|
161 | 161 | |
162 | 162 | $dbresource=$this->Config()->get('config', 'database'); |
163 | 163 | |
164 | - if ( ! $dbresource ) |
|
164 | + if (!$dbresource) |
|
165 | 165 | { |
166 | - if ($test) return array(1,''); |
|
166 | + if ($test) return array(1, ''); |
|
167 | 167 | $this->redirectNow('trapdirector/settings?dberror=1'); |
168 | 168 | return null; |
169 | 169 | } |
170 | - $retDB=$this->getDbByName($dbresource,$test,true); |
|
170 | + $retDB=$this->getDbByName($dbresource, $test, true); |
|
171 | 171 | if ($test === true) return $retDB; |
172 | 172 | $this->trapDB=$retDB; |
173 | 173 | return $this->trapDB; |
@@ -175,78 +175,78 @@ discard block |
||
175 | 175 | |
176 | 176 | public function getIdoDb($test=false) |
177 | 177 | { |
178 | - if ($this->icingaDB != null && $test = false) return $this->icingaDB; |
|
178 | + if ($this->icingaDB != null && $test=false) return $this->icingaDB; |
|
179 | 179 | // TODO : get ido database directly from icingaweb2 config -> (or not if using only API) |
180 | - $dbresource=$this->Config()->get('config', 'IDOdatabase');; |
|
180 | + $dbresource=$this->Config()->get('config', 'IDOdatabase'); ; |
|
181 | 181 | |
182 | - if ( ! $dbresource ) |
|
182 | + if (!$dbresource) |
|
183 | 183 | { |
184 | - if ($test) return array(1,'No database in config.ini'); |
|
184 | + if ($test) return array(1, 'No database in config.ini'); |
|
185 | 185 | $this->redirectNow('trapdirector/settings?idodberror=1'); |
186 | 186 | return null; |
187 | 187 | } |
188 | 188 | |
189 | 189 | try |
190 | 190 | { |
191 | - $dbconn = IcingaDbConnection::fromResourceName($dbresource); |
|
191 | + $dbconn=IcingaDbConnection::fromResourceName($dbresource); |
|
192 | 192 | } |
193 | 193 | catch (Exception $e) |
194 | 194 | { |
195 | - if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2"); |
|
195 | + if ($test) return array(2, "Database $dbresource does not exists in IcingaWeb2"); |
|
196 | 196 | $this->redirectNow('trapdirector/settings?idodberror=2'); |
197 | 197 | return null; |
198 | 198 | } |
199 | 199 | |
200 | 200 | if ($test == false) |
201 | 201 | { |
202 | - $this->icingaDB = $dbconn; |
|
202 | + $this->icingaDB=$dbconn; |
|
203 | 203 | return $this->icingaDB; |
204 | 204 | } |
205 | 205 | |
206 | 206 | try |
207 | 207 | { |
208 | - $query = $dbconn->select() |
|
209 | - ->from('icinga_dbversion',array('version')); |
|
208 | + $query=$dbconn->select() |
|
209 | + ->from('icinga_dbversion', array('version')); |
|
210 | 210 | $version=$dbconn->fetchRow($query); |
211 | - if ( ($version == null) || ! property_exists($version,'version') ) |
|
211 | + if (($version == null) || !property_exists($version, 'version')) |
|
212 | 212 | { |
213 | - return array(4,"$dbresource does not look like an IDO database"); |
|
213 | + return array(4, "$dbresource does not look like an IDO database"); |
|
214 | 214 | } |
215 | 215 | } |
216 | 216 | catch (Exception $e) |
217 | 217 | { |
218 | - return array(3,"Error connecting to $dbresource : " . $e->getMessage()); |
|
218 | + return array(3, "Error connecting to $dbresource : ".$e->getMessage()); |
|
219 | 219 | } |
220 | 220 | |
221 | - return array(0,''); |
|
221 | + return array(0, ''); |
|
222 | 222 | } |
223 | 223 | |
224 | - protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
|
224 | + protected function applyPaginationLimits(Paginatable $paginatable, $limit=25, $offset=null) |
|
225 | 225 | { |
226 | - $limit = $this->params->get('limit', $limit); |
|
227 | - $page = $this->params->get('page', $offset); |
|
226 | + $limit=$this->params->get('limit', $limit); |
|
227 | + $page=$this->params->get('page', $offset); |
|
228 | 228 | |
229 | 229 | $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
230 | 230 | |
231 | 231 | return $paginatable; |
232 | 232 | } |
233 | 233 | |
234 | - public function displayExitError($source,$message) |
|
234 | + public function displayExitError($source, $message) |
|
235 | 235 | { // TODO : check better ways to transmit data (with POST ?) |
236 | 236 | $this->redirectNow('trapdirector/error?source='.$source.'&message='.$message); |
237 | 237 | } |
238 | 238 | |
239 | 239 | protected function checkReadPermission() |
240 | 240 | { |
241 | - if (! $this->Auth()->hasPermission('trapdirector/view')) { |
|
242 | - $this->displayExitError('Permissions','No permission fo view content'); |
|
241 | + if (!$this->Auth()->hasPermission('trapdirector/view')) { |
|
242 | + $this->displayExitError('Permissions', 'No permission fo view content'); |
|
243 | 243 | } |
244 | 244 | } |
245 | 245 | |
246 | 246 | protected function checkConfigPermission() |
247 | 247 | { |
248 | - if (! $this->Auth()->hasPermission('trapdirector/config')) { |
|
249 | - $this->displayExitError('Permissions','No permission fo configure'); |
|
248 | + if (!$this->Auth()->hasPermission('trapdirector/config')) { |
|
249 | + $this->displayExitError('Permissions', 'No permission fo configure'); |
|
250 | 250 | } |
251 | 251 | } |
252 | 252 | |
@@ -257,10 +257,10 @@ discard block |
||
257 | 257 | */ |
258 | 258 | protected function checkModuleConfigPermission($check=0) |
259 | 259 | { |
260 | - if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
|
260 | + if (!$this->Auth()->hasPermission('trapdirector/module_config')) { |
|
261 | 261 | if ($check == 0) |
262 | 262 | { |
263 | - $this->displayExitError('Permissions','No permission fo configure module'); |
|
263 | + $this->displayExitError('Permissions', 'No permission fo configure module'); |
|
264 | 264 | } |
265 | 265 | return false; |
266 | 266 | } |
@@ -272,10 +272,10 @@ discard block |
||
272 | 272 | { // TODO : try/catch here ? or within caller |
273 | 273 | if ($this->trapClass == null) |
274 | 274 | { |
275 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
275 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
276 | 276 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
277 | 277 | //$debug_level=4; |
278 | - $this->trapClass = new Trap($icingaweb2_etc); |
|
278 | + $this->trapClass=new Trap($icingaweb2_etc); |
|
279 | 279 | //$Trap->setLogging($debug_level,'syslog'); |
280 | 280 | } |
281 | 281 | return $this->trapClass; |
@@ -309,12 +309,12 @@ discard block |
||
309 | 309 | protected function getHostByIP($ip) |
310 | 310 | { |
311 | 311 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
312 | - $db = $this->getIdoDb()->getConnection(); |
|
312 | + $db=$this->getIdoDb()->getConnection(); |
|
313 | 313 | // TODO : check for SQL injections |
314 | 314 | $query=$db->select() |
315 | 315 | ->from( |
316 | 316 | array('a' => 'icinga_objects'), |
317 | - array('name' => 'a.name1','id' => 'object_id')) |
|
317 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
318 | 318 | ->join( |
319 | 319 | array('b' => 'icinga_hosts'), |
320 | 320 | 'b.host_object_id=a.object_id', |
@@ -330,12 +330,12 @@ discard block |
||
330 | 330 | protected function getHostByName($name) |
331 | 331 | { |
332 | 332 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
333 | - $db = $this->getIdoDb()->getConnection(); |
|
333 | + $db=$this->getIdoDb()->getConnection(); |
|
334 | 334 | // TODO : check for SQL injections |
335 | 335 | $query=$db->select() |
336 | 336 | ->from( |
337 | 337 | array('a' => 'icinga_objects'), |
338 | - array('name' => 'a.name1','id' => 'object_id')) |
|
338 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
339 | 339 | ->join( |
340 | 340 | array('b' => 'icinga_hosts'), |
341 | 341 | 'b.host_object_id=a.object_id', |
@@ -351,12 +351,12 @@ discard block |
||
351 | 351 | protected function getHostGroupByName($ip) |
352 | 352 | { |
353 | 353 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
354 | - $db = $this->getIdoDb()->getConnection(); |
|
354 | + $db=$this->getIdoDb()->getConnection(); |
|
355 | 355 | // TODO : check for SQL injections |
356 | 356 | $query=$db->select() |
357 | 357 | ->from( |
358 | 358 | array('a' => 'icinga_objects'), |
359 | - array('name' => 'a.name1','id' => 'object_id')) |
|
359 | + array('name' => 'a.name1', 'id' => 'object_id')) |
|
360 | 360 | ->join( |
361 | 361 | array('b' => 'icinga_hostgroups'), |
362 | 362 | 'b.hostgroup_object_id=a.object_id', |
@@ -372,8 +372,8 @@ discard block |
||
372 | 372 | */ |
373 | 373 | protected function getHostInfoByID($id) |
374 | 374 | { |
375 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
376 | - $db = $this->getIdoDb()->getConnection(); |
|
375 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
376 | + $db=$this->getIdoDb()->getConnection(); |
|
377 | 377 | $query=$db->select() |
378 | 378 | ->from( |
379 | 379 | array('a' => 'icinga_objects'), |
@@ -393,17 +393,17 @@ discard block |
||
393 | 393 | */ |
394 | 394 | protected function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
395 | 395 | { |
396 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
397 | - $db = $this->getIdoDb()->getConnection(); |
|
396 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
397 | + $db=$this->getIdoDb()->getConnection(); |
|
398 | 398 | $query=$db->select() |
399 | 399 | ->from( |
400 | 400 | array('a' => 'icinga_objects'), |
401 | - array('name' => 'a.name1','id' => 'a.object_id')) |
|
401 | + array('name' => 'a.name1', 'id' => 'a.object_id')) |
|
402 | 402 | ->join( |
403 | 403 | array('b' => 'icinga_hosts'), |
404 | 404 | 'b.host_object_id=a.object_id', |
405 | - array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
406 | - ->where('a.object_id = ?',$id); |
|
405 | + array('display_name' => 'b.display_name', 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
406 | + ->where('a.object_id = ?', $id); |
|
407 | 407 | return $db->fetchRow($query); |
408 | 408 | } |
409 | 409 | |
@@ -415,16 +415,16 @@ discard block |
||
415 | 415 | protected function getServicesByHostid($id) |
416 | 416 | { |
417 | 417 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
418 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
419 | - $db = $this->getIdoDb()->getConnection(); |
|
418 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
419 | + $db=$this->getIdoDb()->getConnection(); |
|
420 | 420 | $query=$db->select() |
421 | 421 | ->from( |
422 | 422 | array('s' => 'icinga_services'), |
423 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
423 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
424 | 424 | ->join( |
425 | 425 | array('a' => 'icinga_objects'), |
426 | 426 | 's.service_object_id=a.object_id', |
427 | - array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
427 | + array('is_active'=>'a.is_active', 'name2'=>'a.name2')) |
|
428 | 428 | ->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
429 | 429 | return $db->fetchAll($query); |
430 | 430 | } |
@@ -437,8 +437,8 @@ discard block |
||
437 | 437 | */ |
438 | 438 | protected function getServicesByHostGroupid($id) |
439 | 439 | { |
440 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
441 | - $db = $this->getIdoDb()->getConnection(); |
|
440 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
441 | + $db=$this->getIdoDb()->getConnection(); |
|
442 | 442 | $query=$db->select() |
443 | 443 | ->from( |
444 | 444 | array('s' => 'icinga_hostgroup_members'), |
@@ -454,11 +454,11 @@ discard block |
||
454 | 454 | foreach ($hosts as $key => $host) |
455 | 455 | { // For each host, get all services and add in common_services if not found or add counter |
456 | 456 | $host_services=$this->getServicesByHostid($host->host_object_id); |
457 | - foreach($host_services as $service) |
|
457 | + foreach ($host_services as $service) |
|
458 | 458 | { |
459 | 459 | if (isset($common_services[$service->name2]['num'])) |
460 | 460 | { |
461 | - $common_services[$service->name2]['num'] +=1; |
|
461 | + $common_services[$service->name2]['num']+=1; |
|
462 | 462 | } |
463 | 463 | else |
464 | 464 | { |
@@ -474,7 +474,7 @@ discard block |
||
474 | 474 | { |
475 | 475 | if ($common_services[$key]['num'] == $num_hosts) |
476 | 476 | { |
477 | - array_push($result,array($key,$common_services[$key]['name'])); |
|
477 | + array_push($result, array($key, $common_services[$key]['name'])); |
|
478 | 478 | } |
479 | 479 | } |
480 | 480 | |
@@ -487,9 +487,9 @@ discard block |
||
487 | 487 | * @param $name string service name |
488 | 488 | * @return array service id |
489 | 489 | */ |
490 | - protected function getServiceIDByName($hostname,$name) |
|
490 | + protected function getServiceIDByName($hostname, $name) |
|
491 | 491 | { |
492 | - $db = $this->getIdoDb()->getConnection(); |
|
492 | + $db=$this->getIdoDb()->getConnection(); |
|
493 | 493 | if ($name == null) |
494 | 494 | { |
495 | 495 | return array(); |
@@ -498,7 +498,7 @@ discard block |
||
498 | 498 | $query=$db->select() |
499 | 499 | ->from( |
500 | 500 | array('s' => 'icinga_services'), |
501 | - array('name' => 's.display_name','id' => 's.service_object_id')) |
|
501 | + array('name' => 's.display_name', 'id' => 's.service_object_id')) |
|
502 | 502 | ->join( |
503 | 503 | array('a' => 'icinga_objects'), |
504 | 504 | 's.service_object_id=a.object_id', |
@@ -516,12 +516,12 @@ discard block |
||
516 | 516 | protected function getObjectNameByid($id) |
517 | 517 | { |
518 | 518 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
519 | - if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
520 | - $db = $this->getIdoDb()->getConnection(); |
|
519 | + if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); } |
|
520 | + $db=$this->getIdoDb()->getConnection(); |
|
521 | 521 | $query=$db->select() |
522 | 522 | ->from( |
523 | 523 | array('a' => 'icinga_objects'), |
524 | - array('name1' => 'a.name1','name2' => 'a.name2')) |
|
524 | + array('name1' => 'a.name1', 'name2' => 'a.name2')) |
|
525 | 525 | ->where('a.object_id='.$id.' AND a.is_active = 1'); |
526 | 526 | |
527 | 527 | return $db->fetchRow($query); |
@@ -534,17 +534,17 @@ discard block |
||
534 | 534 | protected function addHandlerRule($params) |
535 | 535 | { |
536 | 536 | // TODO Check for rule consistency |
537 | - $db = $this->getDb()->getConnection(); |
|
537 | + $db=$this->getDb()->getConnection(); |
|
538 | 538 | // Add last modified date = creation date and username |
539 | - $params['created'] = new Zend_Db_Expr('NOW()'); |
|
540 | - $params['modified'] = new Zend_Db_Expr('NOW()'); |
|
541 | - $params['modifier'] = $this->Auth()->getUser()->getUsername(); |
|
539 | + $params['created']=new Zend_Db_Expr('NOW()'); |
|
540 | + $params['modified']=new Zend_Db_Expr('NOW()'); |
|
541 | + $params['modifier']=$this->Auth()->getUser()->getUsername(); |
|
542 | 542 | |
543 | 543 | $query=$db->insert( |
544 | 544 | $this->getModuleConfig()->getTrapRuleName(), |
545 | 545 | $params |
546 | 546 | ); |
547 | - if($query==false) |
|
547 | + if ($query == false) |
|
548 | 548 | { |
549 | 549 | return null; |
550 | 550 | } |
@@ -556,13 +556,13 @@ discard block |
||
556 | 556 | * @param integer $ruleID : rule id in db |
557 | 557 | * @return array affected rows |
558 | 558 | */ |
559 | - protected function updateHandlerRule($params,$ruleID) |
|
559 | + protected function updateHandlerRule($params, $ruleID) |
|
560 | 560 | { |
561 | 561 | // TODO Check for rule consistency |
562 | - $db = $this->getDb()->getConnection(); |
|
562 | + $db=$this->getDb()->getConnection(); |
|
563 | 563 | // Add last modified date = creation date and username |
564 | - $params['modified'] = new Zend_Db_Expr('NOW()'); |
|
565 | - $params['modifier'] = $this->Auth()->getUser()->getUsername(); |
|
564 | + $params['modified']=new Zend_Db_Expr('NOW()'); |
|
565 | + $params['modifier']=$this->Auth()->getUser()->getUsername(); |
|
566 | 566 | |
567 | 567 | $numRows=$db->update( |
568 | 568 | $this->getModuleConfig()->getTrapRuleName(), |
@@ -577,8 +577,8 @@ discard block |
||
577 | 577 | */ |
578 | 578 | protected function deleteRule($ruleID) |
579 | 579 | { |
580 | - if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id'); } |
|
581 | - $db = $this->getDb()->getConnection(); |
|
580 | + if (!preg_match('/^[0-9]+$/', $ruleID)) { throw new Exception('Invalid id'); } |
|
581 | + $db=$this->getDb()->getConnection(); |
|
582 | 582 | |
583 | 583 | $query=$db->delete( |
584 | 584 | $this->getModuleConfig()->getTrapRuleName(), |
@@ -591,10 +591,10 @@ discard block |
||
591 | 591 | * @param $ip string source IP (v4 or v6) |
592 | 592 | * @param $oid string oid |
593 | 593 | */ |
594 | - protected function deleteTrap($ip,$oid) |
|
594 | + protected function deleteTrap($ip, $oid) |
|
595 | 595 | { |
596 | 596 | |
597 | - $db = $this->getDb()->getConnection(); |
|
597 | + $db=$this->getDb()->getConnection(); |
|
598 | 598 | $condition=null; |
599 | 599 | if ($ip != null) |
600 | 600 | { |
@@ -602,10 +602,10 @@ discard block |
||
602 | 602 | } |
603 | 603 | if ($oid != null) |
604 | 604 | { |
605 | - $condition=($condition===null)?'':$condition.' AND '; |
|
605 | + $condition=($condition === null) ? '' : $condition.' AND '; |
|
606 | 606 | $condition.="trap_oid='$oid'"; |
607 | 607 | } |
608 | - if($condition === null) return null; |
|
608 | + if ($condition === null) return null; |
|
609 | 609 | $query=$db->delete( |
610 | 610 | $this->getModuleConfig()->getTrapTableName(), |
611 | 611 | $condition |
@@ -619,10 +619,10 @@ discard block |
||
619 | 619 | * @param $ip string source IP (v4 or v6) |
620 | 620 | * @param $oid string oid |
621 | 621 | */ |
622 | - protected function countTrap($ip,$oid) |
|
622 | + protected function countTrap($ip, $oid) |
|
623 | 623 | { |
624 | 624 | |
625 | - $db = $this->getDb()->getConnection(); |
|
625 | + $db=$this->getDb()->getConnection(); |
|
626 | 626 | $condition=null; |
627 | 627 | if ($ip != null) |
628 | 628 | { |
@@ -630,10 +630,10 @@ discard block |
||
630 | 630 | } |
631 | 631 | if ($oid != null) |
632 | 632 | { |
633 | - $condition=($condition===null)?'':$condition.' AND '; |
|
633 | + $condition=($condition === null) ? '' : $condition.' AND '; |
|
634 | 634 | $condition.="trap_oid='$oid'"; |
635 | 635 | } |
636 | - if($condition === null) return 0; |
|
636 | + if ($condition === null) return 0; |
|
637 | 637 | $query=$db->select() |
638 | 638 | ->from( |
639 | 639 | $this->getModuleConfig()->getTrapTableName(), |
@@ -649,27 +649,27 @@ discard block |
||
649 | 649 | protected function getDBConfigValue($element) |
650 | 650 | { |
651 | 651 | |
652 | - $db = $this->getDb()->getConnection(); |
|
652 | + $db=$this->getDb()->getConnection(); |
|
653 | 653 | |
654 | 654 | $query=$db->select() |
655 | 655 | ->from( |
656 | 656 | $this->getModuleConfig()->getDbConfigTableName(), |
657 | 657 | array('value'=>'value')) |
658 | - ->where('name=?',$element); |
|
658 | + ->where('name=?', $element); |
|
659 | 659 | $return_row=$db->fetchRow($query); |
660 | - if ($return_row==null) // value does not exists |
|
660 | + if ($return_row == null) // value does not exists |
|
661 | 661 | { |
662 | 662 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
663 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
663 | + if (!isset($default[$element])) return null; // no default and not value |
|
664 | 664 | |
665 | - $this->addDBConfigValue($element,$default[$element]); |
|
665 | + $this->addDBConfigValue($element, $default[$element]); |
|
666 | 666 | return $default[$element]; |
667 | 667 | } |
668 | 668 | if ($return_row->value == null) // value id empty |
669 | 669 | { |
670 | 670 | $default=$this->getModuleConfig()->getDBConfigDefaults(); |
671 | - if ( ! isset($default[$element])) return null; // no default and not value |
|
672 | - $this->setDBConfigValue($element,$default[$element]); |
|
671 | + if (!isset($default[$element])) return null; // no default and not value |
|
672 | + $this->setDBConfigValue($element, $default[$element]); |
|
673 | 673 | return $default[$element]; |
674 | 674 | } |
675 | 675 | return $return_row->value; |
@@ -680,10 +680,10 @@ discard block |
||
680 | 680 | * @param string $value : value |
681 | 681 | */ |
682 | 682 | |
683 | - protected function addDBConfigValue($element,$value) |
|
683 | + protected function addDBConfigValue($element, $value) |
|
684 | 684 | { |
685 | 685 | |
686 | - $db = $this->getDb()->getConnection(); |
|
686 | + $db=$this->getDb()->getConnection(); |
|
687 | 687 | |
688 | 688 | $query=$db->insert( |
689 | 689 | $this->getModuleConfig()->getDbConfigTableName(), |
@@ -699,10 +699,10 @@ discard block |
||
699 | 699 | * @param string $element : name of config element |
700 | 700 | * @param string $value : value |
701 | 701 | */ |
702 | - protected function setDBConfigValue($element,$value) |
|
702 | + protected function setDBConfigValue($element, $value) |
|
703 | 703 | { |
704 | 704 | |
705 | - $db = $this->getDb()->getConnection(); |
|
705 | + $db=$this->getDb()->getConnection(); |
|
706 | 706 | $query=$db->update( |
707 | 707 | $this->getModuleConfig()->getDbConfigTableName(), |
708 | 708 | array('value'=>$value), |
@@ -717,10 +717,10 @@ discard block |
||
717 | 717 | protected function isDirectorInstalled() |
718 | 718 | { |
719 | 719 | $output=array(); |
720 | - exec('icingacli module list',$output); |
|
720 | + exec('icingacli module list', $output); |
|
721 | 721 | foreach ($output as $line) |
722 | 722 | { |
723 | - if (preg_match('/^director .*enabled/',$line)) |
|
723 | + if (preg_match('/^director .*enabled/', $line)) |
|
724 | 724 | { |
725 | 725 | return true; |
726 | 726 | } |
@@ -51,8 +51,7 @@ discard block |
||
51 | 51 | if ($this->params->get('rule') !== null) |
52 | 52 | { |
53 | 53 | $this->view->rule= $this->params->get('rule'); |
54 | - } |
|
55 | - else |
|
54 | + } else |
|
56 | 55 | { |
57 | 56 | $this->view->rule=''; |
58 | 57 | } |
@@ -106,8 +105,7 @@ discard block |
||
106 | 105 | try |
107 | 106 | { |
108 | 107 | $hosts=$this->getHostByIP($hostfilter); |
109 | - } |
|
110 | - catch (Exception $e) |
|
108 | + } catch (Exception $e) |
|
111 | 109 | { |
112 | 110 | $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
113 | 111 | } |
@@ -120,8 +118,7 @@ discard block |
||
120 | 118 | // Tell JS to get services when page is loaded |
121 | 119 | $this->view->serviceGet=true; |
122 | 120 | |
123 | - } |
|
124 | - else |
|
121 | + } else |
|
125 | 122 | { |
126 | 123 | foreach($hosts as $key=>$val) |
127 | 124 | { |
@@ -171,11 +168,14 @@ discard block |
||
171 | 168 | $allObjects[$val->oid]=null; |
172 | 169 | } |
173 | 170 | } |
174 | - if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
171 | + if ($allObjects!=null) { |
|
172 | + // in case trap doesn't have objects or is not resolved |
|
175 | 173 | { |
176 | 174 | foreach ($allObjects as $key => $val) |
177 | 175 | { |
178 | - if ($val==null) { continue; } |
|
176 | + if ($val==null) { continue; |
|
177 | + } |
|
178 | + } |
|
179 | 179 | array_push($this->view->objectList, array( |
180 | 180 | $oid_index, |
181 | 181 | $key, |
@@ -207,8 +207,7 @@ discard block |
||
207 | 207 | { |
208 | 208 | $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
209 | 209 | $this->view->serviceGet=false; |
210 | - } |
|
211 | - else |
|
210 | + } else |
|
212 | 211 | { |
213 | 212 | // Tell JS to get services when page is loaded |
214 | 213 | $this->view->serviceGet=true; |
@@ -217,8 +216,7 @@ discard block |
||
217 | 216 | if (count($serviceID) ==0) |
218 | 217 | { |
219 | 218 | $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
220 | - } |
|
221 | - else |
|
219 | + } else |
|
222 | 220 | { |
223 | 221 | $this->view->serviceSet=$serviceID[0]->id; |
224 | 222 | } |
@@ -237,8 +235,7 @@ discard block |
||
237 | 235 | { |
238 | 236 | $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
239 | 237 | $this->view->serviceGroupGet=false; |
240 | - } |
|
241 | - else |
|
238 | + } else |
|
242 | 239 | { |
243 | 240 | $grpServices=$this->getServicesByHostGroupid($group_get[0]->id); |
244 | 241 | $foundGrpService=0; |
@@ -288,8 +285,7 @@ discard block |
||
288 | 285 | $object['type'], |
289 | 286 | $object['type_enum'] |
290 | 287 | )); |
291 | - } |
|
292 | - else |
|
288 | + } else |
|
293 | 289 | { |
294 | 290 | array_push($curObjectList, array( |
295 | 291 | $index, |
@@ -358,8 +354,7 @@ discard block |
||
358 | 354 | $this->view->selectGroup=false; |
359 | 355 | // Check if hostname still exists |
360 | 356 | $this->add_check_host_exists($ruleDetail); |
361 | - } |
|
362 | - else |
|
357 | + } else |
|
363 | 358 | { |
364 | 359 | $this->view->selectGroup=true; |
365 | 360 | $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists |
@@ -367,9 +362,11 @@ discard block |
||
367 | 362 | |
368 | 363 | $this->view->mainoid=$ruleDetail->trap_oid; |
369 | 364 | $oidName=$this->getMIB()->translateOID($ruleDetail->trap_oid); |
370 | - if ($oidName != null) // oid is found in mibs |
|
365 | + if ($oidName != null) { |
|
366 | + // oid is found in mibs |
|
371 | 367 | { |
372 | - $this->view->mib=$oidName['mib']; |
|
368 | + $this->view->mib=$oidName['mib']; |
|
369 | + } |
|
373 | 370 | $this->view->name=$oidName['name']; |
374 | 371 | $this->view->trapListForMIB=$this->getMIB() |
375 | 372 | ->getTrapList($oidName['mib']); |
@@ -424,8 +421,7 @@ discard block |
||
424 | 421 | try |
425 | 422 | { |
426 | 423 | $this->deleteRule($postData[$params['db_rule']['post']]); |
427 | - } |
|
428 | - catch (Exception $e) |
|
424 | + } catch (Exception $e) |
|
429 | 425 | { |
430 | 426 | $this->_helper->json(array('status'=>$e->getMessage())); |
431 | 427 | return; |
@@ -439,13 +435,15 @@ discard block |
||
439 | 435 | } |
440 | 436 | foreach (array_keys($params) as $key) |
441 | 437 | { |
442 | - if ($params[$key]['post']==null) continue; // data not sent in post vars |
|
438 | + if ($params[$key]['post']==null) { |
|
439 | + continue; |
|
440 | + } |
|
441 | + // data not sent in post vars |
|
443 | 442 | if (! isset($postData[$params[$key]['post']])) |
444 | 443 | { |
445 | 444 | // should not happen as the js checks data |
446 | 445 | $this->_helper->json(array('status'=>'No ' . $key)); |
447 | - } |
|
448 | - else |
|
446 | + } else |
|
449 | 447 | { |
450 | 448 | $data=$postData[$params[$key]['post']]; |
451 | 449 | if ($data!=null && $data !="") |
@@ -475,8 +473,7 @@ discard block |
||
475 | 473 | $this->_helper->json(array('status'=>"Invalid service id : Please re enter service")); |
476 | 474 | return; |
477 | 475 | } |
478 | - } |
|
479 | - else |
|
476 | + } else |
|
480 | 477 | { |
481 | 478 | $object=$this->getObjectNameByid($params['hostid']['val']); |
482 | 479 | if ($params['host_name']['val'] != $object->name1) |
@@ -500,14 +497,12 @@ discard block |
||
500 | 497 | if ($params['db_rule']['val'] == -1 ) |
501 | 498 | { |
502 | 499 | $ruleID=$this->addHandlerRule($dbparams); |
503 | - } |
|
504 | - else |
|
500 | + } else |
|
505 | 501 | { |
506 | 502 | $this->updateHandlerRule($dbparams,$params['db_rule']['val']); |
507 | 503 | $ruleID=$params['db_rule']['val']; |
508 | 504 | } |
509 | - } |
|
510 | - catch (Exception $e) |
|
505 | + } catch (Exception $e) |
|
511 | 506 | { |
512 | 507 | $this->_helper->json(array('status'=>$e->getMessage())); |
513 | 508 | return; |
@@ -543,8 +538,7 @@ discard block |
||
543 | 538 | $trapDetail = 'NULL'; |
544 | 539 | throw new Exception('No traps was found with id = '.$trapid); |
545 | 540 | } |
546 | - } |
|
547 | - catch (Exception $e) |
|
541 | + } catch (Exception $e) |
|
548 | 542 | { |
549 | 543 | $this->displayExitError('Add handler : get trap detail returning : '.print_r($trapDetail,true),$e->getMessage()); |
550 | 544 | return; |
@@ -577,8 +571,7 @@ discard block |
||
577 | 571 | ->where('trap_id=?',$trapid); |
578 | 572 | $trapDetail=$db->fetchAll($query); |
579 | 573 | // if ( $trapDetail == null ) throw new Exception('No traps was found with id = '.$trapid); |
580 | - } |
|
581 | - catch (Exception $e) |
|
574 | + } catch (Exception $e) |
|
582 | 575 | { |
583 | 576 | $this->displayExitError('Add handler : get trap data detail : ',$e->getMessage()); |
584 | 577 | return array(); |
@@ -604,9 +597,10 @@ discard block |
||
604 | 597 | ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
605 | 598 | ->where('id=?',$ruleid); |
606 | 599 | $ruleDetail=$db->fetchRow($query); |
607 | - if ( $ruleDetail == null ) throw new Exception('No rule was found with id = '.$ruleid); |
|
608 | - } |
|
609 | - catch (Exception $e) |
|
600 | + if ( $ruleDetail == null ) { |
|
601 | + throw new Exception('No rule was found with id = '.$ruleid); |
|
602 | + } |
|
603 | + } catch (Exception $e) |
|
610 | 604 | { |
611 | 605 | $this->displayExitError('Update handler : get rule detail',$e->getMessage()); |
612 | 606 | return array(); |
@@ -11,13 +11,12 @@ discard block |
||
11 | 11 | |
12 | 12 | //use Icinga\Web\Form as Form; |
13 | 13 | /** Rules management |
14 | - |
|
15 | -*/ |
|
14 | + */ |
|
16 | 15 | class HandlerController extends TrapsController |
17 | 16 | { |
18 | 17 | |
19 | 18 | /** index : list existing rules |
20 | - */ |
|
19 | + */ |
|
21 | 20 | public function indexAction() |
22 | 21 | { |
23 | 22 | $this->checkReadPermission(); |
@@ -39,23 +38,23 @@ discard block |
||
39 | 38 | */ |
40 | 39 | public function testruleAction() |
41 | 40 | { |
42 | - $this->checkReadPermission(); |
|
43 | - $this->getTabs()->add('get',array( |
|
44 | - 'active' => true, |
|
45 | - 'label' => $this->translate('Test Rule'), |
|
46 | - 'url' => Url::fromRequest() |
|
47 | - )); |
|
41 | + $this->checkReadPermission(); |
|
42 | + $this->getTabs()->add('get',array( |
|
43 | + 'active' => true, |
|
44 | + 'label' => $this->translate('Test Rule'), |
|
45 | + 'url' => Url::fromRequest() |
|
46 | + )); |
|
48 | 47 | |
49 | - //$db = $this->getDb(); |
|
48 | + //$db = $this->getDb(); |
|
50 | 49 | |
51 | - if ($this->params->get('rule') !== null) |
|
52 | - { |
|
53 | - $this->view->rule= $this->params->get('rule'); |
|
54 | - } |
|
55 | - else |
|
56 | - { |
|
57 | - $this->view->rule=''; |
|
58 | - } |
|
50 | + if ($this->params->get('rule') !== null) |
|
51 | + { |
|
52 | + $this->view->rule= $this->params->get('rule'); |
|
53 | + } |
|
54 | + else |
|
55 | + { |
|
56 | + $this->view->rule=''; |
|
57 | + } |
|
59 | 58 | } |
60 | 59 | |
61 | 60 | /** |
@@ -63,31 +62,31 @@ discard block |
||
63 | 62 | */ |
64 | 63 | private function add_setup_vars() |
65 | 64 | { |
66 | - // variables to send to view |
|
67 | - $this->view->hostlist=array(); // host list to input datalist |
|
68 | - $this->view->hostname=''; // Host name in input text |
|
69 | - $this->view->serviceGet=false; // Set to true to get list of service if only one host set |
|
70 | - $this->view->serviceSet=null; // Select service in services select (must have serviceGet=true). |
|
71 | - $this->view->mainoid=''; // Trap OID |
|
72 | - $this->view->mib=''; // Trap mib |
|
73 | - $this->view->name=''; // Trap name |
|
74 | - $this->view->trapListForMIB=array(); // Trap list if mib exists for trap |
|
75 | - $this->view->objectList=array(); // objects sent with trap |
|
76 | - $this->view->display=''; // Initial display |
|
77 | - $this->view->rule=''; // rule display |
|
78 | - $this->view->revertOK=''; // revert OK in seconds |
|
79 | - $this->view->hostid=-1; // normally set by javascript serviceGet() |
|
80 | - $this->view->ruleid=-1; // Rule id in DB for update & delete |
|
81 | - $this->view->setToUpdate=false; // set form as update form |
|
82 | - $this->view->setRuleMatch=-1; // set action on rule match (default nothing) |
|
83 | - $this->view->setRuleNoMatch=-1; // set action on rule no match (default nothing) |
|
65 | + // variables to send to view |
|
66 | + $this->view->hostlist=array(); // host list to input datalist |
|
67 | + $this->view->hostname=''; // Host name in input text |
|
68 | + $this->view->serviceGet=false; // Set to true to get list of service if only one host set |
|
69 | + $this->view->serviceSet=null; // Select service in services select (must have serviceGet=true). |
|
70 | + $this->view->mainoid=''; // Trap OID |
|
71 | + $this->view->mib=''; // Trap mib |
|
72 | + $this->view->name=''; // Trap name |
|
73 | + $this->view->trapListForMIB=array(); // Trap list if mib exists for trap |
|
74 | + $this->view->objectList=array(); // objects sent with trap |
|
75 | + $this->view->display=''; // Initial display |
|
76 | + $this->view->rule=''; // rule display |
|
77 | + $this->view->revertOK=''; // revert OK in seconds |
|
78 | + $this->view->hostid=-1; // normally set by javascript serviceGet() |
|
79 | + $this->view->ruleid=-1; // Rule id in DB for update & delete |
|
80 | + $this->view->setToUpdate=false; // set form as update form |
|
81 | + $this->view->setRuleMatch=-1; // set action on rule match (default nothing) |
|
82 | + $this->view->setRuleNoMatch=-1; // set action on rule no match (default nothing) |
|
84 | 83 | |
85 | - $this->view->selectGroup=false; // Select by group if true |
|
86 | - $this->view->hostgroupid=-1; // host group id |
|
87 | - $this->view->serviceGroupGet=false; // Get list of service for group (set serviceSet to select one) |
|
84 | + $this->view->selectGroup=false; // Select by group if true |
|
85 | + $this->view->hostgroupid=-1; // host group id |
|
86 | + $this->view->serviceGroupGet=false; // Get list of service for group (set serviceSet to select one) |
|
88 | 87 | |
89 | - $this->view->modifier=null; |
|
90 | - $this->view->modified=null; |
|
88 | + $this->view->modifier=null; |
|
89 | + $this->view->modified=null; |
|
91 | 90 | } |
92 | 91 | |
93 | 92 | /** |
@@ -96,102 +95,102 @@ discard block |
||
96 | 95 | */ |
97 | 96 | private function add_from_existing($trapid) |
98 | 97 | { |
99 | - /********** Setup from existing trap ***************/ |
|
100 | - // Get the full trap info |
|
101 | - $trapDetail=$this->getTrapDetail($trapid); |
|
98 | + /********** Setup from existing trap ***************/ |
|
99 | + // Get the full trap info |
|
100 | + $trapDetail=$this->getTrapDetail($trapid); |
|
102 | 101 | |
103 | - $hostfilter=$trapDetail->source_ip; |
|
102 | + $hostfilter=$trapDetail->source_ip; |
|
104 | 103 | |
105 | - // Get host |
|
106 | - try |
|
107 | - { |
|
108 | - $hosts=$this->getHostByIP($hostfilter); |
|
109 | - } |
|
110 | - catch (Exception $e) |
|
111 | - { |
|
112 | - $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
|
113 | - } |
|
104 | + // Get host |
|
105 | + try |
|
106 | + { |
|
107 | + $hosts=$this->getHostByIP($hostfilter); |
|
108 | + } |
|
109 | + catch (Exception $e) |
|
110 | + { |
|
111 | + $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
|
112 | + } |
|
114 | 113 | |
115 | 114 | |
116 | - // if one unique host found -> put id text input |
|
117 | - if (count($hosts)==1) { |
|
118 | - $this->view->hostname=$hosts[0]->name; |
|
119 | - //$hostid=$hosts[0]->id; |
|
120 | - // Tell JS to get services when page is loaded |
|
121 | - $this->view->serviceGet=true; |
|
115 | + // if one unique host found -> put id text input |
|
116 | + if (count($hosts)==1) { |
|
117 | + $this->view->hostname=$hosts[0]->name; |
|
118 | + //$hostid=$hosts[0]->id; |
|
119 | + // Tell JS to get services when page is loaded |
|
120 | + $this->view->serviceGet=true; |
|
122 | 121 | |
123 | - } |
|
124 | - else |
|
125 | - { |
|
126 | - foreach($hosts as $key=>$val) |
|
127 | - { |
|
128 | - array_push($this->view->hostlist,$hosts[$key]->name); |
|
129 | - } |
|
130 | - } |
|
122 | + } |
|
123 | + else |
|
124 | + { |
|
125 | + foreach($hosts as $key=>$val) |
|
126 | + { |
|
127 | + array_push($this->view->hostlist,$hosts[$key]->name); |
|
128 | + } |
|
129 | + } |
|
131 | 130 | |
132 | - // set up trap oid and objects received by the trap |
|
131 | + // set up trap oid and objects received by the trap |
|
133 | 132 | |
134 | - $this->view->mainoid=$trapDetail->trap_oid; |
|
135 | - if ($trapDetail->trap_name_mib != null) |
|
136 | - { |
|
137 | - $this->view->mib=$trapDetail->trap_name_mib; |
|
138 | - $this->view->name=$trapDetail->trap_name; |
|
139 | - $this->view->trapListForMIB=$this->getMIB() |
|
140 | - ->getTrapList($trapDetail->trap_name_mib); |
|
141 | - } |
|
133 | + $this->view->mainoid=$trapDetail->trap_oid; |
|
134 | + if ($trapDetail->trap_name_mib != null) |
|
135 | + { |
|
136 | + $this->view->mib=$trapDetail->trap_name_mib; |
|
137 | + $this->view->name=$trapDetail->trap_name; |
|
138 | + $this->view->trapListForMIB=$this->getMIB() |
|
139 | + ->getTrapList($trapDetail->trap_name_mib); |
|
140 | + } |
|
142 | 141 | |
143 | - // Get all objects that can be in trap from MIB |
|
144 | - $allObjects=$this->getMIB()->getObjectList($trapDetail->trap_oid); |
|
145 | - // Get all objects in current Trap |
|
146 | - $currentTrapObjects=$this->getTrapobjects($trapid); |
|
147 | - $oid_index=1; |
|
148 | - foreach ($currentTrapObjects as $key => $val) |
|
149 | - { |
|
150 | - $currentObjectType='Unknown'; |
|
151 | - $currentObjectTypeEnum='Unknown'; |
|
152 | - if (isset($allObjects[$val->oid]['type'])) |
|
153 | - { |
|
154 | - $currentObjectType=$allObjects[$val->oid]['type']; |
|
155 | - $currentObjectTypeEnum=$allObjects[$val->oid]['type_enum']; |
|
156 | - } |
|
157 | - $currentObject=array( |
|
158 | - $oid_index, |
|
159 | - $val->oid, |
|
160 | - $val->oid_name_mib, |
|
161 | - $val->oid_name, |
|
162 | - $val->value, |
|
163 | - $currentObjectType, |
|
164 | - $currentObjectTypeEnum |
|
165 | - ); |
|
166 | - $oid_index++; |
|
167 | - array_push($this->view->objectList,$currentObject); |
|
168 | - // set currrent object to null in allObjects |
|
169 | - if (isset($allObjects[$val->oid])) |
|
170 | - { |
|
171 | - $allObjects[$val->oid]=null; |
|
172 | - } |
|
173 | - } |
|
174 | - if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
175 | - { |
|
176 | - foreach ($allObjects as $key => $val) |
|
177 | - { |
|
178 | - if ($val==null) { continue; } |
|
179 | - array_push($this->view->objectList, array( |
|
180 | - $oid_index, |
|
181 | - $key, |
|
182 | - $allObjects[$key]['mib'], |
|
183 | - $allObjects[$key]['name'], |
|
184 | - '', |
|
185 | - $allObjects[$key]['type'], |
|
186 | - $allObjects[$key]['type_enum'] |
|
187 | - )); |
|
188 | - $oid_index++; |
|
189 | - } |
|
190 | - } |
|
142 | + // Get all objects that can be in trap from MIB |
|
143 | + $allObjects=$this->getMIB()->getObjectList($trapDetail->trap_oid); |
|
144 | + // Get all objects in current Trap |
|
145 | + $currentTrapObjects=$this->getTrapobjects($trapid); |
|
146 | + $oid_index=1; |
|
147 | + foreach ($currentTrapObjects as $key => $val) |
|
148 | + { |
|
149 | + $currentObjectType='Unknown'; |
|
150 | + $currentObjectTypeEnum='Unknown'; |
|
151 | + if (isset($allObjects[$val->oid]['type'])) |
|
152 | + { |
|
153 | + $currentObjectType=$allObjects[$val->oid]['type']; |
|
154 | + $currentObjectTypeEnum=$allObjects[$val->oid]['type_enum']; |
|
155 | + } |
|
156 | + $currentObject=array( |
|
157 | + $oid_index, |
|
158 | + $val->oid, |
|
159 | + $val->oid_name_mib, |
|
160 | + $val->oid_name, |
|
161 | + $val->value, |
|
162 | + $currentObjectType, |
|
163 | + $currentObjectTypeEnum |
|
164 | + ); |
|
165 | + $oid_index++; |
|
166 | + array_push($this->view->objectList,$currentObject); |
|
167 | + // set currrent object to null in allObjects |
|
168 | + if (isset($allObjects[$val->oid])) |
|
169 | + { |
|
170 | + $allObjects[$val->oid]=null; |
|
171 | + } |
|
172 | + } |
|
173 | + if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
174 | + { |
|
175 | + foreach ($allObjects as $key => $val) |
|
176 | + { |
|
177 | + if ($val==null) { continue; } |
|
178 | + array_push($this->view->objectList, array( |
|
179 | + $oid_index, |
|
180 | + $key, |
|
181 | + $allObjects[$key]['mib'], |
|
182 | + $allObjects[$key]['name'], |
|
183 | + '', |
|
184 | + $allObjects[$key]['type'], |
|
185 | + $allObjects[$key]['type_enum'] |
|
186 | + )); |
|
187 | + $oid_index++; |
|
188 | + } |
|
189 | + } |
|
191 | 190 | |
192 | - // Add a simple display |
|
193 | - $this->view->display='Trap '.$trapDetail->trap_name.' received'; |
|
194 | - $this->view->create_basic_rule=true; |
|
191 | + // Add a simple display |
|
192 | + $this->view->display='Trap '.$trapDetail->trap_name.' received'; |
|
193 | + $this->view->create_basic_rule=true; |
|
195 | 194 | } |
196 | 195 | |
197 | 196 | /** |
@@ -200,29 +199,29 @@ discard block |
||
200 | 199 | */ |
201 | 200 | private function add_check_host_exists($ruleDetail) |
202 | 201 | { |
203 | - // Check if hostname still exists |
|
204 | - $host_get=$this->getHostByName($this->view->hostname); |
|
202 | + // Check if hostname still exists |
|
203 | + $host_get=$this->getHostByName($this->view->hostname); |
|
205 | 204 | |
206 | - if (count($host_get)==0) |
|
207 | - { |
|
208 | - $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
|
209 | - $this->view->serviceGet=false; |
|
210 | - } |
|
211 | - else |
|
212 | - { |
|
213 | - // Tell JS to get services when page is loaded |
|
214 | - $this->view->serviceGet=true; |
|
215 | - // get service id for form to set : |
|
216 | - $serviceID=$this->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
|
217 | - if (count($serviceID) ==0) |
|
218 | - { |
|
219 | - $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
220 | - } |
|
221 | - else |
|
222 | - { |
|
223 | - $this->view->serviceSet=$serviceID[0]->id; |
|
224 | - } |
|
225 | - } |
|
205 | + if (count($host_get)==0) |
|
206 | + { |
|
207 | + $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
|
208 | + $this->view->serviceGet=false; |
|
209 | + } |
|
210 | + else |
|
211 | + { |
|
212 | + // Tell JS to get services when page is loaded |
|
213 | + $this->view->serviceGet=true; |
|
214 | + // get service id for form to set : |
|
215 | + $serviceID=$this->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
|
216 | + if (count($serviceID) ==0) |
|
217 | + { |
|
218 | + $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
219 | + } |
|
220 | + else |
|
221 | + { |
|
222 | + $this->view->serviceSet=$serviceID[0]->id; |
|
223 | + } |
|
224 | + } |
|
226 | 225 | } |
227 | 226 | |
228 | 227 | /** |
@@ -231,33 +230,33 @@ discard block |
||
231 | 230 | */ |
232 | 231 | private function add_check_hostgroup_exists($ruleDetail) |
233 | 232 | { |
234 | - // Check if groupe exists |
|
235 | - $group_get=$this->getHostGroupByName($this->view->hostgroupname); |
|
236 | - if (count($group_get)==0) |
|
237 | - { |
|
238 | - $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
|
239 | - $this->view->serviceGroupGet=false; |
|
240 | - } |
|
241 | - else |
|
242 | - { |
|
243 | - $grpServices=$this->getServicesByHostGroupid($group_get[0]->id); |
|
244 | - $foundGrpService=0; |
|
245 | - foreach ($grpServices as $grpService) |
|
246 | - { |
|
247 | - if ($grpService[0] == $ruleDetail->service_name) |
|
248 | - { |
|
249 | - $foundGrpService=1; |
|
250 | - $this->view->serviceSet=$ruleDetail->service_name; |
|
251 | - } |
|
252 | - } |
|
233 | + // Check if groupe exists |
|
234 | + $group_get=$this->getHostGroupByName($this->view->hostgroupname); |
|
235 | + if (count($group_get)==0) |
|
236 | + { |
|
237 | + $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
|
238 | + $this->view->serviceGroupGet=false; |
|
239 | + } |
|
240 | + else |
|
241 | + { |
|
242 | + $grpServices=$this->getServicesByHostGroupid($group_get[0]->id); |
|
243 | + $foundGrpService=0; |
|
244 | + foreach ($grpServices as $grpService) |
|
245 | + { |
|
246 | + if ($grpService[0] == $ruleDetail->service_name) |
|
247 | + { |
|
248 | + $foundGrpService=1; |
|
249 | + $this->view->serviceSet=$ruleDetail->service_name; |
|
250 | + } |
|
251 | + } |
|
253 | 252 | |
254 | - // Tell JS to get services when page is loaded |
|
255 | - $this->view->serviceGroupGet=true; |
|
256 | - if ($foundGrpService==0) |
|
257 | - { |
|
258 | - $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
259 | - } |
|
260 | - } |
|
253 | + // Tell JS to get services when page is loaded |
|
254 | + $this->view->serviceGroupGet=true; |
|
255 | + if ($foundGrpService==0) |
|
256 | + { |
|
257 | + $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
258 | + } |
|
259 | + } |
|
261 | 260 | } |
262 | 261 | |
263 | 262 | /** |
@@ -269,52 +268,52 @@ discard block |
||
269 | 268 | */ |
270 | 269 | private function add_create_trap_object_list(&$display, &$rule) |
271 | 270 | { |
272 | - $curObjectList=array(); |
|
273 | - $index=1; |
|
274 | - // check in display & rule for : OID(<oid>) |
|
275 | - $matches=array(); |
|
276 | - while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
|
277 | - preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
|
278 | - { |
|
279 | - $curOid=$matches[1]; |
|
271 | + $curObjectList=array(); |
|
272 | + $index=1; |
|
273 | + // check in display & rule for : OID(<oid>) |
|
274 | + $matches=array(); |
|
275 | + while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
|
276 | + preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
|
277 | + { |
|
278 | + $curOid=$matches[1]; |
|
280 | 279 | |
281 | - if ( (preg_match('/\*/',$curOid) == 0 ) |
|
282 | - && ($object=$this->getMIB()->translateOID($curOid)) != null) |
|
283 | - { |
|
284 | - array_push($curObjectList, array( |
|
285 | - $index, |
|
286 | - $curOid, |
|
287 | - $object['mib'], |
|
288 | - $object['name'], |
|
289 | - '', |
|
290 | - $object['type'], |
|
291 | - $object['type_enum'] |
|
292 | - )); |
|
293 | - } |
|
294 | - else |
|
295 | - { |
|
296 | - array_push($curObjectList, array( |
|
297 | - $index, |
|
298 | - $curOid, |
|
299 | - 'not found', |
|
300 | - 'not found', |
|
301 | - '', |
|
302 | - 'not found', |
|
303 | - 'not found' |
|
304 | - )); |
|
305 | - } |
|
306 | - $curOid = preg_replace('/\*/','\*',$curOid); |
|
307 | - $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
|
308 | - $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
|
309 | - $index++; |
|
310 | - } |
|
311 | - return $curObjectList; |
|
280 | + if ( (preg_match('/\*/',$curOid) == 0 ) |
|
281 | + && ($object=$this->getMIB()->translateOID($curOid)) != null) |
|
282 | + { |
|
283 | + array_push($curObjectList, array( |
|
284 | + $index, |
|
285 | + $curOid, |
|
286 | + $object['mib'], |
|
287 | + $object['name'], |
|
288 | + '', |
|
289 | + $object['type'], |
|
290 | + $object['type_enum'] |
|
291 | + )); |
|
292 | + } |
|
293 | + else |
|
294 | + { |
|
295 | + array_push($curObjectList, array( |
|
296 | + $index, |
|
297 | + $curOid, |
|
298 | + 'not found', |
|
299 | + 'not found', |
|
300 | + '', |
|
301 | + 'not found', |
|
302 | + 'not found' |
|
303 | + )); |
|
304 | + } |
|
305 | + $curOid = preg_replace('/\*/','\*',$curOid); |
|
306 | + $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
|
307 | + $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
|
308 | + $index++; |
|
309 | + } |
|
310 | + return $curObjectList; |
|
312 | 311 | } |
313 | 312 | |
314 | 313 | /** Add a handler |
315 | - * Get params fromid : setup from existing trap (id of trap table) |
|
316 | - * Get param ruleid : edit from existing handler (id of rule table) |
|
317 | - */ |
|
314 | + * Get params fromid : setup from existing trap (id of trap table) |
|
315 | + * Get param ruleid : edit from existing handler (id of rule table) |
|
316 | + */ |
|
318 | 317 | public function addAction() |
319 | 318 | { |
320 | 319 | $this->checkConfigPermission(); |
@@ -333,8 +332,8 @@ discard block |
||
333 | 332 | //$this->view->trapvalues=false; // Set to true to display 'value' colum in objects |
334 | 333 | |
335 | 334 | if (($trapid = $this->params->get('fromid')) !== null) { |
336 | - /********** Setup from existing trap ***************/ |
|
337 | - $this->add_from_existing($trapid); |
|
335 | + /********** Setup from existing trap ***************/ |
|
336 | + $this->add_from_existing($trapid); |
|
338 | 337 | return; |
339 | 338 | } |
340 | 339 | |
@@ -359,14 +358,14 @@ discard block |
||
359 | 358 | $this->view->warning_message=''; |
360 | 359 | if ($this->view->hostname != null) |
361 | 360 | { |
362 | - $this->view->selectGroup=false; |
|
363 | - // Check if hostname still exists |
|
364 | - $this->add_check_host_exists($ruleDetail); |
|
361 | + $this->view->selectGroup=false; |
|
362 | + // Check if hostname still exists |
|
363 | + $this->add_check_host_exists($ruleDetail); |
|
365 | 364 | } |
366 | 365 | else |
367 | 366 | { |
368 | - $this->view->selectGroup=true; |
|
369 | - $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists |
|
367 | + $this->view->selectGroup=true; |
|
368 | + $this->add_check_hostgroup_exists($ruleDetail); // Check if groupe exists |
|
370 | 369 | } |
371 | 370 | |
372 | 371 | $this->view->mainoid=$ruleDetail->trap_oid; |
@@ -394,9 +393,9 @@ discard block |
||
394 | 393 | } |
395 | 394 | |
396 | 395 | /** Validate form and output message to user |
397 | - * @param in postdata |
|
398 | - * @return string status : OK / <Message> |
|
399 | - **/ |
|
396 | + * @param in postdata |
|
397 | + * @return string status : OK / <Message> |
|
398 | + **/ |
|
400 | 399 | protected function handlerformAction() |
401 | 400 | { |
402 | 401 | $postData=$this->getRequest()->getPost(); |
@@ -437,7 +436,7 @@ discard block |
||
437 | 436 | //$this->Module()-> |
438 | 437 | $this->_helper->json(array( |
439 | 438 | 'status'=>'OK', |
440 | - 'redirect'=>'trapdirector/handler' |
|
439 | + 'redirect'=>'trapdirector/handler' |
|
441 | 440 | |
442 | 441 | )); |
443 | 442 | } |
@@ -521,9 +520,9 @@ discard block |
||
521 | 520 | } |
522 | 521 | |
523 | 522 | /** Get trap detail by trapid. |
524 | - * @param integer $trapid : id of trap in received table |
|
525 | - * @return array (objects) |
|
526 | - */ |
|
523 | + * @param integer $trapid : id of trap in received table |
|
524 | + * @return array (objects) |
|
525 | + */ |
|
527 | 526 | protected function getTrapDetail($trapid) |
528 | 527 | { |
529 | 528 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
@@ -544,8 +543,8 @@ discard block |
||
544 | 543 | $trapDetail=$db->fetchRow($query); |
545 | 544 | if ( $trapDetail == null ) |
546 | 545 | { |
547 | - $trapDetail = 'NULL'; |
|
548 | - throw new Exception('No traps was found with id = '.$trapid); |
|
546 | + $trapDetail = 'NULL'; |
|
547 | + throw new Exception('No traps was found with id = '.$trapid); |
|
549 | 548 | } |
550 | 549 | } |
551 | 550 | catch (Exception $e) |
@@ -559,9 +558,9 @@ discard block |
||
559 | 558 | } |
560 | 559 | |
561 | 560 | /** Get trap objects |
562 | - * @param integer $trapid : trap id |
|
563 | - * @return array : full column in db of trap id |
|
564 | - */ |
|
561 | + * @param integer $trapid : trap id |
|
562 | + * @return array : full column in db of trap id |
|
563 | + */ |
|
565 | 564 | protected function getTrapobjects($trapid) |
566 | 565 | { |
567 | 566 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
@@ -592,9 +591,9 @@ discard block |
||
592 | 591 | } |
593 | 592 | |
594 | 593 | /** Get rule detail by ruleid. |
595 | - * @param integer $ruleid int id of rule in rule table |
|
596 | - * @return object : column objects in db |
|
597 | - */ |
|
594 | + * @param integer $ruleid int id of rule in rule table |
|
595 | + * @return object : column objects in db |
|
596 | + */ |
|
598 | 597 | protected function getRuleDetail($ruleid) |
599 | 598 | { |
600 | 599 | if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } |
@@ -621,7 +620,7 @@ discard block |
||
621 | 620 | } |
622 | 621 | |
623 | 622 | /** Setup tabs for rules |
624 | - */ |
|
623 | + */ |
|
625 | 624 | protected function prepareTabs() |
626 | 625 | { |
627 | 626 | return $this->getTabs()->add('status', array( |
@@ -23,14 +23,14 @@ discard block |
||
23 | 23 | $this->checkReadPermission(); |
24 | 24 | $this->prepareTabs()->activate('status'); |
25 | 25 | |
26 | - $db = $this->getDb(); |
|
26 | + $db=$this->getDb(); |
|
27 | 27 | $this->getHandlerListTable()->setConnection($db); |
28 | 28 | $this->getHandlerListTable()->setMibloader($this->getMIB()); |
29 | 29 | // Apply pagination limits |
30 | - $this->view->table=$this->applyPaginationLimits($this->getHandlerListTable(),$this->getModuleConfig()->itemListDisplay()); |
|
30 | + $this->view->table=$this->applyPaginationLimits($this->getHandlerListTable(), $this->getModuleConfig()->itemListDisplay()); |
|
31 | 31 | |
32 | 32 | // Set Filter |
33 | - $this->view->filterEditor = $this->getHandlerListTable()->getFilterEditor($this->getRequest()); |
|
33 | + $this->view->filterEditor=$this->getHandlerListTable()->getFilterEditor($this->getRequest()); |
|
34 | 34 | |
35 | 35 | //$this->displayExitError('Handler/indexAction','Not implemented'); |
36 | 36 | } |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | public function testruleAction() |
41 | 41 | { |
42 | 42 | $this->checkReadPermission(); |
43 | - $this->getTabs()->add('get',array( |
|
43 | + $this->getTabs()->add('get', array( |
|
44 | 44 | 'active' => true, |
45 | 45 | 'label' => $this->translate('Test Rule'), |
46 | 46 | 'url' => Url::fromRequest() |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | |
51 | 51 | if ($this->params->get('rule') !== null) |
52 | 52 | { |
53 | - $this->view->rule= $this->params->get('rule'); |
|
53 | + $this->view->rule=$this->params->get('rule'); |
|
54 | 54 | } |
55 | 55 | else |
56 | 56 | { |
@@ -109,12 +109,12 @@ discard block |
||
109 | 109 | } |
110 | 110 | catch (Exception $e) |
111 | 111 | { |
112 | - $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
|
112 | + $this->displayExitError('Add handler : get host by IP/Name ', $e->getMessage()); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | |
116 | 116 | // if one unique host found -> put id text input |
117 | - if (count($hosts)==1) { |
|
117 | + if (count($hosts) == 1) { |
|
118 | 118 | $this->view->hostname=$hosts[0]->name; |
119 | 119 | //$hostid=$hosts[0]->id; |
120 | 120 | // Tell JS to get services when page is loaded |
@@ -123,9 +123,9 @@ discard block |
||
123 | 123 | } |
124 | 124 | else |
125 | 125 | { |
126 | - foreach($hosts as $key=>$val) |
|
126 | + foreach ($hosts as $key=>$val) |
|
127 | 127 | { |
128 | - array_push($this->view->hostlist,$hosts[$key]->name); |
|
128 | + array_push($this->view->hostlist, $hosts[$key]->name); |
|
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
@@ -164,18 +164,18 @@ discard block |
||
164 | 164 | $currentObjectTypeEnum |
165 | 165 | ); |
166 | 166 | $oid_index++; |
167 | - array_push($this->view->objectList,$currentObject); |
|
167 | + array_push($this->view->objectList, $currentObject); |
|
168 | 168 | // set currrent object to null in allObjects |
169 | 169 | if (isset($allObjects[$val->oid])) |
170 | 170 | { |
171 | 171 | $allObjects[$val->oid]=null; |
172 | 172 | } |
173 | 173 | } |
174 | - if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
|
174 | + if ($allObjects != null) // in case trap doesn't have objects or is not resolved |
|
175 | 175 | { |
176 | 176 | foreach ($allObjects as $key => $val) |
177 | 177 | { |
178 | - if ($val==null) { continue; } |
|
178 | + if ($val == null) { continue; } |
|
179 | 179 | array_push($this->view->objectList, array( |
180 | 180 | $oid_index, |
181 | 181 | $key, |
@@ -203,9 +203,9 @@ discard block |
||
203 | 203 | // Check if hostname still exists |
204 | 204 | $host_get=$this->getHostByName($this->view->hostname); |
205 | 205 | |
206 | - if (count($host_get)==0) |
|
206 | + if (count($host_get) == 0) |
|
207 | 207 | { |
208 | - $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
|
208 | + $this->view->warning_message='Host '.$this->view->hostname.' doesn\'t exists anymore'; |
|
209 | 209 | $this->view->serviceGet=false; |
210 | 210 | } |
211 | 211 | else |
@@ -213,10 +213,10 @@ discard block |
||
213 | 213 | // Tell JS to get services when page is loaded |
214 | 214 | $this->view->serviceGet=true; |
215 | 215 | // get service id for form to set : |
216 | - $serviceID=$this->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
|
217 | - if (count($serviceID) ==0) |
|
216 | + $serviceID=$this->getServiceIDByName($this->view->hostname, $ruleDetail->service_name); |
|
217 | + if (count($serviceID) == 0) |
|
218 | 218 | { |
219 | - $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
219 | + $this->view->warning_message=' Service '.$ruleDetail->service_name.' doesn\'t exists anymore'; |
|
220 | 220 | } |
221 | 221 | else |
222 | 222 | { |
@@ -233,9 +233,9 @@ discard block |
||
233 | 233 | { |
234 | 234 | // Check if groupe exists |
235 | 235 | $group_get=$this->getHostGroupByName($this->view->hostgroupname); |
236 | - if (count($group_get)==0) |
|
236 | + if (count($group_get) == 0) |
|
237 | 237 | { |
238 | - $this->view->warning_message='HostGroup '.$this->view->hostgroupname. ' doesn\'t exists anymore'; |
|
238 | + $this->view->warning_message='HostGroup '.$this->view->hostgroupname.' doesn\'t exists anymore'; |
|
239 | 239 | $this->view->serviceGroupGet=false; |
240 | 240 | } |
241 | 241 | else |
@@ -253,9 +253,9 @@ discard block |
||
253 | 253 | |
254 | 254 | // Tell JS to get services when page is loaded |
255 | 255 | $this->view->serviceGroupGet=true; |
256 | - if ($foundGrpService==0) |
|
256 | + if ($foundGrpService == 0) |
|
257 | 257 | { |
258 | - $this->view->warning_message.=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
|
258 | + $this->view->warning_message.=' Service '.$ruleDetail->service_name.' doesn\'t exists anymore'; |
|
259 | 259 | } |
260 | 260 | } |
261 | 261 | } |
@@ -273,12 +273,12 @@ discard block |
||
273 | 273 | $index=1; |
274 | 274 | // check in display & rule for : OID(<oid>) |
275 | 275 | $matches=array(); |
276 | - while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
|
277 | - preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
|
276 | + while (preg_match('/_OID\(([\.0-9\*]+)\)/', $display, $matches) || |
|
277 | + preg_match('/_OID\(([\.0-9\*]+)\)/', $rule, $matches)) |
|
278 | 278 | { |
279 | 279 | $curOid=$matches[1]; |
280 | 280 | |
281 | - if ( (preg_match('/\*/',$curOid) == 0 ) |
|
281 | + if ((preg_match('/\*/', $curOid) == 0) |
|
282 | 282 | && ($object=$this->getMIB()->translateOID($curOid)) != null) |
283 | 283 | { |
284 | 284 | array_push($curObjectList, array( |
@@ -303,9 +303,9 @@ discard block |
||
303 | 303 | 'not found' |
304 | 304 | )); |
305 | 305 | } |
306 | - $curOid = preg_replace('/\*/','\*',$curOid); |
|
307 | - $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
|
308 | - $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
|
306 | + $curOid=preg_replace('/\*/', '\*', $curOid); |
|
307 | + $display=preg_replace('/_OID\('.$curOid.'\)/', '\$'.$index.'\$', $display); |
|
308 | + $rule=preg_replace('/_OID\('.$curOid.'\)/', '\$'.$index.'\$', $rule); |
|
309 | 309 | $index++; |
310 | 310 | } |
311 | 311 | return $curObjectList; |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | { |
320 | 320 | $this->checkConfigPermission(); |
321 | 321 | // set up tab |
322 | - $this->getTabs()->add('get',array( |
|
322 | + $this->getTabs()->add('get', array( |
|
323 | 323 | 'active' => true, |
324 | 324 | 'label' => $this->translate('Add handler'), |
325 | 325 | 'url' => Url::fromRequest() |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | |
333 | 333 | //$this->view->trapvalues=false; // Set to true to display 'value' colum in objects |
334 | 334 | |
335 | - if (($trapid = $this->params->get('fromid')) !== null) { |
|
335 | + if (($trapid=$this->params->get('fromid')) !== null) { |
|
336 | 336 | /********** Setup from existing trap ***************/ |
337 | 337 | $this->add_from_existing($trapid); |
338 | 338 | return; |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | $this->view->setRuleMatch=$ruleDetail->action_match; |
353 | 353 | $this->view->setRuleNoMatch=$ruleDetail->action_nomatch; |
354 | 354 | $this->view->hostgroupname=$ruleDetail->host_group_name; |
355 | - $this->view->modified=gmdate("Y-m-d\TH:i:s\Z",$ruleDetail->modified); |
|
355 | + $this->view->modified=gmdate("Y-m-d\TH:i:s\Z", $ruleDetail->modified); |
|
356 | 356 | $this->view->modifier=$ruleDetail->modifier; |
357 | 357 | |
358 | 358 | // Warning message if host/service don't exists anymore |
@@ -404,26 +404,26 @@ discard block |
||
404 | 404 | |
405 | 405 | $params=array( |
406 | 406 | // id (also db) => array('post' => post id, 'val' => default val, 'db' => send to table) |
407 | - 'hostgroup' => array('post' => 'hostgroup','db'=>false), |
|
408 | - 'db_rule' => array('post' => 'db_rule','db'=>false), |
|
409 | - 'hostid' => array('post' => 'hostid','db'=>false), |
|
410 | - 'host_name' => array('post' => 'hostname','val' => null,'db'=>true), |
|
411 | - 'host_group_name'=> array('post' => null,'val' => null,'db'=>true), |
|
412 | - 'serviceid' => array('post' => 'serviceid','db'=>false), |
|
413 | - 'service_name' => array('post' => 'serviceName','db'=>true), |
|
414 | - 'trap_oid' => array('post' => 'oid','db'=>true), |
|
415 | - 'revert_ok' => array('post' => 'revertOK','val' => 0,'db'=>true), |
|
416 | - 'display' => array('post' => 'display','val' => '','db'=>true), |
|
417 | - 'rule' => array('post' => 'rule','val' => '','db'=>true), |
|
418 | - 'action_match' => array('post' => 'ruleMatch','val' => -1,'db'=>true), |
|
419 | - 'action_nomatch'=> array('post' => 'ruleNoMatch','val' => -1,'db'=>true), |
|
420 | - 'ip4' => array('post' => null,'val' => null,'db'=>true), |
|
421 | - 'ip6' => array('post' => null,'val' => null,'db'=>true), |
|
422 | - 'action_form' => array('post' => 'action_form','db'=>false) |
|
407 | + 'hostgroup' => array('post' => 'hostgroup', 'db'=>false), |
|
408 | + 'db_rule' => array('post' => 'db_rule', 'db'=>false), |
|
409 | + 'hostid' => array('post' => 'hostid', 'db'=>false), |
|
410 | + 'host_name' => array('post' => 'hostname', 'val' => null, 'db'=>true), |
|
411 | + 'host_group_name'=> array('post' => null, 'val' => null, 'db'=>true), |
|
412 | + 'serviceid' => array('post' => 'serviceid', 'db'=>false), |
|
413 | + 'service_name' => array('post' => 'serviceName', 'db'=>true), |
|
414 | + 'trap_oid' => array('post' => 'oid', 'db'=>true), |
|
415 | + 'revert_ok' => array('post' => 'revertOK', 'val' => 0, 'db'=>true), |
|
416 | + 'display' => array('post' => 'display', 'val' => '', 'db'=>true), |
|
417 | + 'rule' => array('post' => 'rule', 'val' => '', 'db'=>true), |
|
418 | + 'action_match' => array('post' => 'ruleMatch', 'val' => -1, 'db'=>true), |
|
419 | + 'action_nomatch'=> array('post' => 'ruleNoMatch', 'val' => -1, 'db'=>true), |
|
420 | + 'ip4' => array('post' => null, 'val' => null, 'db'=>true), |
|
421 | + 'ip6' => array('post' => null, 'val' => null, 'db'=>true), |
|
422 | + 'action_form' => array('post' => 'action_form', 'db'=>false) |
|
423 | 423 | ); |
424 | 424 | |
425 | 425 | if (isset($postData[$params['action_form']['post']]) |
426 | - && $postData[$params['action_form']['post']] == 'delete' ) |
|
426 | + && $postData[$params['action_form']['post']] == 'delete') |
|
427 | 427 | { |
428 | 428 | try |
429 | 429 | { |
@@ -443,16 +443,16 @@ discard block |
||
443 | 443 | } |
444 | 444 | foreach (array_keys($params) as $key) |
445 | 445 | { |
446 | - if ($params[$key]['post']==null) continue; // data not sent in post vars |
|
447 | - if (! isset($postData[$params[$key]['post']])) |
|
446 | + if ($params[$key]['post'] == null) continue; // data not sent in post vars |
|
447 | + if (!isset($postData[$params[$key]['post']])) |
|
448 | 448 | { |
449 | 449 | // should not happen as the js checks data |
450 | - $this->_helper->json(array('status'=>'No ' . $key)); |
|
450 | + $this->_helper->json(array('status'=>'No '.$key)); |
|
451 | 451 | } |
452 | 452 | else |
453 | 453 | { |
454 | 454 | $data=$postData[$params[$key]['post']]; |
455 | - if ($data!=null && $data !="") |
|
455 | + if ($data != null && $data != "") |
|
456 | 456 | { |
457 | 457 | $params[$key]['val']=$postData[$params[$key]['post']]; |
458 | 458 | } |
@@ -461,8 +461,8 @@ discard block |
||
461 | 461 | |
462 | 462 | try |
463 | 463 | { |
464 | - $isHostGroup=($params['hostgroup']['val'] == 1)?true:false; |
|
465 | - if (! $isHostGroup ) |
|
464 | + $isHostGroup=($params['hostgroup']['val'] == 1) ?true:false; |
|
465 | + if (!$isHostGroup) |
|
466 | 466 | { // checks if selection by host |
467 | 467 | $hostAddr=$this->getHostInfoByID($params['hostid']['val']); |
468 | 468 | $params['ip4']['val']=$hostAddr->ip4; |
@@ -489,25 +489,25 @@ discard block |
||
489 | 489 | return; |
490 | 490 | } |
491 | 491 | // Put param in correct column (group_name) |
492 | - $params['host_group_name']['val'] = $params['host_name']['val']; |
|
492 | + $params['host_group_name']['val']=$params['host_name']['val']; |
|
493 | 493 | $params['host_name']['val']=null; |
494 | 494 | } |
495 | 495 | $dbparams=array(); |
496 | 496 | foreach ($params as $key=>$val) |
497 | 497 | { |
498 | - if ($val['db']==true ) |
|
498 | + if ($val['db'] == true) |
|
499 | 499 | { |
500 | - $dbparams[$key] = $val['val']; |
|
500 | + $dbparams[$key]=$val['val']; |
|
501 | 501 | } |
502 | 502 | } |
503 | 503 | // echo '<br>'; print_r($dbparams);echo '<br>'; |
504 | - if ($params['db_rule']['val'] == -1 ) |
|
504 | + if ($params['db_rule']['val'] == -1) |
|
505 | 505 | { |
506 | 506 | $ruleID=$this->addHandlerRule($dbparams); |
507 | 507 | } |
508 | 508 | else |
509 | 509 | { |
510 | - $this->updateHandlerRule($dbparams,$params['db_rule']['val']); |
|
510 | + $this->updateHandlerRule($dbparams, $params['db_rule']['val']); |
|
511 | 511 | $ruleID=$params['db_rule']['val']; |
512 | 512 | } |
513 | 513 | } |
@@ -526,10 +526,10 @@ discard block |
||
526 | 526 | */ |
527 | 527 | protected function getTrapDetail($trapid) |
528 | 528 | { |
529 | - if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
|
529 | + if (!preg_match('/^[0-9]+$/', $trapid)) { throw new Exception('Invalid id'); } |
|
530 | 530 | $queryArray=$this->getModuleConfig()->trapDetailQuery(); |
531 | 531 | |
532 | - $db = $this->getDb()->getConnection(); |
|
532 | + $db=$this->getDb()->getConnection(); |
|
533 | 533 | // *************** Get main data |
534 | 534 | // extract columns and titles; |
535 | 535 | $elmts=NULL; |
@@ -538,19 +538,19 @@ discard block |
||
538 | 538 | } |
539 | 539 | try |
540 | 540 | { |
541 | - $query = $db->select() |
|
542 | - ->from($this->getModuleConfig()->getTrapTableName(),$elmts) |
|
543 | - ->where('id=?',$trapid); |
|
541 | + $query=$db->select() |
|
542 | + ->from($this->getModuleConfig()->getTrapTableName(), $elmts) |
|
543 | + ->where('id=?', $trapid); |
|
544 | 544 | $trapDetail=$db->fetchRow($query); |
545 | - if ( $trapDetail == null ) |
|
545 | + if ($trapDetail == null) |
|
546 | 546 | { |
547 | - $trapDetail = 'NULL'; |
|
547 | + $trapDetail='NULL'; |
|
548 | 548 | throw new Exception('No traps was found with id = '.$trapid); |
549 | 549 | } |
550 | 550 | } |
551 | 551 | catch (Exception $e) |
552 | 552 | { |
553 | - $this->displayExitError('Add handler : get trap detail returning : '.print_r($trapDetail,true),$e->getMessage()); |
|
553 | + $this->displayExitError('Add handler : get trap detail returning : '.print_r($trapDetail, true), $e->getMessage()); |
|
554 | 554 | return; |
555 | 555 | } |
556 | 556 | |
@@ -564,10 +564,10 @@ discard block |
||
564 | 564 | */ |
565 | 565 | protected function getTrapobjects($trapid) |
566 | 566 | { |
567 | - if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
|
567 | + if (!preg_match('/^[0-9]+$/', $trapid)) { throw new Exception('Invalid id'); } |
|
568 | 568 | $queryArrayData=$this->getModuleConfig()->trapDataDetailQuery(); |
569 | 569 | |
570 | - $db = $this->getDb()->getConnection(); |
|
570 | + $db=$this->getDb()->getConnection(); |
|
571 | 571 | // *************** Get object data |
572 | 572 | // extract columns and titles; |
573 | 573 | $data_elmts=NULL; |
@@ -576,15 +576,15 @@ discard block |
||
576 | 576 | } |
577 | 577 | try |
578 | 578 | { |
579 | - $query = $db->select() |
|
580 | - ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
|
581 | - ->where('trap_id=?',$trapid); |
|
579 | + $query=$db->select() |
|
580 | + ->from($this->moduleConfig->getTrapDataTableName(), $data_elmts) |
|
581 | + ->where('trap_id=?', $trapid); |
|
582 | 582 | $trapDetail=$db->fetchAll($query); |
583 | 583 | // if ( $trapDetail == null ) throw new Exception('No traps was found with id = '.$trapid); |
584 | 584 | } |
585 | 585 | catch (Exception $e) |
586 | 586 | { |
587 | - $this->displayExitError('Add handler : get trap data detail : ',$e->getMessage()); |
|
587 | + $this->displayExitError('Add handler : get trap data detail : ', $e->getMessage()); |
|
588 | 588 | return array(); |
589 | 589 | } |
590 | 590 | |
@@ -597,22 +597,22 @@ discard block |
||
597 | 597 | */ |
598 | 598 | protected function getRuleDetail($ruleid) |
599 | 599 | { |
600 | - if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } |
|
600 | + if (!preg_match('/^[0-9]+$/', $ruleid)) { throw new Exception('Invalid id'); } |
|
601 | 601 | $queryArray=$this->getModuleConfig()->ruleDetailQuery(); |
602 | 602 | |
603 | - $db = $this->getDb()->getConnection(); |
|
603 | + $db=$this->getDb()->getConnection(); |
|
604 | 604 | // *************** Get main data |
605 | 605 | try |
606 | 606 | { |
607 | - $query = $db->select() |
|
608 | - ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
|
609 | - ->where('id=?',$ruleid); |
|
607 | + $query=$db->select() |
|
608 | + ->from($this->getModuleConfig()->getTrapRuleName(), $queryArray) |
|
609 | + ->where('id=?', $ruleid); |
|
610 | 610 | $ruleDetail=$db->fetchRow($query); |
611 | - if ( $ruleDetail == null ) throw new Exception('No rule was found with id = '.$ruleid); |
|
611 | + if ($ruleDetail == null) throw new Exception('No rule was found with id = '.$ruleid); |
|
612 | 612 | } |
613 | 613 | catch (Exception $e) |
614 | 614 | { |
615 | - $this->displayExitError('Update handler : get rule detail',$e->getMessage()); |
|
615 | + $this->displayExitError('Update handler : get rule detail', $e->getMessage()); |
|
616 | 616 | return array(); |
617 | 617 | } |
618 | 618 | |
@@ -626,7 +626,7 @@ discard block |
||
626 | 626 | { |
627 | 627 | return $this->getTabs()->add('status', array( |
628 | 628 | 'label' => $this->translate('Traps'), |
629 | - 'url' => $this->getModuleConfig()->urlPath() . '/handler') |
|
629 | + 'url' => $this->getModuleConfig()->urlPath().'/handler') |
|
630 | 630 | ); |
631 | 631 | } |
632 | 632 |
@@ -17,13 +17,13 @@ discard block |
||
17 | 17 | try |
18 | 18 | { |
19 | 19 | |
20 | - $trap = new Trap($icingaweb2Etc); |
|
21 | - //$Trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
|
22 | - //$Trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
|
23 | - //$Trap->setLogging(4,'syslog'); |
|
20 | + $trap = new Trap($icingaweb2Etc); |
|
21 | + //$Trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
|
22 | + //$Trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
|
23 | + //$Trap->setLogging(4,'syslog'); |
|
24 | 24 | |
25 | - // TODO : tranfer this to reset_trap cli command |
|
26 | - $trap->eraseOldTraps(); |
|
25 | + // TODO : tranfer this to reset_trap cli command |
|
26 | + $trap->eraseOldTraps(); |
|
27 | 27 | |
28 | 28 | $trap->read_trap('php://stdin'); |
29 | 29 | |
@@ -36,15 +36,15 @@ discard block |
||
36 | 36 | } |
37 | 37 | catch (Exception $e) |
38 | 38 | { |
39 | - if ($trap == null) |
|
40 | - { // Exception in trap creation : log in display & syslog |
|
41 | - $logging = new Logging(); |
|
42 | - $logging->log("Caught exception creating Trap class",2); |
|
43 | - } |
|
44 | - else |
|
45 | - { |
|
39 | + if ($trap == null) |
|
40 | + { // Exception in trap creation : log in display & syslog |
|
41 | + $logging = new Logging(); |
|
42 | + $logging->log("Caught exception creating Trap class",2); |
|
43 | + } |
|
44 | + else |
|
45 | + { |
|
46 | 46 | $trap->trapLog("Exception : ". $e->getMessage(),2,0); |
47 | - } |
|
47 | + } |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | //end |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | use Trapdirector\Trap; |
5 | 5 | |
6 | 6 | // start |
7 | -$time1 = microtime(true); |
|
7 | +$time1=microtime(true); |
|
8 | 8 | |
9 | 9 | require_once ('trap_class.php'); |
10 | 10 | |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | try |
18 | 18 | { |
19 | 19 | |
20 | - $trap = new Trap($icingaweb2Etc); |
|
20 | + $trap=new Trap($icingaweb2Etc); |
|
21 | 21 | //$Trap = new Trap($icingaweb2Etc,4,'display'); // For debug |
22 | 22 | //$Trap = new Trap($icingaweb2Etc,4,'syslog'); // For debug |
23 | 23 | //$Trap->setLogging(4,'syslog'); |
@@ -38,12 +38,12 @@ discard block |
||
38 | 38 | { |
39 | 39 | if ($trap == null) |
40 | 40 | { // Exception in trap creation : log in display & syslog |
41 | - $logging = new Logging(); |
|
42 | - $logging->log("Caught exception creating Trap class",2); |
|
41 | + $logging=new Logging(); |
|
42 | + $logging->log("Caught exception creating Trap class", 2); |
|
43 | 43 | } |
44 | 44 | else |
45 | 45 | { |
46 | - $trap->trapLog("Exception : ". $e->getMessage(),2,0); |
|
46 | + $trap->trapLog("Exception : ".$e->getMessage(), 2, 0); |
|
47 | 47 | } |
48 | 48 | } |
49 | 49 |
@@ -33,15 +33,13 @@ |
||
33 | 33 | |
34 | 34 | $trap->add_rule_final(microtime(true) - $time1); |
35 | 35 | |
36 | -} |
|
37 | -catch (Exception $e) |
|
36 | +} catch (Exception $e) |
|
38 | 37 | { |
39 | 38 | if ($trap == null) |
40 | 39 | { // Exception in trap creation : log in display & syslog |
41 | 40 | $logging = new Logging(); |
42 | 41 | $logging->log("Caught exception creating Trap class",2); |
43 | - } |
|
44 | - else |
|
42 | + } else |
|
45 | 43 | { |
46 | 44 | $trap->trapLog("Exception : ". $e->getMessage(),2,0); |
47 | 45 | } |
@@ -64,10 +64,10 @@ discard block |
||
64 | 64 | } |
65 | 65 | |
66 | 66 | /** Mib management |
67 | - * Post param : action=update_mib_db : update mib database |
|
68 | - * Post param : ation=check_update : check if mib update is finished |
|
69 | - * File post : mibfile -> save mib file |
|
70 | - */ |
|
67 | + * Post param : action=update_mib_db : update mib database |
|
68 | + * Post param : ation=check_update : check if mib update is finished |
|
69 | + * File post : mibfile -> save mib file |
|
70 | + */ |
|
71 | 71 | public function mibAction() |
72 | 72 | { |
73 | 73 | $this->prepareTabs()->activate('mib'); |
@@ -86,22 +86,22 @@ discard block |
||
86 | 86 | $return=exec('icingacli trapdirector mib update --pid /tmp/trapdirector_update.pid'); |
87 | 87 | if (preg_match('/OK/',$return)) |
88 | 88 | { |
89 | - $this->_helper->json(array('status'=>'OK')); |
|
89 | + $this->_helper->json(array('status'=>'OK')); |
|
90 | 90 | } |
91 | 91 | // Error |
92 | 92 | $this->_helper->json(array('status'=>$return)); |
93 | 93 | } |
94 | 94 | if ($action == 'check_update') |
95 | 95 | { |
96 | - $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
97 | - if ($file == false) |
|
98 | - { // process is dead |
|
99 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
100 | - return; |
|
101 | - } |
|
102 | - $pid=fgets($file); |
|
103 | - $output=array(); |
|
104 | - $retVal=0; |
|
96 | + $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
97 | + if ($file == false) |
|
98 | + { // process is dead |
|
99 | + $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
100 | + return; |
|
101 | + } |
|
102 | + $pid=fgets($file); |
|
103 | + $output=array(); |
|
104 | + $retVal=0; |
|
105 | 105 | exec('ps '.$pid,$output,$retVal); |
106 | 106 | if ($retVal == 0) |
107 | 107 | { // process is alive |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | } |
110 | 110 | else |
111 | 111 | { // process is dead |
112 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
112 | + $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
113 | 113 | } |
114 | 114 | } |
115 | 115 | $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible' )); |
@@ -117,32 +117,32 @@ discard block |
||
117 | 117 | /** Check for mib file UPLOAD */ |
118 | 118 | if (isset($_FILES['mibfile'])) |
119 | 119 | { |
120 | - $name=filter_var($_FILES['mibfile']['name'],FILTER_SANITIZE_STRING); |
|
120 | + $name=filter_var($_FILES['mibfile']['name'],FILTER_SANITIZE_STRING); |
|
121 | 121 | $DirConf=explode(':',$this->Config()->get('config', 'snmptranslate_dirs')); |
122 | 122 | $destDir=array_shift($DirConf); |
123 | 123 | if (!is_dir($destDir)) |
124 | 124 | { |
125 | - $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
|
125 | + $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
|
126 | 126 | } |
127 | 127 | else |
128 | 128 | { |
129 | - if (!is_writable($destDir)) |
|
130 | - { |
|
131 | - $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
|
132 | - } |
|
133 | - else |
|
134 | - { |
|
135 | - $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
136 | - $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
|
137 | - if (move_uploaded_file($sourceTmpNam,$destination)===false) |
|
138 | - { |
|
139 | - $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
|
140 | - } |
|
141 | - else |
|
142 | - { |
|
143 | - $this->view->uploadStatus="File $name uploaded in $destDir"; |
|
144 | - } |
|
145 | - } |
|
129 | + if (!is_writable($destDir)) |
|
130 | + { |
|
131 | + $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
|
132 | + } |
|
133 | + else |
|
134 | + { |
|
135 | + $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
136 | + $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
|
137 | + if (move_uploaded_file($sourceTmpNam,$destination)===false) |
|
138 | + { |
|
139 | + $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
|
140 | + } |
|
141 | + else |
|
142 | + { |
|
143 | + $this->view->uploadStatus="File $name uploaded in $destDir"; |
|
144 | + } |
|
145 | + } |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | } |
@@ -262,47 +262,47 @@ discard block |
||
262 | 262 | */ |
263 | 263 | public function pluginsAction() |
264 | 264 | { |
265 | - $this->prepareTabs()->activate('plugins'); |
|
265 | + $this->prepareTabs()->activate('plugins'); |
|
266 | 266 | |
267 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
268 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
269 | - $Trap = new Trap($icingaweb2_etc,4); |
|
267 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
268 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
269 | + $Trap = new Trap($icingaweb2_etc,4); |
|
270 | 270 | |
271 | - $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
271 | + $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
272 | 272 | |
273 | - $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
273 | + $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
274 | 274 | |
275 | - $pluginList = $Trap->pluginClass->pluginList(); |
|
275 | + $pluginList = $Trap->pluginClass->pluginList(); |
|
276 | 276 | |
277 | - // Plugin list and fill function name list |
|
278 | - $functionList=array(); |
|
279 | - $this->view->pluginArray=array(); |
|
280 | - foreach ($pluginList as $plugin) |
|
281 | - { |
|
282 | - $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
|
283 | - $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
284 | - $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
285 | - $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
286 | - $pluginDetails->description = htmlentities($pluginDetails->description); |
|
287 | - $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
288 | - array_push($this->view->pluginArray, $pluginDetails); |
|
289 | - // Get functions for function details |
|
290 | - foreach ($pluginDetails->funcArray as $function) |
|
291 | - { |
|
292 | - array_push($functionList,$function); |
|
293 | - } |
|
294 | - } |
|
277 | + // Plugin list and fill function name list |
|
278 | + $functionList=array(); |
|
279 | + $this->view->pluginArray=array(); |
|
280 | + foreach ($pluginList as $plugin) |
|
281 | + { |
|
282 | + $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
|
283 | + $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
284 | + $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
285 | + $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
286 | + $pluginDetails->description = htmlentities($pluginDetails->description); |
|
287 | + $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
288 | + array_push($this->view->pluginArray, $pluginDetails); |
|
289 | + // Get functions for function details |
|
290 | + foreach ($pluginDetails->funcArray as $function) |
|
291 | + { |
|
292 | + array_push($functionList,$function); |
|
293 | + } |
|
294 | + } |
|
295 | 295 | |
296 | - // Function list with details |
|
297 | - $this->view->functionList=array(); |
|
298 | - foreach ($functionList as $function) |
|
299 | - { |
|
300 | - $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
301 | - $functionDetail->params = htmlentities($functionDetail->params); |
|
302 | - $functionDetail->description = htmlentities($functionDetail->description); |
|
303 | - $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
304 | - array_push($this->view->functionList, $functionDetail); |
|
305 | - } |
|
296 | + // Function list with details |
|
297 | + $this->view->functionList=array(); |
|
298 | + foreach ($functionList as $function) |
|
299 | + { |
|
300 | + $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
301 | + $functionDetail->params = htmlentities($functionDetail->params); |
|
302 | + $functionDetail->description = htmlentities($functionDetail->description); |
|
303 | + $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
304 | + array_push($this->view->functionList, $functionDetail); |
|
305 | + } |
|
306 | 306 | |
307 | 307 | } |
308 | 308 | |
@@ -317,30 +317,30 @@ discard block |
||
317 | 317 | )->add('services', array( |
318 | 318 | 'label' => $this->translate('Services management'), |
319 | 319 | 'url' => $this->getModuleConfig()->urlPath() . '/status/services') |
320 | - )->add('plugins', array( |
|
321 | - 'label' => $this->translate('Plugins management'), |
|
322 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
323 | - ); |
|
320 | + )->add('plugins', array( |
|
321 | + 'label' => $this->translate('Plugins management'), |
|
322 | + 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
323 | + ); |
|
324 | 324 | } |
325 | 325 | } |
326 | 326 | |
327 | 327 | // TODO : see if useless |
328 | 328 | class UploadForm extends Form |
329 | 329 | { |
330 | - public function __construct($options = null) |
|
331 | - { |
|
332 | - parent::__construct($options); |
|
333 | - $this->addElements2(); |
|
334 | - } |
|
330 | + public function __construct($options = null) |
|
331 | + { |
|
332 | + parent::__construct($options); |
|
333 | + $this->addElements2(); |
|
334 | + } |
|
335 | 335 | |
336 | - public function addElements2() |
|
337 | - { |
|
338 | - // File Input |
|
339 | - $file = new File('mib-file'); |
|
340 | - $file->setLabel('Mib upload'); |
|
341 | - //->setAttrib('multiple', null); |
|
342 | - $this->addElement($file); |
|
336 | + public function addElements2() |
|
337 | + { |
|
338 | + // File Input |
|
339 | + $file = new File('mib-file'); |
|
340 | + $file->setLabel('Mib upload'); |
|
341 | + //->setAttrib('multiple', null); |
|
342 | + $this->addElement($file); |
|
343 | 343 | $button = new Submit("upload",array('ignore'=>false)); |
344 | 344 | $this->addElement($button);//->setIgnore(false); |
345 | - } |
|
345 | + } |
|
346 | 346 | } |
@@ -21,18 +21,18 @@ discard block |
||
21 | 21 | /************ Trapdb ***********/ |
22 | 22 | try |
23 | 23 | { |
24 | - $db = $this->getDb()->getConnection(); |
|
25 | - $query = $db->select()->from( |
|
24 | + $db=$this->getDb()->getConnection(); |
|
25 | + $query=$db->select()->from( |
|
26 | 26 | $this->getModuleConfig()->getTrapTableName(), |
27 | 27 | array('COUNT(*)') |
28 | 28 | ); |
29 | 29 | $this->view->trap_count=$db->fetchOne($query); |
30 | - $query = $db->select()->from( |
|
30 | + $query=$db->select()->from( |
|
31 | 31 | $this->getModuleConfig()->getTrapDataTableName(), |
32 | 32 | array('COUNT(*)') |
33 | 33 | ); |
34 | 34 | $this->view->trap_object_count=$db->fetchOne($query); |
35 | - $query = $db->select()->from( |
|
35 | + $query=$db->select()->from( |
|
36 | 36 | $this->getModuleConfig()->getTrapRuleName(), |
37 | 37 | array('COUNT(*)') |
38 | 38 | ); |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | } |
44 | 44 | catch (Exception $e) |
45 | 45 | { |
46 | - $this->displayExitError('status',$e->getMessage()); |
|
46 | + $this->displayExitError('status', $e->getMessage()); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /*************** Log destination *******************/ |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | } |
59 | 59 | catch (Exception $e) |
60 | 60 | { |
61 | - $this->displayExitError('status',$e->getMessage()); |
|
61 | + $this->displayExitError('status', $e->getMessage()); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | } |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | if ($action == 'update_mib_db') |
85 | 85 | { // Do the update in background |
86 | 86 | $return=exec('icingacli trapdirector mib update --pid /tmp/trapdirector_update.pid'); |
87 | - if (preg_match('/OK/',$return)) |
|
87 | + if (preg_match('/OK/', $return)) |
|
88 | 88 | { |
89 | 89 | $this->_helper->json(array('status'=>'OK')); |
90 | 90 | } |
@@ -93,32 +93,32 @@ discard block |
||
93 | 93 | } |
94 | 94 | if ($action == 'check_update') |
95 | 95 | { |
96 | - $file=@fopen('/tmp/trapdirector_update.pid','r'); |
|
96 | + $file=@fopen('/tmp/trapdirector_update.pid', 'r'); |
|
97 | 97 | if ($file == false) |
98 | 98 | { // process is dead |
99 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'Cannot open file')); |
|
99 | + $this->_helper->json(array('status'=>'tu quoque fili', 'err'=>'Cannot open file')); |
|
100 | 100 | return; |
101 | 101 | } |
102 | 102 | $pid=fgets($file); |
103 | 103 | $output=array(); |
104 | 104 | $retVal=0; |
105 | - exec('ps '.$pid,$output,$retVal); |
|
105 | + exec('ps '.$pid, $output, $retVal); |
|
106 | 106 | if ($retVal == 0) |
107 | 107 | { // process is alive |
108 | 108 | $this->_helper->json(array('status'=>'Alive and kicking')); |
109 | 109 | } |
110 | 110 | else |
111 | 111 | { // process is dead |
112 | - $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
|
112 | + $this->_helper->json(array('status'=>'tu quoque fili', 'err'=>'no proc'.$pid)); |
|
113 | 113 | } |
114 | 114 | } |
115 | - $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible' )); |
|
115 | + $this->_helper->json(array('status'=>'ERR : no '.$action.' action possible')); |
|
116 | 116 | } |
117 | 117 | /** Check for mib file UPLOAD */ |
118 | 118 | if (isset($_FILES['mibfile'])) |
119 | 119 | { |
120 | - $name=filter_var($_FILES['mibfile']['name'],FILTER_SANITIZE_STRING); |
|
121 | - $DirConf=explode(':',$this->Config()->get('config', 'snmptranslate_dirs')); |
|
120 | + $name=filter_var($_FILES['mibfile']['name'], FILTER_SANITIZE_STRING); |
|
121 | + $DirConf=explode(':', $this->Config()->get('config', 'snmptranslate_dirs')); |
|
122 | 122 | $destDir=array_shift($DirConf); |
123 | 123 | if (!is_dir($destDir)) |
124 | 124 | { |
@@ -132,9 +132,9 @@ discard block |
||
132 | 132 | } |
133 | 133 | else |
134 | 134 | { |
135 | - $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
136 | - $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
|
137 | - if (move_uploaded_file($sourceTmpNam,$destination)===false) |
|
135 | + $destination=$destDir.'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
|
136 | + $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'], FILTER_SANITIZE_STRING); |
|
137 | + if (move_uploaded_file($sourceTmpNam, $destination) === false) |
|
138 | 138 | { |
139 | 139 | $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
140 | 140 | } |
@@ -150,13 +150,13 @@ discard block |
||
150 | 150 | } |
151 | 151 | |
152 | 152 | // snmptranslate tests |
153 | - $snmptranslate = $this->Config()->get('config', 'snmptranslate'); |
|
153 | + $snmptranslate=$this->Config()->get('config', 'snmptranslate'); |
|
154 | 154 | $this->view->snmptranslate_bin=$snmptranslate; |
155 | 155 | $this->view->snmptranslate_state='warn'; |
156 | - if (is_executable ( $snmptranslate )) |
|
156 | + if (is_executable($snmptranslate)) |
|
157 | 157 | { |
158 | - $translate=exec($snmptranslate . ' 1'); |
|
159 | - if (preg_match('/iso/',$translate)) |
|
158 | + $translate=exec($snmptranslate.' 1'); |
|
159 | + if (preg_match('/iso/', $translate)) |
|
160 | 160 | { |
161 | 161 | $this->view->snmptranslate='works fine'; |
162 | 162 | $this->view->snmptranslate_state='ok'; |
@@ -174,46 +174,46 @@ discard block |
||
174 | 174 | // mib database |
175 | 175 | |
176 | 176 | $this->view->mibDbCount=$this->getMIB()->countObjects(); |
177 | - $this->view->mibDbCountTrap=$this->getMIB()->countObjects(null,21); |
|
177 | + $this->view->mibDbCountTrap=$this->getMIB()->countObjects(null, 21); |
|
178 | 178 | |
179 | 179 | // mib dirs |
180 | 180 | $DirConf=$this->Config()->get('config', 'snmptranslate_dirs'); |
181 | - $dirArray=explode(':',$DirConf); |
|
181 | + $dirArray=explode(':', $DirConf); |
|
182 | 182 | |
183 | 183 | // Get base directories from net-snmp-config |
184 | 184 | $output=$matches=array(); |
185 | 185 | $retVal=0; |
186 | - $sysDirs=exec('net-snmp-config --default-mibdirs',$output,$retVal); |
|
187 | - if ($retVal==0) |
|
186 | + $sysDirs=exec('net-snmp-config --default-mibdirs', $output, $retVal); |
|
187 | + if ($retVal == 0) |
|
188 | 188 | { |
189 | - $dirArray=array_merge($dirArray,explode(':',$sysDirs)); |
|
189 | + $dirArray=array_merge($dirArray, explode(':', $sysDirs)); |
|
190 | 190 | } |
191 | 191 | else |
192 | 192 | { |
193 | - $translateOut=exec($this->Config()->get('config', 'snmptranslate') . ' -Dinit_mib .1.3 2>&1 | grep MIBDIRS'); |
|
194 | - if (preg_match('/MIBDIRS.*\'([^\']+)\'/',$translateOut,$matches)) |
|
193 | + $translateOut=exec($this->Config()->get('config', 'snmptranslate').' -Dinit_mib .1.3 2>&1 | grep MIBDIRS'); |
|
194 | + if (preg_match('/MIBDIRS.*\'([^\']+)\'/', $translateOut, $matches)) |
|
195 | 195 | { |
196 | - $dirArray=array_merge($dirArray,explode(':',$matches[1])); |
|
196 | + $dirArray=array_merge($dirArray, explode(':', $matches[1])); |
|
197 | 197 | } |
198 | 198 | else |
199 | 199 | { |
200 | - array_push($dirArray,'Install net-snmp-config to see system directories'); |
|
200 | + array_push($dirArray, 'Install net-snmp-config to see system directories'); |
|
201 | 201 | } |
202 | 202 | } |
203 | 203 | |
204 | 204 | $this->view->dirArray=$dirArray; |
205 | 205 | |
206 | 206 | $output=null; |
207 | - foreach (explode(':',$DirConf) as $mibdir) |
|
207 | + foreach (explode(':', $DirConf) as $mibdir) |
|
208 | 208 | { |
209 | - exec('ls '.$mibdir.' | grep -v traplist.txt',$output); |
|
209 | + exec('ls '.$mibdir.' | grep -v traplist.txt', $output); |
|
210 | 210 | } |
211 | 211 | //$i=0;$listFiles='';while (isset($output[$i])) $listFiles.=$output[$i++]; |
212 | 212 | //$this->view->fileList=explode(' ',$listFiles); |
213 | 213 | $this->view->fileList=$output; |
214 | 214 | |
215 | 215 | // Zend form |
216 | - $this->view->form= new UploadForm(); |
|
216 | + $this->view->form=new UploadForm(); |
|
217 | 217 | //$this->view->form= new Form('upload-form'); |
218 | 218 | |
219 | 219 | |
@@ -237,18 +237,18 @@ discard block |
||
237 | 237 | $this->view->templateForm_output=''; |
238 | 238 | if (isset($postData['template_name']) && isset($postData['template_revert_time'])) |
239 | 239 | { |
240 | - $template_create = 'icingacli director service create --json \'{ "check_command": "dummy", '; |
|
241 | - $template_create .= '"check_interval": "' .$postData['template_revert_time']. '", "check_timeout": "20", "disabled": false, "enable_active_checks": true, "enable_event_handler": true, "enable_notifications": true, "enable_passive_checks": true, "enable_perfdata": true, "max_check_attempts": "1", '; |
|
242 | - $template_create .= '"object_name": "'.$postData['template_name'].'", "object_type": "template", "retry_interval": "'.$postData['template_revert_time'].'"}\''; |
|
240 | + $template_create='icingacli director service create --json \'{ "check_command": "dummy", '; |
|
241 | + $template_create.='"check_interval": "'.$postData['template_revert_time'].'", "check_timeout": "20", "disabled": false, "enable_active_checks": true, "enable_event_handler": true, "enable_notifications": true, "enable_passive_checks": true, "enable_perfdata": true, "max_check_attempts": "1", '; |
|
242 | + $template_create.='"object_name": "'.$postData['template_name'].'", "object_type": "template", "retry_interval": "'.$postData['template_revert_time'].'"}\''; |
|
243 | 243 | $output=array(); |
244 | 244 | $ret_code=0; |
245 | - exec($template_create,$output,$ret_code); |
|
245 | + exec($template_create, $output, $ret_code); |
|
246 | 246 | if ($ret_code != 0) |
247 | 247 | { |
248 | - $this->displayExitError("Status -> Services","Error creating template : ".$output[0].'<br>Command was : '.$template_create); |
|
248 | + $this->displayExitError("Status -> Services", "Error creating template : ".$output[0].'<br>Command was : '.$template_create); |
|
249 | 249 | } |
250 | - exec('icingacli director config deploy',$output,$ret_code); |
|
251 | - $this->view->templateForm_output='Template '.$postData['template_name']. ' created'; |
|
250 | + exec('icingacli director config deploy', $output, $ret_code); |
|
251 | + $this->view->templateForm_output='Template '.$postData['template_name'].' created'; |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | // template creation form |
@@ -264,15 +264,15 @@ discard block |
||
264 | 264 | { |
265 | 265 | $this->prepareTabs()->activate('plugins'); |
266 | 266 | |
267 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
267 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
268 | 268 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
269 | - $Trap = new Trap($icingaweb2_etc,4); |
|
269 | + $Trap=new Trap($icingaweb2_etc, 4); |
|
270 | 270 | |
271 | - $this->view->pluginLoaded = htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
271 | + $this->view->pluginLoaded=htmlentities($Trap->pluginClass->registerAllPlugins(false)); |
|
272 | 272 | |
273 | - $enabledPlugins = $Trap->pluginClass->getEnabledPlugins(); |
|
273 | + $enabledPlugins=$Trap->pluginClass->getEnabledPlugins(); |
|
274 | 274 | |
275 | - $pluginList = $Trap->pluginClass->pluginList(); |
|
275 | + $pluginList=$Trap->pluginClass->pluginList(); |
|
276 | 276 | |
277 | 277 | // Plugin list and fill function name list |
278 | 278 | $functionList=array(); |
@@ -280,16 +280,16 @@ discard block |
||
280 | 280 | foreach ($pluginList as $plugin) |
281 | 281 | { |
282 | 282 | $pluginDetails=$Trap->pluginClass->pluginDetails($plugin); |
283 | - $pluginDetails->enabled = (in_array($plugin, $enabledPlugins)) ? true : false; |
|
284 | - $pluginDetails->catchAllTraps = ($pluginDetails->catchAllTraps === true )? 'Yes' : 'No'; |
|
285 | - $pluginDetails->processTraps = ($pluginDetails->processTraps === true )? 'Yes' : 'No'; |
|
286 | - $pluginDetails->description = htmlentities($pluginDetails->description); |
|
287 | - $pluginDetails->description = preg_replace('/\n/','<br>',$pluginDetails->description); |
|
283 | + $pluginDetails->enabled=(in_array($plugin, $enabledPlugins)) ? true : false; |
|
284 | + $pluginDetails->catchAllTraps=($pluginDetails->catchAllTraps === true) ? 'Yes' : 'No'; |
|
285 | + $pluginDetails->processTraps=($pluginDetails->processTraps === true) ? 'Yes' : 'No'; |
|
286 | + $pluginDetails->description=htmlentities($pluginDetails->description); |
|
287 | + $pluginDetails->description=preg_replace('/\n/', '<br>', $pluginDetails->description); |
|
288 | 288 | array_push($this->view->pluginArray, $pluginDetails); |
289 | 289 | // Get functions for function details |
290 | 290 | foreach ($pluginDetails->funcArray as $function) |
291 | 291 | { |
292 | - array_push($functionList,$function); |
|
292 | + array_push($functionList, $function); |
|
293 | 293 | } |
294 | 294 | } |
295 | 295 | |
@@ -297,10 +297,10 @@ discard block |
||
297 | 297 | $this->view->functionList=array(); |
298 | 298 | foreach ($functionList as $function) |
299 | 299 | { |
300 | - $functionDetail = $Trap->pluginClass->getFunctionDetails($function); |
|
301 | - $functionDetail->params = htmlentities($functionDetail->params); |
|
302 | - $functionDetail->description = htmlentities($functionDetail->description); |
|
303 | - $functionDetail->description = preg_replace('/\n/','<br>',$functionDetail->description); |
|
300 | + $functionDetail=$Trap->pluginClass->getFunctionDetails($function); |
|
301 | + $functionDetail->params=htmlentities($functionDetail->params); |
|
302 | + $functionDetail->description=htmlentities($functionDetail->description); |
|
303 | + $functionDetail->description=preg_replace('/\n/', '<br>', $functionDetail->description); |
|
304 | 304 | array_push($this->view->functionList, $functionDetail); |
305 | 305 | } |
306 | 306 | |
@@ -310,16 +310,16 @@ discard block |
||
310 | 310 | { |
311 | 311 | return $this->getTabs()->add('status', array( |
312 | 312 | 'label' => $this->translate('Status'), |
313 | - 'url' => $this->getModuleConfig()->urlPath() . '/status') |
|
313 | + 'url' => $this->getModuleConfig()->urlPath().'/status') |
|
314 | 314 | )->add('mib', array( |
315 | 315 | 'label' => $this->translate('MIB Management'), |
316 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/mib') |
|
316 | + 'url' => $this->getModuleConfig()->urlPath().'/status/mib') |
|
317 | 317 | )->add('services', array( |
318 | 318 | 'label' => $this->translate('Services management'), |
319 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/services') |
|
319 | + 'url' => $this->getModuleConfig()->urlPath().'/status/services') |
|
320 | 320 | )->add('plugins', array( |
321 | 321 | 'label' => $this->translate('Plugins management'), |
322 | - 'url' => $this->getModuleConfig()->urlPath() . '/status/plugins') |
|
322 | + 'url' => $this->getModuleConfig()->urlPath().'/status/plugins') |
|
323 | 323 | ); |
324 | 324 | } |
325 | 325 | } |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | // TODO : see if useless |
328 | 328 | class UploadForm extends Form |
329 | 329 | { |
330 | - public function __construct($options = null) |
|
330 | + public function __construct($options=null) |
|
331 | 331 | { |
332 | 332 | parent::__construct($options); |
333 | 333 | $this->addElements2(); |
@@ -336,11 +336,11 @@ discard block |
||
336 | 336 | public function addElements2() |
337 | 337 | { |
338 | 338 | // File Input |
339 | - $file = new File('mib-file'); |
|
339 | + $file=new File('mib-file'); |
|
340 | 340 | $file->setLabel('Mib upload'); |
341 | 341 | //->setAttrib('multiple', null); |
342 | 342 | $this->addElement($file); |
343 | - $button = new Submit("upload",array('ignore'=>false)); |
|
344 | - $this->addElement($button);//->setIgnore(false); |
|
343 | + $button=new Submit("upload", array('ignore'=>false)); |
|
344 | + $this->addElement($button); //->setIgnore(false); |
|
345 | 345 | } |
346 | 346 | } |
@@ -40,8 +40,7 @@ discard block |
||
40 | 40 | |
41 | 41 | $this->view->trap_days_delete=$this->getDBConfigValue('db_remove_days'); |
42 | 42 | |
43 | - } |
|
44 | - catch (Exception $e) |
|
43 | + } catch (Exception $e) |
|
45 | 44 | { |
46 | 45 | $this->displayExitError('status',$e->getMessage()); |
47 | 46 | } |
@@ -55,8 +54,7 @@ discard block |
||
55 | 54 | $this->view->currentLogFile=$this->getDBConfigValue('log_file'); |
56 | 55 | $this->view->logLevels=$this->getModuleConfig()->getlogLevels(); |
57 | 56 | $this->view->currentLogLevel=$this->getDBConfigValue('log_level'); |
58 | - } |
|
59 | - catch (Exception $e) |
|
57 | + } catch (Exception $e) |
|
60 | 58 | { |
61 | 59 | $this->displayExitError('status',$e->getMessage()); |
62 | 60 | } |
@@ -106,8 +104,7 @@ discard block |
||
106 | 104 | if ($retVal == 0) |
107 | 105 | { // process is alive |
108 | 106 | $this->_helper->json(array('status'=>'Alive and kicking')); |
109 | - } |
|
110 | - else |
|
107 | + } else |
|
111 | 108 | { // process is dead |
112 | 109 | $this->_helper->json(array('status'=>'tu quoque fili','err'=>'no proc'.$pid)); |
113 | 110 | } |
@@ -123,22 +120,19 @@ discard block |
||
123 | 120 | if (!is_dir($destDir)) |
124 | 121 | { |
125 | 122 | $this->view->uploadStatus="ERROR : no $destDir directory, check module configuration"; |
126 | - } |
|
127 | - else |
|
123 | + } else |
|
128 | 124 | { |
129 | 125 | if (!is_writable($destDir)) |
130 | 126 | { |
131 | 127 | $this->view->uploadStatus="ERROR : $destDir directory is not writable"; |
132 | - } |
|
133 | - else |
|
128 | + } else |
|
134 | 129 | { |
135 | 130 | $destination = $destDir .'/'.$name; //$this->Module()->getBaseDir() . "/mibs/$name"; |
136 | 131 | $sourceTmpNam=filter_var($_FILES['mibfile']['tmp_name'],FILTER_SANITIZE_STRING); |
137 | 132 | if (move_uploaded_file($sourceTmpNam,$destination)===false) |
138 | 133 | { |
139 | 134 | $this->view->uploadStatus="ERROR, file $destination not loaded. Check file and path name or selinux violations"; |
140 | - } |
|
141 | - else |
|
135 | + } else |
|
142 | 136 | { |
143 | 137 | $this->view->uploadStatus="File $name uploaded in $destDir"; |
144 | 138 | } |
@@ -160,13 +154,11 @@ discard block |
||
160 | 154 | { |
161 | 155 | $this->view->snmptranslate='works fine'; |
162 | 156 | $this->view->snmptranslate_state='ok'; |
163 | - } |
|
164 | - else |
|
157 | + } else |
|
165 | 158 | { |
166 | 159 | $this->view->snmptranslate='can execute but no resolution'; |
167 | 160 | } |
168 | - } |
|
169 | - else |
|
161 | + } else |
|
170 | 162 | { |
171 | 163 | $this->view->snmptranslate='cannot execute'; |
172 | 164 | } |
@@ -187,15 +179,13 @@ discard block |
||
187 | 179 | if ($retVal==0) |
188 | 180 | { |
189 | 181 | $dirArray=array_merge($dirArray,explode(':',$sysDirs)); |
190 | - } |
|
191 | - else |
|
182 | + } else |
|
192 | 183 | { |
193 | 184 | $translateOut=exec($this->Config()->get('config', 'snmptranslate') . ' -Dinit_mib .1.3 2>&1 | grep MIBDIRS'); |
194 | 185 | if (preg_match('/MIBDIRS.*\'([^\']+)\'/',$translateOut,$matches)) |
195 | 186 | { |
196 | 187 | $dirArray=array_merge($dirArray,explode(':',$matches[1])); |
197 | - } |
|
198 | - else |
|
188 | + } else |
|
199 | 189 | { |
200 | 190 | array_push($dirArray,'Install net-snmp-config to see system directories'); |
201 | 191 | } |
@@ -92,8 +92,7 @@ discard block |
||
92 | 92 | { |
93 | 93 | $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
94 | 94 | $this->logSetup=true; |
95 | - } |
|
96 | - else |
|
95 | + } else |
|
97 | 96 | { |
98 | 97 | $this->logSetup=false; |
99 | 98 | } |
@@ -118,7 +117,10 @@ discard block |
||
118 | 117 | $this->getDatabaseOptions(); // Get options in database |
119 | 118 | |
120 | 119 | //*************** Setup API |
121 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
120 | + if ($this->apiUse === true) { |
|
121 | + $this->getAPI(); |
|
122 | + } |
|
123 | + // Setup API |
|
122 | 124 | |
123 | 125 | //*************** Setup MIB |
124 | 126 | $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
@@ -161,8 +163,7 @@ discard block |
||
161 | 163 | } |
162 | 164 | $this->logging->log($message,$log_level); |
163 | 165 | return false; |
164 | - } |
|
165 | - else |
|
166 | + } else |
|
166 | 167 | { |
167 | 168 | $option_var=$option_array[$option_category][$option_name]; |
168 | 169 | return true; |
@@ -226,7 +227,10 @@ discard block |
||
226 | 227 | |
227 | 228 | $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
228 | 229 | |
229 | - if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
230 | + if ($this->apiUse === true) { |
|
231 | + return; |
|
232 | + } |
|
233 | + // In case of API use, no IDO is necessary |
|
230 | 234 | |
231 | 235 | // IDO Database |
232 | 236 | if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
@@ -251,9 +255,11 @@ discard block |
||
251 | 255 | protected function getDatabaseOptions() |
252 | 256 | { |
253 | 257 | // Database options |
254 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
258 | + if ($this->logSetup === false) { |
|
259 | + // Only if logging was no setup in constructor |
|
255 | 260 | { |
256 | 261 | $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
262 | + } |
|
257 | 263 | $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
258 | 264 | $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
259 | 265 | } |
@@ -266,7 +272,9 @@ discard block |
||
266 | 272 | protected function getDBConfigIfSet($element,&$variable) |
267 | 273 | { |
268 | 274 | $value=$this->getDBConfig($element); |
269 | - if ($value != null) $variable=$value; |
|
275 | + if ($value != null) { |
|
276 | + $variable=$value; |
|
277 | + } |
|
270 | 278 | } |
271 | 279 | |
272 | 280 | /** |
@@ -358,8 +366,7 @@ discard block |
||
358 | 366 | { |
359 | 367 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
360 | 368 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
361 | - } |
|
362 | - else |
|
369 | + } else |
|
363 | 370 | { |
364 | 371 | $this->trapData['source_ip']=$matches[1]; |
365 | 372 | $this->trapData['destination_ip']=$matches[3]; |
@@ -374,14 +381,12 @@ discard block |
||
374 | 381 | if ($ret_code===0 || $ret_code===false) |
375 | 382 | { |
376 | 383 | $this->logging->log('No match on trap data : '.$vars,WARN,''); |
377 | - } |
|
378 | - else |
|
384 | + } else |
|
379 | 385 | { |
380 | 386 | 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')) |
381 | 387 | { |
382 | 388 | $this->trapData['trap_oid']=$matches[2]; |
383 | - } |
|
384 | - else |
|
389 | + } else |
|
385 | 390 | { |
386 | 391 | $object= new stdClass; |
387 | 392 | $object->oid =$matches[1]; |
@@ -552,7 +557,9 @@ discard block |
||
552 | 557 | } |
553 | 558 | |
554 | 559 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
555 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
560 | + if ($inserted_id==false) { |
|
561 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
562 | + } |
|
556 | 563 | $this->trapId=$inserted_id; |
557 | 564 | break; |
558 | 565 | default: |
@@ -568,7 +575,9 @@ discard block |
||
568 | 575 | { |
569 | 576 | |
570 | 577 | // If action is ignore -> don't send t DB |
571 | - if ($this->trapToDb === false) return; |
|
578 | + if ($this->trapToDb === false) { |
|
579 | + return; |
|
580 | + } |
|
572 | 581 | |
573 | 582 | |
574 | 583 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -625,7 +634,9 @@ discard block |
||
625 | 634 | } |
626 | 635 | |
627 | 636 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
628 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
637 | + if ($inserted_id==false) { |
|
638 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
639 | + } |
|
629 | 640 | $this->trapId=$inserted_id; |
630 | 641 | break; |
631 | 642 | default: |
@@ -754,8 +765,7 @@ discard block |
||
754 | 765 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
755 | 766 | exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
756 | 767 | return true; |
757 | - } |
|
758 | - else |
|
768 | + } else |
|
759 | 769 | { |
760 | 770 | // Get perfdata if found |
761 | 771 | $matches=array(); |
@@ -763,8 +773,7 @@ discard block |
||
763 | 773 | { |
764 | 774 | $display=$matches[1]; |
765 | 775 | $perfdata=$matches[2]; |
766 | - } |
|
767 | - else |
|
776 | + } else |
|
768 | 777 | { |
769 | 778 | $perfdata=''; |
770 | 779 | } |
@@ -776,8 +785,7 @@ discard block |
||
776 | 785 | { |
777 | 786 | $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
778 | 787 | return false; |
779 | - } |
|
780 | - else |
|
788 | + } else |
|
781 | 789 | { |
782 | 790 | $this->logging->log( "Sent result : " .$retmessage,INFO ); |
783 | 791 | return true; |
@@ -873,20 +881,17 @@ discard block |
||
873 | 881 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
874 | 882 | { |
875 | 883 | $this->trapAction.='Error sending status : check cmd/API'; |
876 | - } |
|
877 | - else |
|
884 | + } else |
|
878 | 885 | { |
879 | 886 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
880 | 887 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
881 | 888 | } |
882 | - } |
|
883 | - else |
|
889 | + } else |
|
884 | 890 | { |
885 | 891 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
886 | 892 | } |
887 | 893 | $this->trapToDb=($action==-2)?false:true; |
888 | - } |
|
889 | - else |
|
894 | + } else |
|
890 | 895 | { |
891 | 896 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
892 | 897 | |
@@ -897,14 +902,12 @@ discard block |
||
897 | 902 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
898 | 903 | { |
899 | 904 | $this->trapAction.='Error sending status : check cmd/API'; |
900 | - } |
|
901 | - else |
|
905 | + } else |
|
902 | 906 | { |
903 | 907 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
904 | 908 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
905 | 909 | } |
906 | - } |
|
907 | - else |
|
910 | + } else |
|
908 | 911 | { |
909 | 912 | $this->add_rule_match($rule['id'],$rule['num_match']+1); |
910 | 913 | } |
@@ -914,16 +917,14 @@ discard block |
||
914 | 917 | if (!isset($this->trapData['source_name'])) |
915 | 918 | { |
916 | 919 | $this->trapData['source_name']=$rule['host_name']; |
917 | - } |
|
918 | - else |
|
920 | + } else |
|
919 | 921 | { |
920 | 922 | if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
921 | 923 | { // only add if not present |
922 | 924 | $this->trapData['source_name'].=','.$rule['host_name']; |
923 | 925 | } |
924 | 926 | } |
925 | - } |
|
926 | - catch (Exception $e) |
|
927 | + } catch (Exception $e) |
|
927 | 928 | { |
928 | 929 | $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
929 | 930 | $this->trapAction.=' ERR : '.$e->getMessage(); |
@@ -934,8 +935,7 @@ discard block |
||
934 | 935 | if ($this->trapData['status']=='error') |
935 | 936 | { |
936 | 937 | $this->trapToDb=true; // Always put errors in DB for the use can see |
937 | - } |
|
938 | - else |
|
938 | + } else |
|
939 | 939 | { |
940 | 940 | $this->trapData['status']='done'; |
941 | 941 | } |
@@ -16,960 +16,960 @@ discard block |
||
16 | 16 | */ |
17 | 17 | class Trap |
18 | 18 | { |
19 | - // Configuration files and dirs |
|
20 | - /** @var string Icinga etc path */ |
|
21 | - protected $icingaweb2Etc; |
|
22 | - /** @var string $trapModuleConfig config.ini of module */ |
|
23 | - protected $trapModuleConfig; |
|
24 | - /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
25 | - protected $icingaweb2Ressources; |
|
26 | - // Options from config.ini (default values) |
|
27 | - /** @var string $snmptranslate */ |
|
28 | - protected $snmptranslate='/usr/bin/snmptranslate'; |
|
29 | - /** @var string $snmptranslate_dirs */ |
|
30 | - protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
31 | - /** @var string $icinga2cmd */ |
|
32 | - protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
33 | - /** @var string $dbPrefix */ |
|
34 | - protected $dbPrefix='traps_'; |
|
35 | - |
|
36 | - // API |
|
37 | - /** @var boolean $apiUse */ |
|
38 | - protected $apiUse=false; |
|
39 | - /** @var Icinga2API $icinga2api */ |
|
40 | - protected $icinga2api=null; |
|
41 | - /** @var string $apiHostname */ |
|
42 | - protected $apiHostname=''; |
|
43 | - /** @var integer $apiPort */ |
|
44 | - protected $apiPort=0; |
|
45 | - /** @var string $apiUsername */ |
|
46 | - protected $apiUsername=''; |
|
47 | - /** @var string $apiPassword */ |
|
48 | - protected $apiPassword=''; |
|
49 | - |
|
50 | - // Logs |
|
51 | - /** @var Logging Logging class. */ |
|
52 | - public $logging; //< Logging class. |
|
53 | - /** @var bool true if log was setup in constructor */ |
|
54 | - protected $logSetup; //< bool true if log was setup in constructor |
|
55 | - |
|
56 | - // Databases |
|
57 | - /** @var Database $trapsDB Database class*/ |
|
58 | - public $trapsDB = null; |
|
59 | - |
|
60 | - // Trap received data |
|
61 | - protected $receivingHost; |
|
62 | - /** @var array Main trap data (oid, source...) */ |
|
63 | - public $trapData=array(); |
|
64 | - /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
65 | - public $trapDataExt=array(); |
|
66 | - /** @var int $trapId trap_id after sql insert*/ |
|
67 | - public $trapId=null; |
|
68 | - /** @var string $trapAction trap action for final write*/ |
|
69 | - public $trapAction=null; |
|
70 | - /** @var boolean $trapToDb log trap to DB */ |
|
71 | - protected $trapToDb=true; |
|
72 | - |
|
73 | - /** @var Mib mib class */ |
|
74 | - public $mibClass = null; |
|
75 | - |
|
76 | - /** @var Rule rule class */ |
|
77 | - public $ruleClass = null; |
|
78 | - |
|
79 | - /** @var Plugins plugins manager **/ |
|
80 | - public $pluginClass = null; |
|
81 | - |
|
82 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
83 | - { |
|
84 | - // Paths of ini files |
|
85 | - $this->icingaweb2Etc=$etcDir; |
|
86 | - $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
87 | - $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
88 | - |
|
89 | - //************* Setup logging |
|
90 | - $this->logging = new Logging(); |
|
91 | - if ($baseLogLevel != null) |
|
92 | - { |
|
93 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
94 | - $this->logSetup=true; |
|
95 | - } |
|
96 | - else |
|
97 | - { |
|
98 | - $this->logSetup=false; |
|
99 | - } |
|
100 | - $this->logging->log('Loggin started', INFO); |
|
101 | - |
|
102 | - //*************** Get options from ini files |
|
103 | - if (! is_file($this->trapModuleConfig)) |
|
104 | - { |
|
105 | - throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
106 | - } |
|
107 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
108 | - if ($trapConfig == false) |
|
109 | - { |
|
110 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
111 | - throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
112 | - } |
|
113 | - $this->getMainOptions($trapConfig); // Get main options from ini file |
|
114 | - |
|
115 | - //*************** Setup database class & get options |
|
116 | - $this->setupDatabase($trapConfig); |
|
117 | - |
|
118 | - $this->getDatabaseOptions(); // Get options in database |
|
119 | - |
|
120 | - //*************** Setup API |
|
121 | - if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
122 | - |
|
123 | - //*************** Setup MIB |
|
124 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
125 | - |
|
126 | - //*************** Setup Rule |
|
127 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
128 | - |
|
129 | - $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
130 | - 'source_ip' => 'unknown', |
|
131 | - 'source_port' => 'unknown', |
|
132 | - 'destination_ip' => 'unknown', |
|
133 | - 'destination_port' => 'unknown', |
|
134 | - 'trap_oid' => 'unknown' |
|
135 | - ); |
|
136 | - |
|
137 | - //*************** Setup Plugins |
|
138 | - //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
139 | - $this->pluginClass = new Plugins($this); |
|
19 | + // Configuration files and dirs |
|
20 | + /** @var string Icinga etc path */ |
|
21 | + protected $icingaweb2Etc; |
|
22 | + /** @var string $trapModuleConfig config.ini of module */ |
|
23 | + protected $trapModuleConfig; |
|
24 | + /** @var string $icingaweb2Ressources resources.ini of icingaweb2 */ |
|
25 | + protected $icingaweb2Ressources; |
|
26 | + // Options from config.ini (default values) |
|
27 | + /** @var string $snmptranslate */ |
|
28 | + protected $snmptranslate='/usr/bin/snmptranslate'; |
|
29 | + /** @var string $snmptranslate_dirs */ |
|
30 | + protected $snmptranslate_dirs='/usr/share/icingaweb2/modules/trapdirector/mibs'; |
|
31 | + /** @var string $icinga2cmd */ |
|
32 | + protected $icinga2cmd='/var/run/icinga2/cmd/icinga2.cmd'; |
|
33 | + /** @var string $dbPrefix */ |
|
34 | + protected $dbPrefix='traps_'; |
|
35 | + |
|
36 | + // API |
|
37 | + /** @var boolean $apiUse */ |
|
38 | + protected $apiUse=false; |
|
39 | + /** @var Icinga2API $icinga2api */ |
|
40 | + protected $icinga2api=null; |
|
41 | + /** @var string $apiHostname */ |
|
42 | + protected $apiHostname=''; |
|
43 | + /** @var integer $apiPort */ |
|
44 | + protected $apiPort=0; |
|
45 | + /** @var string $apiUsername */ |
|
46 | + protected $apiUsername=''; |
|
47 | + /** @var string $apiPassword */ |
|
48 | + protected $apiPassword=''; |
|
49 | + |
|
50 | + // Logs |
|
51 | + /** @var Logging Logging class. */ |
|
52 | + public $logging; //< Logging class. |
|
53 | + /** @var bool true if log was setup in constructor */ |
|
54 | + protected $logSetup; //< bool true if log was setup in constructor |
|
55 | + |
|
56 | + // Databases |
|
57 | + /** @var Database $trapsDB Database class*/ |
|
58 | + public $trapsDB = null; |
|
59 | + |
|
60 | + // Trap received data |
|
61 | + protected $receivingHost; |
|
62 | + /** @var array Main trap data (oid, source...) */ |
|
63 | + public $trapData=array(); |
|
64 | + /** @var array $trapDataExt Additional trap data objects (oid/value).*/ |
|
65 | + public $trapDataExt=array(); |
|
66 | + /** @var int $trapId trap_id after sql insert*/ |
|
67 | + public $trapId=null; |
|
68 | + /** @var string $trapAction trap action for final write*/ |
|
69 | + public $trapAction=null; |
|
70 | + /** @var boolean $trapToDb log trap to DB */ |
|
71 | + protected $trapToDb=true; |
|
72 | + |
|
73 | + /** @var Mib mib class */ |
|
74 | + public $mibClass = null; |
|
75 | + |
|
76 | + /** @var Rule rule class */ |
|
77 | + public $ruleClass = null; |
|
78 | + |
|
79 | + /** @var Plugins plugins manager **/ |
|
80 | + public $pluginClass = null; |
|
81 | + |
|
82 | + function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
83 | + { |
|
84 | + // Paths of ini files |
|
85 | + $this->icingaweb2Etc=$etcDir; |
|
86 | + $this->trapModuleConfig=$this->icingaweb2Etc."/modules/trapdirector/config.ini"; |
|
87 | + $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
|
88 | + |
|
89 | + //************* Setup logging |
|
90 | + $this->logging = new Logging(); |
|
91 | + if ($baseLogLevel != null) |
|
92 | + { |
|
93 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
94 | + $this->logSetup=true; |
|
95 | + } |
|
96 | + else |
|
97 | + { |
|
98 | + $this->logSetup=false; |
|
99 | + } |
|
100 | + $this->logging->log('Loggin started', INFO); |
|
101 | + |
|
102 | + //*************** Get options from ini files |
|
103 | + if (! is_file($this->trapModuleConfig)) |
|
104 | + { |
|
105 | + throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
|
106 | + } |
|
107 | + $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
108 | + if ($trapConfig == false) |
|
109 | + { |
|
110 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
111 | + throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
|
112 | + } |
|
113 | + $this->getMainOptions($trapConfig); // Get main options from ini file |
|
114 | + |
|
115 | + //*************** Setup database class & get options |
|
116 | + $this->setupDatabase($trapConfig); |
|
117 | + |
|
118 | + $this->getDatabaseOptions(); // Get options in database |
|
119 | + |
|
120 | + //*************** Setup API |
|
121 | + if ($this->apiUse === true) $this->getAPI(); // Setup API |
|
122 | + |
|
123 | + //*************** Setup MIB |
|
124 | + $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
125 | + |
|
126 | + //*************** Setup Rule |
|
127 | + $this->ruleClass = new Rule($this); //< Create Rule class |
|
128 | + |
|
129 | + $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
|
130 | + 'source_ip' => 'unknown', |
|
131 | + 'source_port' => 'unknown', |
|
132 | + 'destination_ip' => 'unknown', |
|
133 | + 'destination_port' => 'unknown', |
|
134 | + 'trap_oid' => 'unknown' |
|
135 | + ); |
|
136 | + |
|
137 | + //*************** Setup Plugins |
|
138 | + //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
|
139 | + $this->pluginClass = new Plugins($this); |
|
140 | 140 | |
141 | 141 | |
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Get option from array of ini file, send message if empty |
|
146 | - * @param string $option_array Array of ini file |
|
147 | - * @param string $option_category category in ini file |
|
148 | - * @param string $option_name name of option in category |
|
149 | - * @param mixed $option_var variable to fill if found, left untouched if not found |
|
150 | - * @param integer $log_level default 2 (warning) |
|
151 | - * @param string $message warning message if not found |
|
152 | - * @return boolean true if found, or false |
|
153 | - */ |
|
154 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
155 | - { |
|
156 | - if (!isset($option_array[$option_category][$option_name])) |
|
157 | - { |
|
158 | - if ($message === null) |
|
159 | - { |
|
160 | - $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
161 | - } |
|
162 | - $this->logging->log($message,$log_level); |
|
163 | - return false; |
|
164 | - } |
|
165 | - else |
|
166 | - { |
|
167 | - $option_var=$option_array[$option_category][$option_name]; |
|
168 | - return true; |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Get options from ini file |
|
174 | - * @param array $trap_config : ini file array |
|
175 | - */ |
|
176 | - protected function getMainOptions($trapConfig) |
|
177 | - { |
|
178 | - |
|
179 | - // Snmptranslate binary path |
|
180 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
181 | - |
|
182 | - // mibs path |
|
183 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
184 | - |
|
185 | - // icinga2cmd path |
|
186 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
187 | - |
|
188 | - // table prefix |
|
189 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
190 | - |
|
191 | - // API options |
|
192 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
193 | - { |
|
194 | - $this->apiUse=true; |
|
195 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort); |
|
196 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername); |
|
197 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword); |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Create and setup database class for trap & ido (if no api) db |
|
203 | - * @param array $trap_config : ini file array |
|
204 | - */ |
|
205 | - protected function setupDatabase($trapConfig) |
|
206 | - { |
|
207 | - // Trap database |
|
208 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
209 | - { |
|
210 | - $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
211 | - return; |
|
212 | - } |
|
213 | - $dbTrapName=$trapConfig['config']['database']; |
|
214 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
215 | - |
|
216 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
217 | - { |
|
218 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
219 | - return; |
|
220 | - } |
|
221 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
222 | - { |
|
223 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
224 | - return; |
|
225 | - } |
|
226 | - |
|
227 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
228 | - |
|
229 | - if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
230 | - |
|
231 | - // IDO Database |
|
232 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
233 | - { |
|
234 | - $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
235 | - } |
|
236 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
237 | - |
|
238 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
239 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
240 | - { |
|
241 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
242 | - return; |
|
243 | - } |
|
244 | - |
|
245 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Get options in database |
|
250 | - */ |
|
251 | - protected function getDatabaseOptions() |
|
252 | - { |
|
253 | - // Database options |
|
254 | - if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
255 | - { |
|
256 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
257 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
258 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - /** Set $variable to value if $element found in database config table |
|
263 | - * @param string $element |
|
264 | - * @param string $variable |
|
265 | - */ |
|
266 | - protected function getDBConfigIfSet($element,&$variable) |
|
267 | - { |
|
268 | - $value=$this->getDBConfig($element); |
|
269 | - if ($value != null) $variable=$value; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Get data from db_config |
|
274 | - * @param $element string name of param |
|
275 | - * @return mixed : value (or null) |
|
276 | - */ |
|
277 | - protected function getDBConfig($element) // TODO : put this in DB class |
|
278 | - { |
|
279 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
280 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
281 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
282 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
283 | - return null; |
|
284 | - } |
|
285 | - $value=$ret_code->fetch(); |
|
286 | - if ($value != null && isset($value['value'])) |
|
287 | - { |
|
288 | - return $value['value']; |
|
289 | - } |
|
290 | - return null; |
|
291 | - } |
|
292 | - |
|
293 | - /** OBSOLETE Send log. Throws exception on critical error |
|
294 | - * @param string $message Message to log |
|
295 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
296 | - * @param string $destination file/syslog/display |
|
297 | - * @return void |
|
298 | - **/ |
|
299 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
300 | - { |
|
301 | - // TODO : replace ref with $this->logging->log |
|
302 | - $this->logging->log($message, $level, $destination); |
|
303 | - } |
|
304 | - |
|
305 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
306 | - { |
|
307 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Returns or create new IcingaAPI object |
|
312 | - * @return \Icinga\Module\Trapdirector\Icinga2API |
|
313 | - */ |
|
314 | - protected function getAPI() |
|
315 | - { |
|
316 | - if ($this->icinga2api == null) |
|
317 | - { |
|
318 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
319 | - } |
|
320 | - return $this->icinga2api; |
|
321 | - } |
|
322 | - |
|
323 | - |
|
324 | - /** |
|
325 | - * read data from stream |
|
326 | - * @param $stream string input stream, defaults to "php://stdin" |
|
327 | - * @return mixed array trap data or exception with error |
|
328 | - */ |
|
329 | - public function read_trap($stream='php://stdin') |
|
330 | - { |
|
331 | - //Read data from snmptrapd from stdin |
|
332 | - $input_stream=fopen($stream, 'r'); |
|
333 | - |
|
334 | - if ($input_stream === false) |
|
335 | - { |
|
336 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
337 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
338 | - return null; // note : exception thrown by logging |
|
339 | - } |
|
340 | - |
|
341 | - // line 1 : host |
|
342 | - $this->receivingHost=chop(fgets($input_stream)); |
|
343 | - if ($this->receivingHost === false) |
|
344 | - { |
|
345 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
346 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
347 | - } |
|
348 | - // line 2 IP:port=>IP:port |
|
349 | - $IP=chop(fgets($input_stream)); |
|
350 | - if ($IP === false) |
|
351 | - { |
|
352 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
353 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
354 | - } |
|
355 | - $matches=array(); |
|
356 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
357 | - if ($ret_code===0 || $ret_code===false) |
|
358 | - { |
|
359 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
360 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
361 | - } |
|
362 | - else |
|
363 | - { |
|
364 | - $this->trapData['source_ip']=$matches[1]; |
|
365 | - $this->trapData['destination_ip']=$matches[3]; |
|
366 | - $this->trapData['source_port']=$matches[2]; |
|
367 | - $this->trapData['destination_port']=$matches[4]; |
|
368 | - } |
|
369 | - |
|
370 | - while (($vars=fgets($input_stream)) !==false) |
|
371 | - { |
|
372 | - $vars=chop($vars); |
|
373 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
374 | - if ($ret_code===0 || $ret_code===false) |
|
375 | - { |
|
376 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
377 | - } |
|
378 | - else |
|
379 | - { |
|
380 | - 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')) |
|
381 | - { |
|
382 | - $this->trapData['trap_oid']=$matches[2]; |
|
383 | - } |
|
384 | - else |
|
385 | - { |
|
386 | - $object= new stdClass; |
|
387 | - $object->oid =$matches[1]; |
|
388 | - $object->value = $matches[2]; |
|
389 | - array_push($this->trapDataExt,$object); |
|
390 | - } |
|
391 | - } |
|
392 | - } |
|
393 | - |
|
394 | - if ($this->trapData['trap_oid']=='unknown') |
|
395 | - { |
|
396 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
397 | - $this->logging->log('no trap oid found',ERROR,''); |
|
398 | - } |
|
399 | - |
|
400 | - // Translate oids. |
|
401 | - |
|
402 | - $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
403 | - if ($retArray != null) |
|
404 | - { |
|
405 | - $this->trapData['trap_name']=$retArray['trap_name']; |
|
406 | - $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
407 | - } |
|
408 | - foreach ($this->trapDataExt as $key => $val) |
|
409 | - { |
|
410 | - $retArray=$this->translateOID($val->oid); |
|
411 | - if ($retArray != null) |
|
412 | - { |
|
413 | - $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
414 | - $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
415 | - } |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - $this->trapData['status']= 'waiting'; |
|
420 | - |
|
421 | - return $this->trapData; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Translate oid into array(MIB,Name) |
|
426 | - * @param $oid string oid to translate |
|
427 | - * @return mixed : null if not found or array(MIB,Name) |
|
428 | - */ |
|
429 | - public function translateOID($oid) |
|
430 | - { |
|
431 | - // try from database |
|
432 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
433 | - |
|
434 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
435 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
436 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
437 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
438 | - } |
|
439 | - $name=$ret_code->fetch(); |
|
440 | - if ($name['name'] != null) |
|
441 | - { |
|
442 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
443 | - } |
|
444 | - |
|
445 | - // Also check if it is an instance of OID |
|
446 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
447 | - |
|
448 | - $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
449 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
450 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
451 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
452 | - } |
|
453 | - $name=$ret_code->fetch(); |
|
454 | - if ($name['name'] != null) |
|
455 | - { |
|
456 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
457 | - } |
|
458 | - |
|
459 | - // Try to get oid name from snmptranslate |
|
460 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
461 | - ' '.$oid); |
|
462 | - $matches=array(); |
|
463 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
464 | - if ($ret_code===0 || $ret_code === false) { |
|
465 | - return NULL; |
|
466 | - } else { |
|
467 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
468 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
469 | - } |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Erase old trap records |
|
474 | - * @param integer $days : erase traps when more than $days old |
|
475 | - * @return integer : number of lines deleted |
|
476 | - **/ |
|
477 | - public function eraseOldTraps($days=0) |
|
478 | - { |
|
479 | - if ($days==0) |
|
480 | - { |
|
481 | - if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
482 | - { |
|
483 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
484 | - return; |
|
485 | - } |
|
486 | - } |
|
487 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
488 | - $daysago = strtotime("-".$days." day"); |
|
489 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
490 | - if ($db_conn->query($sql) === false) { |
|
491 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
492 | - } |
|
493 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
494 | - } |
|
495 | - |
|
496 | - /** Write error to received trap database |
|
497 | - */ |
|
498 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
499 | - { |
|
500 | - |
|
501 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
502 | - |
|
503 | - // add date time |
|
504 | - $insert_col ='date_received,status'; |
|
505 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
506 | - |
|
507 | - if ($sourceIP !=null) |
|
508 | - { |
|
509 | - $insert_col .=',source_ip'; |
|
510 | - $insert_val .=",'". $sourceIP ."'"; |
|
511 | - } |
|
512 | - if ($trapoid !=null) |
|
513 | - { |
|
514 | - $insert_col .=',trap_oid'; |
|
515 | - $insert_val .=",'". $trapoid ."'"; |
|
516 | - } |
|
517 | - $insert_col .=',status_detail'; |
|
518 | - $insert_val .=",'". $message ."'"; |
|
519 | - |
|
520 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
521 | - |
|
522 | - switch ($this->trapsDB->trapDBType) |
|
523 | - { |
|
524 | - case 'pgsql': |
|
525 | - $sql .= ' RETURNING id;'; |
|
526 | - $this->logging->log('sql : '.$sql,INFO); |
|
527 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
528 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
529 | - } |
|
530 | - $this->logging->log('SQL insertion OK',INFO ); |
|
531 | - // Get last id to insert oid/values in secondary table |
|
532 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Get option from array of ini file, send message if empty |
|
146 | + * @param string $option_array Array of ini file |
|
147 | + * @param string $option_category category in ini file |
|
148 | + * @param string $option_name name of option in category |
|
149 | + * @param mixed $option_var variable to fill if found, left untouched if not found |
|
150 | + * @param integer $log_level default 2 (warning) |
|
151 | + * @param string $message warning message if not found |
|
152 | + * @return boolean true if found, or false |
|
153 | + */ |
|
154 | + protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
155 | + { |
|
156 | + if (!isset($option_array[$option_category][$option_name])) |
|
157 | + { |
|
158 | + if ($message === null) |
|
159 | + { |
|
160 | + $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
161 | + } |
|
162 | + $this->logging->log($message,$log_level); |
|
163 | + return false; |
|
164 | + } |
|
165 | + else |
|
166 | + { |
|
167 | + $option_var=$option_array[$option_category][$option_name]; |
|
168 | + return true; |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Get options from ini file |
|
174 | + * @param array $trap_config : ini file array |
|
175 | + */ |
|
176 | + protected function getMainOptions($trapConfig) |
|
177 | + { |
|
178 | + |
|
179 | + // Snmptranslate binary path |
|
180 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
181 | + |
|
182 | + // mibs path |
|
183 | + $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
184 | + |
|
185 | + // icinga2cmd path |
|
186 | + $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
187 | + |
|
188 | + // table prefix |
|
189 | + $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
190 | + |
|
191 | + // API options |
|
192 | + if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
193 | + { |
|
194 | + $this->apiUse=true; |
|
195 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort); |
|
196 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername); |
|
197 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword); |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Create and setup database class for trap & ido (if no api) db |
|
203 | + * @param array $trap_config : ini file array |
|
204 | + */ |
|
205 | + protected function setupDatabase($trapConfig) |
|
206 | + { |
|
207 | + // Trap database |
|
208 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
209 | + { |
|
210 | + $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
211 | + return; |
|
212 | + } |
|
213 | + $dbTrapName=$trapConfig['config']['database']; |
|
214 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
215 | + |
|
216 | + if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
217 | + { |
|
218 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
219 | + return; |
|
220 | + } |
|
221 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
222 | + { |
|
223 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
224 | + return; |
|
225 | + } |
|
226 | + |
|
227 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
228 | + |
|
229 | + if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
|
230 | + |
|
231 | + // IDO Database |
|
232 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
233 | + { |
|
234 | + $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
235 | + } |
|
236 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
237 | + |
|
238 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
239 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
240 | + { |
|
241 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
242 | + return; |
|
243 | + } |
|
244 | + |
|
245 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Get options in database |
|
250 | + */ |
|
251 | + protected function getDatabaseOptions() |
|
252 | + { |
|
253 | + // Database options |
|
254 | + if ($this->logSetup === false) // Only if logging was no setup in constructor |
|
255 | + { |
|
256 | + $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
257 | + $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
258 | + $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + /** Set $variable to value if $element found in database config table |
|
263 | + * @param string $element |
|
264 | + * @param string $variable |
|
265 | + */ |
|
266 | + protected function getDBConfigIfSet($element,&$variable) |
|
267 | + { |
|
268 | + $value=$this->getDBConfig($element); |
|
269 | + if ($value != null) $variable=$value; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Get data from db_config |
|
274 | + * @param $element string name of param |
|
275 | + * @return mixed : value (or null) |
|
276 | + */ |
|
277 | + protected function getDBConfig($element) // TODO : put this in DB class |
|
278 | + { |
|
279 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
280 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
|
281 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
282 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
283 | + return null; |
|
284 | + } |
|
285 | + $value=$ret_code->fetch(); |
|
286 | + if ($value != null && isset($value['value'])) |
|
287 | + { |
|
288 | + return $value['value']; |
|
289 | + } |
|
290 | + return null; |
|
291 | + } |
|
292 | + |
|
293 | + /** OBSOLETE Send log. Throws exception on critical error |
|
294 | + * @param string $message Message to log |
|
295 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
296 | + * @param string $destination file/syslog/display |
|
297 | + * @return void |
|
298 | + **/ |
|
299 | + public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
300 | + { |
|
301 | + // TODO : replace ref with $this->logging->log |
|
302 | + $this->logging->log($message, $level, $destination); |
|
303 | + } |
|
304 | + |
|
305 | + public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
306 | + { |
|
307 | + $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Returns or create new IcingaAPI object |
|
312 | + * @return \Icinga\Module\Trapdirector\Icinga2API |
|
313 | + */ |
|
314 | + protected function getAPI() |
|
315 | + { |
|
316 | + if ($this->icinga2api == null) |
|
317 | + { |
|
318 | + $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
319 | + } |
|
320 | + return $this->icinga2api; |
|
321 | + } |
|
322 | + |
|
323 | + |
|
324 | + /** |
|
325 | + * read data from stream |
|
326 | + * @param $stream string input stream, defaults to "php://stdin" |
|
327 | + * @return mixed array trap data or exception with error |
|
328 | + */ |
|
329 | + public function read_trap($stream='php://stdin') |
|
330 | + { |
|
331 | + //Read data from snmptrapd from stdin |
|
332 | + $input_stream=fopen($stream, 'r'); |
|
333 | + |
|
334 | + if ($input_stream === false) |
|
335 | + { |
|
336 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
337 | + $this->logging->log("Error reading stdin !",ERROR,''); |
|
338 | + return null; // note : exception thrown by logging |
|
339 | + } |
|
340 | + |
|
341 | + // line 1 : host |
|
342 | + $this->receivingHost=chop(fgets($input_stream)); |
|
343 | + if ($this->receivingHost === false) |
|
344 | + { |
|
345 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
346 | + $this->logging->log("Error reading Host !",ERROR,''); |
|
347 | + } |
|
348 | + // line 2 IP:port=>IP:port |
|
349 | + $IP=chop(fgets($input_stream)); |
|
350 | + if ($IP === false) |
|
351 | + { |
|
352 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
353 | + $this->logging->log("Error reading IP !",ERROR,''); |
|
354 | + } |
|
355 | + $matches=array(); |
|
356 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
357 | + if ($ret_code===0 || $ret_code===false) |
|
358 | + { |
|
359 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
360 | + $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
361 | + } |
|
362 | + else |
|
363 | + { |
|
364 | + $this->trapData['source_ip']=$matches[1]; |
|
365 | + $this->trapData['destination_ip']=$matches[3]; |
|
366 | + $this->trapData['source_port']=$matches[2]; |
|
367 | + $this->trapData['destination_port']=$matches[4]; |
|
368 | + } |
|
369 | + |
|
370 | + while (($vars=fgets($input_stream)) !==false) |
|
371 | + { |
|
372 | + $vars=chop($vars); |
|
373 | + $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
374 | + if ($ret_code===0 || $ret_code===false) |
|
375 | + { |
|
376 | + $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
377 | + } |
|
378 | + else |
|
379 | + { |
|
380 | + 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')) |
|
381 | + { |
|
382 | + $this->trapData['trap_oid']=$matches[2]; |
|
383 | + } |
|
384 | + else |
|
385 | + { |
|
386 | + $object= new stdClass; |
|
387 | + $object->oid =$matches[1]; |
|
388 | + $object->value = $matches[2]; |
|
389 | + array_push($this->trapDataExt,$object); |
|
390 | + } |
|
391 | + } |
|
392 | + } |
|
393 | + |
|
394 | + if ($this->trapData['trap_oid']=='unknown') |
|
395 | + { |
|
396 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
397 | + $this->logging->log('no trap oid found',ERROR,''); |
|
398 | + } |
|
399 | + |
|
400 | + // Translate oids. |
|
401 | + |
|
402 | + $retArray=$this->translateOID($this->trapData['trap_oid']); |
|
403 | + if ($retArray != null) |
|
404 | + { |
|
405 | + $this->trapData['trap_name']=$retArray['trap_name']; |
|
406 | + $this->trapData['trap_name_mib']=$retArray['trap_name_mib']; |
|
407 | + } |
|
408 | + foreach ($this->trapDataExt as $key => $val) |
|
409 | + { |
|
410 | + $retArray=$this->translateOID($val->oid); |
|
411 | + if ($retArray != null) |
|
412 | + { |
|
413 | + $this->trapDataExt[$key]->oid_name=$retArray['trap_name']; |
|
414 | + $this->trapDataExt[$key]->oid_name_mib=$retArray['trap_name_mib']; |
|
415 | + } |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + $this->trapData['status']= 'waiting'; |
|
420 | + |
|
421 | + return $this->trapData; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Translate oid into array(MIB,Name) |
|
426 | + * @param $oid string oid to translate |
|
427 | + * @return mixed : null if not found or array(MIB,Name) |
|
428 | + */ |
|
429 | + public function translateOID($oid) |
|
430 | + { |
|
431 | + // try from database |
|
432 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
433 | + |
|
434 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
|
435 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
436 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
437 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
438 | + } |
|
439 | + $name=$ret_code->fetch(); |
|
440 | + if ($name['name'] != null) |
|
441 | + { |
|
442 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
443 | + } |
|
444 | + |
|
445 | + // Also check if it is an instance of OID |
|
446 | + $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
447 | + |
|
448 | + $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
|
449 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
450 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
451 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
452 | + } |
|
453 | + $name=$ret_code->fetch(); |
|
454 | + if ($name['name'] != null) |
|
455 | + { |
|
456 | + return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
457 | + } |
|
458 | + |
|
459 | + // Try to get oid name from snmptranslate |
|
460 | + $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
461 | + ' '.$oid); |
|
462 | + $matches=array(); |
|
463 | + $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
464 | + if ($ret_code===0 || $ret_code === false) { |
|
465 | + return NULL; |
|
466 | + } else { |
|
467 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
468 | + return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
469 | + } |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Erase old trap records |
|
474 | + * @param integer $days : erase traps when more than $days old |
|
475 | + * @return integer : number of lines deleted |
|
476 | + **/ |
|
477 | + public function eraseOldTraps($days=0) |
|
478 | + { |
|
479 | + if ($days==0) |
|
480 | + { |
|
481 | + if (($days=$this->getDBConfig('db_remove_days')) == null) |
|
482 | + { |
|
483 | + $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
484 | + return; |
|
485 | + } |
|
486 | + } |
|
487 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
488 | + $daysago = strtotime("-".$days." day"); |
|
489 | + $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
490 | + if ($db_conn->query($sql) === false) { |
|
491 | + $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
492 | + } |
|
493 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
494 | + } |
|
495 | + |
|
496 | + /** Write error to received trap database |
|
497 | + */ |
|
498 | + public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
499 | + { |
|
500 | + |
|
501 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
502 | + |
|
503 | + // add date time |
|
504 | + $insert_col ='date_received,status'; |
|
505 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
506 | + |
|
507 | + if ($sourceIP !=null) |
|
508 | + { |
|
509 | + $insert_col .=',source_ip'; |
|
510 | + $insert_val .=",'". $sourceIP ."'"; |
|
511 | + } |
|
512 | + if ($trapoid !=null) |
|
513 | + { |
|
514 | + $insert_col .=',trap_oid'; |
|
515 | + $insert_val .=",'". $trapoid ."'"; |
|
516 | + } |
|
517 | + $insert_col .=',status_detail'; |
|
518 | + $insert_val .=",'". $message ."'"; |
|
519 | + |
|
520 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
521 | + |
|
522 | + switch ($this->trapsDB->trapDBType) |
|
523 | + { |
|
524 | + case 'pgsql': |
|
525 | + $sql .= ' RETURNING id;'; |
|
526 | + $this->logging->log('sql : '.$sql,INFO); |
|
527 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
528 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
529 | + } |
|
530 | + $this->logging->log('SQL insertion OK',INFO ); |
|
531 | + // Get last id to insert oid/values in secondary table |
|
532 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
533 | 533 | |
534 | - $this->logging->log('Erreur recuperation id',1,''); |
|
535 | - } |
|
536 | - if (! isset($inserted_id_ret['id'])) { |
|
537 | - $this->logging->log('Error getting id',1,''); |
|
538 | - } |
|
539 | - $this->trapId=$inserted_id_ret['id']; |
|
540 | - break; |
|
541 | - case 'mysql': |
|
542 | - $sql .= ';'; |
|
543 | - $this->logging->log('sql : '.$sql,INFO ); |
|
544 | - if ($db_conn->query($sql) === false) { |
|
545 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
546 | - } |
|
547 | - $this->logging->log('SQL insertion OK',INFO ); |
|
548 | - // Get last id to insert oid/values in secondary table |
|
549 | - $sql='SELECT LAST_INSERT_ID();'; |
|
550 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
551 | - $this->logging->log('Erreur recuperation id',1,''); |
|
552 | - } |
|
534 | + $this->logging->log('Erreur recuperation id',1,''); |
|
535 | + } |
|
536 | + if (! isset($inserted_id_ret['id'])) { |
|
537 | + $this->logging->log('Error getting id',1,''); |
|
538 | + } |
|
539 | + $this->trapId=$inserted_id_ret['id']; |
|
540 | + break; |
|
541 | + case 'mysql': |
|
542 | + $sql .= ';'; |
|
543 | + $this->logging->log('sql : '.$sql,INFO ); |
|
544 | + if ($db_conn->query($sql) === false) { |
|
545 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
546 | + } |
|
547 | + $this->logging->log('SQL insertion OK',INFO ); |
|
548 | + // Get last id to insert oid/values in secondary table |
|
549 | + $sql='SELECT LAST_INSERT_ID();'; |
|
550 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
551 | + $this->logging->log('Erreur recuperation id',1,''); |
|
552 | + } |
|
553 | 553 | |
554 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
555 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
556 | - $this->trapId=$inserted_id; |
|
557 | - break; |
|
558 | - default: |
|
559 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
560 | - } |
|
561 | - |
|
562 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
563 | - } |
|
564 | - |
|
565 | - /** Write trap data to trap database |
|
566 | - */ |
|
567 | - public function writeTrapToDB() |
|
568 | - { |
|
569 | - |
|
570 | - // If action is ignore -> don't send t DB |
|
571 | - if ($this->trapToDb === false) return; |
|
572 | - |
|
573 | - |
|
574 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
575 | - |
|
576 | - $insert_col=''; |
|
577 | - $insert_val=''; |
|
578 | - // add date time |
|
579 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
580 | - |
|
581 | - $firstcol=1; |
|
582 | - foreach ($this->trapData as $col => $val) |
|
583 | - { |
|
584 | - if ($firstcol==0) |
|
585 | - { |
|
586 | - $insert_col .=','; |
|
587 | - $insert_val .=','; |
|
588 | - } |
|
589 | - $insert_col .= $col ; |
|
590 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
591 | - $firstcol=0; |
|
592 | - } |
|
593 | - |
|
594 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
595 | - switch ($this->trapsDB->trapDBType) |
|
596 | - { |
|
597 | - case 'pgsql': |
|
598 | - $sql .= ' RETURNING id;'; |
|
599 | - $this->logging->log('sql : '.$sql,INFO ); |
|
600 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
601 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
602 | - } |
|
603 | - $this->logging->log('SQL insertion OK',INFO ); |
|
604 | - // Get last id to insert oid/values in secondary table |
|
605 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
554 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
555 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
556 | + $this->trapId=$inserted_id; |
|
557 | + break; |
|
558 | + default: |
|
559 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
560 | + } |
|
561 | + |
|
562 | + $this->logging->log('id found: '. $this->trapId,INFO ); |
|
563 | + } |
|
564 | + |
|
565 | + /** Write trap data to trap database |
|
566 | + */ |
|
567 | + public function writeTrapToDB() |
|
568 | + { |
|
569 | + |
|
570 | + // If action is ignore -> don't send t DB |
|
571 | + if ($this->trapToDb === false) return; |
|
572 | + |
|
573 | + |
|
574 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
575 | + |
|
576 | + $insert_col=''; |
|
577 | + $insert_val=''; |
|
578 | + // add date time |
|
579 | + $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
580 | + |
|
581 | + $firstcol=1; |
|
582 | + foreach ($this->trapData as $col => $val) |
|
583 | + { |
|
584 | + if ($firstcol==0) |
|
585 | + { |
|
586 | + $insert_col .=','; |
|
587 | + $insert_val .=','; |
|
588 | + } |
|
589 | + $insert_col .= $col ; |
|
590 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
591 | + $firstcol=0; |
|
592 | + } |
|
593 | + |
|
594 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
595 | + switch ($this->trapsDB->trapDBType) |
|
596 | + { |
|
597 | + case 'pgsql': |
|
598 | + $sql .= ' RETURNING id;'; |
|
599 | + $this->logging->log('sql : '.$sql,INFO ); |
|
600 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
601 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
602 | + } |
|
603 | + $this->logging->log('SQL insertion OK',INFO ); |
|
604 | + // Get last id to insert oid/values in secondary table |
|
605 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
606 | 606 | |
607 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
608 | - } |
|
609 | - if (! isset($inserted_id_ret['id'])) { |
|
610 | - $this->logging->log('Error getting id',ERROR,''); |
|
611 | - } |
|
612 | - $this->trapId=$inserted_id_ret['id']; |
|
613 | - break; |
|
614 | - case 'mysql': |
|
615 | - $sql .= ';'; |
|
616 | - $this->logging->log('sql : '.$sql,INFO ); |
|
617 | - if ($db_conn->query($sql) === false) { |
|
618 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
619 | - } |
|
620 | - $this->logging->log('SQL insertion OK',INFO ); |
|
621 | - // Get last id to insert oid/values in secondary table |
|
622 | - $sql='SELECT LAST_INSERT_ID();'; |
|
623 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
624 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
625 | - } |
|
607 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
608 | + } |
|
609 | + if (! isset($inserted_id_ret['id'])) { |
|
610 | + $this->logging->log('Error getting id',ERROR,''); |
|
611 | + } |
|
612 | + $this->trapId=$inserted_id_ret['id']; |
|
613 | + break; |
|
614 | + case 'mysql': |
|
615 | + $sql .= ';'; |
|
616 | + $this->logging->log('sql : '.$sql,INFO ); |
|
617 | + if ($db_conn->query($sql) === false) { |
|
618 | + $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
619 | + } |
|
620 | + $this->logging->log('SQL insertion OK',INFO ); |
|
621 | + // Get last id to insert oid/values in secondary table |
|
622 | + $sql='SELECT LAST_INSERT_ID();'; |
|
623 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
624 | + $this->logging->log('Erreur recuperation id',ERROR,''); |
|
625 | + } |
|
626 | 626 | |
627 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
628 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
629 | - $this->trapId=$inserted_id; |
|
630 | - break; |
|
631 | - default: |
|
632 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
633 | - } |
|
634 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
635 | - |
|
636 | - // Fill trap extended data table |
|
637 | - foreach ($this->trapDataExt as $value) { |
|
638 | - // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
639 | - $firstcol=1; |
|
640 | - $value->trap_id = $this->trapId; |
|
641 | - $insert_col=''; |
|
642 | - $insert_val=''; |
|
643 | - foreach ($value as $col => $val) |
|
644 | - { |
|
645 | - if ($firstcol==0) |
|
646 | - { |
|
647 | - $insert_col .=','; |
|
648 | - $insert_val .=','; |
|
649 | - } |
|
650 | - $insert_col .= $col; |
|
651 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
652 | - $firstcol=0; |
|
653 | - } |
|
627 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
628 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
629 | + $this->trapId=$inserted_id; |
|
630 | + break; |
|
631 | + default: |
|
632 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
633 | + } |
|
634 | + $this->logging->log('id found: '.$this->trapId,INFO ); |
|
635 | + |
|
636 | + // Fill trap extended data table |
|
637 | + foreach ($this->trapDataExt as $value) { |
|
638 | + // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
|
639 | + $firstcol=1; |
|
640 | + $value->trap_id = $this->trapId; |
|
641 | + $insert_col=''; |
|
642 | + $insert_val=''; |
|
643 | + foreach ($value as $col => $val) |
|
644 | + { |
|
645 | + if ($firstcol==0) |
|
646 | + { |
|
647 | + $insert_col .=','; |
|
648 | + $insert_val .=','; |
|
649 | + } |
|
650 | + $insert_col .= $col; |
|
651 | + $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
652 | + $firstcol=0; |
|
653 | + } |
|
654 | 654 | |
655 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
655 | + $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
656 | 656 | |
657 | - if ($db_conn->query($sql) === false) { |
|
658 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
659 | - } |
|
660 | - } |
|
661 | - } |
|
662 | - |
|
663 | - /** Get rules from rule database with ip and oid |
|
664 | - * @param $ip string ipv4 or ipv6 |
|
665 | - * @param $oid string oid in numeric |
|
666 | - * @return mixed|boolean : PDO object or false |
|
667 | - */ |
|
668 | - protected function getRules($ip,$oid) |
|
669 | - { |
|
670 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
671 | - // fetch rules based on IP in rule and OID |
|
672 | - $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
673 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
674 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
675 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
676 | - return false; |
|
677 | - } |
|
678 | - $rules_all=$ret_code->fetchAll(); |
|
679 | - //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
680 | - $rules_ret=array(); |
|
681 | - $rule_ret_key=0; |
|
682 | - foreach ($rules_all as $key => $rule) |
|
683 | - { |
|
684 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
685 | - { |
|
686 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
687 | - //TODO : get host name by API (and check if correct in rule). |
|
688 | - $rule_ret_key++; |
|
689 | - continue; |
|
690 | - } |
|
691 | - // TODO : get hosts IP by API |
|
692 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
693 | - { // get ips of group members by oid |
|
694 | - $db_conn2=$this->trapsDB->db_connect_ido(); |
|
695 | - $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
657 | + if ($db_conn->query($sql) === false) { |
|
658 | + $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
659 | + } |
|
660 | + } |
|
661 | + } |
|
662 | + |
|
663 | + /** Get rules from rule database with ip and oid |
|
664 | + * @param $ip string ipv4 or ipv6 |
|
665 | + * @param $oid string oid in numeric |
|
666 | + * @return mixed|boolean : PDO object or false |
|
667 | + */ |
|
668 | + protected function getRules($ip,$oid) |
|
669 | + { |
|
670 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
671 | + // fetch rules based on IP in rule and OID |
|
672 | + $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
|
673 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
674 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
675 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
676 | + return false; |
|
677 | + } |
|
678 | + $rules_all=$ret_code->fetchAll(); |
|
679 | + //echo "rule all :\n";print_r($rules_all);echo "\n"; |
|
680 | + $rules_ret=array(); |
|
681 | + $rule_ret_key=0; |
|
682 | + foreach ($rules_all as $key => $rule) |
|
683 | + { |
|
684 | + if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
685 | + { |
|
686 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
687 | + //TODO : get host name by API (and check if correct in rule). |
|
688 | + $rule_ret_key++; |
|
689 | + continue; |
|
690 | + } |
|
691 | + // TODO : get hosts IP by API |
|
692 | + if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
693 | + { // get ips of group members by oid |
|
694 | + $db_conn2=$this->trapsDB->db_connect_ido(); |
|
695 | + $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
|
696 | 696 | FROM icinga_objects as o |
697 | 697 | LEFT JOIN icinga_hostgroups as h ON o.object_id=h.hostgroup_object_id |
698 | 698 | LEFT JOIN icinga_hostgroup_members as m ON h.hostgroup_id=m.hostgroup_id |
699 | 699 | LEFT JOIN icinga_hosts as a ON a.host_object_id = m.host_object_id |
700 | 700 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
701 | 701 | WHERE o.name1='".$rule['host_group_name']."';"; |
702 | - if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
703 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
704 | - continue; |
|
705 | - } |
|
706 | - $grouphosts=$ret_code2->fetchAll(); |
|
707 | - //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
708 | - foreach ( $grouphosts as $host) |
|
709 | - { |
|
710 | - //echo $host['ip4']."\n"; |
|
711 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
712 | - { |
|
713 | - //echo "Rule added \n"; |
|
714 | - $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
715 | - $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
716 | - $rule_ret_key++; |
|
717 | - } |
|
718 | - } |
|
719 | - } |
|
720 | - } |
|
721 | - //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
722 | - return $rules_ret; |
|
723 | - } |
|
724 | - |
|
725 | - /** Add rule match to rule |
|
726 | - * @param id int : rule id |
|
727 | - * @param set int : value to set |
|
728 | - */ |
|
729 | - protected function add_rule_match($id, $set) |
|
730 | - { |
|
731 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
732 | - $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
733 | - if ($db_conn->query($sql) === false) { |
|
734 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
735 | - } |
|
736 | - } |
|
737 | - |
|
738 | - /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
739 | - * |
|
740 | - * @param string $host |
|
741 | - * @param string $service |
|
742 | - * @param integer $state numerical staus |
|
743 | - * @param string $display |
|
744 | - * @returnn bool true is service check was sent without error |
|
745 | - */ |
|
746 | - public function serviceCheckResult($host,$service,$state,$display) |
|
747 | - { |
|
748 | - if ($this->apiUse === false) |
|
749 | - { |
|
750 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
751 | - $host.';' .$service .';' . $state . ';'.$display; |
|
752 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
702 | + if (($ret_code2=$db_conn2->query($sql)) === false) { |
|
703 | + $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
704 | + continue; |
|
705 | + } |
|
706 | + $grouphosts=$ret_code2->fetchAll(); |
|
707 | + //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
|
708 | + foreach ( $grouphosts as $host) |
|
709 | + { |
|
710 | + //echo $host['ip4']."\n"; |
|
711 | + if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
712 | + { |
|
713 | + //echo "Rule added \n"; |
|
714 | + $rules_ret[$rule_ret_key]=$rules_all[$key]; |
|
715 | + $rules_ret[$rule_ret_key]['host_name']=$host['host_name']; |
|
716 | + $rule_ret_key++; |
|
717 | + } |
|
718 | + } |
|
719 | + } |
|
720 | + } |
|
721 | + //echo "rule rest :\n";print_r($rules_ret);echo "\n";exit(0); |
|
722 | + return $rules_ret; |
|
723 | + } |
|
724 | + |
|
725 | + /** Add rule match to rule |
|
726 | + * @param id int : rule id |
|
727 | + * @param set int : value to set |
|
728 | + */ |
|
729 | + protected function add_rule_match($id, $set) |
|
730 | + { |
|
731 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
732 | + $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
|
733 | + if ($db_conn->query($sql) === false) { |
|
734 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
735 | + } |
|
736 | + } |
|
737 | + |
|
738 | + /** Send SERVICE_CHECK_RESULT with icinga2cmd or API |
|
739 | + * |
|
740 | + * @param string $host |
|
741 | + * @param string $service |
|
742 | + * @param integer $state numerical staus |
|
743 | + * @param string $display |
|
744 | + * @returnn bool true is service check was sent without error |
|
745 | + */ |
|
746 | + public function serviceCheckResult($host,$service,$state,$display) |
|
747 | + { |
|
748 | + if ($this->apiUse === false) |
|
749 | + { |
|
750 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
751 | + $host.';' .$service .';' . $state . ';'.$display; |
|
752 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
753 | 753 | |
754 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
755 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
756 | - return true; |
|
757 | - } |
|
758 | - else |
|
759 | - { |
|
760 | - // Get perfdata if found |
|
761 | - $matches=array(); |
|
762 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
763 | - { |
|
764 | - $display=$matches[1]; |
|
765 | - $perfdata=$matches[2]; |
|
766 | - } |
|
767 | - else |
|
768 | - { |
|
769 | - $perfdata=''; |
|
770 | - } |
|
754 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
755 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
756 | + return true; |
|
757 | + } |
|
758 | + else |
|
759 | + { |
|
760 | + // Get perfdata if found |
|
761 | + $matches=array(); |
|
762 | + if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
763 | + { |
|
764 | + $display=$matches[1]; |
|
765 | + $perfdata=$matches[2]; |
|
766 | + } |
|
767 | + else |
|
768 | + { |
|
769 | + $perfdata=''; |
|
770 | + } |
|
771 | 771 | |
772 | - $api = $this->getAPI(); |
|
773 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
774 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
775 | - if ($retcode == false) |
|
776 | - { |
|
777 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
778 | - return false; |
|
779 | - } |
|
780 | - else |
|
781 | - { |
|
782 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
783 | - return true; |
|
784 | - } |
|
785 | - } |
|
786 | - } |
|
787 | - |
|
788 | - public function getHostByIP($ip) |
|
789 | - { |
|
790 | - $api = $this->getAPI(); |
|
791 | - $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
792 | - return $api->getHostByIP($ip); |
|
793 | - } |
|
794 | - |
|
795 | - /** Resolve display. |
|
796 | - * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
797 | - * @param $display string |
|
798 | - * @return string display |
|
799 | - */ |
|
800 | - protected function applyDisplay($display) |
|
801 | - { |
|
802 | - $matches=array(); |
|
803 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
804 | - { |
|
805 | - $oid=$matches[1]; |
|
806 | - $found=0; |
|
807 | - // Test and transform regexp |
|
808 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
772 | + $api = $this->getAPI(); |
|
773 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
774 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
775 | + if ($retcode == false) |
|
776 | + { |
|
777 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
778 | + return false; |
|
779 | + } |
|
780 | + else |
|
781 | + { |
|
782 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
783 | + return true; |
|
784 | + } |
|
785 | + } |
|
786 | + } |
|
787 | + |
|
788 | + public function getHostByIP($ip) |
|
789 | + { |
|
790 | + $api = $this->getAPI(); |
|
791 | + $api->setCredentials($this->apiUsername, $this->apiPassword); |
|
792 | + return $api->getHostByIP($ip); |
|
793 | + } |
|
794 | + |
|
795 | + /** Resolve display. |
|
796 | + * Changes _OID(<oid>) to value if found or text "<not in trap>" |
|
797 | + * @param $display string |
|
798 | + * @return string display |
|
799 | + */ |
|
800 | + protected function applyDisplay($display) |
|
801 | + { |
|
802 | + $matches=array(); |
|
803 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
804 | + { |
|
805 | + $oid=$matches[1]; |
|
806 | + $found=0; |
|
807 | + // Test and transform regexp |
|
808 | + $oidR = $this->ruleClass->regexp_eval($oid); |
|
809 | 809 | |
810 | - foreach($this->trapDataExt as $val) |
|
811 | - { |
|
812 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
813 | - { |
|
814 | - $val->value=preg_replace('/"/','',$val->value); |
|
815 | - $rep=0; |
|
816 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
817 | - if ($rep==0) |
|
818 | - { |
|
819 | - $this->logging->log("Error in display",WARN,''); |
|
820 | - return $display; |
|
821 | - } |
|
822 | - $found=1; |
|
823 | - break; |
|
824 | - } |
|
825 | - } |
|
826 | - if ($found==0) |
|
827 | - { |
|
828 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
829 | - if ($rep==0) |
|
830 | - { |
|
831 | - $this->logging->log("Error in display",WARN,''); |
|
832 | - return $display; |
|
833 | - } |
|
834 | - } |
|
835 | - } |
|
836 | - return $display; |
|
837 | - } |
|
838 | - |
|
839 | - /** Match rules for current trap and do action |
|
840 | - */ |
|
841 | - public function applyRules() |
|
842 | - { |
|
843 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
844 | - |
|
845 | - if ($rules===false || count($rules)==0) |
|
846 | - { |
|
847 | - $this->logging->log('No rules found for this trap',INFO ); |
|
848 | - $this->trapData['status']='unknown'; |
|
849 | - $this->trapToDb=true; |
|
850 | - return; |
|
851 | - } |
|
852 | - //print_r($rules); |
|
853 | - // Evaluate all rules in sequence |
|
854 | - $this->trapAction=null; |
|
855 | - foreach ($rules as $rule) |
|
856 | - { |
|
810 | + foreach($this->trapDataExt as $val) |
|
811 | + { |
|
812 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
813 | + { |
|
814 | + $val->value=preg_replace('/"/','',$val->value); |
|
815 | + $rep=0; |
|
816 | + $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
817 | + if ($rep==0) |
|
818 | + { |
|
819 | + $this->logging->log("Error in display",WARN,''); |
|
820 | + return $display; |
|
821 | + } |
|
822 | + $found=1; |
|
823 | + break; |
|
824 | + } |
|
825 | + } |
|
826 | + if ($found==0) |
|
827 | + { |
|
828 | + $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
829 | + if ($rep==0) |
|
830 | + { |
|
831 | + $this->logging->log("Error in display",WARN,''); |
|
832 | + return $display; |
|
833 | + } |
|
834 | + } |
|
835 | + } |
|
836 | + return $display; |
|
837 | + } |
|
838 | + |
|
839 | + /** Match rules for current trap and do action |
|
840 | + */ |
|
841 | + public function applyRules() |
|
842 | + { |
|
843 | + $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
844 | + |
|
845 | + if ($rules===false || count($rules)==0) |
|
846 | + { |
|
847 | + $this->logging->log('No rules found for this trap',INFO ); |
|
848 | + $this->trapData['status']='unknown'; |
|
849 | + $this->trapToDb=true; |
|
850 | + return; |
|
851 | + } |
|
852 | + //print_r($rules); |
|
853 | + // Evaluate all rules in sequence |
|
854 | + $this->trapAction=null; |
|
855 | + foreach ($rules as $rule) |
|
856 | + { |
|
857 | 857 | |
858 | - $host_name=$rule['host_name']; |
|
859 | - $service_name=$rule['service_name']; |
|
858 | + $host_name=$rule['host_name']; |
|
859 | + $service_name=$rule['service_name']; |
|
860 | 860 | |
861 | - $display=$this->applyDisplay($rule['display']); |
|
862 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
863 | - try |
|
864 | - { |
|
865 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
866 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
867 | - //->eval_rule($rule['rule']); |
|
861 | + $display=$this->applyDisplay($rule['display']); |
|
862 | + $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
863 | + try |
|
864 | + { |
|
865 | + $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
866 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
867 | + //->eval_rule($rule['rule']); |
|
868 | 868 | |
869 | - if ($evalr == true) |
|
870 | - { |
|
871 | - //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
872 | - $action=$rule['action_match']; |
|
873 | - $this->logging->log('action OK : '.$action,INFO ); |
|
874 | - if ($action >= 0) |
|
875 | - { |
|
876 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
877 | - { |
|
878 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
879 | - } |
|
880 | - else |
|
881 | - { |
|
882 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
883 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
884 | - } |
|
885 | - } |
|
886 | - else |
|
887 | - { |
|
888 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
889 | - } |
|
890 | - $this->trapToDb=($action==-2)?false:true; |
|
891 | - } |
|
892 | - else |
|
893 | - { |
|
894 | - //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
869 | + if ($evalr == true) |
|
870 | + { |
|
871 | + //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
|
872 | + $action=$rule['action_match']; |
|
873 | + $this->logging->log('action OK : '.$action,INFO ); |
|
874 | + if ($action >= 0) |
|
875 | + { |
|
876 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
877 | + { |
|
878 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
879 | + } |
|
880 | + else |
|
881 | + { |
|
882 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
883 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
884 | + } |
|
885 | + } |
|
886 | + else |
|
887 | + { |
|
888 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
889 | + } |
|
890 | + $this->trapToDb=($action==-2)?false:true; |
|
891 | + } |
|
892 | + else |
|
893 | + { |
|
894 | + //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
|
895 | 895 | |
896 | - $action=$rule['action_nomatch']; |
|
897 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
898 | - if ($action >= 0) |
|
899 | - { |
|
900 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
901 | - { |
|
902 | - $this->trapAction.='Error sending status : check cmd/API'; |
|
903 | - } |
|
904 | - else |
|
905 | - { |
|
906 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
907 | - $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
908 | - } |
|
909 | - } |
|
910 | - else |
|
911 | - { |
|
912 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
913 | - } |
|
914 | - $this->trapToDb=($action==-2)?false:true; |
|
915 | - } |
|
916 | - // Put name in source_name |
|
917 | - if (!isset($this->trapData['source_name'])) |
|
918 | - { |
|
919 | - $this->trapData['source_name']=$rule['host_name']; |
|
920 | - } |
|
921 | - else |
|
922 | - { |
|
923 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
924 | - { // only add if not present |
|
925 | - $this->trapData['source_name'].=','.$rule['host_name']; |
|
926 | - } |
|
927 | - } |
|
928 | - } |
|
929 | - catch (Exception $e) |
|
930 | - { |
|
931 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
932 | - $this->trapAction.=' ERR : '.$e->getMessage(); |
|
933 | - $this->trapData['status']='error'; |
|
934 | - } |
|
896 | + $action=$rule['action_nomatch']; |
|
897 | + $this->logging->log('action NOK : '.$action,INFO ); |
|
898 | + if ($action >= 0) |
|
899 | + { |
|
900 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
901 | + { |
|
902 | + $this->trapAction.='Error sending status : check cmd/API'; |
|
903 | + } |
|
904 | + else |
|
905 | + { |
|
906 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
907 | + $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
908 | + } |
|
909 | + } |
|
910 | + else |
|
911 | + { |
|
912 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
913 | + } |
|
914 | + $this->trapToDb=($action==-2)?false:true; |
|
915 | + } |
|
916 | + // Put name in source_name |
|
917 | + if (!isset($this->trapData['source_name'])) |
|
918 | + { |
|
919 | + $this->trapData['source_name']=$rule['host_name']; |
|
920 | + } |
|
921 | + else |
|
922 | + { |
|
923 | + if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
924 | + { // only add if not present |
|
925 | + $this->trapData['source_name'].=','.$rule['host_name']; |
|
926 | + } |
|
927 | + } |
|
928 | + } |
|
929 | + catch (Exception $e) |
|
930 | + { |
|
931 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
932 | + $this->trapAction.=' ERR : '.$e->getMessage(); |
|
933 | + $this->trapData['status']='error'; |
|
934 | + } |
|
935 | 935 | |
936 | - } |
|
937 | - if ($this->trapData['status']=='error') |
|
938 | - { |
|
939 | - $this->trapToDb=true; // Always put errors in DB for the use can see |
|
940 | - } |
|
941 | - else |
|
942 | - { |
|
943 | - $this->trapData['status']='done'; |
|
944 | - } |
|
945 | - } |
|
946 | - |
|
947 | - /** Add Time a action to rule |
|
948 | - * @param string $time : time to process to insert in SQL |
|
949 | - */ |
|
950 | - public function add_rule_final($time) |
|
951 | - { |
|
952 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
953 | - if ($this->trapAction==null) |
|
954 | - { |
|
955 | - $this->trapAction='No action'; |
|
956 | - } |
|
957 | - $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
958 | - if ($db_conn->query($sql) === false) { |
|
959 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
960 | - } |
|
961 | - } |
|
962 | - |
|
963 | - /*********** UTILITIES *********************/ |
|
964 | - |
|
965 | - /** reset service to OK after time defined in rule |
|
966 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
967 | - * @return integer : not in use |
|
968 | - **/ |
|
969 | - public function reset_services() |
|
970 | - { |
|
971 | - // Get all services not in 'ok' state |
|
972 | - $sql_query="SELECT s.service_object_id, |
|
936 | + } |
|
937 | + if ($this->trapData['status']=='error') |
|
938 | + { |
|
939 | + $this->trapToDb=true; // Always put errors in DB for the use can see |
|
940 | + } |
|
941 | + else |
|
942 | + { |
|
943 | + $this->trapData['status']='done'; |
|
944 | + } |
|
945 | + } |
|
946 | + |
|
947 | + /** Add Time a action to rule |
|
948 | + * @param string $time : time to process to insert in SQL |
|
949 | + */ |
|
950 | + public function add_rule_final($time) |
|
951 | + { |
|
952 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
953 | + if ($this->trapAction==null) |
|
954 | + { |
|
955 | + $this->trapAction='No action'; |
|
956 | + } |
|
957 | + $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
|
958 | + if ($db_conn->query($sql) === false) { |
|
959 | + $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
960 | + } |
|
961 | + } |
|
962 | + |
|
963 | + /*********** UTILITIES *********************/ |
|
964 | + |
|
965 | + /** reset service to OK after time defined in rule |
|
966 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
967 | + * @return integer : not in use |
|
968 | + **/ |
|
969 | + public function reset_services() |
|
970 | + { |
|
971 | + // Get all services not in 'ok' state |
|
972 | + $sql_query="SELECT s.service_object_id, |
|
973 | 973 | UNIX_TIMESTAMP(s.last_check) AS last_check, |
974 | 974 | s.current_state as state, |
975 | 975 | v.name1 as host_name, |
@@ -977,43 +977,43 @@ discard block |
||
977 | 977 | FROM icinga_servicestatus AS s |
978 | 978 | LEFT JOIN icinga_objects as v ON s.service_object_id=v.object_id |
979 | 979 | WHERE s.current_state != 0;"; |
980 | - $db_conn=$this->trapsDB->db_connect_ido(); |
|
981 | - if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
982 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
983 | - return 0; |
|
984 | - } |
|
985 | - $services=$services_db->fetchAll(); |
|
986 | - |
|
987 | - // Get all rules |
|
988 | - $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
989 | - $db_conn2=$this->trapsDB->db_connect_trap(); |
|
990 | - if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
991 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
992 | - return 0; |
|
993 | - } |
|
994 | - $rules=$rules_db->fetchAll(); |
|
995 | - |
|
996 | - $now=date('U'); |
|
997 | - |
|
998 | - $numreset=0; |
|
999 | - foreach ($rules as $rule) |
|
1000 | - { |
|
1001 | - foreach ($services as $service) |
|
1002 | - { |
|
1003 | - if ($service['service_name'] == $rule['service_name'] && |
|
1004 | - $service['host_name'] == $rule['host_name'] && |
|
1005 | - ($service['last_check'] + $rule['revert_ok']) < $now) |
|
1006 | - { |
|
1007 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
1008 | - $numreset++; |
|
1009 | - } |
|
1010 | - } |
|
1011 | - } |
|
1012 | - echo "\n"; |
|
1013 | - echo $numreset . " service(s) reset to OK\n"; |
|
1014 | - return 0; |
|
1015 | - |
|
1016 | - } |
|
980 | + $db_conn=$this->trapsDB->db_connect_ido(); |
|
981 | + if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
|
982 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
983 | + return 0; |
|
984 | + } |
|
985 | + $services=$services_db->fetchAll(); |
|
986 | + |
|
987 | + // Get all rules |
|
988 | + $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
|
989 | + $db_conn2=$this->trapsDB->db_connect_trap(); |
|
990 | + if (($rules_db=$db_conn2->query($sql_query)) === false) { |
|
991 | + $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
992 | + return 0; |
|
993 | + } |
|
994 | + $rules=$rules_db->fetchAll(); |
|
995 | + |
|
996 | + $now=date('U'); |
|
997 | + |
|
998 | + $numreset=0; |
|
999 | + foreach ($rules as $rule) |
|
1000 | + { |
|
1001 | + foreach ($services as $service) |
|
1002 | + { |
|
1003 | + if ($service['service_name'] == $rule['service_name'] && |
|
1004 | + $service['host_name'] == $rule['host_name'] && |
|
1005 | + ($service['last_check'] + $rule['revert_ok']) < $now) |
|
1006 | + { |
|
1007 | + $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
1008 | + $numreset++; |
|
1009 | + } |
|
1010 | + } |
|
1011 | + } |
|
1012 | + echo "\n"; |
|
1013 | + echo $numreset . " service(s) reset to OK\n"; |
|
1014 | + return 0; |
|
1015 | + |
|
1016 | + } |
|
1017 | 1017 | |
1018 | 1018 | |
1019 | 1019 | } |
1020 | 1020 | \ No newline at end of file |
@@ -49,13 +49,13 @@ discard block |
||
49 | 49 | |
50 | 50 | // Logs |
51 | 51 | /** @var Logging Logging class. */ |
52 | - public $logging; //< Logging class. |
|
52 | + public $logging; //< Logging class. |
|
53 | 53 | /** @var bool true if log was setup in constructor */ |
54 | - protected $logSetup; //< bool true if log was setup in constructor |
|
54 | + protected $logSetup; //< bool true if log was setup in constructor |
|
55 | 55 | |
56 | 56 | // Databases |
57 | 57 | /** @var Database $trapsDB Database class*/ |
58 | - public $trapsDB = null; |
|
58 | + public $trapsDB=null; |
|
59 | 59 | |
60 | 60 | // Trap received data |
61 | 61 | protected $receivingHost; |
@@ -71,15 +71,15 @@ discard block |
||
71 | 71 | protected $trapToDb=true; |
72 | 72 | |
73 | 73 | /** @var Mib mib class */ |
74 | - public $mibClass = null; |
|
74 | + public $mibClass=null; |
|
75 | 75 | |
76 | 76 | /** @var Rule rule class */ |
77 | - public $ruleClass = null; |
|
77 | + public $ruleClass=null; |
|
78 | 78 | |
79 | 79 | /** @var Plugins plugins manager **/ |
80 | - public $pluginClass = null; |
|
80 | + public $pluginClass=null; |
|
81 | 81 | |
82 | - function __construct($etcDir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
|
82 | + function __construct($etcDir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='') |
|
83 | 83 | { |
84 | 84 | // Paths of ini files |
85 | 85 | $this->icingaweb2Etc=$etcDir; |
@@ -87,10 +87,10 @@ discard block |
||
87 | 87 | $this->icingaweb2Ressources=$this->icingaweb2Etc."/resources.ini"; |
88 | 88 | |
89 | 89 | //************* Setup logging |
90 | - $this->logging = new Logging(); |
|
90 | + $this->logging=new Logging(); |
|
91 | 91 | if ($baseLogLevel != null) |
92 | 92 | { |
93 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
93 | + $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile); |
|
94 | 94 | $this->logSetup=true; |
95 | 95 | } |
96 | 96 | else |
@@ -100,14 +100,14 @@ discard block |
||
100 | 100 | $this->logging->log('Loggin started', INFO); |
101 | 101 | |
102 | 102 | //*************** Get options from ini files |
103 | - if (! is_file($this->trapModuleConfig)) |
|
103 | + if (!is_file($this->trapModuleConfig)) |
|
104 | 104 | { |
105 | 105 | throw new Exception("Ini file ".$this->trapModuleConfig." does not exists"); |
106 | 106 | } |
107 | - $trapConfig=parse_ini_file($this->trapModuleConfig,true); |
|
107 | + $trapConfig=parse_ini_file($this->trapModuleConfig, true); |
|
108 | 108 | if ($trapConfig == false) |
109 | 109 | { |
110 | - $this->logging->log("Error reading ini file : ".$this->trapModuleConfig,ERROR,'syslog'); |
|
110 | + $this->logging->log("Error reading ini file : ".$this->trapModuleConfig, ERROR, 'syslog'); |
|
111 | 111 | throw new Exception("Error reading ini file : ".$this->trapModuleConfig); |
112 | 112 | } |
113 | 113 | $this->getMainOptions($trapConfig); // Get main options from ini file |
@@ -121,10 +121,10 @@ discard block |
||
121 | 121 | if ($this->apiUse === true) $this->getAPI(); // Setup API |
122 | 122 | |
123 | 123 | //*************** Setup MIB |
124 | - $this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class |
|
124 | + $this->mibClass=new Mib($this->logging, $this->trapsDB, $this->snmptranslate, $this->snmptranslate_dirs); // Create Mib class |
|
125 | 125 | |
126 | 126 | //*************** Setup Rule |
127 | - $this->ruleClass = new Rule($this); //< Create Rule class |
|
127 | + $this->ruleClass=new Rule($this); //< Create Rule class |
|
128 | 128 | |
129 | 129 | $this->trapData=array( // TODO : put this in a reset function (DAEMON_MODE) |
130 | 130 | 'source_ip' => 'unknown', |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | |
137 | 137 | //*************** Setup Plugins |
138 | 138 | //Create plugin class. Plugins are not loaded here, but by calling registerAllPlugins |
139 | - $this->pluginClass = new Plugins($this); |
|
139 | + $this->pluginClass=new Plugins($this); |
|
140 | 140 | |
141 | 141 | |
142 | 142 | } |
@@ -151,15 +151,15 @@ discard block |
||
151 | 151 | * @param string $message warning message if not found |
152 | 152 | * @return boolean true if found, or false |
153 | 153 | */ |
154 | - protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
|
154 | + protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null) |
|
155 | 155 | { |
156 | 156 | if (!isset($option_array[$option_category][$option_name])) |
157 | 157 | { |
158 | 158 | if ($message === null) |
159 | 159 | { |
160 | - $message='No ' . $option_name . ' in config file: '. $this->trapModuleConfig; |
|
160 | + $message='No '.$option_name.' in config file: '.$this->trapModuleConfig; |
|
161 | 161 | } |
162 | - $this->logging->log($message,$log_level); |
|
162 | + $this->logging->log($message, $log_level); |
|
163 | 163 | return false; |
164 | 164 | } |
165 | 165 | else |
@@ -177,24 +177,24 @@ discard block |
||
177 | 177 | { |
178 | 178 | |
179 | 179 | // Snmptranslate binary path |
180 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate); |
|
180 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate); |
|
181 | 181 | |
182 | 182 | // mibs path |
183 | - $this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs); |
|
183 | + $this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs); |
|
184 | 184 | |
185 | 185 | // icinga2cmd path |
186 | - $this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd); |
|
186 | + $this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd); |
|
187 | 187 | |
188 | 188 | // table prefix |
189 | - $this->getOptionIfSet($trapConfig,'config','database_prefix', $this->dbPrefix); |
|
189 | + $this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->dbPrefix); |
|
190 | 190 | |
191 | 191 | // API options |
192 | - if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->apiHostname)) |
|
192 | + if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->apiHostname)) |
|
193 | 193 | { |
194 | 194 | $this->apiUse=true; |
195 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->apiPort); |
|
196 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->apiUsername); |
|
197 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->apiPassword); |
|
195 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_port', $this->apiPort); |
|
196 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_user', $this->apiUsername); |
|
197 | + $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_password', $this->apiPassword); |
|
198 | 198 | } |
199 | 199 | } |
200 | 200 | |
@@ -205,40 +205,40 @@ discard block |
||
205 | 205 | protected function setupDatabase($trapConfig) |
206 | 206 | { |
207 | 207 | // Trap database |
208 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
208 | + if (!array_key_exists('database', $trapConfig['config'])) |
|
209 | 209 | { |
210 | - $this->logging->log("No database in config file: ".$this->trapModuleConfig,ERROR,''); |
|
210 | + $this->logging->log("No database in config file: ".$this->trapModuleConfig, ERROR, ''); |
|
211 | 211 | return; |
212 | 212 | } |
213 | 213 | $dbTrapName=$trapConfig['config']['database']; |
214 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
214 | + $this->logging->log("Found database in config file: ".$dbTrapName, INFO); |
|
215 | 215 | |
216 | - if ( ($dbConfig=parse_ini_file($this->icingaweb2Ressources,true)) === false) |
|
216 | + if (($dbConfig=parse_ini_file($this->icingaweb2Ressources, true)) === false) |
|
217 | 217 | { |
218 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources,ERROR,''); |
|
218 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2Ressources, ERROR, ''); |
|
219 | 219 | return; |
220 | 220 | } |
221 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
221 | + if (!array_key_exists($dbTrapName, $dbConfig)) |
|
222 | 222 | { |
223 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
223 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2Ressources, ERROR, ''); |
|
224 | 224 | return; |
225 | 225 | } |
226 | 226 | |
227 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->dbPrefix); |
|
227 | + $this->trapsDB=new Database($this->logging, $dbConfig[$dbTrapName], $this->dbPrefix); |
|
228 | 228 | |
229 | 229 | if ($this->apiUse === true) return; // In case of API use, no IDO is necessary |
230 | 230 | |
231 | 231 | // IDO Database |
232 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
232 | + if (!array_key_exists('IDOdatabase', $trapConfig['config'])) |
|
233 | 233 | { |
234 | - $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig,ERROR,''); |
|
234 | + $this->logging->log("No IDOdatabase in config file: ".$this->trapModuleConfig, ERROR, ''); |
|
235 | 235 | } |
236 | 236 | $dbIdoName=$trapConfig['config']['IDOdatabase']; |
237 | 237 | |
238 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
239 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
238 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO); |
|
239 | + if (!array_key_exists($dbIdoName, $dbConfig)) |
|
240 | 240 | { |
241 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources,ERROR,''); |
|
241 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2Ressources, ERROR, ''); |
|
242 | 242 | return; |
243 | 243 | } |
244 | 244 | |
@@ -253,9 +253,9 @@ discard block |
||
253 | 253 | // Database options |
254 | 254 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
255 | 255 | { |
256 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
257 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
258 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
256 | + $this->getDBConfigIfSet('log_level', $this->logging->debugLevel); |
|
257 | + $this->getDBConfigIfSet('log_destination', $this->logging->outputMode); |
|
258 | + $this->getDBConfigIfSet('log_file', $this->logging->outputFile); |
|
259 | 259 | } |
260 | 260 | } |
261 | 261 | |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | * @param string $element |
264 | 264 | * @param string $variable |
265 | 265 | */ |
266 | - protected function getDBConfigIfSet($element,&$variable) |
|
266 | + protected function getDBConfigIfSet($element, &$variable) |
|
267 | 267 | { |
268 | 268 | $value=$this->getDBConfig($element); |
269 | 269 | if ($value != null) $variable=$value; |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | $db_conn=$this->trapsDB->db_connect_trap(); |
280 | 280 | $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE ( name=\''.$element.'\' )'; |
281 | 281 | if (($ret_code=$db_conn->query($sql)) === false) { |
282 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
282 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
283 | 283 | return null; |
284 | 284 | } |
285 | 285 | $value=$ret_code->fetch(); |
@@ -296,15 +296,15 @@ discard block |
||
296 | 296 | * @param string $destination file/syslog/display |
297 | 297 | * @return void |
298 | 298 | **/ |
299 | - public function trapLog( $message, $level, $destination ='') // OBSOLETE |
|
299 | + public function trapLog($message, $level, $destination='') // OBSOLETE |
|
300 | 300 | { |
301 | 301 | // TODO : replace ref with $this->logging->log |
302 | 302 | $this->logging->log($message, $level, $destination); |
303 | 303 | } |
304 | 304 | |
305 | - public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
|
305 | + public function setLogging($debugLvl, $outputType, $outputOption=null) // OBSOLETE |
|
306 | 306 | { |
307 | - $this->logging->setLogging($debugLvl, $outputType,$outputOption); |
|
307 | + $this->logging->setLogging($debugLvl, $outputType, $outputOption); |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | /** |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | { |
316 | 316 | if ($this->icinga2api == null) |
317 | 317 | { |
318 | - $this->icinga2api = new Icinga2API($this->apiHostname,$this->apiPort); |
|
318 | + $this->icinga2api=new Icinga2API($this->apiHostname, $this->apiPort); |
|
319 | 319 | } |
320 | 320 | return $this->icinga2api; |
321 | 321 | } |
@@ -334,7 +334,7 @@ discard block |
||
334 | 334 | if ($input_stream === false) |
335 | 335 | { |
336 | 336 | $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
337 | - $this->logging->log("Error reading stdin !",ERROR,''); |
|
337 | + $this->logging->log("Error reading stdin !", ERROR, ''); |
|
338 | 338 | return null; // note : exception thrown by logging |
339 | 339 | } |
340 | 340 | |
@@ -343,21 +343,21 @@ discard block |
||
343 | 343 | if ($this->receivingHost === false) |
344 | 344 | { |
345 | 345 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
346 | - $this->logging->log("Error reading Host !",ERROR,''); |
|
346 | + $this->logging->log("Error reading Host !", ERROR, ''); |
|
347 | 347 | } |
348 | 348 | // line 2 IP:port=>IP:port |
349 | 349 | $IP=chop(fgets($input_stream)); |
350 | 350 | if ($IP === false) |
351 | 351 | { |
352 | 352 | $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
353 | - $this->logging->log("Error reading IP !",ERROR,''); |
|
353 | + $this->logging->log("Error reading IP !", ERROR, ''); |
|
354 | 354 | } |
355 | 355 | $matches=array(); |
356 | - $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
|
357 | - if ($ret_code===0 || $ret_code===false) |
|
356 | + $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches); |
|
357 | + if ($ret_code === 0 || $ret_code === false) |
|
358 | 358 | { |
359 | 359 | $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
360 | - $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
|
360 | + $this->logging->log('Error parsing IP : '.$IP, ERROR, ''); |
|
361 | 361 | } |
362 | 362 | else |
363 | 363 | { |
@@ -367,34 +367,34 @@ discard block |
||
367 | 367 | $this->trapData['destination_port']=$matches[4]; |
368 | 368 | } |
369 | 369 | |
370 | - while (($vars=fgets($input_stream)) !==false) |
|
370 | + while (($vars=fgets($input_stream)) !== false) |
|
371 | 371 | { |
372 | 372 | $vars=chop($vars); |
373 | - $ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches); |
|
374 | - if ($ret_code===0 || $ret_code===false) |
|
373 | + $ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches); |
|
374 | + if ($ret_code === 0 || $ret_code === false) |
|
375 | 375 | { |
376 | - $this->logging->log('No match on trap data : '.$vars,WARN,''); |
|
376 | + $this->logging->log('No match on trap data : '.$vars, WARN, ''); |
|
377 | 377 | } |
378 | 378 | else |
379 | 379 | { |
380 | - 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')) |
|
380 | + 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')) |
|
381 | 381 | { |
382 | 382 | $this->trapData['trap_oid']=$matches[2]; |
383 | 383 | } |
384 | 384 | else |
385 | 385 | { |
386 | - $object= new stdClass; |
|
387 | - $object->oid =$matches[1]; |
|
388 | - $object->value = $matches[2]; |
|
389 | - array_push($this->trapDataExt,$object); |
|
386 | + $object=new stdClass; |
|
387 | + $object->oid=$matches[1]; |
|
388 | + $object->value=$matches[2]; |
|
389 | + array_push($this->trapDataExt, $object); |
|
390 | 390 | } |
391 | 391 | } |
392 | 392 | } |
393 | 393 | |
394 | - if ($this->trapData['trap_oid']=='unknown') |
|
394 | + if ($this->trapData['trap_oid'] == 'unknown') |
|
395 | 395 | { |
396 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trapData['source_ip']); |
|
397 | - $this->logging->log('no trap oid found',ERROR,''); |
|
396 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trapData['source_ip']); |
|
397 | + $this->logging->log('no trap oid found', ERROR, ''); |
|
398 | 398 | } |
399 | 399 | |
400 | 400 | // Translate oids. |
@@ -416,7 +416,7 @@ discard block |
||
416 | 416 | } |
417 | 417 | |
418 | 418 | |
419 | - $this->trapData['status']= 'waiting'; |
|
419 | + $this->trapData['status']='waiting'; |
|
420 | 420 | |
421 | 421 | return $this->trapData; |
422 | 422 | } |
@@ -432,40 +432,40 @@ discard block |
||
432 | 432 | $db_conn=$this->trapsDB->db_connect_trap(); |
433 | 433 | |
434 | 434 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid.'\';'; |
435 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
435 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
436 | 436 | if (($ret_code=$db_conn->query($sql)) === false) { |
437 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
437 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
438 | 438 | } |
439 | 439 | $name=$ret_code->fetch(); |
440 | 440 | if ($name['name'] != null) |
441 | 441 | { |
442 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
442 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
443 | 443 | } |
444 | 444 | |
445 | 445 | // Also check if it is an instance of OID |
446 | - $oid_instance=preg_replace('/\.[0-9]+$/','',$oid); |
|
446 | + $oid_instance=preg_replace('/\.[0-9]+$/', '', $oid); |
|
447 | 447 | |
448 | 448 | $sql='SELECT mib,name from '.$this->dbPrefix.'mib_cache WHERE oid=\''.$oid_instance.'\';'; |
449 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
449 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
450 | 450 | if (($ret_code=$db_conn->query($sql)) === false) { |
451 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
451 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
452 | 452 | } |
453 | 453 | $name=$ret_code->fetch(); |
454 | 454 | if ($name['name'] != null) |
455 | 455 | { |
456 | - return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']); |
|
456 | + return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']); |
|
457 | 457 | } |
458 | 458 | |
459 | 459 | // Try to get oid name from snmptranslate |
460 | - $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
460 | + $translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs. |
|
461 | 461 | ' '.$oid); |
462 | 462 | $matches=array(); |
463 | - $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
|
464 | - if ($ret_code===0 || $ret_code === false) { |
|
463 | + $ret_code=preg_match('/(.*)::(.*)/', $translate, $matches); |
|
464 | + if ($ret_code === 0 || $ret_code === false) { |
|
465 | 465 | return NULL; |
466 | 466 | } else { |
467 | - $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO); |
|
468 | - return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]); |
|
467 | + $this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO); |
|
468 | + return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]); |
|
469 | 469 | } |
470 | 470 | } |
471 | 471 | |
@@ -476,90 +476,90 @@ discard block |
||
476 | 476 | **/ |
477 | 477 | public function eraseOldTraps($days=0) |
478 | 478 | { |
479 | - if ($days==0) |
|
479 | + if ($days == 0) |
|
480 | 480 | { |
481 | 481 | if (($days=$this->getDBConfig('db_remove_days')) == null) |
482 | 482 | { |
483 | - $this->logging->log('No days specified & no db value : no tap erase' ,WARN,''); |
|
483 | + $this->logging->log('No days specified & no db value : no tap erase', WARN, ''); |
|
484 | 484 | return; |
485 | 485 | } |
486 | 486 | } |
487 | 487 | $db_conn=$this->trapsDB->db_connect_trap(); |
488 | - $daysago = strtotime("-".$days." day"); |
|
489 | - $sql= 'delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';'; |
|
488 | + $daysago=strtotime("-".$days." day"); |
|
489 | + $sql='delete from '.$this->dbPrefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';'; |
|
490 | 490 | if ($db_conn->query($sql) === false) { |
491 | - $this->logging->log('Error erasing traps : '.$sql,ERROR,''); |
|
491 | + $this->logging->log('Error erasing traps : '.$sql, ERROR, ''); |
|
492 | 492 | } |
493 | - $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO); |
|
493 | + $this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO); |
|
494 | 494 | } |
495 | 495 | |
496 | 496 | /** Write error to received trap database |
497 | 497 | */ |
498 | - public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
|
498 | + public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null) |
|
499 | 499 | { |
500 | 500 | |
501 | 501 | $db_conn=$this->trapsDB->db_connect_trap(); |
502 | 502 | |
503 | 503 | // add date time |
504 | - $insert_col ='date_received,status'; |
|
505 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
504 | + $insert_col='date_received,status'; |
|
505 | + $insert_val="'".date("Y-m-d H:i:s")."','error'"; |
|
506 | 506 | |
507 | - if ($sourceIP !=null) |
|
507 | + if ($sourceIP != null) |
|
508 | 508 | { |
509 | - $insert_col .=',source_ip'; |
|
510 | - $insert_val .=",'". $sourceIP ."'"; |
|
509 | + $insert_col.=',source_ip'; |
|
510 | + $insert_val.=",'".$sourceIP."'"; |
|
511 | 511 | } |
512 | - if ($trapoid !=null) |
|
512 | + if ($trapoid != null) |
|
513 | 513 | { |
514 | - $insert_col .=',trap_oid'; |
|
515 | - $insert_val .=",'". $trapoid ."'"; |
|
514 | + $insert_col.=',trap_oid'; |
|
515 | + $insert_val.=",'".$trapoid."'"; |
|
516 | 516 | } |
517 | - $insert_col .=',status_detail'; |
|
518 | - $insert_val .=",'". $message ."'"; |
|
517 | + $insert_col.=',status_detail'; |
|
518 | + $insert_val.=",'".$message."'"; |
|
519 | 519 | |
520 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
520 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
521 | 521 | |
522 | 522 | switch ($this->trapsDB->trapDBType) |
523 | 523 | { |
524 | 524 | case 'pgsql': |
525 | - $sql .= ' RETURNING id;'; |
|
526 | - $this->logging->log('sql : '.$sql,INFO); |
|
525 | + $sql.=' RETURNING id;'; |
|
526 | + $this->logging->log('sql : '.$sql, INFO); |
|
527 | 527 | if (($ret_code=$db_conn->query($sql)) === false) { |
528 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
528 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
529 | 529 | } |
530 | - $this->logging->log('SQL insertion OK',INFO ); |
|
530 | + $this->logging->log('SQL insertion OK', INFO); |
|
531 | 531 | // Get last id to insert oid/values in secondary table |
532 | 532 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
533 | 533 | |
534 | - $this->logging->log('Erreur recuperation id',1,''); |
|
534 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
535 | 535 | } |
536 | - if (! isset($inserted_id_ret['id'])) { |
|
537 | - $this->logging->log('Error getting id',1,''); |
|
536 | + if (!isset($inserted_id_ret['id'])) { |
|
537 | + $this->logging->log('Error getting id', 1, ''); |
|
538 | 538 | } |
539 | 539 | $this->trapId=$inserted_id_ret['id']; |
540 | 540 | break; |
541 | 541 | case 'mysql': |
542 | - $sql .= ';'; |
|
543 | - $this->logging->log('sql : '.$sql,INFO ); |
|
542 | + $sql.=';'; |
|
543 | + $this->logging->log('sql : '.$sql, INFO); |
|
544 | 544 | if ($db_conn->query($sql) === false) { |
545 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
545 | + $this->logging->log('Error SQL insert : '.$sql, 1, ''); |
|
546 | 546 | } |
547 | - $this->logging->log('SQL insertion OK',INFO ); |
|
547 | + $this->logging->log('SQL insertion OK', INFO); |
|
548 | 548 | // Get last id to insert oid/values in secondary table |
549 | 549 | $sql='SELECT LAST_INSERT_ID();'; |
550 | 550 | if (($ret_code=$db_conn->query($sql)) === false) { |
551 | - $this->logging->log('Erreur recuperation id',1,''); |
|
551 | + $this->logging->log('Erreur recuperation id', 1, ''); |
|
552 | 552 | } |
553 | 553 | |
554 | 554 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
555 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
555 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
556 | 556 | $this->trapId=$inserted_id; |
557 | 557 | break; |
558 | 558 | default: |
559 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
559 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
560 | 560 | } |
561 | 561 | |
562 | - $this->logging->log('id found: '. $this->trapId,INFO ); |
|
562 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
563 | 563 | } |
564 | 564 | |
565 | 565 | /** Write trap data to trap database |
@@ -576,86 +576,86 @@ discard block |
||
576 | 576 | $insert_col=''; |
577 | 577 | $insert_val=''; |
578 | 578 | // add date time |
579 | - $this->trapData['date_received'] = date("Y-m-d H:i:s"); |
|
579 | + $this->trapData['date_received']=date("Y-m-d H:i:s"); |
|
580 | 580 | |
581 | 581 | $firstcol=1; |
582 | 582 | foreach ($this->trapData as $col => $val) |
583 | 583 | { |
584 | - if ($firstcol==0) |
|
584 | + if ($firstcol == 0) |
|
585 | 585 | { |
586 | - $insert_col .=','; |
|
587 | - $insert_val .=','; |
|
586 | + $insert_col.=','; |
|
587 | + $insert_val.=','; |
|
588 | 588 | } |
589 | - $insert_col .= $col ; |
|
590 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
589 | + $insert_col.=$col; |
|
590 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
591 | 591 | $firstcol=0; |
592 | 592 | } |
593 | 593 | |
594 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
594 | + $sql='INSERT INTO '.$this->dbPrefix.'received ('.$insert_col.') VALUES ('.$insert_val.')'; |
|
595 | 595 | switch ($this->trapsDB->trapDBType) |
596 | 596 | { |
597 | 597 | case 'pgsql': |
598 | - $sql .= ' RETURNING id;'; |
|
599 | - $this->logging->log('sql : '.$sql,INFO ); |
|
598 | + $sql.=' RETURNING id;'; |
|
599 | + $this->logging->log('sql : '.$sql, INFO); |
|
600 | 600 | if (($ret_code=$db_conn->query($sql)) === false) { |
601 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
601 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
602 | 602 | } |
603 | - $this->logging->log('SQL insertion OK',INFO ); |
|
603 | + $this->logging->log('SQL insertion OK', INFO); |
|
604 | 604 | // Get last id to insert oid/values in secondary table |
605 | 605 | if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
606 | 606 | |
607 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
607 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
608 | 608 | } |
609 | - if (! isset($inserted_id_ret['id'])) { |
|
610 | - $this->logging->log('Error getting id',ERROR,''); |
|
609 | + if (!isset($inserted_id_ret['id'])) { |
|
610 | + $this->logging->log('Error getting id', ERROR, ''); |
|
611 | 611 | } |
612 | 612 | $this->trapId=$inserted_id_ret['id']; |
613 | 613 | break; |
614 | 614 | case 'mysql': |
615 | - $sql .= ';'; |
|
616 | - $this->logging->log('sql : '.$sql,INFO ); |
|
615 | + $sql.=';'; |
|
616 | + $this->logging->log('sql : '.$sql, INFO); |
|
617 | 617 | if ($db_conn->query($sql) === false) { |
618 | - $this->logging->log('Error SQL insert : '.$sql,ERROR,''); |
|
618 | + $this->logging->log('Error SQL insert : '.$sql, ERROR, ''); |
|
619 | 619 | } |
620 | - $this->logging->log('SQL insertion OK',INFO ); |
|
620 | + $this->logging->log('SQL insertion OK', INFO); |
|
621 | 621 | // Get last id to insert oid/values in secondary table |
622 | 622 | $sql='SELECT LAST_INSERT_ID();'; |
623 | 623 | if (($ret_code=$db_conn->query($sql)) === false) { |
624 | - $this->logging->log('Erreur recuperation id',ERROR,''); |
|
624 | + $this->logging->log('Erreur recuperation id', ERROR, ''); |
|
625 | 625 | } |
626 | 626 | |
627 | 627 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
628 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
628 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
629 | 629 | $this->trapId=$inserted_id; |
630 | 630 | break; |
631 | 631 | default: |
632 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,''); |
|
632 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, ''); |
|
633 | 633 | } |
634 | - $this->logging->log('id found: '.$this->trapId,INFO ); |
|
634 | + $this->logging->log('id found: '.$this->trapId, INFO); |
|
635 | 635 | |
636 | 636 | // Fill trap extended data table |
637 | 637 | foreach ($this->trapDataExt as $value) { |
638 | 638 | // TODO : detect if trap value is encoded and decode it to UTF-8 for database |
639 | 639 | $firstcol=1; |
640 | - $value->trap_id = $this->trapId; |
|
640 | + $value->trap_id=$this->trapId; |
|
641 | 641 | $insert_col=''; |
642 | 642 | $insert_val=''; |
643 | 643 | foreach ($value as $col => $val) |
644 | 644 | { |
645 | - if ($firstcol==0) |
|
645 | + if ($firstcol == 0) |
|
646 | 646 | { |
647 | - $insert_col .=','; |
|
648 | - $insert_val .=','; |
|
647 | + $insert_col.=','; |
|
648 | + $insert_val.=','; |
|
649 | 649 | } |
650 | - $insert_col .= $col; |
|
651 | - $insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val); |
|
650 | + $insert_col.=$col; |
|
651 | + $insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val); |
|
652 | 652 | $firstcol=0; |
653 | 653 | } |
654 | 654 | |
655 | - $sql= 'INSERT INTO '.$this->dbPrefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');'; |
|
655 | + $sql='INSERT INTO '.$this->dbPrefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');'; |
|
656 | 656 | |
657 | 657 | if ($db_conn->query($sql) === false) { |
658 | - $this->logging->log('Erreur insertion data : ' . $sql,WARN,''); |
|
658 | + $this->logging->log('Erreur insertion data : '.$sql, WARN, ''); |
|
659 | 659 | } |
660 | 660 | } |
661 | 661 | } |
@@ -665,14 +665,14 @@ discard block |
||
665 | 665 | * @param $oid string oid in numeric |
666 | 666 | * @return mixed|boolean : PDO object or false |
667 | 667 | */ |
668 | - protected function getRules($ip,$oid) |
|
668 | + protected function getRules($ip, $oid) |
|
669 | 669 | { |
670 | 670 | $db_conn=$this->trapsDB->db_connect_trap(); |
671 | 671 | // fetch rules based on IP in rule and OID |
672 | 672 | $sql='SELECT * from '.$this->dbPrefix.'rules WHERE trap_oid=\''.$oid.'\' '; |
673 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
673 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
674 | 674 | if (($ret_code=$db_conn->query($sql)) === false) { |
675 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
675 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
676 | 676 | return false; |
677 | 677 | } |
678 | 678 | $rules_all=$ret_code->fetchAll(); |
@@ -681,7 +681,7 @@ discard block |
||
681 | 681 | $rule_ret_key=0; |
682 | 682 | foreach ($rules_all as $key => $rule) |
683 | 683 | { |
684 | - if ($rule['ip4']==$ip || $rule['ip6']==$ip) |
|
684 | + if ($rule['ip4'] == $ip || $rule['ip6'] == $ip) |
|
685 | 685 | { |
686 | 686 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
687 | 687 | //TODO : get host name by API (and check if correct in rule). |
@@ -689,7 +689,7 @@ discard block |
||
689 | 689 | continue; |
690 | 690 | } |
691 | 691 | // TODO : get hosts IP by API |
692 | - if (isset($rule['host_group_name']) && $rule['host_group_name']!=null) |
|
692 | + if (isset($rule['host_group_name']) && $rule['host_group_name'] != null) |
|
693 | 693 | { // get ips of group members by oid |
694 | 694 | $db_conn2=$this->trapsDB->db_connect_ido(); |
695 | 695 | $sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name |
@@ -700,15 +700,15 @@ discard block |
||
700 | 700 | LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id |
701 | 701 | WHERE o.name1='".$rule['host_group_name']."';"; |
702 | 702 | if (($ret_code2=$db_conn2->query($sql)) === false) { |
703 | - $this->logging->log('No result in query : ' . $sql,WARN,''); |
|
703 | + $this->logging->log('No result in query : '.$sql, WARN, ''); |
|
704 | 704 | continue; |
705 | 705 | } |
706 | 706 | $grouphosts=$ret_code2->fetchAll(); |
707 | 707 | //echo "rule grp :\n";print_r($grouphosts);echo "\n"; |
708 | - foreach ( $grouphosts as $host) |
|
708 | + foreach ($grouphosts as $host) |
|
709 | 709 | { |
710 | 710 | //echo $host['ip4']."\n"; |
711 | - if ($host['ip4']==$ip || $host['ip6']==$ip) |
|
711 | + if ($host['ip4'] == $ip || $host['ip6'] == $ip) |
|
712 | 712 | { |
713 | 713 | //echo "Rule added \n"; |
714 | 714 | $rules_ret[$rule_ret_key]=$rules_all[$key]; |
@@ -731,7 +731,7 @@ discard block |
||
731 | 731 | $db_conn=$this->trapsDB->db_connect_trap(); |
732 | 732 | $sql="UPDATE ".$this->dbPrefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');"; |
733 | 733 | if ($db_conn->query($sql) === false) { |
734 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
734 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
735 | 735 | } |
736 | 736 | } |
737 | 737 | |
@@ -743,23 +743,23 @@ discard block |
||
743 | 743 | * @param string $display |
744 | 744 | * @returnn bool true is service check was sent without error |
745 | 745 | */ |
746 | - public function serviceCheckResult($host,$service,$state,$display) |
|
746 | + public function serviceCheckResult($host, $service, $state, $display) |
|
747 | 747 | { |
748 | 748 | if ($this->apiUse === false) |
749 | 749 | { |
750 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
751 | - $host.';' .$service .';' . $state . ';'.$display; |
|
752 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
750 | + $send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'. |
|
751 | + $host.';'.$service.';'.$state.';'.$display; |
|
752 | + $this->logging->log($send." : to : ".$this->icinga2cmd, INFO); |
|
753 | 753 | |
754 | 754 | // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
755 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
755 | + exec('echo "'.$send.'" > '.$this->icinga2cmd); |
|
756 | 756 | return true; |
757 | 757 | } |
758 | 758 | else |
759 | 759 | { |
760 | 760 | // Get perfdata if found |
761 | 761 | $matches=array(); |
762 | - if (preg_match('/(.*)\|(.*)/',$display,$matches) == 1) |
|
762 | + if (preg_match('/(.*)\|(.*)/', $display, $matches) == 1) |
|
763 | 763 | { |
764 | 764 | $display=$matches[1]; |
765 | 765 | $perfdata=$matches[2]; |
@@ -769,17 +769,17 @@ discard block |
||
769 | 769 | $perfdata=''; |
770 | 770 | } |
771 | 771 | |
772 | - $api = $this->getAPI(); |
|
772 | + $api=$this->getAPI(); |
|
773 | 773 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
774 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display,$perfdata); |
|
774 | + list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display, $perfdata); |
|
775 | 775 | if ($retcode == false) |
776 | 776 | { |
777 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
777 | + $this->logging->log("Error sending result : ".$retmessage, WARN, ''); |
|
778 | 778 | return false; |
779 | 779 | } |
780 | 780 | else |
781 | 781 | { |
782 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
782 | + $this->logging->log("Sent result : ".$retmessage, INFO); |
|
783 | 783 | return true; |
784 | 784 | } |
785 | 785 | } |
@@ -787,7 +787,7 @@ discard block |
||
787 | 787 | |
788 | 788 | public function getHostByIP($ip) |
789 | 789 | { |
790 | - $api = $this->getAPI(); |
|
790 | + $api=$this->getAPI(); |
|
791 | 791 | $api->setCredentials($this->apiUsername, $this->apiPassword); |
792 | 792 | return $api->getHostByIP($ip); |
793 | 793 | } |
@@ -800,35 +800,35 @@ discard block |
||
800 | 800 | protected function applyDisplay($display) |
801 | 801 | { |
802 | 802 | $matches=array(); |
803 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$display,$matches) == 1) |
|
803 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $display, $matches) == 1) |
|
804 | 804 | { |
805 | 805 | $oid=$matches[1]; |
806 | 806 | $found=0; |
807 | 807 | // Test and transform regexp |
808 | - $oidR = $this->ruleClass->regexp_eval($oid); |
|
808 | + $oidR=$this->ruleClass->regexp_eval($oid); |
|
809 | 809 | |
810 | - foreach($this->trapDataExt as $val) |
|
810 | + foreach ($this->trapDataExt as $val) |
|
811 | 811 | { |
812 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
812 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
813 | 813 | { |
814 | - $val->value=preg_replace('/"/','',$val->value); |
|
814 | + $val->value=preg_replace('/"/', '', $val->value); |
|
815 | 815 | $rep=0; |
816 | - $display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep); |
|
817 | - if ($rep==0) |
|
816 | + $display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep); |
|
817 | + if ($rep == 0) |
|
818 | 818 | { |
819 | - $this->logging->log("Error in display",WARN,''); |
|
819 | + $this->logging->log("Error in display", WARN, ''); |
|
820 | 820 | return $display; |
821 | 821 | } |
822 | 822 | $found=1; |
823 | 823 | break; |
824 | 824 | } |
825 | 825 | } |
826 | - if ($found==0) |
|
826 | + if ($found == 0) |
|
827 | 827 | { |
828 | - $display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep); |
|
829 | - if ($rep==0) |
|
828 | + $display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep); |
|
829 | + if ($rep == 0) |
|
830 | 830 | { |
831 | - $this->logging->log("Error in display",WARN,''); |
|
831 | + $this->logging->log("Error in display", WARN, ''); |
|
832 | 832 | return $display; |
833 | 833 | } |
834 | 834 | } |
@@ -840,11 +840,11 @@ discard block |
||
840 | 840 | */ |
841 | 841 | public function applyRules() |
842 | 842 | { |
843 | - $rules = $this->getRules($this->trapData['source_ip'],$this->trapData['trap_oid']); |
|
843 | + $rules=$this->getRules($this->trapData['source_ip'], $this->trapData['trap_oid']); |
|
844 | 844 | |
845 | - if ($rules===false || count($rules)==0) |
|
845 | + if ($rules === false || count($rules) == 0) |
|
846 | 846 | { |
847 | - $this->logging->log('No rules found for this trap',INFO ); |
|
847 | + $this->logging->log('No rules found for this trap', INFO); |
|
848 | 848 | $this->trapData['status']='unknown'; |
849 | 849 | $this->trapToDb=true; |
850 | 850 | return; |
@@ -859,59 +859,59 @@ discard block |
||
859 | 859 | $service_name=$rule['service_name']; |
860 | 860 | |
861 | 861 | $display=$this->applyDisplay($rule['display']); |
862 | - $this->trapAction = ($this->trapAction==null)? '' : $this->trapAction . ', '; |
|
862 | + $this->trapAction=($this->trapAction == null) ? '' : $this->trapAction.', '; |
|
863 | 863 | try |
864 | 864 | { |
865 | - $this->logging->log('Rule to eval : '.$rule['rule'],INFO ); |
|
866 | - $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt) ; |
|
865 | + $this->logging->log('Rule to eval : '.$rule['rule'], INFO); |
|
866 | + $evalr=$this->ruleClass->eval_rule($rule['rule'], $this->trapDataExt); |
|
867 | 867 | //->eval_rule($rule['rule']); |
868 | 868 | |
869 | 869 | if ($evalr == true) |
870 | 870 | { |
871 | 871 | //$this->logging->log('rules OOK: '.print_r($rule),INFO ); |
872 | 872 | $action=$rule['action_match']; |
873 | - $this->logging->log('action OK : '.$action,INFO ); |
|
873 | + $this->logging->log('action OK : '.$action, INFO); |
|
874 | 874 | if ($action >= 0) |
875 | 875 | { |
876 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
|
876 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
877 | 877 | { |
878 | 878 | $this->trapAction.='Error sending status : check cmd/API'; |
879 | 879 | } |
880 | 880 | else |
881 | 881 | { |
882 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
882 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
883 | 883 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
884 | 884 | } |
885 | 885 | } |
886 | 886 | else |
887 | 887 | { |
888 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
888 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
889 | 889 | } |
890 | - $this->trapToDb=($action==-2)?false:true; |
|
890 | + $this->trapToDb=($action == -2) ?false:true; |
|
891 | 891 | } |
892 | 892 | else |
893 | 893 | { |
894 | 894 | //$this->logging->log('rules KOO : '.print_r($rule),INFO ); |
895 | 895 | |
896 | 896 | $action=$rule['action_nomatch']; |
897 | - $this->logging->log('action NOK : '.$action,INFO ); |
|
897 | + $this->logging->log('action NOK : '.$action, INFO); |
|
898 | 898 | if ($action >= 0) |
899 | 899 | { |
900 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
900 | + if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false) |
|
901 | 901 | { |
902 | 902 | $this->trapAction.='Error sending status : check cmd/API'; |
903 | 903 | } |
904 | 904 | else |
905 | 905 | { |
906 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
906 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
907 | 907 | $this->trapAction.='Status '.$action.' to '.$host_name.'/'.$service_name; |
908 | 908 | } |
909 | 909 | } |
910 | 910 | else |
911 | 911 | { |
912 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
912 | + $this->add_rule_match($rule['id'], $rule['num_match'] + 1); |
|
913 | 913 | } |
914 | - $this->trapToDb=($action==-2)?false:true; |
|
914 | + $this->trapToDb=($action == -2) ?false:true; |
|
915 | 915 | } |
916 | 916 | // Put name in source_name |
917 | 917 | if (!isset($this->trapData['source_name'])) |
@@ -920,7 +920,7 @@ discard block |
||
920 | 920 | } |
921 | 921 | else |
922 | 922 | { |
923 | - if (!preg_match('/'.$rule['host_name'].'/',$this->trapData['source_name'])) |
|
923 | + if (!preg_match('/'.$rule['host_name'].'/', $this->trapData['source_name'])) |
|
924 | 924 | { // only add if not present |
925 | 925 | $this->trapData['source_name'].=','.$rule['host_name']; |
926 | 926 | } |
@@ -928,13 +928,13 @@ discard block |
||
928 | 928 | } |
929 | 929 | catch (Exception $e) |
930 | 930 | { |
931 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
931 | + $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, ''); |
|
932 | 932 | $this->trapAction.=' ERR : '.$e->getMessage(); |
933 | 933 | $this->trapData['status']='error'; |
934 | 934 | } |
935 | 935 | |
936 | 936 | } |
937 | - if ($this->trapData['status']=='error') |
|
937 | + if ($this->trapData['status'] == 'error') |
|
938 | 938 | { |
939 | 939 | $this->trapToDb=true; // Always put errors in DB for the use can see |
940 | 940 | } |
@@ -950,13 +950,13 @@ discard block |
||
950 | 950 | public function add_rule_final($time) |
951 | 951 | { |
952 | 952 | $db_conn=$this->trapsDB->db_connect_trap(); |
953 | - if ($this->trapAction==null) |
|
953 | + if ($this->trapAction == null) |
|
954 | 954 | { |
955 | 955 | $this->trapAction='No action'; |
956 | 956 | } |
957 | 957 | $sql="UPDATE ".$this->dbPrefix."received SET process_time = '".$time."' , status_detail='".$this->trapAction."' WHERE (id = '".$this->trapId."');"; |
958 | 958 | if ($db_conn->query($sql) === false) { |
959 | - $this->logging->log('Error in update query : ' . $sql,WARN,''); |
|
959 | + $this->logging->log('Error in update query : '.$sql, WARN, ''); |
|
960 | 960 | } |
961 | 961 | } |
962 | 962 | |
@@ -979,7 +979,7 @@ discard block |
||
979 | 979 | WHERE s.current_state != 0;"; |
980 | 980 | $db_conn=$this->trapsDB->db_connect_ido(); |
981 | 981 | if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception. |
982 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
982 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
983 | 983 | return 0; |
984 | 984 | } |
985 | 985 | $services=$services_db->fetchAll(); |
@@ -988,7 +988,7 @@ discard block |
||
988 | 988 | $sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->dbPrefix."rules where revert_ok != 0;"; |
989 | 989 | $db_conn2=$this->trapsDB->db_connect_trap(); |
990 | 990 | if (($rules_db=$db_conn2->query($sql_query)) === false) { |
991 | - $this->logging->log('No result in query : ' . $sql_query,ERROR,''); |
|
991 | + $this->logging->log('No result in query : '.$sql_query, ERROR, ''); |
|
992 | 992 | return 0; |
993 | 993 | } |
994 | 994 | $rules=$rules_db->fetchAll(); |
@@ -1004,13 +1004,13 @@ discard block |
||
1004 | 1004 | $service['host_name'] == $rule['host_name'] && |
1005 | 1005 | ($service['last_check'] + $rule['revert_ok']) < $now) |
1006 | 1006 | { |
1007 | - $this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
1007 | + $this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds'); |
|
1008 | 1008 | $numreset++; |
1009 | 1009 | } |
1010 | 1010 | } |
1011 | 1011 | } |
1012 | 1012 | echo "\n"; |
1013 | - echo $numreset . " service(s) reset to OK\n"; |
|
1013 | + echo $numreset." service(s) reset to OK\n"; |
|
1014 | 1014 | return 0; |
1015 | 1015 | |
1016 | 1016 | } |
@@ -65,8 +65,7 @@ discard block |
||
65 | 65 | if (isset($postData['host'])) |
66 | 66 | { |
67 | 67 | $host=$postData['host']; |
68 | - } |
|
69 | - else |
|
68 | + } else |
|
70 | 69 | { |
71 | 70 | $this->_helper->json(array('status'=>'No Hosts','hostid' => -1)); |
72 | 71 | return; |
@@ -77,8 +76,7 @@ discard block |
||
77 | 76 | { |
78 | 77 | $this->_helper->json(array('status'=>'More than one host matches','hostid' => -1)); |
79 | 78 | return; |
80 | - } |
|
81 | - else if (count($hostArray) == 0) |
|
79 | + } else if (count($hostArray) == 0) |
|
82 | 80 | { |
83 | 81 | $this->_helper->json(array('status'=>'No host matches','hostid' => -1)); |
84 | 82 | return; |
@@ -114,8 +112,7 @@ discard block |
||
114 | 112 | { |
115 | 113 | $this->_helper->json(array('status'=>'More than one hostgroup matches','hostid' => -1)); |
116 | 114 | return; |
117 | - } |
|
118 | - else if (count($hostArray) == 0) |
|
115 | + } else if (count($hostArray) == 0) |
|
119 | 116 | { |
120 | 117 | $this->_helper->json(array('status'=>'No hostgroup matches','hostid' => -1)); |
121 | 118 | return; |
@@ -146,8 +143,7 @@ discard block |
||
146 | 143 | { |
147 | 144 | $traplist=$this->getMIB()->getTrapList($mib); |
148 | 145 | $retTraps=array('status'=>'OK','traps' => $traplist); |
149 | - } |
|
150 | - catch (Exception $e) |
|
146 | + } catch (Exception $e) |
|
151 | 147 | { |
152 | 148 | $retTraps=array('status' => 'Error getting mibs'); |
153 | 149 | } |
@@ -169,8 +165,7 @@ discard block |
||
169 | 165 | { |
170 | 166 | $objectlist=$this->getMIB()->getObjectList($trap); |
171 | 167 | $retObjects=array('status'=>'OK','objects' => $objectlist); |
172 | - } |
|
173 | - catch (Exception $e) |
|
168 | + } catch (Exception $e) |
|
174 | 169 | { |
175 | 170 | $retObjects=array('status' => 'not found'); |
176 | 171 | } |
@@ -185,8 +180,7 @@ discard block |
||
185 | 180 | try |
186 | 181 | { |
187 | 182 | $miblist=$this->getMIB()->getMIBList(); |
188 | - } |
|
189 | - catch (Exception $e) |
|
183 | + } catch (Exception $e) |
|
190 | 184 | { |
191 | 185 | $miblist=array('Error getting mibs'); |
192 | 186 | } |
@@ -209,8 +203,7 @@ discard block |
||
209 | 203 | { |
210 | 204 | $this->_helper->json(array('status'=>'Not found')); |
211 | 205 | return; |
212 | - } |
|
213 | - else |
|
206 | + } else |
|
214 | 207 | { |
215 | 208 | $this->_helper->json( |
216 | 209 | array('status'=>'OK', |
@@ -244,8 +237,7 @@ discard block |
||
244 | 237 | try |
245 | 238 | { |
246 | 239 | $this->setDBConfigValue('db_remove_days',$days); |
247 | - } |
|
248 | - catch (Exception $e) |
|
240 | + } catch (Exception $e) |
|
249 | 241 | { |
250 | 242 | $this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
251 | 243 | return; |
@@ -263,8 +255,7 @@ discard block |
||
263 | 255 | $trap = new Trap($icingaweb2_etc); |
264 | 256 | $trap->setLogging($debug_level,'syslog'); |
265 | 257 | $trap->eraseOldTraps($days); |
266 | - } |
|
267 | - catch (Exception $e) |
|
258 | + } catch (Exception $e) |
|
268 | 259 | { |
269 | 260 | $this->_helper->json(array('status'=>'execute error : '.$e->getMessage() )); |
270 | 261 | return; |
@@ -301,14 +292,12 @@ discard block |
||
301 | 292 | $this->_helper->json(array('status'=>'File not writable : '.$file)); |
302 | 293 | return; |
303 | 294 | } |
304 | - } |
|
305 | - else |
|
295 | + } else |
|
306 | 296 | { |
307 | 297 | if ($destination != 'file') |
308 | 298 | { |
309 | 299 | $file=null; |
310 | - } |
|
311 | - else |
|
300 | + } else |
|
312 | 301 | { |
313 | 302 | $this->_helper->json(array('status'=>'No file')); |
314 | 303 | return; |
@@ -322,8 +311,7 @@ discard block |
||
322 | 311 | $this->setDBConfigValue('log_destination',$destination); |
323 | 312 | $this->setDBConfigValue('log_file',$file); |
324 | 313 | $this->setDBConfigValue('log_level',$level); |
325 | - } |
|
326 | - catch (Exception $e) |
|
314 | + } catch (Exception $e) |
|
327 | 315 | { |
328 | 316 | $this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
329 | 317 | return; |
@@ -359,8 +347,7 @@ discard block |
||
359 | 347 | // Eval |
360 | 348 | $item=0; |
361 | 349 | $rule=$trap->ruleClass->evaluation($rule,$item); |
362 | - } |
|
363 | - catch (Exception $e) |
|
350 | + } catch (Exception $e) |
|
364 | 351 | { |
365 | 352 | $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
366 | 353 | return; |
@@ -393,8 +380,7 @@ discard block |
||
393 | 380 | $action=($action == 'enable') ? true : false; |
394 | 381 | $retVal=$trap->pluginClass->enablePlugin($pluginName, $action); |
395 | 382 | |
396 | - } |
|
397 | - catch (Exception $e) |
|
383 | + } catch (Exception $e) |
|
398 | 384 | { |
399 | 385 | $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
400 | 386 | return; |
@@ -402,8 +388,7 @@ discard block |
||
402 | 388 | if ($retVal === true) |
403 | 389 | { |
404 | 390 | $this->_helper->json(array('status'=>'OK')); |
405 | - } |
|
406 | - else |
|
391 | + } else |
|
407 | 392 | { |
408 | 393 | $this->_helper->json(array('status'=>'Error, see logs')); |
409 | 394 | } |
@@ -434,8 +419,7 @@ discard block |
||
434 | 419 | $functionString = $trap->ruleClass->eval_cleanup($functionString); |
435 | 420 | // Eval functions |
436 | 421 | $result = $trap->pluginClass->evaluateFunctionString($functionString); |
437 | - } |
|
438 | - catch (Exception $e) |
|
422 | + } catch (Exception $e) |
|
439 | 423 | { |
440 | 424 | $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
441 | 425 | return; |
@@ -12,8 +12,8 @@ discard block |
||
12 | 12 | { |
13 | 13 | |
14 | 14 | /** Get host list with filter (IP or name) : host=<filter> |
15 | - * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
16 | - */ |
|
15 | + * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
16 | + */ |
|
17 | 17 | public function gethostsAction() |
18 | 18 | { |
19 | 19 | $postData=$this->getRequest()->getPost(); |
@@ -32,8 +32,8 @@ discard block |
||
32 | 32 | } |
33 | 33 | |
34 | 34 | /** Get hostgroup list with filter (name) : hostgroup=<hostFilter> |
35 | - * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
36 | - */ |
|
35 | + * returns in JSON : status=>OK/NOK hosts=>array of hosts |
|
36 | + */ |
|
37 | 37 | public function gethostgroupsAction() |
38 | 38 | { |
39 | 39 | $postData=$this->getRequest()->getPost(); |
@@ -52,11 +52,11 @@ discard block |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | /** Get service list by host name ( host=<host> ) |
55 | - * returns in JSON : |
|
56 | - * status=>OK/No services found/More than one host matches |
|
57 | - * services=>array of services (name) |
|
58 | - * hostid = host object id or -1 if not found. |
|
59 | - */ |
|
55 | + * returns in JSON : |
|
56 | + * status=>OK/No services found/More than one host matches |
|
57 | + * services=>array of services (name) |
|
58 | + * hostid = host object id or -1 if not found. |
|
59 | + */ |
|
60 | 60 | public function getservicesAction() |
61 | 61 | { |
62 | 62 | $postData=$this->getRequest()->getPost(); |
@@ -98,11 +98,11 @@ discard block |
||
98 | 98 | } |
99 | 99 | |
100 | 100 | /** Get service list by host group ( name=<host> ) |
101 | - * returns in JSON : |
|
102 | - * status=>OK/No services found/More than one host matches |
|
103 | - * services=>array of services (name) |
|
104 | - * groupid = group object id or -1 if not found. |
|
105 | - */ |
|
101 | + * returns in JSON : |
|
102 | + * status=>OK/No services found/More than one host matches |
|
103 | + * services=>array of services (name) |
|
104 | + * groupid = group object id or -1 if not found. |
|
105 | + */ |
|
106 | 106 | public function gethostgroupservicesAction() |
107 | 107 | { |
108 | 108 | $postData=$this->getRequest()->getPost(); |
@@ -132,10 +132,10 @@ discard block |
||
132 | 132 | } |
133 | 133 | |
134 | 134 | /** Get traps from mib : entry : mib=<mib> |
135 | - * returns in JSON : |
|
136 | - * status=>OK/No mib/Error getting mibs |
|
137 | - * traps=>array of array( oid -> name) |
|
138 | - */ |
|
135 | + * returns in JSON : |
|
136 | + * status=>OK/No mib/Error getting mibs |
|
137 | + * traps=>array of array( oid -> name) |
|
138 | + */ |
|
139 | 139 | public function gettrapsAction() |
140 | 140 | { |
141 | 141 | $postData=$this->getRequest()->getPost(); |
@@ -155,10 +155,10 @@ discard block |
||
155 | 155 | } |
156 | 156 | |
157 | 157 | /** Get trap objects from mib : entry : trap=<oid> |
158 | - * returns in JSON : |
|
159 | - * status=>OK/no trap/not found |
|
160 | - * objects=>array of array( oid -> name, oid->mib) |
|
161 | - */ |
|
158 | + * returns in JSON : |
|
159 | + * status=>OK/no trap/not found |
|
160 | + * objects=>array of array( oid -> name, oid->mib) |
|
161 | + */ |
|
162 | 162 | public function gettrapobjectsAction() |
163 | 163 | { |
164 | 164 | $postData=$this->getRequest()->getPost(); |
@@ -178,8 +178,8 @@ discard block |
||
178 | 178 | } |
179 | 179 | |
180 | 180 | /** Get list of all loaded mibs : entry : none |
181 | - * return : array of strings. |
|
182 | - */ |
|
181 | + * return : array of strings. |
|
182 | + */ |
|
183 | 183 | public function getmiblistAction() |
184 | 184 | { |
185 | 185 | try |
@@ -194,10 +194,10 @@ discard block |
||
194 | 194 | } |
195 | 195 | |
196 | 196 | /** Get MIB::Name from OID : entry : oid |
197 | - * status=>OK/No oid/not found |
|
198 | - * mib=>string |
|
199 | - * name=>string |
|
200 | - */ |
|
197 | + * status=>OK/No oid/not found |
|
198 | + * mib=>string |
|
199 | + * name=>string |
|
200 | + */ |
|
201 | 201 | public function translateoidAction() |
202 | 202 | { |
203 | 203 | $postData=$this->getRequest()->getPost(); |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | 'name' => $object['name'], |
219 | 219 | 'type' => $object['type'], |
220 | 220 | 'type_enum' => $object['type_enum'], |
221 | - 'description' => $object['description'] |
|
221 | + 'description' => $object['description'] |
|
222 | 222 | ) |
223 | 223 | ); |
224 | 224 | } |
@@ -226,10 +226,10 @@ discard block |
||
226 | 226 | } |
227 | 227 | |
228 | 228 | /** Save or execute database purge of <n> days |
229 | - * days=>int |
|
230 | - * action=>save/execute |
|
231 | - * return : status=>OK/Message error |
|
232 | - */ |
|
229 | + * days=>int |
|
230 | + * action=>save/execute |
|
231 | + * return : status=>OK/Message error |
|
232 | + */ |
|
233 | 233 | public function dbmaintenanceAction() |
234 | 234 | { |
235 | 235 | |
@@ -275,11 +275,11 @@ discard block |
||
275 | 275 | } |
276 | 276 | |
277 | 277 | /** Save log output to db |
278 | - * destination=>log destination |
|
279 | - * file=>file name |
|
280 | - * level => int |
|
281 | - * return : status=>OK/Message error |
|
282 | - */ |
|
278 | + * destination=>log destination |
|
279 | + * file=>file name |
|
280 | + * level => int |
|
281 | + * return : status=>OK/Message error |
|
282 | + */ |
|
283 | 283 | public function logdestinationAction() |
284 | 284 | { |
285 | 285 | $postData=$this->getRequest()->getPost(); |
@@ -298,8 +298,8 @@ discard block |
||
298 | 298 | $fileHandler=@fopen($file,'w'); |
299 | 299 | if ($fileHandler == false) |
300 | 300 | { // File os note writabe / cannot create |
301 | - $this->_helper->json(array('status'=>'File not writable : '.$file)); |
|
302 | - return; |
|
301 | + $this->_helper->json(array('status'=>'File not writable : '.$file)); |
|
302 | + return; |
|
303 | 303 | } |
304 | 304 | } |
305 | 305 | else |
@@ -341,33 +341,33 @@ discard block |
||
341 | 341 | public function testruleAction() |
342 | 342 | { |
343 | 343 | |
344 | - $postData=$this->getRequest()->getPost(); |
|
344 | + $postData=$this->getRequest()->getPost(); |
|
345 | 345 | |
346 | - $rule = $this->checkPostVar($postData, 'rule', '.*'); |
|
346 | + $rule = $this->checkPostVar($postData, 'rule', '.*'); |
|
347 | 347 | |
348 | - $action = $this->checkPostVar($postData, 'action', 'evaluate'); |
|
348 | + $action = $this->checkPostVar($postData, 'action', 'evaluate'); |
|
349 | 349 | |
350 | - if ($action == 'evaluate') |
|
351 | - { |
|
352 | - try |
|
353 | - { |
|
354 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
355 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
356 | - $trap = new Trap($icingaweb2_etc); |
|
357 | - // Cleanup spaces before eval |
|
358 | - $rule=$trap->ruleClass->eval_cleanup($rule); |
|
359 | - // Eval |
|
360 | - $item=0; |
|
361 | - $rule=$trap->ruleClass->evaluation($rule,$item); |
|
362 | - } |
|
363 | - catch (Exception $e) |
|
364 | - { |
|
365 | - $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
|
366 | - return; |
|
367 | - } |
|
368 | - $return=($rule==true)?'true':'false'; |
|
369 | - $this->_helper->json(array('status'=>'OK', 'message' => $return)); |
|
370 | - } |
|
350 | + if ($action == 'evaluate') |
|
351 | + { |
|
352 | + try |
|
353 | + { |
|
354 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
355 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
356 | + $trap = new Trap($icingaweb2_etc); |
|
357 | + // Cleanup spaces before eval |
|
358 | + $rule=$trap->ruleClass->eval_cleanup($rule); |
|
359 | + // Eval |
|
360 | + $item=0; |
|
361 | + $rule=$trap->ruleClass->evaluation($rule,$item); |
|
362 | + } |
|
363 | + catch (Exception $e) |
|
364 | + { |
|
365 | + $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
|
366 | + return; |
|
367 | + } |
|
368 | + $return=($rule==true)?'true':'false'; |
|
369 | + $this->_helper->json(array('status'=>'OK', 'message' => $return)); |
|
370 | + } |
|
371 | 371 | |
372 | 372 | } |
373 | 373 | |
@@ -378,35 +378,35 @@ discard block |
||
378 | 378 | */ |
379 | 379 | public function pluginAction() |
380 | 380 | { |
381 | - $postData=$this->getRequest()->getPost(); |
|
381 | + $postData=$this->getRequest()->getPost(); |
|
382 | 382 | |
383 | - $pluginName = $this->checkPostVar($postData, 'name', '.*'); |
|
383 | + $pluginName = $this->checkPostVar($postData, 'name', '.*'); |
|
384 | 384 | |
385 | - $action = $this->checkPostVar($postData, 'action', 'enable|disable'); |
|
385 | + $action = $this->checkPostVar($postData, 'action', 'enable|disable'); |
|
386 | 386 | |
387 | - try |
|
388 | - { |
|
389 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
390 | - $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
391 | - $trap = new Trap($icingaweb2_etc); |
|
392 | - // Enable plugin. |
|
393 | - $action=($action == 'enable') ? true : false; |
|
394 | - $retVal=$trap->pluginClass->enablePlugin($pluginName, $action); |
|
387 | + try |
|
388 | + { |
|
389 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
390 | + $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
391 | + $trap = new Trap($icingaweb2_etc); |
|
392 | + // Enable plugin. |
|
393 | + $action=($action == 'enable') ? true : false; |
|
394 | + $retVal=$trap->pluginClass->enablePlugin($pluginName, $action); |
|
395 | 395 | |
396 | - } |
|
397 | - catch (Exception $e) |
|
398 | - { |
|
399 | - $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
400 | - return; |
|
401 | - } |
|
402 | - if ($retVal === true) |
|
403 | - { |
|
404 | - $this->_helper->json(array('status'=>'OK')); |
|
405 | - } |
|
406 | - else |
|
407 | - { |
|
408 | - $this->_helper->json(array('status'=>'Error, see logs')); |
|
409 | - } |
|
396 | + } |
|
397 | + catch (Exception $e) |
|
398 | + { |
|
399 | + $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
400 | + return; |
|
401 | + } |
|
402 | + if ($retVal === true) |
|
403 | + { |
|
404 | + $this->_helper->json(array('status'=>'OK')); |
|
405 | + } |
|
406 | + else |
|
407 | + { |
|
408 | + $this->_helper->json(array('status'=>'Error, see logs')); |
|
409 | + } |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | /** Function evaluation |
@@ -416,49 +416,49 @@ discard block |
||
416 | 416 | */ |
417 | 417 | public function functionAction() |
418 | 418 | { |
419 | - $postData=$this->getRequest()->getPost(); |
|
419 | + $postData=$this->getRequest()->getPost(); |
|
420 | 420 | |
421 | - $functionString = $this->checkPostVar($postData, 'function', '.*'); |
|
421 | + $functionString = $this->checkPostVar($postData, 'function', '.*'); |
|
422 | 422 | |
423 | - $this->checkPostVar($postData, 'action', 'evaluate'); |
|
423 | + $this->checkPostVar($postData, 'action', 'evaluate'); |
|
424 | 424 | |
425 | - // Only one action possible for now, no tests on action. |
|
426 | - try |
|
427 | - { |
|
428 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
429 | - $icingaweb2Etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
430 | - $trap = new Trap($icingaweb2Etc); |
|
431 | - // load all plugins in case tested function is not enabled. |
|
432 | - $trap->pluginClass->registerAllPlugins(false); |
|
433 | - // Clean all spaces |
|
434 | - $functionString = $trap->ruleClass->eval_cleanup($functionString); |
|
435 | - // Eval functions |
|
436 | - $result = $trap->pluginClass->evaluateFunctionString($functionString); |
|
437 | - } |
|
438 | - catch (Exception $e) |
|
439 | - { |
|
440 | - $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
441 | - return; |
|
442 | - } |
|
425 | + // Only one action possible for now, no tests on action. |
|
426 | + try |
|
427 | + { |
|
428 | + require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
429 | + $icingaweb2Etc=$this->Config()->get('config', 'icingaweb2_etc'); |
|
430 | + $trap = new Trap($icingaweb2Etc); |
|
431 | + // load all plugins in case tested function is not enabled. |
|
432 | + $trap->pluginClass->registerAllPlugins(false); |
|
433 | + // Clean all spaces |
|
434 | + $functionString = $trap->ruleClass->eval_cleanup($functionString); |
|
435 | + // Eval functions |
|
436 | + $result = $trap->pluginClass->evaluateFunctionString($functionString); |
|
437 | + } |
|
438 | + catch (Exception $e) |
|
439 | + { |
|
440 | + $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
441 | + return; |
|
442 | + } |
|
443 | 443 | |
444 | - $result = ($result === true)?'True':'False'; |
|
445 | - $this->_helper->json(array('status'=>'OK','message' => $result)); |
|
444 | + $result = ($result === true)?'True':'False'; |
|
445 | + $this->_helper->json(array('status'=>'OK','message' => $result)); |
|
446 | 446 | } |
447 | 447 | |
448 | - /************** Utilities **********************/ |
|
448 | + /************** Utilities **********************/ |
|
449 | 449 | |
450 | 450 | private function checkPostVar(array $postData,string $postVar, string $validRegexp) : string |
451 | 451 | { |
452 | - if (!isset ($postData[$postVar])) |
|
453 | - { |
|
454 | - $this->_helper->json(array('status'=>'No ' . $postVar)); |
|
455 | - return ''; |
|
456 | - } |
|
457 | - if (preg_match('/'.$validRegexp.'/', $postData[$postVar]) != 1) |
|
458 | - { |
|
459 | - $this->_helper->json(array('status'=>'Unknown ' . $postVar . ' value '.$postData[$postVar])); |
|
460 | - return ''; |
|
461 | - } |
|
462 | - return $postData[$postVar]; |
|
452 | + if (!isset ($postData[$postVar])) |
|
453 | + { |
|
454 | + $this->_helper->json(array('status'=>'No ' . $postVar)); |
|
455 | + return ''; |
|
456 | + } |
|
457 | + if (preg_match('/'.$validRegexp.'/', $postData[$postVar]) != 1) |
|
458 | + { |
|
459 | + $this->_helper->json(array('status'=>'Unknown ' . $postVar . ' value '.$postData[$postVar])); |
|
460 | + return ''; |
|
461 | + } |
|
462 | + return $postData[$postVar]; |
|
463 | 463 | } |
464 | 464 | } |
@@ -18,14 +18,14 @@ discard block |
||
18 | 18 | { |
19 | 19 | $postData=$this->getRequest()->getPost(); |
20 | 20 | |
21 | - $hostFilter = $this->checkPostVar($postData, 'hostFilter', '.*'); |
|
21 | + $hostFilter=$this->checkPostVar($postData, 'hostFilter', '.*'); |
|
22 | 22 | |
23 | - $retHosts=array('status'=>'OK','hosts' => array()); |
|
23 | + $retHosts=array('status'=>'OK', 'hosts' => array()); |
|
24 | 24 | |
25 | 25 | $hosts=$this->getHostByIP($hostFilter); |
26 | 26 | foreach ($hosts as $val) |
27 | 27 | { |
28 | - array_push($retHosts['hosts'],$val->name); |
|
28 | + array_push($retHosts['hosts'], $val->name); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | $this->_helper->json($retHosts); |
@@ -38,14 +38,14 @@ discard block |
||
38 | 38 | { |
39 | 39 | $postData=$this->getRequest()->getPost(); |
40 | 40 | |
41 | - $hostFilter = $this->checkPostVar($postData, 'hostFilter', '.*'); |
|
41 | + $hostFilter=$this->checkPostVar($postData, 'hostFilter', '.*'); |
|
42 | 42 | |
43 | - $retHosts=array('status'=>'OK','hosts' => array()); |
|
43 | + $retHosts=array('status'=>'OK', 'hosts' => array()); |
|
44 | 44 | |
45 | 45 | $hosts=$this->getHostGroupByName($hostFilter); |
46 | 46 | foreach ($hosts as $val) |
47 | 47 | { |
48 | - array_push($retHosts['hosts'],$val->name); |
|
48 | + array_push($retHosts['hosts'], $val->name); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | $this->_helper->json($retHosts); |
@@ -68,31 +68,31 @@ discard block |
||
68 | 68 | } |
69 | 69 | else |
70 | 70 | { |
71 | - $this->_helper->json(array('status'=>'No Hosts','hostid' => -1)); |
|
71 | + $this->_helper->json(array('status'=>'No Hosts', 'hostid' => -1)); |
|
72 | 72 | return; |
73 | 73 | } |
74 | 74 | |
75 | 75 | $hostArray=$this->getHostByName($host); |
76 | 76 | if (count($hostArray) > 1) |
77 | 77 | { |
78 | - $this->_helper->json(array('status'=>'More than one host matches','hostid' => -1)); |
|
78 | + $this->_helper->json(array('status'=>'More than one host matches', 'hostid' => -1)); |
|
79 | 79 | return; |
80 | 80 | } |
81 | 81 | else if (count($hostArray) == 0) |
82 | 82 | { |
83 | - $this->_helper->json(array('status'=>'No host matches','hostid' => -1)); |
|
83 | + $this->_helper->json(array('status'=>'No host matches', 'hostid' => -1)); |
|
84 | 84 | return; |
85 | 85 | } |
86 | 86 | $services=$this->getServicesByHostid($hostArray[0]->id); |
87 | 87 | if (count($services) < 1) |
88 | 88 | { |
89 | - $this->_helper->json(array('status'=>'No services found for host','hostid' => $hostArray[0]->id)); |
|
89 | + $this->_helper->json(array('status'=>'No services found for host', 'hostid' => $hostArray[0]->id)); |
|
90 | 90 | return; |
91 | 91 | } |
92 | - $retServices=array('status'=>'OK','services' => array(),'hostid' => $hostArray[0]->id); |
|
92 | + $retServices=array('status'=>'OK', 'services' => array(), 'hostid' => $hostArray[0]->id); |
|
93 | 93 | foreach ($services as $val) |
94 | 94 | { |
95 | - array_push($retServices['services'],array($val->id , $val->name)); |
|
95 | + array_push($retServices['services'], array($val->id, $val->name)); |
|
96 | 96 | } |
97 | 97 | $this->_helper->json($retServices); |
98 | 98 | } |
@@ -107,26 +107,26 @@ discard block |
||
107 | 107 | { |
108 | 108 | $postData=$this->getRequest()->getPost(); |
109 | 109 | |
110 | - $host = $this->checkPostVar($postData, 'host', '.+'); |
|
110 | + $host=$this->checkPostVar($postData, 'host', '.+'); |
|
111 | 111 | |
112 | 112 | $hostArray=$this->getHostGroupByName($host); |
113 | 113 | if (count($hostArray) > 1) |
114 | 114 | { |
115 | - $this->_helper->json(array('status'=>'More than one hostgroup matches','hostid' => -1)); |
|
115 | + $this->_helper->json(array('status'=>'More than one hostgroup matches', 'hostid' => -1)); |
|
116 | 116 | return; |
117 | 117 | } |
118 | 118 | else if (count($hostArray) == 0) |
119 | 119 | { |
120 | - $this->_helper->json(array('status'=>'No hostgroup matches','hostid' => -1)); |
|
120 | + $this->_helper->json(array('status'=>'No hostgroup matches', 'hostid' => -1)); |
|
121 | 121 | return; |
122 | 122 | } |
123 | 123 | $services=$this->getServicesByHostGroupid($hostArray[0]->id); |
124 | 124 | if (count($services) < 1) |
125 | 125 | { |
126 | - $this->_helper->json(array('status'=>'No services found for hostgroup','hostid' => $hostArray[0]->id)); |
|
126 | + $this->_helper->json(array('status'=>'No services found for hostgroup', 'hostid' => $hostArray[0]->id)); |
|
127 | 127 | return; |
128 | 128 | } |
129 | - $retServices=array('status'=>'OK','services' => $services,'hostid' => $hostArray[0]->id); |
|
129 | + $retServices=array('status'=>'OK', 'services' => $services, 'hostid' => $hostArray[0]->id); |
|
130 | 130 | |
131 | 131 | $this->_helper->json($retServices); |
132 | 132 | } |
@@ -140,12 +140,12 @@ discard block |
||
140 | 140 | { |
141 | 141 | $postData=$this->getRequest()->getPost(); |
142 | 142 | |
143 | - $mib = $this->checkPostVar($postData, 'mib', '.*'); |
|
143 | + $mib=$this->checkPostVar($postData, 'mib', '.*'); |
|
144 | 144 | |
145 | 145 | try |
146 | 146 | { |
147 | 147 | $traplist=$this->getMIB()->getTrapList($mib); |
148 | - $retTraps=array('status'=>'OK','traps' => $traplist); |
|
148 | + $retTraps=array('status'=>'OK', 'traps' => $traplist); |
|
149 | 149 | } |
150 | 150 | catch (Exception $e) |
151 | 151 | { |
@@ -163,12 +163,12 @@ discard block |
||
163 | 163 | { |
164 | 164 | $postData=$this->getRequest()->getPost(); |
165 | 165 | |
166 | - $trap = $this->checkPostVar($postData, 'trap', '.*'); |
|
166 | + $trap=$this->checkPostVar($postData, 'trap', '.*'); |
|
167 | 167 | |
168 | 168 | try |
169 | 169 | { |
170 | 170 | $objectlist=$this->getMIB()->getObjectList($trap); |
171 | - $retObjects=array('status'=>'OK','objects' => $objectlist); |
|
171 | + $retObjects=array('status'=>'OK', 'objects' => $objectlist); |
|
172 | 172 | } |
173 | 173 | catch (Exception $e) |
174 | 174 | { |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | { |
203 | 203 | $postData=$this->getRequest()->getPost(); |
204 | 204 | |
205 | - $oid = $this->checkPostVar($postData, 'oid', '.*'); |
|
205 | + $oid=$this->checkPostVar($postData, 'oid', '.*'); |
|
206 | 206 | |
207 | 207 | // Try to get oid name from snmptranslate |
208 | 208 | if (($object=$this->getMIB()->translateOID($oid)) == null) |
@@ -235,19 +235,19 @@ discard block |
||
235 | 235 | |
236 | 236 | $postData=$this->getRequest()->getPost(); |
237 | 237 | |
238 | - $days = $this->checkPostVar($postData, 'days', '^[0-9]+$'); |
|
238 | + $days=$this->checkPostVar($postData, 'days', '^[0-9]+$'); |
|
239 | 239 | |
240 | - $action = $this->checkPostVar($postData, 'action', 'save|execute'); |
|
240 | + $action=$this->checkPostVar($postData, 'action', 'save|execute'); |
|
241 | 241 | |
242 | 242 | if ($action == 'save') |
243 | 243 | { |
244 | 244 | try |
245 | 245 | { |
246 | - $this->setDBConfigValue('db_remove_days',$days); |
|
246 | + $this->setDBConfigValue('db_remove_days', $days); |
|
247 | 247 | } |
248 | 248 | catch (Exception $e) |
249 | 249 | { |
250 | - $this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
|
250 | + $this->_helper->json(array('status'=>'Save error : '.$e->getMessage())); |
|
251 | 251 | return; |
252 | 252 | } |
253 | 253 | $this->_helper->json(array('status'=>'OK')); |
@@ -257,16 +257,16 @@ discard block |
||
257 | 257 | { |
258 | 258 | try |
259 | 259 | { |
260 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
260 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
261 | 261 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
262 | 262 | $debug_level=4; |
263 | - $trap = new Trap($icingaweb2_etc); |
|
264 | - $trap->setLogging($debug_level,'syslog'); |
|
263 | + $trap=new Trap($icingaweb2_etc); |
|
264 | + $trap->setLogging($debug_level, 'syslog'); |
|
265 | 265 | $trap->eraseOldTraps($days); |
266 | 266 | } |
267 | 267 | catch (Exception $e) |
268 | 268 | { |
269 | - $this->_helper->json(array('status'=>'execute error : '.$e->getMessage() )); |
|
269 | + $this->_helper->json(array('status'=>'execute error : '.$e->getMessage())); |
|
270 | 270 | return; |
271 | 271 | } |
272 | 272 | $this->_helper->json(array('status'=>'OK')); |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | { |
285 | 285 | $postData=$this->getRequest()->getPost(); |
286 | 286 | |
287 | - $destination = $this->checkPostVar($postData, 'destination', '.*'); |
|
287 | + $destination=$this->checkPostVar($postData, 'destination', '.*'); |
|
288 | 288 | $logDest=$this->getModuleConfig()->getLogDestinations(); |
289 | 289 | if (!isset($logDest[$destination])) |
290 | 290 | { |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | if (isset($postData['file'])) |
296 | 296 | { |
297 | 297 | $file=$postData['file']; |
298 | - $fileHandler=@fopen($file,'w'); |
|
298 | + $fileHandler=@fopen($file, 'w'); |
|
299 | 299 | if ($fileHandler == false) |
300 | 300 | { // File os note writabe / cannot create |
301 | 301 | $this->_helper->json(array('status'=>'File not writable : '.$file)); |
@@ -315,17 +315,17 @@ discard block |
||
315 | 315 | } |
316 | 316 | } |
317 | 317 | |
318 | - $level = $this->checkPostVar($postData, 'level', '[0-9]'); |
|
318 | + $level=$this->checkPostVar($postData, 'level', '[0-9]'); |
|
319 | 319 | |
320 | 320 | try |
321 | 321 | { |
322 | - $this->setDBConfigValue('log_destination',$destination); |
|
323 | - $this->setDBConfigValue('log_file',$file); |
|
324 | - $this->setDBConfigValue('log_level',$level); |
|
322 | + $this->setDBConfigValue('log_destination', $destination); |
|
323 | + $this->setDBConfigValue('log_file', $file); |
|
324 | + $this->setDBConfigValue('log_level', $level); |
|
325 | 325 | } |
326 | 326 | catch (Exception $e) |
327 | 327 | { |
328 | - $this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
|
328 | + $this->_helper->json(array('status'=>'Save error : '.$e->getMessage())); |
|
329 | 329 | return; |
330 | 330 | } |
331 | 331 | $this->_helper->json(array('status'=>'OK')); |
@@ -343,29 +343,29 @@ discard block |
||
343 | 343 | |
344 | 344 | $postData=$this->getRequest()->getPost(); |
345 | 345 | |
346 | - $rule = $this->checkPostVar($postData, 'rule', '.*'); |
|
346 | + $rule=$this->checkPostVar($postData, 'rule', '.*'); |
|
347 | 347 | |
348 | - $action = $this->checkPostVar($postData, 'action', 'evaluate'); |
|
348 | + $action=$this->checkPostVar($postData, 'action', 'evaluate'); |
|
349 | 349 | |
350 | 350 | if ($action == 'evaluate') |
351 | 351 | { |
352 | 352 | try |
353 | 353 | { |
354 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
354 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
355 | 355 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
356 | - $trap = new Trap($icingaweb2_etc); |
|
356 | + $trap=new Trap($icingaweb2_etc); |
|
357 | 357 | // Cleanup spaces before eval |
358 | 358 | $rule=$trap->ruleClass->eval_cleanup($rule); |
359 | 359 | // Eval |
360 | 360 | $item=0; |
361 | - $rule=$trap->ruleClass->evaluation($rule,$item); |
|
361 | + $rule=$trap->ruleClass->evaluation($rule, $item); |
|
362 | 362 | } |
363 | 363 | catch (Exception $e) |
364 | 364 | { |
365 | - $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage() )); |
|
365 | + $this->_helper->json(array('status'=>'Evaluation error : '.$e->getMessage())); |
|
366 | 366 | return; |
367 | 367 | } |
368 | - $return=($rule==true)?'true':'false'; |
|
368 | + $return=($rule == true) ? 'true' : 'false'; |
|
369 | 369 | $this->_helper->json(array('status'=>'OK', 'message' => $return)); |
370 | 370 | } |
371 | 371 | |
@@ -380,15 +380,15 @@ discard block |
||
380 | 380 | { |
381 | 381 | $postData=$this->getRequest()->getPost(); |
382 | 382 | |
383 | - $pluginName = $this->checkPostVar($postData, 'name', '.*'); |
|
383 | + $pluginName=$this->checkPostVar($postData, 'name', '.*'); |
|
384 | 384 | |
385 | - $action = $this->checkPostVar($postData, 'action', 'enable|disable'); |
|
385 | + $action=$this->checkPostVar($postData, 'action', 'enable|disable'); |
|
386 | 386 | |
387 | 387 | try |
388 | 388 | { |
389 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
389 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
390 | 390 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
391 | - $trap = new Trap($icingaweb2_etc); |
|
391 | + $trap=new Trap($icingaweb2_etc); |
|
392 | 392 | // Enable plugin. |
393 | 393 | $action=($action == 'enable') ? true : false; |
394 | 394 | $retVal=$trap->pluginClass->enablePlugin($pluginName, $action); |
@@ -396,7 +396,7 @@ discard block |
||
396 | 396 | } |
397 | 397 | catch (Exception $e) |
398 | 398 | { |
399 | - $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
399 | + $this->_helper->json(array('status'=>'Action error : '.$e->getMessage())); |
|
400 | 400 | return; |
401 | 401 | } |
402 | 402 | if ($retVal === true) |
@@ -418,45 +418,45 @@ discard block |
||
418 | 418 | { |
419 | 419 | $postData=$this->getRequest()->getPost(); |
420 | 420 | |
421 | - $functionString = $this->checkPostVar($postData, 'function', '.*'); |
|
421 | + $functionString=$this->checkPostVar($postData, 'function', '.*'); |
|
422 | 422 | |
423 | 423 | $this->checkPostVar($postData, 'action', 'evaluate'); |
424 | 424 | |
425 | 425 | // Only one action possible for now, no tests on action. |
426 | 426 | try |
427 | 427 | { |
428 | - require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
|
428 | + require_once($this->Module()->getBaseDir().'/bin/trap_class.php'); |
|
429 | 429 | $icingaweb2Etc=$this->Config()->get('config', 'icingaweb2_etc'); |
430 | - $trap = new Trap($icingaweb2Etc); |
|
430 | + $trap=new Trap($icingaweb2Etc); |
|
431 | 431 | // load all plugins in case tested function is not enabled. |
432 | 432 | $trap->pluginClass->registerAllPlugins(false); |
433 | 433 | // Clean all spaces |
434 | - $functionString = $trap->ruleClass->eval_cleanup($functionString); |
|
434 | + $functionString=$trap->ruleClass->eval_cleanup($functionString); |
|
435 | 435 | // Eval functions |
436 | - $result = $trap->pluginClass->evaluateFunctionString($functionString); |
|
436 | + $result=$trap->pluginClass->evaluateFunctionString($functionString); |
|
437 | 437 | } |
438 | 438 | catch (Exception $e) |
439 | 439 | { |
440 | - $this->_helper->json(array('status'=>'Action error : '.$e->getMessage() )); |
|
440 | + $this->_helper->json(array('status'=>'Action error : '.$e->getMessage())); |
|
441 | 441 | return; |
442 | 442 | } |
443 | 443 | |
444 | - $result = ($result === true)?'True':'False'; |
|
445 | - $this->_helper->json(array('status'=>'OK','message' => $result)); |
|
444 | + $result=($result === true) ? 'True' : 'False'; |
|
445 | + $this->_helper->json(array('status'=>'OK', 'message' => $result)); |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | /************** Utilities **********************/ |
449 | 449 | |
450 | - private function checkPostVar(array $postData,string $postVar, string $validRegexp) : string |
|
450 | + private function checkPostVar(array $postData, string $postVar, string $validRegexp) : string |
|
451 | 451 | { |
452 | 452 | if (!isset ($postData[$postVar])) |
453 | 453 | { |
454 | - $this->_helper->json(array('status'=>'No ' . $postVar)); |
|
454 | + $this->_helper->json(array('status'=>'No '.$postVar)); |
|
455 | 455 | return ''; |
456 | 456 | } |
457 | 457 | if (preg_match('/'.$validRegexp.'/', $postData[$postVar]) != 1) |
458 | 458 | { |
459 | - $this->_helper->json(array('status'=>'Unknown ' . $postVar . ' value '.$postData[$postVar])); |
|
459 | + $this->_helper->json(array('status'=>'Unknown '.$postVar.' value '.$postData[$postVar])); |
|
460 | 460 | return ''; |
461 | 461 | } |
462 | 462 | return $postData[$postVar]; |
@@ -21,104 +21,104 @@ |
||
21 | 21 | */ |
22 | 22 | class NetworkRule extends PluginTemplate |
23 | 23 | { |
24 | - /** @var string $description Description of plugin */ |
|
25 | - public $description='Network functions to use into rules |
|
24 | + /** @var string $description Description of plugin */ |
|
25 | + public $description='Network functions to use into rules |
|
26 | 26 | test test test'; |
27 | 27 | |
28 | - /** @var array[] $functions Functions of this plugin for rule eval. |
|
29 | - * If no functions are declared, set to empty array |
|
30 | - * $functions[<name>]['function'] : Name of the function to be called in this class |
|
31 | - * $functions[<name>]['params'] : Description of input parameters of function. |
|
32 | - * $functions[<name>]['description'] : Description. Can be multiline. |
|
33 | - */ |
|
34 | - public $functions=array( |
|
35 | - 'inNetwork' => array( // The name of the function in rules |
|
36 | - 'function' => 'isInNetwork', // Name of the function |
|
37 | - 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
38 | - 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
28 | + /** @var array[] $functions Functions of this plugin for rule eval. |
|
29 | + * If no functions are declared, set to empty array |
|
30 | + * $functions[<name>]['function'] : Name of the function to be called in this class |
|
31 | + * $functions[<name>]['params'] : Description of input parameters of function. |
|
32 | + * $functions[<name>]['description'] : Description. Can be multiline. |
|
33 | + */ |
|
34 | + public $functions=array( |
|
35 | + 'inNetwork' => array( // The name of the function in rules |
|
36 | + 'function' => 'isInNetwork', // Name of the function |
|
37 | + 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
38 | + 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
39 | 39 | Does not work with IPV6' // Description (can be multiline). |
40 | - ), |
|
41 | - 'test' => array( // The name of the function in rules |
|
42 | - 'function' => 'testParam', // Name of the function |
|
43 | - 'params' => '<boolean to return as string>', // parameters description |
|
44 | - 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
45 | - ) |
|
46 | - ); |
|
40 | + ), |
|
41 | + 'test' => array( // The name of the function in rules |
|
42 | + 'function' => 'testParam', // Name of the function |
|
43 | + 'params' => '<boolean to return as string>', // parameters description |
|
44 | + 'description' => 'Returns value passed as argument' // Description (can be multiline). |
|
45 | + ) |
|
46 | + ); |
|
47 | 47 | |
48 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
49 | - public $catchAllTraps=false; |
|
48 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
49 | + public $catchAllTraps=false; |
|
50 | 50 | |
51 | - /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
52 | - public $processTraps=false; |
|
51 | + /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
52 | + public $processTraps=false; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Constructor. Can throw exceptions on error, but no logging at this point. |
|
56 | - * @throws \Exception |
|
57 | - * @return \Trapdirector\Plugins\NetworkRule |
|
58 | - */ |
|
59 | - function __construct() |
|
60 | - { |
|
61 | - $this->name=basename(__FILE__,'.php'); |
|
62 | - return $this; |
|
63 | - } |
|
54 | + /** |
|
55 | + * Constructor. Can throw exceptions on error, but no logging at this point. |
|
56 | + * @throws \Exception |
|
57 | + * @return \Trapdirector\Plugins\NetworkRule |
|
58 | + */ |
|
59 | + function __construct() |
|
60 | + { |
|
61 | + $this->name=basename(__FILE__,'.php'); |
|
62 | + return $this; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Function called by trapdirector if found in rules |
|
67 | - * Parameters check has to be done in function. |
|
68 | - * @param array $params Function parameters |
|
69 | - * @throws Exception |
|
70 | - * @return bool Evaluation |
|
71 | - */ |
|
72 | - public function isInNetwork(array $params) : bool |
|
73 | - { |
|
74 | - // Check param numbers and thrown exception if not correct. |
|
75 | - if (count($params)!=3) |
|
76 | - { |
|
77 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
78 | - } |
|
65 | + /** |
|
66 | + * Function called by trapdirector if found in rules |
|
67 | + * Parameters check has to be done in function. |
|
68 | + * @param array $params Function parameters |
|
69 | + * @throws Exception |
|
70 | + * @return bool Evaluation |
|
71 | + */ |
|
72 | + public function isInNetwork(array $params) : bool |
|
73 | + { |
|
74 | + // Check param numbers and thrown exception if not correct. |
|
75 | + if (count($params)!=3) |
|
76 | + { |
|
77 | + throw new Exception('Invalid number of parameters : ' . count($params)); |
|
78 | + } |
|
79 | 79 | |
80 | - $ip = $params[0]; |
|
81 | - $net = $params[1]; |
|
82 | - $masq = $params[2]; |
|
80 | + $ip = $params[0]; |
|
81 | + $net = $params[1]; |
|
82 | + $masq = $params[2]; |
|
83 | 83 | |
84 | 84 | |
85 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
85 | + $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
86 | 86 | |
87 | - $ip2 = ip2long($ip); |
|
88 | - $net2 = ip2long($net); |
|
87 | + $ip2 = ip2long($ip); |
|
88 | + $net2 = ip2long($net); |
|
89 | 89 | |
90 | - if ($ip2 === false ) |
|
91 | - { |
|
92 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
93 | - throw new Exception('Invalid IP'); |
|
94 | - } |
|
95 | - if ($net2 === false) |
|
96 | - { |
|
97 | - $this->log('Invalid network',WARN); |
|
98 | - throw new Exception('Invalid net'); |
|
99 | - } |
|
100 | - if ($masq<1 || $masq > 32) |
|
101 | - { |
|
102 | - $this->log('Invalid masq',WARN); |
|
103 | - throw new Exception('Invalid net masq'); |
|
104 | - } |
|
105 | - // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
90 | + if ($ip2 === false ) |
|
91 | + { |
|
92 | + $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
93 | + throw new Exception('Invalid IP'); |
|
94 | + } |
|
95 | + if ($net2 === false) |
|
96 | + { |
|
97 | + $this->log('Invalid network',WARN); |
|
98 | + throw new Exception('Invalid net'); |
|
99 | + } |
|
100 | + if ($masq<1 || $masq > 32) |
|
101 | + { |
|
102 | + $this->log('Invalid masq',WARN); |
|
103 | + throw new Exception('Invalid net masq'); |
|
104 | + } |
|
105 | + // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
106 | 106 | |
107 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
108 | - $masq = ~ $masq; |
|
109 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
107 | + $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
108 | + $masq = ~ $masq; |
|
109 | + return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
110 | 110 | |
111 | - } |
|
111 | + } |
|
112 | 112 | |
113 | - public function testParam(array $param) |
|
114 | - { |
|
115 | - if (count($param)!=1) |
|
116 | - { |
|
117 | - throw new Exception('Invalid number of parameters : ' . count($param)); |
|
118 | - } |
|
119 | - if ($param[0] == 'true') return true; |
|
120 | - return false; |
|
121 | - } |
|
113 | + public function testParam(array $param) |
|
114 | + { |
|
115 | + if (count($param)!=1) |
|
116 | + { |
|
117 | + throw new Exception('Invalid number of parameters : ' . count($param)); |
|
118 | + } |
|
119 | + if ($param[0] == 'true') return true; |
|
120 | + return false; |
|
121 | + } |
|
122 | 122 | } |
123 | 123 | |
124 | 124 |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | */ |
59 | 59 | function __construct() |
60 | 60 | { |
61 | - $this->name=basename(__FILE__,'.php'); |
|
61 | + $this->name=basename(__FILE__, '.php'); |
|
62 | 62 | return $this; |
63 | 63 | } |
64 | 64 | |
@@ -72,49 +72,49 @@ discard block |
||
72 | 72 | public function isInNetwork(array $params) : bool |
73 | 73 | { |
74 | 74 | // Check param numbers and thrown exception if not correct. |
75 | - if (count($params)!=3) |
|
75 | + if (count($params) != 3) |
|
76 | 76 | { |
77 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
77 | + throw new Exception('Invalid number of parameters : '.count($params)); |
|
78 | 78 | } |
79 | 79 | |
80 | - $ip = $params[0]; |
|
81 | - $net = $params[1]; |
|
82 | - $masq = $params[2]; |
|
80 | + $ip=$params[0]; |
|
81 | + $net=$params[1]; |
|
82 | + $masq=$params[2]; |
|
83 | 83 | |
84 | 84 | |
85 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
85 | + $this->log('#'.$ip.'# / #'.$net.'# / #'.$masq, DEBUG); |
|
86 | 86 | |
87 | - $ip2 = ip2long($ip); |
|
88 | - $net2 = ip2long($net); |
|
87 | + $ip2=ip2long($ip); |
|
88 | + $net2=ip2long($net); |
|
89 | 89 | |
90 | - if ($ip2 === false ) |
|
90 | + if ($ip2 === false) |
|
91 | 91 | { |
92 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
92 | + $this->log('Invalid IP : #'.$ip.'#', WARN); |
|
93 | 93 | throw new Exception('Invalid IP'); |
94 | 94 | } |
95 | 95 | if ($net2 === false) |
96 | 96 | { |
97 | - $this->log('Invalid network',WARN); |
|
97 | + $this->log('Invalid network', WARN); |
|
98 | 98 | throw new Exception('Invalid net'); |
99 | 99 | } |
100 | - if ($masq<1 || $masq > 32) |
|
100 | + if ($masq < 1 || $masq > 32) |
|
101 | 101 | { |
102 | - $this->log('Invalid masq',WARN); |
|
102 | + $this->log('Invalid masq', WARN); |
|
103 | 103 | throw new Exception('Invalid net masq'); |
104 | 104 | } |
105 | 105 | // $range is in IP/CIDR format eg 127.0.0.1/24 |
106 | 106 | |
107 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
108 | - $masq = ~ $masq; |
|
109 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
107 | + $masq=pow(2, (32 - $masq)) - 1; |
|
108 | + $masq=~ $masq; |
|
109 | + return (($ip2 & $masq) == ($net2 & $masq)); |
|
110 | 110 | |
111 | 111 | } |
112 | 112 | |
113 | 113 | public function testParam(array $param) |
114 | 114 | { |
115 | - if (count($param)!=1) |
|
115 | + if (count($param) != 1) |
|
116 | 116 | { |
117 | - throw new Exception('Invalid number of parameters : ' . count($param)); |
|
117 | + throw new Exception('Invalid number of parameters : '.count($param)); |
|
118 | 118 | } |
119 | 119 | if ($param[0] == 'true') return true; |
120 | 120 | return false; |
@@ -116,7 +116,9 @@ |
||
116 | 116 | { |
117 | 117 | throw new Exception('Invalid number of parameters : ' . count($param)); |
118 | 118 | } |
119 | - if ($param[0] == 'true') return true; |
|
119 | + if ($param[0] == 'true') { |
|
120 | + return true; |
|
121 | + } |
|
120 | 122 | return false; |
121 | 123 | } |
122 | 124 | } |
@@ -7,396 +7,396 @@ |
||
7 | 7 | class Rule |
8 | 8 | { |
9 | 9 | |
10 | - /** @var Logging $logging logging class*/ |
|
11 | - protected $logging; |
|
10 | + /** @var Logging $logging logging class*/ |
|
11 | + protected $logging; |
|
12 | 12 | |
13 | - /** @var Trap $trapClass */ |
|
14 | - protected $trapClass; |
|
13 | + /** @var Trap $trapClass */ |
|
14 | + protected $trapClass; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Setup Rule Class |
|
18 | - * @param Trap $trapClass : To get logging class & plugin class |
|
19 | - */ |
|
20 | - function __construct($trapClass) |
|
21 | - { |
|
22 | - $this->trapClass=$trapClass; |
|
23 | - $this->logging=$trapClass->logging; |
|
24 | - } |
|
16 | + /** |
|
17 | + * Setup Rule Class |
|
18 | + * @param Trap $trapClass : To get logging class & plugin class |
|
19 | + */ |
|
20 | + function __construct($trapClass) |
|
21 | + { |
|
22 | + $this->trapClass=$trapClass; |
|
23 | + $this->logging=$trapClass->logging; |
|
24 | + } |
|
25 | 25 | |
26 | 26 | /** |
27 | 27 | * Get full number |
28 | 28 | * @return array<number,string> |
29 | 29 | */ |
30 | - private function get_number($rule,&$item) |
|
31 | - { |
|
32 | - $item2=$item+1; |
|
33 | - while ( |
|
34 | - ($item2!=strlen($rule)) |
|
35 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
36 | - { |
|
37 | - $item2++ ; |
|
38 | - } |
|
39 | - $val=substr($rule,$item,$item2-$item); |
|
40 | - $item=$item2; |
|
41 | - //echo "number ".$val."\n"; |
|
30 | + private function get_number($rule,&$item) |
|
31 | + { |
|
32 | + $item2=$item+1; |
|
33 | + while ( |
|
34 | + ($item2!=strlen($rule)) |
|
35 | + && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
36 | + { |
|
37 | + $item2++ ; |
|
38 | + } |
|
39 | + $val=substr($rule,$item,$item2-$item); |
|
40 | + $item=$item2; |
|
41 | + //echo "number ".$val."\n"; |
|
42 | 42 | |
43 | - return array(0,$val); |
|
44 | - } |
|
43 | + return array(0,$val); |
|
44 | + } |
|
45 | 45 | |
46 | - private function get_string($rule,&$item) |
|
47 | - { |
|
48 | - $item++; |
|
49 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
50 | - $val=substr($rule,$item,$item2-$item-1); |
|
51 | - $item=$item2; |
|
52 | - //echo "string : ".$val."\n"; |
|
53 | - return array(1,$val); |
|
46 | + private function get_string($rule,&$item) |
|
47 | + { |
|
48 | + $item++; |
|
49 | + $item2=$this->eval_getNext($rule,$item,'"'); |
|
50 | + $val=substr($rule,$item,$item2-$item-1); |
|
51 | + $item=$item2; |
|
52 | + //echo "string : ".$val."\n"; |
|
53 | + return array(1,$val); |
|
54 | 54 | |
55 | - } |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Parse elements inside () : jumps over "" and count parenthesis. |
|
59 | - * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
60 | - * @param string $rule : the current rule |
|
61 | - * @param int $item : actual position in rule |
|
62 | - * @throws Exception |
|
63 | - * @return string : everything inside parenthesis |
|
64 | - */ |
|
65 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
66 | - { |
|
67 | - $item++; |
|
68 | - $start=$item; |
|
69 | - $parenthesis_count=0; |
|
70 | - while (($item < strlen($rule)) // Not end of string AND |
|
71 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
72 | - { |
|
73 | - if ($rule[$item] == '"' ) |
|
74 | - { // pass through string |
|
75 | - $item++; |
|
76 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
77 | - } |
|
78 | - else{ |
|
79 | - if ($rule[$item] == '(') |
|
80 | - { |
|
81 | - $parenthesis_count++; |
|
82 | - } |
|
83 | - if ($rule[$item] == ')') |
|
84 | - { |
|
85 | - $parenthesis_count--; |
|
86 | - } |
|
87 | - $item++; |
|
88 | - } |
|
89 | - } |
|
57 | + /** |
|
58 | + * Parse elements inside () : jumps over "" and count parenthesis. |
|
59 | + * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
60 | + * @param string $rule : the current rule |
|
61 | + * @param int $item : actual position in rule |
|
62 | + * @throws Exception |
|
63 | + * @return string : everything inside parenthesis |
|
64 | + */ |
|
65 | + private function parse_parenthesis(string $rule,int &$item) : string |
|
66 | + { |
|
67 | + $item++; |
|
68 | + $start=$item; |
|
69 | + $parenthesis_count=0; |
|
70 | + while (($item < strlen($rule)) // Not end of string AND |
|
71 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
72 | + { |
|
73 | + if ($rule[$item] == '"' ) |
|
74 | + { // pass through string |
|
75 | + $item++; |
|
76 | + $item=$this->eval_getNext($rule,$item,'"'); |
|
77 | + } |
|
78 | + else{ |
|
79 | + if ($rule[$item] == '(') |
|
80 | + { |
|
81 | + $parenthesis_count++; |
|
82 | + } |
|
83 | + if ($rule[$item] == ')') |
|
84 | + { |
|
85 | + $parenthesis_count--; |
|
86 | + } |
|
87 | + $item++; |
|
88 | + } |
|
89 | + } |
|
90 | 90 | |
91 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
92 | - $val=substr($rule,$start,$item-$start); |
|
93 | - $item++; |
|
94 | - return $val; |
|
95 | - } |
|
91 | + if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
92 | + $val=substr($rule,$start,$item-$start); |
|
93 | + $item++; |
|
94 | + return $val; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | |
98 | - /** |
|
99 | - * Get and eval a grouped condition - ex : (1==1) |
|
100 | - * @param string $rule |
|
101 | - * @param int $item |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - private function get_group(string $rule,int &$item) : array |
|
105 | - { |
|
106 | - // gets eveything inside parenthesis |
|
107 | - $val=$this->parse_parenthesis($rule, $item); |
|
108 | - // Returns boolean with evaluation of all inside parenthesis |
|
109 | - $start=0; |
|
110 | - return array(2,$this->evaluation($val,$start)); |
|
111 | - } |
|
98 | + /** |
|
99 | + * Get and eval a grouped condition - ex : (1==1) |
|
100 | + * @param string $rule |
|
101 | + * @param int $item |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + private function get_group(string $rule,int &$item) : array |
|
105 | + { |
|
106 | + // gets eveything inside parenthesis |
|
107 | + $val=$this->parse_parenthesis($rule, $item); |
|
108 | + // Returns boolean with evaluation of all inside parenthesis |
|
109 | + $start=0; |
|
110 | + return array(2,$this->evaluation($val,$start)); |
|
111 | + } |
|
112 | 112 | |
113 | - private function get_function(string $rule,int &$item) : array |
|
114 | - { |
|
115 | - // function is : __function(param1,param2...) |
|
116 | - $start=$item; |
|
117 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
118 | - { |
|
119 | - $item++; |
|
120 | - } |
|
121 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
113 | + private function get_function(string $rule,int &$item) : array |
|
114 | + { |
|
115 | + // function is : __function(param1,param2...) |
|
116 | + $start=$item; |
|
117 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
118 | + { |
|
119 | + $item++; |
|
120 | + } |
|
121 | + if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
122 | 122 | |
123 | - // get parameters between parenthesis |
|
123 | + // get parameters between parenthesis |
|
124 | 124 | |
125 | - $this->parse_parenthesis($rule, $item); |
|
125 | + $this->parse_parenthesis($rule, $item); |
|
126 | 126 | |
127 | - $val=substr($rule,$start,$item-$start); |
|
127 | + $val=substr($rule,$start,$item-$start); |
|
128 | 128 | |
129 | - $this->logging->log('got function ' . $val,DEBUG); |
|
129 | + $this->logging->log('got function ' . $val,DEBUG); |
|
130 | 130 | |
131 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
131 | + return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
132 | 132 | |
133 | - } |
|
133 | + } |
|
134 | 134 | |
135 | - protected function eval_getElement($rule,&$item) |
|
136 | - { |
|
137 | - if ($item >= strlen($rule)) |
|
138 | - { |
|
139 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
140 | - } |
|
141 | - while ($rule[$item]==' ') $item++; |
|
142 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
143 | - { // number |
|
144 | - return $this->get_number($rule, $item); |
|
145 | - } |
|
146 | - if ($rule[$item] == '"') |
|
147 | - { // string |
|
148 | - return $this->get_string($rule, $item); |
|
149 | - } |
|
135 | + protected function eval_getElement($rule,&$item) |
|
136 | + { |
|
137 | + if ($item >= strlen($rule)) |
|
138 | + { |
|
139 | + throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
140 | + } |
|
141 | + while ($rule[$item]==' ') $item++; |
|
142 | + if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
143 | + { // number |
|
144 | + return $this->get_number($rule, $item); |
|
145 | + } |
|
146 | + if ($rule[$item] == '"') |
|
147 | + { // string |
|
148 | + return $this->get_string($rule, $item); |
|
149 | + } |
|
150 | 150 | |
151 | - if ($rule[$item] == '(') |
|
152 | - { // grouping |
|
153 | - return $this->get_group($rule, $item); |
|
154 | - } |
|
155 | - if ($rule[$item] == '_') |
|
156 | - { // function |
|
157 | - return $this->get_function($rule, $item); |
|
158 | - } |
|
159 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
151 | + if ($rule[$item] == '(') |
|
152 | + { // grouping |
|
153 | + return $this->get_group($rule, $item); |
|
154 | + } |
|
155 | + if ($rule[$item] == '_') |
|
156 | + { // function |
|
157 | + return $this->get_function($rule, $item); |
|
158 | + } |
|
159 | + throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
160 | 160 | |
161 | - } |
|
161 | + } |
|
162 | 162 | |
163 | - protected function eval_getNext($rule,$item,$tok) |
|
164 | - { |
|
165 | - while ( |
|
166 | - ($rule[$item] != $tok ) |
|
167 | - && ($item < strlen($rule))) |
|
168 | - { |
|
169 | - $item++; |
|
170 | - } |
|
171 | - if ($item==strlen($rule)) { |
|
172 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
173 | - } |
|
174 | - return $item+1; |
|
175 | - } |
|
163 | + protected function eval_getNext($rule,$item,$tok) |
|
164 | + { |
|
165 | + while ( |
|
166 | + ($rule[$item] != $tok ) |
|
167 | + && ($item < strlen($rule))) |
|
168 | + { |
|
169 | + $item++; |
|
170 | + } |
|
171 | + if ($item==strlen($rule)) { |
|
172 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
173 | + } |
|
174 | + return $item+1; |
|
175 | + } |
|
176 | 176 | |
177 | - protected function eval_getOper($rule,&$item) |
|
178 | - { |
|
179 | - while ($rule[$item]==' ') $item++; |
|
180 | - switch ($rule[$item]) |
|
181 | - { |
|
182 | - case '<': |
|
183 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
184 | - $item++; return array(0,"<"); |
|
185 | - case '>': |
|
186 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
187 | - $item++; return array(0,">"); |
|
188 | - case '=': |
|
189 | - $item++; return array(0,"="); |
|
190 | - case '!': |
|
191 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
192 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
193 | - case '~': |
|
194 | - $item++; return array(0,"~"); |
|
195 | - case '|': |
|
196 | - $item++; return array(1,"|"); |
|
197 | - case '&': |
|
198 | - $item++; return array(1,"&"); |
|
199 | - default : |
|
200 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
201 | - } |
|
202 | - } |
|
177 | + protected function eval_getOper($rule,&$item) |
|
178 | + { |
|
179 | + while ($rule[$item]==' ') $item++; |
|
180 | + switch ($rule[$item]) |
|
181 | + { |
|
182 | + case '<': |
|
183 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
184 | + $item++; return array(0,"<"); |
|
185 | + case '>': |
|
186 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
187 | + $item++; return array(0,">"); |
|
188 | + case '=': |
|
189 | + $item++; return array(0,"="); |
|
190 | + case '!': |
|
191 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
192 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
193 | + case '~': |
|
194 | + $item++; return array(0,"~"); |
|
195 | + case '|': |
|
196 | + $item++; return array(1,"|"); |
|
197 | + case '&': |
|
198 | + $item++; return array(1,"&"); |
|
199 | + default : |
|
200 | + throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
201 | + } |
|
202 | + } |
|
203 | 203 | |
204 | - private function check_negate_first($rule,&$item) |
|
205 | - { |
|
206 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
207 | - { |
|
208 | - $item++; |
|
209 | - return true; |
|
210 | - } |
|
211 | - else |
|
212 | - { |
|
213 | - return false; |
|
214 | - } |
|
215 | - } |
|
204 | + private function check_negate_first($rule,&$item) |
|
205 | + { |
|
206 | + if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
207 | + { |
|
208 | + $item++; |
|
209 | + return true; |
|
210 | + } |
|
211 | + else |
|
212 | + { |
|
213 | + return false; |
|
214 | + } |
|
215 | + } |
|
216 | 216 | |
217 | - private function do_compare($val1,$val2,$comp,$negate) |
|
218 | - { |
|
219 | - switch ($comp){ |
|
220 | - case '<': $retVal= ($val1 < $val2); break; |
|
221 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
222 | - case '>': $retVal= ($val1 > $val2); break; |
|
223 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
224 | - case '=': $retVal= ($val1 == $val2); break; |
|
225 | - case '!=': $retVal= ($val1 != $val2); break; |
|
226 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
227 | - case '|': $retVal= ($val1 || $val2); break; |
|
228 | - case '&': $retVal= ($val1 && $val2); break; |
|
229 | - default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
230 | - } |
|
231 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
217 | + private function do_compare($val1,$val2,$comp,$negate) |
|
218 | + { |
|
219 | + switch ($comp){ |
|
220 | + case '<': $retVal= ($val1 < $val2); break; |
|
221 | + case '<=': $retVal= ($val1 <= $val2); break; |
|
222 | + case '>': $retVal= ($val1 > $val2); break; |
|
223 | + case '>=': $retVal= ($val1 >= $val2); break; |
|
224 | + case '=': $retVal= ($val1 == $val2); break; |
|
225 | + case '!=': $retVal= ($val1 != $val2); break; |
|
226 | + case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
227 | + case '|': $retVal= ($val1 || $val2); break; |
|
228 | + case '&': $retVal= ($val1 && $val2); break; |
|
229 | + default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
230 | + } |
|
231 | + if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
232 | 232 | |
233 | - return $retVal; |
|
234 | - } |
|
233 | + return $retVal; |
|
234 | + } |
|
235 | 235 | |
236 | - /** Evaluation : makes token and evaluate. |
|
237 | - * Public function for expressions testing |
|
238 | - * accepts : < > = <= >= != (typec = 0) |
|
239 | - * operators : & | (typec=1) |
|
240 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
241 | - * comparison int vs strings will return null (error) |
|
242 | - * return : bool or null on error |
|
243 | - */ |
|
244 | - public function evaluation($rule,&$item) |
|
245 | - { |
|
246 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
247 | - $negate=$this->check_negate_first($rule, $item); |
|
248 | - // First element : number, string or () |
|
249 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
250 | - //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
236 | + /** Evaluation : makes token and evaluate. |
|
237 | + * Public function for expressions testing |
|
238 | + * accepts : < > = <= >= != (typec = 0) |
|
239 | + * operators : & | (typec=1) |
|
240 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
241 | + * comparison int vs strings will return null (error) |
|
242 | + * return : bool or null on error |
|
243 | + */ |
|
244 | + public function evaluation($rule,&$item) |
|
245 | + { |
|
246 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
247 | + $negate=$this->check_negate_first($rule, $item); |
|
248 | + // First element : number, string or () |
|
249 | + list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
250 | + //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
251 | 251 | |
252 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
253 | - { |
|
254 | - if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
255 | - if ($negate === true) $val1= ! $val1; |
|
256 | - return $val1; |
|
257 | - } |
|
252 | + if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
253 | + { |
|
254 | + if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
255 | + if ($negate === true) $val1= ! $val1; |
|
256 | + return $val1; |
|
257 | + } |
|
258 | 258 | |
259 | - // Second element : operator |
|
260 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
261 | - //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
259 | + // Second element : operator |
|
260 | + list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
261 | + //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
262 | 262 | |
263 | - // Third element : number, string or () |
|
264 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
265 | - { |
|
266 | - $item++; |
|
267 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
268 | - $val2= ! $this->evaluation($rule,$item); |
|
269 | - $type2=2; // result is a boolean |
|
270 | - } |
|
271 | - else |
|
272 | - { |
|
273 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
274 | - } |
|
275 | - //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
263 | + // Third element : number, string or () |
|
264 | + if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
265 | + { |
|
266 | + $item++; |
|
267 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
268 | + $val2= ! $this->evaluation($rule,$item); |
|
269 | + $type2=2; // result is a boolean |
|
270 | + } |
|
271 | + else |
|
272 | + { |
|
273 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
274 | + } |
|
275 | + //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
276 | 276 | |
277 | - if ($type1!=$type2) // cannot compare different types |
|
278 | - { |
|
279 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
280 | - } |
|
281 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
282 | - { |
|
283 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
284 | - } |
|
277 | + if ($type1!=$type2) // cannot compare different types |
|
278 | + { |
|
279 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
280 | + } |
|
281 | + if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
282 | + { |
|
283 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
284 | + } |
|
285 | 285 | |
286 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
286 | + $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
287 | 287 | |
288 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
289 | - // check for logical operator : |
|
290 | - switch ($rule[$item]) |
|
291 | - { |
|
292 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
293 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
288 | + if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
289 | + // check for logical operator : |
|
290 | + switch ($rule[$item]) |
|
291 | + { |
|
292 | + case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
293 | + case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
294 | 294 | |
295 | - default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
296 | - } |
|
297 | - } |
|
295 | + default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
296 | + } |
|
297 | + } |
|
298 | 298 | |
299 | - // Remove all whitespaces (when not quoted) |
|
300 | - public function eval_cleanup($rule) |
|
301 | - { |
|
302 | - $item=0; |
|
303 | - $rule2=''; |
|
304 | - while ($item < strlen($rule)) |
|
305 | - { |
|
306 | - if ($rule[$item]==' ') { $item++; continue; } |
|
307 | - if ($rule[$item]=='"') |
|
308 | - { |
|
309 | - $rule2.=$rule[$item]; |
|
310 | - $item++; |
|
311 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
312 | - { |
|
313 | - $rule2.=$rule[$item]; |
|
314 | - $item++; |
|
315 | - } |
|
316 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
317 | - $rule2.=$rule[$item]; |
|
318 | - $item++; |
|
319 | - continue; |
|
320 | - } |
|
299 | + // Remove all whitespaces (when not quoted) |
|
300 | + public function eval_cleanup($rule) |
|
301 | + { |
|
302 | + $item=0; |
|
303 | + $rule2=''; |
|
304 | + while ($item < strlen($rule)) |
|
305 | + { |
|
306 | + if ($rule[$item]==' ') { $item++; continue; } |
|
307 | + if ($rule[$item]=='"') |
|
308 | + { |
|
309 | + $rule2.=$rule[$item]; |
|
310 | + $item++; |
|
311 | + while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
312 | + { |
|
313 | + $rule2.=$rule[$item]; |
|
314 | + $item++; |
|
315 | + } |
|
316 | + if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
317 | + $rule2.=$rule[$item]; |
|
318 | + $item++; |
|
319 | + continue; |
|
320 | + } |
|
321 | 321 | |
322 | - $rule2.=$rule[$item]; |
|
323 | - $item++; |
|
324 | - } |
|
322 | + $rule2.=$rule[$item]; |
|
323 | + $item++; |
|
324 | + } |
|
325 | 325 | |
326 | - return $rule2; |
|
327 | - } |
|
326 | + return $rule2; |
|
327 | + } |
|
328 | 328 | |
329 | 329 | |
330 | - /** |
|
331 | - * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
332 | - * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
333 | - * @return string correct regexp format |
|
334 | - */ |
|
335 | - public function regexp_eval(string &$oid) |
|
336 | - { |
|
337 | - // ** replaced by .* |
|
338 | - $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
339 | - // * replaced by [0-9]+ |
|
340 | - $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
330 | + /** |
|
331 | + * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
332 | + * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
333 | + * @return string correct regexp format |
|
334 | + */ |
|
335 | + public function regexp_eval(string &$oid) |
|
336 | + { |
|
337 | + // ** replaced by .* |
|
338 | + $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
339 | + // * replaced by [0-9]+ |
|
340 | + $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
341 | 341 | |
342 | - // replace * with \* in oid for preg_replace |
|
343 | - $oid=preg_replace('/\*/', '\*', $oid); |
|
342 | + // replace * with \* in oid for preg_replace |
|
343 | + $oid=preg_replace('/\*/', '\*', $oid); |
|
344 | 344 | |
345 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
345 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
346 | 346 | |
347 | - return $oidR; |
|
348 | - } |
|
347 | + return $oidR; |
|
348 | + } |
|
349 | 349 | |
350 | 350 | |
351 | - /** Evaluation rule (uses eval_* functions recursively) |
|
352 | - * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
353 | - * @param array $oidList : OIDs values to sustitute. |
|
354 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
355 | - */ |
|
356 | - public function eval_rule($rule,$oidList) |
|
357 | - { |
|
358 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
359 | - { |
|
360 | - return true; |
|
361 | - } |
|
362 | - $matches=array(); |
|
363 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
364 | - { |
|
365 | - $oid=$matches[1]; |
|
366 | - $found=0; |
|
367 | - // Test and transform regexp |
|
368 | - $oidR = $this->regexp_eval($oid); |
|
351 | + /** Evaluation rule (uses eval_* functions recursively) |
|
352 | + * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
353 | + * @param array $oidList : OIDs values to sustitute. |
|
354 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
355 | + */ |
|
356 | + public function eval_rule($rule,$oidList) |
|
357 | + { |
|
358 | + if ($rule==null || $rule == '') // Empty rule is always true |
|
359 | + { |
|
360 | + return true; |
|
361 | + } |
|
362 | + $matches=array(); |
|
363 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
364 | + { |
|
365 | + $oid=$matches[1]; |
|
366 | + $found=0; |
|
367 | + // Test and transform regexp |
|
368 | + $oidR = $this->regexp_eval($oid); |
|
369 | 369 | |
370 | - foreach($oidList as $val) |
|
371 | - { |
|
372 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
373 | - { |
|
374 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
375 | - { // If not a number, change " to ' and put " around it |
|
376 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
377 | - $val->value='"'.$val->value.'"'; |
|
378 | - } |
|
379 | - $rep=0; |
|
380 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
381 | - if ($rep==0) |
|
382 | - { |
|
383 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
384 | - return false; |
|
385 | - } |
|
386 | - $found=1; |
|
387 | - break; |
|
388 | - } |
|
389 | - } |
|
390 | - if ($found==0) |
|
391 | - { // OID not found : throw error |
|
392 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
393 | - } |
|
394 | - } |
|
395 | - $item=0; |
|
396 | - $rule=$this->eval_cleanup($rule); |
|
397 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
370 | + foreach($oidList as $val) |
|
371 | + { |
|
372 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
373 | + { |
|
374 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
375 | + { // If not a number, change " to ' and put " around it |
|
376 | + $val->value=preg_replace('/"/',"'",$val->value); |
|
377 | + $val->value='"'.$val->value.'"'; |
|
378 | + } |
|
379 | + $rep=0; |
|
380 | + $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
381 | + if ($rep==0) |
|
382 | + { |
|
383 | + $this->logging->log("Error in rule_eval",WARN,''); |
|
384 | + return false; |
|
385 | + } |
|
386 | + $found=1; |
|
387 | + break; |
|
388 | + } |
|
389 | + } |
|
390 | + if ($found==0) |
|
391 | + { // OID not found : throw error |
|
392 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
393 | + } |
|
394 | + } |
|
395 | + $item=0; |
|
396 | + $rule=$this->eval_cleanup($rule); |
|
397 | + $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
398 | 398 | |
399 | - return $this->evaluation($rule,$item); |
|
400 | - } |
|
399 | + return $this->evaluation($rule,$item); |
|
400 | + } |
|
401 | 401 | |
402 | 402 | } |
403 | 403 | \ No newline at end of file |
@@ -27,30 +27,30 @@ discard block |
||
27 | 27 | * Get full number |
28 | 28 | * @return array<number,string> |
29 | 29 | */ |
30 | - private function get_number($rule,&$item) |
|
30 | + private function get_number($rule, &$item) |
|
31 | 31 | { |
32 | - $item2=$item+1; |
|
32 | + $item2=$item + 1; |
|
33 | 33 | while ( |
34 | - ($item2!=strlen($rule)) |
|
35 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
34 | + ($item2 != strlen($rule)) |
|
35 | + && (preg_match('/[\-0-9\.]/', $rule[$item2]))) |
|
36 | 36 | { |
37 | - $item2++ ; |
|
37 | + $item2++; |
|
38 | 38 | } |
39 | - $val=substr($rule,$item,$item2-$item); |
|
39 | + $val=substr($rule, $item, $item2 - $item); |
|
40 | 40 | $item=$item2; |
41 | 41 | //echo "number ".$val."\n"; |
42 | 42 | |
43 | - return array(0,$val); |
|
43 | + return array(0, $val); |
|
44 | 44 | } |
45 | 45 | |
46 | - private function get_string($rule,&$item) |
|
46 | + private function get_string($rule, &$item) |
|
47 | 47 | { |
48 | 48 | $item++; |
49 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
50 | - $val=substr($rule,$item,$item2-$item-1); |
|
49 | + $item2=$this->eval_getNext($rule, $item, '"'); |
|
50 | + $val=substr($rule, $item, $item2 - $item - 1); |
|
51 | 51 | $item=$item2; |
52 | 52 | //echo "string : ".$val."\n"; |
53 | - return array(1,$val); |
|
53 | + return array(1, $val); |
|
54 | 54 | |
55 | 55 | } |
56 | 56 | |
@@ -62,20 +62,20 @@ discard block |
||
62 | 62 | * @throws Exception |
63 | 63 | * @return string : everything inside parenthesis |
64 | 64 | */ |
65 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
65 | + private function parse_parenthesis(string $rule, int &$item) : string |
|
66 | 66 | { |
67 | 67 | $item++; |
68 | 68 | $start=$item; |
69 | 69 | $parenthesis_count=0; |
70 | 70 | while (($item < strlen($rule)) // Not end of string AND |
71 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
71 | + && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded () |
|
72 | 72 | { |
73 | - if ($rule[$item] == '"' ) |
|
73 | + if ($rule[$item] == '"') |
|
74 | 74 | { // pass through string |
75 | 75 | $item++; |
76 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
76 | + $item=$this->eval_getNext($rule, $item, '"'); |
|
77 | 77 | } |
78 | - else{ |
|
78 | + else { |
|
79 | 79 | if ($rule[$item] == '(') |
80 | 80 | { |
81 | 81 | $parenthesis_count++; |
@@ -88,8 +88,8 @@ discard block |
||
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
91 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
92 | - $val=substr($rule,$start,$item-$start); |
|
91 | + if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); } |
|
92 | + $val=substr($rule, $start, $item - $start); |
|
93 | 93 | $item++; |
94 | 94 | return $val; |
95 | 95 | } |
@@ -101,45 +101,45 @@ discard block |
||
101 | 101 | * @param int $item |
102 | 102 | * @return array |
103 | 103 | */ |
104 | - private function get_group(string $rule,int &$item) : array |
|
104 | + private function get_group(string $rule, int &$item) : array |
|
105 | 105 | { |
106 | 106 | // gets eveything inside parenthesis |
107 | 107 | $val=$this->parse_parenthesis($rule, $item); |
108 | 108 | // Returns boolean with evaluation of all inside parenthesis |
109 | 109 | $start=0; |
110 | - return array(2,$this->evaluation($val,$start)); |
|
110 | + return array(2, $this->evaluation($val, $start)); |
|
111 | 111 | } |
112 | 112 | |
113 | - private function get_function(string $rule,int &$item) : array |
|
113 | + private function get_function(string $rule, int &$item) : array |
|
114 | 114 | { |
115 | 115 | // function is : __function(param1,param2...) |
116 | 116 | $start=$item; |
117 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
117 | + while (($item < strlen($rule)) && ($rule[$item] != '(')) // Not end of string AND not opening '(' |
|
118 | 118 | { |
119 | 119 | $item++; |
120 | 120 | } |
121 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
121 | + if ($item == strlen($rule)) {throw new Exception("no opening () for function in ".$rule." at ".$item); } |
|
122 | 122 | |
123 | 123 | // get parameters between parenthesis |
124 | 124 | |
125 | 125 | $this->parse_parenthesis($rule, $item); |
126 | 126 | |
127 | - $val=substr($rule,$start,$item-$start); |
|
127 | + $val=substr($rule, $start, $item - $start); |
|
128 | 128 | |
129 | - $this->logging->log('got function ' . $val,DEBUG); |
|
129 | + $this->logging->log('got function '.$val, DEBUG); |
|
130 | 130 | |
131 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
131 | + return array(2, $this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
132 | 132 | |
133 | 133 | } |
134 | 134 | |
135 | - protected function eval_getElement($rule,&$item) |
|
135 | + protected function eval_getElement($rule, &$item) |
|
136 | 136 | { |
137 | 137 | if ($item >= strlen($rule)) |
138 | 138 | { |
139 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
139 | + throw new Exception("Early end of string ".$rule." at ".$item); |
|
140 | 140 | } |
141 | - while ($rule[$item]==' ') $item++; |
|
142 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
141 | + while ($rule[$item] == ' ') $item++; |
|
142 | + if (preg_match('/[\-0-9\.]/', $rule[$item])) |
|
143 | 143 | { // number |
144 | 144 | return $this->get_number($rule, $item); |
145 | 145 | } |
@@ -156,54 +156,54 @@ discard block |
||
156 | 156 | { // function |
157 | 157 | return $this->get_function($rule, $item); |
158 | 158 | } |
159 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
159 | + throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]); |
|
160 | 160 | |
161 | 161 | } |
162 | 162 | |
163 | - protected function eval_getNext($rule,$item,$tok) |
|
163 | + protected function eval_getNext($rule, $item, $tok) |
|
164 | 164 | { |
165 | 165 | while ( |
166 | - ($rule[$item] != $tok ) |
|
166 | + ($rule[$item] != $tok) |
|
167 | 167 | && ($item < strlen($rule))) |
168 | 168 | { |
169 | 169 | $item++; |
170 | 170 | } |
171 | - if ($item==strlen($rule)) { |
|
172 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
171 | + if ($item == strlen($rule)) { |
|
172 | + throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item); |
|
173 | 173 | } |
174 | - return $item+1; |
|
174 | + return $item + 1; |
|
175 | 175 | } |
176 | 176 | |
177 | - protected function eval_getOper($rule,&$item) |
|
177 | + protected function eval_getOper($rule, &$item) |
|
178 | 178 | { |
179 | - while ($rule[$item]==' ') $item++; |
|
179 | + while ($rule[$item] == ' ') $item++; |
|
180 | 180 | switch ($rule[$item]) |
181 | 181 | { |
182 | 182 | case '<': |
183 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
184 | - $item++; return array(0,"<"); |
|
183 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); } |
|
184 | + $item++; return array(0, "<"); |
|
185 | 185 | case '>': |
186 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
187 | - $item++; return array(0,">"); |
|
186 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); } |
|
187 | + $item++; return array(0, ">"); |
|
188 | 188 | case '=': |
189 | - $item++; return array(0,"="); |
|
189 | + $item++; return array(0, "="); |
|
190 | 190 | case '!': |
191 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
192 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
191 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); } |
|
192 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule." at ".$item); |
|
193 | 193 | case '~': |
194 | - $item++; return array(0,"~"); |
|
194 | + $item++; return array(0, "~"); |
|
195 | 195 | case '|': |
196 | - $item++; return array(1,"|"); |
|
196 | + $item++; return array(1, "|"); |
|
197 | 197 | case '&': |
198 | - $item++; return array(1,"&"); |
|
198 | + $item++; return array(1, "&"); |
|
199 | 199 | default : |
200 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
200 | + throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item); |
|
201 | 201 | } |
202 | 202 | } |
203 | 203 | |
204 | - private function check_negate_first($rule,&$item) |
|
204 | + private function check_negate_first($rule, &$item) |
|
205 | 205 | { |
206 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
206 | + if ($rule[$item] == '!') // If '!' found, negate next expression. |
|
207 | 207 | { |
208 | 208 | $item++; |
209 | 209 | return true; |
@@ -214,21 +214,21 @@ discard block |
||
214 | 214 | } |
215 | 215 | } |
216 | 216 | |
217 | - private function do_compare($val1,$val2,$comp,$negate) |
|
217 | + private function do_compare($val1, $val2, $comp, $negate) |
|
218 | 218 | { |
219 | - switch ($comp){ |
|
220 | - case '<': $retVal= ($val1 < $val2); break; |
|
221 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
222 | - case '>': $retVal= ($val1 > $val2); break; |
|
223 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
224 | - case '=': $retVal= ($val1 == $val2); break; |
|
225 | - case '!=': $retVal= ($val1 != $val2); break; |
|
226 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
227 | - case '|': $retVal= ($val1 || $val2); break; |
|
228 | - case '&': $retVal= ($val1 && $val2); break; |
|
219 | + switch ($comp) { |
|
220 | + case '<': $retVal=($val1 < $val2); break; |
|
221 | + case '<=': $retVal=($val1 <= $val2); break; |
|
222 | + case '>': $retVal=($val1 > $val2); break; |
|
223 | + case '>=': $retVal=($val1 >= $val2); break; |
|
224 | + case '=': $retVal=($val1 == $val2); break; |
|
225 | + case '!=': $retVal=($val1 != $val2); break; |
|
226 | + case '~': $retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break; |
|
227 | + case '|': $retVal=($val1 || $val2); break; |
|
228 | + case '&': $retVal=($val1 && $val2); break; |
|
229 | 229 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
230 | 230 | } |
231 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
231 | + if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression |
|
232 | 232 | |
233 | 233 | return $retVal; |
234 | 234 | } |
@@ -241,56 +241,56 @@ discard block |
||
241 | 241 | * comparison int vs strings will return null (error) |
242 | 242 | * return : bool or null on error |
243 | 243 | */ |
244 | - public function evaluation($rule,&$item) |
|
244 | + public function evaluation($rule, &$item) |
|
245 | 245 | { |
246 | 246 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
247 | 247 | $negate=$this->check_negate_first($rule, $item); |
248 | 248 | // First element : number, string or () |
249 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
249 | + list($type1, $val1)=$this->eval_getElement($rule, $item); |
|
250 | 250 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
251 | 251 | |
252 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
252 | + if ($item == strlen($rule)) // If only element, return value, but only boolean |
|
253 | 253 | { |
254 | 254 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
255 | - if ($negate === true) $val1= ! $val1; |
|
255 | + if ($negate === true) $val1=!$val1; |
|
256 | 256 | return $val1; |
257 | 257 | } |
258 | 258 | |
259 | 259 | // Second element : operator |
260 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
260 | + list($typec, $comp)=$this->eval_getOper($rule, $item); |
|
261 | 261 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
262 | 262 | |
263 | 263 | // Third element : number, string or () |
264 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
264 | + if ($rule[$item] == '!') // starts with a ! so evaluate whats next |
|
265 | 265 | { |
266 | 266 | $item++; |
267 | 267 | if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
268 | - $val2= ! $this->evaluation($rule,$item); |
|
268 | + $val2=!$this->evaluation($rule, $item); |
|
269 | 269 | $type2=2; // result is a boolean |
270 | 270 | } |
271 | 271 | else |
272 | 272 | { |
273 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
273 | + list($type2, $val2)=$this->eval_getElement($rule, $item); |
|
274 | 274 | } |
275 | 275 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
276 | 276 | |
277 | - if ($type1!=$type2) // cannot compare different types |
|
277 | + if ($type1 != $type2) // cannot compare different types |
|
278 | 278 | { |
279 | 279 | throw new Exception("Cannot compare string & number : ".$rule); |
280 | 280 | } |
281 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
281 | + if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number |
|
282 | 282 | { |
283 | 283 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
284 | 284 | } |
285 | 285 | |
286 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
286 | + $retVal=$this->do_compare($val1, $val2, $comp, $negate); |
|
287 | 287 | |
288 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
288 | + if ($item == strlen($rule)) return $retVal; // End of string : return evaluation |
|
289 | 289 | // check for logical operator : |
290 | 290 | switch ($rule[$item]) |
291 | 291 | { |
292 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
293 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
292 | + case '|': $item++; return ($retVal || $this->evaluation($rule, $item)); |
|
293 | + case '&': $item++; return ($retVal && $this->evaluation($rule, $item)); |
|
294 | 294 | |
295 | 295 | default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
296 | 296 | } |
@@ -303,17 +303,17 @@ discard block |
||
303 | 303 | $rule2=''; |
304 | 304 | while ($item < strlen($rule)) |
305 | 305 | { |
306 | - if ($rule[$item]==' ') { $item++; continue; } |
|
307 | - if ($rule[$item]=='"') |
|
306 | + if ($rule[$item] == ' ') { $item++; continue; } |
|
307 | + if ($rule[$item] == '"') |
|
308 | 308 | { |
309 | 309 | $rule2.=$rule[$item]; |
310 | 310 | $item++; |
311 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
311 | + while (($item < strlen($rule)) && ($rule[$item] != '"')) |
|
312 | 312 | { |
313 | 313 | $rule2.=$rule[$item]; |
314 | 314 | $item++; |
315 | 315 | } |
316 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
316 | + if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item); |
|
317 | 317 | $rule2.=$rule[$item]; |
318 | 318 | $item++; |
319 | 319 | continue; |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | // replace * with \* in oid for preg_replace |
343 | 343 | $oid=preg_replace('/\*/', '\*', $oid); |
344 | 344 | |
345 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
345 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR, DEBUG); |
|
346 | 346 | |
347 | 347 | return $oidR; |
348 | 348 | } |
@@ -353,50 +353,50 @@ discard block |
||
353 | 353 | * @param array $oidList : OIDs values to sustitute. |
354 | 354 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
355 | 355 | */ |
356 | - public function eval_rule($rule,$oidList) |
|
356 | + public function eval_rule($rule, $oidList) |
|
357 | 357 | { |
358 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
358 | + if ($rule == null || $rule == '') // Empty rule is always true |
|
359 | 359 | { |
360 | 360 | return true; |
361 | 361 | } |
362 | 362 | $matches=array(); |
363 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
363 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1) |
|
364 | 364 | { |
365 | 365 | $oid=$matches[1]; |
366 | 366 | $found=0; |
367 | 367 | // Test and transform regexp |
368 | - $oidR = $this->regexp_eval($oid); |
|
368 | + $oidR=$this->regexp_eval($oid); |
|
369 | 369 | |
370 | - foreach($oidList as $val) |
|
370 | + foreach ($oidList as $val) |
|
371 | 371 | { |
372 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
372 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
373 | 373 | { |
374 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
374 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/', $val->value)) |
|
375 | 375 | { // If not a number, change " to ' and put " around it |
376 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
376 | + $val->value=preg_replace('/"/', "'", $val->value); |
|
377 | 377 | $val->value='"'.$val->value.'"'; |
378 | 378 | } |
379 | 379 | $rep=0; |
380 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
381 | - if ($rep==0) |
|
380 | + $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep); |
|
381 | + if ($rep == 0) |
|
382 | 382 | { |
383 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
383 | + $this->logging->log("Error in rule_eval", WARN, ''); |
|
384 | 384 | return false; |
385 | 385 | } |
386 | 386 | $found=1; |
387 | 387 | break; |
388 | 388 | } |
389 | 389 | } |
390 | - if ($found==0) |
|
390 | + if ($found == 0) |
|
391 | 391 | { // OID not found : throw error |
392 | 392 | throw new Exception('OID '.$oid.' not found in trap'); |
393 | 393 | } |
394 | 394 | } |
395 | 395 | $item=0; |
396 | 396 | $rule=$this->eval_cleanup($rule); |
397 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
397 | + $this->logging->log('Rule after clenup: '.$rule, INFO); |
|
398 | 398 | |
399 | - return $this->evaluation($rule,$item); |
|
399 | + return $this->evaluation($rule, $item); |
|
400 | 400 | } |
401 | 401 | |
402 | 402 | } |
403 | 403 | \ No newline at end of file |
@@ -68,14 +68,15 @@ discard block |
||
68 | 68 | $start=$item; |
69 | 69 | $parenthesis_count=0; |
70 | 70 | while (($item < strlen($rule)) // Not end of string AND |
71 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
71 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) { |
|
72 | + // Closing ')' or embeded () |
|
72 | 73 | { |
73 | 74 | if ($rule[$item] == '"' ) |
74 | 75 | { // pass through string |
75 | 76 | $item++; |
77 | + } |
|
76 | 78 | $item=$this->eval_getNext($rule,$item,'"'); |
77 | - } |
|
78 | - else{ |
|
79 | + } else{ |
|
79 | 80 | if ($rule[$item] == '(') |
80 | 81 | { |
81 | 82 | $parenthesis_count++; |
@@ -114,9 +115,11 @@ discard block |
||
114 | 115 | { |
115 | 116 | // function is : __function(param1,param2...) |
116 | 117 | $start=$item; |
117 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
118 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) { |
|
119 | + // Not end of string AND not opening '(' |
|
118 | 120 | { |
119 | 121 | $item++; |
122 | + } |
|
120 | 123 | } |
121 | 124 | if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
122 | 125 | |
@@ -138,7 +141,9 @@ discard block |
||
138 | 141 | { |
139 | 142 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
140 | 143 | } |
141 | - while ($rule[$item]==' ') $item++; |
|
144 | + while ($rule[$item]==' ') { |
|
145 | + $item++; |
|
146 | + } |
|
142 | 147 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
143 | 148 | { // number |
144 | 149 | return $this->get_number($rule, $item); |
@@ -176,7 +181,9 @@ discard block |
||
176 | 181 | |
177 | 182 | protected function eval_getOper($rule,&$item) |
178 | 183 | { |
179 | - while ($rule[$item]==' ') $item++; |
|
184 | + while ($rule[$item]==' ') { |
|
185 | + $item++; |
|
186 | + } |
|
180 | 187 | switch ($rule[$item]) |
181 | 188 | { |
182 | 189 | case '<': |
@@ -203,12 +210,13 @@ discard block |
||
203 | 210 | |
204 | 211 | private function check_negate_first($rule,&$item) |
205 | 212 | { |
206 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
213 | + if ( $rule[$item] == '!') { |
|
214 | + // If '!' found, negate next expression. |
|
207 | 215 | { |
208 | 216 | $item++; |
209 | - return true; |
|
210 | 217 | } |
211 | - else |
|
218 | + return true; |
|
219 | + } else |
|
212 | 220 | { |
213 | 221 | return false; |
214 | 222 | } |
@@ -228,7 +236,10 @@ discard block |
||
228 | 236 | case '&': $retVal= ($val1 && $val2); break; |
229 | 237 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
230 | 238 | } |
231 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
239 | + if ($negate === true) { |
|
240 | + $retVal = ! $retVal; |
|
241 | + } |
|
242 | + // Inverse result if negate before expression |
|
232 | 243 | |
233 | 244 | return $retVal; |
234 | 245 | } |
@@ -249,10 +260,14 @@ discard block |
||
249 | 260 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
250 | 261 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
251 | 262 | |
252 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
263 | + if ($item==strlen($rule)) { |
|
264 | + // If only element, return value, but only boolean |
|
253 | 265 | { |
254 | 266 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
255 | - if ($negate === true) $val1= ! $val1; |
|
267 | + } |
|
268 | + if ($negate === true) { |
|
269 | + $val1= ! $val1; |
|
270 | + } |
|
256 | 271 | return $val1; |
257 | 272 | } |
258 | 273 | |
@@ -261,31 +276,41 @@ discard block |
||
261 | 276 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
262 | 277 | |
263 | 278 | // Third element : number, string or () |
264 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
279 | + if ( $rule[$item] == '!') { |
|
280 | + // starts with a ! so evaluate whats next |
|
265 | 281 | { |
266 | 282 | $item++; |
267 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
283 | + } |
|
284 | + if ($typec != 1) { |
|
285 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
286 | + } |
|
268 | 287 | $val2= ! $this->evaluation($rule,$item); |
269 | 288 | $type2=2; // result is a boolean |
270 | - } |
|
271 | - else |
|
289 | + } else |
|
272 | 290 | { |
273 | 291 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
274 | 292 | } |
275 | 293 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
276 | 294 | |
277 | - if ($type1!=$type2) // cannot compare different types |
|
295 | + if ($type1!=$type2) { |
|
296 | + // cannot compare different types |
|
278 | 297 | { |
279 | 298 | throw new Exception("Cannot compare string & number : ".$rule); |
280 | 299 | } |
281 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
300 | + } |
|
301 | + if ($typec==1 && $type1 !=2) { |
|
302 | + // cannot use & or | with string/number |
|
282 | 303 | { |
283 | 304 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
284 | 305 | } |
306 | + } |
|
285 | 307 | |
286 | 308 | $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
287 | 309 | |
288 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
310 | + if ($item==strlen($rule)) { |
|
311 | + return $retVal; |
|
312 | + } |
|
313 | + // End of string : return evaluation |
|
289 | 314 | // check for logical operator : |
290 | 315 | switch ($rule[$item]) |
291 | 316 | { |
@@ -313,7 +338,9 @@ discard block |
||
313 | 338 | $rule2.=$rule[$item]; |
314 | 339 | $item++; |
315 | 340 | } |
316 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
341 | + if ($item == strlen ($rule)) { |
|
342 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
343 | + } |
|
317 | 344 | $rule2.=$rule[$item]; |
318 | 345 | $item++; |
319 | 346 | continue; |
@@ -355,10 +382,12 @@ discard block |
||
355 | 382 | */ |
356 | 383 | public function eval_rule($rule,$oidList) |
357 | 384 | { |
358 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
385 | + if ($rule==null || $rule == '') { |
|
386 | + // Empty rule is always true |
|
359 | 387 | { |
360 | 388 | return true; |
361 | 389 | } |
390 | + } |
|
362 | 391 | $matches=array(); |
363 | 392 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
364 | 393 | { |