Subversion Repositories sysadmin_scripts

Rev

Rev 134 | Rev 138 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 134 Rev 136
Line 59... Line 59...
59
# Added the ability to override or supplement arp and dns searches for mac addresses
59
# Added the ability to override or supplement arp and dns searches for mac addresses
60
# 20230323 RWR v2.3.0
60
# 20230323 RWR v2.3.0
61
# Added ability to have one or more non_router files, with mac/name/ip in it in case arp lookup failed
61
# Added ability to have one or more non_router files, with mac/name/ip in it in case arp lookup failed
62
# added script to create static html file in addition to csv
62
# added script to create static html file in addition to csv
63
#
63
#
64
# 20230327 RWR v3.0.0
64
# 20230324 RWR v3.0.0
65
# rewrite to make things more efficient, and store data in different format. You MUST remove old yaml file
65
# rewrite to make things more efficient, and store data in different format. You MUST remove old yaml file
66
# before running this.
66
# before running this.
-
 
67
#
-
 
68
# 20230325 RWR v3.0.1
-
 
69
# Added ability to delete entries which haven't been seen since $config->{'ttl'} seconds. ttl can be
-
 
70
# an integer followed by a unit, with units being single char h,d,w,m,y (hours, days, weeks, months, years)
-
 
71
# added ability to set so it will only refresh dns and device names every $refresh seconds, decreasing
-
 
72
# run time to 33% value when not used. Test system decreases from 15s to 5-7s
-
 
73
# reorganized so system runs smoother (test system decreased from 40 seconds to 15)
-
 
74
 
-
 
75
# for Debian, to get the required libraries
-
 
76
# apt install libyaml-tiny-perl libnet-dns-perl snmp
67
 
77
 
68
use strict;
78
use strict;
69
use warnings;
79
use warnings;
70
use Data::Dumper; # only used for debugging
80
use Data::Dumper; # only used for debugging
71
use Socket; # for reverse dns entries
81
use Socket; # for reverse dns entries
72
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
82
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
73
use File::Spec; # find location of script so we can put storage in same directory
83
use File::Spec; # find location of script so we can put storage in same directory
74
use File::Basename;
84
use File::Basename;
-
 
85
use Net::DNS; # dns lookup, apt install libnet-dns-perl
-
 
86
 
75
 
87
 
76
my $DEBUG = 0;
88
my $DEBUG = 0;
77
 
89
 
78
# define the version number
90
# define the version number
79
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
91
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
80
use version;
92
use version;
81
our $VERSION = version->declare("v3.3.0");
93
our $VERSION = version->declare("v3.3.1");
82
 
94
 
83
# see https://perldoc.perl.org/Getopt/Long.html
95
# see https://perldoc.perl.org/Getopt/Long.html
84
use Getopt::Long;
96
use Getopt::Long;
85
# allow -vvn (ie, --verbose --verbose --dryrun)
97
# allow -vvn (ie, --verbose --verbose --dryrun)
86
Getopt::Long::Configure ("bundling");
98
Getopt::Long::Configure ("bundling");
Line 93... Line 105...
93
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
105
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
94
# put the statefile in there
106
# put the statefile in there
95
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
107
my $STATEFILE = $scriptDir . '/mapSwitches.yaml';
96
my $CONFIGFILE = $scriptDir . '/mapSwitches.config.yaml';
108
my $CONFIGFILE = $scriptDir . '/mapSwitches.config.yaml';
97
 
109
 
-
 
110
 
-
 
111
 
98
my $SWITCHPORTMIB  = 'iso.3.6.1.2.1.2.2.1.8'; # returns port number and state, with a state of '1' meaning operational
112
my $SWITCHPORTMIB  = 'iso.3.6.1.2.1.2.2.1.8'; # returns port number and state, with a state of '1' meaning operational
99
my $SWITCHMACMIB   = 'iso.3.6.1.2.1.17.4.3.1.1'; # dot1dTpFdbAddress, aka mac addresses
113
my $SWITCHMACMIB   = 'iso.3.6.1.2.1.17.4.3.1.1'; # dot1dTpFdbAddress, aka mac addresses
100
my $SWITCHIFALIASMIB = 'iso.3.6.1.2.1.31.1.1.1.18'; # ifAlias, the the 'alias' field
114
my $SWITCHIFALIASMIB = 'iso.3.6.1.2.1.31.1.1.1.18'; # ifAlias, the the 'alias' field
101
my $SWITCHIFDESCMIB = 'iso.3.6.1.2.1.2.2.1.2'; # ifDescr, the "description" field
115
my $SWITCHIFDESCMIB = 'iso.3.6.1.2.1.2.2.1.2'; # ifDescr, the "description" field
102
my $BRIDGEPORTSMIB = 'iso.3.6.1.2.1.17.4.3.1.2';
116
my $BRIDGEPORTSMIB = 'iso.3.6.1.2.1.17.4.3.1.2';
Line 152... Line 166...
152
      }
