host.class.php 22.8 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

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

38
39
abstract class AbstractHost extends AbstractObject
{
40
41
42
43
44

    const VERTICAL_NOTIFICATION = 1;
    // CLOSE_NOTIFICATION = 2
    const CUMULATIVE_NOTIFICATION = 3;

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
    protected $attributes_select = '
        host_id,
        command_command_id as check_command_id,
        command_command_id_arg1 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,
        host_name,
        host_alias as alias,
        host_address as address,
        display_name,
        host_max_check_attempts as max_check_attempts,
        host_check_interval as check_interval,
        host_retry_check_interval as retry_interval,
        host_active_checks_enabled as active_checks_enabled,
        host_passive_checks_enabled as passive_checks_enabled,
        initial_state,
        host_obsess_over_host as obsess_over_host,
        host_check_freshness as check_freshness,
        host_freshness_threshold as freshness_threshold,
        host_event_handler_enabled as event_handler_enabled,
        host_low_flap_threshold as low_flap_threshold,
        host_high_flap_threshold as high_flap_threshold,
        host_flap_detection_enabled as flap_detection_enabled,
        flap_detection_options,
        host_process_perf_data as process_perf_data,
        host_retain_status_information as retain_status_information,
        host_retain_nonstatus_information as retain_nonstatus_information,
        host_notification_interval as notification_interval,
        host_notification_options as notification_options,
        host_notifications_enabled as notifications_enabled,
        contact_additive_inheritance,
        cg_additive_inheritance,
        host_first_notification_delay as first_notification_delay,
80
        host_recovery_notification_delay as recovery_notification_delay,
81
82
83
84
85
86
87
88
89
90
        host_stalking_options as stalking_options,
        host_snmp_community,
        host_snmp_version,
        host_register as register,
        ehi_notes as notes,
        ehi_notes_url as notes_url,
        ehi_action_url as action_url,
        ehi_icon_image as icon_image_id,
        ehi_icon_image_alt as icon_image_alt,
        ehi_statusmap_image as statusmap_image_id,
91
92
        host_location,
        host_acknowledgement_timeout as acknowledgement_timeout
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    ';
    protected $attributes_write = array(
        'host_name',
        'alias',
        'address',
        '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',
116
        'recovery_notification_delay',
117
118
119
120
121
122
123
124
        'stalking_options',
        'register',
        'notes',
        'notes_url',
        'action_url',
        'icon_image',
        'icon_image_alt',
        'statusmap_image',
125
126
        'timezone',
        'acknowledgement_timeout'
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    );
    protected $attributes_default = array(
        'active_checks_enabled',
        'passive_checks_enabled',
        'event_handler_enabled',
        'flap_detection_enabled',
        'notifications_enabled',
        'obsess_over_host',
        'check_freshness',
        'process_perf_data',
        'retain_status_information',
        'retain_nonstatus_information',
    );
    protected $attributes_array = array(
        'use',
        'parents',
    );
    protected $attributes_hash = array(
        'macros'
    );
    protected $loop_htpl = array(); # To be reset
    protected $stmt_macro = null;
    protected $stmt_htpl = null;
    protected $stmt_contact = null;
151
    protected $notificationOption = null;
152
    protected $stmt_cg = null;
153
154
155

    protected function getImages(&$host)
    {
Loic Laurent's avatar
Loic Laurent committed
156
        $media = Media::getInstance($this->dependencyInjector);
157
158
159
160
161
162
163
        if (!isset($host['icon_image'])) {
            $host['icon_image'] = $media->getMediaPathFromId($host['icon_image_id']);
        }
        if (!isset($host['statusmap_image'])) {
            $host['statusmap_image'] = $media->getMediaPathFromId($host['statusmap_image_id']);
        }
    }
164
165
166

