-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcfgmaker
More file actions
executable file
·2882 lines (2339 loc) · 107 KB
/
cfgmaker
File metadata and controls
executable file
·2882 lines (2339 loc) · 107 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
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
#! /usr/bin/perl -w
# -*- mode: Perl -*-
##################################################################
# MRTG 2.17.4 -- Config file creator
##################################################################
# Created by Tobias Oetiker <tobi@oetiker.ch>
# this produces an mrtg config file for one router or more routers
# by pulling info off the router via snmp
##################################################################
# Distributed under the GNU copyleft
# Copyright 2000 by Tobias Oetiker
##################################################################
# DEBUG TARGETS
# base - basic program flow
# snpo - SNMP Polling
# snpd - SNMP Detail
#@main::DEBUG=qw(base);
@main::DEBUG=qw(base snpo snpd);
require 5.005;
use strict;
BEGIN {
# Automatic OS detection ... do NOT touch
if ( $^O =~ /^(?:(ms)?(dos|win(32|nt)?))/i ) {
$main::OS = 'NT';
$main::SL = '\\';
$main::PS = ';';
} elsif ( $^O =~ /^NetWare$/i ) {
$main::OS = 'NW';
$main::SL = '/';
$main::PS = ';';
} elsif ( $^O =~ /^VMS$/i ) {
$main::OS = 'VMS';
$main::SL = '.';
$main::PS = ':';
} else {
$main::OS = 'UNIX';
$main::SL = '/';
$main::PS = ':';
}
}
use FindBin;
use lib "${FindBin::Bin}";
use lib "${FindBin::Bin}${main::SL}..${main::SL}lib${main::SL}mrtg2";
use MRTG_lib "2.100015";
use Getopt::Long;
use Pod::Usage;
use Socket;
sub main {
# assign defaults
my %opt = (
'enable-ipv6' => 0,
'use-16bit' => 0,
'community' => 'public',
'interfaces' => 1,
'enablesnmpv3' => 0,
# 'snmp-options' => ':::::2'
);
my %routers;
my %confcache;
my $ipv4only;
my %v3opt;
$opt{fullcmd} =
"$0 ".(join " ",
map {$_ =~ /[ \[\]\*\{\}\;\>\<\&]/ ? qq{"$_"} : $_ } @ARGV);
options(\%opt,\%routers);
# Check for IPv6 libraries if IPv6 is enabled.
# If the check fails, IPv6 is disabled.
$ipv4only = 1;
if ($opt{'enable-ipv6'} == 1) {
if ((eval {local $SIG{__DIE__};require Socket6;}) && (eval {local $SIG{__DIE__};require IO::Socket::INET6;})) {
debug ('base',"IPv6 libraries found, IPv6 enabled.");
$ipv4only = 0;
} else {
warn "WARNING: IPv6 libraries not found, IPv6 disabled.\n";
$opt{'enable-ipv6'} = 0;
}
}
if ($opt{'use-16bit'} == 1) {
warn "WARNING: Using 16bit RequestIDs\n";
no warnings;
$SNMP_Session::default_use_16bit_request_ids=1;
}
# Check for SNMP V3
#
if (exists($opt{username}) or lc($opt{enablesnmpv3}) eq "yes" ) {
if (eval {local $SIG{__DIE__};require Net_SNMP_util;}) {
import Net_SNMP_util;
debug('base', "SNMP V3 libraries found, SNMP V3 enabled.");
$opt{enablesnmpv3} = "yes";
push @{$opt{global}}, "enablesnmpv3: yes";
} else {
warn "WARNING: SNMP V3 libraries not found, SNMP V3 disabled. Falling back to V2c.\n";
require SNMP_util;
import SNMP_util;
$opt{enablesnmpv3} = "revert";
}
}
else {
require SNMP_util;
import SNMP_util;
$opt{enablesnmpv3} = "no";
}
init();
foreach my $router
(sort
{($routers{$a}{noofrouter}) <=> ($routers{$b}{noofrouter})}
keys %routers)
{
my @snmpopt = split(/:/,$routers{$router}{'snmp-options'});
if ($snmpopt[5] and $snmpopt[5] == 3) {
if ($opt{enablesnmpv3} eq "revert") {
$snmpopt[5] = 2;
warn "reverting to snmpV2c for router $router\n";
$routers{$router}{'snmp-options'} = join(":",@snmpopt);
$routers{$router}{snmpopt_current} = $routers{$router}{'snmp-options'};
} else {
die "SNMP V3 requires a --username parameter as part of the User Security Model for router $routers{$router}{routerkey}" if $opt{enablesnmpv3} eq "no";
$routers{$router}{enablesnmpv3} = $opt{enablesnmpv3};
%v3opt = parsev3(\%opt);
}
} else {
debug('base',"snmpv3 available, but using v1/v2c for $routers{$router}{routerkey}") if $opt{enablesnmpv3} eq "yes";
}
# pod2usage(-verbose=>1,-message=>"ERROR: Could not Parse $router\n")
# unless $router =~ /.*\@.*/;
debug('base',"Get Device Info on $router");
$routers{$router}{ipv4only} = $ipv4only;
if ( my $devinfo = DeviceInfo($router,\%routers,\%v3opt) ) {
$routers{$router}{deviceinfo} = $devinfo;
} else {
warn "WARNING: Skipping $router as no info could be retrieved\n\n";
sleep 5;
next;
}
if ($opt{interfaces}) {
debug('base',"Populating confcache");
populateconfcache(\%confcache,$router,$routers{$router}{ipv4only},1,\%v3opt);
debug('base',"Get Interface Info");
InterfaceInfo(\%confcache,\%routers,$router,\%opt,\%v3opt);
}
}
GenConf(\%opt,\%routers,\%confcache,\%v3opt);
} # end main
main;
exit 0;
sub InterfaceInfo($$$$$) {
my $confcache = shift;
my $routers = shift;
my $router = shift;
my $opt = shift;
my $v3opt = shift;
my @Variables = qw (ifIndex ifType ifAdminStatus ifOperStatus ifMtu);
my $snmphost = v4onlyifnecessary($router, $routers->{$router}{ipv4only});
if ($routers->{$router}{deviceinfo}{Vendor} eq 'cisco' &&
$routers->{$router}{deviceinfo}{sysDescr} =~ m/Version\s+(\d+\.\d+)/) {
push @Variables, ($1 > 11.0 or $1 < 10.0 ) ? "ifAlias" : "CiscolocIfDescr";
if ($1 > 11.2) {push @Variables, "vmVlan";};
if ($1 > 11.3) {push @Variables, "vlanTrunkPortDynamicStatus";};
} elsif ( $routers->{$router}{deviceinfo}{Vendor} =~ /(?:hp|juniper|dlink|wwp|foundry|dellLan|force10|3com|extremenetworks|openBSD|arista|enterasys|zyxel|vyatta)/i) {
push @Variables, "ifAlias";
}
my $descr = $routers->{$router}{deviceinfo}{sysDescr};
if ($routers->{$router}{deviceinfo}{Vendor} eq 'cisco' &&
$descr =~ m/Catalyst\sOperating\sSystem|Cisco\sSystems\sWS-C2900/ ) {
push @Variables, "CiscoCatalystPortName";
push @Variables, "vmVlan";
}
if ($routers->{$router}{deviceinfo}{Vendor} eq 'cisco' &&
$descr =~ m/Catalyst/ ) {
push @Variables, "vlanTrunkPortDynamicStatus";
}
if ($descr =~ m/Passport-8610/ || $descr =~ m/MERS-8610/ ) {
push @Variables, "ppPortName";
}
foreach my $var (@Variables) {
debug('base',"Walking $var");
foreach my $tuple (snmpwalk($snmphost,$v3opt, $var)){
my($if,$value) = split /:/, $tuple, 2;
$value =~ s/[\0- ]+$//; # no trailing space
$routers->{$router}{$if}{$var} = $value;
debug('snpd'," $router -> $if -> $var = $value");
}
}
# interface speed var depends on snmp version
my $snmp_version = (split(':', $router, 6))[5] || 1;
if ( $snmp_version =~ /[23]/ ) {
debug('base',"Walking ifSpeed");
my @ifSpeed = snmpwalk($snmphost, $v3opt,'ifSpeed');
debug('snpd',"\@ifSpeed = @ifSpeed\n");
debug('base',"Walking ifHighSpeed");
my @ifHighSpeed = snmpwalk($snmphost,$v3opt, 'ifHighSpeed');
for ( my $i=0; $i<=$#ifSpeed; $i++ ) {
my ($if,$value) = split /:/, $ifSpeed[$i], 2;
# the mib entry on ifSpeed says
# "An estimate of the interface's current bandwidth in bits
# per second. For interfaces which do not vary in bandwidth
# or for those where no accurate estimation can be made, this
# object should contain the nominal bandwidth. If the
# bandwidth of the interface is greater than the maximum value
# reportable by this object then this object should report its
# maximum value (4,294,967,295) and ifHighSpeed must be used
# to report the interace's speed. For a sub-layer which has
# no concept of bandwidth, this object should be zero."
if ( (not defined $value) || ($value == 2**32-1) ) {
($if, $value) = split /:/, $ifHighSpeed[$i], 2;
$value = $value * 1000000; # highSpeed = contador * 10^6
debug('base',"Speed: $if - $value");
}
if ( ($descr =~ m/MERS-8610/ ) && (defined $ifHighSpeed[$i]) ) {
($if, $value) = split /:/, $ifHighSpeed[$i], 2;
$value = $value * 1000000; # highSpeed = contador * 10^6
debug('base',"Speed: $if - $value");
}
$routers->{$router}{$if}{'ifSpeed'} = $value;
}
} else {
debug('base',"Walking ifSpeed");
foreach my $tuple (snmpwalk($snmphost,$v3opt, 'ifSpeed')){
my($if,$value) = split /:/, $tuple, 2;
$routers->{$router}{$if}{'ifSpeed'} = $value;
debug('snpd'," $router -> $if -> ifSpeed = $value");
}
}
# magic speed determination for portmaster IFs
if ($routers->{$router}{deviceinfo}{Vendor} eq 'portmaster') {
# We can only approximate speeds
#
# so we think that ppp can do 76800 bit/s, and slip 38400.
# (actualy, slip is a bit faster, but usualy users with newer modems
# use ppp). Alternatively, you can set async speed to 115200 or
# 230400 (the maximum speed supported by portmaster).
#
# But if the interface is ptpW (sync), max speed is 128000
# change it to your needs. On various Portmasters there are
# various numbers of sync interfaces, so modify it.
#
# The most commonly used PM-2ER has only one sync.
#
# Paul Makeev (mac@redline.ru)
#
foreach my $if (keys %{$routers->{$router}}) {
next unless $if =~ /^\d+$/;
my $ift = $routers->{$router}{$if}{ifType};
my $ifd = $routers->{$router}{$if}{Descr};
if ($ift == 23) {
if ($ifd eq 'ptpW1') {
$routers->{$router}{$if}{ifSpeed} = 128000;
} else {
$routers->{$router}{$if}{ifSpeed} = 76800;
}
} elsif ($ift == 28) {
$routers->{$router}{$if}{ifSpeed} = 38400;
} elsif ($ift == 6) {
$routers->{$router}{$if}{ifSpeed} = 10000000;
}
}
}
# match confcache info into tree
my $cachekey = cleanhostkey $router;
foreach my $method (keys %{$$confcache{$cachekey}}) {
foreach my $key (keys %{$$confcache{$cachekey}{$method}}) {
my $if = readfromcache($confcache,$router,$method,$key);
next unless $if =~ /^\d+$/;
$routers->{$router}{$if}{$method} = $key;
for ($method) {
#fix special chars in ifdescr
# no need for this we fix if references later on
# /^Descr|Name$/ && do {
# $routers->{$router}{$if}{"R$method"} = $routers->{$router}{$if}{$method};
# $routers->{$router}{$if}{$method} =~ s/(:)/\\$1/g;
# next;
# };
#find hostname of IF
!$$opt{noreversedns} && /^Ip$/ and do {
my $name =
gethostbyaddr(
pack('C4',
split(/\./,
$routers->{$router}{$if}{$method})),
AF_INET);
$routers->{$router}{$if}{DNSName} = ($name or "");
next;
};
}
}
}
} # end InterfaceInfo
sub GenConf ($$$$) {
my $opt = shift;
my $routers = shift;
my $confcache = shift;
my $v3opt = shift;
my $conf = "# Created by \n# $$opt{fullcmd}\n\n";
# print global options
$conf .= <<ECHO;
### Global Config Options
# for UNIX
# WorkDir: /home/http/mrtg
# or for NT
# WorkDir: c:\\mrtgdata
### Global Defaults
# to get bits instead of bytes and graphs growing to the right
# Options[_]: growright, bits
ECHO
# Add EnableIPv6 option to configuration file
if ($$opt{'enable-ipv6'} == 1) {
$conf .= "EnableIPv6: yes\n";
} else {
$conf .= "EnableIPv6: no\n";
}
foreach my $router
(sort {($$routers{$a}{noofrouter}) <=> ($$routers{$b}{noofrouter})}
keys %$routers ) {
my $router_ref = $$routers{$router};
my $router_opt = $$router_ref{opt};
my $router_dev = $$router_ref{deviceinfo};
# Did any global options appear on the command line
# before this router? If so, include them into the
# configuration file.
if (defined $$router_opt{global}) {
foreach my $key (@{$$router_opt{global}}) {
$conf .= "$key\n";
}
}
# If IPv6 is enabled, add IPv4Only target directive for targets
# that do not support SNMP over IPv6.
my $ipv4only_directive;
my $router_ipv4only = ($$opt{'enable-ipv6'} == 1) && $$router_ref{ipv4only};
my $syscontact = $$router_dev{sysContact};
my $html_syscontact = html_escape($syscontact);
my $syslocation = $$router_dev{sysLocation};
my $html_syslocation = html_escape($syslocation);
my $sysname = $$router_dev{sysName};
my $sysdescr = $$router_dev{sysDescr};
my $comment_sysdescr = $sysdescr;
# make sure embeded newlines do not harm us here
$comment_sysdescr =~ s/[\n\r]+/\n\# /g;
my $community = $$router_ref{community};
$community =~ s/([@ ])/\\$1/g;
my $router_connect = "$community\@$$router_ref{routername}$$router_ref{snmpopt_current}";
my @v3options;
foreach my $v3op (keys %$v3opt) {
push @v3options, $v3op."=>'".$$v3opt{$v3op}."'";
}
my $v3options = join(",",@v3options) if $$router_ref{'snmp-options'} =~ /(?::[^:]*){4}:3/ ;
my $html_sysdescr = html_escape($sysdescr);
my $router_name =
($$router_ref{routername}
. (($$router_ref{'dns-domain'})?'.':'')
. $$router_ref{'dns-domain'});
# James Overbeck 2001/09/20
# Moved $directory_name definition from within the interface
# foreach loop to here. In its previous location, $directory_name
# was not accessible to host templates. $directory_name is not
# changed per-interface so it might as well be here instead of
# where it was.
my $directory_name = "";
if (defined $$router_opt{subdirs}) {
$directory_name = $$router_opt{subdirs};
$directory_name =~ s/HOSTNAME/$router_name/g;
$directory_name =~ s/SNMPNAME/$$router_dev{sysName}/g;
}
my $target_lines = "";
my $problem_lines = "";
my $head_lines = "
######################################################################
# System: $sysname
# Description: $comment_sysdescr
# Contact: $syscontact
# Location: $syslocation
######################################################################
";
my $separator_lines = "\n\n";
# Host specific config lines generation code starts HERE
if(defined $$router_opt{'host-template'}) {
# First test if the file exists and is readable, die if not.
die "File $$router_opt{'host-template'} didn't exist.\n"
unless (-e $$router_opt{'host-template'}
and -r $$router_opt{'host-template'});
# Open the file (or die).
open IF_TEMPLATE, $$router_opt{'host-template'}
or die "File $$router_opt{'host-template'} couldn't be opened.\n";
my @template_lines = readline *IF_TEMPLATE;
close IF_TEMPLATE;
$@ = undef;
eval ('local $SIG{__DIE__};'.join("", @template_lines));
die "ERROR Evaluation of the contents in the file \n\n".
"$$router_opt{'host-template'}\ngave the error \n\n\"$@\"\n\nExiting cfgmaker\n" if $@;
}
$conf .= ($head_lines
. $problem_lines
. $target_lines
. $separator_lines);
# Host specific config lines generation code ends HERE
if ($$router_opt{'interfaces'}) {
foreach my $ifindex (sort {int($a) <=> int($b)} grep /^\d+$/, keys %$router_ref) {
my $i = $$router_ref{$ifindex};
# Now define a number of variables used for this interface.
# Some variables are just used internally by cfgmaker to
# process the interface, others are provided for usage in
# host and interface templates and for interface filters.
my $if_index = $ifindex;
my $if_eth = $$i{Eth} || 'No Ethernet Id';
# does it make sense to look at the interface ?
my @prob;
my $default_ifstate = 1; # State assumed up.
my $default_iftype = 1; # iftype assumed ok.
my $if_ok = 1; #
my $if_admin = (($$i{ifAdminStatus} || 0) == 1);
my $if_oper = (($$i{ifOperStatus} || 0) == 1);
my $if_type = $$i{ifType} || -1;
my $if_is_ethernet = 0 < scalar(grep {$_ == $if_type;}
(6,7,26,62,69,117));
my $if_is_isdn = (0 < scalar (grep {$_ == $if_type;}
(20,21,63,75,76,77)));
my $if_is_dialup = $if_is_isdn ||
(0 < scalar (grep {$_ == $if_type;}
(23,81,82,108)));
my $if_is_atm = (0 < scalar(grep {$_ == $if_type;}
(37,49,107,105,106,114,134)));
my $if_is_wan = 0 < scalar(grep {$_ == $if_type;}
(22,30,32,39,44,46));
my $if_is_lan = $if_is_ethernet ||
(0 < scalar (grep {$_ == $if_type;}
(8,9,11,15,26,55,59,60,115)));
my $if_is_dsl = (0 < scalar(grep {$_ == $if_type;}
(94,95,96,97)));
my $if_is_loopback = $if_type == 24;
my $if_is_ciscovlan =
($$router_dev{Vendor} eq 'cisco'
and $$i{Descr} =~ /^(unrouted )?[- ]?VLAN[- ]?\d*$/i);
my $if_ip = $$i{Ip} || 'No Ip';
my $if_snmp_descr = $$i{Descr} || 'No Description';
$if_snmp_descr =~ s/\n$//; # no you don't want to know who does this
# ok ... dell 3524
$if_snmp_descr =~ s/ /-/g;
my $if_type_num = $$i{ifType} || 'Unknown Type';
$$i{ifType} ||= -1;
my $if_snmp_name = $$i{Name} || 'No Name';
my $if_snmp_alias = $$i{ifAlias} || '';
my $if_cisco_descr = $$i{CiscolocIfDescr} || '';
my $if_dns_name = $$i{DNSName} || 'No DNS name';
my $if_vlan_id = $$i{vmVlan} || 'No Vlan';
my $if_cisco_trunk = ($$i{vlanTrunkPortDynamicStatus} || 0 == 1);
my $if_MTU = $$i{ifMtu} || 'No MTU';
# For Nokia IPSO, find non-ethernet interfaces with IP addresses
# and add missing MAC address and Port Speed information to
# to the LOGICAL and LOGICAL+VLAN interfaces.
if ( $$router_dev{Vendor} eq 'nokiaipsofw' ) {
if ($$i{ifType} ne "6" and
$$router_dev{sysDescr} =~ / IPSO / &&
$$i{Ip} =~ /^\d+/ and
(not $$i{Eth} or
not $$i{ifSpeed} or
$$i{ifSpeed} < 10 )
) {
my $logical_if_name = $$i{Name};
# Split the LOGICAL interface name in attempt
# to match with base PHYSICAL interface detail.
my ($logical_if_HEAD, $logical_if_TAIL) =
$logical_if_name =~ /^(.*)(c\d+)$/;
foreach my $ifindexTMP (sort {int($a) <=> int($b)}
grep /^\d+$/, keys %$router_ref) {
next unless $ifindexTMP =~ /^\d+$/;
my $physical_if_name = $$router_ref{$ifindexTMP};
if ($$physical_if_name{ifType} == 6 &&
$logical_if_HEAD eq $$physical_if_name{Name} ) {
$$i{Eth} ||= $$physical_if_name{Eth};
$$i{ifSpeed} = $$physical_if_name{ifSpeed}
if ( not $$i{ifSpeed} or $$i{ifSpeed} < 10 );
}
}
}
}
# First investigate the state of the interface.
if (not defined $$router_opt{'no-down'}) {
if (($$i{ifAdminStatus} || 0 )== 2) {
push @prob, "it is administratively DOWN";
$default_ifstate = 0;
} elsif (($$i{ifAdminStatus} || 0 ) == 3) {
push @prob, "it is in administrative TEST mode";
$default_ifstate = 0;
}
if (not defined $$router_opt{'show-op-down'}) {
if (($$i{ifOperStatus} || 0 ) == 2) {
push @prob, "it is operationally DOWN";
$default_ifstate = 0;
} elsif (($$i{ifOperStatus} || 0 ) == 3) {
push @prob, "it is in operational TEST mode";
$default_ifstate = 0;
}
}
}
# Investigate the type of the interface.
if ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 18) { # by fwo@obsidian.co.za
push @prob, "it is a DS1 controllers";
$default_iftype = 0;
} elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 19) { # by fwo@obsidian.co.za
push @prob, "it is a E1 controllers";
$default_iftype = 0;
} elsif ($$i{ifType} == 24) {
push @prob, "it is a Software Loopback interface" ;
$default_iftype = 0;
} elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 30) { # by blube@floridadigital.net
push @prob, "it is a DS3 controller";
$default_iftype = 0;
} elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 102) { # by dan.mcdonald@austinenergy.com
push @prob, "it is a Voice controller";
$default_iftype = 0;
} elsif ($$router_dev{Vendor} eq 'cisco' && $$i{ifType} == 103) { # by dan.mcdonald@austinenergy.com
push @prob, "it is a Voice dial peer";
$default_iftype = 0;
} elsif ($$i{ifType} == 162) {
push @prob, "it is a CEF Sub-interface"; # John Begley <maslow@mediaone.net>
} elsif ($$router_dev{Vendor} eq 'cisco'
and $$i{Descr} eq 'Null0') {
push @prob, "it is a cisco Null0 interface";
$default_iftype = 0;
}
my $default = $default_iftype && $default_ifstate;
# Do some futher investigation if the interface makes
# sense to collect on
# I debated whether to insert the zero-speed check before
# or after the "will always be zero" sections below.
# I settled on before since I'll assume the user knows
# what speed the zero-speed interfaces should be better
# than the simple logic below.
if (not $$i{ifSpeed} and $$router_opt{'zero-speed'}) {
# Set all interfaces with zero speed to the value specified
# by the --zero-speed= command line option.
# Be sure the value specified is a valid integer.
# It seems like this could be done once when
# $$router_opt is set, but I didn't see any example
# of input validation in that part of cfgmaker,
# so it gets done here, more times than are
# really necessary. ;-)
unless ($$router_opt{'zero-speed'} =~ /^\d+$/) {
die "ERROR: zero-speed specified with non-integer speed: $$router_opt{'zero-speed'}";
}
$$i{ifSpeed} = $$router_opt{'zero-speed'};
}
if (not $$i{ifSpeed} and $$router_dev{Vendor} eq 'foundry' and $$i{ifType} == 194) {
# foundry ATM subinterfaces always report 0 speed, make it 155Mbps instead.
$$i{ifSpeed} = 155000000;
} elsif (not $$i{ifSpeed} and $$router_dev{Vendor} eq 'foundry' and $$i{ifType} == 135) {
# Foundry virtual Ethernet interfaces always report 0 speed, make it 1GB instead.
$$i{ifSpeed} = 1000000000;
} elsif (not $$i{ifSpeed} and $$router_dev{Vendor} eq 'cisco' and $$i{sysDescr} =~ /FWSM-Firewall / ) {
# Cisco PIX Firewall Switch Modules have effective backplane speed of 600 Megs
$$i{ifSpeed} = 600000000;
} elsif (not $$i{ifSpeed} and $$router_dev{Vendor} eq '3com' and $$i{Descr} =~ /RMON VLAN (\d+)/ ) {
$$i{ifSpeed} = 100000000;
$if_vlan_id = $1;
} elsif (not $$i{ifSpeed}) {
push @prob, "has no ifSpeed property";
$$i{ifSpeed} = 0;
$if_ok = 0;
}
my $message;
my $nohc =0;
if ($message = IsCounterBroken($ifindex, $router_ref,$v3opt)) {
# set snmpopt_current to working snmp options
if ($message eq '1') {
$nohc = 1;
} else {
push @prob, "got '$message' from interface when trying to query";
$if_ok = 0;
}
}
my $community = $$router_ref{community};
$community =~ s/([@ ])/\\$1/g;
my $router_connect = "$community\@$$router_ref{routername}$$router_ref{snmpopt_current}";
my $v3options = join(",",@v3options) if $$router_ref{snmpopt_current} =~ /(?::[^:]*){4}:3/ ;
# determine interface reference
my $if_ref;
if (defined $$router_opt{ifref}) {
foreach (split /,/,$$router_opt{ifref}) {
/^ip$/ && do { if($$i{Ip} ){ $if_ref = "/".$$i{Ip}; last;} next};
/^eth$/ && do { if($$i{Eth} ){ $if_ref = "!".$$i{Eth}; last;} next};
/^descr?$/&& do { if($$i{Descr}){ $if_ref = "\\".$$i{Descr};last;} next};
/^name$/ && do { if($$i{Name} ){ $if_ref = "#".$$i{Name}; last;} next};
/^type$/ && do { if($$i{Type} ){ $if_ref = "%".$$i{Type}; last;} next};
/^nr$/ && do { $if_ref = $ifindex; last };
die "ERROR: Invalid value for --ifref: $$router_opt{ifref} ($_)\n";
}
if (not defined $if_ref) {
push @prob, "--ifref=$$router_opt{ifref} is not unique for this interface";
$if_ref = $ifindex;
$if_ok = 0;
}
} else {
$if_ref = $ifindex;
}
# generate Target name
my $trim_if_ref = $if_ref;
$trim_if_ref =~ s/[\#!\/\\:\s\@%]+/_/g;
$trim_if_ref =~ s/^_*(.+?)_*$/$1/;
my $target_name = "${router_name}_$trim_if_ref";
my $if_title_desc = $if_ref;
$if_title_desc =~ s/^[^\d]//;
my $if_speed = int($$i{ifSpeed} / 8);
my $if_speed_str = fmi($if_speed,$$router_ref{flags});
my $if_type_desc = IfType($$i{ifType});
my $html_if_type_desc = html_escape($if_type_desc);
my $desc_prefix = 'Traffic Analysis for ';
my $port_dot = $$i{Name} || 'Unknown';
$port_dot =~ s/\//./g;
my $if_port_name = $$router_ref{$port_dot}{CiscoCatalystPortName};
my $if_pp_port_name = $$router_ref{$ifindex}{ppPortName};
if (defined $$router_opt{ifdesc}) {
$desc_prefix = '';
foreach (split /,/,$$router_opt{ifdesc}) {
/^ip$/ && do { if($$i{Ip}) { $if_title_desc = $$i{Ip}; last } next };
/^eth$/ && do { if($$i{Eth}) { $if_title_desc = $$i{Eth}; last } next };
/^descr?$/ && do { if($$i{Descr}){ $if_title_desc = $if_snmp_descr; last } next };
/^alias$/ && do { if($$i{ifAlias}){ $if_title_desc = "$if_snmp_descr $if_snmp_alias $if_cisco_descr"; last } next };
/^name$/ && do { if($$i{Name}) {$if_title_desc = "#".$$i{Name}; last } next };
/^catname$/ && do {$if_title_desc = "$if_port_name"." (".$$i{Name}.($$i{vmVlan} ? " - VLAN ".$$i{vmVlan} : " - dot1q").")"; last};
/^ppname$/ && do {$if_title_desc = $if_pp_port_name; last};
/^type$/ && do { if($$i{Type}) { $if_title_desc = "%".$$i{Type}; last } next };
/^nr$/ && do {$if_title_desc = "Interface $ifindex"; last};
/^$/ && do {$if_title_desc = $if_type_desc || $if_snmp_descr; last };
die "ERROR: Invalid value for --ifdesc: '$$router_opt{ifdesc} ($_)'\n";
}
}
# Now setup a large number of variables needed for the
# generation of the configuration lines.
$if_title_desc =~ s/\\([:@\\\/\# ])/$1/g; # unescape
$if_title_desc = $if_snmp_name if not $if_title_desc;
my $html_if_title_desc = html_escape($if_title_desc);
my $html_desc_prefix = html_escape($desc_prefix);
my $html_if_snmp_descr = html_escape($if_snmp_descr);
my $html_if_snmp_name = html_escape($if_snmp_name);
my $html_if_snmp_alias = html_escape($if_snmp_alias);
my $html_if_cisco_descr = html_escape($if_cisco_descr);
my $if_description = "$if_snmp_descr $if_snmp_alias $if_cisco_descr";
my $html_if_description = html_escape($if_description);
my $if_title = "$desc_prefix$if_title_desc -- $sysname";
my $html_if_title = html_escape($if_title);
my $head_lines = "### Interface $ifindex >> Descr: '$if_snmp_descr' |".
" Name: '$if_snmp_name' | Ip: '$if_ip' | Eth: '$if_eth' ###\n";
my $target_lines = "";
my $separator_lines = "\n\n";
# escape the if reference
$if_ref =~ s/([& :])/\\$1/g;
my $default_target_directive = "Target[$target_name]: $if_ref:$router_connect";
$default_target_directive .= "\nSnmpOptions[$target_name]: $v3options" if $$router_ref{snmpopt_current} =~ /(?::[^:]*){4}:3/ ;
$default_target_directive .= "\nnoHC[$target_name]: yes" if $nohc == 1;
my $if_snmp_descr_save = $if_snmp_descr;
$if_snmp_descr_save =~ s/"/'/g;
my $default_setenv_directive = "SetEnv[$target_name]: MRTG_INT_IP=\"$if_ip\" MRTG_INT_DESCR=\"$if_snmp_descr_save\"";
my $default_directory_directive = ($directory_name ? "Directory[$target_name]: $directory_name" : "");
my $default_maxbytes_directive = "MaxBytes[$target_name]: $if_speed";
$ipv4only_directive = $router_ipv4only ? "IPv4Only[$target_name]: yes" : "";
my $default_title_directive = "Title[$target_name]: $html_desc_prefix$html_if_title_desc -- $sysname";
my $default_pagetop_directive =
"PageTop[$target_name]: <h1>$html_desc_prefix$html_if_title_desc -- $sysname</h1>
<div id=\"sysdetails\">
<table>
<tr>
<td>System:</td>
<td>$sysname in $html_syslocation</td>
</tr>
<tr>
<td>Maintainer:</td>
<td>$html_syscontact</td>
</tr>
<tr>
<td>Description:</td>
<td>$html_if_description</td>
</tr>
<tr>
<td>ifType:</td>
<td>$html_if_type_desc ($if_type_num)</td>
</tr>
<tr>
<td>ifName:</td>
<td>$html_if_snmp_name</td>
</tr>";
$default_pagetop_directive .= "
<tr>
<td>Port Name:</td>
<td>$if_port_name</td>
</tr>" if defined $if_port_name;
$default_pagetop_directive .= "
<tr>
<td>Port Name:</td>
<td>$if_pp_port_name</td>
</tr>" if defined $if_pp_port_name;
$default_pagetop_directive .= "
<tr>
<td>Max Speed:</td>
<td>$if_speed_str</td>
</tr>";
$default_pagetop_directive .= "
<tr>
<td>Ip:</td>
<td>$if_ip ($if_dns_name)</td>
</tr>" if $if_ip;
$default_pagetop_directive .= "
</table>
</div>";
my $default_target_lines =
("\n"
. $default_target_directive . "\n"
. $default_setenv_directive . "\n"
. ($default_directory_directive
? ($default_directory_directive . "\n")
: "")
. $default_maxbytes_directive . "\n"
. ($ipv4only_directive
? ($ipv4only_directive . "\n")
: "")
. $default_title_directive . "\n"
. $default_pagetop_directive . "\n");
# If --if-filter is provided, evalutat that. If it
# returns true, clear @prob. If it returns false,
# instead add a complaint to @prob.
if (defined $$router_opt{'if-filter'}) {
$@ = undef;
if (eval('local $SIG{__DIE__};'.$$router_opt{'if-filter'})) {
@prob = ();
} else {
push @prob, "filter specified by --if-filter rejected the interface";
$if_ok = 0;
}
die "ERROR: with if-filter $$router_opt{'if-filter'}: $@" if $@;
}
# issue problem report
my $problem_lines = "";
if (@prob) {
$problem_lines .= "### The following interface is commented out because:\n";
map {$problem_lines .= "### * $_\n"} @prob;
$if_ok = 0;
}
# The target config generation code starts HERE.
if (defined $$router_opt{'if-template'}) {
# First test if the file exists and is readable,
# die if not.
die "File $$router_opt{'if-template'} didn't exist.\n" unless (-e $$router_opt{'if-template'}
and -r $$router_opt{'if-template'});
# Open the file (or die).
open IF_TEMPLATE, $$router_opt{'if-template'}
or die "File $$router_opt{'if-template'} couldn't be opened.\n";
my @template_lines = readline *IF_TEMPLATE;
$@ = undef;
eval ('local $SIG{__DIE__};'.join( "", @template_lines));
die "Evaluation of the contents in the file \n\n$$router_opt{'if-template'}\n".
"gave the error \n\n\"$@\"\n\nExiting cfgmaker\n" if $@;
} else {
$target_lines = $default_target_lines;
}
if ($target_lines && not $if_ok) { # comment out the target lines if needed
$target_lines =~ s/^/\# /gm;
}
$conf .= ($head_lines
. $problem_lines
. $target_lines
. $separator_lines);
}
# Target generation code ends HERE.
}
}
# print any global options which might have
# appeared on the command line after the last
# router.
if (defined $$opt{global}) {
foreach my $key (@{$$opt{global}}) {
$conf .= "$key\n";
}
}
if ($$opt{output}) {
debug ('base', "Writing $$opt{output}");
open X, ">$$opt{output}" or die "ERROR: creating $$opt{output}: $!\n";
print X $conf;
close X;
} else {
print $conf;
}
} # end GenConf
sub IsCounterBroken ($$$) {
my $if = shift;
my $router_ref = shift;
my $v3opt = shift;
my $router = $$router_ref{routerkey};
my $fallback = 0;
local $SNMP_Session::suppress_warnings = 3;
local $Net_SNMP_util::suppress_warnings = 3;
my $ipv4only = $$router_ref{ipv4only};
my $snmphost = v4onlyifnecessary($router, $ipv4only);
if ($router =~ /:[\d.]*:[\d.]*:[\d.]*:[23]/) {
my $speed = (snmpget($snmphost, $v3opt, 'ifHighSpeed.'.$if))[0] || 'unknown';
debug('base',"snmpget $snmphost for ifHighSpeed.$if -> $speed Mb/s");
$SNMP_Session::errmsg = undef;
$Net_SNMP_util::ErrorMessage = undef;
my $counter = (snmpget($snmphost,$v3opt, 'ifHCInOctets.'.$if))[0] || 'unknown';
debug('base',"snmpget $snmphost for ifHCInOctets.$if -> $counter");
if( $speed eq 'unknown' or $counter !~ /^\d+$/ or $SNMP_Session::errmsg or $Net_SNMP_util::ErrorMessage){
$SNMP_Session::errmsg = undef;
$Net_SNMP_util::ErrorMessage = undef;
$fallback = 1;
debug('base',"check for HighspeedCounters failed ... Dropping back to V1");
} else {
return 0;
}
}
if ( $fallback == 1 or $$router_ref{snmpopt_current} !~ /:[\d.]*:[\d.]*:[\d.]*:[23]/) {
my $counter = (snmpget($snmphost, 'ifInOctets.'.$if))[0];
if (defined $SNMP_Session::errmsg) {
my $error = $SNMP_Session::errmsg;
$SNMP_Session::errmsg = undef;
$error =~ s/\n/\n### /g;
return $error;
} elsif (defined $Net_SNMP_util::ErrorMessage and $Net_SNMP_util::ErrorMessage =~ /\w/) {
my $error = $Net_SNMP_util::ErrorMessage;
$Net_SNMP_util::ErrorMessage = undef;
$error =~ s/\n/\n### /g;
return $error;
} elsif (not defined $counter or $counter eq '' or $counter =~ /\D/) {
return "No counter exists for $if";
}
}
return $fallback;
} # end IsCounterBroken
# DeviceInfo does fallback between IPv6 and IPv4: if an IPv6 snmpwalk returns
# undef values (= an error) and the target is a hostname, then it repeats the
# query using IPv4 in case the target does not support SNMP over IPv6.
# If DeviceInfo falls back to IPv4, it sets the ipv4only field for the target
# in the routers hash.
sub DeviceInfo ($$$) {
my $router=shift;
my $routers=shift;
my $v3opt=shift;
my %DevInfo;
my $ipv4only = $$routers{$router}{ipv4only};
my @variables = snmpwalk(v4onlyifnecessary($router, $ipv4only),$v3opt,'1.3.6.1.2.1.1'); # walk system
if (!(defined $variables[0])) {
# Do we need to fall back to IPv4?
my ($commmunity, $host) = ($1, $2) if ($router =~ /^(.*)@([^@]+)$/);
if ( ( ! $ipv4only ) && ( $host !~ /^\[(.*)\]/) ) {
# Not using IPv4, not an IPv6 address, so a hostname
debug ('base',"No response using IPv6 for $router, trying again using IPv4");
$$routers{$router}{ipv4only} = 1;
@variables = snmpwalk(v4onlyifnecessary($router, 1),$v3opt, '1.3.6.1.2.1.1');
}
}
if ( defined $variables[0] ) {
my (%DevInfo, %revOIDS);
if ($$routers{$router}{enablesnmpv3} || '' eq "yes") {
no warnings;
%revOIDS = reverse %Net_SNMP_util::OIDS;
}
else {
no warnings;
%revOIDS = reverse %SNMP_util::OIDS;
}
foreach my $variable ( @variables ) {
my ($oid, $value) = split ( ':', $variable, 2);
if ($revOIDS{'1.3.6.1.2.1.1.'.$oid}){
$DevInfo{ $revOIDS{'1.3.6.1.2.1.1.'.$oid} } = $value;
}
}
# vendor identification
my %vendorIDs = (
# Add your vendor here
# sysObjectID Vendora
'1.3.6.1.4.1.43.' => '3com',
'1.3.6.1.4.1.11.' => 'hp',
'1.3.6.1.4.1.9.' => 'cisco',
'1.3.6.1.4.1.171.' => 'dlink',
'1.3.6.1.4.1.6141.' => 'wwp',
'1.3.6.1.4.1.674.10895.' => 'dellLan',
'1.3.6.1.4.1.1916.' => 'extremenetworks',
'1.3.6.1.4.1.1991.' => 'foundry',
'1.3.6.1.4.1.6027.' => 'force10',
'1.3.6.1.4.1.2636.' => 'juniper',
'1.3.6.1.4.1.94.' => 'nokiaipsofw',
'1.3.6.1.4.1.307.' => 'portmaster',
'1.3.6.1.4.1.890.' => 'zyxel',
'1.3.6.1.4.1.2272.30' => 'nortel',
'1.3.6.1.4.1.30155.' => 'openBSD',
'1.3.6.1.4.1.30065.' => 'arista',
'1.3.6.1.4.1.5624.' => 'enterasys',
'1.3.6.1.4.1.30803.' => 'Vyatta',