forked from BotFodder/dpdiscover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findhosts.php
1364 lines (1277 loc) · 56.6 KB
/
findhosts.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Modifications are Copyright (C) 2013 Eric Stewart - [email protected] or
* Rights to the modifications are hereby transferred to The Cacti Group,
* per the below "Original Copyright," providing attribution remains.
*
* This findhosts.php is a heavily modified version of the original
* autodiscovery plugin. Instead of using an IP sweep, we check known
* devices for LLDP/CDP data via SNMP. If that data is there, we then attempt
* to add the device using a hostname and configured SNMP data. Also, we
* keep a table of discovered devices so that, if they are not successfully
* added, an admin can add them manually.
*
* All tree and graph creation logic removed. Suggest you use autom8 to
* perform those functions.
*/
/* Original Copyright:
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2011 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/ |
+-------------------------------------------------------------------------+
*/
/* do NOT run this script through a web browser */
if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die("<br><strong>This script is only meant to run at the command line.</strong>");
}
/* let PHP run just as long as it has to */
ini_set("max_execution_time", "0");
error_reporting('E_ALL');
$dir = dirname(__FILE__);
chdir($dir);
if (strpos($dir, 'plugins') !== false) {
chdir('../../');
}
include("./include/global.php");
include_once($config["base_path"] . '/lib/ping.php');
include_once($config["base_path"] . '/lib/utility.php');
include_once($config["base_path"] . '/lib/api_data_source.php');
include_once($config["base_path"] . '/lib/api_graph.php');
include_once($config["base_path"] . '/lib/snmp.php');
include_once($config["base_path"] . '/lib/data_query.php');
include_once($config["base_path"] . '/lib/api_device.php');
include_once($config["base_path"] . '/lib/sort.php');
include_once($config["base_path"] . '/lib/html_form_template.php');
include_once($config["base_path"] . '/lib/template.php');
$current_time = strtotime("now");
dpdiscover_check_upgrade();
/* process calling arguments */
$parms = $_SERVER["argv"];
array_shift($parms);
$debug = FALSE;
$forcerun = FALSE;
foreach($parms as $parameter) {
@list($arg, $value) = @explode('=', $parameter);
switch ($arg) {
case "-r":
dpdiscover_recreate_tables();
break;
case "-d":
$debug = TRUE;
break;
case "-h":
display_help();
exit;
case "-f":
$forcerun = TRUE;
break;
case "-v":
display_help();
exit;
case "--version":
display_help();
exit;
case "--help":
display_help();
exit;
default:
print "ERROR: Invalid Parameter " . $parameter . "\n\n";
display_help();
exit;
}
}
if (read_config_option("dpdiscover_use_lldp") != "on" &&
read_config_option("dpdiscover_use_cdp") != "on" &&
read_config_option("dpdiscover_use_fdp") != "on") {
print "No discovery protocol active! Please activate one in the settings.\n";
display_help();
exit;
}
if (read_config_option("dpdiscover_collection_timing") == "disabled") {
dpdiscover_debug("Discovery Polling is set to disabled.\n");
if(!isset($debug)) {
exit;
}
}
dpdiscover_debug("Checking to determine if it's time to run.\n");
$seconds_offset = read_config_option("dpdiscover_collection_timing");
$seconds_offset = $seconds_offset * 60;
$base_start_time = read_config_option("dpdiscover_base_time");
$last_run_time = read_config_option("dpdiscover_last_run_time");
$previous_base_start_time = read_config_option("dpdiscover_prev_base_time");
if ($base_start_time == '') {
dpdiscover_debug("Base Starting Time is blank, using '12:00am'\n");
$base_start_time = '12:00am';
}
$minutes = date("i", strtotime($base_start_time));
$hourdate = date("Y-m-d H:$minutes:00");
$hourtime = strtotime($hourdate);
dpdiscover_debug($hourdate . " " . $hourtime . "\n");
/* see if the user desires a new start time */
dpdiscover_debug("Checking if user changed the start time\n");
if (!empty($previous_base_start_time)) {
if ($base_start_time <> $previous_base_start_time) {
dpdiscover_debug(" User changed the start time from '$previous_base_start_time' to '$base_start_time'\n");
unset($last_run_time);
db_execute("DELETE FROM settings WHERE name='dpdiscover_last_run_time'");
}
}
/* Check for the polling interval, only valid with the Multipoller patch */
$poller_interval = read_config_option("poller_interval");
if (!isset($poller_interval)) {
$poller_interval = 300;
}
/* set to detect if the user cleared the time between polling cycles */
db_execute("REPLACE INTO settings (name, value) VALUES ('dpdiscover_prev_base_time', '$base_start_time')");
/* determine the next start time */
if (empty($last_run_time)) {
if ($current_time > strtotime($base_start_time)) {
/* if timer expired within a polling interval, then poll */
if (($current_time - $poller_interval) < strtotime($base_start_time)) {
$next_run_time = strtotime(date("Y-m-d") . " " . $base_start_time);
}else{
$next_run_time = strtotime(date("Y-m-d") . " " . $base_start_time) + $seconds_offset;
}
}else{
$next_run_time = strtotime(date("Y-m-d") . " " . $base_start_time);
}
}else{
$next_run_time = $last_run_time + $seconds_offset;
}
$time_till_next_run = $next_run_time - $current_time;
if ($time_till_next_run < 0) {
dpdiscover_debug("The next run time has been determined to be NOW\n");
}else{
dpdiscover_debug("The next run time has been determined to be at\n " . date("Y-m-d G:i:s", $next_run_time) . "\n");
}
if ($time_till_next_run > 0 && $forcerun == FALSE) {
exit;
}
if ($forcerun) {
dpdiscover_debug("Scanning has been forced\n");
}
/* Let's fake this to be on the current hour, instead of the actual time */
if ($forcerun == FALSE) {
db_execute("REPLACE INTO settings (name, value) VALUES ('dpdiscover_last_run_time', '$hourtime')");
}
/* If name data from LLDP doesn't tell us the FQDN or IP, then we'll append
* this to whatever name we get. */
$domain_name = read_config_option("dpdiscover_domain_name");
/* Do we use the FQDN name as the description? */
$use_fqdn_description = read_config_option("dpdiscover_use_fqdn_description");
/* Do we use the IP for the hostname? If not, use FQDN */
$use_ip_hostname = read_config_option("dpdiscover_use_ip_hostname");
$fix_ip_hostname = read_config_option("dpdiscover_fix_ip_hostname");
cacti_log("DP Discover is now running", true, "POLLER");
// Get array of snmp information.
$known_hosts = db_fetch_assoc("SELECT id, host_template_id, description, hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_port, snmp_timeout, disabled, availability_method, ping_method, ping_port, ping_timeout, ping_retries, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, max_oids, device_threads FROM host");
if (!is_array($known_hosts)) {
cacti_log("DP Discover failed to pull known hosts? Exiting.", true, "POLLER");
exit;
}
// Get Oses
$temp = db_fetch_assoc("SELECT plugin_dpdiscover_template.*, host_template.name
FROM plugin_dpdiscover_template
LEFT JOIN host_template
ON (plugin_dpdiscover_template.host_template=host_template.id)");
$os = array();
$templates = array();
$count = 0;
if (is_array($temp) && count($temp) > 0) {
foreach ($temp as $d) {
$os[] = $d;
$count++;
}
}else{
cacti_log("DP Discover ended: had issues finding DPDiscover Templates, exiting.", true, "POLLER");
exit;
}
if($count == 0) {
cacti_log("No DP Discover Templates found, exiting.", true, "POLLER");
exit;
}
$temp = db_fetch_assoc("SELECT id, name FROM host_template");
if (is_array($temp)) {
foreach ($temp as $d) {
$templates[$d['id']] = $d['name'];
}
}
/* Someday I'll have to set up SNMP v3 options here. */
$cnames = read_config_option("dpdiscover_readstrings");
if ($cnames == '') {
$cnames = 'public';
}
dpdiscover_debug("Community Names : $cnames\n");
/* $dpdiscovered['dphost'][$dphost]
* $dpdiscovered['dphost'][$dphost]['ip'] = $ip;
* $dpdiscovered['dphost'][$dphost]['hostname'] = $fqdn;
* $dpdiscovered['dphost'][$dphost]['description'] = $shortname;
* $dpdiscovered['dphost'][$dphost]['parent'] = $parent;
* $dpdiscovered['dphost'][$dphost]['port'] = $portfromparent;
* $dpdiscovered['dphost'][$dphost]['protocol'] = "protocol";
* $dpdiscovered['dphost'][$dphost]['added'] = 0/1;
* $dpdiscovered['ip'][$ip] = $dphost;
* $dpdiscovered['hostname'][$fqdn] = $dphost;
* $dpdiscovered['description'][$shortname] = $dphost;
*/
$snmp_retries = read_config_option("snmp_retries");
$dpdiscovered = array();
$search = $known_hosts;
// Seed the relevant arrays with known information.
dpdiscover_debug("Seeding array: ");
foreach($known_hosts as $host) {
// Might be short, might not. Make sure and use it as key.
$dphost = get_shorthost($host['description']);
$dpdiscovered['dphost'][$dphost] = $host;
// Okay, change what we need to.
$dpdiscovered['dphost'][$dphost]['protocol'] = "known";
// $dpdiscovered['dphost'][$dphost]['description'][$host['description']] = $dphost;
$dpdiscovered['dphost'][$dphost]['added'] = 0;
$dpdiscovered['dphost'][$dphost]['os'] = $templates[$dpdiscovered['dphost'][$dphost]['host_template_id']];
$dpdiscovered['dphost'][$dphost]['snmp_sysName'] = '';
$dpdiscovered['dphost'][$dphost]['snmp_sysLocation'] = '';
$dpdiscovered['dphost'][$dphost]['snmp_sysContact'] = '';
$dpdiscovered['dphost'][$dphost]['snmp_sysDescr'] = '';
$dpdiscovered['dphost'][$dphost]['snmp_sysUptime'] = '';
$dpdiscovered['dphost'][$dphost]['snmp_status'] = 1;
// If hostname is stored as an IP, base FQDN off of description.
if(is_ipv4($host['hostname']) || is_ipv6($host['hostname'])) {
$fqdnname = make_fqdn($host['description']);
$dpdiscovered['dphost'][$dphost]['ip'] = $host['hostname'];
$dpdiscovered['dphost'][$dphost]['hostname'] = $fqdnname;
$dpdiscovered['ip'][$host['hostname']] = $dphost;
$dpdiscovered['hostname'][$fqdnname] = $dphost;
}else{
// If hostname is stored as an address, check it for FQDN-ness and get the IP
$fqdnname = make_fqdn($host['hostname']);
$hostip = get_ip($fqdnname);
$dpdiscovered['dphost'][$dphost]['hostname'] = $fqdnname;
$dpdiscovered['dphost'][$dphost]['ip'] = $hostip;
$dpdiscovered['ip'][$hostip] = $dphost;
$dpdiscovered['hostname'][$fqdnname] = $dphost;
}
if((is_ipv4($dpdiscovered['dphost'][$dphost]['ip']) ||
is_ipv6($dpdiscovered['dphost'][$dphost]['ip'])) &&
$dpdiscovered['dphost'][$dphost]['disabled'] != "on") {
dpdiscover_debug($dphost." ");
dpdiscover_get_snmp_values($dpdiscovered['dphost'][$dphost]);
}
}
dpdiscover_debug("\n");
$sysObjectID_OID = ".1.3.6.1.2.1.1.2.0";
$sidx = 0;
/* We loop through "search" using a while, as more items may be added to the
* loop as we proceed through it. This *should* ensure that even new items
* are searched for children.
*/
while(isset($search[$sidx])) {
$shortsearch = get_shorthost($search[$sidx]['description']);
if (isset($search[$sidx]['disabled']) && $search[$sidx]['disabled'] == "on") {
dpdiscover_debug("$sidx $shortsearch is disabled\n");
$sidx++;
continue;
}
if(check_exclusion($shortsearch)) {
dpdiscover_debug("$sidx Excluding $shortsearch\n");
$sidx++;
continue;
}
dpdiscover_debug("$sidx BEGIN: $shortsearch\n");
if(is_ipv4($dpdiscovered['dphost'][$shortsearch]['ip']) === FALSE &&
is_ipv6($dpdiscovered['dphost'][$shortsearch]['ip']) === FALSE) {
$sidx++;
continue;
}
$sysObjectID = cacti_snmp_get($dpdiscovered['dphost'][$shortsearch]['ip'],
$search[$sidx]['snmp_community'], $sysObjectID_OID,
$search[$sidx]['snmp_version'], $search[$sidx]['snmp_username'], $search[$sidx]['snmp_password'],
$search[$sidx]['snmp_auth_protocol'], $search[$sidx]['snmp_priv_passphrase'],
$search[$sidx]['snmp_priv_protocol'], $search[$sidx]['snmp_context'], $search[$sidx]['snmp_port'],
$search[$sidx]['snmp_timeout'], $snmp_retries, $search[$sidx]['max_oids'], SNMP_POLLER);
// If we can't find it and it's a known device, then we try to fix it.
if (!isset($sysObjectID) || $sysObjectID == "") {
dpdiscover_debug("$sidx FAILED sysObjectID for ".$dpdiscovered['dphost'][$shortsearch]['ip']."\n");
// Should we fix it?
if($fix_ip_hostname == "on" && $use_ip_hostname == "on" &&
$dpdiscovered['dphost'][$dphost]['protocol'] == "known") {
$newip = get_ip($dpdiscovered['dphost'][$shortsearch]['hostname']);
if($newip != $dpdiscovered['dphost'][$shortsearch]['ip'] &&
(is_ipv4($newip) || is_ipv6($newip))) {
$sysObjectID = cacti_snmp_get($newip,
$search[$sidx]['snmp_community'], $sysObjectID_OID,
$search[$sidx]['snmp_version'], $search[$sidx]['snmp_username'], $search[$sidx]['snmp_password'],
$search[$sidx]['snmp_auth_protocol'], $search[$sidx]['snmp_priv_passphrase'],
$search[$sidx]['snmp_priv_protocol'], $search[$sidx]['snmp_context'], $search[$sidx]['snmp_port'],
$search[$sidx]['snmp_timeout'], $snmp_retries, $search[$sidx]['max_oids'], SNMP_POLLER);
// Can't find it via SNMP - give up.
if (!isset($sysObjectID) || $sysObjectID == "") {
$sidx++;
continue;
}else{
dpdiscover_debug("$sidx see new IP: $newip for $shortsearch\n");
$dpdiscovered['dphost'][$shortsearch]['oldip'] = $dpdiscovered['dphost'][$shortsearch]['ip'];
$dpdiscovered['dphost'][$shortsearch]['ip'] = $newip;
// Fix it!
if($debug === FALSE) {
api_device_save(
$dpdiscovered['dphost'][$shortsearch]['id'],
$dpdiscovered['dphost'][$shortsearch]['host_template_id'],
$dpdiscovered['dphost'][$shortsearch]['description'],
$newip,
$dpdiscovered['dphost'][$shortsearch]['snmp_community'],
$dpdiscovered['dphost'][$shortsearch]['snmp_version'],
$dpdiscovered['dphost'][$shortsearch]['snmp_username'],
$dpdiscovered['dphost'][$shortsearch]['snmp_password'],
$dpdiscovered['dphost'][$shortsearch]['snmp_port'],
$dpdiscovered['dphost'][$shortsearch]['snmp_timeout'],
$dpdiscovered['dphost'][$shortsearch]['disabled'],
$dpdiscovered['dphost'][$shortsearch]['availability_method'],
$dpdiscovered['dphost'][$shortsearch]['ping_method'],
$dpdiscovered['dphost'][$shortsearch]['ping_port'],
$dpdiscovered['dphost'][$shortsearch]['ping_timeout'],
$dpdiscovered['dphost'][$shortsearch]['ping_retries'],
"DPDiscover changed IP from: ".$dpdiscovered['dphost'][$shortsearch]['oldip'],
$dpdiscovered['dphost'][$shortsearch]['snmp_auth_protocol'],
$dpdiscovered['dphost'][$shortsearch]['snmp_priv_passphrase'],
$dpdiscovered['dphost'][$shortsearch]['snmp_priv_protocol'],
$dpdiscovered['dphost'][$shortsearch]['snmp_context'],
$dpdiscovered['dphost'][$shortsearch]['max_oids'],
$dpdiscovered['dphost'][$shortsearch]['device_threads']
);
cacti_log("DPDiscover changed IP for ".$dpdiscovered['dphost'][$shortsearch]['id']." ".$shortsearch." from ".$dpdiscovered['dphost'][$shortsearch]['oldip']." to ".$newip, TRUE, "POLLER");
}else{
dpdiscover_debug("UPDATE host SET hostname='".$newip."' WHERE id=".$dpdiscovered['dphost'][$shortsearch]['id']."\n");
cacti_log("DPDiscover would change IP for ".$dpdiscovered['dphost'][$shortsearch]['id']." ".$shortsearch." from ".$dpdiscovered['dphost'][$shortsearch]['oldip']." to ".$newip, TRUE, "POLLER");
}
}
}else{
$sidx++;
continue;
}
}else{
$sidx++;
continue;
}
}
$dparray = array();
$use_lldp = read_config_option("dpdiscover_use_lldp");
$use_cdp = read_config_option("dpdiscover_use_cdp");
$use_fdp = read_config_option("dpdiscover_use_fdp");
$lldpfail = TRUE;
$cdpfail = TRUE;
$fdpfail = TRUE;
// Try LLDP
if ($use_lldp == "on" && FALSE !== ($lldparray = LLDP_Discovery($search[$sidx]))) {
$dparray = array_merge($dparray, $lldparray);
$lldpfail = FALSE;
}
// Try CDP
if($use_cdp == "on" && FALSE !== ($cdparray = CDP_Discovery($search[$sidx]))) {
$dparray = array_merge($dparray, $cdparray);
$cdpfail = FALSE;
}
// Try FDP
if($use_fdp == "on" && FALSE !== ($fdparray = FDP_Discovery($search[$sidx]))) {
dpdiscover_debug("$sidx FDP Found stuff instead. ".sizeof($dparray)."\n");
$dparray = array_merge($dparray, $fdparray);
$fdpfail = FALSE;
}
// Try FDP again but now via SNMPv1, if nothing else worked
if($use_fdp == "on" && read_config_option("dpdiscover_fdp_try_v1") == "on" &&
$lldpfail === TRUE && $cdpfail === TRUE && $fdpfail === TRUE &&
$search[$sidx]['snmp_version'] == 2) {
$search[$sidx]['snmp_version'] = 1;
dpdiscover_debug($search[$sidx]['description'].": Punting to v1\n");
if(FALSE !== ($fdparray = FDP_Discovery($search[$sidx]))) {
$dparray = array_merge($dparray, $fdparray);
}
$search[$sidx]['snmp_version'] = 2;
}
// Either we know about everything connected to this device or we
// can't figure out what's connected to this device.
if(sizeof($dparray) < 1) {
dpdiscover_debug("$sidx Nothing Found.\n");
$sidx++;
continue;
}
$snmp_version = read_config_option("snmp_ver");
$snmp_port = read_config_option("snmp_port");
$snmp_timeout = read_config_option("snmp_timeout");
// Loop through the new things we've discovered and try to add them.
foreach($dparray as $pdevice) {
$pdevice['snmp_status'] = 0;
$pdevice['ip'] = $dpdiscovered['dphost'][$pdevice['description']]['ip'];
$pdevice['snmp_community'] = '';
$pdevice['snmp_username'] = '';
$pdevice['snmp_password'] = '';
$pdevice['snmp_auth_protocol'] = '';
$pdevice['snmp_priv_passphrase'] = '';
$pdevice['snmp_priv_protocol'] = '';
$pdevice['snmp_context'] = '';
$pdevice['snmp_readstrings'] = $cnames;
$pdevice['snmp_version'] = $snmp_version;
$pdevice['snmp_port'] = $snmp_port;
$pdevice['snmp_timeout'] = $snmp_timeout;
$pdevice['snmp_sysObjectID'] = '';
$pdevice['snmp_sysName'] = '';
$pdevice['snmp_sysLocation'] = '';
$pdevice['snmp_sysContact'] = '';
$pdevice['snmp_sysDescr'] = '';
$pdevice['snmp_sysUptime'] = '';
$pdevice['host_template_id'] = '';
if(!isset($pdevice['max_oids'])) {
$pdevice['max_oids'] = 10;
}
// Verify IP validity and make sure we aren't supposed to ignore it.
if((is_ipv4($dpdiscovered['dphost'][$pdevice['description']]['ip']) ||
is_ipv6($dpdiscovered['dphost'][$pdevice['description']]['ip'])) &&
check_exclusion($pdevice['description']) === FALSE) {
if (dpdiscover_valid_snmp_device($pdevice, $search[$sidx]) === TRUE) {
dpdiscover_debug("I think we can add ".$pdevice['description']."\n");
$fos = dpdiscover_find_os($pdevice['snmp_sysDescr']);
if ($fos !== FALSE) {
$pdevice['os'] = $fos['name'];
$pdevice['host_template_id'] = $fos['host_template'];
$dpdiscovered['dphost'][$pdevice['description']]['os'] = $pdevice['os'];
$dpdiscovered['dphost'][$pdevice['description']]['host_template_id'] = $pdevice['host_template_id'];
dpdiscover_debug($pdevice['description']." OS ".$pdevice['os']." ID ".$pdevice['host_template_id']."\n");
$host_id = 0;
$host_id = dpdiscover_add_device($pdevice);
}else{
$dpdiscovered['dphost'][$pdevice['description']]['os'] = "No Match";
}
$search[] = $pdevice;
}
}
$dpdiscovered['dphost'][$pdevice['description']]['snmp_status'] = $pdevice['snmp_status'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_community'] = $pdevice['snmp_community'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_username'] = $pdevice['snmp_username'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_password'] = $pdevice['snmp_password'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_version'] = $pdevice['snmp_version'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_sysName'] = $pdevice['snmp_sysName'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_sysLocation'] = $pdevice['snmp_sysLocation'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_sysContact'] = $pdevice['snmp_sysContact'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_sysDescr'] = $pdevice['snmp_sysDescr'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_sysUptime'] = $pdevice['snmp_sysUptime'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_auth_protocol'] = $pdevice['snmp_auth_protocol'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_priv_passphrase'] = $pdevice['snmp_priv_passphrase'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_priv_protocol'] = $pdevice['snmp_priv_protocol'];
$dpdiscovered['dphost'][$pdevice['description']]['snmp_context'] = $pdevice['snmp_context'];
if(!isset($dpdiscovered['dphost'][$pdevice['description']]['os'])) {
$dpdiscovered['dphost'][$pdevice['description']]['os'] = '';
}
}
$sidx++;
}
// DUMP $dpdiscovered to database; send report
$send_report_to = read_config_option("dpdiscover_email_report");
if (isset($dpdiscovered['dphost'])) {
$ipchange = "";
$message = "\nREPORT OF DEVICES ADDED BY DPDISCOVERY:\n\n";
$found = "\nREPORT: Has IP, but not added:\n\n";
$skipped = "\nDevices Excluded:\n\n";
foreach($dpdiscovered['dphost'] as $host => $device) {
if(isset($device['newname'])) {
dpdiscover_debug("Skipping ".$host." = ".$device['newname']."\n");
continue;
}
if(isset($device['disabled']) && $device['disabled'] == "on") {
dpdiscover_debug("Skipping ".$host." - DISABLED\n");
continue;
}
if(!isset($device['parent'])) {
$device['parent'] = '';
}
if(!isset($device['port'])) {
$device['port'] = '';
}
if(isset($device['oldname'])) {
$ipchange .= "Changed ".$device['description']." name from ".$device['oldname']."\n";
}
if(isset($device['oldip'])) {
$ipchange .= "Changed ".$device['description']." IP from ".$device['oldip']." to ".$device['ip']."\n";
}
if($device['added'] == 1) {
$message .= $device['description']." - ".$device['ip']." - ".$device['os']." FOUND VIA: ".$device['parent']." - ".$device['port']."\n";
}
if((is_ipv6($device['ip']) || is_ipv4($device['ip'])) &&
$device['added'] != 1 && $device['protocol'] != "known") {
if(check_exclusion($device['description'])) {
$skipped .= $device['description']." - ".$device['ip']." FOUND VIA: ".$device['parent']." - ".$device['port']."\n";
}else{
$found .= $device['description']." - ".$device['ip']." FOUND VIA: ".$device['parent']." - ".$device['port']."\n";
}
}
// Dump into a database table to check things out.
if($debug === FALSE) {
db_execute("REPLACE INTO plugin_dpdiscover_hosts (hostname, ip, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, sysName, sysLocation, sysContact, sysDescr, sysUptime, os, added, snmp_status, time, protocol, parent, port, lastseen) VALUES ('"
. sql_sanitize($device['hostname'])."', '"
. sql_sanitize($device['ip'])."', '"
. sql_sanitize($device['snmp_community']) . "', "
. sql_sanitize($device['snmp_version']) . ", '"
. sql_sanitize($device['snmp_username']) . "', '"
. sql_sanitize($device['snmp_password']) . "', '"
. sql_sanitize($device['snmp_auth_protocol']) . "', '"
. sql_sanitize($device['snmp_priv_passphrase']) . "', '"
. sql_sanitize($device['snmp_priv_protocol']) . "', '"
. sql_sanitize($device['snmp_context']) . "', '"
. sql_sanitize($device['snmp_sysName']) . "', '"
. sql_sanitize($device['snmp_sysLocation']) . "', '"
. sql_sanitize($device['snmp_sysContact']) . "', '"
. sql_sanitize($device['snmp_sysDescr']) . "', '"
. sql_sanitize($device['snmp_sysUptime']) . "', '"
. sql_sanitize($device['os']) . "', "
. $device['added'] .", "
. $device['snmp_status'] .", "
. time() .", '"
. sql_sanitize($device['protocol']) ."', '"
. sql_sanitize($device['parent']) ."', '"
. sql_sanitize($device['port']) ."',"
. "NOW())" );
}
}
if(filter_var($send_report_to, FILTER_VALIDATE_EMAIL)) {
$subject = "DP Discover Report";
if(read_config_option("dpdiscover_include_skipped") == "on") {
mail($send_report_to, $subject, $ipchange.$message.$found.$skipped);
}else{
mail($send_report_to, $subject, $ipchange.$message.$found);
}
}else{
dpdiscover_debug($send_report_to." is not a valid email\n");
}
dpdiscover_debug($ipchange.$message.$found.$skipped."\n");
}
exit;
// Found a device that has a name we don't know but an IP we do?
function DPChange_Name($newname) {
global $dpdiscovered, $debug;
if(check_exclusion($newname['description']) ||
read_config_option("dpdiscover_fix_names") != "on") {
return;
}
dpdiscover_debug("Name Change? ".$newname['ip']." ".$newname['description']." but known: ".$dpdiscovered['ip'][$newname['ip']]."\n");
// Store the old one
$oldname = $dpdiscovered['ip'][$newname['ip']];
$dpdiscovered['dphost'][$newname['description']] = $dpdiscovered['dphost'][$oldname];
$dpdiscovered['dphost'][$newname['description']]['hostname'] = $newname['hostname'];
$dpdiscovered['dphost'][$newname['description']]['description'] = $newname['description'];
$dpdiscovered['ip'][$newname['ip']] = $newname['description'];
$dpdiscovered['dphost'][$newname['description']]['oldname'] = $oldname;
$dpdiscovered['dphost'][$oldname]['newname'] = $newname['description'];
$use_ip_for_hostname = read_config_option("dpdiscover_use_ip_hostname");
$use_fqdn_for_descr = read_config_option("dpdiscover_use_fqdn_for_description");
if($use_fqdn_for_descr == "on") {
$description = $dpdiscovered['dphost'][$newname['description']]['hostname'];
}else{
$description = $newname['description'];
}
if($use_ip_for_hostname == "on") {
$hostname = $dpdiscovered['dphost'][$newname['description']]['ip'];
}else{
$hostname = $newname['hostname'];
if(is_ipv6($device['ip'])) {
$hostname = "udp6:$hostname";
}
}
if($debug === FALSE) {
api_device_save(
$dpdiscovered['dphost'][$newname['description']]['id'],
$dpdiscovered['dphost'][$newname['description']]['host_template_id'],
$dpdiscovered['dphost'][$newname['description']]['description'],
$dpdiscovered['dphost'][$newname['description']]['ip'],
$dpdiscovered['dphost'][$newname['description']]['snmp_community'],
$dpdiscovered['dphost'][$newname['description']]['snmp_version'],
$dpdiscovered['dphost'][$newname['description']]['snmp_username'],
$dpdiscovered['dphost'][$newname['description']]['snmp_password'],
$dpdiscovered['dphost'][$newname['description']]['snmp_port'],
$dpdiscovered['dphost'][$newname['description']]['snmp_timeout'],
$dpdiscovered['dphost'][$newname['description']]['disabled'],
$dpdiscovered['dphost'][$newname['description']]['availability_method'],
$dpdiscovered['dphost'][$newname['description']]['ping_method'],
$dpdiscovered['dphost'][$newname['description']]['ping_port'],
$dpdiscovered['dphost'][$newname['description']]['ping_timeout'],
$dpdiscovered['dphost'][$newname['description']]['ping_retries'],
"DPDiscover changed name from: ".$dpdiscovered['dphost'][$newname['description']]['oldname'],
$dpdiscovered['dphost'][$newname['description']]['snmp_auth_protocol'],
$dpdiscovered['dphost'][$newname['description']]['snmp_priv_passphrase'],
$dpdiscovered['dphost'][$newname['description']]['snmp_priv_protocol'],
$dpdiscovered['dphost'][$newname['description']]['snmp_context'],
$dpdiscovered['dphost'][$newname['description']]['max_oids'],
$dpdiscovered['dphost'][$newname['description']]['device_threads']
);
}else{
dpdiscover_debug("api_device_save: ".
$dpdiscovered['dphost'][$newname['description']]['id']."\n".
$dpdiscovered['dphost'][$newname['description']]['host_template_id']."\n".
$dpdiscovered['dphost'][$newname['description']]['description']."\n".
$dpdiscovered['dphost'][$newname['description']]['ip']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_community']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_version']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_username']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_password']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_port']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_timeout']."\n".
$dpdiscovered['dphost'][$newname['description']]['disabled']."\n".
$dpdiscovered['dphost'][$newname['description']]['availability_method']."\n".
$dpdiscovered['dphost'][$newname['description']]['ping_method']."\n".
$dpdiscovered['dphost'][$newname['description']]['ping_port']."\n".
$dpdiscovered['dphost'][$newname['description']]['ping_timeout']."\n".
$dpdiscovered['dphost'][$newname['description']]['ping_retries']."\n".
"DPDiscover changed name from: ".$dpdiscovered['dphost'][$newname['description']]['oldname']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_auth_protocol']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_priv_passphrase']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_priv_protocol']."\n".
$dpdiscovered['dphost'][$newname['description']]['snmp_context']."\n".
$dpdiscovered['dphost'][$newname['description']]['max_oids']."\n".
$dpdiscovered['dphost'][$newname['description']]['device_threads']."\n");
dpdiscover_debug($oldname." would become ".$newname['description']."\n");
}
}
// LLDP Discovery
function LLDP_Discovery($searchme) {
global $dpdiscovered;
$lldpLocPortId_OID = ".1.0.8802.1.1.2.1.3.7.1.3";
$lldpLocPortDesc_OID = ".1.0.8802.1.1.2.1.3.7.1.4";
$lldpRemSysName_OID = ".1.0.8802.1.1.2.1.4.1.1.9";
$ifName_OID = ".1.3.6.1.2.1.31.1.1.1.1";
$snmp_retries = read_config_option("snmp_retries");
$lldphost = get_shorthost($searchme['description']);
$lldpnames = cacti_snmp_walk($dpdiscovered['dphost'][$lldphost]['ip'],
$searchme['snmp_community'], $lldpRemSysName_OID, $searchme['snmp_version'],
$searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'],
$searchme['snmp_port'], $searchme['snmp_timeout'], $snmp_retries,
$searchme['max_oids'], SNMP_POLLER);
if (sizeof($lldpnames) == 0 || sizeof($lldpnames) === FALSE) {
dpdiscover_debug("No lldpnames found.\n");
return FALSE;
}
DP_setup($lldpnames);
dpdiscover_debug("LLDP: Walked $lldphost ".$dpdiscovered['dphost'][$lldphost]['ip']."\n");
$answer = array();
for ($i=0; $i<sizeof($lldpnames); $i++) {
if($lldpnames[$i]['description'] == "") {
continue;
}
if(isset($dpdiscovered['dphost'][$lldpnames[$i]['description']]['parent'])) {
continue;
}
$lldpport = cacti_snmp_get($dpdiscovered['dphost'][$lldphost]['ip'],
$searchme['snmp_community'], $lldpLocPortId_OID.".".$lldpnames[$i]['portindex'],
$searchme['snmp_version'], $searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'], $searchme['snmp_port'],
$searchme['snmp_timeout'], $snmp_retries, $searchme['max_oids'], SNMP_POLLER);
// I don't like how the Brocades use MACs for PortId, so fall back to desc.
if (preg_match("/^..:..:..:..:..:..$/",$lldpport) > 0) {
$lldpport = cacti_snmp_get($dpdiscovered['dphost'][$lldphost]['ip'],
$searchme['snmp_community'], $lldpLocPortDesc_OID.".".$lldpnames[$i]['portindex'],
$searchme['snmp_version'], $searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'], $searchme['snmp_port'],
$searchme['snmp_timeout'], $snmp_retries, $searchme['max_oids'], SNMP_POLLER);
}
// GD Nexus 7000 doesn't have local port info in LLDP MIB. Try inName
if(!$lldpport) {
$lldpport = cacti_snmp_get($dpdiscovered['dphost'][$lldphost]['ip'],
$searchme['snmp_community'], $ifName_OID.".".$lldpnames[$i]['portindex'],
$searchme['snmp_version'], $searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'], $searchme['snmp_port'],
$searchme['snmp_timeout'], $snmp_retries, $searchme['max_oids'], SNMP_POLLER);
if(!$lldpport) {
return FALSE;
}
}
if(!isset($dpdiscovered['dphost'][$lldpnames[$i]['description']])) {
if(isset($dpdiscovered['ip'][$lldpnames[$i]['ip']])) {
DPChange_Name($lldpnames[$i]);
}else{
$dpdiscovered['dphost'][$lldpnames[$i]['description']] = $lldpnames[$i];
$dpdiscovered['dphost'][$lldpnames[$i]['description']]['protocol'] = "LLDP";
$dpdiscovered['dphost'][$lldpnames[$i]['description']]['parent'] = $lldphost;
$dpdiscovered['dphost'][$lldpnames[$i]['description']]['port'] = $lldpport;
$dpdiscovered['dphost'][$lldpnames[$i]['description']]['added'] = 0;
$dpdiscovered['hostname'][$lldpnames[$i]['hostname']] = $lldpnames[$i]['description'];
$dpdiscovered['description'][$lldpnames[$i]['description']] = $lldpnames[$i]['description'];
if(is_ipv4($lldpnames[$i]['ip']) || is_ipv6($lldpnames[$i]['ip'])) {
$dpdiscovered['ip'][$lldpnames[$i]['ip']] = $lldpnames[$i]['description'];
}
$answer[] = $lldpnames[$i];
}
}elseif (!isset($dpdiscovered['dphost'][$lldpnames[$i]['description']]['parent'])) {
dpdiscover_debug("Just adding parent - ".$lldpnames[$i]['description']." $lldphost $lldpport\n");
$dpdiscovered['dphost'][$lldpnames[$i]['description']]['parent'] = $lldphost;
$dpdiscovered['dphost'][$lldpnames[$i]['description']]['port'] = $lldpport;
}
}
return $answer;
}
// Try CDP.
function CDP_Discovery($searchme) {
global $dpdiscovered;
$cdpInterfaceName_OID = ".1.3.6.1.4.1.9.9.23.1.1.1.1.6";
$cdpCacheDeviceId_OID = ".1.3.6.1.4.1.9.9.23.1.2.1.1.6";
$ifName_OID = ".1.3.6.1.2.1.31.1.1.1.1";
$snmp_retries = read_config_option("snmp_retries");
$cdphost = get_shorthost($searchme['description']);
$cdpnames = cacti_snmp_walk($dpdiscovered['dphost'][$cdphost]['ip'],
$searchme['snmp_community'], $cdpCacheDeviceId_OID, $searchme['snmp_version'],
$searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'],
$searchme['snmp_port'], $searchme['snmp_timeout'], $snmp_retries,
$searchme['max_oids'], SNMP_POLLER);
if (sizeof($cdpnames) == 0 || sizeof($cdpnames) === FALSE) {
dpdiscover_debug("No cdpnames found.\n");
return FALSE;
}
DP_setup($cdpnames);
dpdiscover_debug("CDP: Walked $cdphost ".$dpdiscovered['dphost'][$cdphost]['ip']."\n");
$answer = array();
for ($i=0; $i<sizeof($cdpnames); $i++) {
if($cdpnames[$i]['description'] == "") {
continue;
}
if (isset($dpdiscovered['dphost'][$cdpnames[$i]['description']]['parent'])) {
continue;
}
$cdpport = cacti_snmp_get($dpdiscovered['dphost'][$cdphost]['ip'],
$searchme['snmp_community'], $cdpInterfaceName_OID.".".$cdpnames[$i]['portindex'],
$searchme['snmp_version'], $searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'], $searchme['snmp_port'],
$searchme['snmp_timeout'], $snmp_retries, $searchme['max_oids'], SNMP_POLLER);
// GD Nexus 7000 doesn't have local port info in LLDP MIB. Try inName
if(!$cdpport) {
$cdpport = cacti_snmp_get($dpdiscovered['dphost'][$cdphost]['ip'],
$searchme['snmp_community'], $ifName_OID.".".$cdpnames[$i]['portindex'],
$searchme['snmp_version'], $searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'], $searchme['snmp_port'],
$searchme['snmp_timeout'], $snmp_retries, $searchme['max_oids'], SNMP_POLLER);
if(!$cdpport) {
return FALSE;
}
}
if(!isset($dpdiscovered['dphost'][$cdpnames[$i]['description']])) {
if(isset($dpdiscovered['ip'][$cdpnames[$i]['ip']])) {
DPChange_Name($cdpnames[$i]);
}else{
$dpdiscovered['dphost'][$cdpnames[$i]['description']] = $cdpnames[$i];
$dpdiscovered['dphost'][$cdpnames[$i]['description']]['protocol'] = "CDP";
$dpdiscovered['dphost'][$cdpnames[$i]['description']]['parent'] = $cdphost;
$dpdiscovered['dphost'][$cdpnames[$i]['description']]['port'] = $cdpport;
$dpdiscovered['dphost'][$cdpnames[$i]['description']]['added'] = 0;
$dpdiscovered['hostname'][$cdpnames[$i]['hostname']] = $cdpnames[$i]['description'];
$dpdiscovered['description'][$cdpnames[$i]['description']] = $cdpnames[$i]['description'];
if(is_ipv4($cdpnames[$i]['ip']) || is_ipv6($cdpnames[$i]['ip'])) {
$dpdiscovered['ip'][$cdpnames[$i]['ip']] = $cdpnames[$i]['description'];
}
$answer[] = $cdpnames[$i];
}
}elseif (!isset($dpdiscovered['dphost'][$cdpnames[$i]['description']]['parent'])) {
dpdiscover_debug("Just adding parent - ".$cdpnames[$i]['description']." $cdphost $cdpport\n");
$dpdiscovered['dphost'][$cdpnames[$i]['description']]['parent'] = $cdphost;
$dpdiscovered['dphost'][$cdpnames[$i]['description']]['port'] = $cdpport;
}
}
return $answer;
}
// Try FDP
function FDP_Discovery($searchme) {
global $dpdiscovered;
$snFdpCacheDeviceId_OID = ".1.3.6.1.4.1.1991.1.1.3.20.1.2.1.1.3";
$ifName_OID = ".1.3.6.1.2.1.31.1.1.1.1";
$snmp_retries = read_config_option("snmp_retries");
$fdphost = get_shorthost($searchme['description']);
$fdpnames = cacti_snmp_walk($dpdiscovered['dphost'][$fdphost]['ip'],
$searchme['snmp_community'], $snFdpCacheDeviceId_OID, $searchme['snmp_version'],
$searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'],
$searchme['snmp_port'], $searchme['snmp_timeout'], $snmp_retries,
$searchme['max_oids'], SNMP_POLLER);
if (sizeof($fdpnames) == 0 || sizeof($fdpnames) === FALSE) {
dpdiscover_debug("No fdpnames found.\n");
return FALSE;
}
DP_setup($fdpnames);
dpdiscover_debug("FDP: Walked $fdphost ".$dpdiscovered['dphost'][$fdphost]['ip']."\n");
$answer = array();
for ($i=0; $i<sizeof($fdpnames); $i++) {
if($fdpnames[$i]['description'] == "") {
continue;
}
if (isset($dpdiscovered['dphost'][$fdpnames[$i]['description']]['parent'])) {
continue;
}
$fdpport = cacti_snmp_get($dpdiscovered['dphost'][$fdphost]['ip'],
$searchme['snmp_community'], $ifName_OID.".".$fdpnames[$i]['portindex'],
$searchme['snmp_version'], $searchme['snmp_username'], $searchme['snmp_password'],
$searchme['snmp_auth_protocol'], $searchme['snmp_priv_passphrase'],
$searchme['snmp_priv_protocol'], $searchme['snmp_context'], $searchme['snmp_port'],
$searchme['snmp_timeout'], $snmp_retries, $searchme['max_oids'], SNMP_POLLER);
if(!$fdpport) {
return FALSE;
}
if(!isset($dpdiscovered['dphost'][$fdpnames[$i]['description']])) {
if(isset($dpdiscovered['ip'][$fdpnames[$i]['ip']])) {
DPChange_Name($fdpnames[$i]);
}else{
$dpdiscovered['dphost'][$fdpnames[$i]['description']] = $fdpnames[$i];
$dpdiscovered['dphost'][$fdpnames[$i]['description']]['protocol'] = "FDP";
$dpdiscovered['dphost'][$fdpnames[$i]['description']]['parent'] = $fdphost;
$dpdiscovered['dphost'][$fdpnames[$i]['description']]['port'] = $fdpport;
$dpdiscovered['dphost'][$fdpnames[$i]['description']]['added'] = 0;
$dpdiscovered['hostname'][$fdpnames[$i]['hostname']] = $fdpnames[$i]['description'];
$dpdiscovered['description'][$fdpnames[$i]['description']] = $fdpnames[$i]['description'];
if(is_ipv4($fdpnames[$i]['ip']) || is_ipv6($fdpnames[$i]['ip'])) {
$dpdiscovered['ip'][$fdpnames[$i]['ip']] = $fdpnames[$i]['description'];
}
$answer[] = $fdpnames[$i];
}
}elseif (!isset($dpdiscovered['dphost'][$fdpnames[$i]['description']]['parent'])) {
dpdiscover_debug("Just adding parent - ".$fdpnames[$i]['description']." $fdphost $fdpport\n");
$dpdiscovered['dphost'][$fdpnames[$i]['description']]['parent'] = $fdphost;
$dpdiscovered['dphost'][$fdpnames[$i]['description']]['port'] = $fdpport;
}
}
return $answer;
}
// Add the device
function dpdiscover_add_device ($device) {
global $plugins, $config, $dpdiscovered, $debug;
$use_ip_for_hostname = read_config_option("dpdiscover_use_ip_hostname");
$use_fqdn_for_descr = read_config_option("dpdiscover_use_fqdn_for_description");
$template_id = $device['host_template_id'];
$snmp_sysName = preg_split('/[\s.]+/', $device['snmp_sysName'], -1, PREG_SPLIT_NO_EMPTY);
if($use_fqdn_for_descr == "on") {
$description = $device['hostname'];
}else{
$description = $device['description'];
}
if($use_ip_for_hostname == "on") {
$hostname = $device['ip'];
}else{
$hostname = $device['hostname'];
if(is_ipv6($device['ip'])) {
$hostname = "udp6:$hostname";
}
}
$community = sql_sanitize($device['snmp_community']);
$snmp_ver = sql_sanitize($device['snmp_version']);
$snmp_username = sql_sanitize($device['snmp_username']);
$snmp_password = sql_sanitize($device['snmp_password']);
$snmp_port = sql_sanitize($device['snmp_port']);
$snmp_timeout = sql_sanitize(read_config_option('snmp_timeout'));
$disable = false;
$availability_method = sql_sanitize(read_config_option("ping_method"));
$ping_method = sql_sanitize(read_config_option("ping_method"));
$ping_port = sql_sanitize(read_config_option("ping_port"));
$ping_timeout = sql_sanitize(read_config_option("ping_timeout"));
$ping_retries = sql_sanitize(read_config_option("ping_retries"));
$notes = 'Added by DPDiscover Plugin';
$snmp_auth_protocol = sql_sanitize($device['snmp_auth_protocol']);
$snmp_priv_passphrase = sql_sanitize($device['snmp_priv_passphrase']);
$snmp_priv_protocol = sql_sanitize($device['snmp_priv_protocol']);
$snmp_context = sql_sanitize($device['snmp_context']);
$device_threads = 1;
$max_oids = sql_sanitize($device['max_oids']);
if($debug === FALSE) {
$host_id = api_device_save(0, $template_id, $description, $hostname,
$community, $snmp_ver, $snmp_username, $snmp_password,
$snmp_port, $snmp_timeout, $disable, $availability_method,
$ping_method, $ping_port, $ping_timeout, $ping_retries,
$notes, $snmp_auth_protocol, $snmp_priv_passphrase,
$snmp_priv_protocol, $snmp_context, $max_oids, $device_threads);
}else{
dpdiscover_debug("Host not added - debugging: $description $hostname\n");
$host_id = 1;
}
$dpdiscovered['dphost'][$device['description']]['added'] = 1;
return $host_id;
}
function dpdiscover_recreate_tables () {
dpdiscover_debug("Request received to recreate the LLDiscover Plugin's tables\n");
dpdiscover_debug(" Dropping the tables\n");
db_execute("drop table plugin_dpdiscover_hosts");
dpdiscover_debug(" Creating the tables\n");
dpdiscover_setup_table ();
}
function dpdiscover_find_os($text) {
global $os;
if($text == "") {
return false;
}
for ($a = 0; $a < count($os); $a++) {
if (stristr($text, $os[$a]['sysdescr'])) {
return $os[$a];
}
}
return false;
}
function dpdiscover_debug($text) {
global $debug;
if ($debug) print $text;
}
function dpdiscover_valid_snmp_device (&$device, $parent) {
// Do we try our parent's data for SNMP checks?
$use_parent_snmp = read_config_option("dpdiscover_use_parent_snmp");
// Should we filter it?
$use_parent_snmp_filter = read_config_option("dpdiscover_parent_filter");