forked from Cacti/plugin_mactrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmactrack_scanner.php
246 lines (217 loc) · 10.1 KB
/
mactrack_scanner.php
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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2019 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/ |
+-------------------------------------------------------------------------+
*/
$dir = dirname(__FILE__);
chdir($dir);
if (substr_count(strtolower($dir), 'mactrack')) {
chdir('../../');
}
include('./include/cli_check.php');
include_once($config['base_path'] . '/lib/snmp.php');
include_once($config['base_path'] . '/lib/ping.php');
include_once($config['base_path'] . '/plugins/mactrack/lib/mactrack_functions.php');
include_once($config['base_path'] . '/plugins/mactrack/lib/mactrack_vendors.php');
include_once($config['base_path'] . '/plugins/mactrack/mactrack_actions.php');
/* obtain some date/times for later use */
$scan_date = read_config_option('mt_scan_date');
$start_time = microtime(true);
/* drop a few environment variables to minimize net-snmp load times */
putenv('MIBS=RFC-1215');
ini_set('max_execution_time', '0');
ini_set('memory_limit', '-1');
/* 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);
global $web, $debug;
$debug = false;
$web = false;
$test_mode = false;
if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-id':
$device_id = $value;
break;
case '-d':
case '--debug':
$debug = true;
break;
case '-w':
case '--web':
$web = true;
break;
case '-t':
$test_mode = true;
exit;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit;
}
}
} else {
print "ERROR: You must supply input parameters\n\n";
display_help();
exit;
}
/* place a process marker in the database */
if (!$test_mode) {
db_process_add($device_id, true);
}
/* get device information */
$device = db_fetch_row_prepared('SELECT * FROM mac_track_devices WHERE device_id = ?', array($device_id));
if (cacti_sizeof($device) == 0) {
mactrack_debug("ERROR: Device with Id of '$device_id' not found in database. Can not continue.");
db_process_remove($device_id);
exit;
}
/* get the site name */
$site = db_fetch_cell_prepared('SELECT site_name FROM mac_track_sites WHERE site_id = ?', array($device['site_id']));
if (strlen($site) == 0) {
mactrack_debug('ERROR: Site not found in database. Can not continue.');
db_process_remove($device_id);
exit;
}
/* get device types */
$device_types = db_fetch_assoc('SELECT * FROM mac_track_device_types');
if (cacti_sizeof($device_types) == 0) {
mactrack_debug('ERROR: No device types have been found.');
db_process_remove($device_id);
exit;
}
/* check the devices read string for validity, set to new if changed */
if (valid_snmp_device($device)) {
mactrack_debug('HOST: ' . $device['hostname'] . ' is alive, processing has begun.');
$host_up = true;
/* locate the device type to obtain scanning function and low and high ports */
$device_type = find_scanning_function($device, $device_types);
if (isset($device_type) && sizeof($device_type) > 0) {
/* for switches/hubs, we need to determine the mac to port mappings */
if (($device['scan_type'] == DEVICE_HUB_SWITCH) ||
($device['scan_type'] == DEVICE_SWITCH_ROUTER)) {
/* verify that the scanning function is not null and call it as applicable */
if (isset($device_type['scanning_function'])) {
if (!empty($device_type['scanning_function'])) {
if (function_exists($device_type['scanning_function'])) {
mactrack_debug('Scanning function is ' . $device_type['scanning_function']);
$device['device_type_id'] = $device_type['device_type_id'];
$device['scan_type'] = $device_type['device_type'];
$device = call_user_func_array($device_type['scanning_function'], array($site, &$device, $device_type['lowPort'], $device_type['highPort']));
} else {
mactrack_debug('WARNING: SITE: ' . $site . ', IP: ' . $device['hostname'] . ', TYPE: ' . substr($device['snmp_sysDescr'],0,40) . ', ERROR: Scanning Function \'' . $device_type['scanning_function'] . '\' Does Not Exist.');
$device['last_runmessage'] = 'WARNING: Scanning Function \'' . $device_type['scanning_function'] . '\' Does Not Exist.';
$device['snmp_status'] = HOST_ERROR;
}
}
} else {
mactrack_debug('WARNING: SITE: ' . $site . ', IP: ' . $device['hostname'] . ', TYPE: ' . substr($device['snmp_sysDescr'],0,40) . ', ERROR: Scanning Function in Device Type Table Is Null.');
$device['last_runmessage'] = 'WARNING: Scanning Function in Device Type Table Is Null.';
$device['snmp_status'] = HOST_ERROR;
}
}
/* for routers and switch/routers we need to push the ARP table to mac_track_ip table */
if (($device['scan_type'] == DEVICE_SWITCH_ROUTER) ||
($device['scan_type'] == DEVICE_ROUTER)) {
if (isset($device_type['ip_scanning_function'])) {
if (!empty($device_type['ip_scanning_function'])) {
if (function_exists($device_type['ip_scanning_function'])) {
mactrack_debug('IP Scanning function is ' . $device_type['ip_scanning_function']);
$device['device_type_id'] = $device_type['device_type_id'];
$device['scan_type'] = $device_type['device_type'];
call_user_func_array($device_type['ip_scanning_function'], array($site, &$device));
} else {
mactrack_debug('WARNING: SITE: ' . $site . ', IP: ' . $device['hostname'] . ', TYPE: ' . substr($device['snmp_sysDescr'],0,40) . ', ERROR: IP Address Scanning Function \'' . $device_type['ip_scanning_function'] . '\' Does Not Exist.');
$device['last_runmessage'] = 'WARNING: Scanning Function \'' . $device_type['ip_scanning_function'] . '\' Does Not Exist.';
$device['snmp_status'] = HOST_ERROR;
}
}
} else {
mactrack_debug('WARNING: SITE: ' . $site . ', IP: ' . $device['hostname'] . ', TYPE: ' . substr($device['snmp_sysDescr'],0,40) . ', ERROR: IP Scanning Function in Device Type Table Is Null.');
$device['last_runmessage'] = 'WARNING: IP Scanning Function in Device Type Table Is Null.';
$device['snmp_status'] = HOST_ERROR;
}
if (isset($device_type['dot1x_scanning_function'])) {
if (!empty($device_type['dot1x_scanning_function'])) {
if (function_exists($device_type['dot1x_scanning_function'])) {
mactrack_debug('802.1x Scanning function is ' . $device_type['dot1x_scanning_function']);
$device['device_type_id'] = $device_type['device_type_id'];
$device['scan_type'] = $device_type['device_type'];
call_user_func_array($device_type['dot1x_scanning_function'], array($site, &$device));
} else {
mactrack_debug('WARNING: SITE: ' . $site . ', IP: ' . $device['hostname'] . ', TYPE: ' . substr($device['snmp_sysDescr'],0,40) . ', ERROR: 802.1x Address Scanning Function \'' . $device_type['dot1x_scanning_function'] . '\' Does Not Exist.');
$device['last_runmessage'] = 'WARNING: 802.1x Address Scanning Function \'' . $device_type['dot1x_scanning_function'] . '\' Does Not Exist.';
$device['snmp_status'] = HOST_ERROR;
}
}
}
}
} else {
mactrack_debug('WARNING: SITE: ' . $site . ', IP: ' . $device['hostname'] . ', TYPE: ' . substr($device['snmp_sysDescr'],0,40) . ', ERROR: Device Type Not Found in Device Type Table.');
$device['last_runmessage'] = 'WARNING: Device Type Not Found in Device Type Table.';
$device['snmp_status'] = HOST_ERROR;
}
} else {
mactrack_debug('WARNING: SITE: ' . $site . ', IP: ' . $device['hostname'] . ', TYPE: ' . substr($device['snmp_sysDescr'],0,40) . ', ERROR: Device unreachable.');
$device['last_runmessage'] = 'Device unreachable.';
$host_up = false;
}
/* update the database with device status information */
db_update_device_status($device, $host_up, $scan_date, $start_time);
db_process_remove($device_id);
exit;
function display_version() {
global $config;
$info = plugin_mactrack_version();
print "Network Device Tracking, Version " . $info['version'] .", " . COPYRIGHT_YEARS . "\n";
}
/* display_help - displays the usage of the function */
function display_help () {
display_version();
print "\nusage: mactrack_device.php -id=host_id [-w] [-d] [-h] [--help] [-v] [--version]\n\n";
print "-id=host_id - the mac_track_devices host_id to scan\n";
print "-d | --debug - Display verbose output during execution\n";
print "-w | --web - Display web compatible output during execution\n";
print "-t - Test mode, don't log a process id and interfere with system\n";
print "-v --version - Display this help message\n";
print "-h --help - display this help message\n";
}