Changeset 369
- Timestamp:
- Dec 8, 2017, 11:37:55 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/klask
r368 r369 4 4 # 5 5 # $Id$ 6 # 7 # perltidy -i=3 -ci=3 -pt=2 -icb -cti=3 -sbt=2 -bt=2 -bbt=2 -nsfs -bar -otr -olc -nolq -l=132 -vmll -msc=1 -iscl -nbbc -nola -st klask > klask.tmp 6 8 7 9 use strict; … … 47 49 LEVEL_OF_EACH_SWITCH: 48 50 for my $sw (@SWITCH_LIST) { 49 $SWITCH_LEVEL{$sw->{'hostname'}} = $sw->{'level'} || $DEFAULT{'switch_level'} 51 $SWITCH_LEVEL{$sw->{'hostname'}} = $sw->{'level'} || $DEFAULT{'switch_level'} || 2; 50 52 $SWITCH_DB{$sw->{'hostname'}} = $sw; 51 53 … … 62 64 $sw->{'snmp_param_session'} = \%session; 63 65 } 64 @SWITCH_LIST = reverse sort { $SWITCH_LEVEL{$a->{'hostname'}} <=> $SWITCH_LEVEL{$b->{'hostname'}}} @{$KLASK_CFG->{'switch'}};66 @SWITCH_LIST = reverse sort {$SWITCH_LEVEL{$a->{'hostname'}} <=> $SWITCH_LEVEL{$b->{'hostname'}}} @{$KLASK_CFG->{'switch'}}; 65 67 66 68 #my %SWITCH_PORT_COUNT = (); … … 177 179 sysContact => '1.3.6.1.2.1.1.4.0', 178 180 sysLocation => '1.3.6.1.2.1.1.6.0', 179 searchPort1 => '1.3.6.1.2.1.17.4.3.1.2', 180 searchPort2 => '1.3.6.1.2.1.17.7.1.2.2.1.2', 181 vlanPortDefault => '1.3.6.1.2.1.17.7.1.4.5.1.1', 182 vlanStatus => '1.3.6.1.2.1.17.7.1.4.3.1.5', 183 vlanName => '1.3.6.1.2.1.17.7.1.4.3.1.1', 184 HPicfReset => '1.3.6.1.4.1.11.2.14.11.1.4.1', 185 ifIndex => '1.3.6.1.2.1.17.1.4.1.2', 186 ifName => '1.3.6.1.2.1.31.1.1.1.1', 187 portUpDown => '1.3.6.1.2.1.2.2.1.7', 188 poeState => '1.3.6.1.2.1.105.1.1.1.3.1', 189 NApoeState => '1.3.6.1.4.1.266.20.3.1.1.21', 190 ifAggregator 181 searchPort1 => '1.3.6.1.2.1.17.4.3.1.2', # BRIDGE-MIB (802.1D). 182 searchPort2 => '1.3.6.1.2.1.17.7.1.2.2.1.2', # Q-BRIDGE-MIB (802.1Q) add 0 if unknown vlan id 183 vlanPortDefault => '1.3.6.1.2.1.17.7.1.4.5.1.1', # dot1qPvid 184 vlanStatus => '1.3.6.1.2.1.17.7.1.4.3.1.5', # integer 4 Create, 6 Destroy 185 vlanName => '1.3.6.1.2.1.17.7.1.4.3.1.1', # string 186 HPicfReset => '1.3.6.1.4.1.11.2.14.11.1.4.1', # HP reboot switch 187 ifIndex => '1.3.6.1.2.1.17.1.4.1.2', # dot1dBasePortIfIndex - Interface index redirection 188 ifName => '1.3.6.1.2.1.31.1.1.1.1', # Interface name (give port number) 189 portUpDown => '1.3.6.1.2.1.2.2.1.7', # 1.3.6.1.2.1.2.2.1.7.NoPort = 1 (up) = 2 (down) 190 poeState => '1.3.6.1.2.1.105.1.1.1.3.1', # 1.3.6.1.2.1.105.1.1.1.3.1.NoPort = 1 (poe up) = 2 (poe down) - Cisco and Zyxel 191 NApoeState => '1.3.6.1.4.1.266.20.3.1.1.21', # .NoPort = 2 (poe off) = 8 (poe atHighPower) - Nexans 192 ifAggregator => '1.2.840.10006.300.43.1.2.1.1.12', # dot3adAggPortSelectedAggID - 0 not part of an Aggregator - Ciso Dell HP Comware - See https://stackoverflow.com/questions/14960157/how-to-map-portchannel-to-interfaces-via-snmp https://gist.github.com/bldewolf/6314435 191 193 ); 192 194 … … 201 203 Readonly my $RE_FLOAT_HOSTNAME => $DEFAULT{'float-regex'} || qr{ ^float }xms; 202 204 203 Readonly my $SEP_AGGREGATOR_PORT => ','; 205 Readonly my $SEP_AGGREGATOR_PORT => ','; # : is already use to join switch and port 204 206 Readonly my $SEP_SWITCH_PORT => ':'; 205 207 … … 269 271 sub resolve_ip_arp_host { 270 272 my $param_ip_or_host = shift; 271 my $interface = shift || q{*};272 my $type = shift || q{fast};273 my $already = shift || q{yes};273 my $interface = shift || q{*}; 274 my $type = shift || q{fast}; 275 my $already = shift || q{yes}; 274 276 275 277 my %ret = ( … … 307 309 my $nowtimestamp = time; 308 310 309 if ( $type eq 'fast' and ( not defined $timestamp or $timestamp < ( $nowtimestamp - 45 * 60 ) )) { # 45 min311 if ($type eq 'fast' and (not defined $timestamp or $timestamp < ($nowtimestamp - 45 * 60))) { # 45 min 310 312 $ret{'mac_address'} = 'unknow'; 311 313 return %ret; … … 328 330 # Last chance to have the mac_address 329 331 if ($ret{'interface'} eq '*') { 330 my $cmd_arp 331 if ( $cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s dev \s ([\w\d\.\:]+) \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms) {332 my $cmd_arp = shell_command "ip neigh show to $ret{'ipv4_address'}"; 333 if ($cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s dev \s ([\w\d\.\:]+) \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms) { 332 334 ($ret{'interface'}, $ret{'mac_address'}) = ($1, $2); 333 335 } 334 336 } 335 337 else { 336 my $cmd_arp 337 if ( $cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms) {338 my $cmd_arp = shell_command "ip neigh show to $ret{'ipv4_address'} dev $ret{'interface'}"; 339 if ($cmd_arp =~ m{ ^$RE_IPv4_ADDRESS \s lladdr \s ( $RE_MAC_ADDRESS ) \s }xms) { 338 340 $ret{'mac_address'} = $1; 339 341 } … … 344 346 if ($ret{'mac_address'} ne 'unknow') { 345 347 my @paquets = (); 346 for ( split m/ : /xms, $ret{'mac_address'}) {348 for (split m/ : /xms, $ret{'mac_address'}) { 347 349 my @chars = split m//xms, uc "00$_"; 348 350 push @paquets, "$chars[-2]$chars[-1]"; … … 391 393 my ($verbose, $verb_description, $check_hostname, $check_location) = @_; 392 394 393 printf "%-26s %-25s %s\n", 'Switch','Description','Type(Revision)' if $verbose;395 printf "%-26s %-25s %s\n", 'Switch', 'Description', 'Type(Revision)' if $verbose; 394 396 print "----------------------------------------------------------------------------------\n" if $verbose; 395 397 396 398 INIT_EACH_SWITCH: 397 399 for my $sw (my @CLONE = @SWITCH_LIST) { # Make a local clone because some element can be deleted 398 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});400 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 399 401 print "$error \n" if $error; 400 402 … … 411 413 $session->close(); 412 414 # Remove bad switch 413 @SWITCH_LIST = grep { $_->{'hostname'} ne $sw->{'hostname'}} @SWITCH_LIST;415 @SWITCH_LIST = grep {$_->{'hostname'} ne $sw->{'hostname'}} @SWITCH_LIST; 414 416 delete $SWITCH_LEVEL{$sw->{'hostname'}} if exists $SWITCH_LEVEL{$sw->{'hostname'}}; 415 417 delete $SWITCH_DB{$sw->{'hostname'}} if exists $SWITCH_DB{$sw->{'hostname'}}; … … 1721 1723 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} eq ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}); # switch on himself ! 1722 1724 1723 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'};1725 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $computerdb->{$ip}{'timestamp'}; 1724 1726 $year += 1900; 1725 1727 $mon++; … … 1729 1731 $vlan = $computerdb->{$ip}{'network'}.'('.get_current_vlan_id($computerdb->{$ip}{'network'}).')' if $computerdb->{$ip}{'network'}; 1730 1732 1731 my $arrow = '<---';1732 $arrow ='<===' if $computerdb->{$ip}{'switch_port_hr'} =~ m/^(Trk|Br|Po)/;1733 my $arrow = '<---'; 1734 $arrow = '<===' if $computerdb->{$ip}{'switch_port_hr'} =~ m/^(Trk|Br|Po)/; 1733 1735 1734 1736 $tb_computer->add( … … 1757 1759 my $computerdb = computerdb_load(); 1758 1760 1759 #<link rel="stylesheet" type="text/css" href="style-klask.css" />1760 #<script src="sorttable-klask.js"></script>1761 #<link rel="stylesheet" type="text/css" href="style-klask.css" /> 1762 #<script src="sorttable-klask.js"></script> 1761 1763 1762 1764 print <<'END_HTML'; … … 1796 1798 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} eq ($computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'}); # switch on himself ! 1797 1799 1798 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'};1800 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $computerdb->{$ip}{'timestamp'}; 1799 1801 $year += 1900; 1800 1802 $mon++; 1801 1803 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; 1802 1804 1803 # $odd_or_even++;1804 # my $typerow = $odd_or_even % 2 ? 'odd' : 'even';1805 # $odd_or_even++; 1806 # my $typerow = $odd_or_even % 2 ? 'odd' : 'even'; 1805 1807 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 1806 1808 1807 1809 #my $arrow ='←'; 1808 1810 # $arrow ='⇐' if $computerdb->{$ip}{'switch_port_hr'} =~ m/^(Trk|Br|Po)/; 1809 my $arrow = '⟵';1810 $arrow ='⟸' if $computerdb->{$ip}{'switch_port_hr'} =~ m/^(Trk|Br|Po)/;1811 my $arrow = '⟵'; 1812 $arrow = '⟸' if $computerdb->{$ip}{'switch_port_hr'} =~ m/^(Trk|Br|Po)/; 1811 1813 1812 1814 my $switch_hostname = $computerdb->{$ip}{'switch_hostname'} || $computerdb->{$ip}{'switch_description'} || 'unkown'; … … 1819 1821 1820 1822 $computerdb->{$ip}{'hostname_fq'} = 'unknow' if $computerdb->{$ip}{'hostname_fq'} =~ m/^ \d+ \. \d+ \. \d+ \. \d+ $/xms; 1821 my ( $host_short) = split m/ \. /xms, $computerdb->{$ip}{'hostname_fq'};1823 my ($host_short) = split m/ \. /xms, $computerdb->{$ip}{'hostname_fq'}; 1822 1824 1823 1825 my $vlan = ''; … … 1860 1862 #my $arrow ='⇾'; 1861 1863 # $arrow ='⇨' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/; 1862 my $arrow = '⟼';1863 $arrow ='⟾' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/;1864 my $arrow = '⟼'; 1865 $arrow = '⟾' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/; 1864 1866 1865 1867 if (exists $db_switch_parent{$sw}) { … … 1872 1874 my $timestamp = $db_switch{$db_switch_parent{$sw}->{'switch'}}->{'timestamp'}; 1873 1875 1874 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp;1876 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $timestamp; 1875 1877 $year += 1900; 1876 1878 $mon++; … … 1911 1913 my $host_short = format_switchport4sort($sw, $db_switch_output_port{$sw}); 1912 1914 1913 my $mac_address = $db_switch{$sw}->{'mac_address'};1915 my $mac_address = $db_switch{$sw}->{'mac_address'}; 1914 1916 my $ipv4_address = $db_switch{$sw}->{'ipv4_address'}; 1915 my $timestamp = $db_switch{$sw}->{'timestamp'};1916 1917 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp;1917 my $timestamp = $db_switch{$sw}->{'timestamp'}; 1918 1919 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $timestamp; 1918 1920 $year += 1900; 1919 1921 $mon++; … … 1925 1927 1926 1928 my $vlan = $db_switch{$sw}->{'network'}; 1927 $vlan .= ' (' .get_current_vlan_id($db_switch{$sw}->{'network'}).')' if $db_switch{$sw}->{'network'};1928 1929 my $arrow = '⟻';1930 $arrow ='⟽' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/;1929 $vlan .= ' (' . get_current_vlan_id($db_switch{$sw}->{'network'}) . ')' if $db_switch{$sw}->{'network'}; 1930 1931 my $arrow = '⟻'; 1932 $arrow = '⟽' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/; 1931 1933 1932 1934 fqdn_html_breakable($sw); … … 1955 1957 my $switch_hostname_sort = format_switchport4sort($sw_connect, $port_connect); 1956 1958 1957 my $mac_address = $db_switch{$sw}->{'mac_address'};1959 my $mac_address = $db_switch{$sw}->{'mac_address'}; 1958 1960 my $ipv4_address = $db_switch{$sw}->{'ipv4_address'}; 1959 my $timestamp = $db_switch{$sw}->{'timestamp'};1960 1961 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp;1961 my $timestamp = $db_switch{$sw}->{'timestamp'}; 1962 1963 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $timestamp; 1962 1964 $year += 1900; 1963 1965 $mon++; 1964 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon,$mday,$hour,$min;1966 my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; 1965 1967 1966 1968 my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ [\.\*] /xms, $ipv4_address; # \* for fake-ip … … 1972 1974 #my $arrow ='⇽'; 1973 1975 # $arrow ='⇦' if $port_connect =~ m/^(Trk|Br|Po)/; 1974 my $arrow = '⟻';1975 $arrow ='⟽' if $port_connect =~ m/^(Trk|Br|Po)/;1976 my $arrow = '⟻'; 1977 $arrow = '⟽' if $port_connect =~ m/^(Trk|Br|Po)/; 1976 1978 1977 1979 my $vlan = $db_switch{$sw}->{'network'}; 1978 $vlan .= ' (' .get_current_vlan_id($db_switch{$sw}->{'network'}).')' if $db_switch{$sw}->{'network'};1980 $vlan .= ' (' . get_current_vlan_id($db_switch{$sw}->{'network'}) . ')' if $db_switch{$sw}->{'network'}; 1979 1981 1980 1982 if (exists $db_switch_output_port{$sw}) { 1981 1983 1982 my $host_short = format_switchport4sort(split( 1984 my $host_short = format_switchport4sort(split(m/\./xms, $sw, 1), $db_switch_output_port{$sw}); 1983 1985 1984 1986 my $parent_port_hr = format_aggregator4html($port_connect); … … 2051 2053 2052 2054 GetOptions( 2053 'day|d=i' 2054 'format|f=s' 2055 'day|d=i' => \$days_before_alert, 2056 'format|f=s' => \$format, 2055 2057 ); 2056 2058 … … 2058 2060 txt => \&cmd_bad_vlan_id_txt, 2059 2061 html => \&cmd_bad_vlan_id_html, 2060 none => sub { },2062 none => sub { }, 2061 2063 ); 2062 2064 $format = 'txt' if not defined $possible_format{$format}; … … 2114 2116 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'hostname_fq'} !~ m/$RE_FLOAT_HOSTNAME/; 2115 2117 2116 my $src_ip = $switchportdb{$swpt}->{'ip'};2118 my $src_ip = $switchportdb{$swpt}->{'ip'}; 2117 2119 my $src_timestamp = 0; 2118 2120 LOOP_ON_IP_ADDRESS: 2119 2121 for my $ip (keys %{$computerdb}) { 2120 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'mac_address'} ne 2122 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'mac_address'} ne $switchportdb{$swpt}->{'mac_address'}; 2121 2123 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} =~ m/$RE_FLOAT_HOSTNAME/; 2122 2124 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'timestamp'} < $src_timestamp; 2123 2125 2124 $src_ip = $ip;2126 $src_ip = $ip; 2125 2127 $src_timestamp = $computerdb->{$ip}{'timestamp'}; 2126 2128 } … … 2130 2132 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'timestamp'} < $src_timestamp; 2131 2133 2132 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $switchportdb{$swpt}->{'timestamp'};2134 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $switchportdb{$swpt}->{'timestamp'}; 2133 2135 $year += 1900; 2134 2136 $mon++; 2135 2137 my $date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; 2136 2138 2137 ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$src_ip}{'timestamp'};2139 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $computerdb->{$src_ip}{'timestamp'}; 2138 2140 $year += 1900; 2139 2141 $mon++; … … 2142 2144 my $vlan_id = get_current_vlan_id($computerdb->{$src_ip}{'network'}); 2143 2145 my ($switch_hostname, $port_hr) = split /\s+/, $swpt, 2; 2144 2146 2145 2147 push @result, { 2146 2148 switch => $switch_hostname, 2147 2149 port_hr => $port_hr, 2148 2150 vlan_bad => $switchportdb{$swpt}->{'vlan'}, 2149 vlan_good => $computerdb->{$src_ip}{'network'},2151 vlan_good => $computerdb->{$src_ip}{'network'}, 2150 2152 vlan_id => $vlan_id, 2151 2153 date_last => $date, … … 2231 2233 2232 2234 my $switch_hostname_sort = format_switchport4sort($item->{'switch'}, $item->{'port_hr'}); 2233 my ( $host_short) = split m/ \. /xms, $item->{'hostname_fq'};2235 my ($host_short) = split m/ \. /xms, $item->{'hostname_fq'}; 2234 2236 2235 2237 my $vlan_nameid = $item->{'vlan_good'} . ' (' . $item->{'vlan_id'} . ')'; … … 2298 2300 -varbindlist => [$oid_search, INTEGER, 8], # Only NEXANS 2299 2301 ); 2300 print $session->error() ."\n" if $session->error_status();2302 print $session->error() . "\n" if $session->error_status(); 2301 2303 2302 2304 $session->close; … … 2336 2338 -varbindlist => [$oid_search, INTEGER, 2], # Only NEXANS 2337 2339 ); 2338 print $session->error() ."\n" if $session->error_status();2340 print $session->error() . "\n" if $session->error_status(); 2339 2341 2340 2342 $session->close; … … 2368 2370 2369 2371 my $sw = $SWITCH_DB{$sw_name}; 2370 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});2372 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 2371 2373 print "$error \n" if $error; 2372 2374 … … 2400 2402 ); 2401 2403 2402 my $switch_name = shift @ARGV || q{};2404 my $switch_name = shift @ARGV || q{}; 2403 2405 my $switch_location = shift @ARGV || q{}; 2404 2406 … … 2438 2440 } 2439 2441 2440 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST 2442 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST) if $switch_name eq q{*}; 2441 2443 2442 2444 for my $sw_name (split /,/, $switch_name) { … … 2446 2448 2447 2449 my $oid_search_port1 = $OID_NUMBER{'searchPort1'} . mac_address_hex2dec($mac_address); 2448 my $oid_search_port2 = $OID_NUMBER{'searchPort2'} . '.'. 0 . mac_address_hex2dec($mac_address);2450 my $oid_search_port2 = $OID_NUMBER{'searchPort2'} . '.' . 0 . mac_address_hex2dec($mac_address); 2449 2451 print "Klask search OID $oid_search_port1 on switch $sw_name\n"; 2450 2452 print "Klask search OID $oid_search_port2 on switch $sw_name\n"; 2451 2453 2452 2454 my $sw = $SWITCH_DB{$sw_name}; 2453 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});2455 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 2454 2456 print "$error \n" if $error; 2455 2457 … … 2501 2503 2502 2504 my $sw = $SWITCH_DB{$sw_name}; 2503 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});2505 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 2504 2506 print "$error \n" if $error; 2505 2507 … … 2568 2570 } 2569 2571 2570 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST 2572 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST) if $switch_name eq q{*}; 2571 2573 2572 2574 for my $sw_name (split /,/, $switch_name) { … … 2578 2580 2579 2581 my $sw = $SWITCH_DB{$sw_name}; 2580 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});2582 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 2581 2583 print "$error \n" if $error; 2582 2584 … … 2606 2608 } 2607 2609 2608 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST 2610 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST) if $switch_name eq q{*}; 2609 2611 2610 2612 for my $sw_name (split /,/, $switch_name) { … … 2614 2616 2615 2617 my $sw = $SWITCH_DB{$sw_name}; 2616 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});2618 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 2617 2619 print "$error \n" if $error; 2618 2620 … … 2662 2664 2663 2665 GetOptions( 2664 'day|d=i' 2665 'format|f=s' 2666 'verbose|v' 2666 'day|d=i' => \$days_to_death, 2667 'format|f=s' => \$format, 2668 'verbose|v' => \$verbose, 2667 2669 ); 2668 2670 … … 2670 2672 txt => \&cmd_ip_free_txt, 2671 2673 html => \&cmd_ip_free_html, 2672 none => sub { },2674 none => sub { }, 2673 2675 ); 2674 2676 $format = 'txt' if not defined $possible_format{$format}; … … 2678 2680 2679 2681 my $computerdb = {}; 2680 2682 $computerdb = computerdb_load() if -e "$KLASK_DB_FILE"; 2681 2683 my $timestamp = time; 2682 2684 … … 2706 2708 my $ip_date_last_detection = ''; 2707 2709 if (exists $computerdb->{$ip}) { 2708 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{'timestamp'};2710 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $computerdb->{$ip}{'timestamp'}; 2709 2711 $year += 1900; 2710 2712 $mon++; … … 2712 2714 } 2713 2715 2714 my $packed_ip = scalar gethostbyname($ip);2716 my $packed_ip = scalar gethostbyname($ip); 2715 2717 my $hostname_fq = 'unknown'; 2716 2718 $hostname_fq = scalar gethostbyaddr($packed_ip, AF_INET) || 'unknown' if defined $packed_ip and get_current_scan_mode($vlan) eq 'active'; … … 2720 2722 $result_ip{$ip} ||= {}; 2721 2723 $result_ip{$ip}->{'date_last_detection'} = $ip_date_last_detection; 2722 $result_ip{$ip}->{'hostname_fq'} = $hostname_fq;2723 $result_ip{$ip}->{'vlan'} = $vlan;2724 $result_ip{$ip}->{'hostname_fq'} = $hostname_fq; 2725 $result_ip{$ip}->{'vlan'} = $vlan; 2724 2726 2725 2727 printf "VERBOSE_1: %-15s %-12s %s\n", $ip, $vlan, $hostname_fq if $verbose; … … 2745 2747 LOOP_ON_IP_ADDRESS: 2746 2748 for my $ip (Net::Netmask::sort_by_ip_address(keys %result_ip)) { 2747 my $vlan_nameid = $result_ip{$ip}->{'vlan'} .'('.get_current_vlan_id($result_ip{$ip}->{'vlan'}).')';2749 my $vlan_nameid = $result_ip{$ip}->{'vlan'} . '(' . get_current_vlan_id($result_ip{$ip}->{'vlan'}) . ')'; 2748 2750 #printf "%-15s %-40s %-16s %s\n", $ip, $result_ip{$ip}->{'hostname_fq'}, $result_ip{$ip}->{'date_last_detection'}, $vlan_nameid; 2749 2751 $tb_computer->add( … … 2785 2787 2786 2788 my $ip_sort = sprintf '%03i%03i%03i%03i', split m/ \. /xms, $ip; 2787 my ( $host_short) = split m/ \. /xms, $result_ip{$ip}->{'hostname_fq'};2788 2789 my $vlan_nameid = $result_ip{$ip}->{'vlan'} .' ('.get_current_vlan_id($result_ip{$ip}->{'vlan'}).')';2789 my ($host_short) = split m/ \. /xms, $result_ip{$ip}->{'hostname_fq'}; 2790 2791 my $vlan_nameid = $result_ip{$ip}->{'vlan'} . ' (' . get_current_vlan_id($result_ip{$ip}->{'vlan'}) . ')'; 2790 2792 2791 2793 fqdn_html_breakable(my $hostname_fq_html = $result_ip{$ip}->{'hostname_fq'}); … … 2843 2845 die "Error : Port $port_hr does not exist on switch $switch_name\n" if not $port_id =~ m/^\d+$/; 2844 2846 2845 my $oid_search_portstatus = $OID_NUMBER{'portUpDown'} . '.'. $port_id;2847 my $oid_search_portstatus = $OID_NUMBER{'portUpDown'} . '.' . $port_id; 2846 2848 print "Info: switch $switch_name port $port_hr SNMP OID $oid_search_portstatus\n" if $verbose; 2847 2849 … … 2888 2890 die "Error : Port $port_hr does not exist on switch $switch_name\n" if not $port_id =~ m/^\d+$/; 2889 2891 2890 my $oid_search_portstatus = $OID_NUMBER{'portUpDown'} . '.'. $port_id;2892 my $oid_search_portstatus = $OID_NUMBER{'portUpDown'} . '.' . $port_id; 2891 2893 print "Info: switch $switch_name port $port_hr SNMP OID $oid_search_portstatus\n" if $verbose; 2892 2894 … … 2924 2926 2925 2927 my $sw = $SWITCH_DB{$switch_name}; 2926 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});2928 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 2927 2929 print "$error \n" if $error; 2928 2930 … … 2931 2933 die "Error : Port $port_hr does not exist on switch $switch_name\n" if not $port_id =~ m/^\d+$/; 2932 2934 2933 my $oid_search_portstatus = $OID_NUMBER{'portUpDown'} . '.'. $port_id;2935 my $oid_search_portstatus = $OID_NUMBER{'portUpDown'} . '.' . $port_id; 2934 2936 print "Info: switch $switch_name port $port_hr ($port_id) SNMP OID $oid_search_portstatus\n" if $verbose; 2935 2937 … … 2968 2970 2969 2971 $mac_address = normalize_mac_address($mac_address); 2970 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST 2972 $switch_name = join(',', map {$_->{'hostname'}} @SWITCH_LIST) if $switch_name eq q{*} or $switch_name eq q{all}; 2971 2973 2972 2974 for my $sw_name (split /,/, $switch_name) { … … 2976 2978 2977 2979 my $oid_search_port1 = $OID_NUMBER{'searchPort1'} . mac_address_hex2dec($mac_address); 2978 my $oid_search_port2 = $OID_NUMBER{'searchPort2'} . '.'. $vlan_id . mac_address_hex2dec($mac_address);2980 my $oid_search_port2 = $OID_NUMBER{'searchPort2'} . '.' . $vlan_id . mac_address_hex2dec($mac_address); 2979 2981 print "Klask search OID $oid_search_port1 on switch $sw_name\n" if $verbose; 2980 2982 print "Klask search OID $oid_search_port2 on switch $sw_name\n" if $verbose; 2981 2983 2982 2984 my $sw = $SWITCH_DB{$sw_name}; 2983 my ($session, $error) = Net::SNMP->session( %{$sw->{'snmp_param_session'}});2985 my ($session, $error) = Net::SNMP->session(%{$sw->{'snmp_param_session'}}); 2984 2986 print "$error \n" if $error; 2985 2987 … … 3027 3029 test_switchdb_environnement(); 3028 3030 3029 my $format = 'txt';3031 my $format = 'txt'; 3030 3032 my $graph_modulo = 0; 3031 3033 my $graph_shift = 1; 3032 3034 3033 3035 GetOptions( 3034 'format|f=s' 3035 'modulo|m=i' 3036 'shift|s=i' 3036 'format|f=s' => \$format, 3037 'modulo|m=i' => \$graph_modulo, 3038 'shift|s=i' => \$graph_shift, 3037 3039 ); 3038 3040 … … 3070 3072 ); 3071 3073 for my $sw (sort keys %db_switch_output_port) { 3072 my $arrow = '--->';3073 $arrow ='===>' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/;3074 my $arrow = '--->'; 3075 $arrow = '===>' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/; 3074 3076 if (exists $db_switch_parent{$sw}) { 3075 3077 $tb_child->add($sw, $db_switch_output_port{$sw}, $arrow, $db_switch_parent{$sw}->{'port_hr'}, $db_switch_parent{$sw}->{'switch'}); … … 3080 3082 } 3081 3083 } 3082 my @colrange = map { scalar $tb_child->colrange($_)} (0 .. 4); # force scaler context3083 $tb_child->add(map { ' ' x $_ } reverse @colrange);# add empty line to force symetric table output3084 my @colrange = map {scalar $tb_child->colrange($_)} (0 .. 4); # force scaler context 3085 $tb_child->add(map {' ' x $_} reverse @colrange); # add empty line to force symetric table output 3084 3086 print $tb_child->title(); 3085 3087 print $tb_child->rule('-'); 3086 print $tb_child->body(0, $tb_child->body_height() -1);# remove last fake line3088 print $tb_child->body(0, $tb_child->body_height() - 1); # remove last fake line 3087 3089 $tb_child->clear; 3088 3090 3089 3091 # Switch parent and children port inter-connection 3090 3092 print "\n"; 3091 my $tb_parent = Text::Table->new( # http://www.perlmonks.org/?node_id=9883203093 my $tb_parent = Text::Table->new( # http://www.perlmonks.org/?node_id=988320 3092 3094 {align => 'left', align_title => 'left', title => 'Parent-Switch'}, 3093 3095 {align => 'right', align_title => 'right', title => 'Input-Port'}, … … 3099 3101 my ($sw_connect, $port_connect) = split m/ $SEP_SWITCH_PORT /xms, $swport, 2; 3100 3102 for my $sw (keys %{$db_switch_connected_on_port{$swport}}) { 3101 my $arrow = '<---';3102 $arrow ='<===' if $port_connect =~ m/^(Trk|Br|Po)/;3103 my $arrow = '<---'; 3104 $arrow = '<===' if $port_connect =~ m/^(Trk|Br|Po)/; 3103 3105 if (exists $db_switch_output_port{$sw}) { 3104 3106 $tb_parent->add($sw_connect, $port_connect, $arrow, $db_switch_output_port{$sw}, $sw); … … 3109 3111 } 3110 3112 } 3111 @colrange = map { scalar $tb_parent->colrange($_)} (0 .. 4); # force scaler context3112 $tb_parent->add(map { ' ' x $_ } reverse @colrange);# add empty line to force symetric table output3113 @colrange = map {scalar $tb_parent->colrange($_)} (0 .. 4); # force scaler context 3114 $tb_parent->add(map {' ' x $_} reverse @colrange); # add empty line to force symetric table output 3113 3115 print $tb_parent->title(); 3114 3116 print $tb_parent->rule('-'); 3115 print $tb_parent->body(0, $tb_parent->body_height() -1);# remove last fake line3117 print $tb_parent->body(0, $tb_parent->body_height() - 1); # remove last fake line 3116 3118 $tb_parent->clear; 3117 3119 … … 3135 3137 my $invisible_node = 0; # Count number of invisible node 3136 3138 3137 my %db_building = ();3139 my %db_building = (); 3138 3140 my %db_switch_line = (); # Number of line drawed on a switch 3139 3141 for my $sw (values %db_switch_global) { … … 3141 3143 $db_building{$building} ||= {}; 3142 3144 $db_building{$building}->{$location} ||= {}; 3143 $db_building{$building}->{$location}{ $sw->{'hostname'}} = 'y';3145 $db_building{$building}->{$location}{$sw->{'hostname'}} = 'y'; 3144 3146 3145 3147 $db_switch_line{$sw} = 0; … … 3154 3156 print "internet [label=\"internet\", color=black, fillcolor=cyan, shape=house, style=filled];\n"; 3155 3157 3156 my ($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp;3158 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $timestamp; 3157 3159 $year += 1900; 3158 3160 $mon++; … … 3161 3163 print "site -> \"$date\" [style=invis];\n"; 3162 3164 3163 my $b =0;3165 my $b = 0; 3164 3166 for my $building (keys %db_building) { 3165 3167 $b++; … … 3179 3181 3180 3182 my $peripheries = 1; 3181 my $color = 'lightblue';3183 my $color = 'lightblue'; 3182 3184 if ($db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/) { 3183 3185 $peripheries = 2; 3184 $color = "\"$color:$color\"";3186 $color = "\"$color:$color\""; 3185 3187 } 3186 3188 print "\"$sw:$db_switch_output_port{$sw}\" [label=\"".format_aggregator4dot($db_switch_output_port{$sw})."\", color=black, fillcolor=lightblue, peripheries=$peripheries, style=filled];\n"; … … 3198 3200 next if $port_connect eq $db_switch_output_port{$sw}; 3199 3201 my $peripheries = 1; 3200 my $color = 'plum';3202 my $color = 'plum'; 3201 3203 if ($port_connect =~ m/^(Trk|Br|Po)/) { 3202 3204 $peripheries = 2; 3203 $color = "\"$color:$color\"";3205 $color = "\"$color:$color\""; 3204 3206 } 3205 3207 print "\"$sw:$port_connect\" [label=\"".format_aggregator4dot($port_connect)."\", color=black, fillcolor=plum, peripheries=$peripheries, style=filled];\n"; … … 3215 3217 # print "$invisible -> \"$sw\" [style=invis]\n"; 3216 3218 # } 3217 }3219 } 3218 3220 } 3219 3221 } 3220 3222 } 3221 3223 3222 # print "Switch output port and parent port connection\n";3223 # print "---------------------------------------------\n";3224 # print "Switch output port and parent port connection\n"; 3225 # print "---------------------------------------------\n"; 3224 3226 for my $sw (sort keys %db_switch_output_port) { 3225 3227 if (exists $db_switch_parent{$sw}) { … … 3241 3243 my $graph_breaker = 1; 3242 3244 3243 # print "Switch parent and children port inter-connection\n";3244 # print "------------------------------------------------\n";3245 # print "Switch parent and children port inter-connection\n"; 3246 # print "------------------------------------------------\n"; 3245 3247 for my $swport (sort keys %db_switch_connected_on_port) { 3246 3248 my ($sw_connect, $port_connect) = split m/ $SEP_SWITCH_PORT /xms, $swport, 2; … … 3282 3284 } 3283 3285 3284 print "}\n";3286 print "}\n"; 3285 3287 return; 3286 3288 }
Note: See TracChangeset
for help on using the changeset viewer.