Subversion Repositories sysadmin_scripts

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 25
Line 29... Line 29...
29
#                 {lastseen} (last time this was active as unix timestamp)
29
#                 {lastseen} (last time this was active as unix timestamp)
30
 
30
 
31
# 20190407 RWR
31
# 20190407 RWR
32
# converted to use external config file in YAML format
32
# converted to use external config file in YAML format
33
# added the ability to ignore ports on the switches
33
# added the ability to ignore ports on the switches
-
 
34
# 20190514 RWR
-
 
35
# Added port aliases
34
 
36
 
35
use strict;
37
use strict;
36
use warnings;
38
use warnings;
37
use Data::Dumper; # only used for debugging
39
use Data::Dumper; # only used for debugging
38
use Socket; # for reverse dns entries
40
use Socket; # for reverse dns entries
39
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
41
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian
40
use File::Spec; # find location of script so we can put storage in same directory
42
use File::Spec; # find location of script so we can put storage in same directory
41
use File::Basename;
43
use File::Basename;
42
 
44
 
-
 
45
our $VERSION = '1.1';
-
 
46
 
43
my %config;
47
my %config;
44
 
48
 
45
# where the script is located
49
# where the script is located
46
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
50
my $scriptDir = dirname( File::Spec->rel2abs( __FILE__ ) );
47
# put the statefile in there
51
# put the statefile in there
Line 50... Line 54...
50
# main hash that holds the data we collect
54
# main hash that holds the data we collect
51
my %switchports;
55
my %switchports;
52
# some OIDS we need for the program
56
# some OIDS we need for the program
53
my $SWITCHPORTMIB  = 'iso.3.6.1.2.1.17.4.3.1.2';
57
my $SWITCHPORTMIB  = 'iso.3.6.1.2.1.17.4.3.1.2';
54
my $SWITCHMACMIB   = 'iso.3.6.1.2.1.17.4.3.1.1';
58
my $SWITCHMACMIB   = 'iso.3.6.1.2.1.17.4.3.1.1';
-
 
59
my $SWITCHIFNAMEMIB = '1.3.6.1.2.1.31.1.1.1.18';
55
my $ROUTERIPMACMIB = 'iso.3.6.1.2.1.4.22.1.2'; #'iso.3.6.1.2.1.3.1.1.2';
60
my $ROUTERIPMACMIB = 'iso.3.6.1.2.1.4.22.1.2'; #'iso.3.6.1.2.1.3.1.1.2';
56
my $DEVICENAMEMIB  = '1.3.6.1.2.1.1.5';
61
my $DEVICENAMEMIB  = '1.3.6.1.2.1.1.5';
57
my $DEVICELOCMIB   = '1.3.6.1.2.1.1.6';
62
my $DEVICELOCMIB   = '1.3.6.1.2.1.1.6';
58
 
63
 
59
sub makeMAC {
64
sub makeMAC {
Line 94... Line 99...
94
   foreach my $switch ( keys %$switchports ) {
99
   foreach my $switch ( keys %$switchports ) {
95
      my $thisSwitch = $switchports->{$switch}->{'ports'};
100
      my $thisSwitch = $switchports->{$switch}->{'ports'};
96
      foreach my $port ( keys %$thisSwitch ) {
101
      foreach my $port ( keys %$thisSwitch ) {
97
         my $thisPort = $thisSwitch->{$port};
102
         my $thisPort = $thisSwitch->{$port};
98
         foreach my $connection ( keys %$thisPort ) {
103
         foreach my $connection ( keys %$thisPort ) {
-
 
104
            # skip the alias information on a port
-
 
105
            next if $connection eq 'alias';
99
            &initialize( $thisPort,$connection,'mac','ip','hostname','lastseen' );
106
            &initialize( $thisPort,$connection,'mac','ip','hostname','lastseen' );
100
            if ( $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'mac'} eq $mac ) {
107
            if ( $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'mac'} eq $mac ) {
101
               my $modified = &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'ip'}, $ip );
108
               my $modified = &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'ip'}, $ip );
102
               $modified |= &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'hostname'},gethostbyaddr( inet_aton( $ip ), AF_INET ));
109
               $modified |= &updateValue( \$switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'hostname'},gethostbyaddr( inet_aton( $ip ), AF_INET ));
103
               $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'lastseen'} = time() if $modified;
