-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathIcinga2API.php
More file actions
313 lines (276 loc) · 9.11 KB
/
Copy pathIcinga2API.php
File metadata and controls
313 lines (276 loc) · 9.11 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
namespace Icinga\Module\Trapdirector;
use Icinga\Module\Trapdirector\IcingaApi\IcingaApiBase;
use RuntimeException;
use Exception;
class Icinga2API extends IcingaApiBase
{
/**
* Creates Icinga2API object
*
* @param string $host host name or IP
* @param number $port API port
*/
public function __construct($host, $port = 5665)
{
parent::__construct($host,$port);
}
/**
/************ Host query ************/
/**
* return array of host by filter
* @param string $hostfilter
* @throws Exception
* @return array objects : array('__name','name','display_name','id' (=__name), 'address', 'ip4' (=address), 'address6', 'ip6' (=address6)
*/
public function getHostByFilter(string $hostfilter)
{
$hosts = $this->standardQuery(
'host',
$hostfilter,
//'match("*' . $ip . '*",host.address) || match("*' . $ip . '*",host.address6) || match("*' . $ip . '*",host.name) || match("*' . $ip . '*",host.display_name)',
array('__name','name','display_name','address','address6')
);
foreach ( array_keys($hosts) as $key )
{
$hosts[$key]->id = $hosts[$key]->__name;
$hosts[$key]->ip4 = $hosts[$key]->address;
$hosts[$key]->ip6 = $hosts[$key]->address6;
}
return $hosts;
}
/**
* return array of host by IP (4 or 6)
* @param string $ip
* @throws Exception
* @return array objects : array('__name','name','display_name')
*/
public function getHostByIP(string $ip)
{
return $this->getHostByFilter('match("*' . $ip . '*",host.address) || match("*' . $ip . '*",host.address6)');
}
/**
* Get host(s) by name in API
* @param string $name
* @return array|NULL[] : see getHostByIP
*/
public function getHostByName(string $name)
{
return $this->getHostByFilter('match("*' . $name . '*",host.name) || match("*' . $name . '*",host.display_name)');
}
/**
* Get host(s) by name in API
* @param string $name
* @return array|NULL[] : see getHostByIP
*/
public function getHostByNameOrIP(string $name)
{
return $this->getHostByFilter(
'match("*' . $name . '*",host.name) || match("*' . $name . '*",host.display_name) || match("*' . $name . '*",host.address) || match("*' . $name . '*",host.address6)');
}
public function getHostInfoByID(string $name)
{
$host = $this->getHostByFilter(
'host.__name=="'. $name .'"');
if (isset($host[0]))
return $host[0];
else
return NULL;
}
/**
* Get all host and IP from hostgroup
* @param string $hostGroup
* @throws Exception
* @return array : attributes : address, address6, name
*/
public function getHostsIPByHostGroup($hostGroup)
{
return $this->standardQuery(
'host',
'"' . $hostGroup . '" in host.groups',
array('address','address6','name')
);
}
/** Get services from host in API
*
* @throws Exception
* @param $id string host name
* @param bool $active
* @param bool $passive_svc
* @return array display_name (of service), service_object_id
*/
public function getServicesByHostid(string $id, bool $active = TRUE, bool $passive_svc = TRUE)
{
$filter = 'match("' . $id . '!*", service.__name)';
if ($active === TRUE)
{
$filter .= ' && service.active==true';
}
if ($passive_svc === TRUE)
{
$filter .= ' && service.enable_passive_checks==true';
}
$services = $this->standardQuery(
'service',
$filter,
array('__name','name','display_name','active')
);
foreach ( array_keys($services) as $key )
{
$services[$key]->id = $services[$key]->__name;
}
return $services;
}
/************ Host group query ************/
/**
* return array of host by IP (4 or 6) or name
* @param string $group Host group name
* @throws Exception
* @return array objects : array('name','display_name')
*/
public function getHostsByGroup(string $group)
{
return $this->standardQuery(
'host',
'"' . $group . '" in host.groups',
array('name','display_name')
);
}
public function getServicesByHostGroupid(string $group)
{
$hostList = $this->getHostsByGroup($group);
//return $hostList;
$hostNum = count($hostList);
$serviceList=array();
foreach ($hostList as $curHost)
{
$services = $this->getServicesByHostid($curHost->name);
foreach ($services as $service)
{
//return $service;
if (! isset($serviceList[$service->name]))
{
$serviceList[$service->name]=
array('num'=> 1 ,'__name' => $service->__name,'display_name' => $service->display_name);
}
else
{
$serviceList[$service->name]['num']++;
}
}
}
$commonServices=array();
foreach ($serviceList as $key => $values)
{
if ($values['num'] >= $hostNum)
{
array_push($commonServices,array($key,$values['display_name']));
}
}
return $commonServices;
}
/**
* Get all host and IP from hostgroup
* @param string $hostGroup
* @throws Exception
* @return array : attributes : address, address6, name
*/
public function getHostGroupByName($name)
{
$hosts = $this->standardQuery(
'hostgroup',
'match("*' . $name . '*",hostgroup.name)',
array('__name','name','display_name')
);
foreach ( array_keys($hosts) as $key )
{
$hosts[$key]->id = $hosts[$key]->__name;
}
return $hosts;
}
/**
* Get hostgroup by id (__name)
* @param string $hostGroup
* @throws Exception
* @return array : __name, name, display_name
*/
public function getHostGroupById($name)
{
$hosts = $this->standardQuery(
'hostgroup',
'hostgroup.__name=="'. $name .'"',
array('__name','name','display_name')
);
$hosts[0]->id = $hosts[0]->__name;
return $hosts[0];
}
/**************** Service queries ************/
/** Get services object id by host name / service name
* does not catch exceptions
* @param $hostname string host name
* @param $name string service name
* @param bool $active : if true, return only active service
* @param bool $passive_svc : if true, return only service accepting passive checks
* @return array service id
*/
public function getServiceIDByName($hostname,$name,bool $active = TRUE, bool $passive_svc = TRUE)
{
$filter = 'service.__name=="' . $hostname . '!'. $name .'"';
if ($active === TRUE)
{
$filter .= ' && service.active==true';
}
if ($passive_svc === TRUE)
{
$filter .= ' && service.enable_passive_checks==true';
}
$services = $this->standardQuery(
'service',
$filter,
array('__name','name','display_name','active','enable_passive_checks')
);
foreach ( array_keys($services) as $key )
{
$services[$key]->id = $services[$key]->__name;
}
return $services;
}
/** Get services object by id (host!name)
* does not catch exceptions
* @param $name string service __name (host!name)
* @return array service id
*/
public function getServiceById($name)
{
$filter = 'service.__name=="' . $name .'"';
$services = $this->standardQuery(
'service',
$filter,
array('__name','name','display_name','active','enable_passive_checks')
);
foreach ( array_keys($services) as $key )
{
$services[$key]->id = $services[$key]->__name;
}
return $services;
}
/** Get services object by id (host!name)
* does not catch exceptions
* @param $name string service __name (host!name)
* @return array service id
*/
public function getNOKService()
{
$filter = 'service.state != ServiceOK && !(service.acknowledgement || service.downtime_depth || service.host.downtime_depth)';
$services = $this->standardQuery(
'service',
$filter,
array('__name','name','last_check','host_name','state')
);
foreach ( array_keys($services) as $key )
{
$services[$key]->id = $services[$key]->__name;
}
return $services;
}
}