166
      }
153
   }
167
   }
154
   return $return;
168
   return $return;
155
}
169
}
156
 
170
 
-
 
171
# do a reverse dns lookup of an IP address and return the hostname
-
 
172
# note that $dns is an instance of Net::DNS
-
 
173
sub getHostName {
-
 
174
   my ($dns, $ip ) = @_;
-
 
175
   foreach my $resource ( @$dns ) {
-
 
176
      my  $reply = $resource->search( $ip );
-
 
177
      next unless defined $reply;
-
 
178
      my @answer = grep { ! /^((;.*)|(\s*))$/ } split( "\n", $reply->string );
-
 
179
      $answer[0] =~ m/\s([a-z0-9.-]+)\.$/;
-
 
180
      return $1;
-
 
181
   }
-
 
182
   return '';
157
 
183
}
158
 
184
 
159
# Load configuration file, die if we could not find it
185
# Load configuration file, die if we could not find it
160
# config file is a single page YAML file
186
# config file is a single page YAML file
161
sub loadConfig {
187
sub loadConfig {
162
   my $filename = shift;
188
   my $filename = shift;
Line 175... Line 201...
175
   use File::Basename;
201
   use File::Basename;
176
   print basename($0) . " $VERSION\n";
202
   print basename($0) . " $VERSION\n";
177
   print <<END
203
   print <<END
178
$0 [options]
204
$0 [options]
179
Options:
205
Options:
180
   --no-snmp        - do not actually run snmp commands
206
   --no-snmp         - do not actually run snmp commands
181
   --debug          - set debug level
207
   --debug           - set debug level
-
 
208
   --refresh         - force a refresh even if not time
182
   --version        - display version and exit
209
   --version         - display version and exit
183
   --help           - This page
210
   --help            - This page
184
END
211
END
185
}
212
}
186
 
213
 
187
# get information about one single switch
214
# get information about one single switch
188
#
215
#
189
sub getSwitchInfo {
216
sub getSwitchInfo {
190
   my ( $ip, $community, $ignorePorts, $switchInfo ) = @_;
217
   my ( $ip, $community, $ignorePorts ) = @_;
-
 
218
   my $switchInfo = {};
191
   print "Working on $ip with community $community\n" if $DEBUG;
219
   print "Working on $ip with community $community\n" if $DEBUG;
192
   $switchInfo->{$ip}->{'name'} = # get actual device name
220
   $switchInfo->{$ip}->{'name'} = # get actual device name
193
      &getOneSNMPValue( $DEVICENAMEMIB,
221
      &getOneSNMPValue( $DEVICENAMEMIB,
194
                        $community,
222
                        $community,
195
                        $ip, 
223
                        $ip, 
Line 225... Line 253...
225
            $community, 
253
            $community, 
226
            $ip, 
254
            $ip, 
227
            qr/\.\d+ = STRING: "?([^\"]*)"?$/ );
255
            qr/\.\d+ = STRING: "?([^\"]*)"?$/ );
228
      $switchInfo->{$ip}->{'ports'}->{$port}->{'description'} = $port unless defined ( $switchInfo->{$ip}->{'ports'}->{$port}->{'description'} );
256
      $switchInfo->{$ip}->{'ports'}->{$port}->{'description'} = $port unless defined ( $switchInfo->{$ip}->{'ports'}->{$port}->{'description'} );
229
   }
257
   }
-
 
258
   return $switchInfo->{$ip};
230
}
259
}
231
 
260
 