110
               $switchports->{$switch}->{'ports'}->{$port}->{$connection}->{'lastseen'} = time() if $modified;
Line 106... Line 113...
106
         } # for connection
113
         } # for connection
107
      } # for port
114
      } # for port
108
   } # for switch
115
   } # for switch
109
} # updateIP
116
} # updateIP
110
 
117
 
-
 
118
sub getAliases {
-
 
119
   my ($ip, $community, $MIB ) = @_;
-
 
120
   my %return;
-
 
121
   # get the aliases
-
 
122
   my $values = `snmpwalk -v1 -c $community $ip $MIB`;
-
 
123
   my @aliases = split( "\n", $values );
-
 
124
   chomp @aliases;
-
 
125
   foreach my $entry ( @aliases ) {
-
 
126
      $entry =~ m/\.(\d+) = STRING: "?([^"]*)"?$/;
-
 
127
      $return{$1} = $2;
-
 
128
   }
-
 
129
   return \%return;
-
 
130
}
-
 
131
   
-
 
132
 
111
if ( -e $CONFIGFILE ) {
133
if ( -e $CONFIGFILE ) {
112
   my $yaml = YAML::Tiny->read( $CONFIGFILE );
134
   my $yaml = YAML::Tiny->read( $CONFIGFILE );
113
   %config = %{ $yaml->[0] };
135
   %config = %{ $yaml->[0] };
114
} else {
136
} else {
115
   die "could not locate config file $CONFIGFILE\n";
137
   die "could not locate config file $CONFIGFILE\n";
Line 131... Line 153...
131
 
153
 
132
   &updateValue(
154
   &updateValue(
133
      \$switchports{$switch}{'location'},
155
      \$switchports{$switch}{'location'},
134
      &getOneSNMPValue( $DEVICELOCMIB,$config{'switches'}{$switch}{'community'},$switch, '= STRING: "?([^"]*)"?' )
156
      &getOneSNMPValue( $DEVICELOCMIB,$config{'switches'}{$switch}{'community'},$switch, '= STRING: "?([^"]*)"?' )
135
      );
157
      );
136
 
-
 
-
 
158
   # get the MAC addresses
137
   my $values = `snmpwalk -v1 -c $config{switches}{$switch}{'community'} $switch $SWITCHPORTMIB`;
159
   my $values = `snmpwalk -v1 -c $config{switches}{$switch}{'community'} $switch $SWITCHPORTMIB`;
138
   my @lines = split( "\n", $values );
160
   my @lines = split( "\n", $values );
-
 
161
   my $aliases = getAliases ( $switch, $config{switches}{$switch}{'community'}, $SWITCHIFNAMEMIB );
-
 
162
   
139
   foreach my $line ( @lines ) {
163
   foreach my $line ( @lines ) {
140
      $line =~ m/$SWITCHPORTMIB\.([0-9.]+)\s=\sINTEGER:\s+(\d+)/;
164
      $line =~ m/$SWITCHPORTMIB\.([0-9.]+)\s=\sINTEGER:\s+(\d+)/;
141
      my $uuid = $1; # this is the ID string of this MAC; normally the MAC itself in decimal form
165
      my $uuid = $1; # this is the ID string of this MAC; normally the MAC itself in decimal form
142
      my $port = $2; # this is the port number
166
      my $port = $2; # this is the port number
143
      next unless $port; # skip port 0, or any port which has nothing
167
      next unless $port; # skip port 0, or any port which has nothing
144
      next if $config{'switches'}{$switch}{'portsToIgnore'} && grep( /^$port$/, @{$config{'switches'}{$switch}{'portsToIgnore'}} );
168
      next if $config{'switches'}{$switch}{'portsToIgnore'} && grep( /^$port$/, @{$config{'switches'}{$switch}{'portsToIgnore'}} );
145
      $switchports{$switch}{'ports'}{$port}{$uuid}{'mac'} = &makeMAC( $1 );
169
      $switchports{$switch}{'ports'}{$port}{$uuid}{'mac'} = &makeMAC( $1 );
-
 
170
      $switchports{$switch}{'ports'}{$port}{'alias'} = $aliases->{$port} ? $aliases->{$port} : '';
146
   }
171
   }
147
}
172
}
148
 
173
 
149
# die Dumper( \%switchports );
174
# die Dumper( \%switchports );
150
 
175