    protected function getMacros(&$host)
    {
167
168
169
        if (isset($host['macros'])) {
            return 1;
        }
170

171
172
173
174
175
176
177
178
179
180
        if (is_null($this->stmt_macro)) {
            $this->stmt_macro = $this->backend_instance->db->prepare("SELECT 
              host_macro_name, host_macro_value
            FROM on_demand_macro_host
            WHERE host_host_id = :host_id
            ");
        }
        $this->stmt_macro->bindParam(':host_id', $host['host_id'], PDO::PARAM_INT);
        $this->stmt_macro->execute();
        $macros = $this->stmt_macro->fetchAll(PDO::FETCH_ASSOC);
181

182
183
        $host['macros'] = array();
        foreach ($macros as $macro) {
184
185
186
187
188
            $host['macros'][preg_replace(
                '/\$_HOST(.*)\$/',
                '_$1',
                $macro['host_macro_name']
            )] = $macro['host_macro_value'];
189
190
191
192
        }
        if (!is_null($host['host_snmp_community']) && $host['host_snmp_community'] != '') {
            $host['macros']['_SNMPCOMMUNITY'] = $host['host_snmp_community'];
        }
193
        if (!is_null($host['host_snmp_version']) && $host['host_snmp_version'] != 0) {
194
195
            $host['macros']['_SNMPVERSION'] = $host['host_snmp_version'];
        }
196

197
198
        return 0;
    }
199
200
201

    protected function getHostTemplates(&$host)
    {
202
203
204
205
206
207
208
209
        if (!isset($host['htpl'])) {
            if (is_null($this->stmt_htpl)) {
                $this->stmt_htpl = $this->backend_instance->db->prepare("SELECT 
                    host_tpl_id
                FROM host_template_relation
                WHERE host_host_id = :host_id
                ORDER BY `order` ASC
                ");
210
            }
211
212
213
214
            $this->stmt_htpl->bindParam(':host_id', $host['host_id'], PDO::PARAM_INT);
            $this->stmt_htpl->execute();
            $host['htpl'] = $this->stmt_htpl->fetchAll(PDO::FETCH_COLUMN);
        }
215

Loic Laurent's avatar
Loic Laurent committed
216
        $host_template = HostTemplate::getInstance($this->dependencyInjector);
217
218
219
220
221
        $host['use'] = array();
        foreach ($host['htpl'] as $template_id) {
            $host['use'][] = $host_template->generateFromHostId($template_id);
        }
    }
222

223
224
225
226
227
228
    /**
     * Get contacts list for the configuration file by host
     *
     * @param array $host
     */
    protected function getContacts(array &$host): void
229
    {
230
231
232
233
234
        $contactResult = '';
        $hostListing = $this->listHostsWithContacts($host);
        //check if we have Host link to a contact.
        if (!empty($hostListing)) {
            $contactResult = implode(',', $this->getInheritanceContact(array_unique($hostListing)));
235
        }
236
237
        $host['contacts'] = $contactResult;
    }
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
    /**
     * Get the tree of hosts with contact according to the inheritance notification option
     *
     * @param array $host
     * @return array
     */
    public function listHostsWithContacts(array $host): array
    {
        //check notification mode
        if (is_null($this->notificationOption)) {
            $this->notificationOption = (int)$this->getInheritanceMode();
        }
        $hostListing = array();
        //check cumulative option
        if (self::CUMULATIVE_NOTIFICATION === $this->notificationOption) {
            // get all host / template inheritance
            $this->getCumulativeInheritance($host['host_id'], $hostListing);
        } else {
            // get the first host (template) link to a contact group
            // use for close and vertical inheritance mode
            $this->getContactCloseInheritance($host['host_id'], $hostListing);
            //check vertical inheritance
            if (!empty($hostListing)
                && (self::VERTICAL_NOTIFICATION === $this->notificationOption)
            ) {
                //use the first template found to start
                $startHost = $hostListing[0];
                $hostListing = array();
                $this->getContactVerticalInheritance($startHost, $hostListing);
268
269
            }
        }
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
        return $hostListing;
    }

    /**
     * Get the tree of host for vertical notification option on contact
     *
     * @param int $hostId
     * @param array $hostList
     */
    protected function getContactVerticalInheritance(int $hostId, &$hostList = array()): void
    {
        $stmt = $this->backend_instance->db->query(
            'SELECT host_notifications_enabled, contact_additive_inheritance, host_tpl_id 
            FROM host, host_template_relation
            WHERE `host_id` = `host_host_id`
            AND `order` = 1
            AND `host_activate` != "0"
            AND `host_id` = ' . $hostId
        );
        $hostAdd = $stmt->fetch();
        if ($hostAdd['host_notifications_enabled'] != '0') {
            $hostList[] = $hostId;
        }
        if (isset($hostAdd['host_tpl_id']) && (int)$hostAdd['contact_additive_inheritance'] === 1) {
            $this->getContactVerticalInheritance((int)$hostAdd['host_tpl_id'], $hostList);
        }
    }
297

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    /**
     * Get the tree of host for cumulative notification option
     *
     * @param int $hostId
     * @param array $hostList
     */
    protected function getCumulativeInheritance(int $hostId, &$hostList = array()): void
    {

        $stmt = $this->backend_instance->db->query(
            'SELECT host.host_notifications_enabled, host_template_relation.host_tpl_id
            FROM host
            LEFT JOIN host_template_relation ON host_template_relation.`host_host_id` = host.`host_id` 
            WHERE host.`host_id` = ' . $hostId . ' AND host.`host_activate` = "1"'
        );
        while (($row = $stmt->fetch())) {
            if($row['host_notifications_enabled'] != '0'){
                $hostList[] = $hostId;
            }
            if($row['host_tpl_id']){
                $this->getCumulativeInheritance((int)$row['host_tpl_id'], $hostList);
319
320
321
            }
        }
    }
322

323
324
325
326
327
328
329
    /**
     * Get the first host who have a valid notifiable contact
     *
     * @param int $hostId
     * @param array $hostList
     */
    protected function getContactCloseInheritance(int $hostId, &$hostList = array()): void
330
    {
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
        $stmt = $this->backend_instance->db->query(
            'SELECT GROUP_CONCAT(contact.contact_id) AS contact_id, 
                (SELECT GROUP_CONCAT(host_template_relation.host_tpl_id)
	            FROM host_template_relation , host
	            WHERE host_template_relation.host_host_id = ' . $hostId . '
                AND host.host_id = host_template_relation.host_host_id
	            AND host.host_activate = "1") AS host_tpl_id	
            FROM contact, contact_host_relation, host
            WHERE contact.`contact_id` = contact_host_relation.`contact_id`
            AND contact_host_relation.host_host_id = ' . $hostId . '
            AND contact.contact_enable_notifications != "0"
            AND contact.contact_activate = "1"
            AND host.host_id = contact_host_relation.host_host_id
            AND host.host_activate = "1"
            AND host.host_notifications_enabled != "0"'
        );

        if ($row = $stmt->fetch() && empty($hostList)) {
            if ($row['contact_id']) {
                $hostList[] = (int)$hostId;
            } elseif ($row['host_tpl_id']) {
                foreach (explode(',', $row['host_tpl_id']) as $hostTplId) {
                    $this->getContactCloseInheritance((int)$hostTplId, $hostList);
                }
355
356
            }
        }
357
    }
358

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    /**
     * Get enable and notifiable contact id/name of a host list
     *
     * @param array $hostIds list of host id
     * @return array
     */
    protected function getInheritanceContact(array $hostIds): array
    {
        $contact = Contact::getInstance($this->dependencyInjector);
        $contacts = array();
        $stmt = $this->backend_instance->db->query(
            'SELECT contact.contact_id , contact.contact_name 
            FROM contact, contact_host_relation 
            WHERE contact_host_relation.host_host_id IN (' . implode(',', $hostIds) . ') 
            AND contact_host_relation.contact_id = contact.contact_id 
            AND contact.contact_activate = "1" 
            AND contact.contact_enable_notifications != "0"'
        );

        while ($row = $stmt->fetch()) {
            $contacts[$row['contact_id']] = $contact->generateFromContactId($row['contact_id']);
        }
        return $contacts;
    }

    /**
     * Get contact groups list for the configuration file by host
     *
     * @param array $host
     */
    protected function getContactGroups(array &$host): void
    {
        $cgResult = '';
        $hostListing = $this->listHostsWithContactGroups($host);
        //check if we have Host link to a contactGroup.
        if (!empty($hostListing)) {
            $cgResult = implode(',', $this->getInheritanceContactGroups(array_unique($hostListing)));
        }
        $host['contact_groups'] = $cgResult;
    }

    /**
     * Get the tree of hosts with contact group according to the inheritance notification option
     *
     * @param array $host
     * @return array
     */
    public function listHostsWithContactGroups(array $host): array
    {
        //check notification mode
        if (is_null($this->notificationOption)) {
            $this->notificationOption = (int)$this->getInheritanceMode();
        }
        $hostListing = array();
        //check cumulative option
        if (self::CUMULATIVE_NOTIFICATION === $this->notificationOption) {
            // get all host / template inheritance
            $this->getCumulativeInheritance((int)$host['host_id'], $hostListing);
        } else {
            // get the first host (template) link to a contact group
            // use for close inheritance mode too
            $this->getContactGroupsCloseInheritance((int)$host['host_id'], $hostListing);
            //check vertical inheritance
            if (!empty($hostListing) && (self::VERTICAL_NOTIFICATION === $this->notificationOption)) {
                //use the first template found to start
                $startHost = (int)$hostListing[0];
                $hostListing = array();
                $this->getContactGroupsVerticalInheritance($startHost, $hostListing);
427
428
            }
        }
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
        return $hostListing;
    }

    /**
     * Get the tree of host for vertical notification option on contact group
     *
     * @param int $hostId
     * @param array $hostList
     */
    protected function getContactGroupsVerticalInheritance(int $hostId, &$hostList = array()): void
    {
        $stmt = $this->backend_instance->db->query(
            'SELECT cg_additive_inheritance, host_tpl_id, host_notifications_enabled
            FROM host, host_template_relation
            WHERE `host_id` = `host_host_id`
            AND `order` = 1
            AND `host_id` = ' . $hostId
        );
        $hostAdd = $stmt->fetch();

        if ($hostAdd['host_notifications_enabled'] != '0') {
            $hostList[] = $hostId;
        }
        if (isset($hostAdd['host_tpl_id']) && (int)$hostAdd['cg_additive_inheritance'] === 1) {
            $this->getContactGroupsVerticalInheritance((int)$hostAdd['host_tpl_id'], $hostList);
        }
    }
456

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    /**
     * Get the first host who have a valid notifiable contact group
     *
     * @param int $hostId
     * @param array $hostList
     */
    protected function getContactGroupsCloseInheritance(int $hostId, &$hostList = array()): void
    {
        $stmt = $this->backend_instance->db->query(
            'SELECT GROUP_CONCAT(contactgroup.cg_id) AS cg_id, 
                (SELECT GROUP_CONCAT(host_template_relation.host_tpl_id)
	            FROM host_template_relation , host
	            WHERE host_template_relation.host_host_id = ' . $hostId . '
                AND host.host_id = host_template_relation.host_host_id
	            AND host.host_activate = "1") as host_tpl_id	
            FROM contactgroup, contactgroup_host_relation, host
            WHERE contactgroup.`cg_id` = contactgroup_host_relation.`contactgroup_cg_id`
            AND contactgroup_host_relation.host_host_id = ' . $hostId . '
            AND contactgroup.cg_activate = "1"
            AND host.host_id = contactgroup_host_relation.host_host_id
            AND host.host_activate = "1"
            AND host.host_notifications_enabled != "0"'
        );
        if ($row = $stmt->fetch() && empty($hostList)) {
            if ($row['cg_id']) {
                $hostList[] = (int)$hostId;
            } elseif ($row['host_tpl_id']) {
                foreach (explode(',', $row['host_tpl_id']) as $hostTplId) {
                    $this->getContactGroupsCloseInheritance((int)$hostTplId, $hostList);
                }
487
488
489
            }
        }
    }
490

491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
    /**
     * Get enable contact group id/name of a host list
     *
     * @param array $hostIds List of host id
     * @return array
     */
    protected function getInheritanceContactGroups(array $hostIds): array
    {
        $cg = Contactgroup::getInstance($this->dependencyInjector);
        $contactGroups = array();
        $stmt = $this->backend_instance->db->query(
            'SELECT c.cg_id , cg_name FROM contactgroup c, contactgroup_host_relation ch
            WHERE ch.host_host_id IN (' . implode(',', $hostIds) . ') AND ch.contactgroup_cg_id = c.cg_id 
            AND cg_activate = "1"'
        );
        while (($row = $stmt->fetch())) {
            $contactGroups[$row['cg_id']] = $cg->generateFromCgId($row['cg_id']);
        }
        return $contactGroups;
    }

    /**
     * @param $host_id
     * @param $host_tpl_id
     * @return int
     */
517
518
    public function isHostTemplate($host_id, $host_tpl_id)
    {
519
520
        $loop = array();
        $stack = array();
521

Loic Laurent's avatar
Loic Laurent committed
522
        $hosts_tpl = HostTemplate::getInstance($this->dependencyInjector)->hosts;
523
524
525
526
527
528
529
530
531
532
533
        $stack = $this->hosts[$host_id]['htpl'];
        while (($host_id = array_shift($stack))) {
            if (isset($loop[$host_id])) {
                continue;
            }
            $loop[$host_id] = 1;
            if ($host_id == $host_tpl_id) {
                return 1;
            }
            $stack = array_merge($hosts_tpl[$host_id]['htpl'], $stack);
        }
534

535
536
        return 0;
    }
537
538
539

    protected function findCommandName($host_id, $command_label)
    {
540
541
        $loop = array();
        $stack = array();
542

Loic Laurent's avatar
Loic Laurent committed
543
        $hosts_tpl = HostTemplate::getInstance($this->dependencyInjector)->hosts;
544
545
546
547
548
549
550
551
552
553
554
        $stack = $this->hosts[$host_id]['htpl'];
        while (($host_id = array_shift($stack))) {
            if (isset($loop[$host_id])) {
                continue;
            }
            $loop[$host_id] = 1;
            if (isset($hosts_tpl[$host_id][$command_label]) && !is_null($hosts_tpl[$host_id][$command_label])) {
                return $hosts_tpl[$host_id][$command_label];
            }
            $stack = array_merge($hosts_tpl[$host_id]['htpl'], $stack);
        }
555

556
557
        return null;
    }
558

loiclau's avatar
loiclau committed
559
560
    protected function getHostTimezone(&$host)
    {
561
562
563
564
565
566
        $oTimezone = Timezone::getInstance($this->dependencyInjector);
        $timezone = $oTimezone->getTimezoneFromId($host['host_location']);
        if (!is_null($timezone)) {
            $host['timezone'] = ':' . $timezone;
        }
    }
loiclau's avatar
loiclau committed
567
568
569

    protected function getHostCommand(&$host, $result_name, $command_id_label, $command_arg_label)
    {
Loic Laurent's avatar
Loic Laurent committed
570
571
        $command_name = Command::getInstance($this->dependencyInjector)
            ->generateFromCommandId($host[$command_id_label]);
572
        $command_arg = '';
573

574
575
576
577
        if (isset($host[$result_name])) {
            return 1;
        }
        $host[$result_name] = $command_name;
578
        if (isset($host[$command_arg_label])
Loic Laurent's avatar
Loic Laurent committed
579
580
            && !is_null($host[$command_arg_label]) && $host[$command_arg_label] != ''
        ) {
581
582
583
584
585
586
587
588
589
590
591
592
593
            $command_arg = $host[$command_arg_label];
            if (is_null($command_name)) {
                # Find Command Name in templates
                $command_name = $this->findCommandName($host['host_id'], $result_name);
                # Can have 'args after'. We replace
                if (!is_null($command_name)) {
                    $command_name = preg_replace('/!.*/', '', $command_name);
                    $host[$result_name] = $command_name . $command_arg;
                }
            } else {
                $host[$result_name] = $command_name . $command_arg;
            }
        }
594

595
596
        return 0;
    }
597
598
599
600

    protected function getHostCommands(&$host)
    {
        $this->getHostCommand($host, 'check_command', 'check_command_id', 'check_command_arg');
601
602
        $this->getHostCommand($host, 'event_handler', 'event_handler_id', 'event_handler_arg');
    }
603
604
605

    protected function getHostPeriods(&$host)
    {
Loic Laurent's avatar
Loic Laurent committed
606
        $period = Timeperiod::getInstance($this->dependencyInjector);
607
608
609
        $host['check_period'] = $period->generateFromTimeperiodId($host['check_period_id']);
        $host['notification_period'] = $period->generateFromTimeperiodId($host['notification_period_id']);
    }
610
611
612

    public function getString($host_id, $attr)
    {
613
614
615
616
617
618
        if (isset($this->hosts[$host_id][$attr])) {
            return $this->hosts[$host_id][$attr];
        }
        return null;
    }
}