1 | #!/usr/bin/env php |
||
2 | <?php |
||
3 | |||
4 | /* |
||
5 | * LibreNMS module to poll Nagios Services |
||
6 | * |
||
7 | * Copyright (c) 2016 Aaron Daniels <[email protected]> |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify it |
||
10 | * under the terms of the GNU General Public License as published by the |
||
11 | * Free Software Foundation, either version 3 of the License, or (at your |
||
12 | * option) any later version. Please see LICENSE.txt at the top level of |
||
13 | * the source code distribution for details. |
||
14 | */ |
||
15 | |||
16 | use LibreNMS\Data\Store\Datastore; |
||
17 | use LibreNMS\Util\Debug; |
||
18 | |||
19 | $init_modules = []; |
||
20 | require __DIR__ . '/includes/init.php'; |
||
21 | |||
22 | $options = getopt('drfpgh:'); |
||
23 | if (Debug::set(isset($options['d']))) { |
||
24 | echo "DEBUG!\n"; |
||
25 | } |
||
26 | |||
27 | $poller_start = microtime(true); |
||
28 | |||
29 | $datastore = Datastore::init($options); |
||
30 | |||
31 | echo "Starting service polling run:\n\n"; |
||
32 | $polled_services = 0; |
||
33 | |||
34 | $where = ''; |
||
35 | $params = []; |
||
36 | if ($options['h']) { |
||
37 | if (is_numeric($options['h'])) { |
||
38 | $where = 'AND `S`.`device_id` = ?'; |
||
39 | $params[] = (int) $options['h']; |
||
40 | } else { |
||
41 | if (preg_match('/\*/', $options['h'])) { |
||
42 | $where = "AND `hostname` LIKE '?'"; |
||
43 | $params[] = str_replace('*', '%', $options['h']); |
||
44 | } else { |
||
45 | $where = "AND `hostname` = '?'"; |
||
46 | $params[] = $options['h']; |
||
47 | } |
||
48 | } |
||
49 | } |
||
50 | |||
51 | $sql = 'SELECT D.*,S.*,attrib_value FROM `devices` AS D' |
||
52 | . ' INNER JOIN `services` AS S ON S.device_id = D.device_id AND D.disabled = 0 ' . $where |
||
53 | . ' LEFT JOIN `devices_attribs` as A ON D.device_id = A.device_id AND A.attrib_type = "override_icmp_disable"' |
||
54 | . ' ORDER by D.device_id DESC;'; |
||
55 | |||
56 | foreach (dbFetchRows($sql, $params) as $service) { |
||
57 | // Run the polling function if service is enabled and the associated device is up, "Disable ICMP Test" option is not enabled, |
||
58 | // or service hostname/ip is different from associated device |
||
59 | if (! $service['service_disabled'] && ($service['status'] == 1 || ($service['status'] == 0 && $service['status_reason'] === 'snmp') || |
||
60 | $service['attrib_value'] === 'true' || ($service['service_ip'] !== $service['hostname'] && |
||
61 | $service['service_ip'] !== inet6_ntop($service['ip'])))) { |
||
62 | poll_service($service); |
||
63 | $polled_services++; |
||
64 | } else { |
||
65 | if (! $service['service_disabled']) { |
||
66 | d_echo("\nService check - " . $service['service_id'] . "\nSkipping service check because device " |
||
67 | . $service['hostname'] . " is down due to icmp.\n"); |
||
68 | Log::event( |
||
69 | "Service check - {$service['service_desc']} ({$service['service_id']}) - |
||
70 | Skipping service check because device {$service['hostname']} is down due to icmp", |
||
71 | $service['device_id'], |
||
72 | 'service', |
||
73 | 4, |
||
74 | $service['service_id'] |
||
75 | ); |
||
76 | } else { |
||
77 | d_echo("\nService check - " . $service['service_id'] . "\nSkipping service check because device " |
||
78 | . $service['service_type'] . " is disabled.\n"); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | $poller_end = microtime(true); |
||
84 | $poller_run = ($poller_end - $poller_start); |
||
85 | $poller_time = substr($poller_run, 0, 5); |
||
86 | |||
87 | $string = $argv[0] . ' ' . date(\LibreNMS\Config::get('dateformat.compact')) |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
88 | . " - $polled_services services polled in $poller_time secs"; |
||
89 | d_echo("$string\n"); |
||
90 | |||
91 | Datastore::terminate(); |
||
92 |