@@ -10,174 +10,174 @@ discard block |
||
| 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 $snmptranslateDirs; |
|
| 16 | + public $snmptranslate; |
|
| 17 | + public $snmptranslateDirs; |
|
| 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 | - private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
|
| 24 | + private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
|
| 25 | 25 | |
| 26 | - // Timing vars for update |
|
| 27 | - private $timing=array(); |
|
| 26 | + // Timing vars for update |
|
| 27 | + private $timing=array(); |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Setup Mib Class |
|
| 31 | - * @param Logging $logClass : where to log |
|
| 32 | - * @param Database $dbClass : Database |
|
| 33 | - */ |
|
| 34 | - function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 35 | - { |
|
| 36 | - $this->logging=$logClass; |
|
| 37 | - $this->trapsDB=$dbClass; |
|
| 38 | - $this->snmptranslate=$snmptrans; |
|
| 39 | - $this->snmptranslateDirs=$snmptransdir; |
|
| 29 | + /** |
|
| 30 | + * Setup Mib Class |
|
| 31 | + * @param Logging $logClass : where to log |
|
| 32 | + * @param Database $dbClass : Database |
|
| 33 | + */ |
|
| 34 | + function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 35 | + { |
|
| 36 | + $this->logging=$logClass; |
|
| 37 | + $this->trapsDB=$dbClass; |
|
| 38 | + $this->snmptranslate=$snmptrans; |
|
| 39 | + $this->snmptranslateDirs=$snmptransdir; |
|
| 40 | 40 | |
| 41 | - } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 46 | - * and $this->oidDesc doe data |
|
| 47 | - * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 48 | - */ |
|
| 49 | - public function update_oid() |
|
| 50 | - { |
|
| 51 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 52 | - $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 53 | - if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 54 | - { |
|
| 55 | - if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 56 | - { // newly created. |
|
| 57 | - return 0; |
|
| 58 | - } |
|
| 59 | - $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 60 | - $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 61 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 62 | - $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 63 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
| 64 | - ) |
|
| 65 | - { // Do update |
|
| 66 | - $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
| 67 | - 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 68 | - ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 69 | - ' WHERE id= :id'; |
|
| 70 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 44 | + /** |
|
| 45 | + * Update or add an OID to database uses $this->dbOidIndex for mem cache |
|
| 46 | + * and $this->oidDesc doe data |
|
| 47 | + * @return number : 0=unchanged, 1 = changed, 2=created |
|
| 48 | + */ |
|
| 49 | + public function update_oid() |
|
| 50 | + { |
|
| 51 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 52 | + $this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']); |
|
| 53 | + if (isset($this->dbOidIndex[$this->oidDesc['oid']])) |
|
| 54 | + { |
|
| 55 | + if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1) |
|
| 56 | + { // newly created. |
|
| 57 | + return 0; |
|
| 58 | + } |
|
| 59 | + $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
|
| 60 | + $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
|
| 61 | + if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 62 | + $this->oidDesc['mib'] != $dbOid['mib'] || |
|
| 63 | + $this->oidDesc['type'] !=$dbOid['type'] |
|
| 64 | + ) |
|
| 65 | + { // Do update |
|
| 66 | + $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
|
| 67 | + 'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. |
|
| 68 | + ', syntax = :syntax, type_enum = :type_enum, description = :description '. |
|
| 69 | + ' WHERE id= :id'; |
|
| 70 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 71 | 71 | |
| 72 | - $sqlParam=array( |
|
| 73 | - ':name' => $this->oidDesc['name'], |
|
| 74 | - ':type' => $this->oidDesc['type'], |
|
| 75 | - ':mib' => $this->oidDesc['mib'], |
|
| 76 | - ':tc' => $this->oidDesc['textconv']??'null', |
|
| 77 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 78 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 79 | - ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 80 | - ':description' => $this->oidDesc['description']??'null', |
|
| 81 | - ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
| 82 | - ); |
|
| 72 | + $sqlParam=array( |
|
| 73 | + ':name' => $this->oidDesc['name'], |
|
| 74 | + ':type' => $this->oidDesc['type'], |
|
| 75 | + ':mib' => $this->oidDesc['mib'], |
|
| 76 | + ':tc' => $this->oidDesc['textconv']??'null', |
|
| 77 | + ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 78 | + ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 79 | + ':type_enum' => $this->oidDesc['type_enum']??'null', |
|
| 80 | + ':description' => $this->oidDesc['description']??'null', |
|
| 81 | + ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
|
| 82 | + ); |
|
| 83 | 83 | |
| 84 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 85 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 86 | - } |
|
| 87 | - $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 88 | - return 1; |
|
| 89 | - } |
|
| 90 | - else |
|
| 91 | - { |
|
| 92 | - $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 93 | - return 0; |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - // create new OID. |
|
| 84 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 85 | + $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 86 | + } |
|
| 87 | + $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 88 | + return 1; |
|
| 89 | + } |
|
| 90 | + else |
|
| 91 | + { |
|
| 92 | + $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 93 | + return 0; |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + // create new OID. |
|
| 97 | 97 | |
| 98 | - // Insert data |
|
| 98 | + // Insert data |
|
| 99 | 99 | |
| 100 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
| 101 | - '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 102 | - ', syntax, type_enum , description ) ' . |
|
| 103 | - 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 104 | - ', :syntax, :type_enum, :description )'; |
|
| 100 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
|
| 101 | + '(oid, name, type , mib, textual_convention, display_hint '. |
|
| 102 | + ', syntax, type_enum , description ) ' . |
|
| 103 | + 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
|
| 104 | + ', :syntax, :type_enum, :description )'; |
|
| 105 | 105 | |
| 106 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 106 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 107 | 107 | |
| 108 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 108 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 109 | 109 | |
| 110 | - $sqlParam=array( |
|
| 111 | - ':oid' => $this->oidDesc['oid'], |
|
| 112 | - ':name' => $this->oidDesc['name'], |
|
| 113 | - ':type' => $this->oidDesc['type'], |
|
| 114 | - ':mib' => $this->oidDesc['mib'], |
|
| 115 | - ':tc' => ($this->oidDesc['textconv']==null)?'null':$this->oidDesc['textconv'] , |
|
| 116 | - ':display_hint' => ($this->oidDesc['dispHint']==null)?'null':$this->oidDesc['dispHint'] , |
|
| 117 | - ':syntax' => ($this->oidDesc['syntax']==null)?'null':$this->oidDesc['syntax'], |
|
| 118 | - ':type_enum' => ($this->oidDesc['type_enum']==null)?'null':$this->oidDesc['type_enum'], |
|
| 119 | - ':description' => ($this->oidDesc['description']==null)?'null':$this->oidDesc['description'] |
|
| 120 | - ); |
|
| 110 | + $sqlParam=array( |
|
| 111 | + ':oid' => $this->oidDesc['oid'], |
|
| 112 | + ':name' => $this->oidDesc['name'], |
|
| 113 | + ':type' => $this->oidDesc['type'], |
|
| 114 | + ':mib' => $this->oidDesc['mib'], |
|
| 115 | + ':tc' => ($this->oidDesc['textconv']==null)?'null':$this->oidDesc['textconv'] , |
|
| 116 | + ':display_hint' => ($this->oidDesc['dispHint']==null)?'null':$this->oidDesc['dispHint'] , |
|
| 117 | + ':syntax' => ($this->oidDesc['syntax']==null)?'null':$this->oidDesc['syntax'], |
|
| 118 | + ':type_enum' => ($this->oidDesc['type_enum']==null)?'null':$this->oidDesc['type_enum'], |
|
| 119 | + ':description' => ($this->oidDesc['description']==null)?'null':$this->oidDesc['description'] |
|
| 120 | + ); |
|
| 121 | 121 | |
| 122 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 123 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 124 | - } |
|
| 122 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 123 | + $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - switch ($this->trapsDB->trapDBType) |
|
| 127 | - { |
|
| 128 | - case 'pgsql': |
|
| 129 | - // Get last id to insert oid/values in secondary table |
|
| 130 | - if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 131 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 132 | - } |
|
| 133 | - if (! isset($inserted_id_ret['id'])) { |
|
| 134 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 135 | - } |
|
| 136 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 137 | - break; |
|
| 138 | - case 'mysql': |
|
| 139 | - // Get last id to insert oid/values in secondary table |
|
| 140 | - $sql='SELECT LAST_INSERT_ID();'; |
|
| 141 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 142 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 143 | - } |
|
| 126 | + switch ($this->trapsDB->trapDBType) |
|
| 127 | + { |
|
| 128 | + case 'pgsql': |
|
| 129 | + // Get last id to insert oid/values in secondary table |
|
| 130 | + if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
|
| 131 | + $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 132 | + } |
|
| 133 | + if (! isset($inserted_id_ret['id'])) { |
|
| 134 | + $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 135 | + } |
|
| 136 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
|
| 137 | + break; |
|
| 138 | + case 'mysql': |
|
| 139 | + // Get last id to insert oid/values in secondary table |
|
| 140 | + $sql='SELECT LAST_INSERT_ID();'; |
|
| 141 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 142 | + $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 146 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 147 | - $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 148 | - break; |
|
| 149 | - default: |
|
| 150 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 151 | - } |
|
| 145 | + $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
|
| 146 | + if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 147 | + $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
|
| 148 | + break; |
|
| 149 | + default: |
|
| 150 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - // Set as newly created. |
|
| 154 | - $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 155 | - return 2; |
|
| 156 | - } |
|
| 153 | + // Set as newly created. |
|
| 154 | + $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
|
| 155 | + return 2; |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | /** |
| 159 | 159 | * get all objects for a trap. |
| 160 | 160 | * @param integer $trapId |
| 161 | 161 | * @return array : array of cached objects |
| 162 | 162 | */ |
| 163 | - private function cache_db_objects($trapId) |
|
| 164 | - { |
|
| 165 | - $dbObjects=array(); // cache of objects for trap in db |
|
| 166 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 167 | - // Get all objects |
|
| 168 | - $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 169 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 170 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 171 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 172 | - } |
|
| 173 | - $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 163 | + private function cache_db_objects($trapId) |
|
| 164 | + { |
|
| 165 | + $dbObjects=array(); // cache of objects for trap in db |
|
| 166 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 167 | + // Get all objects |
|
| 168 | + $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
|
| 169 | + $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 170 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 171 | + $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 172 | + } |
|
| 173 | + $dbObjectsRaw=$ret_code->fetchAll(); |
|
| 174 | 174 | |
| 175 | - foreach ($dbObjectsRaw as $val) |
|
| 176 | - { |
|
| 177 | - $dbObjects[$val['object_id']]=1; |
|
| 178 | - } |
|
| 179 | - return $dbObjects; |
|
| 180 | - } |
|
| 175 | + foreach ($dbObjectsRaw as $val) |
|
| 176 | + { |
|
| 177 | + $dbObjects[$val['object_id']]=1; |
|
| 178 | + } |
|
| 179 | + return $dbObjects; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | 182 | /** |
| 183 | 183 | * Get object details & mib , returns snmptranslate output |
@@ -185,459 +185,459 @@ discard block |
||
| 185 | 185 | * @param string $trapmib : mib of trap |
| 186 | 186 | * @return NULL|array : null if not found, or output of snmptranslate |
| 187 | 187 | */ |
| 188 | - private function get_object_details($object,$trapmib) |
|
| 189 | - { |
|
| 190 | - $match=$snmptrans=array(); |
|
| 191 | - $retVal=0; |
|
| 192 | - $this->oidDesc['mib']=$trapmib; |
|
| 193 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 194 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 195 | - if ($retVal!=0) |
|
| 196 | - { |
|
| 197 | - // Maybe not trap mib, search with IR |
|
| 198 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 199 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 200 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 201 | - { // Not found -> continue with warning |
|
| 202 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 203 | - return null; |
|
| 204 | - } |
|
| 205 | - $this->oidDesc['mib']=$match[1]; |
|
| 188 | + private function get_object_details($object,$trapmib) |
|
| 189 | + { |
|
| 190 | + $match=$snmptrans=array(); |
|
| 191 | + $retVal=0; |
|
| 192 | + $this->oidDesc['mib']=$trapmib; |
|
| 193 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 194 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 195 | + if ($retVal!=0) |
|
| 196 | + { |
|
| 197 | + // Maybe not trap mib, search with IR |
|
| 198 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 199 | + ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 200 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 201 | + { // Not found -> continue with warning |
|
| 202 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 203 | + return null; |
|
| 204 | + } |
|
| 205 | + $this->oidDesc['mib']=$match[1]; |
|
| 206 | 206 | |
| 207 | - // Do the snmptranslate again. |
|
| 208 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 209 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
| 210 | - if ($retVal!=0) { |
|
| 211 | - $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
| 212 | - return null; |
|
| 213 | - } |
|
| 207 | + // Do the snmptranslate again. |
|
| 208 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 209 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
| 210 | + if ($retVal!=0) { |
|
| 211 | + $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
| 212 | + return null; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - } |
|
| 216 | - return $snmptrans; |
|
| 217 | - } |
|
| 215 | + } |
|
| 216 | + return $snmptrans; |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | 219 | /** |
| 220 | 220 | * Parse snmptranslate output and set $this->oidDesc with elements |
| 221 | 221 | * @param array $snmptrans : multi line output of snmptrans |
| 222 | 222 | */ |
| 223 | - private function parse_object($snmptrans) |
|
| 224 | - { |
|
| 225 | - $tmpdesc=''; // For multiline description |
|
| 226 | - $indesc=false; // true if currently inside multiline description |
|
| 227 | - $match=array(); |
|
| 223 | + private function parse_object($snmptrans) |
|
| 224 | + { |
|
| 225 | + $tmpdesc=''; // For multiline description |
|
| 226 | + $indesc=false; // true if currently inside multiline description |
|
| 227 | + $match=array(); |
|
| 228 | 228 | |
| 229 | - foreach ($snmptrans as $line) |
|
| 230 | - { |
|
| 231 | - if ($indesc===true) |
|
| 232 | - { |
|
| 233 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 234 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 235 | - { |
|
| 236 | - $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
| 237 | - $indesc=false; |
|
| 238 | - } |
|
| 239 | - $tmpdesc.=$line; |
|
| 240 | - continue; |
|
| 241 | - } |
|
| 242 | - if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 243 | - { |
|
| 244 | - $this->oidDesc['oid']=$line; |
|
| 245 | - continue; |
|
| 246 | - } |
|
| 247 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 248 | - { |
|
| 249 | - $this->oidDesc['syntax']=$match[1]; |
|
| 250 | - $this->oidDesc['type_enum']=$match[2]; |
|
| 251 | - continue; |
|
| 252 | - } |
|
| 253 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 254 | - { |
|
| 255 | - $this->oidDesc['syntax']=$match[1]; |
|
| 256 | - continue; |
|
| 257 | - } |
|
| 258 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 259 | - { |
|
| 260 | - $this->oidDesc['dispHint']=$match[1]; |
|
| 261 | - continue; |
|
| 262 | - } |
|
| 263 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 264 | - { |
|
| 265 | - $this->oidDesc['description']=$match[1]; |
|
| 266 | - continue; |
|
| 267 | - } |
|
| 268 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 269 | - { |
|
| 270 | - $tmpdesc=$match[1]; |
|
| 271 | - $indesc=true; |
|
| 272 | - continue; |
|
| 273 | - } |
|
| 274 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 275 | - { |
|
| 276 | - $this->oidDesc['textconv']=$match[1]; |
|
| 277 | - continue; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - } |
|
| 229 | + foreach ($snmptrans as $line) |
|
| 230 | + { |
|
| 231 | + if ($indesc===true) |
|
| 232 | + { |
|
| 233 | + $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 234 | + if (preg_match('/(.*)"$/', $line,$match)) |
|
| 235 | + { |
|
| 236 | + $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
| 237 | + $indesc=false; |
|
| 238 | + } |
|
| 239 | + $tmpdesc.=$line; |
|
| 240 | + continue; |
|
| 241 | + } |
|
| 242 | + if (preg_match('/^\.[0-9\.]+$/', $line)) |
|
| 243 | + { |
|
| 244 | + $this->oidDesc['oid']=$line; |
|
| 245 | + continue; |
|
| 246 | + } |
|
| 247 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 248 | + { |
|
| 249 | + $this->oidDesc['syntax']=$match[1]; |
|
| 250 | + $this->oidDesc['type_enum']=$match[2]; |
|
| 251 | + continue; |
|
| 252 | + } |
|
| 253 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 254 | + { |
|
| 255 | + $this->oidDesc['syntax']=$match[1]; |
|
| 256 | + continue; |
|
| 257 | + } |
|
| 258 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 259 | + { |
|
| 260 | + $this->oidDesc['dispHint']=$match[1]; |
|
| 261 | + continue; |
|
| 262 | + } |
|
| 263 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 264 | + { |
|
| 265 | + $this->oidDesc['description']=$match[1]; |
|
| 266 | + continue; |
|
| 267 | + } |
|
| 268 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 269 | + { |
|
| 270 | + $tmpdesc=$match[1]; |
|
| 271 | + $indesc=true; |
|
| 272 | + continue; |
|
| 273 | + } |
|
| 274 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 275 | + { |
|
| 276 | + $this->oidDesc['textconv']=$match[1]; |
|
| 277 | + continue; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | - /** |
|
| 283 | - * create or update (with check_existing = true) objects of trap |
|
| 284 | - * @param string $trapOID : trap oid |
|
| 285 | - * @param string $trapmib : mib of trap |
|
| 286 | - * @param array $objects : array of objects name (without MIB) |
|
| 287 | - * @param bool $check_existing : check instead of create |
|
| 288 | - */ |
|
| 289 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 290 | - { |
|
| 291 | - $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 282 | + /** |
|
| 283 | + * create or update (with check_existing = true) objects of trap |
|
| 284 | + * @param string $trapOID : trap oid |
|
| 285 | + * @param string $trapmib : mib of trap |
|
| 286 | + * @param array $objects : array of objects name (without MIB) |
|
| 287 | + * @param bool $check_existing : check instead of create |
|
| 288 | + */ |
|
| 289 | + public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 290 | + { |
|
| 291 | + $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 292 | 292 | |
| 293 | - if ($check_existing === true) |
|
| 294 | - { |
|
| 295 | - $dbObjects=$this->cache_db_objects($trapId); |
|
| 296 | - } |
|
| 293 | + if ($check_existing === true) |
|
| 294 | + { |
|
| 295 | + $dbObjects=$this->cache_db_objects($trapId); |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - foreach ($objects as $object) |
|
| 299 | - { |
|
| 298 | + foreach ($objects as $object) |
|
| 299 | + { |
|
| 300 | 300 | |
| 301 | - $this->reset_oidDesc(); |
|
| 301 | + $this->reset_oidDesc(); |
|
| 302 | 302 | |
| 303 | - $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
|
| 304 | - if ($snmptrans === null) continue; // object not found |
|
| 303 | + $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
|
| 304 | + if ($snmptrans === null) continue; // object not found |
|
| 305 | 305 | |
| 306 | - $this->parse_object($snmptrans); |
|
| 306 | + $this->parse_object($snmptrans); |
|
| 307 | 307 | |
| 308 | - $this->oidDesc['name'] = $object; |
|
| 308 | + $this->oidDesc['name'] = $object; |
|
| 309 | 309 | |
| 310 | - $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
| 310 | + $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
| 311 | 311 | |
| 312 | - // Update |
|
| 313 | - $this->update_oid(); |
|
| 312 | + // Update |
|
| 313 | + $this->update_oid(); |
|
| 314 | 314 | |
| 315 | - if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
|
| 316 | - { // if link exists, continue |
|
| 317 | - $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
|
| 318 | - continue; |
|
| 319 | - } |
|
| 320 | - if ($check_existing === true) |
|
| 321 | - { |
|
| 322 | - // TODO : check link trap - objects exists, mark them. |
|
| 323 | - } |
|
| 324 | - // Associate in object table |
|
| 325 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 326 | - $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 327 | - 'values (:trap_id, :object_id)'; |
|
| 328 | - $sqlQuery=$db_conn->prepare($sql); |
|
| 329 | - $sqlParam=array( |
|
| 330 | - ':trap_id' => $trapId, |
|
| 331 | - ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
|
| 332 | - ); |
|
| 315 | + if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
|
| 316 | + { // if link exists, continue |
|
| 317 | + $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
|
| 318 | + continue; |
|
| 319 | + } |
|
| 320 | + if ($check_existing === true) |
|
| 321 | + { |
|
| 322 | + // TODO : check link trap - objects exists, mark them. |
|
| 323 | + } |
|
| 324 | + // Associate in object table |
|
| 325 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 326 | + $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
|
| 327 | + 'values (:trap_id, :object_id)'; |
|
| 328 | + $sqlQuery=$db_conn->prepare($sql); |
|
| 329 | + $sqlParam=array( |
|
| 330 | + ':trap_id' => $trapId, |
|
| 331 | + ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
|
| 332 | + ); |
|
| 333 | 333 | |
| 334 | - if ($sqlQuery->execute($sqlParam) === false) { |
|
| 335 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - if ($check_existing === true) |
|
| 339 | - { |
|
| 340 | - // TODO : remove link trap - objects that wasn't marked. |
|
| 341 | - } |
|
| 334 | + if ($sqlQuery->execute($sqlParam) === false) { |
|
| 335 | + $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + if ($check_existing === true) |
|
| 339 | + { |
|
| 340 | + // TODO : remove link trap - objects that wasn't marked. |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - } |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - private function reset_oidDesc() |
|
| 346 | - { |
|
| 347 | - $this->oidDesc['oid']=null; |
|
| 348 | - $this->oidDesc['name']=null; |
|
| 349 | - $this->oidDesc['type']=null; |
|
| 350 | - $this->oidDesc['mib']=null; |
|
| 351 | - $this->oidDesc['textconv']=null; |
|
| 352 | - $this->oidDesc['dispHint'] =null; |
|
| 353 | - $this->oidDesc['syntax']=null; |
|
| 354 | - $this->oidDesc['type_enum']=null; |
|
| 355 | - $this->oidDesc['description']=null; |
|
| 356 | - } |
|
| 345 | + private function reset_oidDesc() |
|
| 346 | + { |
|
| 347 | + $this->oidDesc['oid']=null; |
|
| 348 | + $this->oidDesc['name']=null; |
|
| 349 | + $this->oidDesc['type']=null; |
|
| 350 | + $this->oidDesc['mib']=null; |
|
| 351 | + $this->oidDesc['textconv']=null; |
|
| 352 | + $this->oidDesc['dispHint'] =null; |
|
| 353 | + $this->oidDesc['syntax']=null; |
|
| 354 | + $this->oidDesc['type_enum']=null; |
|
| 355 | + $this->oidDesc['description']=null; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Fills $this->objectsAll with all mibs from snmptranslate |
|
| 360 | - * @return integer : number of elements |
|
| 361 | - */ |
|
| 362 | - private function load_mibs_snmptranslate() |
|
| 363 | - { |
|
| 364 | - $retVal=0; |
|
| 365 | - // Get all mib objects from all mibs |
|
| 366 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 367 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 368 | - unset($this->objectsAll); |
|
| 369 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 370 | - if ($retVal!=0) |
|
| 371 | - { |
|
| 372 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 373 | - } |
|
| 374 | - // Count elements to show progress |
|
| 375 | - $numElements=count($this->objectsAll); |
|
| 376 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 377 | - return $numElements; |
|
| 378 | - } |
|
| 358 | + /** |
|
| 359 | + * Fills $this->objectsAll with all mibs from snmptranslate |
|
| 360 | + * @return integer : number of elements |
|
| 361 | + */ |
|
| 362 | + private function load_mibs_snmptranslate() |
|
| 363 | + { |
|
| 364 | + $retVal=0; |
|
| 365 | + // Get all mib objects from all mibs |
|
| 366 | + $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 367 | + $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 368 | + unset($this->objectsAll); |
|
| 369 | + exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 370 | + if ($retVal!=0) |
|
| 371 | + { |
|
| 372 | + $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 373 | + } |
|
| 374 | + // Count elements to show progress |
|
| 375 | + $numElements=count($this->objectsAll); |
|
| 376 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 377 | + return $numElements; |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
|
| 382 | - */ |
|
| 383 | - private function load_mibs_from_db() |
|
| 384 | - { |
|
| 385 | - // Get all mibs from databse to have a memory index |
|
| 380 | + /** |
|
| 381 | + * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
|
| 382 | + */ |
|
| 383 | + private function load_mibs_from_db() |
|
| 384 | + { |
|
| 385 | + // Get all mibs from databse to have a memory index |
|
| 386 | 386 | |
| 387 | - $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 387 | + $db_conn=$this->trapsDB->db_connect_trap(); |
|
| 388 | 388 | |
| 389 | - $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 390 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 391 | - if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 392 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 393 | - } |
|
| 394 | - $this->dbOidAll=$ret_code->fetchAll(); |
|
| 395 | - $this->dbOidIndex=array(); |
|
| 396 | - // Create the index for db; |
|
| 397 | - foreach($this->dbOidAll as $key=>$val) |
|
| 398 | - { |
|
| 399 | - $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 400 | - $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 401 | - } |
|
| 402 | - } |
|
| 389 | + $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
|
| 390 | + $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 391 | + if (($ret_code=$db_conn->query($sql)) === false) { |
|
| 392 | + $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 393 | + } |
|
| 394 | + $this->dbOidAll=$ret_code->fetchAll(); |
|
| 395 | + $this->dbOidIndex=array(); |
|
| 396 | + // Create the index for db; |
|
| 397 | + foreach($this->dbOidAll as $key=>$val) |
|
| 398 | + { |
|
| 399 | + $this->dbOidIndex[$val['oid']]['key']=$key; |
|
| 400 | + $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | 403 | |
| 404 | - /** |
|
| 405 | - * Reset all update timers & count to zero |
|
| 406 | - */ |
|
| 407 | - private function reset_update_timers() |
|
| 408 | - { |
|
| 409 | - $this->timing['base_parse_time']=0; |
|
| 410 | - $this->timing['base_check_time']=0; |
|
| 411 | - $this->timing['type0_check_time']=0; |
|
| 412 | - $this->timing['nottrap_time']=0; |
|
| 413 | - $this->timing['update_time']=0; |
|
| 414 | - $this->timing['objects_time']=0; |
|
| 415 | - $this->timing['base_parse_num']=0; |
|
| 416 | - $this->timing['base_check_num']=0; |
|
| 417 | - $this->timing['type0_check_num']=0; |
|
| 418 | - $this->timing['nottrap_num']=0; |
|
| 419 | - $this->timing['update_num']=0; |
|
| 420 | - $this->timing['objects_num']=0; |
|
| 421 | - $this->timing['num_traps']=0; |
|
| 422 | - } |
|
| 404 | + /** |
|
| 405 | + * Reset all update timers & count to zero |
|
| 406 | + */ |
|
| 407 | + private function reset_update_timers() |
|
| 408 | + { |
|
| 409 | + $this->timing['base_parse_time']=0; |
|
| 410 | + $this->timing['base_check_time']=0; |
|
| 411 | + $this->timing['type0_check_time']=0; |
|
| 412 | + $this->timing['nottrap_time']=0; |
|
| 413 | + $this->timing['update_time']=0; |
|
| 414 | + $this->timing['objects_time']=0; |
|
| 415 | + $this->timing['base_parse_num']=0; |
|
| 416 | + $this->timing['base_check_num']=0; |
|
| 417 | + $this->timing['type0_check_num']=0; |
|
| 418 | + $this->timing['nottrap_num']=0; |
|
| 419 | + $this->timing['update_num']=0; |
|
| 420 | + $this->timing['objects_num']=0; |
|
| 421 | + $this->timing['num_traps']=0; |
|
| 422 | + } |
|
| 423 | 423 | |
| 424 | - /** |
|
| 425 | - * Detect if $this->objectsAll[$curElement] is a trap |
|
| 426 | - * @param integer $curElement |
|
| 427 | - * @param bool $onlyTraps : set to false to get all and not only traps. |
|
| 428 | - * @return boolean : false if it's a trap , true if not |
|
| 429 | - */ |
|
| 430 | - private function detect_trap($curElement,$onlyTraps) |
|
| 431 | - { |
|
| 432 | - // Get oid or pass if not found |
|
| 433 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 434 | - { |
|
| 435 | - $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
| 436 | - $this->timing['base_parse_num'] ++; |
|
| 437 | - return true; |
|
| 438 | - } |
|
| 439 | - $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
|
| 424 | + /** |
|
| 425 | + * Detect if $this->objectsAll[$curElement] is a trap |
|
| 426 | + * @param integer $curElement |
|
| 427 | + * @param bool $onlyTraps : set to false to get all and not only traps. |
|
| 428 | + * @return boolean : false if it's a trap , true if not |
|
| 429 | + */ |
|
| 430 | + private function detect_trap($curElement,$onlyTraps) |
|
| 431 | + { |
|
| 432 | + // Get oid or pass if not found |
|
| 433 | + if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 434 | + { |
|
| 435 | + $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
| 436 | + $this->timing['base_parse_num'] ++; |
|
| 437 | + return true; |
|
| 438 | + } |
|
| 439 | + $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
|
| 440 | 440 | |
| 441 | - // get next line |
|
| 442 | - $curElement++; |
|
| 443 | - $match=$snmptrans=array(); |
|
| 444 | - if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 445 | - $this->objectsAll[$curElement],$match)) |
|
| 446 | - { |
|
| 447 | - $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 448 | - $this->timing['base_check_num']++; |
|
| 449 | - return true; |
|
| 450 | - } |
|
| 441 | + // get next line |
|
| 442 | + $curElement++; |
|
| 443 | + $match=$snmptrans=array(); |
|
| 444 | + if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
|
| 445 | + $this->objectsAll[$curElement],$match)) |
|
| 446 | + { |
|
| 447 | + $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 448 | + $this->timing['base_check_num']++; |
|
| 449 | + return true; |
|
| 450 | + } |
|
| 451 | 451 | |
| 452 | - $this->oidDesc['name']=$match[1]; // Name |
|
| 453 | - $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 452 | + $this->oidDesc['name']=$match[1]; // Name |
|
| 453 | + $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
|
| 454 | 454 | |
| 455 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 456 | - { |
|
| 457 | - // Check if next is suboid -> in that case is cannot be a trap |
|
| 458 | - if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
| 459 | - { |
|
| 460 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 461 | - $this->timing['type0_check_num']++; |
|
| 462 | - return true; |
|
| 463 | - } |
|
| 464 | - unset($snmptrans); |
|
| 465 | - $retVal=0; |
|
| 466 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 467 | - ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 468 | - if ($retVal!=0) |
|
| 469 | - { |
|
| 470 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 471 | - $this->timing['type0_check_num']++; |
|
| 472 | - return true; |
|
| 473 | - } |
|
| 474 | - //echo "\n v1 trap found : $this->oidDesc['oid'] \n"; |
|
| 475 | - // Force as trap. |
|
| 476 | - $this->oidDesc['type']=21; |
|
| 477 | - } |
|
| 478 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 479 | - { |
|
| 480 | - $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
| 481 | - $this->timing['nottrap_num']++; |
|
| 482 | - return true; |
|
| 483 | - } |
|
| 484 | - return false; |
|
| 485 | - } |
|
| 455 | + if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 456 | + { |
|
| 457 | + // Check if next is suboid -> in that case is cannot be a trap |
|
| 458 | + if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
| 459 | + { |
|
| 460 | + $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 461 | + $this->timing['type0_check_num']++; |
|
| 462 | + return true; |
|
| 463 | + } |
|
| 464 | + unset($snmptrans); |
|
| 465 | + $retVal=0; |
|
| 466 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 467 | + ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 468 | + if ($retVal!=0) |
|
| 469 | + { |
|
| 470 | + $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 471 | + $this->timing['type0_check_num']++; |
|
| 472 | + return true; |
|
| 473 | + } |
|
| 474 | + //echo "\n v1 trap found : $this->oidDesc['oid'] \n"; |
|
| 475 | + // Force as trap. |
|
| 476 | + $this->oidDesc['type']=21; |
|
| 477 | + } |
|
| 478 | + if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 479 | + { |
|
| 480 | + $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
| 481 | + $this->timing['nottrap_num']++; |
|
| 482 | + return true; |
|
| 483 | + } |
|
| 484 | + return false; |
|
| 485 | + } |
|
| 486 | 486 | |
| 487 | 487 | |
| 488 | - private function get_trap_mib_description() |
|
| 489 | - { |
|
| 490 | - $retVal=0; |
|
| 491 | - $match=$snmptrans=array(); |
|
| 492 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 493 | - ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
| 494 | - if ($retVal!=0) |
|
| 495 | - { |
|
| 496 | - $this->logging->log('error executing snmptranslate',ERROR); |
|
| 497 | - } |
|
| 488 | + private function get_trap_mib_description() |
|
| 489 | + { |
|
| 490 | + $retVal=0; |
|
| 491 | + $match=$snmptrans=array(); |
|
| 492 | + exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 493 | + ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
| 494 | + if ($retVal!=0) |
|
| 495 | + { |
|
| 496 | + $this->logging->log('error executing snmptranslate',ERROR); |
|
| 497 | + } |
|
| 498 | 498 | |
| 499 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 500 | - { |
|
| 501 | - $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
| 502 | - } |
|
| 503 | - $this->oidDesc['mib']=$match[1]; |
|
| 499 | + if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 500 | + { |
|
| 501 | + $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
| 502 | + } |
|
| 503 | + $this->oidDesc['mib']=$match[1]; |
|
| 504 | 504 | |
| 505 | - $numLine=1; |
|
| 506 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 507 | - if (isset($snmptrans[$numLine])) |
|
| 508 | - { |
|
| 509 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 505 | + $numLine=1; |
|
| 506 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 507 | + if (isset($snmptrans[$numLine])) |
|
| 508 | + { |
|
| 509 | + $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 510 | 510 | |
| 511 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 512 | - { |
|
| 513 | - $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 514 | - $numLine++; |
|
| 515 | - } |
|
| 516 | - if (isset($snmptrans[$numLine])) { |
|
| 517 | - $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 518 | - $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
| 519 | - } |
|
| 511 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 512 | + { |
|
| 513 | + $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 514 | + $numLine++; |
|
| 515 | + } |
|
| 516 | + if (isset($snmptrans[$numLine])) { |
|
| 517 | + $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 518 | + $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
| 519 | + } |
|
| 520 | 520 | |
| 521 | - } |
|
| 522 | - return $snmptrans; |
|
| 523 | - } |
|
| 521 | + } |
|
| 522 | + return $snmptrans; |
|
| 523 | + } |
|
| 524 | 524 | |
| 525 | - /** |
|
| 526 | - * Cache mib in database |
|
| 527 | - * @param boolean $display_progress : Display progress on standard output |
|
| 528 | - * @param boolean $check_change : Force check of trap params & objects |
|
| 529 | - * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 530 | - * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 531 | - */ |
|
| 532 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 533 | - { |
|
| 534 | - // Global Timing |
|
| 535 | - $timeTaken = microtime(true); |
|
| 525 | + /** |
|
| 526 | + * Cache mib in database |
|
| 527 | + * @param boolean $display_progress : Display progress on standard output |
|
| 528 | + * @param boolean $check_change : Force check of trap params & objects |
|
| 529 | + * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
|
| 530 | + * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
|
| 531 | + */ |
|
| 532 | + public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 533 | + { |
|
| 534 | + // Global Timing |
|
| 535 | + $timeTaken = microtime(true); |
|
| 536 | 536 | |
| 537 | - $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
|
| 537 | + $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
|
| 538 | 538 | |
| 539 | - $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
|
| 539 | + $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
|
| 540 | 540 | |
| 541 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 542 | - $num_step=0; |
|
| 543 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 541 | + $step=$basestep=$numElements/10; // output display of % done |
|
| 542 | + $num_step=0; |
|
| 543 | + $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 544 | 544 | |
| 545 | - // Create index for trap objects |
|
| 546 | - $this->trapObjectsIndex=array(); |
|
| 545 | + // Create index for trap objects |
|
| 546 | + $this->trapObjectsIndex=array(); |
|
| 547 | 547 | |
| 548 | - // detailed timing (time_* vars) |
|
| 549 | - $this->reset_update_timers(); |
|
| 548 | + // detailed timing (time_* vars) |
|
| 549 | + $this->reset_update_timers(); |
|
| 550 | 550 | |
| 551 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 552 | - { |
|
| 553 | - $this->timing['base_time']= microtime(true); |
|
| 554 | - if ($display_progress) |
|
| 555 | - { |
|
| 556 | - if ((microtime(true)-$timeFiveSec) > 2) |
|
| 557 | - { // echo a . every 2 sec |
|
| 558 | - echo '.'; |
|
| 559 | - $timeFiveSec = microtime(true); |
|
| 560 | - } |
|
| 561 | - if ($curElement>$step) |
|
| 562 | - { // display progress |
|
| 563 | - $num_step++; |
|
| 564 | - $step+=$basestep; |
|
| 565 | - echo "\n" . ($num_step*10). '% : '; |
|
| 566 | - } |
|
| 567 | - } |
|
| 551 | + for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 552 | + { |
|
| 553 | + $this->timing['base_time']= microtime(true); |
|
| 554 | + if ($display_progress) |
|
| 555 | + { |
|
| 556 | + if ((microtime(true)-$timeFiveSec) > 2) |
|
| 557 | + { // echo a . every 2 sec |
|
| 558 | + echo '.'; |
|
| 559 | + $timeFiveSec = microtime(true); |
|
| 560 | + } |
|
| 561 | + if ($curElement>$step) |
|
| 562 | + { // display progress |
|
| 563 | + $num_step++; |
|
| 564 | + $step+=$basestep; |
|
| 565 | + echo "\n" . ($num_step*10). '% : '; |
|
| 566 | + } |
|
| 567 | + } |
|
| 568 | 568 | |
| 569 | - $this->reset_oidDesc(); |
|
| 570 | - if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
| 571 | - { |
|
| 572 | - continue; |
|
| 573 | - } |
|
| 569 | + $this->reset_oidDesc(); |
|
| 570 | + if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
| 571 | + { |
|
| 572 | + continue; |
|
| 573 | + } |
|
| 574 | 574 | |
| 575 | - $this->timing['num_traps']++; |
|
| 575 | + $this->timing['num_traps']++; |
|
| 576 | 576 | |
| 577 | - $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
| 578 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 577 | + $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
| 578 | + if ($display_progress) echo '#'; // echo a # when trap found |
|
| 579 | 579 | |
| 580 | - //################################ |
|
| 581 | - // get trap objects & source MIB |
|
| 580 | + //################################ |
|
| 581 | + // get trap objects & source MIB |
|
| 582 | 582 | |
| 583 | - $snmptrans=$this->get_trap_mib_description(); // get MIB & description |
|
| 583 | + $snmptrans=$this->get_trap_mib_description(); // get MIB & description |
|
| 584 | 584 | |
| 585 | - //################################ |
|
| 586 | - $update=$this->update_oid(); // Do update of trap. |
|
| 585 | + //################################ |
|
| 586 | + $update=$this->update_oid(); // Do update of trap. |
|
| 587 | 587 | |
| 588 | - $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
| 589 | - $this->timing['update_num']++; |
|
| 588 | + $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
| 589 | + $this->timing['update_num']++; |
|
| 590 | 590 | |
| 591 | - $this->timing['base_time']= microtime(true); // Reset to check object time |
|
| 591 | + $this->timing['base_time']= microtime(true); // Reset to check object time |
|
| 592 | 592 | |
| 593 | - if (($update==0) && ($check_change===false)) |
|
| 594 | - { // Trapd didn't change & force check disabled |
|
| 595 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 596 | - if ($display_progress) echo "C"; |
|
| 597 | - continue; |
|
| 598 | - } |
|
| 593 | + if (($update==0) && ($check_change===false)) |
|
| 594 | + { // Trapd didn't change & force check disabled |
|
| 595 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 596 | + if ($display_progress) echo "C"; |
|
| 597 | + continue; |
|
| 598 | + } |
|
| 599 | 599 | |
| 600 | - $synt=null; |
|
| 601 | - $match=array(); |
|
| 602 | - foreach ($snmptrans as $line) |
|
| 603 | - { |
|
| 604 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 605 | - { |
|
| 606 | - $synt=$match[1]; |
|
| 607 | - } |
|
| 608 | - } |
|
| 609 | - if ($synt == null) |
|
| 610 | - { |
|
| 611 | - //echo "No objects for $trapOID\n"; |
|
| 612 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 613 | - continue; |
|
| 614 | - } |
|
| 615 | - //echo "$synt \n"; |
|
| 616 | - $trapObjects=array(); |
|
| 617 | - while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 618 | - { |
|
| 619 | - array_push($trapObjects,$match[1]); |
|
| 620 | - $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 621 | - } |
|
| 600 | + $synt=null; |
|
| 601 | + $match=array(); |
|
| 602 | + foreach ($snmptrans as $line) |
|
| 603 | + { |
|
| 604 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 605 | + { |
|
| 606 | + $synt=$match[1]; |
|
| 607 | + } |
|
| 608 | + } |
|
| 609 | + if ($synt == null) |
|
| 610 | + { |
|
| 611 | + //echo "No objects for $trapOID\n"; |
|
| 612 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 613 | + continue; |
|
| 614 | + } |
|
| 615 | + //echo "$synt \n"; |
|
| 616 | + $trapObjects=array(); |
|
| 617 | + while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 618 | + { |
|
| 619 | + array_push($trapObjects,$match[1]); |
|
| 620 | + $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 621 | + } |
|
| 622 | 622 | |
| 623 | - $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
|
| 623 | + $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
|
| 624 | 624 | |
| 625 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 626 | - $this->timing['objects_num']++; |
|
| 627 | - } |
|
| 625 | + $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 626 | + $this->timing['objects_num']++; |
|
| 627 | + } |
|
| 628 | 628 | |
| 629 | - if ($display_progress) |
|
| 630 | - { |
|
| 631 | - echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
| 632 | - echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
| 633 | - echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
| 634 | - echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
| 635 | - echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
| 629 | + if ($display_progress) |
|
| 630 | + { |
|
| 631 | + echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
| 632 | + echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
| 633 | + echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
| 634 | + echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
| 635 | + echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
| 636 | 636 | |
| 637 | - $timeTaken=microtime(true) - $timeTaken; |
|
| 638 | - echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 639 | - } |
|
| 640 | - } |
|
| 637 | + $timeTaken=microtime(true) - $timeTaken; |
|
| 638 | + echo "Global time : ".round($timeTaken)." seconds\n"; |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | 641 | |
| 642 | 642 | |
| 643 | 643 | } |
| 644 | 644 | \ No newline at end of file |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @param Logging $logClass : where to log |
| 32 | 32 | * @param Database $dbClass : Database |
| 33 | 33 | */ |
| 34 | - function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
|
| 34 | + function __construct($logClass, $dbClass, $snmptrans, $snmptransdir) |
|
| 35 | 35 | { |
| 36 | 36 | $this->logging=$logClass; |
| 37 | 37 | $this->trapsDB=$dbClass; |
@@ -58,9 +58,9 @@ discard block |
||
| 58 | 58 | } |
| 59 | 59 | $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll |
| 60 | 60 | $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element |
| 61 | - if ( $this->oidDesc['name'] != $dbOid['name'] || |
|
| 61 | + if ($this->oidDesc['name'] != $dbOid['name'] || |
|
| 62 | 62 | $this->oidDesc['mib'] != $dbOid['mib'] || |
| 63 | - $this->oidDesc['type'] !=$dbOid['type'] |
|
| 63 | + $this->oidDesc['type'] != $dbOid['type'] |
|
| 64 | 64 | ) |
| 65 | 65 | { // Do update |
| 66 | 66 | $sql='UPDATE '.$this->trapsDB->dbPrefix.'mib_cache SET '. |
@@ -74,22 +74,22 @@ discard block |
||
| 74 | 74 | ':type' => $this->oidDesc['type'], |
| 75 | 75 | ':mib' => $this->oidDesc['mib'], |
| 76 | 76 | ':tc' => $this->oidDesc['textconv']??'null', |
| 77 | - ':display_hint' => $this->oidDesc['dispHint']??'null' , |
|
| 78 | - ':syntax' => $this->oidDesc['syntax']==null??'null', |
|
| 77 | + ':display_hint' => $this->oidDesc['dispHint']??'null', |
|
| 78 | + ':syntax' => $this->oidDesc['syntax'] == null??'null', |
|
| 79 | 79 | ':type_enum' => $this->oidDesc['type_enum']??'null', |
| 80 | 80 | ':description' => $this->oidDesc['description']??'null', |
| 81 | 81 | ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']] |
| 82 | 82 | ); |
| 83 | 83 | |
| 84 | 84 | if ($sqlQuery->execute($sqlParam) === false) { |
| 85 | - $this->logging->log('Error in query : ' . $sql,ERROR,''); |
|
| 85 | + $this->logging->log('Error in query : '.$sql, ERROR, ''); |
|
| 86 | 86 | } |
| 87 | - $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 87 | + $this->logging->log('Trap updated : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
| 88 | 88 | return 1; |
| 89 | 89 | } |
| 90 | 90 | else |
| 91 | 91 | { |
| 92 | - $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
|
| 92 | + $this->logging->log('Trap unchanged : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], DEBUG); |
|
| 93 | 93 | return 0; |
| 94 | 94 | } |
| 95 | 95 | } |
@@ -99,11 +99,11 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
| 101 | 101 | '(oid, name, type , mib, textual_convention, display_hint '. |
| 102 | - ', syntax, type_enum , description ) ' . |
|
| 102 | + ', syntax, type_enum , description ) '. |
|
| 103 | 103 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 104 | 104 | ', :syntax, :type_enum, :description )'; |
| 105 | 105 | |
| 106 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 106 | + if ($this->trapsDB->trapDBType == 'pgsql') $sql.='RETURNING id'; |
|
| 107 | 107 | |
| 108 | 108 | $sqlQuery=$db_conn->prepare($sql); |
| 109 | 109 | |
@@ -112,15 +112,15 @@ discard block |
||
| 112 | 112 | ':name' => $this->oidDesc['name'], |
| 113 | 113 | ':type' => $this->oidDesc['type'], |
| 114 | 114 | ':mib' => $this->oidDesc['mib'], |
| 115 | - ':tc' => ($this->oidDesc['textconv']==null)?'null':$this->oidDesc['textconv'] , |
|
| 116 | - ':display_hint' => ($this->oidDesc['dispHint']==null)?'null':$this->oidDesc['dispHint'] , |
|
| 117 | - ':syntax' => ($this->oidDesc['syntax']==null)?'null':$this->oidDesc['syntax'], |
|
| 118 | - ':type_enum' => ($this->oidDesc['type_enum']==null)?'null':$this->oidDesc['type_enum'], |
|
| 119 | - ':description' => ($this->oidDesc['description']==null)?'null':$this->oidDesc['description'] |
|
| 115 | + ':tc' => ($this->oidDesc['textconv'] == null) ? 'null' : $this->oidDesc['textconv'], |
|
| 116 | + ':display_hint' => ($this->oidDesc['dispHint'] == null) ? 'null' : $this->oidDesc['dispHint'], |
|
| 117 | + ':syntax' => ($this->oidDesc['syntax'] == null) ? 'null' : $this->oidDesc['syntax'], |
|
| 118 | + ':type_enum' => ($this->oidDesc['type_enum'] == null) ? 'null' : $this->oidDesc['type_enum'], |
|
| 119 | + ':description' => ($this->oidDesc['description'] == null) ? 'null' : $this->oidDesc['description'] |
|
| 120 | 120 | ); |
| 121 | 121 | |
| 122 | 122 | if ($sqlQuery->execute($sqlParam) === false) { |
| 123 | - $this->logging->log('Error in query : ' . $sql,1,''); |
|
| 123 | + $this->logging->log('Error in query : '.$sql, 1, ''); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | switch ($this->trapsDB->trapDBType) |
@@ -128,10 +128,10 @@ discard block |
||
| 128 | 128 | case 'pgsql': |
| 129 | 129 | // Get last id to insert oid/values in secondary table |
| 130 | 130 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
| 131 | - $this->logging->log('Error getting id - pgsql - ',1,''); |
|
| 131 | + $this->logging->log('Error getting id - pgsql - ', 1, ''); |
|
| 132 | 132 | } |
| 133 | - if (! isset($inserted_id_ret['id'])) { |
|
| 134 | - $this->logging->log('Error getting id - pgsql - empty.',1,''); |
|
| 133 | + if (!isset($inserted_id_ret['id'])) { |
|
| 134 | + $this->logging->log('Error getting id - pgsql - empty.', 1, ''); |
|
| 135 | 135 | } |
| 136 | 136 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
| 137 | 137 | break; |
@@ -139,15 +139,15 @@ discard block |
||
| 139 | 139 | // Get last id to insert oid/values in secondary table |
| 140 | 140 | $sql='SELECT LAST_INSERT_ID();'; |
| 141 | 141 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 142 | - $this->logging->log('Erreur getting id - mysql - ',1,''); |
|
| 142 | + $this->logging->log('Erreur getting id - mysql - ', 1, ''); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 146 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 146 | + if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 147 | 147 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
| 148 | 148 | break; |
| 149 | 149 | default: |
| 150 | - $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,''); |
|
| 150 | + $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType, 1, ''); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | // Set as newly created. |
@@ -166,9 +166,9 @@ discard block |
||
| 166 | 166 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 167 | 167 | // Get all objects |
| 168 | 168 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
| 169 | - $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
|
| 169 | + $this->logging->log('SQL query get all traps: '.$sql, DEBUG); |
|
| 170 | 170 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 171 | - $this->logging->log('No result in query : ' . $sql,1,''); |
|
| 171 | + $this->logging->log('No result in query : '.$sql, 1, ''); |
|
| 172 | 172 | } |
| 173 | 173 | $dbObjectsRaw=$ret_code->fetchAll(); |
| 174 | 174 | |
@@ -185,30 +185,30 @@ discard block |
||
| 185 | 185 | * @param string $trapmib : mib of trap |
| 186 | 186 | * @return NULL|array : null if not found, or output of snmptranslate |
| 187 | 187 | */ |
| 188 | - private function get_object_details($object,$trapmib) |
|
| 188 | + private function get_object_details($object, $trapmib) |
|
| 189 | 189 | { |
| 190 | 190 | $match=$snmptrans=array(); |
| 191 | 191 | $retVal=0; |
| 192 | 192 | $this->oidDesc['mib']=$trapmib; |
| 193 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 194 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 195 | - if ($retVal!=0) |
|
| 193 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 194 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 195 | + if ($retVal != 0) |
|
| 196 | 196 | { |
| 197 | 197 | // Maybe not trap mib, search with IR |
| 198 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 199 | - ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
|
| 200 | - if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
|
| 198 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 199 | + ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal); |
|
| 200 | + if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match)) |
|
| 201 | 201 | { // Not found -> continue with warning |
| 202 | - $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
|
| 202 | + $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, ''); |
|
| 203 | 203 | return null; |
| 204 | 204 | } |
| 205 | 205 | $this->oidDesc['mib']=$match[1]; |
| 206 | 206 | |
| 207 | 207 | // Do the snmptranslate again. |
| 208 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 209 | - ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
|
| 210 | - if ($retVal!=0) { |
|
| 211 | - $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
|
| 208 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 209 | + ' -On -Td '.$this->oidDesc['mib'].'::'.$object, $snmptrans, $retVal); |
|
| 210 | + if ($retVal != 0) { |
|
| 211 | + $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object, 2, ''); |
|
| 212 | 212 | return null; |
| 213 | 213 | } |
| 214 | 214 | |
@@ -228,12 +228,12 @@ discard block |
||
| 228 | 228 | |
| 229 | 229 | foreach ($snmptrans as $line) |
| 230 | 230 | { |
| 231 | - if ($indesc===true) |
|
| 231 | + if ($indesc === true) |
|
| 232 | 232 | { |
| 233 | - $line=preg_replace('/[\t ]+/',' ',$line); |
|
| 234 | - if (preg_match('/(.*)"$/', $line,$match)) |
|
| 233 | + $line=preg_replace('/[\t ]+/', ' ', $line); |
|
| 234 | + if (preg_match('/(.*)"$/', $line, $match)) |
|
| 235 | 235 | { |
| 236 | - $this->oidDesc['description'] = $tmpdesc . $match[1]; |
|
| 236 | + $this->oidDesc['description']=$tmpdesc.$match[1]; |
|
| 237 | 237 | $indesc=false; |
| 238 | 238 | } |
| 239 | 239 | $tmpdesc.=$line; |
@@ -244,34 +244,34 @@ discard block |
||
| 244 | 244 | $this->oidDesc['oid']=$line; |
| 245 | 245 | continue; |
| 246 | 246 | } |
| 247 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match)) |
|
| 247 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match)) |
|
| 248 | 248 | { |
| 249 | 249 | $this->oidDesc['syntax']=$match[1]; |
| 250 | 250 | $this->oidDesc['type_enum']=$match[2]; |
| 251 | 251 | continue; |
| 252 | 252 | } |
| 253 | - if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match)) |
|
| 253 | + if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match)) |
|
| 254 | 254 | { |
| 255 | 255 | $this->oidDesc['syntax']=$match[1]; |
| 256 | 256 | continue; |
| 257 | 257 | } |
| 258 | - if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match)) |
|
| 258 | + if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match)) |
|
| 259 | 259 | { |
| 260 | 260 | $this->oidDesc['dispHint']=$match[1]; |
| 261 | 261 | continue; |
| 262 | 262 | } |
| 263 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match)) |
|
| 263 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match)) |
|
| 264 | 264 | { |
| 265 | 265 | $this->oidDesc['description']=$match[1]; |
| 266 | 266 | continue; |
| 267 | 267 | } |
| 268 | - if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match)) |
|
| 268 | + if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match)) |
|
| 269 | 269 | { |
| 270 | 270 | $tmpdesc=$match[1]; |
| 271 | 271 | $indesc=true; |
| 272 | 272 | continue; |
| 273 | 273 | } |
| 274 | - if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match)) |
|
| 274 | + if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match)) |
|
| 275 | 275 | { |
| 276 | 276 | $this->oidDesc['textconv']=$match[1]; |
| 277 | 277 | continue; |
@@ -286,9 +286,9 @@ discard block |
||
| 286 | 286 | * @param array $objects : array of objects name (without MIB) |
| 287 | 287 | * @param bool $check_existing : check instead of create |
| 288 | 288 | */ |
| 289 | - public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
|
| 289 | + public function trap_objects($trapOID, $trapmib, $objects, $check_existing) |
|
| 290 | 290 | { |
| 291 | - $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 291 | + $trapId=$this->dbOidIndex[$trapOID]['id']; // Get id of trap |
|
| 292 | 292 | |
| 293 | 293 | if ($check_existing === true) |
| 294 | 294 | { |
@@ -305,9 +305,9 @@ discard block |
||
| 305 | 305 | |
| 306 | 306 | $this->parse_object($snmptrans); |
| 307 | 307 | |
| 308 | - $this->oidDesc['name'] = $object; |
|
| 308 | + $this->oidDesc['name']=$object; |
|
| 309 | 309 | |
| 310 | - $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
|
| 310 | + $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'], DEBUG); |
|
| 311 | 311 | |
| 312 | 312 | // Update |
| 313 | 313 | $this->update_oid(); |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | ); |
| 333 | 333 | |
| 334 | 334 | if ($sqlQuery->execute($sqlParam) === false) { |
| 335 | - $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
|
| 335 | + $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$this->oidDesc['oid']]['id'], 1, ''); |
|
| 336 | 336 | } |
| 337 | 337 | } |
| 338 | 338 | if ($check_existing === true) |
@@ -349,7 +349,7 @@ discard block |
||
| 349 | 349 | $this->oidDesc['type']=null; |
| 350 | 350 | $this->oidDesc['mib']=null; |
| 351 | 351 | $this->oidDesc['textconv']=null; |
| 352 | - $this->oidDesc['dispHint'] =null; |
|
| 352 | + $this->oidDesc['dispHint']=null; |
|
| 353 | 353 | $this->oidDesc['syntax']=null; |
| 354 | 354 | $this->oidDesc['type_enum']=null; |
| 355 | 355 | $this->oidDesc['description']=null; |
@@ -363,17 +363,17 @@ discard block |
||
| 363 | 363 | { |
| 364 | 364 | $retVal=0; |
| 365 | 365 | // Get all mib objects from all mibs |
| 366 | - $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 367 | - $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
|
| 366 | + $snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
|
| 367 | + $this->logging->log('Getting all traps : '.$snmpCommand, DEBUG); |
|
| 368 | 368 | unset($this->objectsAll); |
| 369 | - exec($snmpCommand,$this->objectsAll,$retVal); |
|
| 370 | - if ($retVal!=0) |
|
| 369 | + exec($snmpCommand, $this->objectsAll, $retVal); |
|
| 370 | + if ($retVal != 0) |
|
| 371 | 371 | { |
| 372 | - $this->logging->log('error executing snmptranslate',ERROR,''); |
|
| 372 | + $this->logging->log('error executing snmptranslate', ERROR, ''); |
|
| 373 | 373 | } |
| 374 | 374 | // Count elements to show progress |
| 375 | 375 | $numElements=count($this->objectsAll); |
| 376 | - $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
|
| 376 | + $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO); |
|
| 377 | 377 | return $numElements; |
| 378 | 378 | } |
| 379 | 379 | |
@@ -387,14 +387,14 @@ discard block |
||
| 387 | 387 | $db_conn=$this->trapsDB->db_connect_trap(); |
| 388 | 388 | |
| 389 | 389 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
| 390 | - $this->logging->log('SQL query : '.$sql,DEBUG ); |
|
| 390 | + $this->logging->log('SQL query : '.$sql, DEBUG); |
|
| 391 | 391 | if (($ret_code=$db_conn->query($sql)) === false) { |
| 392 | - $this->logging->log('No result in query : ' . $sql,ERROR,''); |
|
| 392 | + $this->logging->log('No result in query : '.$sql, ERROR, ''); |
|
| 393 | 393 | } |
| 394 | 394 | $this->dbOidAll=$ret_code->fetchAll(); |
| 395 | 395 | $this->dbOidIndex=array(); |
| 396 | 396 | // Create the index for db; |
| 397 | - foreach($this->dbOidAll as $key=>$val) |
|
| 397 | + foreach ($this->dbOidAll as $key=>$val) |
|
| 398 | 398 | { |
| 399 | 399 | $this->dbOidIndex[$val['oid']]['key']=$key; |
| 400 | 400 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
@@ -427,13 +427,13 @@ discard block |
||
| 427 | 427 | * @param bool $onlyTraps : set to false to get all and not only traps. |
| 428 | 428 | * @return boolean : false if it's a trap , true if not |
| 429 | 429 | */ |
| 430 | - private function detect_trap($curElement,$onlyTraps) |
|
| 430 | + private function detect_trap($curElement, $onlyTraps) |
|
| 431 | 431 | { |
| 432 | 432 | // Get oid or pass if not found |
| 433 | - if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement])) |
|
| 433 | + if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement])) |
|
| 434 | 434 | { |
| 435 | - $this->timing['base_parse_time'] += microtime(true) - $this->timing['base_time']; |
|
| 436 | - $this->timing['base_parse_num'] ++; |
|
| 435 | + $this->timing['base_parse_time']+=microtime(true) - $this->timing['base_time']; |
|
| 436 | + $this->timing['base_parse_num']++; |
|
| 437 | 437 | return true; |
| 438 | 438 | } |
| 439 | 439 | $this->oidDesc['oid']=$this->objectsAll[$curElement]; |
@@ -442,9 +442,9 @@ discard block |
||
| 442 | 442 | $curElement++; |
| 443 | 443 | $match=$snmptrans=array(); |
| 444 | 444 | if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/', |
| 445 | - $this->objectsAll[$curElement],$match)) |
|
| 445 | + $this->objectsAll[$curElement], $match)) |
|
| 446 | 446 | { |
| 447 | - $this->timing['base_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 447 | + $this->timing['base_check_time']+=microtime(true) - $this->timing['base_time']; |
|
| 448 | 448 | $this->timing['base_check_num']++; |
| 449 | 449 | return true; |
| 450 | 450 | } |
@@ -452,22 +452,22 @@ discard block |
||
| 452 | 452 | $this->oidDesc['name']=$match[1]; // Name |
| 453 | 453 | $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 454 | 454 | |
| 455 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 455 | + if ($this->oidDesc['type'] == 0) // object type=0 : check if v1 trap |
|
| 456 | 456 | { |
| 457 | 457 | // Check if next is suboid -> in that case is cannot be a trap |
| 458 | - if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
|
| 458 | + if (preg_match("/^".$this->oidDesc['oid']."/", $this->objectsAll[$curElement + 1])) |
|
| 459 | 459 | { |
| 460 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 460 | + $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time']; |
|
| 461 | 461 | $this->timing['type0_check_num']++; |
| 462 | 462 | return true; |
| 463 | 463 | } |
| 464 | 464 | unset($snmptrans); |
| 465 | 465 | $retVal=0; |
| 466 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 467 | - ' -Td '.$this->oidDesc['oid'] . ' | grep OBJECTS ',$snmptrans,$retVal); |
|
| 468 | - if ($retVal!=0) |
|
| 466 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 467 | + ' -Td '.$this->oidDesc['oid'].' | grep OBJECTS ', $snmptrans, $retVal); |
|
| 468 | + if ($retVal != 0) |
|
| 469 | 469 | { |
| 470 | - $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
|
| 470 | + $this->timing['type0_check_time']+=microtime(true) - $this->timing['base_time']; |
|
| 471 | 471 | $this->timing['type0_check_num']++; |
| 472 | 472 | return true; |
| 473 | 473 | } |
@@ -475,9 +475,9 @@ discard block |
||
| 475 | 475 | // Force as trap. |
| 476 | 476 | $this->oidDesc['type']=21; |
| 477 | 477 | } |
| 478 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 478 | + if ($onlyTraps === true && $this->oidDesc['type'] != 21) // if only traps and not a trap, continue |
|
| 479 | 479 | { |
| 480 | - $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
|
| 480 | + $this->timing['nottrap_time']+=microtime(true) - $this->timing['base_time']; |
|
| 481 | 481 | $this->timing['nottrap_num']++; |
| 482 | 482 | return true; |
| 483 | 483 | } |
@@ -489,33 +489,33 @@ discard block |
||
| 489 | 489 | { |
| 490 | 490 | $retVal=0; |
| 491 | 491 | $match=$snmptrans=array(); |
| 492 | - exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 493 | - ' -Td '.$this->oidDesc['oid'],$snmptrans,$retVal); |
|
| 494 | - if ($retVal!=0) |
|
| 492 | + exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslateDirs. |
|
| 493 | + ' -Td '.$this->oidDesc['oid'], $snmptrans, $retVal); |
|
| 494 | + if ($retVal != 0) |
|
| 495 | 495 | { |
| 496 | - $this->logging->log('error executing snmptranslate',ERROR); |
|
| 496 | + $this->logging->log('error executing snmptranslate', ERROR); |
|
| 497 | 497 | } |
| 498 | 498 | |
| 499 | - if (!preg_match('/^(.*)::/',$snmptrans[0],$match)) |
|
| 499 | + if (!preg_match('/^(.*)::/', $snmptrans[0], $match)) |
|
| 500 | 500 | { |
| 501 | - $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : ' . $snmptrans[0],ERROR); |
|
| 501 | + $this->logging->log('Error getting mib from trap '.$this->oidDesc['oid'].' : '.$snmptrans[0], ERROR); |
|
| 502 | 502 | } |
| 503 | 503 | $this->oidDesc['mib']=$match[1]; |
| 504 | 504 | |
| 505 | 505 | $numLine=1; |
| 506 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 506 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++; |
|
| 507 | 507 | if (isset($snmptrans[$numLine])) |
| 508 | 508 | { |
| 509 | - $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
|
| 509 | + $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]); |
|
| 510 | 510 | |
| 511 | - while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine])) |
|
| 511 | + while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine])) |
|
| 512 | 512 | { |
| 513 | - $this->oidDesc['description'].=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]); |
|
| 513 | + $this->oidDesc['description'].=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]); |
|
| 514 | 514 | $numLine++; |
| 515 | 515 | } |
| 516 | 516 | if (isset($snmptrans[$numLine])) { |
| 517 | - $this->oidDesc['description'].=preg_replace('/".*/','',$snmptrans[$numLine]); |
|
| 518 | - $this->oidDesc['description']=preg_replace('/[\t ]+/',' ',$this->oidDesc['description']); |
|
| 517 | + $this->oidDesc['description'].=preg_replace('/".*/', '', $snmptrans[$numLine]); |
|
| 518 | + $this->oidDesc['description']=preg_replace('/[\t ]+/', ' ', $this->oidDesc['description']); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | } |
@@ -529,18 +529,18 @@ discard block |
||
| 529 | 529 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
| 530 | 530 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
| 531 | 531 | */ |
| 532 | - public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
|
| 532 | + public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1') |
|
| 533 | 533 | { |
| 534 | 534 | // Global Timing |
| 535 | - $timeTaken = microtime(true); |
|
| 535 | + $timeTaken=microtime(true); |
|
| 536 | 536 | |
| 537 | 537 | $numElements=$this->load_mibs_snmptranslate(); // Load objectsAll |
| 538 | 538 | |
| 539 | 539 | $this->load_mibs_from_db(); // Load from db dbOidAll & dbOidIndex |
| 540 | 540 | |
| 541 | - $step=$basestep=$numElements/10; // output display of % done |
|
| 541 | + $step=$basestep=$numElements / 10; // output display of % done |
|
| 542 | 542 | $num_step=0; |
| 543 | - $timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds |
|
| 543 | + $timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds |
|
| 544 | 544 | |
| 545 | 545 | // Create index for trap objects |
| 546 | 546 | $this->trapObjectsIndex=array(); |
@@ -548,33 +548,33 @@ discard block |
||
| 548 | 548 | // detailed timing (time_* vars) |
| 549 | 549 | $this->reset_update_timers(); |
| 550 | 550 | |
| 551 | - for ($curElement=0;$curElement < $numElements;$curElement++) |
|
| 551 | + for ($curElement=0; $curElement < $numElements; $curElement++) |
|
| 552 | 552 | { |
| 553 | - $this->timing['base_time']= microtime(true); |
|
| 553 | + $this->timing['base_time']=microtime(true); |
|
| 554 | 554 | if ($display_progress) |
| 555 | 555 | { |
| 556 | - if ((microtime(true)-$timeFiveSec) > 2) |
|
| 556 | + if ((microtime(true) - $timeFiveSec) > 2) |
|
| 557 | 557 | { // echo a . every 2 sec |
| 558 | 558 | echo '.'; |
| 559 | - $timeFiveSec = microtime(true); |
|
| 559 | + $timeFiveSec=microtime(true); |
|
| 560 | 560 | } |
| 561 | - if ($curElement>$step) |
|
| 561 | + if ($curElement > $step) |
|
| 562 | 562 | { // display progress |
| 563 | 563 | $num_step++; |
| 564 | 564 | $step+=$basestep; |
| 565 | - echo "\n" . ($num_step*10). '% : '; |
|
| 565 | + echo "\n".($num_step * 10).'% : '; |
|
| 566 | 566 | } |
| 567 | 567 | } |
| 568 | 568 | |
| 569 | 569 | $this->reset_oidDesc(); |
| 570 | - if ($this->detect_trap($curElement,$onlyTraps)===true) |
|
| 570 | + if ($this->detect_trap($curElement, $onlyTraps) === true) |
|
| 571 | 571 | { |
| 572 | 572 | continue; |
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | $this->timing['num_traps']++; |
| 576 | 576 | |
| 577 | - $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
|
| 577 | + $this->logging->log('Found trap : '.$this->oidDesc['name'].' / OID : '.$this->oidDesc['oid'], INFO); |
|
| 578 | 578 | if ($display_progress) echo '#'; // echo a # when trap found |
| 579 | 579 | |
| 580 | 580 | //################################ |
@@ -585,14 +585,14 @@ discard block |
||
| 585 | 585 | //################################ |
| 586 | 586 | $update=$this->update_oid(); // Do update of trap. |
| 587 | 587 | |
| 588 | - $this->timing['update_time'] += microtime(true) - $this->timing['base_time']; |
|
| 588 | + $this->timing['update_time']+=microtime(true) - $this->timing['base_time']; |
|
| 589 | 589 | $this->timing['update_num']++; |
| 590 | 590 | |
| 591 | - $this->timing['base_time']= microtime(true); // Reset to check object time |
|
| 591 | + $this->timing['base_time']=microtime(true); // Reset to check object time |
|
| 592 | 592 | |
| 593 | - if (($update==0) && ($check_change===false)) |
|
| 593 | + if (($update == 0) && ($check_change === false)) |
|
| 594 | 594 | { // Trapd didn't change & force check disabled |
| 595 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 595 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
| 596 | 596 | if ($display_progress) echo "C"; |
| 597 | 597 | continue; |
| 598 | 598 | } |
@@ -601,7 +601,7 @@ discard block |
||
| 601 | 601 | $match=array(); |
| 602 | 602 | foreach ($snmptrans as $line) |
| 603 | 603 | { |
| 604 | - if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
|
| 604 | + if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match)) |
|
| 605 | 605 | { |
| 606 | 606 | $synt=$match[1]; |
| 607 | 607 | } |
@@ -609,30 +609,30 @@ discard block |
||
| 609 | 609 | if ($synt == null) |
| 610 | 610 | { |
| 611 | 611 | //echo "No objects for $trapOID\n"; |
| 612 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 612 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
| 613 | 613 | continue; |
| 614 | 614 | } |
| 615 | 615 | //echo "$synt \n"; |
| 616 | 616 | $trapObjects=array(); |
| 617 | - while (preg_match('/ *([^ ,]+) *,* */',$synt,$match)) |
|
| 617 | + while (preg_match('/ *([^ ,]+) *,* */', $synt, $match)) |
|
| 618 | 618 | { |
| 619 | - array_push($trapObjects,$match[1]); |
|
| 620 | - $synt=preg_replace('/'.$match[0].'/','',$synt); |
|
| 619 | + array_push($trapObjects, $match[1]); |
|
| 620 | + $synt=preg_replace('/'.$match[0].'/', '', $synt); |
|
| 621 | 621 | } |
| 622 | 622 | |
| 623 | 623 | $this->trap_objects($this->oidDesc['oid'], $this->oidDesc['mib'], $trapObjects, false); |
| 624 | 624 | |
| 625 | - $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
|
| 625 | + $this->timing['objects_time']+=microtime(true) - $this->timing['base_time']; |
|
| 626 | 626 | $this->timing['objects_num']++; |
| 627 | 627 | } |
| 628 | 628 | |
| 629 | 629 | if ($display_progress) |
| 630 | 630 | { |
| 631 | - echo "\nNumber of processed traps : ". $this->timing['num_traps'] ."\n"; |
|
| 632 | - echo "\nParsing : " . number_format($this->timing['base_parse_time']+$this->timing['base_check_time'],1) ." sec / " . ($this->timing['base_parse_num']+ $this->timing['base_check_num']) . " occurences\n"; |
|
| 633 | - echo "Detecting traps : " . number_format($this->timing['type0_check_time']+$this->timing['nottrap_time'],1) . " sec / " . ($this->timing['type0_check_num']+$this->timing['nottrap_num']) ." occurences\n"; |
|
| 634 | - echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'],1)." sec , "; |
|
| 635 | - echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'],1)." sec \n"; |
|
| 631 | + echo "\nNumber of processed traps : ".$this->timing['num_traps']."\n"; |
|
| 632 | + echo "\nParsing : ".number_format($this->timing['base_parse_time'] + $this->timing['base_check_time'], 1)." sec / ".($this->timing['base_parse_num'] + $this->timing['base_check_num'])." occurences\n"; |
|
| 633 | + echo "Detecting traps : ".number_format($this->timing['type0_check_time'] + $this->timing['nottrap_time'], 1)." sec / ".($this->timing['type0_check_num'] + $this->timing['nottrap_num'])." occurences\n"; |
|
| 634 | + echo "Trap processing (".$this->timing['update_num']."): ".number_format($this->timing['update_time'], 1)." sec , "; |
|
| 635 | + echo "Objects processing (".$this->timing['objects_num'].") : ".number_format($this->timing['objects_time'], 1)." sec \n"; |
|
| 636 | 636 | |
| 637 | 637 | $timeTaken=microtime(true) - $timeTaken; |
| 638 | 638 | echo "Global time : ".round($timeTaken)." seconds\n"; |
@@ -86,8 +86,7 @@ discard block |
||
| 86 | 86 | } |
| 87 | 87 | $this->logging->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
| 88 | 88 | return 1; |
| 89 | - } |
|
| 90 | - else |
|
| 89 | + } else |
|
| 91 | 90 | { |
| 92 | 91 | $this->logging->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG ); |
| 93 | 92 | return 0; |
@@ -103,7 +102,9 @@ discard block |
||
| 103 | 102 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
| 104 | 103 | ', :syntax, :type_enum, :description )'; |
| 105 | 104 | |
| 106 | - if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
|
| 105 | + if ($this->trapsDB->trapDBType == 'pgsql') { |
|
| 106 | + $sql .= 'RETURNING id'; |
|
| 107 | + } |
|
| 107 | 108 | |
| 108 | 109 | $sqlQuery=$db_conn->prepare($sql); |
| 109 | 110 | |
@@ -143,7 +144,9 @@ discard block |
||
| 143 | 144 | } |
| 144 | 145 | |
| 145 | 146 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
| 146 | - if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 147 | + if ($inserted_id==false) { |
|
| 148 | + throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
|
| 149 | + } |
|
| 147 | 150 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
| 148 | 151 | break; |
| 149 | 152 | default: |
@@ -301,7 +304,10 @@ discard block |
||
| 301 | 304 | $this->reset_oidDesc(); |
| 302 | 305 | |
| 303 | 306 | $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
| 304 | - if ($snmptrans === null) continue; // object not found |
|
| 307 | + if ($snmptrans === null) { |
|
| 308 | + continue; |
|
| 309 | + } |
|
| 310 | + // object not found |
|
| 305 | 311 | |
| 306 | 312 | $this->parse_object($snmptrans); |
| 307 | 313 | |
@@ -452,12 +458,14 @@ discard block |
||
| 452 | 458 | $this->oidDesc['name']=$match[1]; // Name |
| 453 | 459 | $this->oidDesc['type']=$match[2]; // type (21=trap, 0: may be trap, else : not trap |
| 454 | 460 | |
| 455 | - if ($this->oidDesc['type']==0) // object type=0 : check if v1 trap |
|
| 461 | + if ($this->oidDesc['type']==0) { |
|
| 462 | + // object type=0 : check if v1 trap |
|
| 456 | 463 | { |
| 457 | 464 | // Check if next is suboid -> in that case is cannot be a trap |
| 458 | 465 | if (preg_match("/^".$this->oidDesc['oid']."/",$this->objectsAll[$curElement+1])) |
| 459 | 466 | { |
| 460 | 467 | $this->timing['type0_check_time'] += microtime(true) - $this->timing['base_time']; |
| 468 | + } |
|
| 461 | 469 | $this->timing['type0_check_num']++; |
| 462 | 470 | return true; |
| 463 | 471 | } |
@@ -475,9 +483,11 @@ discard block |
||
| 475 | 483 | // Force as trap. |
| 476 | 484 | $this->oidDesc['type']=21; |
| 477 | 485 | } |
| 478 | - if ($onlyTraps===true && $this->oidDesc['type']!=21) // if only traps and not a trap, continue |
|
| 486 | + if ($onlyTraps===true && $this->oidDesc['type']!=21) { |
|
| 487 | + // if only traps and not a trap, continue |
|
| 479 | 488 | { |
| 480 | 489 | $this->timing['nottrap_time'] += microtime(true) - $this->timing['base_time']; |
| 490 | + } |
|
| 481 | 491 | $this->timing['nottrap_num']++; |
| 482 | 492 | return true; |
| 483 | 493 | } |
@@ -503,7 +513,9 @@ discard block |
||
| 503 | 513 | $this->oidDesc['mib']=$match[1]; |
| 504 | 514 | |
| 505 | 515 | $numLine=1; |
| 506 | - while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++; |
|
| 516 | + while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) { |
|
| 517 | + $numLine++; |
|
| 518 | + } |
|
| 507 | 519 | if (isset($snmptrans[$numLine])) |
| 508 | 520 | { |
| 509 | 521 | $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]); |
@@ -575,7 +587,10 @@ discard block |
||
| 575 | 587 | $this->timing['num_traps']++; |
| 576 | 588 | |
| 577 | 589 | $this->logging->log('Found trap : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],INFO ); |
| 578 | - if ($display_progress) echo '#'; // echo a # when trap found |
|
| 590 | + if ($display_progress) { |
|
| 591 | + echo '#'; |
|
| 592 | + } |
|
| 593 | + // echo a # when trap found |
|
| 579 | 594 | |
| 580 | 595 | //################################ |
| 581 | 596 | // get trap objects & source MIB |
@@ -593,7 +608,9 @@ discard block |
||
| 593 | 608 | if (($update==0) && ($check_change===false)) |
| 594 | 609 | { // Trapd didn't change & force check disabled |
| 595 | 610 | $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
| 596 | - if ($display_progress) echo "C"; |
|
| 611 | + if ($display_progress) { |
|
| 612 | + echo "C"; |
|
| 613 | + } |
|
| 597 | 614 | continue; |
| 598 | 615 | } |
| 599 | 616 | |