@@ -11,330 +11,330 @@ |
||
| 11 | 11 | class Database |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - // Databases |
|
| 15 | - protected $trapDB=null; //< trap database |
|
| 16 | - protected $idoDB=null; //< ido database |
|
| 17 | - public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 18 | - public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 14 | + // Databases |
|
| 15 | + protected $trapDB=null; //< trap database |
|
| 16 | + protected $idoDB=null; //< ido database |
|
| 17 | + public $trapDBType; //< Type of database for traps (mysql, pgsql) |
|
| 18 | + public $idoDBType; //< Type of database for ido (mysql, pgsql) |
|
| 19 | 19 | |
| 20 | - protected $trapDSN; //< trap database connection params |
|
| 21 | - protected $trapUsername; //< trap database connection params |
|
| 22 | - protected $trapPass; //< trap database connection params |
|
| 23 | - public $dbPrefix; //< database tables prefix |
|
| 20 | + protected $trapDSN; //< trap database connection params |
|
| 21 | + protected $trapUsername; //< trap database connection params |
|
| 22 | + protected $trapPass; //< trap database connection params |
|
| 23 | + public $dbPrefix; //< database tables prefix |
|
| 24 | 24 | |
| 25 | - protected $idoSet; //< bool true is ido database set |
|
| 26 | - protected $idoDSN; //< trap database connection params |
|
| 27 | - protected $idoUsername; //< trap database connection params |
|
| 28 | - protected $idoPass; //< trap database connection params |
|
| 25 | + protected $idoSet; //< bool true is ido database set |
|
| 26 | + protected $idoDSN; //< trap database connection params |
|
| 27 | + protected $idoUsername; //< trap database connection params |
|
| 28 | + protected $idoPass; //< trap database connection params |
|
| 29 | 29 | |
| 30 | - // Logging function |
|
| 30 | + // Logging function |
|
| 31 | 31 | |
| 32 | - protected $logging; //< logging class |
|
| 32 | + protected $logging; //< logging class |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param Logging $logClass : where to log |
|
| 36 | - * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 37 | - */ |
|
| 38 | - function __construct($logClass,$dbParam,$dbPrefix) |
|
| 39 | - { |
|
| 40 | - $this->logging=$logClass; |
|
| 41 | - $this->dbPrefix=$dbPrefix; |
|
| 34 | + /** |
|
| 35 | + * @param Logging $logClass : where to log |
|
| 36 | + * @param array $dbParam : array of named params type,host,dbname,username,[port],[password] |
|
| 37 | + */ |
|
| 38 | + function __construct($logClass,$dbParam,$dbPrefix) |
|
| 39 | + { |
|
| 40 | + $this->logging=$logClass; |
|
| 41 | + $this->dbPrefix=$dbPrefix; |
|
| 42 | 42 | |
| 43 | - $this->trapDSN=$this->setupDSN($dbParam); |
|
| 44 | - $this->trapUsername = $dbParam['username']; |
|
| 45 | - $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 46 | - $this->trapDBType=$dbParam['db']; |
|
| 47 | - $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 48 | - $this->db_connect_trap(); |
|
| 43 | + $this->trapDSN=$this->setupDSN($dbParam); |
|
| 44 | + $this->trapUsername = $dbParam['username']; |
|
| 45 | + $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 46 | + $this->trapDBType=$dbParam['db']; |
|
| 47 | + $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO); |
|
| 48 | + $this->db_connect_trap(); |
|
| 49 | 49 | |
| 50 | - } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Setup and connect to IDO database |
|
| 54 | - * @param array $dbParam : array of named params |
|
| 55 | - */ |
|
| 56 | - public function setupIDO($dbParam) |
|
| 57 | - { |
|
| 58 | - $this->idoDSN=$this->setupDSN($dbParam); |
|
| 59 | - $this->idoUsername = $dbParam['username']; |
|
| 60 | - $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 61 | - $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 62 | - $this->idoDBType=$dbParam['db']; |
|
| 63 | - $this->db_connect_ido(); |
|
| 64 | - } |
|
| 52 | + /** |
|
| 53 | + * Setup and connect to IDO database |
|
| 54 | + * @param array $dbParam : array of named params |
|
| 55 | + */ |
|
| 56 | + public function setupIDO($dbParam) |
|
| 57 | + { |
|
| 58 | + $this->idoDSN=$this->setupDSN($dbParam); |
|
| 59 | + $this->idoUsername = $dbParam['username']; |
|
| 60 | + $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:''; |
|
| 61 | + $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO); |
|
| 62 | + $this->idoDBType=$dbParam['db']; |
|
| 63 | + $this->db_connect_ido(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Connect to IDO database |
|
| 68 | - * @return \PDO |
|
| 69 | - */ |
|
| 70 | - public function db_connect_ido() |
|
| 71 | - { |
|
| 72 | - if ($this->idoDB != null) { |
|
| 73 | - // Check if connection is still alive |
|
| 74 | - try { |
|
| 75 | - $this->idoDB->query('select 1')->fetchColumn(); |
|
| 76 | - return $this->idoDB; |
|
| 77 | - } catch (Exception $e) { |
|
| 78 | - // select 1 failed, try to reconnect. |
|
| 79 | - $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - try { |
|
| 83 | - $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 84 | - } catch (PDOException $e) { |
|
| 85 | - $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 86 | - } |
|
| 87 | - return $this->idoDB; |
|
| 88 | - } |
|
| 66 | + /** |
|
| 67 | + * Connect to IDO database |
|
| 68 | + * @return \PDO |
|
| 69 | + */ |
|
| 70 | + public function db_connect_ido() |
|
| 71 | + { |
|
| 72 | + if ($this->idoDB != null) { |
|
| 73 | + // Check if connection is still alive |
|
| 74 | + try { |
|
| 75 | + $this->idoDB->query('select 1')->fetchColumn(); |
|
| 76 | + return $this->idoDB; |
|
| 77 | + } catch (Exception $e) { |
|
| 78 | + // select 1 failed, try to reconnect. |
|
| 79 | + $this->logging->log('Database IDO connection lost, reconnecting',WARN); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + try { |
|
| 83 | + $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass); |
|
| 84 | + } catch (PDOException $e) { |
|
| 85 | + $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,''); |
|
| 86 | + } |
|
| 87 | + return $this->idoDB; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Connect to Trap database |
|
| 92 | - * @return \PDO |
|
| 93 | - */ |
|
| 94 | - public function db_connect_trap() |
|
| 95 | - { |
|
| 90 | + /** |
|
| 91 | + * Connect to Trap database |
|
| 92 | + * @return \PDO |
|
| 93 | + */ |
|
| 94 | + public function db_connect_trap() |
|
| 95 | + { |
|
| 96 | 96 | |
| 97 | - if ($this->trapDB != null) { |
|
| 98 | - // Check if connection is still alive |
|
| 99 | - try { |
|
| 100 | - $this->trapDB->query('select 1')->fetchColumn(); |
|
| 101 | - return $this->trapDB; |
|
| 102 | - } catch (Exception $e) { |
|
| 103 | - // select 1 failed, try to reconnect. |
|
| 104 | - $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - try { |
|
| 108 | - $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 109 | - } catch (PDOException $e) { |
|
| 110 | - $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 111 | - } |
|
| 112 | - return $this->trapDB; |
|
| 113 | - } |
|
| 97 | + if ($this->trapDB != null) { |
|
| 98 | + // Check if connection is still alive |
|
| 99 | + try { |
|
| 100 | + $this->trapDB->query('select 1')->fetchColumn(); |
|
| 101 | + return $this->trapDB; |
|
| 102 | + } catch (Exception $e) { |
|
| 103 | + // select 1 failed, try to reconnect. |
|
| 104 | + $this->logging->log('Database connection lost, reconnecting',WARN); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + try { |
|
| 108 | + $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass); |
|
| 109 | + } catch (PDOException $e) { |
|
| 110 | + $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,''); |
|
| 111 | + } |
|
| 112 | + return $this->trapDB; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Setup dsn and check parameters |
|
| 117 | - * @param array $configElmt |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - protected function setupDSN($configElmt) |
|
| 121 | - { |
|
| 122 | - if (!array_key_exists('db',$configElmt) || |
|
| 123 | - !array_key_exists('host',$configElmt) || |
|
| 124 | - !array_key_exists('dbname',$configElmt) || |
|
| 125 | - !array_key_exists('username',$configElmt)) |
|
| 126 | - { |
|
| 127 | - $this->logging->log('Missing DB params',ERROR); |
|
| 128 | - return ''; |
|
| 129 | - } |
|
| 115 | + /** |
|
| 116 | + * Setup dsn and check parameters |
|
| 117 | + * @param array $configElmt |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + protected function setupDSN($configElmt) |
|
| 121 | + { |
|
| 122 | + if (!array_key_exists('db',$configElmt) || |
|
| 123 | + !array_key_exists('host',$configElmt) || |
|
| 124 | + !array_key_exists('dbname',$configElmt) || |
|
| 125 | + !array_key_exists('username',$configElmt)) |
|
| 126 | + { |
|
| 127 | + $this->logging->log('Missing DB params',ERROR); |
|
| 128 | + return ''; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 132 | - $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 131 | + // $dsn = 'mysql:dbname=traps;host=127.0.0.1'; |
|
| 132 | + $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host']; |
|
| 133 | 133 | |
| 134 | - if (array_key_exists('port', $configElmt)) |
|
| 135 | - { |
|
| 136 | - $dsn .= ';port='.$configElmt['port']; |
|
| 137 | - } |
|
| 138 | - return $dsn; |
|
| 139 | - } |
|
| 134 | + if (array_key_exists('port', $configElmt)) |
|
| 135 | + { |
|
| 136 | + $dsn .= ';port='.$configElmt['port']; |
|
| 137 | + } |
|
| 138 | + return $dsn; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | 141 | |
| 142 | - /** Create database schema |
|
| 143 | - * @param $schema_file string File to read schema from |
|
| 144 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 145 | - */ |
|
| 146 | - public function create_schema($schema_file,$table_prefix) |
|
| 147 | - { |
|
| 148 | - //Read data from snmptrapd from stdin |
|
| 149 | - $input_stream=fopen($schema_file, 'r'); |
|
| 142 | + /** Create database schema |
|
| 143 | + * @param $schema_file string File to read schema from |
|
| 144 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 145 | + */ |
|
| 146 | + public function create_schema($schema_file,$table_prefix) |
|
| 147 | + { |
|
| 148 | + //Read data from snmptrapd from stdin |
|
| 149 | + $input_stream=fopen($schema_file, 'r'); |
|
| 150 | 150 | |
| 151 | - if ($input_stream=== false) |
|
| 152 | - { |
|
| 153 | - $this->logging->log("Error reading schema !",ERROR,''); |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - $newline=''; |
|
| 157 | - $cur_table=''; |
|
| 158 | - $cur_table_array=array(); |
|
| 159 | - $db_conn=$this->db_connect_trap(); |
|
| 151 | + if ($input_stream=== false) |
|
| 152 | + { |
|
| 153 | + $this->logging->log("Error reading schema !",ERROR,''); |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + $newline=''; |
|
| 157 | + $cur_table=''; |
|
| 158 | + $cur_table_array=array(); |
|
| 159 | + $db_conn=$this->db_connect_trap(); |
|
| 160 | 160 | |
| 161 | - while (($line=fgets($input_stream)) !== false) |
|
| 162 | - { |
|
| 163 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 164 | - if (preg_match('/; *$/', $newline)) |
|
| 165 | - { |
|
| 166 | - $sql= $newline; |
|
| 167 | - if ($db_conn->query($sql) === false) { |
|
| 168 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 169 | - return; |
|
| 170 | - } |
|
| 171 | - if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 172 | - { |
|
| 173 | - $cur_table='table '.$cur_table_array[1]; |
|
| 174 | - } |
|
| 175 | - else |
|
| 176 | - { |
|
| 177 | - $cur_table='secret SQL stuff :-)'; |
|
| 178 | - } |
|
| 179 | - $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 180 | - $newline=''; |
|
| 181 | - } |
|
| 182 | - } |
|
| 161 | + while (($line=fgets($input_stream)) !== false) |
|
| 162 | + { |
|
| 163 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 164 | + if (preg_match('/; *$/', $newline)) |
|
| 165 | + { |
|
| 166 | + $sql= $newline; |
|
| 167 | + if ($db_conn->query($sql) === false) { |
|
| 168 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 169 | + return; |
|
| 170 | + } |
|
| 171 | + if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 172 | + { |
|
| 173 | + $cur_table='table '.$cur_table_array[1]; |
|
| 174 | + } |
|
| 175 | + else |
|
| 176 | + { |
|
| 177 | + $cur_table='secret SQL stuff :-)'; |
|
| 178 | + } |
|
| 179 | + $this->logging->log('Creating : ' . $cur_table,INFO ); |
|
| 180 | + $newline=''; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - $sql= $newline; |
|
| 185 | - if ($sql != '' ) |
|
| 186 | - { |
|
| 187 | - if ($db_conn->query($sql) === false) { |
|
| 188 | - $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 189 | - return; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - $this->logging->log('Schema created',INFO); |
|
| 193 | - } |
|
| 184 | + $sql= $newline; |
|
| 185 | + if ($sql != '' ) |
|
| 186 | + { |
|
| 187 | + if ($db_conn->query($sql) === false) { |
|
| 188 | + $this->logging->log('Error create schema : '.$sql,ERROR,''); |
|
| 189 | + return; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + $this->logging->log('Schema created',INFO); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Update database schema from current (as set in db) to $target_version |
|
| 197 | - * @param $prefix string file prefix of sql update File |
|
| 198 | - * @param $target_version int target db version number |
|
| 199 | - * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 200 | - * @param bool $getmsg : only get messages from version upgrades |
|
| 201 | - * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 202 | - */ |
|
| 203 | - public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 204 | - { |
|
| 205 | - // Get current db number |
|
| 206 | - $db_conn=$this->db_connect_trap(); |
|
| 207 | - $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 208 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 209 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 210 | - $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 211 | - return 'ERROR'; |
|
| 212 | - } |
|
| 213 | - $version=$ret_code->fetchAll(); |
|
| 214 | - $cur_version=$version[0]['value']; |
|
| 195 | + /** |
|
| 196 | + * Update database schema from current (as set in db) to $target_version |
|
| 197 | + * @param $prefix string file prefix of sql update File |
|
| 198 | + * @param $target_version int target db version number |
|
| 199 | + * @param $table_prefix string to replace #PREFIX# in schema file by this |
|
| 200 | + * @param bool $getmsg : only get messages from version upgrades |
|
| 201 | + * @return string : if $getmsg=true, return messages or 'ERROR' on error. |
|
| 202 | + */ |
|
| 203 | + public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) |
|
| 204 | + { |
|
| 205 | + // Get current db number |
|
| 206 | + $db_conn=$this->db_connect_trap(); |
|
| 207 | + $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' '; |
|
| 208 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 209 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 210 | + $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); |
|
| 211 | + return 'ERROR'; |
|
| 212 | + } |
|
| 213 | + $version=$ret_code->fetchAll(); |
|
| 214 | + $cur_version=$version[0]['value']; |
|
| 215 | 215 | |
| 216 | - if ($this->trapDBType == 'pgsql') |
|
| 217 | - { |
|
| 218 | - $prefix .= 'update_pgsql/schema_'; |
|
| 219 | - } |
|
| 220 | - else |
|
| 221 | - { |
|
| 222 | - $prefix .= 'update_sql/schema_'; |
|
| 223 | - } |
|
| 224 | - //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 225 | - if ($getmsg === true) |
|
| 226 | - { |
|
| 227 | - return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 228 | - } |
|
| 216 | + if ($this->trapDBType == 'pgsql') |
|
| 217 | + { |
|
| 218 | + $prefix .= 'update_pgsql/schema_'; |
|
| 219 | + } |
|
| 220 | + else |
|
| 221 | + { |
|
| 222 | + $prefix .= 'update_sql/schema_'; |
|
| 223 | + } |
|
| 224 | + //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; |
|
| 225 | + if ($getmsg === true) |
|
| 226 | + { |
|
| 227 | + return $this->update_schema_message($prefix, $cur_version, $target_version); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 231 | - { |
|
| 232 | - return 'ERROR'; |
|
| 233 | - } |
|
| 234 | - return ''; |
|
| 230 | + if ($this->update_schema_do($prefix, $cur_version, $target_version, $table_prefix) === true) |
|
| 231 | + { |
|
| 232 | + return 'ERROR'; |
|
| 233 | + } |
|
| 234 | + return ''; |
|
| 235 | 235 | |
| 236 | - } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * Update database schema from current (as set in db) to $target_version |
|
| 240 | - * @param string $prefix file prefix of sql update File |
|
| 241 | - * @param int $cur_version current db version number |
|
| 242 | - * @param int $target_version target db version number |
|
| 243 | - * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 244 | - * @return bool : true on error |
|
| 245 | - */ |
|
| 246 | - public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 247 | - { |
|
| 248 | - while($cur_version<$target_version) |
|
| 249 | - { // TODO : execute pre & post scripts |
|
| 250 | - $cur_version++; |
|
| 251 | - $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 252 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 253 | - $input_stream=fopen($updateFile, 'r'); |
|
| 254 | - if ($input_stream=== false) |
|
| 255 | - { |
|
| 256 | - $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 257 | - return true; |
|
| 258 | - } |
|
| 259 | - $newline=''; |
|
| 260 | - $db_conn=$this->db_connect_trap(); |
|
| 261 | - $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 262 | - while (($line=fgets($input_stream)) !== false) |
|
| 263 | - { |
|
| 264 | - if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 265 | - $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 266 | - if (preg_match('/; *$/', $newline)) |
|
| 267 | - { |
|
| 268 | - $sql_req=$db_conn->prepare($newline); |
|
| 269 | - if ($sql_req->execute() === false) { |
|
| 270 | - $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 271 | - return true; |
|
| 272 | - } |
|
| 273 | - $cur_table_array=array(); |
|
| 274 | - if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 275 | - { |
|
| 276 | - $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 277 | - } |
|
| 278 | - else |
|
| 279 | - { |
|
| 280 | - $cur_table='secret SQL stuff :-)'; |
|
| 281 | - //$cur_table=$newline; |
|
| 282 | - } |
|
| 283 | - $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 238 | + /** |
|
| 239 | + * Update database schema from current (as set in db) to $target_version |
|
| 240 | + * @param string $prefix file prefix of sql update File |
|
| 241 | + * @param int $cur_version current db version number |
|
| 242 | + * @param int $target_version target db version number |
|
| 243 | + * @param string $table_prefix to replace #PREFIX# in schema file by this |
|
| 244 | + * @return bool : true on error |
|
| 245 | + */ |
|
| 246 | + public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix) |
|
| 247 | + { |
|
| 248 | + while($cur_version<$target_version) |
|
| 249 | + { // TODO : execute pre & post scripts |
|
| 250 | + $cur_version++; |
|
| 251 | + $this->logging->log('Updating to version : ' .$cur_version ,INFO ); |
|
| 252 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 253 | + $input_stream=fopen($updateFile, 'r'); |
|
| 254 | + if ($input_stream=== false) |
|
| 255 | + { |
|
| 256 | + $this->logging->log("Error reading update file ". $updateFile,ERROR); |
|
| 257 | + return true; |
|
| 258 | + } |
|
| 259 | + $newline=''; |
|
| 260 | + $db_conn=$this->db_connect_trap(); |
|
| 261 | + $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
| 262 | + while (($line=fgets($input_stream)) !== false) |
|
| 263 | + { |
|
| 264 | + if (preg_match('/^#/', $line)) continue; // ignore comment lines |
|
| 265 | + $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); |
|
| 266 | + if (preg_match('/; *$/', $newline)) |
|
| 267 | + { |
|
| 268 | + $sql_req=$db_conn->prepare($newline); |
|
| 269 | + if ($sql_req->execute() === false) { |
|
| 270 | + $this->logging->log('Error create schema : '.$newline,ERROR); |
|
| 271 | + return true; |
|
| 272 | + } |
|
| 273 | + $cur_table_array=array(); |
|
| 274 | + if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) |
|
| 275 | + { |
|
| 276 | + $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; |
|
| 277 | + } |
|
| 278 | + else |
|
| 279 | + { |
|
| 280 | + $cur_table='secret SQL stuff :-)'; |
|
| 281 | + //$cur_table=$newline; |
|
| 282 | + } |
|
| 283 | + $this->logging->log('Doing : ' . $cur_table,INFO ); |
|
| 284 | 284 | |
| 285 | - $newline=''; |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - fclose($input_stream); |
|
| 285 | + $newline=''; |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + fclose($input_stream); |
|
| 289 | 289 | |
| 290 | - $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 291 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 292 | - if ($db_conn->query($sql) === false) { |
|
| 293 | - $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 294 | - return true; |
|
| 295 | - } |
|
| 290 | + $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )'; |
|
| 291 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 292 | + if ($db_conn->query($sql) === false) { |
|
| 293 | + $this->logging->log('Cannot update db version. Query : ' . $sql,WARN); |
|
| 294 | + return true; |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 298 | - } |
|
| 299 | - return false; |
|
| 300 | - } |
|
| 297 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 298 | + } |
|
| 299 | + return false; |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - /** |
|
| 303 | - * Get database message for update to $target_version |
|
| 304 | - * @param string $prefix file prefix of sql update File |
|
| 305 | - * @param int $cur_version current db version number |
|
| 306 | - * @param int $target_version target db version number |
|
| 307 | - * @return string : return messages or 'ERROR'. |
|
| 308 | - */ |
|
| 309 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 310 | - { |
|
| 302 | + /** |
|
| 303 | + * Get database message for update to $target_version |
|
| 304 | + * @param string $prefix file prefix of sql update File |
|
| 305 | + * @param int $cur_version current db version number |
|
| 306 | + * @param int $target_version target db version number |
|
| 307 | + * @return string : return messages or 'ERROR'. |
|
| 308 | + */ |
|
| 309 | + private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 310 | + { |
|
| 311 | 311 | |
| 312 | - $message=''; |
|
| 313 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 314 | - while($cur_version<$target_version) |
|
| 315 | - { |
|
| 316 | - $cur_version++; |
|
| 317 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 318 | - $input_stream=fopen($updateFile, 'r'); |
|
| 319 | - if ($input_stream=== false) |
|
| 320 | - { |
|
| 321 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 322 | - return 'ERROR'; |
|
| 323 | - } |
|
| 324 | - do |
|
| 325 | - { |
|
| 326 | - $line=fgets($input_stream); |
|
| 327 | - } |
|
| 328 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 329 | - fclose($input_stream); |
|
| 330 | - if ($line === false) |
|
| 331 | - { |
|
| 332 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 333 | - return ''; |
|
| 334 | - } |
|
| 335 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 336 | - } |
|
| 337 | - return $message; |
|
| 338 | - } |
|
| 312 | + $message=''; |
|
| 313 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 314 | + while($cur_version<$target_version) |
|
| 315 | + { |
|
| 316 | + $cur_version++; |
|
| 317 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 318 | + $input_stream=fopen($updateFile, 'r'); |
|
| 319 | + if ($input_stream=== false) |
|
| 320 | + { |
|
| 321 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 322 | + return 'ERROR'; |
|
| 323 | + } |
|
| 324 | + do |
|
| 325 | + { |
|
| 326 | + $line=fgets($input_stream); |
|
| 327 | + } |
|
| 328 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 329 | + fclose($input_stream); |
|
| 330 | + if ($line === false) |
|
| 331 | + { |
|
| 332 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 333 | + return ''; |
|
| 334 | + } |
|
| 335 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 336 | + } |
|
| 337 | + return $message; |
|
| 338 | + } |
|
| 339 | 339 | |
| 340 | 340 | } |
| 341 | 341 | \ No newline at end of file |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | |
| 58 | 58 | function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='') |
| 59 | 59 | { |
| 60 | - // Paths of ini files |
|
| 60 | + // Paths of ini files |
|
| 61 | 61 | $this->icingaweb2_etc=$etc_dir; |
| 62 | 62 | $this->trap_module_config=$this->icingaweb2_etc."/modules/trapdirector/config.ini"; |
| 63 | 63 | $this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini"; |
@@ -66,18 +66,18 @@ discard block |
||
| 66 | 66 | $this->logging = new Logging(); |
| 67 | 67 | if ($baseLogLevel != null) |
| 68 | 68 | { |
| 69 | - $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 70 | - $this->logSetup=true; |
|
| 69 | + $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile); |
|
| 70 | + $this->logSetup=true; |
|
| 71 | 71 | } |
| 72 | 72 | else |
| 73 | - $this->logSetup=false; |
|
| 73 | + $this->logSetup=false; |
|
| 74 | 74 | $this->logging->log('Loggin started', INFO); |
| 75 | 75 | |
| 76 | 76 | // Get options from ini files |
| 77 | 77 | $trapConfig=parse_ini_file($this->trap_module_config,true); |
| 78 | 78 | if ($trapConfig == false) |
| 79 | 79 | { |
| 80 | - $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 80 | + $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); |
|
| 81 | 81 | } |
| 82 | 82 | $this->getMainOptions($trapConfig); // Get main options from ini file |
| 83 | 83 | $this->setupDatabase($trapConfig); // Setup database class |
@@ -109,26 +109,26 @@ discard block |
||
| 109 | 109 | */ |
| 110 | 110 | protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null) |
| 111 | 111 | { |
| 112 | - if (!isset($option_array[$option_category][$option_name])) |
|
| 113 | - { |
|
| 114 | - if ($message === null) |
|
| 115 | - { |
|
| 116 | - $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 117 | - } |
|
| 118 | - $this->logging->log($message,$log_level,'syslog'); |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - else |
|
| 122 | - { |
|
| 123 | - $option_var=$option_array[$option_category][$option_name]; |
|
| 124 | - return true; |
|
| 125 | - } |
|
| 112 | + if (!isset($option_array[$option_category][$option_name])) |
|
| 113 | + { |
|
| 114 | + if ($message === null) |
|
| 115 | + { |
|
| 116 | + $message='No ' . $option_name . ' in config file: '. $this->trap_module_config; |
|
| 117 | + } |
|
| 118 | + $this->logging->log($message,$log_level,'syslog'); |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + else |
|
| 122 | + { |
|
| 123 | + $option_var=$option_array[$option_category][$option_name]; |
|
| 124 | + return true; |
|
| 125 | + } |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
| 129 | 129 | * Get options from ini file |
| 130 | 130 | * @param array $trap_config : ini file array |
| 131 | - */ |
|
| 131 | + */ |
|
| 132 | 132 | protected function getMainOptions($trapConfig) |
| 133 | 133 | { |
| 134 | 134 | |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | // API options |
| 148 | 148 | if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname)) |
| 149 | 149 | { |
| 150 | - $this->api_use=true; |
|
| 151 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 152 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 153 | - $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 150 | + $this->api_use=true; |
|
| 151 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port); |
|
| 152 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username); |
|
| 153 | + $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password); |
|
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
@@ -160,45 +160,45 @@ discard block |
||
| 160 | 160 | */ |
| 161 | 161 | protected function setupDatabase($trapConfig) |
| 162 | 162 | { |
| 163 | - // Trap database |
|
| 164 | - if (!array_key_exists('database',$trapConfig['config'])) |
|
| 165 | - { |
|
| 166 | - $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 167 | - return; |
|
| 168 | - } |
|
| 169 | - $dbTrapName=$trapConfig['config']['database']; |
|
| 170 | - $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 163 | + // Trap database |
|
| 164 | + if (!array_key_exists('database',$trapConfig['config'])) |
|
| 165 | + { |
|
| 166 | + $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,''); |
|
| 167 | + return; |
|
| 168 | + } |
|
| 169 | + $dbTrapName=$trapConfig['config']['database']; |
|
| 170 | + $this->logging->log("Found database in config file: ".$dbTrapName,INFO ); |
|
| 171 | 171 | |
| 172 | 172 | if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false) |
| 173 | - { |
|
| 174 | - $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 175 | - return; |
|
| 176 | - } |
|
| 177 | - if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 178 | - { |
|
| 179 | - $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 180 | - return; |
|
| 181 | - } |
|
| 173 | + { |
|
| 174 | + $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); |
|
| 175 | + return; |
|
| 176 | + } |
|
| 177 | + if (!array_key_exists($dbTrapName,$dbConfig)) |
|
| 178 | + { |
|
| 179 | + $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 183 | + $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix); |
|
| 184 | 184 | |
| 185 | - if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 185 | + if ($this->api_use === true) return; // In case of API use, no IDO is necessary |
|
| 186 | 186 | |
| 187 | - // IDO Database |
|
| 188 | - if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 189 | - { |
|
| 190 | - $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 191 | - } |
|
| 192 | - $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 187 | + // IDO Database |
|
| 188 | + if (!array_key_exists('IDOdatabase',$trapConfig['config'])) |
|
| 189 | + { |
|
| 190 | + $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); |
|
| 191 | + } |
|
| 192 | + $dbIdoName=$trapConfig['config']['IDOdatabase']; |
|
| 193 | 193 | |
| 194 | - $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 195 | - if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 196 | - { |
|
| 197 | - $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 198 | - return; |
|
| 199 | - } |
|
| 194 | + $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO ); |
|
| 195 | + if (!array_key_exists($dbIdoName,$dbConfig)) |
|
| 196 | + { |
|
| 197 | + $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,''); |
|
| 198 | + return; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 201 | + $this->trapsDB->setupIDO($dbConfig[$dbIdoName]); |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | /** |
@@ -209,9 +209,9 @@ discard block |
||
| 209 | 209 | // Database options |
| 210 | 210 | if ($this->logSetup === false) // Only if logging was no setup in constructor |
| 211 | 211 | { |
| 212 | - $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 213 | - $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 214 | - $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 212 | + $this->getDBConfigIfSet('log_level',$this->logging->debugLevel); |
|
| 213 | + $this->getDBConfigIfSet('log_destination',$this->logging->outputMode); |
|
| 214 | + $this->getDBConfigIfSet('log_file',$this->logging->outputFile); |
|
| 215 | 215 | } |
| 216 | 216 | } |
| 217 | 217 | |
@@ -222,10 +222,10 @@ discard block |
||
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | - * Get data from db_config |
|
| 226 | - * @param $element string name of param |
|
| 227 | - * @return mixed : value (or null) |
|
| 228 | - */ |
|
| 225 | + * Get data from db_config |
|
| 226 | + * @param $element string name of param |
|
| 227 | + * @return mixed : value (or null) |
|
| 228 | + */ |
|
| 229 | 229 | protected function getDBConfig($element) |
| 230 | 230 | { |
| 231 | 231 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -243,15 +243,15 @@ discard block |
||
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** OBSOLETE Send log. Throws exception on critical error |
| 246 | - * @param string $message Message to log |
|
| 247 | - * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 248 | - * @param string $destination file/syslog/display |
|
| 249 | - * @return void |
|
| 250 | - **/ |
|
| 246 | + * @param string $message Message to log |
|
| 247 | + * @param int $level 1=critical 2=warning 3=trace 4=debug |
|
| 248 | + * @param string $destination file/syslog/display |
|
| 249 | + * @return void |
|
| 250 | + **/ |
|
| 251 | 251 | public function trapLog( $message, $level, $destination ='') // OBSOLETE |
| 252 | 252 | { |
| 253 | 253 | // TODO : replace ref with $this->logging->log |
| 254 | - $this->logging->log($message, $level, $destination); |
|
| 254 | + $this->logging->log($message, $level, $destination); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | public function setLogging($debugLvl,$outputType,$outputOption=null) // OBSOLETE |
@@ -261,19 +261,19 @@ discard block |
||
| 261 | 261 | |
| 262 | 262 | protected function getAPI() |
| 263 | 263 | { |
| 264 | - if ($this->icinga2api == null) |
|
| 265 | - { |
|
| 266 | - $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 267 | - } |
|
| 268 | - return $this->icinga2api; |
|
| 264 | + if ($this->icinga2api == null) |
|
| 265 | + { |
|
| 266 | + $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port); |
|
| 267 | + } |
|
| 268 | + return $this->icinga2api; |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | |
| 272 | 272 | /** |
| 273 | 273 | * read data from stream |
| 274 | - * @param $stream string input stream, defaults to "php://stdin" |
|
| 275 | - * @return mixed array trap data or exception with error |
|
| 276 | - */ |
|
| 274 | + * @param $stream string input stream, defaults to "php://stdin" |
|
| 275 | + * @return mixed array trap data or exception with error |
|
| 276 | + */ |
|
| 277 | 277 | public function read_trap($stream='php://stdin') |
| 278 | 278 | { |
| 279 | 279 | //Read data from snmptrapd from stdin |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | |
| 282 | 282 | if ($input_stream === false) |
| 283 | 283 | { |
| 284 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 284 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)"); |
|
| 285 | 285 | $this->logging->log("Error reading stdin !",ERROR,''); |
| 286 | 286 | return null; // note : exception thrown by logging |
| 287 | 287 | } |
@@ -290,21 +290,21 @@ discard block |
||
| 290 | 290 | $this->receivingHost=chop(fgets($input_stream)); |
| 291 | 291 | if ($this->receivingHost === false) |
| 292 | 292 | { |
| 293 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 293 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)"); |
|
| 294 | 294 | $this->logging->log("Error reading Host !",ERROR,''); |
| 295 | 295 | } |
| 296 | 296 | // line 2 IP:port=>IP:port |
| 297 | 297 | $IP=chop(fgets($input_stream)); |
| 298 | 298 | if ($IP === false) |
| 299 | 299 | { |
| 300 | - $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 300 | + $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)"); |
|
| 301 | 301 | $this->logging->log("Error reading IP !",ERROR,''); |
| 302 | 302 | } |
| 303 | 303 | $matches=array(); |
| 304 | 304 | $ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches); |
| 305 | 305 | if ($ret_code===0 || $ret_code===false) |
| 306 | 306 | { |
| 307 | - $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 307 | + $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)"); |
|
| 308 | 308 | $this->logging->log('Error parsing IP : '.$IP,ERROR,''); |
| 309 | 309 | } |
| 310 | 310 | else |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | } |
| 326 | 326 | else |
| 327 | 327 | { |
| 328 | - 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')) |
|
| 328 | + 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')) |
|
| 329 | 329 | { |
| 330 | 330 | $this->trap_data['trap_oid']=$matches[2]; |
| 331 | 331 | } |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | |
| 342 | 342 | if ($this->trap_data['trap_oid']=='unknown') |
| 343 | 343 | { |
| 344 | - $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 344 | + $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']); |
|
| 345 | 345 | $this->logging->log('no trap oid found',ERROR,''); |
| 346 | 346 | } |
| 347 | 347 | |
@@ -371,9 +371,9 @@ discard block |
||
| 371 | 371 | |
| 372 | 372 | /** |
| 373 | 373 | * Translate oid into array(MIB,Name) |
| 374 | - * @param $oid string oid to translate |
|
| 375 | - * @return mixed : null if not found or array(MIB,Name) |
|
| 376 | - */ |
|
| 374 | + * @param $oid string oid to translate |
|
| 375 | + * @return mixed : null if not found or array(MIB,Name) |
|
| 376 | + */ |
|
| 377 | 377 | public function translateOID($oid) |
| 378 | 378 | { |
| 379 | 379 | // try from database |
@@ -406,7 +406,7 @@ discard block |
||
| 406 | 406 | |
| 407 | 407 | // Try to get oid name from snmptranslate |
| 408 | 408 | $translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
| 409 | - ' '.$oid); |
|
| 409 | + ' '.$oid); |
|
| 410 | 410 | $matches=array(); |
| 411 | 411 | $ret_code=preg_match('/(.*)::(.*)/',$translate,$matches); |
| 412 | 412 | if ($ret_code===0 || $ret_code === false) { |
@@ -419,9 +419,9 @@ discard block |
||
| 419 | 419 | |
| 420 | 420 | /** |
| 421 | 421 | * Erase old trap records |
| 422 | - * @param integer $days : erase traps when more than $days old |
|
| 423 | - * @return integer : number of lines deleted |
|
| 424 | - **/ |
|
| 422 | + * @param integer $days : erase traps when more than $days old |
|
| 423 | + * @return integer : number of lines deleted |
|
| 424 | + **/ |
|
| 425 | 425 | public function eraseOldTraps($days=0) |
| 426 | 426 | { |
| 427 | 427 | if ($days==0) |
@@ -446,72 +446,72 @@ discard block |
||
| 446 | 446 | public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null) |
| 447 | 447 | { |
| 448 | 448 | |
| 449 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 449 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 450 | 450 | |
| 451 | - // add date time |
|
| 452 | - $insert_col ='date_received,status'; |
|
| 453 | - $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 451 | + // add date time |
|
| 452 | + $insert_col ='date_received,status'; |
|
| 453 | + $insert_val = "'" . date("Y-m-d H:i:s")."','error'"; |
|
| 454 | 454 | |
| 455 | - if ($sourceIP !=null) |
|
| 456 | - { |
|
| 457 | - $insert_col .=',source_ip'; |
|
| 458 | - $insert_val .=",'". $sourceIP ."'"; |
|
| 459 | - } |
|
| 460 | - if ($trapoid !=null) |
|
| 461 | - { |
|
| 462 | - $insert_col .=',trap_oid'; |
|
| 463 | - $insert_val .=",'". $trapoid ."'"; |
|
| 464 | - } |
|
| 465 | - $insert_col .=',status_detail'; |
|
| 466 | - $insert_val .=",'". $message ."'"; |
|
| 455 | + if ($sourceIP !=null) |
|
| 456 | + { |
|
| 457 | + $insert_col .=',source_ip'; |
|
| 458 | + $insert_val .=",'". $sourceIP ."'"; |
|
| 459 | + } |
|
| 460 | + if ($trapoid !=null) |
|
| 461 | + { |
|
| 462 | + $insert_col .=',trap_oid'; |
|
| 463 | + $insert_val .=",'". $trapoid ."'"; |
|
| 464 | + } |
|
| 465 | + $insert_col .=',status_detail'; |
|
| 466 | + $insert_val .=",'". $message ."'"; |
|
| 467 | 467 | |
| 468 | - $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 468 | + $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')'; |
|
| 469 | 469 | |
| 470 | - switch ($this->trapsDB->trapDBType) |
|
| 471 | - { |
|
| 472 | - case 'pgsql': |
|
| 473 | - $sql .= ' RETURNING id;'; |
|
| 474 | - $this->logging->log('sql : '.$sql,INFO); |
|
| 475 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 476 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 477 | - } |
|
| 478 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 479 | - // Get last id to insert oid/values in secondary table |
|
| 480 | - if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 470 | + switch ($this->trapsDB->trapDBType) |
|
| 471 | + { |
|
| 472 | + case 'pgsql': |
|
| 473 | + $sql .= ' RETURNING id;'; |
|
| 474 | + $this->logging->log('sql : '.$sql,INFO); |
|
| 475 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 476 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 477 | + } |
|
| 478 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 479 | + // Get last id to insert oid/values in secondary table |
|
| 480 | + if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 481 | 481 | |
| 482 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 483 | - } |
|
| 484 | - if (! isset($inserted_id_ret['id'])) { |
|
| 485 | - $this->logging->log('Error getting id',1,''); |
|
| 486 | - } |
|
| 487 | - $this->trap_id=$inserted_id_ret['id']; |
|
| 488 | - break; |
|
| 489 | - case 'mysql': |
|
| 490 | - $sql .= ';'; |
|
| 491 | - $this->logging->log('sql : '.$sql,INFO ); |
|
| 492 | - if ($db_conn->query($sql) === false) { |
|
| 493 | - $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 494 | - } |
|
| 495 | - $this->logging->log('SQL insertion OK',INFO ); |
|
| 496 | - // Get last id to insert oid/values in secondary table |
|
| 497 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 498 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 499 | - $this->logging->log('Erreur recuperation id',1,''); |
|
| 500 | - } |
|
| 482 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 483 | + } |
|
| 484 | + if (! isset($inserted_id_ret['id'])) { |
|
| 485 | + $this->logging->log('Error getting id',1,''); |
|
| 486 | + } |
|
| 487 | + $this->trap_id=$inserted_id_ret['id']; |
|
| 488 | + break; |
|
| 489 | + case 'mysql': |
|
| 490 | + $sql .= ';'; |
|
| 491 | + $this->logging->log('sql : '.$sql,INFO ); |
|
| 492 | + if ($db_conn->query($sql) === false) { |
|
| 493 | + $this->logging->log('Error SQL insert : '.$sql,1,''); |
|
| 494 | + } |
|
| 495 | + $this->logging->log('SQL insertion OK',INFO ); |
|
| 496 | + // Get last id to insert oid/values in secondary table |
|
| 497 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 498 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 499 | + $this->logging->log('Erreur recuperation id',1,''); |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 503 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 504 | - $this->trap_id=$inserted_id; |
|
| 505 | - break; |
|
| 506 | - default: |
|
| 507 | - $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 508 | - } |
|
| 502 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 503 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 504 | + $this->trap_id=$inserted_id; |
|
| 505 | + break; |
|
| 506 | + default: |
|
| 507 | + $this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 510 | + $this->logging->log('id found: '. $this->trap_id,INFO ); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | /** Write trap data to trap database |
| 514 | - */ |
|
| 514 | + */ |
|
| 515 | 515 | public function writeTrapToDB() |
| 516 | 516 | { |
| 517 | 517 | |
@@ -609,10 +609,10 @@ discard block |
||
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | /** Get rules from rule database with ip and oid |
| 612 | - * @param $ip string ipv4 or ipv6 |
|
| 613 | - * @param $oid string oid in numeric |
|
| 614 | - * @return mixed : PDO object or false |
|
| 615 | - */ |
|
| 612 | + * @param $ip string ipv4 or ipv6 |
|
| 613 | + * @param $oid string oid in numeric |
|
| 614 | + * @return mixed : PDO object or false |
|
| 615 | + */ |
|
| 616 | 616 | protected function getRules($ip,$oid) |
| 617 | 617 | { |
| 618 | 618 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -671,9 +671,9 @@ discard block |
||
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | /** Add rule match to rule |
| 674 | - * @param id int : rule id |
|
| 675 | - * @param set int : value to set |
|
| 676 | - */ |
|
| 674 | + * @param id int : rule id |
|
| 675 | + * @param set int : value to set |
|
| 676 | + */ |
|
| 677 | 677 | protected function add_rule_match($id, $set) |
| 678 | 678 | { |
| 679 | 679 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -690,53 +690,53 @@ discard block |
||
| 690 | 690 | * @param integer $state numerical staus |
| 691 | 691 | * @param string $display |
| 692 | 692 | * @returnn bool true is service check was sent without error |
| 693 | - */ |
|
| 693 | + */ |
|
| 694 | 694 | public function serviceCheckResult($host,$service,$state,$display) |
| 695 | 695 | { |
| 696 | - if ($this->api_use === false) |
|
| 697 | - { |
|
| 698 | - $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 699 | - $host.';' .$service .';' . $state . ';'.$display; |
|
| 700 | - $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 696 | + if ($this->api_use === false) |
|
| 697 | + { |
|
| 698 | + $send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' . |
|
| 699 | + $host.';' .$service .';' . $state . ';'.$display; |
|
| 700 | + $this->logging->log( $send." : to : " .$this->icinga2cmd,INFO ); |
|
| 701 | 701 | |
| 702 | - // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 703 | - exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 704 | - return true; |
|
| 705 | - } |
|
| 706 | - else |
|
| 707 | - { |
|
| 708 | - $api = $this->getAPI(); |
|
| 709 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 710 | - list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
| 711 | - if ($retcode == false) |
|
| 712 | - { |
|
| 713 | - $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 714 | - return false; |
|
| 715 | - } |
|
| 716 | - else |
|
| 717 | - { |
|
| 718 | - $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 719 | - return true; |
|
| 720 | - } |
|
| 721 | - } |
|
| 702 | + // TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default.... |
|
| 703 | + exec('echo "'.$send.'" > ' .$this->icinga2cmd); |
|
| 704 | + return true; |
|
| 705 | + } |
|
| 706 | + else |
|
| 707 | + { |
|
| 708 | + $api = $this->getAPI(); |
|
| 709 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 710 | + list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display); |
|
| 711 | + if ($retcode == false) |
|
| 712 | + { |
|
| 713 | + $this->logging->log( "Error sending result : " .$retmessage,WARN,''); |
|
| 714 | + return false; |
|
| 715 | + } |
|
| 716 | + else |
|
| 717 | + { |
|
| 718 | + $this->logging->log( "Sent result : " .$retmessage,INFO ); |
|
| 719 | + return true; |
|
| 720 | + } |
|
| 721 | + } |
|
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | public function getHostByIP($ip) |
| 725 | 725 | { |
| 726 | - $api = $this->getAPI(); |
|
| 727 | - $api->setCredentials($this->api_username, $this->api_password); |
|
| 728 | - return $api->getHostByIP($ip); |
|
| 726 | + $api = $this->getAPI(); |
|
| 727 | + $api->setCredentials($this->api_username, $this->api_password); |
|
| 728 | + return $api->getHostByIP($ip); |
|
| 729 | 729 | } |
| 730 | 730 | |
| 731 | 731 | /** Resolve display. |
| 732 | - * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
| 733 | - * @param $display string |
|
| 734 | - * @return string display |
|
| 735 | - */ |
|
| 732 | + * Changes OID(<oid>) to value if found or text "<not in trap>" |
|
| 733 | + * @param $display string |
|
| 734 | + * @return string display |
|
| 735 | + */ |
|
| 736 | 736 | protected function applyDisplay($display) |
| 737 | 737 | { |
| 738 | - $matches=array(); |
|
| 739 | - while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 738 | + $matches=array(); |
|
| 739 | + while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1) |
|
| 740 | 740 | { |
| 741 | 741 | $oid=$matches[1]; |
| 742 | 742 | $found=0; |
@@ -796,11 +796,11 @@ discard block |
||
| 796 | 796 | |
| 797 | 797 | if ($rule[$item] == '(') |
| 798 | 798 | { // grouping |
| 799 | - $item++; |
|
| 799 | + $item++; |
|
| 800 | 800 | $start=$item; |
| 801 | 801 | $parenthesis_count=0; |
| 802 | 802 | while (($item < strlen($rule)) // Not end of string AND |
| 803 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 803 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 804 | 804 | { |
| 805 | 805 | if ($rule[$item] == '"' ) |
| 806 | 806 | { // pass through string |
@@ -808,14 +808,14 @@ discard block |
||
| 808 | 808 | $item=$this->eval_getNext($rule,$item,'"'); |
| 809 | 809 | } |
| 810 | 810 | else{ |
| 811 | - if ($rule[$item] == '(') |
|
| 812 | - { |
|
| 813 | - $parenthesis_count++; |
|
| 814 | - } |
|
| 815 | - if ($rule[$item] == ')') |
|
| 816 | - { |
|
| 817 | - $parenthesis_count--; |
|
| 818 | - } |
|
| 811 | + if ($rule[$item] == '(') |
|
| 812 | + { |
|
| 813 | + $parenthesis_count++; |
|
| 814 | + } |
|
| 815 | + if ($rule[$item] == ')') |
|
| 816 | + { |
|
| 817 | + $parenthesis_count--; |
|
| 818 | + } |
|
| 819 | 819 | $item++; |
| 820 | 820 | } |
| 821 | 821 | } |
@@ -867,24 +867,24 @@ discard block |
||
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | /** Evaluation : makes token and evaluate. |
| 870 | - * Public function for expressions testing |
|
| 871 | - * accepts : < > = <= >= != (typec = 0) |
|
| 872 | - * operators : & | (typec=1) |
|
| 873 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 874 | - * comparison int vs strings will return null (error) |
|
| 875 | - * return : bool or null on error |
|
| 876 | - */ |
|
| 870 | + * Public function for expressions testing |
|
| 871 | + * accepts : < > = <= >= != (typec = 0) |
|
| 872 | + * operators : & | (typec=1) |
|
| 873 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 874 | + * comparison int vs strings will return null (error) |
|
| 875 | + * return : bool or null on error |
|
| 876 | + */ |
|
| 877 | 877 | public function evaluation($rule,&$item) |
| 878 | 878 | { |
| 879 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 879 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 880 | 880 | if ( $rule[$item] == '!') // If '!' found, negate next expression. |
| 881 | 881 | { |
| 882 | - $negate=true; |
|
| 883 | - $item++; |
|
| 882 | + $negate=true; |
|
| 883 | + $item++; |
|
| 884 | 884 | } |
| 885 | 885 | else |
| 886 | 886 | { |
| 887 | - $negate=false; |
|
| 887 | + $negate=false; |
|
| 888 | 888 | } |
| 889 | 889 | // First element : number, string or () |
| 890 | 890 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
@@ -904,24 +904,24 @@ discard block |
||
| 904 | 904 | // Third element : number, string or () |
| 905 | 905 | if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
| 906 | 906 | { |
| 907 | - $item++; |
|
| 908 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 909 | - $val2= ! $this->evaluation($rule,$item); |
|
| 910 | - $type2=2; // result is a boolean |
|
| 907 | + $item++; |
|
| 908 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 909 | + $val2= ! $this->evaluation($rule,$item); |
|
| 910 | + $type2=2; // result is a boolean |
|
| 911 | 911 | } |
| 912 | 912 | else |
| 913 | 913 | { |
| 914 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 914 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 915 | 915 | } |
| 916 | 916 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 917 | 917 | |
| 918 | 918 | if ($type1!=$type2) // cannot compare different types |
| 919 | 919 | { |
| 920 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
| 920 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
| 921 | 921 | } |
| 922 | 922 | if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
| 923 | 923 | { |
| 924 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 924 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 925 | 925 | } |
| 926 | 926 | |
| 927 | 927 | switch ($comp){ |
@@ -979,9 +979,9 @@ discard block |
||
| 979 | 979 | } |
| 980 | 980 | |
| 981 | 981 | /** Evaluation rule (uses eval_* functions recursively) |
| 982 | - * @param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
| 983 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 984 | - */ |
|
| 982 | + * @param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
| 983 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 984 | + */ |
|
| 985 | 985 | |
| 986 | 986 | protected function eval_rule($rule) |
| 987 | 987 | { |
@@ -1026,7 +1026,7 @@ discard block |
||
| 1026 | 1026 | } |
| 1027 | 1027 | if ($found==0) |
| 1028 | 1028 | { // OID not found : throw error |
| 1029 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
| 1029 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
| 1030 | 1030 | } |
| 1031 | 1031 | } |
| 1032 | 1032 | $item=0; |
@@ -1037,7 +1037,7 @@ discard block |
||
| 1037 | 1037 | } |
| 1038 | 1038 | |
| 1039 | 1039 | /** Match rules for current trap and do action |
| 1040 | - */ |
|
| 1040 | + */ |
|
| 1041 | 1041 | public function applyRules() |
| 1042 | 1042 | { |
| 1043 | 1043 | $rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']); |
@@ -1074,12 +1074,12 @@ discard block |
||
| 1074 | 1074 | { |
| 1075 | 1075 | if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false) |
| 1076 | 1076 | { |
| 1077 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1077 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1078 | 1078 | } |
| 1079 | 1079 | else |
| 1080 | 1080 | { |
| 1081 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1082 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1081 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1082 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1083 | 1083 | } |
| 1084 | 1084 | } |
| 1085 | 1085 | else |
@@ -1096,15 +1096,15 @@ discard block |
||
| 1096 | 1096 | $this->logging->log('action NOK : '.$action,INFO ); |
| 1097 | 1097 | if ($action >= 0) |
| 1098 | 1098 | { |
| 1099 | - if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 1100 | - { |
|
| 1101 | - $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1102 | - } |
|
| 1103 | - else |
|
| 1104 | - { |
|
| 1105 | - $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1106 | - $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1107 | - } |
|
| 1099 | + if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false) |
|
| 1100 | + { |
|
| 1101 | + $this->trap_action.='Error sending status : check cmd/API'; |
|
| 1102 | + } |
|
| 1103 | + else |
|
| 1104 | + { |
|
| 1105 | + $this->add_rule_match($rule['id'],$rule['num_match']+1); |
|
| 1106 | + $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name; |
|
| 1107 | + } |
|
| 1108 | 1108 | } |
| 1109 | 1109 | else |
| 1110 | 1110 | { |
@@ -1127,9 +1127,9 @@ discard block |
||
| 1127 | 1127 | } |
| 1128 | 1128 | catch (Exception $e) |
| 1129 | 1129 | { |
| 1130 | - $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 1131 | - $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 1132 | - $this->trap_data['status']='error'; |
|
| 1130 | + $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,''); |
|
| 1131 | + $this->trap_action.=' ERR : '.$e->getMessage(); |
|
| 1132 | + $this->trap_data['status']='error'; |
|
| 1133 | 1133 | } |
| 1134 | 1134 | |
| 1135 | 1135 | } |
@@ -1144,8 +1144,8 @@ discard block |
||
| 1144 | 1144 | } |
| 1145 | 1145 | |
| 1146 | 1146 | /** Add Time a action to rule |
| 1147 | - * @param string $time : time to process to insert in SQL |
|
| 1148 | - */ |
|
| 1147 | + * @param string $time : time to process to insert in SQL |
|
| 1148 | + */ |
|
| 1149 | 1149 | public function add_rule_final($time) |
| 1150 | 1150 | { |
| 1151 | 1151 | $db_conn=$this->trapsDB->db_connect_trap(); |
@@ -1162,9 +1162,9 @@ discard block |
||
| 1162 | 1162 | /*********** UTILITIES *********************/ |
| 1163 | 1163 | |
| 1164 | 1164 | /** reset service to OK after time defined in rule |
| 1165 | - * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 1166 | - * @return integer : not in use |
|
| 1167 | - **/ |
|
| 1165 | + * TODO logic is : get all service in error + all rules, see if getting all rules then select services is better |
|
| 1166 | + * @return integer : not in use |
|
| 1167 | + **/ |
|
| 1168 | 1168 | public function reset_services() |
| 1169 | 1169 | { |
| 1170 | 1170 | // Get all services not in 'ok' state |
@@ -20,77 +20,77 @@ discard block |
||
| 20 | 20 | class MibCommand extends Command |
| 21 | 21 | { |
| 22 | 22 | /** |
| 23 | - * Update mib database |
|
| 24 | - * |
|
| 25 | - * USAGE |
|
| 26 | - * |
|
| 27 | - * icingli trapdirector mib update |
|
| 28 | - * |
|
| 29 | - * OPTIONS |
|
| 30 | - * |
|
| 31 | - * --pid <file> : run in background with pid in <file> |
|
| 32 | - * |
|
| 33 | - * --verb : Set output log to verbose |
|
| 34 | - * |
|
| 35 | - * --force-check : force check of all traps & objects for change. (NOT IMPLEMENTED) |
|
| 36 | - */ |
|
| 23 | + * Update mib database |
|
| 24 | + * |
|
| 25 | + * USAGE |
|
| 26 | + * |
|
| 27 | + * icingli trapdirector mib update |
|
| 28 | + * |
|
| 29 | + * OPTIONS |
|
| 30 | + * |
|
| 31 | + * --pid <file> : run in background with pid in <file> |
|
| 32 | + * |
|
| 33 | + * --verb : Set output log to verbose |
|
| 34 | + * |
|
| 35 | + * --force-check : force check of all traps & objects for change. (NOT IMPLEMENTED) |
|
| 36 | + */ |
|
| 37 | 37 | public function updateAction() |
| 38 | 38 | { |
| 39 | - $background = $this->params->get('pid', null); |
|
| 40 | - $logLevel= $this->params->has('verb') ? 4 : 2; |
|
| 41 | - if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
| 42 | - $forceCheck=$this->params->has('force-check')?True:False; |
|
| 43 | - $pid=1; |
|
| 44 | - if ($background != null) |
|
| 45 | - { |
|
| 46 | - $file=@fopen($background,'w'); |
|
| 47 | - if ($file == false) |
|
| 48 | - { |
|
| 49 | - echo 'Error : cannot open pid file '.$background; |
|
| 50 | - return 1; |
|
| 51 | - } |
|
| 52 | - $pid = pcntl_fork(); |
|
| 53 | - if ($pid == -1) { |
|
| 54 | - echo 'Error : Cannot fork process'; |
|
| 55 | - return 1; |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
|
| 39 | + $background = $this->params->get('pid', null); |
|
| 40 | + $logLevel= $this->params->has('verb') ? 4 : 2; |
|
| 41 | + if ($this->params->has('force-check')) { echo "Not implemented"; return;} |
|
| 42 | + $forceCheck=$this->params->has('force-check')?True:False; |
|
| 43 | + $pid=1; |
|
| 44 | + if ($background != null) |
|
| 45 | + { |
|
| 46 | + $file=@fopen($background,'w'); |
|
| 47 | + if ($file == false) |
|
| 48 | + { |
|
| 49 | + echo 'Error : cannot open pid file '.$background; |
|
| 50 | + return 1; |
|
| 51 | + } |
|
| 52 | + $pid = pcntl_fork(); |
|
| 53 | + if ($pid == -1) { |
|
| 54 | + echo 'Error : Cannot fork process'; |
|
| 55 | + return 1; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName()); |
|
| 59 | 59 | require_once($module->getBaseDir() .'/bin/trap_class.php'); |
| 60 | 60 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
| 61 | 61 | $trap = new Trap($icingaweb2_etc); |
| 62 | 62 | if ($pid == 1) |
| 63 | 63 | { |
| 64 | - $trap->setLogging($logLevel,'display'); |
|
| 64 | + $trap->setLogging($logLevel,'display'); |
|
| 65 | 65 | } |
| 66 | 66 | else |
| 67 | 67 | { // use default display TODO : if default is 'display' son process will be killed at first output.... |
| 68 | - if ($pid != 0) |
|
| 69 | - { |
|
| 70 | - // father process |
|
| 71 | - fwrite($file,$pid); |
|
| 72 | - fclose($file); |
|
| 73 | - echo "OK : process $pid in bckground"; |
|
| 74 | - return 0; |
|
| 75 | - } |
|
| 76 | - else |
|
| 77 | - { // son process : close all file descriptors and go to a new session |
|
| 78 | - fclose($file); |
|
| 68 | + if ($pid != 0) |
|
| 69 | + { |
|
| 70 | + // father process |
|
| 71 | + fwrite($file,$pid); |
|
| 72 | + fclose($file); |
|
| 73 | + echo "OK : process $pid in bckground"; |
|
| 74 | + return 0; |
|
| 75 | + } |
|
| 76 | + else |
|
| 77 | + { // son process : close all file descriptors and go to a new session |
|
| 78 | + fclose($file); |
|
| 79 | 79 | // $sid = posix_setsid(); |
| 80 | - fclose(STDIN); |
|
| 81 | - fclose(STDOUT); |
|
| 82 | - fclose(STDERR); |
|
| 83 | - try |
|
| 84 | - { |
|
| 85 | - $trap->mibClass->update_mib_database(false,$forceCheck); |
|
| 86 | - } |
|
| 87 | - catch (Exception $e) |
|
| 88 | - { |
|
| 89 | - $trap->trapLog('Error in updating : ' . $e->getMessage(),2); |
|
| 90 | - } |
|
| 91 | - unlink($background); |
|
| 92 | - return 0; |
|
| 93 | - } |
|
| 80 | + fclose(STDIN); |
|
| 81 | + fclose(STDOUT); |
|
| 82 | + fclose(STDERR); |
|
| 83 | + try |
|
| 84 | + { |
|
| 85 | + $trap->mibClass->update_mib_database(false,$forceCheck); |
|
| 86 | + } |
|
| 87 | + catch (Exception $e) |
|
| 88 | + { |
|
| 89 | + $trap->trapLog('Error in updating : ' . $e->getMessage(),2); |
|
| 90 | + } |
|
| 91 | + unlink($background); |
|
| 92 | + return 0; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | } |
| 96 | 96 | |
@@ -108,20 +108,20 @@ discard block |
||
| 108 | 108 | } |
| 109 | 109 | if ($pid != 1) |
| 110 | 110 | { |
| 111 | - unlink($background); |
|
| 111 | + unlink($background); |
|
| 112 | 112 | } |
| 113 | 113 | } |
| 114 | 114 | /** |
| 115 | - * purge all mib database NOT IMPLEMENTED |
|
| 116 | - * |
|
| 117 | - * USAGE |
|
| 118 | - * |
|
| 119 | - * icingli trapdirector mib purge --confirm yes |
|
| 120 | - * |
|
| 121 | - * OPTIONS |
|
| 122 | - * |
|
| 123 | - * --confirm yes : needed to execute purge |
|
| 124 | - */ |
|
| 115 | + * purge all mib database NOT IMPLEMENTED |
|
| 116 | + * |
|
| 117 | + * USAGE |
|
| 118 | + * |
|
| 119 | + * icingli trapdirector mib purge --confirm yes |
|
| 120 | + * |
|
| 121 | + * OPTIONS |
|
| 122 | + * |
|
| 123 | + * --confirm yes : needed to execute purge |
|
| 124 | + */ |
|
| 125 | 125 | public function purgeAction() |
| 126 | 126 | { |
| 127 | 127 | $db_prefix=$this->Config()->get('config', 'database_prefix'); |