232
# get all MAC addresses on switch and associate them with the correct port numbers
261
# get all MAC addresses on switch and associate them with the correct port numbers
233
# https://www.cisco.com/c/en/us/support/docs/ip/simple-network-management-protocol-snmp/44800-mactoport44800.html
262
# https://www.cisco.com/c/en/us/support/docs/ip/simple-network-management-protocol-snmp/44800-mactoport44800.html
234
sub getSwitchPortMacs {
263
sub getSwitchPortMacs {
Line 255... Line 284...
255
      $macList->{&makeMAC($mac)}->{'originalMAC'} = $mac;
284
      $macList->{&makeMAC($mac)}->{'originalMAC'} = $mac;
256
      $macList->{&makeMAC($mac)}->{'lastseen'} = time();
285
      $macList->{&makeMAC($mac)}->{'lastseen'} = time();
257
   }
286
   }
258
}
287
}
259
 
288
 
260
# get IP address from router(s), and hostname from rdns
289
# get IP address from router(s)
-
 
290
# since the information is already there, we grab the interface also
261
sub getRouterInfo {
291
sub getRouterInfo {
262
   my ( $ip, $community, $routerInfo ) = @_;
292
   my ( $ip, $community, $routerInfo ) = @_;
263
   print "Working on $ip\n" if $DEBUG;
293
   print "Working on $ip\n" if $DEBUG;
264
   print "snmpwalk -v1 -c $community $ip $ROUTERIPMACMIB\n" if $DEBUG > 1;
294
   print "snmpwalk -v1 -c $community $ip $ROUTERIPMACMIB\n" if $DEBUG > 1;
265
   my $values = `snmpwalk -v1 -c $community $ip $ROUTERIPMACMIB`;
295
   my $values = `snmpwalk -v1 -c $community $ip $ROUTERIPMACMIB`;
Line 269... Line 299...
269
      my $interface = $1;
299
      my $interface = $1;
270
      my $ip = $2;
300
      my $ip = $2;
271
      my $mac = &cleanMac( $3 );
301
      my $mac = &cleanMac( $3 );
272
      $routerInfo->{$mac}->{'ip'} = $ip;
302
      $routerInfo->{$mac}->{'ip'} = $ip;
273
      $routerInfo->{$mac}->{'interface'} = $interface;
303
      $routerInfo->{$mac}->{'interface'} = $interface;
274
      $routerInfo->{$mac}->{'hostname'} = gethostbyaddr( inet_aton( $ip ), AF_INET );
-
 
275
   }
304
   }
276
}
305
}
277
 
306
 
278
# load tab delimited values from $filename into hashref $nonRouterEntries
307
# load tab delimited values from $filename into hashref $nonRouterEntries
279
# format is
308
# format is
Line 281... Line 310...
281
#     'ip' = IP Address
310
#     'ip' = IP Address
282
#     'mac' = MAC Address
311
#     'mac' = MAC Address
283
#     'hostname' = HostName
312
#     'hostname' = HostName
284
 
313
 
