-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmactrack_resolver.php
More file actions
322 lines (263 loc) · 9.07 KB
/
mactrack_resolver.php
File metadata and controls
322 lines (263 loc) · 9.07 KB
1
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| 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, or (at your option) any later version. |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
if (function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
} else {
declare(ticks = 100);
}
ini_set('output_buffering', 'Off');
ini_set('max_runtime', '-1');
ini_set('memory_limit', '-1');
ini_set('max_execution_time', '-1');
set_time_limit(0);
ob_implicit_flush();
$dir = __DIR__;
chdir($dir);
include('../../include/cli_check.php');
include_once($config['base_path'] . '/plugins/mactrack/lib/mactrack_functions.php');
include_once($config['base_path'] . '/plugins/mactrack/Net/DNS2.php');
// install signal handlers for UNIX only
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
}
// get the mactrack polling cycle
$max_run_duration = read_config_option('mt_collection_timing');
// get the mactrack max script runtime - in minutes
$max_script_runtime = read_config_option('mt_script_runtime') * 60;
if (is_numeric($max_run_duration)) {
// let PHP a 5 minutes less than the rerun frequency
$max_run_duration = ($max_run_duration * 60) - 270;
ini_set('max_execution_time', $max_run_duration);
} else {
$max_run_duration = 3270;
ini_set('max_execution_time', $max_run_duration);
}
// establish constants
define('DEVICE_HUB_SWITCH', 1);
define('DEVICE_SWITCH_ROUTER', 2);
define('DEVICE_ROUTER', 3);
// process calling arguments
$parms = $_SERVER['argv'];
array_shift($parms);
$debug = false;
$site_id = '';
$start = time();
if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
[$arg, $value] = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-sid':
$site_id = $value;
break;
case '-d':
case '--debug':
$debug = true;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL;
display_help();
exit;
}
}
}
// check if you need to run or not
if (read_config_option('mt_reverse_dns') == 'on') {
$timeout = read_config_option('mt_dns_timeout');
$dns_primary = read_config_option('mt_dns_primary');
$dns_secondary = read_config_option('mt_dns_secondary');
$primary_down = false;
$secondary_down = false;
} else {
mactrack_debug('Exiting due to Reverse DNS being disabled');
exit;
}
// silently end if the registered process is still running
if (!register_process_start('mactrack_resolver', 'master', 0, $max_run_duration - 300)) {
print 'FATAL: Detected an already running process.' . PHP_EOL;
exit(0);
}
// place a process marker in the database for the ip resolver
db_process_add(0, true);
if ($site_id != '') {
$sql_where = 'AND site_id=' . $site_id;
} else {
$sql_where = '';
}
$nameservers = [];
if ($dns_primary != '') {
$nameservers[] = $dns_primary;
}
if ($dns_secondary != '') {
$nameservers[] = $dns_secondary;
}
if (cacti_sizeof($nameservers)) {
$use_resolver = false;
$resolver = false;
} else {
$use_resolver = true;
$resolver = new Net_DNS2_Resolver(['nameservers' => $nameservers]);
}
// if more than 15 second is nothing to do, ending
$nothing = 0;
// loop until you are it
while (1) {
// check for pending signals
pcntl_signal_dispatch();
$now = time();
$processes_running = db_fetch_cell('SELECT COUNT(*)
FROM mac_track_processes
WHERE device_id != 0');
if ((($now - $start) > ($max_script_runtime)) && ($processes_running == 0) || $nothing > 5) {
if ($nothing > 5) {
mactrack_debug('Terminating DNS resolving, nothing to do');
}
$break = true;
} else {
$break = false;
}
$unresolved_ips = db_fetch_assoc("SELECT *
FROM mac_track_temp_ports
WHERE ip_address != ''
AND (dns_hostname = '' OR dns_hostname IS NULL)");
if (cacti_sizeof($unresolved_ips) == 0) {
mactrack_debug('No IP\'s require resolving this pass');
$nothing++;
sleep(3);
} else {
$nothing = 0;
mactrack_debug(cacti_sizeof($unresolved_ips) . ' IP\'s require resolving this pass');
foreach ($unresolved_ips as $key => $unresolved_ip) {
$dns_hostname = $unresolved_ip['ip_address'];
if ($use_resolver) {
try {
$resp = $resolver->query($dns_hostname, 'PTR');
$dns_hostname = $resp->answer[0]->ptrdname;
} catch (Net_DNS2_Exception $e) {
$dns_hostname = gethostbyaddr($unresolved_ip['ip_address']);
if ($dns_hostname === false) {
mactrack_debug('Unable to resolve IP Address: ' . $unresolved_ip['ip_address']);
}
}
} else {
$dns_hostname = gethostbyaddr($unresolved_ip['ip_address']);
if ($dns_hostname === false) {
$dns_hostname = $unresolved_ip['ip_address'];
mactrack_debug('Unable to resolve IP Address: ' . $unresolved_ip['ip_address']);
}
}
$unresolved_ips[$key]['dns_hostname'] = $dns_hostname;
}
mactrack_debug('DNS host association complete.');
$sql = [];
// output updated details to database
foreach ($unresolved_ips as $unresolved_ip) {
$sql[] = '(' .
$unresolved_ip['site_id'] . ',' .
$unresolved_ip['device_id'] . ',' .
db_qstr($unresolved_ip['hostname']) . ',' .
db_qstr($unresolved_ip['dns_hostname']) . ',' .
db_qstr($unresolved_ip['device_name']) . ',' .
db_qstr($unresolved_ip['vlan_id']) . ',' .
db_qstr($unresolved_ip['vlan_name']) . ',' .
db_qstr($unresolved_ip['mac_address']) . ',' .
db_qstr($unresolved_ip['vendor_mac']) . ',' .
db_qstr($unresolved_ip['ip_address']) . ',' .
db_qstr($unresolved_ip['port_number']) . ',' .
db_qstr($unresolved_ip['port_name']) . ',' .
db_qstr($unresolved_ip['scan_date']) . ')';
}
$sql_prefix = 'INSERT INTO mac_track_temp_ports
(site_id, device_id, hostname, dns_hostname, device_name, vlan_id, vlan_name,
mac_address, vendor_mac, ip_address, port_number, port_name, scan_date) VALUES ';
$sql_suffix = ' ON DUPLICATE KEY UPDATE
site_id = VALUES(site_id),
hostname = VALUES(hostname),
dns_hostname = VALUES(dns_hostname),
device_name = VALUES(device_name),
vlan_id = VALUES(vlan_id),
vlan_name = VALUES(vlan_name),
vendor_mac = VALUES(vendor_mac),
port_name = VALUES(port_name)';
db_execute($sql_prefix . implode(', ', $sql) . $sql_suffix);
mactrack_debug('Records updated with DNS information included.');
}
if ($break) {
break;
}
}
unregister_process('mactrack_resolver', 'master');
// allow parent to close by removing process and then exit
db_process_remove(0);
exit;
/**
* sig_handler - provides a generic means to catch exceptions to the Cacti log.
*
* @param (int) $signo - the signal that was thrown by the interface.
*
* @return (void)
*/
function sig_handler($signo) {
global $config;
switch ($signo) {
case SIGTERM:
case SIGINT:
cacti_log("WARNING: MacTrack Resolver 'master' is shutting down by signal!", false, 'MACTRACK');
unregister_process('mactrack_resolver', 'master');
exit(1);
break;
default:
// ignore all other signals
}
}
function display_version() {
global $config;
$info = plugin_mactrack_version();
print 'Network Mactrack IP Resolver, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . PHP_EOL;
}
// display_help - displays the usage of the function
function display_help() {
display_version();
print PHP_EOL;
print 'usage: mactrack_resolver.php [-sid=ID] [-d] [-h] [--help] [-v] [--version]' . PHP_EOL . PHP_EOL;
print '-sid=ID - The site id to resolve for' . PHP_EOL;
print '-d | --debug - Display verbose output during execution' . PHP_EOL;
print '-v --version - Display this help message' . PHP_EOL;
print '-h --help - display this help message' . PHP_EOL;
}