-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdpdiscover.php
More file actions
550 lines (496 loc) · 19.6 KB
/
dpdiscover.php
File metadata and controls
550 lines (496 loc) · 19.6 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
323
324
325
326
327
328
329
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
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
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
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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2007 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/ |
+-------------------------------------------------------------------------+
CHANGES FOR DPDiscover (Discovery Protocol Discovery) care of:
Eric Stewart eric@usf.edu / eric@ericdives.com
*/
$guest_account = true;
chdir('../../');
include("./include/auth.php");
dpdiscover_setup_table();
dpdiscover_check_upgrade();
define("MAX_DISPLAY_PAGES", 21);
$os_arr = array_rekey(db_fetch_assoc("SELECT DISTINCT os FROM plugin_dpdiscover_hosts"), "os", "os");
$status_arr = array('Down', 'Up');
$protocol_arr = array_rekey(db_fetch_assoc("SELECT DISTINCT protocol FROM plugin_dpdiscover_hosts"), "protocol", "protocol");
/* ================= input validation ================= */
input_validate_input_number(get_request_var("page"));
input_validate_input_number(get_request_var("rows"));
/* ==================================================== */
/* clean up status string */
if (isset($_REQUEST["status"])) {
$_REQUEST["status"] = sanitize_search_string(get_request_var("status"));
}
/* clean up snmp string */
if (isset($_REQUEST["snmp"])) {
$_REQUEST["snmp"] = sanitize_search_string(get_request_var("snmp"));
}
/* clean up os string */
if (isset($_REQUEST["os"])) {
$_REQUEST["os"] = sanitize_search_string(get_request_var("os"));
}
/* clean up os string */
if (isset($_REQUEST["protocol"])) {
$_REQUEST["protocol"] = sanitize_search_string(get_request_var("protocol"));
}
/* clean up host string */
if (isset($_REQUEST["host"])) {
$_REQUEST["host"] = sanitize_search_string(get_request_var("host"));
}
/* clean up ip string */
if (isset($_REQUEST["ip"])) {
$_REQUEST["ip"] = sanitize_search_string(get_request_var("ip"));
}
/* clean up sort_column */
if (isset($_REQUEST["sort_column"])) {
$_REQUEST["sort_column"] = sanitize_search_string(get_request_var("sort_column"));
}
/* clean up search string */
if (isset($_REQUEST["sort_direction"])) {
$_REQUEST["sort_direction"] = sanitize_search_string(get_request_var("sort_direction"));
}
/* if the user pushed the 'clear' button */
if (isset($_REQUEST["button_clear_x"])) {
kill_session_var("sess_dpdiscover_current_page");
kill_session_var("sess_dpdiscover_status");
kill_session_var("sess_dpdiscover_snmp");
kill_session_var("sess_dpdiscover_os");
kill_session_var("sess_dpdiscover_host");
kill_session_var("sess_dpdiscover_ip");
kill_session_var("sess_dpdiscover_rows");
kill_session_var("sess_dpdiscover_sort_column");
kill_session_var("sess_dpdiscover_sort_direction");
kill_session_var("sess_dpdiscover_protocol");
unset($_REQUEST["page"]);
unset($_REQUEST["status"]);
unset($_REQUEST["snmp"]);
unset($_REQUEST["os"]);
unset($_REQUEST["host"]);
unset($_REQUEST["ip"]);
unset($_REQUEST["rows"]);
unset($_REQUEST["sort_column"]);
unset($_REQUEST["sort_direction"]);
unset($_REQUEST["protocol"]);
}
/* remember these search fields in session vars so we don't have to keep passing them around */
load_current_session_value("page", "sess_dpdiscover_current_page", "1");
// load_current_session_value("status", "sess_dpdiscover_status", "");
load_current_session_value("snmp", "sess_dpdiscover_snmp", "");
load_current_session_value("protocol", "sess_dpdiscover_protocol", "");
load_current_session_value("os", "sess_dpdiscover_os", "");
load_current_session_value("host", "sess_dpdiscover_host", "");
load_current_session_value("ip", "sess_dpdiscover_ip", "");
load_current_session_value("rows", "sess_dpdiscover_rows", "-1");
load_current_session_value("sort_column", "sess_dpdiscover_sort_column", "hostname");
load_current_session_value("sort_direction", "sess_dpdiscover_sort_direction", "ASC");
$sql_where = '';
$status = get_request_var_request("status");
$snmp = get_request_var_request("snmp");
$os = get_request_var_request("os");
$host = get_request_var_request("host");
$ip = get_request_var_request("ip");
$protocol = get_request_var_request("protocol");
/* Okay - this isn't used ... not really
if ($status == 'Down') {
$sql_where .= "WHERE snmp_status=0";
}else if ($status == 'Up') {
$sql_where .= "WHERE snmp_status=1";
}
*/
if ($snmp == 'Down') {
$sql_where .= (strlen($sql_where) ? " AND ":"WHERE ") . "snmp_status=0";
}else if ($snmp == 'Up') {
$sql_where .= (strlen($sql_where) ? " AND ":"WHERE ") . "snmp_status=1";
}
if ($os != '' && in_array($os, $os_arr)) {
$sql_where .= (strlen($sql_where) ? " AND ":"WHERE ") . "os='$os'";
}
if ($host != '') {
$sql_where .= (strlen($sql_where) ? " AND ":"WHERE ") . "hostname like '%$host%'";
}
if ($ip != '') {
$sql_where .= (strlen($sql_where) ? " AND ":"WHERE ") . "ip like '%$ip%'";
}
if ($protocol != '') {
$sql_where .= (strlen($sql_where) ? " AND ":"WHERE ") . "protocol='$protocol'";
}
if (isset($_GET['button_export_x'])) {
$result = db_fetch_assoc("SELECT * FROM plugin_dpdiscover_hosts $sql_where order by hostname");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=dpdiscover_results.csv");
print "Host,IP,Community Name,SNMP Name,Location,Contact,Description,OS,Parent,Port,Protocol,Uptime,SNMP\n";
foreach ($result as $host) {
if ($host['sysUptime'] != 0) {
$days = intval($host['sysUptime']/8640000);
$hours = intval(($host['sysUptime'] - ($days * 8640000)) / 360000);
$uptime = $days . ' days ' . $hours . ' hours';
} else {
$uptime = '';
}
foreach($host as $h=>$r) {
$host['$h'] = str_replace(',','',$r);
}
print $host['hostname'] . ",";
print $host['ip'] . ",";
print $host['snmp_community'] . ",";
print $host['sysName'] . ",";
print $host['sysLocation'] . ",";
print $host['sysContact'] . ",";
print $host['sysDescr'] . ",";
print $host['os'] . ",";
print $host['parent'] . ",";
print $host['port'] . ",";
print $host['protocol'] . ",";
print $uptime . ",";
print $host['snmp_status'] . "\n";
}
exit;
}
include(dirname(__FILE__) . "/general_header.php");
$total_rows = db_fetch_cell("SELECT
COUNT(*)
FROM plugin_dpdiscover_hosts
$sql_where");
$page = get_request_var_request("page");
if (get_request_var_request("rows") == "-1") {
$per_row = read_config_option("num_rows_device");
}else{
$per_row = get_request_var_request("rows");
}
$sortby = get_request_var_request("sort_column");
if ($sortby=="ip") {
$sortby = "INET_ATON(ip)";
}
$sql_query = "SELECT *
FROM plugin_dpdiscover_hosts
$sql_where
ORDER BY " . $sortby . " " . get_request_var_request("sort_direction") . "
LIMIT " . ($per_row*($page-1)) . "," . $per_row;
$result = db_fetch_assoc($sql_query);
?>
<script type="text/javascript">
<!--
function applyFilterChange(objForm) {
strURL = '?status=' + objForm.status.value;
strURL = strURL + '&ip=' + objForm.ip.value;
strURL = strURL + '&snmp=' + objForm.snmp.value;
strURL = strURL + '&os=' + objForm.os.value;
strURL = strURL + '&host=' + objForm.host.value;
strURL = strURL + '&rows=' + objForm.rows.value;
document.location = strURL;
}
-->
</script>
<?php
// TOP DEVICE SELECTION
html_start_box("<strong>Filters</strong>", "100%", $colors["header"], "3", "center", "");
?>
<tr bgcolor="#<?php print $colors["panel"];?>" class="noprint">
<td class="noprint">
<form style="padding:0px;margin:0px;" name="form" method="get" action="<?php print $config['url_path'];?>plugins/dpdiscover/dpdiscover.php">
<table width="100%" cellpadding="0" cellspacing="0">
<tr class="noprint">
<td nowrap style='white-space: nowrap;' width="1">
OS:
</td>
<td width="1">
<select name="os" onChange="applyFilterChange(document.form)">
<option value=""<?php if (get_request_var_request("os") == "") {?> selected<?php }?>>Any</option>
<?php
if (sizeof($os_arr)) {
foreach ($os_arr as $st) {
print "<option value='" . $st . "'"; if (get_request_var_request("os") == $st) { print " selected"; } print ">" . $st . "</option>\n";
}
}
?>
</select>
</td>
<td nowrap style='white-space: nowrap;' width="1">
PROTOCOL:
</td>
<td width="1">
<select name="protocol" onChange="applyFilterChange(document.form)">
<option value=""<?php if (get_request_var_request("protocol") == "") {?> selected<?php }?>>Any</option>
<?php
if (sizeof($protocol_arr)) {
foreach ($protocol_arr as $st) {
print "<option value='" . $st . "'"; if (get_request_var_request("protocol") == $st) { print " selected"; } print ">" . $st . "</option>\n";
}
}
?>
</select>
</td>
<td nowrap style='white-space: nowrap;' width="1">
SNMP:
</td>
<td width="1">
<select name="snmp" onChange="applyFilterChange(document.form)">
<option value=""<?php if (get_request_var_request("snmp") == "") {?> selected<?php }?>>Any</option>
<?php
if (sizeof($status_arr)) {
foreach ($status_arr as $st) {
print "<option value='" . $st . "'"; if (get_request_var_request("snmp") == $st) { print " selected"; } print ">" . $st . "</option>\n";
}
}
?>
</select>
</td>
<td nowrap style='white-space: nowrap;' width="1">
Host:
</td>
<td width="1">
<input type="text" name="host" size="25" value="<?php print get_request_var_request("host");?>">
</td>
<td nowrap style='white-space: nowrap;' width="1">
IP:
</td>
<td width="1">
<input type="text" name="ip" size="15" value="<?php print get_request_var_request("ip");?>">
</td>
<td nowrap style='white-space: nowrap;' width="1">
Rows:
</td>
<td width="1">
<select name="rows" onChange="applyFilterChange(document.form)">
<option value="-1"<?php if (get_request_var_request("rows") == "-1") {?> selected<?php }?>>Default</option>
<?php
if (sizeof($item_rows) > 0) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'"; if (get_request_var_request("rows") == $key) { print " selected"; } print ">" . $value . "</option>\n";
}
}
?>
</select>
</td>
<td nowrap style='white-space: nowrap;'>
<input type="submit" value="Go" title="Set/Refresh Filters">
<input type="submit" name="button_clear_x" value="Clear" title="Reset fields to defaults">
<input type="submit" name="button_export_x" value="Export" title="Export to a file">
</td>
</tr>
</table>
</form>
</td>
</tr>
<?php
html_end_box();
html_start_box("", "100%", $colors["header"], "3", "center", "");
/* generate page list */
$url_page_select = get_page_list($page, MAX_DISPLAY_PAGES, $per_row, $total_rows, "dpdiscover.php?view");
$nav = "<tr bgcolor='#" . $colors["header"] . "'>
<td colspan='14'>
<table width='100%' cellspacing='0' cellpadding='0' border='0'>
<tr>
<td align='left' class='textHeaderDark'>
<strong><< "; if ($page > 1) { $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("dpdiscover.php?view&status=$status&os=$os&snmp=$snmp&ip=$ip&host=$host" . "&page=" . ($page-1)) . "'>"; } $nav .= "Previous"; if ($page > 1) { $nav .= "</a>"; } $nav .= "</strong>
</td>\n
<td align='center' class='textHeaderDark'>
Showing Rows " . (($per_row*($page-1))+1) . " to " . ((($total_rows < $per_row) || ($total_rows < ($per_row*$page))) ? $total_rows : ($per_row*$page)) . " of $total_rows [$url_page_select]
</td>\n
<td align='right' class='textHeaderDark'>
<strong>"; if (($page * get_request_var_request("host_rows")) < $total_rows) { $nav .= "<a class='linkOverDark' href='" . htmlspecialchars("dpdiscover.php?view&status=$status&os=$os&snmp=$snmp&ip=$ip&host=$host" . "&page=" . (get_request_var_request("page")+1)) . "'>"; } $nav .= "Next"; if ((get_request_var_request("page") * get_request_var_request("host_rows")) < $total_rows) { $nav .= "</a>"; } $nav .= " >></strong>
</td>\n
</tr>
</table>
</td>
</tr>\n";
print $nav;
$display_text = array(
"hostname" => array("Host", "ASC"),
"ip" => array("IP", "ASC"),
"sysName" => array("SNMP Name", "ASC"),
"sysLocation" => array("Location", "ASC"),
"sysContact" => array("Contact", "ASC"),
"sysDescr" => array("Description", "ASC"),
"os" => array("Template", "ASC"),
"parent" => array("Parent", "ASC"),
"port" => array("Port", "ASC"),
"protocol" => array("Protocol", "DESC"),
"time" => array("Uptime", "DESC"),
"snmp" => array("SNMP", "DESC"),
"lastseen" => array("Last Seen", "ASC"),
"nosort" => array("", ""));
html_header_sort($display_text, get_request_var_request("sort_column"), get_request_var_request("sort_direction"), false);
$snmp_version = read_config_option("snmp_ver");
$snmp_port = read_config_option("snmp_port");
$snmp_timeout = read_config_option("snmp_timeout");
$snmp_username = read_config_option("snmp_username");
$snmp_password = read_config_option("snmp_password");
$max_oids = read_config_option("max_get_size");
$ping_method = read_config_option("ping_method");
$availability_method = read_config_option("availability_method");
$ping_timeout = read_config_option("ping_timeout");
$ping_retries = read_config_option("ping_retries");
$use_ip = read_config_option("dpdiscover_use_ip_hostname");
$use_fqdn_desc = read_config_option("dpdiscover-use_fqdn_for_description");
$i=0;
$status = array('<font color=red>Down</font>','<font color=green>Up</font>');
if (sizeof($result)) {
foreach($result as $row) {
form_alternate_row_color($colors["alternate"], $colors["light"], $i); $i++;
if ($row['sysUptime'] != 0) {
$days = intval($row['sysUptime']/8640000);
$hours = intval(($row['sysUptime'] - ($days * 8640000)) / 360000);
$uptime = $days . ' days ' . $hours . ' hours';
} else {
$uptime = '';
}
if ($row["hostname"] == "") {
$row["hostname"] = "Not Detected";
}
if($use_fqdn_for_hostname == "on") {
$addname = $row['hostname'];
}else{
$addname = get_shorthost($row['hostname']);
}
if($use_ip == "on") {
$addaddr = $row['ip'];
}else{
$addaddr = $row['hostname'];
}
$testid = get_id_from_name_addr($addname, $addaddr);
print"<td style='padding: 4px; margin: 4px;'>" . $row['hostname'] . "</td>
<td>" . $row['ip'] . "</td>
<td>" . $row['sysName'] . "</td>
<td>" . $row['sysLocation'] . "</td>
<td>" . $row['sysContact'] . "</td>
<td>" . $row['sysDescr'] . "</td>
<td>" . $row['os'] . "</td>
<td>" . $row['parent'] . "</td>
<td>" . $row['port'] . "</td>\n";
if($row['protocol'] != "known" && $testid !== FALSE && $testid != 0) {
print "<td><B>ADDED</B></td>\n";
}else{
print "<td>" . $row['protocol'] . "</td>\n";
}
print '<td>' . $uptime . '</td>
<td>' . $status[$row['snmp_status']] . '</td>
<td>' . $row['lastseen'] . '</td>
<td align="right">';
if ($row['protocol'] != "known" && $row['added'] != 1) {
if($testid === FALSE || $testid == 0) {
print "<form style=\"padding:0px;margin:0px;\" method=\"post\" action=\"../../host.php\">
<input type=hidden name=save_component_host value=1>
<input type=hidden name=host_template_id value=0>
<input type=hidden name=action value=\"save\">
<input type=hidden name=hostname value=\"$addaddr\">
<input type=hidden name=id value=0>
<input type=hidden name=description value=\"$addname\">
<input type=hidden name=snmp_community value=\"" . $row['snmp_community'] . "\">
<input type=hidden name=snmp_version value=\"$snmp_version\">
<input type=hidden name=snmp_username value=\"$snmp_username\">
<input type=hidden name=snmp_password value=\"$snmp_password\">
<input type=hidden name=snmp_port value=$snmp_port>
<input type=hidden name=snmp_timeout value=$snmp_timeout>
<input type=hidden name=snmp_password_confirm value=\"\">
<input type=hidden name=availability_method value=\"$availability_method\">
<input type=hidden name=ping_method value=\"$ping_method\">
<input type=hidden name=ping_port value=\"\">
<input type=hidden name=ping_timeout value=\"$ping_timeout\">
<input type=hidden name=ping_retries value=\"$ping_retries\">
<input type=hidden name=notes value=\"\">
<input type=hidden name=snmp_auth_protocol value=\"\">
<input type=hidden name=snmp_priv_passphrase value=\"\">
<input type=hidden name=snmp_priv_protocol value=\"\">
<input type=hidden name=snmp_context value=\"\">
<input type=hidden name=max_oids value=\"$max_oids\">
<input type=hidden name=device_threads value=\"1\">
<input type='submit' value='Add' style='text-align:middle;font-size:11px;'>
</form></td>";
}else{
print "<B>ADDED</B><!-- $testid --></td>";
}
}else{
print "</td>";
}
}
}else{
print "<tr><td style='padding: 4px; margin: 4px;' colspan=11><center>There are no Hosts to display!</center></td></tr>";
}
print $nav;
html_end_box(false);
include_once("./include/bottom_footer.php");
function get_id_from_name_addr($description, $address) {
$host = db_fetch_assoc("SELECT id FROM host WHERE description='$description' AND hostname='$address'");
if (!is_array($host)) {
return FALSE;
} else {
$firsthost = current($host);
$hostid = $firsthost['id'];
return $hostid;
}
}
/* Oh, someday I may need to make this better. */
function is_fqdn($address) {
if(is_ipv4($address) || is_ipv6($address)) {
return FALSE;
}else{
if(preg_match("/^udp6:(.*)$/", $address, $matches)) {
$address = $matches[1];
}
return (!empty($address) && preg_match('/(?=^.{1,254}$)(^(?:(?!\d|-)[a-z0-9\-]{1,63}(?<!-)\.)+(?:[a-z]{2,})$)/i', $address) > 0);
}
}
function make_fqdn($address) {
global $domain_name;
if(preg_match("/^udp6:(.*)$/", $address, $matches)) {
$address = $matches[1];
}
if(!is_fqdn($address)) {
$address = $address.".".$domain_name;
}
return $address;
}
function get_shorthost($address) {
if(!is_fqdn($address)) {
return $address;
}else{
return substr($address, 0, strpos($address, "."));
}
}
function is_ipv4($address) {
if(filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {
return FALSE;
}else{
return TRUE;
}
}
function is_ipv6($address) {
if(filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) {
// Check for SNMP specification and brackets
if(preg_match("/udp6:\[(.*)\]/", $address, $matches) > 0 &&
filter_var($matches[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE) {
return TRUE;
}
return FALSE;
}else{
return TRUE;
}
}
function is_ipv6_raw($address) {
if(filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) {
return FALSE;
}else{
return TRUE;
}
}
?>