285
sub loadNonRouterEntries {
314
sub loadNonRouterEntries {
286
   my ( $nonRouterEntries, $filename ) = @_;
315
   my ( $scriptDir, $files, $staticMaps, $routerARP ) = @_;
287
   print "In loadNonRouterEntries with $filename\n" if $DEBUG > 1;
-
 
288
   open FN,"<$filename" or die "Could not open $filename: $!\n";
316
   # first, load the information from the files
289
   my $line = <FN>;
-
 
290
   chomp $line;
-
 
291
   my @headers = split( "\t", $line );
-
 
292
   my $macIndex = 0;
-
 
293
   while ( $headers[$macIndex] !~ 'mac' && $macIndex < @headers ) {
-
 
294
      $macIndex++;
-
 
295
   }
-
 
296
   if ( $macIndex >= @headers ) {
317
   foreach my $filename ( @$files ) {
297
      warn "File $filename does not appear to contain a mac column\n";
318
      open FN,"<$scriptDir/$filename" or die "Could not open $scriptDir/$filename: $!\n";
298
      return;
-
 
299
   }
-
 
300
   while ( $line = <FN> ) {
319
      my $line = <FN>;
301
      next if $line =~ m/^#/;
-
 
302
      chomp $line;
320
      chomp $line;
303
      my @values = split( "\t", $line );
321
      my @headers = split( "\t", $line );
-
 
322
      my $macIndex = 0;
-
 
323
      while ( $headers[$macIndex] !~ 'mac' && $macIndex < @headers ) {
-
 
324
         $macIndex++;
-
 
325
      }
304
      for( my $i = 0; $i < @headers; $i++ ) {
326
      if ( $macIndex >= @headers ) {
305
         $nonRouterEntries->{&cleanMac($values[$macIndex])}->{$headers[$i]} = lc $values[$i];
327
         warn "File $filename does not appear to contain a mac column\n";
-
 
328
         return;
306
      }
329
      }
-
 
330
      while ( $line = <FN> ) {
-
 
331
         next if $line =~ m/^#/;
-
 
332
         chomp $line;
-
 
333
         my @values = split( "\t", $line );
-
 
334
         my $mac = &cleanMac($values[$macIndex]);
-
 
335
         for( my $i = 0; $i < @headers; $i++ ) {
-
 
336
            # update the value if one does not already exist
-
 
337
            $routerARP->{$mac}->{$headers[$i]} = lc $values[$i] unless $routerARP->{$mac}->{$headers[$i]};
-
 
338
         }
-
 
339
      }
-
 
340
      close FN;
-
 
341
   }
-
 
342
   # now, update from our staticmaps
-
 
343
   foreach my $mac ( keys %$staticMaps ) {
-
 
344
      # only update ip if it doesn't exist, or if we have an override
-
 
345
      $routerARP->{$mac}->{'ip'} = $staticMaps->{$mac}->{'ip'} if $staticMaps->{$mac}->{'override'} || ! $routerARP->{$mac}->{'ip'};
-
 
346
      # these don't exist in $routerARP, so we just add them
-
 
347
      $routerARP->{$mac}->{'hostname'} = $staticMaps->{$mac}->{'hostname'};
-
 
348
      $routerARP->{$mac}->{'override'} = $staticMaps->{$mac}->{'override'};
307
   }
349
   }
308
   close FN;
-
 
309
}
350
}
-
 
351
 
310
   
352
   
311
# merge nonRouterEntries into $routerARP, with $routerARP having precedence
353
# merge nonRouterEntries into $routerARP, with $routerARP having precedence
312
sub updateRouterWithStatic {
354
sub updateRouterWithStatic {
313
   my ( $routerARP, $nonRouterEntries ) = @_;
355
   my ( $routerARP, $nonRouterEntries ) = @_;
314
   foreach my $mac ( keys %$nonRouterEntries ) {
356
   foreach my $mac ( keys %$nonRouterEntries ) {
Line 328... Line 370...
328
                  || $nonRouterEntries->{$mac}->{'override'} 
370
                  || $nonRouterEntries->{$mac}->{'override'} 
329
                  )
371
                  )
330
               );
372
               );
331
   }
373
   }
332
}
374
}
-
 
375
 
-
 
376
# get IP addresses from the list of them we have
-
 
377
sub getIPAddresses {
-
 
378
   my ( $macList, $ipList ) = @_;
-
 
379
   foreach my $mac ( keys %$macList ) {
-
 
380
      $macList->{$mac}->{'ip'} = $ipList->{$mac}->{'ip'} if $ipList->{$mac}->{'ip'};
-
 
381
   }
-
 
382
}
-
 
383
 
-
 
384
# grab names by reverse DNS from the IP addresses
-
 
385
# if that fails, or if there is an override
-
 
386
# use the name from $routerARP, if it exists;
-
 
387
sub getDNSNames {
-
 
388
   my ( $macList, $dnsResource, $routerARP ) = @_;
-
 
389
   foreach my $mac ( keys %$macList ) {
-
 
390
      if ( $routerARP->{$mac}->{'override'} && $routerARP->{$mac}->{'hostname'} ) {
-
 
391
         $macList->{$mac}->{'hostname'} = $routerARP->{$mac}->{'hostname'};
-
 
392
      } else { # try a DNS lookup
-
 
393
         $macList->{$mac}->{'hostname'} = &getHostName( $dnsResource, $macList->{$mac}->{'ip'} );
-
 
394
         # still didn't find one, so add one from $routerARP if it exists
-
 
395
         $macList->{$mac}->{'hostname'} = $routerARP->{$mac}->{'hostname'} if ! $macList->{$mac}->{'hostname'} && $routerARP->{$mac}->{'hostname'};
-
 
396
      }
-
 
397
   }
-
 
398
}
-
 
399
 
-
 
400
# given a value of a number follwed by a unit (ie, 2w, 12h, 3m), determine 
-
 
