summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/Object/ServiceList.php
blob: 5bc0bdb47dbbbf4669e98362eec117c7f92e4837 (plain)
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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Object;

use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterOr;
use Icinga\Data\SimpleQuery;
use Icinga\Util\StringHelper;

/**
 * A service list
 */
class ServiceList extends ObjectList
{
    protected $hostStateSummary;

    protected $serviceStateSummary;

    protected $dataViewName = 'servicestatus';

    protected $columns = array('host_name', 'service_description');

    protected function fetchObjects()
    {
        $services = array();
        $query = $this->backend->select()->from($this->dataViewName, $this->columns)->applyFilter($this->filter)
            ->getQuery()->getSelectQuery()->query();
        foreach ($query as $row) {
            /** @var object $row */
            $service = new Service($this->backend, $row->host_name, $row->service_description);
            $service->setProperties($row);
            $services[] = $service;
        }
        return $services;
    }

    /**
     * Create a state summary of all services that can be consumed by servicesummary.phtml
     *
     * @return  SimpleQuery
     */
    public function getServiceStateSummary()
    {
        if (! $this->serviceStateSummary) {
            $this->initStateSummaries();
        }

        $ds = new ArrayDatasource(array((object) $this->serviceStateSummary));
        return $ds->select();
    }

    /**
     * Create a state summary of all hosts that can be consumed by hostsummary.phtml
     *
     * @return  SimpleQuery
     */
    public function getHostStateSummary()
    {
        if (! $this->hostStateSummary) {
            $this->initStateSummaries();
        }

        $ds = new ArrayDatasource(array((object) $this->hostStateSummary));
        return $ds->select();
    }

    /**
     * Calculate the current state summary and populate hostStateSummary and serviceStateSummary
     * properties
     */
    protected function initStateSummaries()
    {
        $serviceStates = array_fill_keys(self::getServiceStatesSummaryEmpty(), 0);
        $hostStates = array_fill_keys(HostList::getHostStatesSummaryEmpty(), 0);

        foreach ($this as $service) {
            $unhandled = false;
            if ((bool) $service->problem === true && (bool) $service->handled === false) {
                $unhandled = true;
            }

            $stateName = 'services_' . $service::getStateText($service->state);
            ++$serviceStates[$stateName];
            ++$serviceStates[$stateName . ($unhandled ? '_unhandled' : '_handled')];

            if (! isset($knownHostStates[$service->getHost()->getName()])) {
                $unhandledHost = (bool) $service->host_problem === true && (bool) $service->host_handled === false;
                ++$hostStates['hosts_' . $service->getHost()->getStateText($service->host_state)];
                ++$hostStates['hosts_' . $service->getHost()->getStateText($service->host_state)
                        . ($unhandledHost ? '_unhandled' : '_handled')];
                $knownHostStates[$service->getHost()->getName()] = true;
            }
        }

        $serviceStates['services_total'] = count($this);
        $this->hostStateSummary = $hostStates;
        $this->serviceStateSummary = $serviceStates;
    }

    /**
     * Return an empty array with all possible host state names
     *
     * @return array    An array containing all possible host states as keys and 0 as values.
     */
    public static function getServiceStatesSummaryEmpty()
    {
        return StringHelper::cartesianProduct(
            array(
                array('services'),
                array(
                    Service::getStateText(Service::STATE_OK),
                    Service::getStateText(Service::STATE_WARNING),
                    Service::getStateText(Service::STATE_CRITICAL),
                    Service::getStateText(Service::STATE_UNKNOWN),
                    Service::getStateText(Service::STATE_PENDING)
                ),
                array(null, 'handled', 'unhandled')
            ),
            '_'
        );
    }

    /**
     * Returns a Filter that matches all hosts in this HostList
     *
     * @param   array   $columns    Override filter column names
     *
     * @return  Filter
     */
    public function objectsFilter($columns = array('host' => 'host', 'service' => 'service'))
    {
        $filterExpression = array();
        foreach ($this as $service) {
            $filterExpression[] = Filter::matchAll(
                Filter::where($columns['host'], $service->getHost()->getName()),
                Filter::where($columns['service'], $service->getName())
            );
        }
        return FilterOr::matchAny($filterExpression);
    }

    /**
     * Get the comments
     *
     * @return \Icinga\Module\Monitoring\DataView\Hostcomment
     */
    public function getComments()
    {
        return $this->backend
            ->select()
            ->from('servicecomment', array('host_name', 'service_description'))
            ->applyFilter(clone $this->filter);
    }

    /**
     * Get the scheduled downtimes
     *
     * @return \Icinga\Module\Monitoring\DataView\Servicedowntime
     */
    public function getScheduledDowntimes()
    {
        return $this->backend
            ->select()
            ->from('servicedowntime', array('host_name', 'service_description'))
            ->applyFilter(clone $this->filter);
    }

    /**
     * @return ObjectList
     */
    public function getUnacknowledgedObjects()
    {
        $unhandledObjects = array();
        foreach ($this as $object) {
            if (! in_array((int) $object->state, array(0, 99)) &&
                  (bool) $object->service_acknowledged === false) {
                $unhandledObjects[] = $object;
            }
        }
        return $this->newFromArray($unhandledObjects);
    }
}