1 | #!/usr/bin/env php |
||
2 | <?php |
||
3 | /** |
||
4 | * poller.php |
||
5 | * |
||
6 | * -Description- |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
20 | * |
||
21 | * @copyright (C) 2006 - 2012 Adam Armstrong |
||
22 | * |
||
23 | * Modified 4/17/19 |
||
24 | * @author Heath Barnhart <[email protected]> |
||
25 | */ |
||
26 | |||
27 | use LibreNMS\Alert\AlertRules; |
||
28 | use LibreNMS\Config; |
||
0 ignored issues
–
show
|
|||
29 | use LibreNMS\Data\Store\Datastore; |
||
30 | use LibreNMS\Util\Debug; |
||
31 | |||
32 | $init_modules = ['polling', 'alerts', 'laravel']; |
||
33 | require __DIR__ . '/includes/init.php'; |
||
34 | |||
35 | $poller_start = microtime(true); |
||
36 | echo Config::get('project_name') . " Poller\n"; |
||
37 | |||
38 | $options = getopt('h:m:i:n:r::d::v::a::f::q'); |
||
39 | |||
40 | if (isset($options['h'])) { |
||
41 | if ($options['h'] == 'odd') { |
||
42 | $options['n'] = '1'; |
||
43 | $options['i'] = '2'; |
||
44 | } elseif ($options['h'] == 'even') { |
||
45 | $options['n'] = '0'; |
||
46 | $options['i'] = '2'; |
||
47 | } elseif ($options['h'] == 'all') { |
||
48 | $where = ' '; |
||
49 | $doing = 'all'; |
||
50 | } elseif ($options['h']) { |
||
51 | if (is_numeric($options['h'])) { |
||
52 | $where = 'AND `device_id` = ' . $options['h']; |
||
53 | $doing = $options['h']; |
||
54 | } else { |
||
55 | if (preg_match('/\*/', $options['h'])) { |
||
56 | $where = "AND `hostname` LIKE '" . str_replace('*', '%', $options['h']) . "'"; |
||
57 | } else { |
||
58 | $where = "AND `hostname` = '" . $options['h'] . "'"; |
||
59 | } |
||
60 | $doing = $options['h']; |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | |||
65 | if (isset($options['i']) && $options['i'] && isset($options['n'])) { |
||
66 | $where = true; |
||
67 | // FIXME |
||
68 | $query = 'SELECT * FROM (SELECT @rownum :=0) r, |
||
69 | ( |
||
70 | SELECT @rownum := @rownum +1 AS rownum, `devices`.* |
||
71 | FROM `devices` |
||
72 | WHERE `disabled` = 0 |
||
73 | ORDER BY `device_id` ASC |
||
74 | ) temp |
||
75 | WHERE MOD(temp.rownum, ' . $options['i'] . ') = ' . $options['n'] . ';'; |
||
76 | $doing = $options['n'] . '/' . $options['i']; |
||
77 | } |
||
78 | |||
79 | if (empty($where)) { |
||
80 | echo "-h <device id> | <device hostname wildcard> Poll single device\n"; |
||
81 | echo "-h odd Poll odd numbered devices (same as -i 2 -n 0)\n"; |
||
82 | echo "-h even Poll even numbered devices (same as -i 2 -n 1)\n"; |
||
83 | echo "-h all Poll all devices\n\n"; |
||
84 | echo "-i <instances> -n <number> Poll as instance <number> of <instances>\n"; |
||
85 | echo " Instances start at 0. 0-3 for -n 4\n\n"; |
||
86 | echo "Debugging and testing options:\n"; |
||
87 | echo "-r Do not create or update RRDs\n"; |
||
88 | echo "-f Do not insert data into InfluxDB\n"; |
||
89 | echo "-p Do not insert data into Prometheus\n"; |
||
90 | echo "-d Enable debugging output\n"; |
||
91 | echo "-v Enable verbose debugging output\n"; |
||
92 | echo "-m Specify module(s) to be run. Comma separate modules, submodules may be added with /\n"; |
||
93 | echo "\n"; |
||
94 | echo "No polling type specified!\n"; |
||
95 | exit; |
||
96 | } |
||
97 | |||
98 | if (Debug::set(isset($options['d']), false) || isset($options['v'])) { |
||
99 | $versions = version_info(); |
||
100 | echo <<<EOH |
||
101 | =================================== |
||
102 | Version info: |
||
103 | Commit SHA: {$versions['local_sha']} |
||
104 | Commit Date: {$versions['local_date']} |
||
105 | DB Schema: {$versions['db_schema']} |
||
106 | PHP: {$versions['php_ver']} |
||
107 | MySQL: {$versions['mysql_ver']} |
||
108 | RRDTool: {$versions['rrdtool_ver']} |
||
109 | SNMP: {$versions['netsnmp_ver']} |
||
110 | ================================== |
||
111 | EOH; |
||
112 | |||
113 | echo "DEBUG!\n"; |
||
114 | if (isset($options['v'])) { |
||
115 | Debug::setVerbose(); |
||
116 | } |
||
117 | \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache |
||
118 | } |
||
119 | |||
120 | // If we've specified modules with -m, use them |
||
121 | $module_override = parse_modules('poller', $options); |
||
122 | |||
123 | $datastore = Datastore::init($options); |
||
124 | |||
125 | echo "Starting polling run:\n\n"; |
||
126 | $polled_devices = 0; |
||
127 | $unreachable_devices = 0; |
||
128 | if (! isset($query)) { |
||
129 | $query = "SELECT * FROM `devices` WHERE `disabled` = 0 $where ORDER BY `device_id` ASC"; |
||
130 | } |
||
131 | |||
132 | foreach (dbFetch($query) as $device) { |
||
133 | DeviceCache::setPrimary($device['device_id']); |
||
134 | |||
135 | if (! poll_device($device, $module_override)) { |
||
136 | $unreachable_devices++; |
||
137 | } |
||
138 | |||
139 | // Update device_groups |
||
140 | echo "### Start Device Groups ###\n"; |
||
141 | $dg_start = microtime(true); |
||
142 | $group_changes = \App\Models\DeviceGroup::updateGroupsFor($device['device_id']); |
||
143 | d_echo('Groups Added: ' . implode(',', $group_changes['attached']) . PHP_EOL); |
||
144 | d_echo('Groups Removed: ' . implode(',', $group_changes['detached']) . PHP_EOL); |
||
145 | echo '### End Device Groups, runtime: ' . round(microtime(true) - $dg_start, 4) . "s ### \n\n"; |
||
146 | |||
147 | echo "#### Start Alerts ####\n"; |
||
148 | $rules = new AlertRules(); |
||
149 | $rules->runRules($device['device_id']); |
||
150 | echo "#### End Alerts ####\r\n"; |
||
151 | $polled_devices++; |
||
152 | } |
||
153 | |||
154 | $poller_end = microtime(true); |
||
155 | $poller_run = ($poller_end - $poller_start); |
||
156 | $poller_time = substr($poller_run, 0, 5); |
||
157 | |||
158 | $string = $argv[0] . " $doing " . date(Config::get('dateformat.compact')) . " - $polled_devices devices polled in $poller_time secs"; |
||
159 | d_echo("$string\n"); |
||
160 | |||
161 | if (! isset($options['q'])) { |
||
162 | printStats(); |
||
163 | } |
||
164 | |||
165 | logfile($string); |
||
166 | Datastore::terminate(); |
||
167 | // Remove this for testing |
||
168 | // print_r(get_defined_vars()); |
||
169 | |||
170 | if ($polled_devices === $unreachable_devices) { |
||
171 | exit(6); |
||
172 | } |
||
173 | |||
174 | exit(0); |
||
175 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: