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

namespace Icinga\Module\Monitoring\Object;

use Icinga\Data\Filter\FilterEqual;
use Icinga\Module\Monitoring\DataView\Servicestatus;
use InvalidArgumentException;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;

/**
 * An Icinga service
 */
class Service extends MonitoredObject
{
    /**
     * Service state 'OK'
     */
    const STATE_OK = 0;

    /**
     * Service state 'WARNING'
     */
    const STATE_WARNING = 1;

    /**
     * Service state 'CRITICAL'
     */
    const STATE_CRITICAL = 2;

    /**
     * Service state 'UNKNOWN'
     */
    const STATE_UNKNOWN = 3;

    /**
     * Service state 'PENDING'
     */
    const STATE_PENDING = 99;

    /**
     * Type of the Icinga service
     *
     * @var string
     */
    public $type = self::TYPE_SERVICE;

    /**
     * Prefix of the Icinga service
     *
     * @var string
     */
    public $prefix = 'service_';

    /**
     * Host the service is running on
     *
     * @var Host
     */
    protected $host;

    /**
     * Service name
     *
     * @var string
     */
    protected $service;

    /**
     * Create a new service
     *
     * @param MonitoringBackend $backend    Backend to fetch service information from
     * @param string            $host       Hostname the service is running on
     * @param string            $service    Service name
     */
    public function __construct(MonitoringBackend $backend, $host, $service)
    {
        parent::__construct($backend);
        $this->host = new Host($backend, $host);
        $this->service = $service;
    }

    /**
     * Get the host the service is running on
     *
     * @return Host
     */
    public function getHost()
    {
        return $this->host;
    }

    /**
     * Get the service name
     *
     * @return string
     */
    public function getName()
    {
        return $this->service;
    }

    /**
     * Get the data view
     *
     * @return Servicestatus
     */
    protected function getDataView()
    {
        return $this->backend->select()->from('servicestatus', array(
            'instance_name',
            'host_attempt',
            'host_icon_image',
            'host_icon_image_alt',
            'host_acknowledged',
            'host_active_checks_enabled',
            'host_address',
            'host_address6',
            'host_alias',
            'host_display_name',
            'host_handled',
            'host_in_downtime',
            'host_is_flapping',
            'host_last_state_change',
            'host_name',
            'host_notifications_enabled',
            'host_passive_checks_enabled',
            'host_state',
            'host_state_type',
            'service_icon_image',
            'service_icon_image_alt',
            'service_acknowledged',
            'service_acknowledgement_type',
            'service_action_url',
            'service_active_checks_enabled',
            'service_active_checks_enabled_changed',
            'service_attempt',
            'service_check_command',
            'service_check_execution_time',
            'service_check_interval',
            'service_check_latency',
            'service_check_source',
            'service_check_timeperiod',
            'service_current_notification_number',
            'service_description',
            'service_display_name',
            'service_event_handler_enabled',
            'service_event_handler_enabled_changed',
            'service_flap_detection_enabled',
            'service_flap_detection_enabled_changed',
            'service_handled',
            'service_in_downtime',
            'service_is_flapping',
            'service_is_reachable',
            'service_last_check',
            'service_last_notification',
            'service_last_state_change',
            'service_long_output',
            'service_next_check',
            'service_next_update',
            'service_notes',
            'service_notes_url',
            'service_notifications_enabled',
            'service_notifications_enabled_changed',
            'service_obsessing',
            'service_obsessing_changed',
            'service_output',
            'service_passive_checks_enabled',
            'service_passive_checks_enabled_changed',
            'service_percent_state_change',
            'service_perfdata',
            'service_process_perfdata' => 'service_process_performance_data',
            'service_state',
            'service_state_type'
        ))
            ->whereEx(new FilterEqual('host_name', '=', $this->host->getName()))
            ->whereEx(new FilterEqual('service_description', '=', $this->service));
    }

    /**
     * Get the optional translated textual representation of a service state
     *
     * @param   int     $state
     * @param   bool    $translate
     *
     * @return  string
     * @throws  InvalidArgumentException If the service state is not valid
     */
    public static function getStateText($state, $translate = false)
    {
        $translate = (bool) $translate;
        switch ((int) $state) {
            case self::STATE_OK:
                $text = $translate ? mt('monitoring', 'OK') : 'ok';
                break;
            case self::STATE_WARNING:
                $text = $translate ? mt('monitoring', 'WARNING') : 'warning';
                break;
            case self::STATE_CRITICAL:
                $text = $translate ? mt('monitoring', 'CRITICAL') : 'critical';
                break;
            case self::STATE_UNKNOWN:
                $text = $translate ? mt('monitoring', 'UNKNOWN') : 'unknown';
                break;
            case self::STATE_PENDING:
                $text = $translate ? mt('monitoring', 'PENDING') : 'pending';
                break;
            default:
                throw new InvalidArgumentException(sprintf('Invalid service state \'%s\'', $state));
        }
        return $text;
    }

    public function getNotesUrls()
    {
        return $this->resolveAllStrings(
            MonitoredObject::parseAttributeUrls($this->service_notes_url)
        );
    }
}