-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMibDatabase.php
More file actions
195 lines (168 loc) · 7.28 KB
/
Copy pathMibDatabase.php
File metadata and controls
195 lines (168 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* MIB DB queries and update
*
* @license GPL
* @author Patrick Proy
* @package trapdirector
* @subpackage Processing
*
*/
trait MibDatabase
{
/** @var array $oidDesc MUST be overide by calling class */
//protected $oidDesc;
/** @var array $dbOidIndex MUST be overide by calling class */
//protected $dbOidIndex;
/** @return \Trapdirector\Logging */
abstract public function getLogging();
/** @return \Trapdirector\Database */
abstract public function getTrapsDB();
/**
* Update or add an OID to database uses $this->dbOidIndex for mem cache
* and $this->oidDesc doe data
* @return number : 0=unchanged, 1 = changed, 2=created
*/
public function update_oid()
{
$db_conn=$this->getTrapsDB()->db_connect_trap();
// Quote description.
$this->oidDesc['description']=$db_conn->quote($this->oidDesc['description']);
if (isset($this->dbOidIndex[$this->oidDesc['oid']]))
{ // oid exists in db, so update
return $this->update_oid_update();
}
// create new OID.
return $this->update_oid_create();
}
/**
* Update object in DB with object in dbOidIndex if name/mib/type has changed.
* @return number : 0=unchanged, 1 = changed, 2=created
*/
private function update_oid_update()
{
$db_conn=$this->getTrapsDB()->db_connect_trap();
if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1)
{ // newly created.
return 0;
}
$oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll
$dbOid=$this->dbOidAll[$oidIndex]; // Get array of element
if ( $this->oidDesc['name'] != $dbOid['name'] ||
$this->oidDesc['mib'] != $dbOid['mib'] ||
$this->oidDesc['type'] !=$dbOid['type']
)
{ // Do update
$sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '.
'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'.
', syntax = :syntax, type_enum = :type_enum, description = :description '.
' WHERE id= :id';
$sqlQuery=$db_conn->prepare($sql);
$sqlParam=array(
':name' => $this->oidDesc['name'],
':type' => $this->oidDesc['type'],
':mib' => $this->oidDesc['mib'],
':tc' => $this->oidDesc['textconv']??'null',
':display_hint' => $this->oidDesc['dispHint']??'null' ,
':syntax' => $this->oidDesc['syntax']==null??'null',
':type_enum' => $this->oidDesc['type_enum']??'null',
':description' => $this->oidDesc['description']??'null',
':id' => $this->dbOidIndex[$this->oidDesc['oid']]['id']
// ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']]
);
if ($sqlQuery->execute($sqlParam) === false) {
$this->getLogging()->log('Error in query : ' . $sql,ERROR,'');
}
$this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
return 1;
}
else
{
$this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
return 0;
}
}
/**
* Create object in DB with object in dbOidIndex
* @return number : 0=unchanged, 1 = changed, 2=created
*/
private function update_oid_create()
{
// Insert data
$db_conn=$this->getTrapsDB()->db_connect_trap();
$sql='INSERT INTO '.$this->getTrapsDB()->dbPrefix.'mib_cache '.
'(oid, name, type , mib, textual_convention, display_hint '.
', syntax, type_enum , description ) ' .
'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
', :syntax, :type_enum, :description )';
if ($this->getTrapsDB()->trapDBType == 'pgsql') $sql .= 'RETURNING id';
$sqlQuery=$db_conn->prepare($sql);
$sqlParam=array(
':oid' => $this->oidDesc['oid'],
':name' => $this->oidDesc['name'],
':type' => $this->oidDesc['type'],
':mib' => $this->oidDesc['mib'],
':tc' => $this->oidDesc['textconv']??'null',
':display_hint' => $this->oidDesc['dispHint']??'null',
':syntax' => $this->oidDesc['syntax']??'null',
':type_enum' => $this->oidDesc['type_enum']??'null',
':description' => $this->oidDesc['description']??'null'
);
if ($sqlQuery->execute($sqlParam) === false) {
$this->getLogging()->log('Error in query : ' . $sql,1,'');
}
switch ($this->getTrapsDB()->trapDBType)
{
case 'pgsql':
// Get last id to insert oid/values in secondary table
if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) {
$this->getLogging()->log('Error getting id - pgsql - ',1,'');
}
if (! isset($inserted_id_ret['id'])) {
$this->getLogging()->log('Error getting id - pgsql - empty.',ERROR);
return 0;
}
$this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id'];
break;
case 'mysql':
// Get last id to insert oid/values in secondary table
$sql='SELECT LAST_INSERT_ID();';
if (($ret_code=$db_conn->query($sql)) === false) {
$this->getLogging()->log('Erreur getting id - mysql - ',ERROR);
return 0;
}
$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
$this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id;
break;
default:
$this->getLogging()->log('Error SQL type Unknown : '.$this->getTrapsDB()->trapDBType,ERROR);
return 0;
}
// Set as newly created.
$this->dbOidIndex[$this->oidDesc['oid']]['key']=-1;
return 2;
}
/**
* get all objects for a trap.
* @param integer $trapId
* @return array : array of cached objects
*/
private function cache_db_objects($trapId)
{
$dbObjects=array(); // cache of objects for trap in db
$db_conn=$this->getTrapsDB()->db_connect_trap();
// Get all objects
$sql='SELECT * FROM '.$this->getTrapsDB()->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';';
$this->getLogging()->log('SQL query get all traps: '.$sql,DEBUG );
if (($ret_code=$db_conn->query($sql)) === false) {
$this->getLogging()->log('No result in query : ' . $sql,1,'');
}
$dbObjectsRaw=$ret_code->fetchAll();
foreach ($dbObjectsRaw as $val)
{
$dbObjects[$val['object_id']]=1;
}
return $dbObjects;
}
}