Passed
Push — master ( 8ef52a...4d2ca2 )
by Patrick
02:00
created

Icinga2API::getServicesByHostGroupid()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
c 0
b 0
f 0
nc 12
nop 1
dl 0
loc 32
rs 9.1111
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 IP (4 or 6) or name
29
     * @param string $ip
30
     * @throws Exception
31
     * @return array objects : array('__name','name','display_name')
32
     */
33
    public function getHostByIP(string $ip) 
34
    {
35
        return $this->standardQuery(
36
            'host', 
37
            'match("*' . $ip . '*",host.address) || match("*' . $ip . '*",host.address6) || match("*' . $ip . '*",host.name) || match("*' . $ip . '*",host.display_name)',
38
            array('__name','name','display_name')
39
        );
40
    }
41
42
    
43
    /**
44
     * Get host(s) by name in API
45
     * @param string $name
46
     * @return array|NULL[] : see getHostByIP
47
     */
48
    public function getHostByName(string $name)
49
    {
50
        return $this->getHostByIP($name);
51
    }
52
 
53
    /**
54
     * Get all host and IP from hostgroup
55
     * @param string $hostGroup
56
     * @throws Exception
57
     * @return array : attributes : address, address6, name
58
     */
59
    public function getHostsIPByHostGroup($hostGroup)
60
    {        
61
        return $this->standardQuery(
62
            'host', 
63
            '"' . $hostGroup . '" in host.groups',
64
            array('address','address6','name')
65
                
66
        );
67
    }
68
    
69
    /**
70
     * Get all host and IP from hostgroup
71
     * @param string $hostGroup
72
     * @throws Exception
73
     * @return array : attributes : address, address6, name
74
     */
75
    public function getHostGroupByName($name)
76
    {
77
        return $this->standardQuery(
78
            'hostgroup',
79
            'match("*' . $name . '*",hostgroup.name)',
80
            array('name','display_name')
81
            
82
            );
83
    }
84
85
    /** Get services from host in API
86
     *	
87
     *  @throws Exception
88
     *	@param $id string host name
89
     *	@return array display_name (of service), service_object_id
90
     */
91
    public function getServicesByHostid(string $id, bool $active = TRUE, bool $passive_svc = TRUE)
92
    {
93
        $filter = 'match("' . $id . '!*", service.__name)';
94
        if ($active === TRUE)
95
        {
96
            $filter .= ' && service.active==true';
97
        }
98
        if ($passive_svc === TRUE)
99
        {
100
            $filter .= ' && service.enable_passive_checks==true';
101
        }
102
        return $this->standardQuery(
103
            'service',
104
            $filter,
105
            array('__name','name','display_name','active')
106
            );
107
        
108
    }
109
110
    /**
111
     * return array of host by IP (4 or 6) or name
112
     * @param string $group Host group name
113
     * @throws Exception
114
     * @return array objects : array('name','display_name')
115
     */
116
    public function getHostsByGroup(string $group)
117
    {
118
         return $this->standardQuery(
119
            'host',
120
            '"' . $group . '" in host.groups',
121
            array('name','display_name')
122
            );
123
    }
124
    
125
    public function getServicesByHostGroupid(string $group)
126
    {
127
        $hostList = $this->getHostsByGroup($group);
128
        //return $hostList;
129
        $hostNum = count($hostList);
130
        $serviceList=array();
131
        foreach ($hostList as $curHost)
132
        {
133
            $services = $this->getServicesByHostid($curHost->name);
134
            foreach ($services as $service)
135
            {
136
                //return $service;
137
                if (! isset($serviceList[$service->name]))
138
                {
139
                    $serviceList[$service->name]=
140
                        array('num'=> 1 ,'display_name' => $service->display_name);
141
                }
142
                else
143
                {
144
                    $serviceList[$service->name]['num']++;
145
                }
146
            }
147
        }
148
        $commonServices=array();
149
        foreach ($serviceList as $serviceName => $values)
150
        {
151
            if ($values['num'] >= $hostNum)
152
            {
153
                $commonServices[$serviceName] = $values['display_name'];
154
            }
155
        }
156
        return $commonServices;
157
    }
158
    
159
}
160
161