service.class.php 11 KB
Newer Older
1
<?php
2
/*
3
4
 * Copyright 2005-2019 Centreon
 * Centreon is developed by : Julien Mathis and Romain Le Merlus under
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
 * GPL Licence 2.0.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation ; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses>.
 *
 * Linking this program statically or dynamically with other modules is making a
 * combined work based on this program. Thus, the terms and conditions of the GNU
 * General Public License cover the whole combination.
 *
 * As a special exception, the copyright holders of this program give Centreon
 * permission to link this program with independent modules to produce an executable,
 * regardless of the license terms of these independent modules, and to copy and
 * distribute the resulting executable under terms of Centreon choice, provided that
 * Centreon also meet, for each linked independent module, the terms  and conditions
 * of the license of that module. An independent module is a module which is not
 * derived from this program. If you modify this program, you may extend this
 * exception to your version of the program, but you are not obliged to do so. If you
 * do not wish to do so, delete this exception statement from your version.
 *
 * For more information : contact@centreon.com
 *
 */
35
36
37

require_once dirname(__FILE__) . '/object.class.php';

loiclau's avatar
loiclau committed
38
39
abstract class AbstractService extends AbstractObject
{
40
    // no flap_detection_options attribute
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
    protected $attributes_select = '
        service_id,
        service_template_model_stm_id,
        command_command_id as check_command_id,
        command_command_id_arg as check_command_arg,
        timeperiod_tp_id as check_period_id,
        timeperiod_tp_id2 as notification_period_id,
        command_command_id2 as event_handler_id,
        command_command_id_arg2 as event_handler_arg,
        service_description,
        service_alias as name,
        display_name,
        service_is_volatile as is_volatile,
        service_max_check_attempts as max_check_attempts,
        service_normal_check_interval as check_interval,
        service_retry_check_interval as retry_interval,
        service_active_checks_enabled as active_checks_enabled,
        service_passive_checks_enabled as passive_checks_enabled,
        initial_state,
        service_obsess_over_service as obsess_over_service,
        service_check_freshness as check_freshness,
        service_freshness_threshold as freshness_threshold,
        service_event_handler_enabled as event_handler_enabled,
        service_low_flap_threshold as low_flap_threshold,
        service_high_flap_threshold as high_flap_threshold,
        service_flap_detection_enabled as flap_detection_enabled,
        service_process_perf_data as process_perf_data,
        service_retain_status_information as retain_status_information,
        service_retain_nonstatus_information as retain_nonstatus_information,
        service_notification_interval as notification_interval,
        service_notification_options as notification_options,
        service_notifications_enabled as notifications_enabled,
        contact_additive_inheritance,
74
        service_use_only_contacts_from_host,
75
76
        cg_additive_inheritance,
        service_first_notification_delay as first_notification_delay,
77
        service_recovery_notification_delay as recovery_notification_delay,
78
79
80
81
82
83
        service_stalking_options as stalking_options,
        service_register as register,
        esi_notes as notes,
        esi_notes_url as notes_url,
        esi_action_url as action_url,
        esi_icon_image as icon_image_id,
84
85
        esi_icon_image_alt as icon_image_alt,
        service_acknowledgement_timeout as acknowledgement_timeout
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    ';
    protected $attributes_write = array(
        'host_name',
        'service_description',
        'display_name',
        'contacts',
        'contact_groups',
        'check_command',
        'check_period',
        'notification_period',
        'event_handler',
        'max_check_attempts',
        'check_interval',
        'retry_interval',
        'initial_state',
        'freshness_threshold',
        'low_flap_threshold',
        'high_flap_threshold',
        'flap_detection_options',
        'notification_interval',
        'notification_options',
        'first_notification_delay',
108
        'recovery_notification_delay',
109
110
111
112
113
114
        'stalking_options',
        'register',
        'notes',
        'notes_url',
        'action_url',
        'icon_image',
115
        'icon_id',
116
117
        'icon_image_alt',
        'acknowledgement_timeout'
118
119
    );
    protected $attributes_default = array(
120
        'is_volatile',
121
122
123
124
125
126
127
128
129
130
131
132
133
        'active_checks_enabled',
        'passive_checks_enabled',
        'event_handler_enabled',
        'flap_detection_enabled',
        'notifications_enabled',
        'obsess_over_service',
        'check_freshness',
        'process_perf_data',
        'retain_status_information',
        'retain_nonstatus_information',
    );
    protected $attributes_array = array(
        'use',
134
        'category_tags',
135
136
137
138
139
140
141
142
143
    );
    protected $attributes_hash = array(
        'macros'
    );
    protected $loop_stpl = array(); # To be reset
    protected $stmt_macro = null;
    protected $stmt_stpl = null;
    protected $stmt_contact = null;
    protected $stmt_service = null;
loiclau's avatar
loiclau committed
144
145
146

    protected function getImages(&$service)
    {
Loic Laurent's avatar
Loic Laurent committed
147
        $media = Media::getInstance($this->dependencyInjector);
148
149
        if (!isset($service['icon_image'])) {
            $service['icon_image'] = $media->getMediaPathFromId($service['icon_image_id']);
150
            $service['icon_id'] = $service['icon_image_id'];
151
152
        }
    }
loiclau's avatar
loiclau committed
153
154
155

    protected function getMacros(&$service)
    {
156
157
158
        if (isset($service['macros'])) {
            return 1;
        }
loiclau's avatar
loiclau committed
159

160
        $service['macros'] = Macro::getInstance($this->dependencyInjector)
loiclau's avatar
loiclau committed
161
            ->getServiceMacroByServiceId($service['service_id']);
162
163
        return 0;
    }
loiclau's avatar
loiclau committed
164
165
166
167
168
169
170

