service.class.php 11.5 KB
Newer Older
1
<?php
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
/*
 * Copyright 2005-2015 Centreon
 * Centreon is developped by : Julien Mathis and Romain Le Merlus under
 * 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
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

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

abstract class AbstractService extends AbstractObject {
    # no flap_detection_options attribute
    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,
73
        service_use_only_contacts_from_host,
74
75
        cg_additive_inheritance,
        service_first_notification_delay as first_notification_delay,
76
        service_recovery_notification_delay as recovery_notification_delay,
77
78
79
80
81
82
83
        service_stalking_options as stalking_options,
        service_register as register,
        service_inherit_contacts_from_host,
        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
116
        'icon_image_alt',
        'acknowledgement_timeout'
117
118
    );
    protected $attributes_default = array(
119
        'is_volatile',
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
        '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',
    );
    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;
    
    protected function getImages(&$service) {
        $media = Media::getInstance();
        if (!isset($service['icon_image'])) {
            $service['icon_image'] = $media->getMediaPathFromId($service['icon_image_id']);
        }
    }
    
    protected function getMacros(&$service) {
        if (isset($service['macros'])) {
            return 1;
        }
        
        $service['macros'] = &Macro::getInstance()->getServiceMacroByServiceId($service['service_id']);
        return 0;
    }
    
    protected function getServiceTemplates(&$service) {        
        $service['use'] = array(ServiceTemplate::getInstance()->generateFromServiceId($service['service_template_model_stm_id']));
    }
    
163
    protected function getContacts(&$service) {
164
165
        if (isset($service['service_use_only_contacts_from_host']) && $service['service_use_only_contacts_from_host'] == 1) {
            $service['contacts_cache'] = array();
166
167
168
169
170
171
172
173
174
175
176
177
            $service['contacts'] = "";
        } else {
            $contact = Contact::getInstance();
            $service['contacts_cache'] = &$contact->getContactForService($service['service_id']);
            $contact_result = '';
            $contact_result_append = '';
            foreach ($service['contacts_cache'] as $contact_id) {
                $tmp = $contact->generateFromContactId($contact_id);
                if (!is_null($tmp)) {
                    $contact_result .= $contact_result_append . $tmp;
                    $contact_result_append = ',';
                }
178
            }
179
180
181
182
183
184

            if ($contact_result != '') {
                $service['contacts'] = $contact_result;
                if (!is_null($service['contact_additive_inheritance']) && $service['contact_additive_inheritance'] == 1) {
                    $service['contacts'] = '+' . $service['contacts'];
                }
185
        }
186
187
188
        }
    }
    
189
    protected function getContactGroups(&$service)
190
191
192
    {        
         if (isset($service['service_use_only_contacts_from_host']) && $service['service_use_only_contacts_from_host'] == 1) {
            $service['contact_groups_cache'] = array();
193
194
195
196
197
198
199
200
201
202
203
204
            $service['contact_groups'] = "";
        } else {
            $cg = Contactgroup::getInstance();
            $service['contact_groups_cache'] = &$cg->getCgForService($service['service_id']);
            $cg_result = '';
            $cg_result_append = '';
            foreach ($service['contact_groups_cache'] as $cg_id) {
                $tmp = $cg->generateFromCgId($cg_id);
                if (!is_null($tmp)) {
                    $cg_result .= $cg_result_append . $tmp;
                    $cg_result_append = ',';
                }
205
            }
206
207
208
209
210
211

            if ($cg_result != '') {
                $service['contact_groups'] = $cg_result;
                if (!is_null($service['cg_additive_inheritance']) && $service['cg_additive_inheritance'] == 1) {
                    $service['contact_groups'] = '+' . $service['contact_groups'];
                }
212
        }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
        }
    }
    
    protected function findCommandName($service_id, $command_label) {
        $loop = array();
        
        $services_tpl = ServiceTemplate::getInstance()->service_cache;
        $service_id = isset($this->service_cache[$service_id]['service_template_model_stm_id']) ? $this->service_cache[$service_id]['service_template_model_stm_id'] : null;
        while (!is_null($service_id)) {
            if (isset($loop[$service_id])) {
                break;
            }
            $loop[$service_id] = 1;
            if (isset($services_tpl[$service_id][$command_label]) && !is_null($services_tpl[$service_id][$command_label])) {
                return $services_tpl[$service_id][$command_label];
            }
            $service_id = isset($services_tpl[$service_id]['service_template_model_stm_id']) ? $services_tpl[$service_id]['service_template_model_stm_id'] : null;
        }
        
        return null;
    }
    
    protected function getServiceCommand(&$service, $result_name, $command_id_label, $command_arg_label) {
        $command_name = Command::getInstance()->generateFromCommandId($service[$command_id_label]);
        $command_arg = '';
        
        if (isset($service[$result_name])) {
            return 1;
        }
        $service[$result_name] = $command_name;
        if (isset($service[$command_arg_label]) && !is_null($service[$command_arg_label]) && $service[$command_arg_label] != '') {
            $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;
            }
        }
        
        return 0;
    }
    
    protected function getServiceCommands(&$service) {
        $this->getServiceCommand($service, 'check_command', 'check_command_id', 'check_command_arg');        
        $this->getServiceCommand($service, 'event_handler', 'event_handler_id', 'event_handler_arg');
    }
    
    protected function getServicePeriods(&$service) {
        $period = Timeperiod::getInstance();
        $service['check_period'] = $period->generateFromTimeperiodId($service['check_period_id']);
        $service['notification_period'] = $period->generateFromTimeperiodId($service['notification_period_id']);
    }
    
    public function getString($service_id, $attr) {
        if (isset($this->service_cache[$service_id][$attr])) {
            return $this->service_cache[$service_id][$attr];
        }
        return null;
    }
}