Test Setup Failed
Push — master ( f19443...d25d51 )
by Patrick
06:34
created

Icinga2API::getServiceById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
4
namespace Icinga\Module\Trapdirector;
5
6
use Icinga\Module\Trapdirector\IcingaApi\IcingaApiBase;
7
use RuntimeException;
8
use Exception;
9
10
class Icinga2API extends IcingaApiBase
11
{
12
    
13
    /**
14
     * Creates Icinga2API object
15
     * 
16
     * @param string $host host name or IP
17
     * @param number $port API port
18
     */
19
    public function __construct($host, $port = 5665)
20
    {
21
        parent::__construct($host,$port);
22
    }
23
    /**
24
25
/************ Host query ************/  
26
27
    /**
28
     * return array of host by filter
29
     * @param string $hostfilter
30
     * @throws Exception
31
     * @return array objects : array('__name','name','display_name','id' (=__name), 'address', 'ip4' (=address), 'address6', 'ip6' (=address6)
32
     */
33
    public function getHostByFilter(string $hostfilter)
34
    {
35
        $hosts = $this->standardQuery(
36
            'host',
37
            $hostfilter,
38
            //'match("*' . $ip . '*",host.address) || match("*' . $ip . '*",host.address6) || match("*' . $ip . '*",host.name) || match("*' . $ip . '*",host.display_name)',
39
            array('__name','name','display_name','address','address6')
40
            );
41
        foreach ( array_keys($hosts) as $key )
42
        {
43
            $hosts[$key]->id = $hosts[$key]->__name;
44
            $hosts[$key]->ip4 = $hosts[$key]->address;
45
            $hosts[$key]->ip6 = $hosts[$key]->address6;
46
        }
47
        return $hosts;
48
    }
49
    
50
    /**
51
     * return array of host by IP (4 or 6)
52
     * @param string $ip
53
     * @throws Exception
54
     * @return array objects : array('__name','name','display_name')
55
     */
56
    public function getHostByIP(string $ip) 
57
    {
58
        return $this->getHostByFilter('match("*' . $ip . '*",host.address) || match("*' . $ip . '*",host.address6)');
59
    }
60
61
    
62
    /**
63
     * Get host(s) by name in API
64
     * @param string $name
65
     * @return array|NULL[] : see getHostByIP
66
     */
67
    public function getHostByName(string $name)
68
    {
69
        return $this->getHostByFilter('match("*' . $name . '*",host.name) || match("*' . $name . '*",host.display_name)');
70
    }
71
72
    /**
73
     * Get host(s) by name in API
74
     * @param string $name
75
     * @return array|NULL[] : see getHostByIP
76
     */
77
    public function getHostByNameOrIP(string $name)
78
    {
79
        return $this->getHostByFilter( 
80
            'match("*' . $name . '*",host.name) || match("*' . $name . '*",host.display_name) || match("*' . $name . '*",host.address) || match("*' . $name . '*",host.address6)');
81
    }
82
    
83
    public function getHostInfoByID(string $name)
84
    {
85
        $host = $this->getHostByFilter(
86
            'host.__name=="'. $name .'"');
87
        if (isset($host[0]))
88
            return $host[0];
89
        else
90
            return NULL;
91
    }
92
 
93
    /**
94
     * Get all host and IP from hostgroup
95
     * @param string $hostGroup
96
     * @throws Exception
97
     * @return array : attributes : address, address6, name
98
     */
99
    public function getHostsIPByHostGroup($hostGroup)
100
    {        
101
        return $this->standardQuery(
102
            'host', 
103
            '"' . $hostGroup . '" in host.groups',
104
            array('address','address6','name')
105
                
106
        );
107
    }
108
109
110
    /** Get services from host in API
111
     *	
112
     *  @throws Exception
113
     *	@param $id string host name
114
     *  @param bool $active
115
     *  @param bool $passive_svc
116
     *	@return array display_name (of service), service_object_id
117
     */
118
    public function getServicesByHostid(string $id, bool $active = TRUE, bool $passive_svc = TRUE)
119
    {
120
        $filter = 'match("' . $id . '!*", service.__name)';
121
        if ($active === TRUE)
122
        {
123
            $filter .= ' && service.active==true';
124
        }
125
        if ($passive_svc === TRUE)
126
        {
127
            $filter .= ' && service.enable_passive_checks==true';
128
        }
129
        $services =  $this->standardQuery(
130
            'service',
131
            $filter,
132
            array('__name','name','display_name','active')
133
            );
134
        
135
        foreach ( array_keys($services) as $key )
136
        {
137
            $services[$key]->id = $services[$key]->__name;
138
        }
139
        
140
        return $services;
141
        
142
    }
143
144
/************  Host group query ************/    
145
    /**
146
     * return array of host by IP (4 or 6) or name
147
     * @param string $group Host group name
148
     * @throws Exception
149
     * @return array objects : array('name','display_name')
150
     */
151
    public function getHostsByGroup(string $group)
152
    {
153
         return $this->standardQuery(
154
            'host',
155
            '"' . $group . '" in host.groups',
156
            array('name','display_name')
157
            );
158
    }
159
    
160
    public function getServicesByHostGroupid(string $group)
161
    {
162
        $hostList = $this->getHostsByGroup($group);
163
        //return $hostList;
164
        $hostNum = count($hostList);
165
        $serviceList=array();
166
        foreach ($hostList as $curHost)
167
        {
168
            $services = $this->getServicesByHostid($curHost->name);
169
            foreach ($services as $service)
170
            {
171
                //return $service;
172
                if (! isset($serviceList[$service->name]))
173
                {
174
                    $serviceList[$service->name]=
175
                        array('num'=> 1 ,'__name' => $service->__name,'display_name' => $service->display_name);
176
                }
177
                else
178
                {
179
                    $serviceList[$service->name]['num']++;
180
                }
181
            }
182
        }
183
        $commonServices=array();
184
        foreach ($serviceList as $key => $values)
185
        {
186
            if ($values['num'] >= $hostNum)
187
            {
188
                array_push($commonServices,array($key,$values['display_name']));
189
            }
190
        }
191
        return $commonServices;
192
    }
193
 
194
    /**
195
     * Get all host and IP from hostgroup
196
     * @param string $hostGroup
197
     * @throws Exception
198
     * @return array : attributes : address, address6, name
199
     */
200
    public function getHostGroupByName($name)
201
    {
202
        $hosts = $this->standardQuery(
203
            'hostgroup',
204
            'match("*' . $name . '*",hostgroup.name)',
205
            array('__name','name','display_name')
206
            
207
            );
208
        foreach ( array_keys($hosts) as $key )
209
        {
210
            $hosts[$key]->id = $hosts[$key]->__name;
211
        }
212
        return $hosts;
213
    }
214
    
215
    /**
216
     * Get hostgroup by id (__name)
217
     * @param string $hostGroup
218
     * @throws Exception
219
     * @return array : __name, name, display_name
220
     */
221
    public function getHostGroupById($name)
222
    {
223
        $hosts = $this->standardQuery(
224
            'hostgroup',
225
            'hostgroup.__name=="'. $name .'"',
226
            array('__name','name','display_name')
227
            
228
            );
229
        $hosts[0]->id = $hosts[0]->__name;
230
        return $hosts[0];
231
    }
232
    
233
/****************   Service queries ************/
234
    /** Get services object id by host name / service name
235
     *	does not catch exceptions
236
     *	@param $hostname string host name
237
     *	@param $name string service name
238
     *  @param bool $active : if true, return only active service
239
     *  @param bool $passive_svc : if true, return only service accepting passive checks
240
     *	@return array  service id
241
     */
242
    public function getServiceIDByName($hostname,$name,bool $active = TRUE, bool $passive_svc = TRUE)
243
    {
244
        $filter = 'service.__name=="' . $hostname . '!'. $name .'"';
245
        if ($active === TRUE)
246
        {
247
            $filter .= ' && service.active==true';
248
        }
249
        if ($passive_svc === TRUE)
250
        {
251
            $filter .= ' && service.enable_passive_checks==true';
252
        }
253
        $services =  $this->standardQuery(
254
            'service',
255
            $filter,
256
            array('__name','name','display_name','active','enable_passive_checks')
257
            );
258
        
259
        foreach ( array_keys($services) as $key )
260
        {
261
            $services[$key]->id = $services[$key]->__name;
262
        }
263
        
264
        return $services;
265
    }
266
 
267
    /** Get services object by id (host!name)
268
     *	does not catch exceptions
269
     *	@param $name string service __name (host!name)
270
     *	@return array  service id
271
     */
272
    public function getServiceById($name)
273
    {
274
        $filter = 'service.__name=="' .  $name .'"';
275
        $services =  $this->standardQuery(
276
            'service',
277
            $filter,
278
            array('__name','name','display_name','active','enable_passive_checks')
279
            );
280
        
281
        foreach ( array_keys($services) as $key )
282
        {
283
            $services[$key]->id = $services[$key]->__name;
284
        }
285
        
286
        return $services;
287
    }
288
    
289
    
290
}
291
292