    protected function getServiceTemplates(&$service)
    {
        $service['use'] = array(
            ServiceTemplate::getInstance($this->dependencyInjector)
                ->generateFromServiceId($service['service_template_model_stm_id'])
        );
171
    }
loiclau's avatar
loiclau committed
172

173
    /**
174
     * @param array $service (passing by Reference)
175
176
     */
    protected function getContacts(array &$service): void
loiclau's avatar
loiclau committed
177
    {
178
179
180
        if (!isset($service['contacts_cache'])) {
            $contact = Contact::getInstance($this->dependencyInjector);
            $service['contacts_cache'] = $contact->getContactForService($service['service_id']);
181
182
183
184
        }
    }

    /**
185
      * @param array $service (passing by Reference)
186
     */
187
    protected function getContactGroups(array &$service): void
188
    {
189
190
191
        if (!isset($service['contact_groups_cache'])) {
            $cg = Contactgroup::getInstance($this->dependencyInjector);
            $service['contact_groups_cache'] = $cg->getCgForService($service['service_id']);
192
        }
193
    }
loiclau's avatar
loiclau committed
194
195
196

    protected function findCommandName($service_id, $command_label)
    {
197
        $loop = array();
loiclau's avatar
loiclau committed
198

Loic Laurent's avatar
Loic Laurent committed
199
        $services_tpl = ServiceTemplate::getInstance($this->dependencyInjector)->service_cache;
loiclau's avatar
loiclau committed
200
201
202
        $service_id = isset($this->service_cache[$service_id]['service_template_model_stm_id'])
            ? $this->service_cache[$service_id]['service_template_model_stm_id']
            : null;
203
204
205
206
207
        while (!is_null($service_id)) {
            if (isset($loop[$service_id])) {
                break;
            }
            $loop[$service_id] = 1;
loiclau's avatar
loiclau committed
208
209
210
            if (isset($services_tpl[$service_id][$command_label]) &&
                !is_null($services_tpl[$service_id][$command_label])
            ) {
211
212
                return $services_tpl[$service_id][$command_label];
            }
loiclau's avatar
loiclau committed
213
214
215
            $service_id = isset($services_tpl[$service_id]['service_template_model_stm_id'])
                ? $services_tpl[$service_id]['service_template_model_stm_id']
                : null;
216
        }
loiclau's avatar
loiclau committed
217

218
219
        return null;
    }
loiclau's avatar
loiclau committed
220
221
222
223
224

    protected function getServiceCommand(&$service, $result_name, $command_id_label, $command_arg_label)
    {
        $command_name = Command::getInstance($this->dependencyInjector)
            ->generateFromCommandId($service[$command_id_label]);
225
        $command_arg = '';
loiclau's avatar
loiclau committed
226

227
228
229
230
        if (isset($service[$result_name])) {
            return 1;
        }
        $service[$result_name] = $command_name;
loiclau's avatar
loiclau committed
231
232
233
234
        if (isset($service[$command_arg_label]) &&
            !is_null($service[$command_arg_label]) &&
            $service[$command_arg_label] != ''
        ) {
235
236
237
238
239
240
241
242
243
244
245
246
247
            $command_arg = $service[$command_arg_label];
            if (is_null($command_name)) {
                # Find Command Name in templates
                $command_name = $this->findCommandName($service['service_id'], $result_name);
                # Can have 'args after'. We replace
                if (!is_null($command_name)) {
                    $command_name = preg_replace('/!.*/', '', $command_name);
                    $service[$result_name] = $command_name . $command_arg;
                }
            } else {
                $service[$result_name] = $command_name . $command_arg;
            }
        }
loiclau's avatar
loiclau committed
248

249
250
        return 0;
    }
loiclau's avatar
loiclau committed
251
252
253
254

    protected function getServiceCommands(&$service)
    {
        $this->getServiceCommand($service, 'check_command', 'check_command_id', 'check_command_arg');
255
256
        $this->getServiceCommand($service, 'event_handler', 'event_handler_id', 'event_handler_arg');
    }
loiclau's avatar
loiclau committed
257
258
259

    protected function getServicePeriods(&$service)
    {
Loic Laurent's avatar
Loic Laurent committed
260
        $period = Timeperiod::getInstance($this->dependencyInjector);
261
262
263
        $service['check_period'] = $period->generateFromTimeperiodId($service['check_period_id']);
        $service['notification_period'] = $period->generateFromTimeperiodId($service['notification_period_id']);
    }
loiclau's avatar
loiclau committed
264
265
266

    public function getString($service_id, $attr)
    {
267
268
269
270
271
        if (isset($this->service_cache[$service_id][$attr])) {
            return $this->service_cache[$service_id][$attr];
        }
        return null;
    }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

    /**
     * @param ServiceCategory $serviceCategory
     * @param int $serviceId
     */
    protected function insertServiceInServiceCategoryMembers(ServiceCategory $serviceCategory, int $serviceId): void
    {
        $this->service_cache[$serviceId]['serviceCategories'] =
            $serviceCategory->getServiceCategoriesByServiceId($serviceId);

        foreach ($this->service_cache[$serviceId]['serviceCategories'] as $serviceCategoryId) {
            if (! is_null($serviceCategoryId)) {
                $serviceCategory->insertServiceToServiceCategoryMembers(
                    $serviceCategoryId,
                    $serviceId,
                    $this->service_cache[$serviceId]['service_description']
                );
            }
        }
    }
292
}