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