401
# the unixtime for that long ago.
-
 
402
sub calcAge {
-
 
403
   my $time = shift;
-
 
404
   my %seconds = ( 
-
 
405
      's' => 1,
-
 
406
      'h' => 60*60,
-
 
407
      'd' => 86400,
-
 
408
      'w' => 7*86400,
-
 
409
      'm' => 7*30.5*86400,
-
 
410
      'y' => 86400*365.2425
-
 
411
      );
-
 
412
   $time = lc $time;
-
 
413
   $time =~ m/^(\d+)(.*)/;
-
 
414
   $time = $1;
-
 
415
   my $unit = $2;
-
 
416
   $unit = 's' unless $unit;
-
 
417
   return defined( $seconds{$unit} ) ? $time * $seconds{$unit} : 0;
-
 
418
}
-
 
419
 
-
 
420
# given an integer/modifier (1w, 3d, 10h), remove all mac entries which have not
-
 
421
# been seen since then.
-
 
422
sub cleanUpOldEntries {
-
 
423
   my ( $ttl, $list ) = @_;
-
 
424
   my $currentTime = time;
-
 
425
   my $deleteBefore = &calcAge( $ttl );
-
 
426
   if ( $deleteBefore == 0 ) {
-
 
427
      warn "Invalid ttl definition [$ttl], not cleaning up\n";
-
 
428
      return;
-
 
429
   }
-
 
430
   $deleteBefore = time - $deleteBefore;
-
 
431
   foreach my $mac ( keys %{ $list->{'macList'} } ) {
-
 
432
      if ( $list->{'macList'}->{$mac}->{'lastseen'} < $deleteBefore ) {
-
 
433
#         print 
-
 
434
#            "Deleting $mac with a last seen of " . 
-
 
435
#            $list->{'macList'}->{$mac}->{'lastseen'} . 
-
 
436
#            ", current time is $currentTime for a difference of " . 
-
 
437
#            ( $currentTime - $list->{'macList'}->{$mac}->{'lastseen'} ) . 
-
 
438
#            "\n";
-
 
439
         delete $list->{'macList'}->{$mac} if $list->{'macList'}->{$mac}->{'lastseen'};
-
 
440
      }
333
   
441
   }
-
 
442
}
334
 
443
 
335
################################################################################
444
################################################################################
336
#              Main
445
#              Main
337
################################################################################
446
################################################################################
338
 
447
 
339
# handle any command line parameters that may have been passed in
448
# handle any command line parameters that may have been passed in
340
my $version = 0; # just used to determine if we should display the version
449
my $version = 0; # just used to determine if we should display the version
341
my $help = 0; # also if we want help
450
my $help = 0; # also if we want help
342
my $nosnmp = 0; # we do NOT want to run snmp commands
451
my $nosnmp = 0; # we do NOT want to run snmp commands
-
 
452
my $forceRefres = 0; # force a refresh even if one is not called for now
343
GetOptions (
453
GetOptions (
344
            'debug|d=i'     => \$DEBUG,
454
            'debug|d=i'     => \$DEBUG,
345
            'nosnmp|n'      => \$nosnmp,
455
            'nosnmp|n'      => \$nosnmp,
-
 
456
            'refresh|r'     => \$forceRefres,
346
            'help|h'        => \$help,
457
            'help|h'        => \$help,
347
            'version|v'     => \$version,
458
            'version|v'     => \$version,
348
            ) or die "Error parsing command line\n";
459
            ) or die "Error parsing command line\n";
349
 
460
 
350
                  
461
                  
351
if ( $help ) { &help() ; exit; }
462
if ( $help ) { &help() ; exit; }
352
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
463
if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; }
353
 
464
 
354
# Get configuration file into $config
465
# Get configuration file into $config
355
my $config = &loadConfig( $CONFIGFILE );
466
my $config = &loadConfig( $CONFIGFILE );
356
print Dumper( $config ) if $DEBUG > 1;
467
print Dumper( $config ) if $DEBUG > 1; die if $DEBUG > 4;
-
 
468
 
-
 
469
# load the savefile, if it exists
-
 
470
my $saveFile = { 'macList' => {}, 'switchInfo' => {} };
-
 
471
# read the saved state into memory if it exists
-
 
472
if ( -e $STATEFILE ) {
-
 
473
   my $yaml = YAML::Tiny->read( $STATEFILE );
-
 
474
   $saveFile = \%{ $yaml->[0] };
-
 
475
}
-
 
