@@ -14,40 +14,40 @@ |
||
| 14 | 14 | |
| 15 | 15 | if (!array_key_exists('v',$options) || !array_key_exists('c',$options) || !array_key_exists('b',$options)|| !array_key_exists('a',$options)) |
| 16 | 16 | { |
| 17 | - printf("Need version -v, path -a, database -b (mysql,pgsql) command -c (create/update)\n"); |
|
| 18 | - exit(1); |
|
| 17 | + printf("Need version -v, path -a, database -b (mysql,pgsql) command -c (create/update)\n"); |
|
| 18 | + exit(1); |
|
| 19 | 19 | } |
| 20 | 20 | $command=$options['c']; |
| 21 | 21 | $path=$options['a']; |
| 22 | 22 | try { |
| 23 | - switch($command) |
|
| 24 | - { |
|
| 25 | - case 'create': |
|
| 26 | - $schema=($options['b']=='mysql')?'schema_v'.$options['v'].'.sql':'schema_v'.$options['v'].'.pgsql'; |
|
| 27 | - $schema=$path.'/SQL/'.$schema; |
|
| 28 | - $trap->trapsDB->create_schema($schema, 'traps_'); |
|
| 29 | - break; |
|
| 30 | - case 'update': |
|
| 31 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_',true); |
|
| 32 | - printf("Update message : %s\n",$message); |
|
| 33 | - if ($message == 'ERROR') |
|
| 34 | - { |
|
| 35 | - exit(1); |
|
| 36 | - } |
|
| 37 | - printf("Messages DONE, updating : \n"); |
|
| 38 | - $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_'); |
|
| 39 | - if ($message == 'ERROR') |
|
| 40 | - { |
|
| 41 | - exit(1); |
|
| 42 | - } |
|
| 43 | - break; |
|
| 44 | - default: |
|
| 45 | - printf("Unknown command\n"); |
|
| 46 | - exit(1); |
|
| 47 | - } |
|
| 23 | + switch($command) |
|
| 24 | + { |
|
| 25 | + case 'create': |
|
| 26 | + $schema=($options['b']=='mysql')?'schema_v'.$options['v'].'.sql':'schema_v'.$options['v'].'.pgsql'; |
|
| 27 | + $schema=$path.'/SQL/'.$schema; |
|
| 28 | + $trap->trapsDB->create_schema($schema, 'traps_'); |
|
| 29 | + break; |
|
| 30 | + case 'update': |
|
| 31 | + $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_',true); |
|
| 32 | + printf("Update message : %s\n",$message); |
|
| 33 | + if ($message == 'ERROR') |
|
| 34 | + { |
|
| 35 | + exit(1); |
|
| 36 | + } |
|
| 37 | + printf("Messages DONE, updating : \n"); |
|
| 38 | + $message=$trap->trapsDB->update_schema($path."/SQL/",$options['v'], 'traps_'); |
|
| 39 | + if ($message == 'ERROR') |
|
| 40 | + { |
|
| 41 | + exit(1); |
|
| 42 | + } |
|
| 43 | + break; |
|
| 44 | + default: |
|
| 45 | + printf("Unknown command\n"); |
|
| 46 | + exit(1); |
|
| 47 | + } |
|
| 48 | 48 | } catch (Exception $e) { |
| 49 | - printf("Caught Exception %s\n",$e->getMessage()); |
|
| 50 | - exit (1); |
|
| 49 | + printf("Caught Exception %s\n",$e->getMessage()); |
|
| 50 | + exit (1); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | exit(0); |
@@ -10,519 +10,519 @@ |
||
| 10 | 10 | class Mib |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - protected $logging; //< logging class |
|
| 14 | - protected $trapsDB; //< Database class |
|
| 13 | + protected $logging; //< logging class |
|
| 14 | + protected $trapsDB; //< Database class |
|
| 15 | 15 | |
| 16 | - public $snmptranslate; |
|
| 17 | - public $snmptranslate_dirs; |
|
| 16 | + public $snmptranslate; |
|
| 17 | + public $snmptranslate_dirs; |
|
| 18 | 18 | |
| 19 | - private $dbOidAll; //< All oid in database; |
|
| 20 | - private $dbOidIndex; //< Index of oid in dbOidAll |
|
| 21 | - private $objectsAll; //< output lines of snmptranslate list |
|
| 22 | - private $trapObjectsIndex; //< array of traps objects (as OID) |
|
| 19 | + private $dbOidAll; //< All oid in database; |
|
| 20 | + private $dbOidIndex; //< Index of oid in dbOidAll |
|
| 21 | + private $objectsAll; //< output lines of snmptranslate list |
|
| 22 | + private $trapObjectsIndex; //< array of traps objects (as OID) |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Setup Mib Class |
|
| 26 | - * @param Logging $logClass : where to log |
|
| 27 | - * @param Database $dbClass : Database |
|
| 28 | - */ |
|
| 29 | - function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 30 | - { |
|
| 31 | - $this->logging=$logClass; |
|
| 32 | - $this->trapsDB=$dbClass; |
|
| 33 | - $this->snmptranslate=$snmptrans; |
|
| 34 | - $this->snmptranslate_dirs=$snmptransdir; |
|
| 24 | + /** |
|
| 25 | + * Setup Mib Class |
|
| 26 | + * @param Logging $logClass : where to log |
|
| 27 | + * @param Database $dbClass : Database |
|
| 28 | + */ |
|
| 29 | + function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 30 | + { |
|
| 31 | + $this->logging=$logClass; |
|
| 32 | + $this->trapsDB=$dbClass; |
|
| 33 | + $this->snmptranslate=$snmptrans; |
|
| 34 | + $this->snmptranslate_dirs=$snmptransdir; |
|
| 35 | 35 | |
| 36 | - } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 41 | - * @param string $oid |
|
| 42 | - * @param string $mib |
|
| 43 | - * @param string $name |
|
| 44 | - * @param string $type |
|
| 45 | - * @param string $textConv |
|
| 46 | - * @param string $dispHint |
|
| 47 | - * @param string $syntax |
|
| 48 | - * @param string $type_enum |
|
| 49 | - * @param string $description |
|
| 50 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | - */ |
|
| 52 | - public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
|
| 53 | - { |
|
| 54 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 55 | - $description=$db_conn->quote($description); |
|
| 56 | - if (isset($this->dbOidIndex[$oid])) |
|
| 57 | - { |
|
| 58 | - if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 59 | - { // newly created. |
|
| 60 | - return 0; |
|
| 61 | - } |
|
| 62 | - if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 63 | - $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 64 | - $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 65 | - //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 66 | - //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 67 | - //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 68 | - //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 69 | - //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 70 | - ) |
|
| 71 | - { // Do update |
|
| 72 | - $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
| 73 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 74 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 75 | - ' WHERE id= :id'; |
|
| 76 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 39 | + /** |
|
| 40 | + * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 41 | + * @param string $oid |
|
| 42 | + * @param string $mib |
|
| 43 | + * @param string $name |
|
| 44 | + * @param string $type |
|
| 45 | + * @param string $textConv |
|
| 46 | + * @param string $dispHint |
|
| 47 | + * @param string $syntax |
|
| 48 | + * @param string $type_enum |
|
| 49 | + * @param string $description |
|
| 50 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 51 | + */ |
|
| 52 | + public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL) |
|
| 53 | + { |
|
| 54 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 55 | + $description=$db_conn->quote($description); |
|
| 56 | + if (isset($this->dbOidIndex[$oid])) |
|
| 57 | + { |
|
| 58 | + if ($this->dbOidIndex[$oid]['key'] == -1) |
|
| 59 | + { // newly created. |
|
| 60 | + return 0; |
|
| 61 | + } |
|
| 62 | + if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] || |
|
| 63 | + $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] || |
|
| 64 | + $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //|| |
|
| 65 | + //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //|| |
|
| 66 | + //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] || |
|
| 67 | + //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] || |
|
| 68 | + //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] || |
|
| 69 | + //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description'] |
|
| 70 | + ) |
|
| 71 | + { // Do update |
|
| 72 | + $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
| 73 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 74 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 75 | + ' WHERE id= :id'; |
|
| 76 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 77 | 77 | |
| 78 | - $sqlParam=array( |
|
| 79 | - ':name' => $name, |
|
| 80 | - ':type' => $type, |
|
| 81 | - ':mib' => $mib, |
|
| 82 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 83 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 84 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 85 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 86 | - ':description' => ($description==null)?'null':$description, |
|
| 87 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 88 | - ); |
|
| 78 | + $sqlParam=array( |
|
| 79 | + ':name' => $name, |
|
| 80 | + ':type' => $type, |
|
| 81 | + ':mib' => $mib, |
|
| 82 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 83 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 84 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 85 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 86 | + ':description' => ($description==null)?'null':$description, |
|
| 87 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']] |
|
| 88 | + ); |
|
| 89 | 89 | |
| 90 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 91 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 92 | - } |
|
| 93 | - $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 94 | - return 1; |
|
| 95 | - } |
|
| 96 | - else |
|
| 97 | - { |
|
| 98 | - $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 99 | - return 0; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - // create new OID. |
|
| 90 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 91 | + $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 92 | + } |
|
| 93 | + $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 94 | + return 1; |
|
| 95 | + } |
|
| 96 | + else |
|
| 97 | + { |
|
| 98 | + $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG ); |
|
| 99 | + return 0; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + // create new OID. |
|
| 103 | 103 | |
| 104 | - // Insert data |
|
| 104 | + // Insert data |
|
| 105 | 105 | |
| 106 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
| 107 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 108 | - ', syntax, type_enum , description ) ' . |
|
| 109 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 110 | - ', :syntax, :type_enum, :description )'; |
|
| 106 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
| 107 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 108 | + ', syntax, type_enum , description ) ' . |
|
| 109 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 110 | + ', :syntax, :type_enum, :description )'; |
|
| 111 | 111 | |
| 112 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 112 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 113 | 113 | |
| 114 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 114 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 115 | 115 | |
| 116 | - $sqlParam=array( |
|
| 117 | - ':oid' => $oid, |
|
| 118 | - ':name' => $name, |
|
| 119 | - ':type' => $type, |
|
| 120 | - ':mib' => $mib, |
|
| 121 | - ':tc' => ($textConv==null)?'null':$textConv , |
|
| 122 | - ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 123 | - ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 124 | - ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 125 | - ':description' => ($description==null)?'null':$description |
|
| 126 | - ); |
|
| 116 | + $sqlParam=array( |
|
| 117 | + ':oid' => $oid, |
|
| 118 | + ':name' => $name, |
|
| 119 | + ':type' => $type, |
|
| 120 | + ':mib' => $mib, |
|
| 121 | + ':tc' => ($textConv==null)?'null':$textConv , |
|
| 122 | + ':display_hint' => ($dispHint==null)?'null':$dispHint , |
|
| 123 | + ':syntax' => ($syntax==null)?'null':$syntax, |
|
| 124 | + ':type_enum' => ($type_enum==null)?'null':$type_enum, |
|
| 125 | + ':description' => ($description==null)?'null':$description |
|
| 126 | + ); |
|
| 127 | 127 | |
| 128 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 129 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 130 | - } |
|
| 128 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 129 | + $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - switch ($this->trapsDB->trapDBType) |
|
| 133 | - { |
|
| 134 | - case 'pgsql': |
|
| 135 | - // Get last id to insert oid/values in secondary table |
|
| 136 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 137 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 138 | - } |
|
| 139 | - if (! isset($inserted_id_ret['id'])) { |
|
| 140 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 141 | - } |
|
| 142 | - $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 143 | - break; |
|
| 144 | - case 'mysql': |
|
| 145 | - // Get last id to insert oid/values in secondary table |
|
| 146 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 147 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 148 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 149 | - } |
|
| 132 | + switch ($this->trapsDB->trapDBType) |
|
| 133 | + { |
|
| 134 | + case 'pgsql': |
|
| 135 | + // Get last id to insert oid/values in secondary table |
|
| 136 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 137 | + $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 138 | + } |
|
| 139 | + if (! isset($inserted_id_ret['id'])) { |
|
| 140 | + $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 141 | + } |
|
| 142 | + $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id']; |
|
| 143 | + break; |
|
| 144 | + case 'mysql': |
|
| 145 | + // Get last id to insert oid/values in secondary table |
|
| 146 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 147 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 148 | + $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 152 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 153 | - $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 154 | - break; |
|
| 155 | - default: |
|
| 156 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 157 | - } |
|
| 151 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 152 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 153 | + $this->dbOidIndex[$oid]['id']=$inserted_id; |
|
| 154 | + break; |
|
| 155 | + default: |
|
| 156 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - // Set as newly created. |
|
| 160 | - $this->dbOidIndex[$oid]['key']=-1; |
|
| 161 | - return 2; |
|
| 162 | - } |
|
| 159 | + // Set as newly created. |
|
| 160 | + $this->dbOidIndex[$oid]['key']=-1; |
|
| 161 | + return 2; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * create or update (with check_existing = true) objects of trap |
|
| 166 | - * @param string $trapOID : trap oid |
|
| 167 | - * @param string $trapmib : mib of trap |
|
| 168 | - * @param array $objects : array of objects name (without MIB) |
|
| 169 | - * @param bool $check_existing : check instead of create |
|
| 170 | - */ |
|
| 171 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 172 | - { |
|
| 173 | - $dbObjects=null; // cache of objects for trap in db |
|
| 174 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 164 | + /** |
|
| 165 | + * create or update (with check_existing = true) objects of trap |
|
| 166 | + * @param string $trapOID : trap oid |
|
| 167 | + * @param string $trapmib : mib of trap |
|
| 168 | + * @param array $objects : array of objects name (without MIB) |
|
| 169 | + * @param bool $check_existing : check instead of create |
|
| 170 | + */ |
|
| 171 | + public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 172 | + { |
|
| 173 | + $dbObjects=null; // cache of objects for trap in db |
|
| 174 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 175 | 175 | |
| 176 | - // Get id of trapmib. |
|
| 176 | + // Get id of trapmib. |
|
| 177 | 177 | |
| 178 | - $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 179 | - if ($check_existing === true) |
|
| 180 | - { |
|
| 181 | - // Get all objects |
|
| 182 | - $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 183 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 184 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 185 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 186 | - } |
|
| 187 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 178 | + $trapId = $this->dbOidIndex[$trapOID]['id']; |
|
| 179 | + if ($check_existing === true) |
|
| 180 | + { |
|
| 181 | + // Get all objects |
|
| 182 | + $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 183 | + $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 184 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 185 | + $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 186 | + } |
|
| 187 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 188 | 188 | |
| 189 | - foreach ($dbObjectsRaw as $val) |
|
| 190 | - { |
|
| 191 | - $dbObjects[$val['object_id']]=1; |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - foreach ($objects as $object) |
|
| 195 | - { |
|
| 196 | - $match=$snmptrans=array(); |
|
| 197 | - $retVal=0; |
|
| 198 | - $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 199 | - $tmpdesc='';$indesc=false; |
|
| 189 | + foreach ($dbObjectsRaw as $val) |
|
| 190 | + { |
|
| 191 | + $dbObjects[$val['object_id']]=1; |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + foreach ($objects as $object) |
|
| 195 | + { |
|
| 196 | + $match=$snmptrans=array(); |
|
| 197 | + $retVal=0; |
|
| 198 | + $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL; |
|
| 199 | + $tmpdesc='';$indesc=false; |
|
| 200 | 200 | |
| 201 | - $objMib=$trapmib; |
|
| 202 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 203 | - ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 204 | - if ($retVal!=0) |
|
| 205 | - { |
|
| 206 | - // Maybe not trap mib, search with IR |
|
| 207 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 208 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 209 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 210 | - { // Not found -> continue with warning |
|
| 211 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 212 | - continue; |
|
| 213 | - } |
|
| 214 | - $objMib=$match[1]; |
|
| 201 | + $objMib=$trapmib; |
|
| 202 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 203 | + ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 204 | + if ($retVal!=0) |
|
| 205 | + { |
|
| 206 | + // Maybe not trap mib, search with IR |
|
| 207 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 208 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 209 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 210 | + { // Not found -> continue with warning |
|
| 211 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 212 | + continue; |
|
| 213 | + } |
|
| 214 | + $objMib=$match[1]; |
|
| 215 | 215 | |
| 216 | - // Do the snmptranslate again. |
|
| 217 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 218 | - ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 219 | - if ($retVal!=0) { |
|
| 220 | - $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 221 | - } |
|
| 216 | + // Do the snmptranslate again. |
|
| 217 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 218 | + ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal); |
|
| 219 | + if ($retVal!=0) { |
|
| 220 | + $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,''); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - } |
|
| 224 | - foreach ($snmptrans as $line) |
|
| 225 | - { |
|
| 226 | - if ($indesc===true) |
|
| 227 | - { |
|
| 228 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 229 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 230 | - { |
|
| 231 | - $objDesc = $tmpdesc . $match[1]; |
|
| 232 | - $indesc=false; |
|
| 233 | - } |
|
| 234 | - $tmpdesc.=$line; |
|
| 235 | - continue; |
|
| 236 | - } |
|
| 237 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 238 | - { |
|
| 239 | - $objOid=$line; |
|
| 240 | - continue; |
|
| 241 | - } |
|
| 242 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 243 | - { |
|
| 244 | - $objSyntax=$match[1]; |
|
| 245 | - $objEnum=$match[2]; |
|
| 246 | - continue; |
|
| 247 | - } |
|
| 248 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 249 | - { |
|
| 250 | - $objSyntax=$match[1]; |
|
| 251 | - continue; |
|
| 252 | - } |
|
| 253 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 254 | - { |
|
| 255 | - $objDispHint=$match[1]; |
|
| 256 | - continue; |
|
| 257 | - } |
|
| 258 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 259 | - { |
|
| 260 | - $objDesc=$match[1]; |
|
| 261 | - continue; |
|
| 262 | - } |
|
| 263 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 264 | - { |
|
| 265 | - $tmpdesc=$match[1]; |
|
| 266 | - $indesc=true; |
|
| 267 | - continue; |
|
| 268 | - } |
|
| 269 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 270 | - { |
|
| 271 | - $objTc=$match[1]; |
|
| 272 | - continue; |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 276 | - //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 277 | - // Update |
|
| 278 | - $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 223 | + } |
|
| 224 | + foreach ($snmptrans as $line) |
|
| 225 | + { |
|
| 226 | + if ($indesc===true) |
|
| 227 | + { |
|
| 228 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 229 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
| 230 | + { |
|
| 231 | + $objDesc = $tmpdesc . $match[1]; |
|
| 232 | + $indesc=false; |
|
| 233 | + } |
|
| 234 | + $tmpdesc.=$line; |
|
| 235 | + continue; |
|
| 236 | + } |
|
| 237 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 238 | + { |
|
| 239 | + $objOid=$line; |
|
| 240 | + continue; |
|
| 241 | + } |
|
| 242 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 243 | + { |
|
| 244 | + $objSyntax=$match[1]; |
|
| 245 | + $objEnum=$match[2]; |
|
| 246 | + continue; |
|
| 247 | + } |
|
| 248 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 249 | + { |
|
| 250 | + $objSyntax=$match[1]; |
|
| 251 | + continue; |
|
| 252 | + } |
|
| 253 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 254 | + { |
|
| 255 | + $objDispHint=$match[1]; |
|
| 256 | + continue; |
|
| 257 | + } |
|
| 258 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 259 | + { |
|
| 260 | + $objDesc=$match[1]; |
|
| 261 | + continue; |
|
| 262 | + } |
|
| 263 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 264 | + { |
|
| 265 | + $tmpdesc=$match[1]; |
|
| 266 | + $indesc=true; |
|
| 267 | + continue; |
|
| 268 | + } |
|
| 269 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 270 | + { |
|
| 271 | + $objTc=$match[1]; |
|
| 272 | + continue; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG ); |
|
| 276 | + //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n"; |
|
| 277 | + // Update |
|
| 278 | + $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc); |
|
| 279 | 279 | |
| 280 | - if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 281 | - { // if link exists, continue |
|
| 282 | - $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 283 | - continue; |
|
| 284 | - } |
|
| 285 | - if ($check_existing === true) |
|
| 286 | - { |
|
| 287 | - // TODO : check link trap - objects exists, mark them. |
|
| 288 | - } |
|
| 289 | - // Associate in object table |
|
| 290 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 291 | - 'values (:trap_id, :object_id)'; |
|
| 292 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 293 | - $sqlParam=array( |
|
| 294 | - ':trap_id' => $trapId, |
|
| 295 | - ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 296 | - ); |
|
| 280 | + if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']])) |
|
| 281 | + { // if link exists, continue |
|
| 282 | + $dbObjects[$this->dbOidIndex[$objOid]['id']]=2; |
|
| 283 | + continue; |
|
| 284 | + } |
|
| 285 | + if ($check_existing === true) |
|
| 286 | + { |
|
| 287 | + // TODO : check link trap - objects exists, mark them. |
|
| 288 | + } |
|
| 289 | + // Associate in object table |
|
| 290 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 291 | + 'values (:trap_id, :object_id)'; |
|
| 292 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 293 | + $sqlParam=array( |
|
| 294 | + ':trap_id' => $trapId, |
|
| 295 | + ':object_id' => $this->dbOidIndex[$objOid]['id'], |
|
| 296 | + ); |
|
| 297 | 297 | |
| 298 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 299 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - if ($check_existing === true) |
|
| 303 | - { |
|
| 304 | - // TODO : remove link trap - objects that wasn't marked. |
|
| 305 | - } |
|
| 298 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 299 | + $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,''); |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + if ($check_existing === true) |
|
| 303 | + { |
|
| 304 | + // TODO : remove link trap - objects that wasn't marked. |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | - } |
|
| 307 | + } |
|
| 308 | 308 | |
| 309 | - /** |
|
| 310 | - * Cache mib in database |
|
| 311 | - * @param boolean $display_progress : Display progress on standard output |
|
| 312 | - * @param boolean $check_change : Force check of trap params & objects |
|
| 313 | - * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 314 | - * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 315 | - */ |
|
| 316 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 317 | - { |
|
| 318 | - // Timing |
|
| 319 | - $timeTaken = microtime(true); |
|
| 320 | - $retVal=0; |
|
| 321 | - // Get all mib objects from all mibs |
|
| 322 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 323 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 324 | - unset($this->objectsAll); |
|
| 325 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 326 | - if ($retVal!=0) |
|
| 327 | - { |
|
| 328 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 329 | - } |
|
| 309 | + /** |
|
| 310 | + * Cache mib in database |
|
| 311 | + * @param boolean $display_progress : Display progress on standard output |
|
| 312 | + * @param boolean $check_change : Force check of trap params & objects |
|
| 313 | + * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 314 | + * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 315 | + */ |
|
| 316 | + public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 317 | + { |
|
| 318 | + // Timing |
|
| 319 | + $timeTaken = microtime(true); |
|
| 320 | + $retVal=0; |
|
| 321 | + // Get all mib objects from all mibs |
|
| 322 | + $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null'; |
|
| 323 | + $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 324 | + unset($this->objectsAll); |
|
| 325 | + exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 326 | + if ($retVal!=0) |
|
| 327 | + { |
|
| 328 | + $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - // Get all mibs from databse to have a memory index |
|
| 331 | + // Get all mibs from databse to have a memory index |
|
| 332 | 332 | |
| 333 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 333 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 334 | 334 | |
| 335 | - $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 336 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 337 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 338 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 339 | - } |
|
| 340 | - $this->dbOidAll=$ret_code->fetchAll(); |
|
| 341 | - $this->dbOidIndex=array(); |
|
| 342 | - // Create the index for db; |
|
| 343 | - foreach($this->dbOidAll as $key=>$val) |
|
| 344 | - { |
|
| 345 | - $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 346 | - $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 347 | - } |
|
| 335 | + $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 336 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 337 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 338 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 339 | + } |
|
| 340 | + $this->dbOidAll=$ret_code->fetchAll(); |
|
| 341 | + $this->dbOidIndex=array(); |
|
| 342 | + // Create the index for db; |
|
| 343 | + foreach($this->dbOidAll as $key=>$val) |
|
| 344 | + { |
|
| 345 | + $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 346 | + $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - // Count elements to show progress |
|
| 350 | - $numElements=count($this->objectsAll); |
|
| 351 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 349 | + // Count elements to show progress |
|
| 350 | + $numElements=count($this->objectsAll); |
|
| 351 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 352 | 352 | |
| 353 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 354 | - $num_step=0; |
|
| 355 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 353 | + $step=$basestep=$numElements/10; // output display of % done |
|
| 354 | + $num_step=0; |
|
| 355 | + $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 356 | 356 | |
| 357 | - // Create index for trap objects |
|
| 358 | - $this->trapObjectsIndex=array(); |
|
| 357 | + // Create index for trap objects |
|
| 358 | + $this->trapObjectsIndex=array(); |
|
| 359 | 359 | |
| 360 | - // detailed timing (time_* vars) |
|
| 361 | - $time_parse1=$time_check1=$time_check2=$time_check3=$time_update=$time_objects=0; |
|
| 362 | - $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
|
| 363 | - $time_num_traps=0; |
|
| 360 | + // detailed timing (time_* vars) |
|
| 361 | + $time_parse1=$time_check1=$time_check2=$time_check3=$time_update=$time_objects=0; |
|
| 362 | + $time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0; |
|
| 363 | + $time_num_traps=0; |
|
| 364 | 364 | |
| 365 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 366 | - { |
|
| 367 | - $time_1= microtime(true); |
|
| 368 | - if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
|
| 369 | - { // echo a . every 2 sec |
|
| 370 | - echo '.'; |
|
| 371 | - $timeFiveSec = microtime(true); |
|
| 372 | - } |
|
| 373 | - if ($curElement>$step) |
|
| 374 | - { // display progress |
|
| 375 | - $num_step++; |
|
| 376 | - $step+=$basestep; |
|
| 377 | - if ($display_progress) |
|
| 378 | - { |
|
| 379 | - echo "\n" . ($num_step*10). '% : '; |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - // Get oid or pass if not found |
|
| 383 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 384 | - { |
|
| 385 | - $time_parse1 += microtime(true) - $time_1; |
|
| 386 | - $time_parse1N ++; |
|
| 387 | - continue; |
|
| 388 | - } |
|
| 389 | - $oid=$this->objectsAll[$curElement]; |
|
| 365 | + for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 366 | + { |
|
| 367 | + $time_1= microtime(true); |
|
| 368 | + if ((microtime(true)-$timeFiveSec) > 2 && $display_progress) |
|
| 369 | + { // echo a . every 2 sec |
|
| 370 | + echo '.'; |
|
| 371 | + $timeFiveSec = microtime(true); |
|
| 372 | + } |
|
| 373 | + if ($curElement>$step) |
|
| 374 | + { // display progress |
|
| 375 | + $num_step++; |
|
| 376 | + $step+=$basestep; |
|
| 377 | + if ($display_progress) |
|
| 378 | + { |
|
| 379 | + echo "\n" . ($num_step*10). '% : '; |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + // Get oid or pass if not found |
|
| 383 | + if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 384 | + { |
|
| 385 | + $time_parse1 += microtime(true) - $time_1; |
|
| 386 | + $time_parse1N ++; |
|
| 387 | + continue; |
|
| 388 | + } |
|
| 389 | + $oid=$this->objectsAll[$curElement]; |
|
| 390 | 390 | |
| 391 | - // get next line |
|
| 392 | - $curElement++; |
|
| 393 | - $match=$snmptrans=array(); |
|
| 394 | - if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 395 | - $this->objectsAll[$curElement],$match)) |
|
| 396 | - { |
|
| 397 | - $time_check1 += microtime(true) - $time_1; |
|
| 398 | - $time_check1N++; |
|
| 399 | - continue; |
|
| 400 | - } |
|
| 391 | + // get next line |
|
| 392 | + $curElement++; |
|
| 393 | + $match=$snmptrans=array(); |
|
| 394 | + if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 395 | + $this->objectsAll[$curElement],$match)) |
|
| 396 | + { |
|
| 397 | + $time_check1 += microtime(true) - $time_1; |
|
| 398 | + $time_check1N++; |
|
| 399 | + continue; |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - $name=$match[1]; // Name |
|
| 403 | - $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 402 | + $name=$match[1]; // Name |
|
| 403 | + $type=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 404 | 404 | |
| 405 | - if ($type==0) // object type=0 : check if v1 trap |
|
| 406 | - { |
|
| 407 | - // Check if next is suboid -> in that case is cannot be a trap |
|
| 408 | - if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
|
| 409 | - { |
|
| 410 | - $time_check2 += microtime(true) - $time_1; |
|
| 411 | - $time_check2N++; |
|
| 412 | - continue; |
|
| 413 | - } |
|
| 414 | - unset($snmptrans); |
|
| 415 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 416 | - ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 417 | - if ($retVal!=0) |
|
| 418 | - { |
|
| 419 | - $time_check2 += microtime(true) - $time_1; |
|
| 420 | - $time_check2N++; |
|
| 421 | - continue; |
|
| 422 | - } |
|
| 423 | - //echo "\n v1 trap found : $oid \n"; |
|
| 424 | - // Force as trap. |
|
| 425 | - $type=21; |
|
| 426 | - } |
|
| 427 | - if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 428 | - { |
|
| 429 | - $time_check3 += microtime(true) - $time_1; |
|
| 430 | - $time_check3N++; |
|
| 431 | - continue; |
|
| 432 | - } |
|
| 405 | + if ($type==0) // object type=0 : check if v1 trap |
|
| 406 | + { |
|
| 407 | + // Check if next is suboid -> in that case is cannot be a trap |
|
| 408 | + if (preg_match("/^$oid/",$this->objectsAll[$curElement+1])) |
|
| 409 | + { |
|
| 410 | + $time_check2 += microtime(true) - $time_1; |
|
| 411 | + $time_check2N++; |
|
| 412 | + continue; |
|
| 413 | + } |
|
| 414 | + unset($snmptrans); |
|
| 415 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 416 | + ' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 417 | + if ($retVal!=0) |
|
| 418 | + { |
|
| 419 | + $time_check2 += microtime(true) - $time_1; |
|
| 420 | + $time_check2N++; |
|
| 421 | + continue; |
|
| 422 | + } |
|
| 423 | + //echo "\n v1 trap found : $oid \n"; |
|
| 424 | + // Force as trap. |
|
| 425 | + $type=21; |
|
| 426 | + } |
|
| 427 | + if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue |
|
| 428 | + { |
|
| 429 | + $time_check3 += microtime(true) - $time_1; |
|
| 430 | + $time_check3N++; |
|
| 431 | + continue; |
|
| 432 | + } |
|
| 433 | 433 | |
| 434 | - $time_num_traps++; |
|
| 434 | + $time_num_traps++; |
|
| 435 | 435 | |
| 436 | - $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
|
| 437 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 436 | + $this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO ); |
|
| 437 | + if ($display_progress) echo '#'; // echo a # when trap found |
|
| 438 | 438 | |
| 439 | - // get trap objects & source MIB |
|
| 440 | - unset($snmptrans); |
|
| 441 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 442 | - ' -Td '.$oid,$snmptrans,$retVal); |
|
| 443 | - if ($retVal!=0) |
|
| 444 | - { |
|
| 445 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 446 | - } |
|
| 439 | + // get trap objects & source MIB |
|
| 440 | + unset($snmptrans); |
|
| 441 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs. |
|
| 442 | + ' -Td '.$oid,$snmptrans,$retVal); |
|
| 443 | + if ($retVal!=0) |
|
| 444 | + { |
|
| 445 | + $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 446 | + } |
|
| 447 | 447 | |
| 448 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 449 | - { |
|
| 450 | - $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 451 | - } |
|
| 452 | - $trapMib=$match[1]; |
|
| 448 | + if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 449 | + { |
|
| 450 | + $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,''); |
|
| 451 | + } |
|
| 452 | + $trapMib=$match[1]; |
|
| 453 | 453 | |
| 454 | - $numLine=1;$trapDesc=''; |
|
| 455 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 456 | - if (isset($snmptrans[$numLine])) |
|
| 457 | - { |
|
| 458 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 454 | + $numLine=1;$trapDesc=''; |
|
| 455 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 456 | + if (isset($snmptrans[$numLine])) |
|
| 457 | + { |
|
| 458 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 459 | 459 | |
| 460 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 461 | - { |
|
| 462 | - $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 463 | - $numLine++; |
|
| 464 | - } |
|
| 465 | - if (isset($snmptrans[$numLine])) { |
|
| 466 | - $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 467 | - $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 468 | - } |
|
| 460 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 461 | + { |
|
| 462 | + $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 463 | + $numLine++; |
|
| 464 | + } |
|
| 465 | + if (isset($snmptrans[$numLine])) { |
|
| 466 | + $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 467 | + $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc); |
|
| 468 | + } |
|
| 469 | 469 | |
| 470 | - } |
|
| 471 | - $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
|
| 472 | - $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
|
| 470 | + } |
|
| 471 | + $update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc); |
|
| 472 | + $time_update += microtime(true) - $time_1; $time_1= microtime(true); |
|
| 473 | 473 | |
| 474 | - if (($update==0) && ($check_change===false)) |
|
| 475 | - { // Trapd didn't change & force check disabled |
|
| 476 | - $time_objects += microtime(true) - $time_1; |
|
| 477 | - if ($display_progress) echo "C"; |
|
| 478 | - continue; |
|
| 479 | - } |
|
| 474 | + if (($update==0) && ($check_change===false)) |
|
| 475 | + { // Trapd didn't change & force check disabled |
|
| 476 | + $time_objects += microtime(true) - $time_1; |
|
| 477 | + if ($display_progress) echo "C"; |
|
| 478 | + continue; |
|
| 479 | + } |
|
| 480 | 480 | |
| 481 | - $synt=null; |
|
| 482 | - foreach ($snmptrans as $line) |
|
| 483 | - { |
|
| 484 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 485 | - { |
|
| 486 | - $synt=$match[1]; |
|
| 487 | - } |
|
| 488 | - } |
|
| 489 | - if ($synt == null) |
|
| 490 | - { |
|
| 491 | - //echo "No objects for $trapOID\n"; |
|
| 492 | - $time_objects += microtime(true) - $time_1; |
|
| 493 | - continue; |
|
| 494 | - } |
|
| 495 | - //echo "$synt \n"; |
|
| 496 | - $trapObjects=array(); |
|
| 497 | - while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 498 | - { |
|
| 499 | - array_push($trapObjects,$match[1]); |
|
| 500 | - $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 501 | - } |
|
| 481 | + $synt=null; |
|
| 482 | + foreach ($snmptrans as $line) |
|
| 483 | + { |
|
| 484 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 485 | + { |
|
| 486 | + $synt=$match[1]; |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | + if ($synt == null) |
|
| 490 | + { |
|
| 491 | + //echo "No objects for $trapOID\n"; |
|
| 492 | + $time_objects += microtime(true) - $time_1; |
|
| 493 | + continue; |
|
| 494 | + } |
|
| 495 | + //echo "$synt \n"; |
|
| 496 | + $trapObjects=array(); |
|
| 497 | + while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 498 | + { |
|
| 499 | + array_push($trapObjects,$match[1]); |
|
| 500 | + $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 501 | + } |
|
| 502 | 502 | |
| 503 | - $this->trap_objects($oid, $trapMib, $trapObjects, false); |
|
| 503 | + $this->trap_objects($oid, $trapMib, $trapObjects, false); |
|
| 504 | 504 | |
| 505 | - $time_objects += microtime(true) - $time_1; |
|
| 506 | - $time_objectsN++; |
|
| 507 | - } |
|
| 505 | + $time_objects += microtime(true) - $time_1; |
|
| 506 | + $time_objectsN++; |
|
| 507 | + } |
|
| 508 | 508 | |
| 509 | - if ($display_progress) |
|
| 510 | - { |
|
| 511 | - echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 512 | - echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 513 | - echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 514 | - echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 515 | - echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 516 | - } |
|
| 509 | + if ($display_progress) |
|
| 510 | + { |
|
| 511 | + echo "\nNumber of processed traps : $time_num_traps \n"; |
|
| 512 | + echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N) . " occurences\n"; |
|
| 513 | + echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n"; |
|
| 514 | + echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , "; |
|
| 515 | + echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n"; |
|
| 516 | + } |
|
| 517 | 517 | |
| 518 | - // Timing ends |
|
| 519 | - $timeTaken=microtime(true) - $timeTaken; |
|
| 520 | - if ($display_progress) |
|
| 521 | - { |
|
| 522 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 523 | - } |
|
| 518 | + // Timing ends |
|
| 519 | + $timeTaken=microtime(true) - $timeTaken; |
|
| 520 | + if ($display_progress) |
|
| 521 | + { |
|
| 522 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 523 | + } |
|
| 524 | 524 | |
| 525 | - } |
|
| 525 | + } |
|
| 526 | 526 | |
| 527 | 527 | |
| 528 | 528 | } |
| 529 | 529 | \ No newline at end of file |
@@ -11,329 +11,329 @@ |
||
| 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 'ERROR'; |
|
| 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 'ERROR'; |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 298 | - } |
|
| 299 | - } |
|
| 297 | + $this->logging->log('Schema updated to version : '.$cur_version ,INFO); |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * Get database message for update to $target_version |
|
| 303 | - * @param string $prefix file prefix of sql update File |
|
| 304 | - * @param int $cur_version current db version number |
|
| 305 | - * @param int $target_version target db version number |
|
| 306 | - * @return string : return messages or 'ERROR'. |
|
| 307 | - */ |
|
| 308 | - private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 309 | - { |
|
| 301 | + /** |
|
| 302 | + * Get database message for update to $target_version |
|
| 303 | + * @param string $prefix file prefix of sql update File |
|
| 304 | + * @param int $cur_version current db version number |
|
| 305 | + * @param int $target_version target db version number |
|
| 306 | + * @return string : return messages or 'ERROR'. |
|
| 307 | + */ |
|
| 308 | + private function update_schema_message($prefix,$cur_version,$target_version) |
|
| 309 | + { |
|
| 310 | 310 | |
| 311 | - $message=''; |
|
| 312 | - $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 313 | - while($cur_version<$target_version) |
|
| 314 | - { |
|
| 315 | - $cur_version++; |
|
| 316 | - $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 317 | - $input_stream=fopen($updateFile, 'r'); |
|
| 318 | - if ($input_stream=== false) |
|
| 319 | - { |
|
| 320 | - $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 321 | - return 'ERROR'; |
|
| 322 | - } |
|
| 323 | - do |
|
| 324 | - { |
|
| 325 | - $line=fgets($input_stream); |
|
| 326 | - } |
|
| 327 | - while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 328 | - fclose($input_stream); |
|
| 329 | - if ($line === false) |
|
| 330 | - { |
|
| 331 | - $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 332 | - return ''; |
|
| 333 | - } |
|
| 334 | - $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 335 | - } |
|
| 336 | - return $message; |
|
| 337 | - } |
|
| 311 | + $message=''; |
|
| 312 | + $this->logging->log('getting message for upgrade',DEBUG ); |
|
| 313 | + while($cur_version<$target_version) |
|
| 314 | + { |
|
| 315 | + $cur_version++; |
|
| 316 | + $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; |
|
| 317 | + $input_stream=fopen($updateFile, 'r'); |
|
| 318 | + if ($input_stream=== false) |
|
| 319 | + { |
|
| 320 | + $this->logging->log("Error reading update file ". $updateFile,2,''); |
|
| 321 | + return 'ERROR'; |
|
| 322 | + } |
|
| 323 | + do |
|
| 324 | + { |
|
| 325 | + $line=fgets($input_stream); |
|
| 326 | + } |
|
| 327 | + while ($line !== false && !preg_match('/#MESSAGE/',$line)); |
|
| 328 | + fclose($input_stream); |
|
| 329 | + if ($line === false) |
|
| 330 | + { |
|
| 331 | + $this->logging->log("No message in file ". $updateFile,2,''); |
|
| 332 | + return ''; |
|
| 333 | + } |
|
| 334 | + $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; |
|
| 335 | + } |
|
| 336 | + return $message; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | \ No newline at end of file |