|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Icinga\Module\Trapdirector\TrapsActions; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Database queries for UI (on IDO database) |
|
9
|
|
|
* Calling class must implement : getTrapCtrl , getIdoDbConn |
|
10
|
|
|
* @license GPL |
|
11
|
|
|
* @author Patrick Proy |
|
12
|
|
|
* @package trapdirector |
|
13
|
|
|
* @subpackage UI |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
trait IdoDBQuery |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** Get host(s) by IP (v4 or v6) or by name in IDO database |
|
20
|
|
|
* does not catch exceptions |
|
21
|
|
|
* @return array of objects ( name, id (object_id), display_name) |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getHostByIP($ip) |
|
24
|
|
|
{ |
|
25
|
|
|
// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
26
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
|
|
|
|
|
27
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
28
|
|
|
|
|
29
|
|
|
// TODO : check for SQL injections |
|
30
|
|
|
$query=$dbConn->select() |
|
31
|
|
|
->from( |
|
32
|
|
|
array('a' => 'icinga_objects'), |
|
33
|
|
|
array('name' => 'a.name1','id' => 'object_id')) |
|
34
|
|
|
->join( |
|
35
|
|
|
array('b' => 'icinga_hosts'), |
|
36
|
|
|
'b.host_object_id=a.object_id', |
|
37
|
|
|
array('display_name' => 'b.display_name')) |
|
38
|
|
|
->where("(b.address LIKE '%".$ip."%' OR b.address6 LIKE '%".$ip."%' OR a.name1 LIKE '%".$ip."%' OR b.display_name LIKE '%".$ip."%') and a.is_active = 1"); |
|
39
|
|
|
return $dbConn->fetchAll($query); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** Get host(s) by name in IDO database |
|
43
|
|
|
* does not catch exceptions |
|
44
|
|
|
* @return array of objects ( name, id (object_id), display_name) |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getHostByName($name) |
|
47
|
|
|
{ |
|
48
|
|
|
// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
49
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
50
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
51
|
|
|
|
|
52
|
|
|
// TODO : check for SQL injections |
|
53
|
|
|
$query=$dbConn->select() |
|
54
|
|
|
->from( |
|
55
|
|
|
array('a' => 'icinga_objects'), |
|
56
|
|
|
array('name' => 'a.name1','id' => 'object_id')) |
|
57
|
|
|
->join( |
|
58
|
|
|
array('b' => 'icinga_hosts'), |
|
59
|
|
|
'b.host_object_id=a.object_id', |
|
60
|
|
|
array('display_name' => 'b.display_name')) |
|
61
|
|
|
->where("a.name1 = '$name'"); |
|
62
|
|
|
return $dbConn->fetchAll($query); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** Get host groups by name in IDO database |
|
66
|
|
|
* does not catch exceptions |
|
67
|
|
|
* @return array of objects ( name, id (object_id), display_name) |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getHostGroupByName($ip) |
|
70
|
|
|
{ |
|
71
|
|
|
// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
72
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
73
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
74
|
|
|
// TODO : check for SQL injections |
|
75
|
|
|
$query=$dbConn->select() |
|
76
|
|
|
->from( |
|
77
|
|
|
array('a' => 'icinga_objects'), |
|
78
|
|
|
array('name' => 'a.name1','id' => 'object_id')) |
|
79
|
|
|
->join( |
|
80
|
|
|
array('b' => 'icinga_hostgroups'), |
|
81
|
|
|
'b.hostgroup_object_id=a.object_id', |
|
82
|
|
|
array('display_name' => 'b.alias')) |
|
83
|
|
|
->where("(a.name1 LIKE '%".$ip."%' OR b.alias LIKE '%".$ip."%') and a.is_active = 1"); |
|
84
|
|
|
return $dbConn->fetchAll($query); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** Get host IP (v4 and v6) by name in IDO database |
|
89
|
|
|
* does not catch exceptions |
|
90
|
|
|
* @return array ( name, display_name, ip4, ip6) |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getHostInfoByID($id) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
95
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
96
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
97
|
|
|
$query=$dbConn->select() |
|
98
|
|
|
->from( |
|
99
|
|
|
array('a' => 'icinga_objects'), |
|
100
|
|
|
array('name' => 'a.name1')) |
|
101
|
|
|
->join( |
|
102
|
|
|
array('b' => 'icinga_hosts'), |
|
103
|
|
|
'b.host_object_id=a.object_id', |
|
104
|
|
|
array('ip4' => 'b.address', 'ip6' => 'b.address6', 'display_name' => 'b.display_name')) |
|
105
|
|
|
->where("a.object_id = '".$id."'"); |
|
106
|
|
|
return $dbConn->fetchRow($query); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** Get host by objectid in IDO database |
|
111
|
|
|
* does not catch exceptions |
|
112
|
|
|
* @return array of objects ( id, name, display_name, ip, ip6, ) |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
|
115
|
|
|
{ |
|
116
|
|
|
if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
117
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
118
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
119
|
|
|
$query=$dbConn->select() |
|
120
|
|
|
->from( |
|
121
|
|
|
array('a' => 'icinga_objects'), |
|
122
|
|
|
array('name' => 'a.name1','id' => 'a.object_id')) |
|
123
|
|
|
->join( |
|
124
|
|
|
array('b' => 'icinga_hosts'), |
|
125
|
|
|
'b.host_object_id=a.object_id', |
|
126
|
|
|
array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
|
127
|
|
|
->where('a.object_id = ?',$id); |
|
128
|
|
|
return $dbConn->fetchRow($query); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** Get services from object ( host_object_id) in IDO database |
|
132
|
|
|
* does not catch exceptions |
|
133
|
|
|
* @param $id int object_id |
|
134
|
|
|
* @return array display_name (of service), service_object_id |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getServicesByHostid($id) |
|
137
|
|
|
{ |
|
138
|
|
|
// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
139
|
|
|
if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
140
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
141
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
142
|
|
|
$query=$dbConn->select() |
|
143
|
|
|
->from( |
|
144
|
|
|
array('s' => 'icinga_services'), |
|
145
|
|
|
array('name' => 's.display_name','id' => 's.service_object_id')) |
|
146
|
|
|
->join( |
|
147
|
|
|
array('a' => 'icinga_objects'), |
|
148
|
|
|
's.service_object_id=a.object_id', |
|
149
|
|
|
array('is_active'=>'a.is_active','name2'=>'a.name2')) |
|
150
|
|
|
->where('s.host_object_id='.$id.' AND a.is_active = 1'); |
|
151
|
|
|
return $dbConn->fetchAll($query); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
|
155
|
|
|
* gets all hosts in hostgroup and return common services |
|
156
|
|
|
* does not catch exceptions |
|
157
|
|
|
* @param $id int object_id |
|
158
|
|
|
* @return array display_name (of service), service_object_id |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getServicesByHostGroupid($id) |
|
161
|
|
|
{ |
|
162
|
|
|
if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
163
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
164
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
165
|
|
|
$query=$dbConn->select() |
|
166
|
|
|
->from( |
|
167
|
|
|
array('s' => 'icinga_hostgroup_members'), |
|
168
|
|
|
array('host_object_id' => 's.host_object_id')) |
|
169
|
|
|
->join( |
|
170
|
|
|
array('a' => 'icinga_hostgroups'), |
|
171
|
|
|
's.hostgroup_id=a.hostgroup_id', |
|
172
|
|
|
'hostgroup_object_id') |
|
173
|
|
|
->where('a.hostgroup_object_id='.$id); |
|
174
|
|
|
$hosts=$dbConn->fetchAll($query); |
|
175
|
|
|
$common_services=array(); |
|
176
|
|
|
$num_hosts=count($hosts); |
|
177
|
|
|
foreach ($hosts as $key => $host) |
|
178
|
|
|
{ // For each host, get all services and add in common_services if not found or add counter |
|
179
|
|
|
$host_services=$this->getServicesByHostid($host->host_object_id); |
|
180
|
|
|
foreach($host_services as $service) |
|
181
|
|
|
{ |
|
182
|
|
|
if (isset($common_services[$service->name2]['num'])) |
|
183
|
|
|
{ |
|
184
|
|
|
$common_services[$service->name2]['num'] +=1; |
|
185
|
|
|
} |
|
186
|
|
|
else |
|
187
|
|
|
{ |
|
188
|
|
|
$common_services[$service->name2]['num']=1; |
|
189
|
|
|
$common_services[$service->name2]['name']=$service->name; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
$result=array(); |
|
194
|
|
|
|
|
195
|
|
|
//print_r($common_services); |
|
196
|
|
|
foreach (array_keys($common_services) as $key) |
|
197
|
|
|
{ |
|
198
|
|
|
if ($common_services[$key]['num'] == $num_hosts) |
|
199
|
|
|
{ |
|
200
|
|
|
array_push($result,array($key,$common_services[$key]['name'])); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $result; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** Get services object id by host name / service name in IDO database |
|
208
|
|
|
* does not catch exceptions |
|
209
|
|
|
* @param $hostname string host name |
|
210
|
|
|
* @param $name string service name |
|
211
|
|
|
* @return array service id |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getServiceIDByName($hostname,$name) |
|
214
|
|
|
{ |
|
215
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
216
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
217
|
|
|
|
|
218
|
|
|
if ($name == null) |
|
219
|
|
|
{ |
|
220
|
|
|
return array(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$query=$dbConn->select() |
|
224
|
|
|
->from( |
|
225
|
|
|
array('s' => 'icinga_services'), |
|
226
|
|
|
array('name' => 's.display_name','id' => 's.service_object_id')) |
|
227
|
|
|
->join( |
|
228
|
|
|
array('a' => 'icinga_objects'), |
|
229
|
|
|
's.service_object_id=a.object_id', |
|
230
|
|
|
'is_active') |
|
231
|
|
|
->where('a.name2=\''.$name.'\' AND a.name1=\''.$hostname.'\' AND a.is_active = 1'); |
|
232
|
|
|
|
|
233
|
|
|
return $dbConn->fetchAll($query); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** Get object name from object_id in IDO database |
|
237
|
|
|
* does not catch exceptions |
|
238
|
|
|
* @param int $id object_id (default to null, used first if not null) |
|
239
|
|
|
* @return array name1 (host) name2 (service) |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getObjectNameByid($id) |
|
242
|
|
|
{ |
|
243
|
|
|
// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
|
244
|
|
|
if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
|
245
|
|
|
$dbConn = $this->getIdoDbConn(); |
|
246
|
|
|
if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
|
247
|
|
|
|
|
248
|
|
|
$query=$dbConn->select() |
|
249
|
|
|
->from( |
|
250
|
|
|
array('a' => 'icinga_objects'), |
|
251
|
|
|
array('name1' => 'a.name1','name2' => 'a.name2')) |
|
252
|
|
|
->where('a.object_id='.$id.' AND a.is_active = 1'); |
|
253
|
|
|
|
|
254
|
|
|
return $dbConn->fetchRow($query); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
} |