476
 
-
 
477
# set up one dns resource for every router we have
-
 
478
# too many failures when trying to define both of them at the same time
-
 
479
my $dnsResource = [];
-
 
480
foreach my $router ( keys %{ $config->{'routers'} } ) {
-
 
481
   push @$dnsResource, Net::DNS::Resolver->new(
-
 
482
    'nameservers' => [ $router ],
-
 
483
    'recurse' => 0
-
 
484
    );
-
 
485
}
-
 
486
 
-
 
487
my $refresh = 1; # determines if we get extra information like switch info, port aliases, etc...
-
 
488
if ( defined( $config->{'refresh'} ) ) {
-
 
489
   # get the number of seconds between refreshes
-
 
490
   $refresh = &calcAge( $config->{'refresh'} );
-
 
491
   # if we have not recorded a refresh, set the refresh time to run now
-
 
492
   $saveFile->{'lastrefresh'} = time - &calcAge( $config->{'refresh'} ) - 1 
-
 
493
      unless defined $saveFile->{'lastrefresh'};
-
 
494
   $refresh = $saveFile->{'lastrefresh'} + &calcAge( $config->{'refresh'} ) < time || $forceRefres;
-
 
495
   $saveFile->{'lastrefresh'} = time if $refresh;
-
 
496
}
-
 
497
 
-
 
498
print STDERR "Doing a full refresh\n" if $refresh && $DEBUG > 1;
357
 
499
 
358
die if $DEBUG > 4;
-
 
359
 
500
 
360
my $switchInfo = {}; # stores information about switches
-
 
361
my $macList = {}; # a list of all MAC addresses
-
 
362
 
501
 
363
# load the information for the switches into $switchInfo.
502
# load the information for the switches into $switchInfo.
364
foreach my $switch ( keys %{$config->{'switches'}} ) {
503
foreach my $switch ( keys %{$config->{'switches'}} ) {
365
   last if $nosnmp; # do not read switches if $nosnmp set
504
   last if $nosnmp; # do not read switches if $nosnmp set
366
   my %ignoreList = map { $_ => 1 } @{ $config->{'switches'}->{$switch}->{'portsToIgnore'} }; # hash of ports to ignore
505
   my $ignoreList = { map { $_ => 1 } @{ $config->{'switches'}->{$switch}->{'portsToIgnore'} } }; # hash of ports to ignore
367
   my $ignoreList = \%ignoreList;
506
   #my $ignoreList = \%ignoreList;
368
   print "ignoreList for $switch\n" . Dumper ($ignoreList) if $DEBUG > 2;
507
   print "ignoreList for $switch\n" . Dumper ($ignoreList) if $DEBUG > 2;
369
   # just get basic switch information, inlcuding a list of all ports which are active and not in portsToIgnore
508
   # just get basic switch information, inlcuding a list of all ports which are active and not in portsToIgnore
370
   &getSwitchInfo( $switch, $config->{'switches'}->{$switch}->{'community'}, $ignoreList, $switchInfo );
509
   $saveFile->{'switchInfo'}->{$switch} = &getSwitchInfo( $switch, $config->{'switches'}->{$switch}->{'community'}, $ignoreList ) if $refresh;
371
   # get all the MAC's on this switch
510
   # get all the MAC's on this switch
372
   &getSwitchPortMacs( $switch, $config->{'switches'}->{$switch}->{'community'}, $ignoreList, $macList );
511
   &getSwitchPortMacs( $switch, $config->{'switches'}->{$switch}->{'community'}, $ignoreList, $saveFile->{'macList'} );
373
}
512
}
374
print "switchInfo\n" . Dumper( $switchInfo ) if $DEBUG > 2;
-
 
375
print "macList\n" . Dumper( $macList ) if $DEBUG > 2;
-
 
-
 
513
 
376
 
514
 
377
my $routerARP = {};
515
my $routerARP = {};
378
# Read the ARP table from the router(s)
516
# Read the ARP table from the router(s)
379
foreach my $router ( keys %{$config->{'routers'}} ) {
517
foreach my $router ( keys %{$config->{'routers'}} ) {
380
   last if $nosnmp; # do not read routers if $nosnmp set
518
   #last if $nosnmp; # do not read routers if $nosnmp set
381
   &getRouterInfo( $router,  $config->{'routers'}->{$router}->{'community'}, $routerARP );
519
   &getRouterInfo( $router,  $config->{'routers'}->{$router}->{'community'}, $routerARP ) if $refresh;
382
}
520
}
383
print "Routers\n" . Dumper ($routerARP ) if $DEBUG > 2;
521
print "Routers\n" . Dumper ($routerARP ) if $DEBUG > 2;
384
 
-
 
385
# load non-router entries, if they exist
-
 
386
my $nonRouterEntries = {};
-
 
387
if ( defined ( $config->{'nonrouter'} ) ) {
-
 
388
   foreach my $filename ( @{$config->{'nonrouter'}} ) {
522
# add in anything we have defined in files or in staticmaps in the config file
389
      &loadNonRouterEntries( $nonRouterEntries, $scriptDir . '/' . $filename );
523
&loadNonRouterEntries( $scriptDir, $config->{'nonrouter'}, $config->{'staticmaps'}, $routerARP );
390
   }
-
 
391
}
-
 
392
 
-
 
393
print "Non-Router Entries\n" . Dumper ( $nonRouterEntries ) if $DEBUG > 2;
524
print "Routers after loadNonRouterEntries\n" . Dumper ($routerARP ) if $DEBUG > 2;
394
 
-
 
395
# add in any staticmaps we may have. Note it will overwrite anything from files above
-
 
396
foreach my $mac ( keys %{$config->{'staticmaps'} } ) {
525
# populate the mac list with information from $routerARP
397
   $nonRouterEntries->{$mac}->{'hostname'} = $config->{'staticmaps'}->{$mac}->{'hostname'} if $config->{'staticmaps'}->{$mac}->{'hostname'};
-
 
398
   $nonRouterEntries->{$mac}->{'ip'} = $config->{'staticmaps'}->{$mac}->{'ip'} if $config->{'staticmaps'}->{$mac}->{'ip'};
-
 
399
   $nonRouterEntries->{$mac}->{'override'} = $config->{'staticmaps'}->{$mac}->{'override'} if $config->{'staticmaps'}->{$mac}->{'override'};
-
 
400
}
-
 
401
 
-
 
402
&updateRouterWithStatic( $routerARP, $nonRouterEntries );
526
&getIPAddresses( $saveFile->{'macList'}, $routerARP );
403
 
-
 
404
print "routerInfo\n" . Dumper ( $routerARP ) if $DEBUG > 2;
527
print "Routers after getIPAddresses\n" . Dumper ($routerARP ) if $DEBUG > 2;
405
 
-
 
406
#print Dumper( $macList ); die;
-
 
407
 
-
 
408
my $saveFile = {};
528
# get the DNS names
409
# read the saved state into memory if it exists
-
 
410
if ( -e $STATEFILE ) {
-
 
411
   my $yaml = YAML::Tiny->read( $STATEFILE );
-
 
412
   $saveFile = \%{ $yaml->[0] };
-
 
413
}
-
 
414
 
-
 
415
$saveFile->{'switchInfo'} = $switchInfo;
-
 
416
 
-
 
417
foreach my $mac ( keys %$macList ) {
-
 
418
   $saveFile->{'macList'}->{$mac} = $macList->{$mac};
-
 
419
   $saveFile->{'macList'}->{$mac}->{'hostname'} = $routerARP->{$mac}->{'hostname'};
529
&getDNSNames( $saveFile->{'macList'}, $dnsResource, $routerARP ) if $refresh;
420
   $saveFile->{'macList'}->{$mac}->{'ip'} = $routerARP->{$mac}->{'ip'};
530
print "Routers after getDNSNames\n" . Dumper ($routerARP ) if $DEBUG > 2;
421
}
-
 
422
 
-
 
423
#print Dumper( $saveFile ); die;
531
# clean up any old entries
424
 
-
 
425
#print Dumper( $switchInfo); die;
532
&cleanUpOldEntries( $config->{'ttl'}, $saveFile ) if $config->{'ttl'} && $refresh;
426
 
-
 
427
# save the file
533
# save the file
428
my $yaml = YAML::Tiny->new( $saveFile );
534
my $yaml = YAML::Tiny->new( $saveFile );
429
$yaml->write( $STATEFILE );
535
$yaml->write( $STATEFILE );
430
 
536
 
431
 
537