- Timestamp:
- Oct 31, 2017, 3:40:44 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/klask
r325 r326 2012 2012 2013 2013 my $days_before_alert = $DEFAULT{'days-before-alert'} || 15; 2014 my $format = 'txt'; 2014 2015 my $verbose; 2015 2016 2016 2017 GetOptions( 2017 'day|d=i' => \$days_before_alert, 2018 'day|d=i' => \$days_before_alert, 2019 'format|f=s' => \$format, 2018 2020 ); 2021 2022 my %possible_format = ( 2023 txt => \&cmd_bad_vlan_id_txt, 2024 html => \&cmd_bad_vlan_id_html, 2025 none => sub {}, 2026 ); 2027 $format = 'txt' if not defined $possible_format{$format}; 2019 2028 2020 2029 test_maindb_environnement(); … … 2063 2072 } 2064 2073 2065 # swL2-legi-1.hmg.priv Gi1/0/3 / legi-calcul +-> legi-683 (337) 2017-10-31/10:30 2017-09-08/11:02 78:24:AF:88:69:64 meige8pcsv149.legi.grenoble-inp.fr 2074 my @result = (); 2075 2076 LOOP_ON_RECENT_COMPUTER: 2077 for my $swpt (keys %switchportdb) { 2078 next LOOP_ON_RECENT_COMPUTER if $swpt =~ m/^\s*0$/; 2079 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'hostname_fq'} !~ m/$RE_FLOAT_HOSTNAME/; 2080 2081 my $src_ip = $switchportdb{$swpt}->{'ip'}; 2082 my $src_timestamp = 0; 2083 LOOP_ON_IP_ADDRESS: 2084 for my $ip (keys %{$computerdb}) { 2085 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'mac_address'} ne $switchportdb{$swpt}->{'mac_address'}; 2086 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} =~ m/$RE_FLOAT_HOSTNAME/; 2087 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'timestamp'} < $src_timestamp; 2088 2089 $src_ip = $ip; 2090 $src_timestamp = $computerdb->{$ip}{'timestamp'}; 2091 } 2092 2093 # keep only if float computer is the most recent 2094 next LOOP_ON_RECENT_COMPUTER if $src_timestamp == 0; 2095 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'timestamp'} < $src_timestamp; 2096 2097 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $switchportdb{$swpt}->{'timestamp'}; 2098 $year += 1900; 2099 $mon++; 2100 my $date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; 2101 2102 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$src_ip}{'timestamp'}; 2103 $year += 1900; 2104 $mon++; 2105 my $src_date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; 2106 2107 my $vlan_id = get_current_vlan_id($computerdb->{$src_ip}{'network'}); 2108 my ($switch_hostname, $port_hr) = split /\s+/, $swpt, 2; 2109 2110 push @result, { 2111 switch => $switch_hostname, 2112 port_hr => $port_hr, 2113 vlan_bad => $switchportdb{$swpt}->{'vlan'}, 2114 vlan_good =>$computerdb->{$src_ip}{'network'}, 2115 vlan_id => $vlan_id, 2116 date_last => $date, 2117 date_good => $src_date, 2118 mac_address => $computerdb->{$src_ip}{'mac_address'}, 2119 hostname_fq => $computerdb->{$src_ip}{'hostname_fq'}, 2120 }; 2121 } 2122 2123 $possible_format{$format}->(@result); 2124 } 2125 2126 #--------------------------------------------------------------- 2127 sub cmd_bad_vlan_id_txt { 2128 @result = @_; 2129 2066 2130 my $tb_bad = Text::Table->new( 2067 2131 {align => 'left', align_title => 'left', title => 'Switch'}, … … 2084 2148 ); 2085 2149 2086 LOOP_ON_RECENT_COMPUTER: 2087 for my $swpt (keys %switchportdb) { 2088 next LOOP_ON_RECENT_COMPUTER if $swpt =~ m/^\s*0$/; 2089 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'hostname_fq'} !~ m/$RE_FLOAT_HOSTNAME/; 2090 2091 my $src_ip = $switchportdb{$swpt}->{'ip'}; 2092 my $src_timestamp = 0; 2093 LOOP_ON_IP_ADDRESS: 2094 for my $ip (keys %{$computerdb}) { 2095 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'mac_address'} ne $switchportdb{$swpt}->{'mac_address'}; 2096 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'hostname_fq'} =~ m/$RE_FLOAT_HOSTNAME/; 2097 next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{'timestamp'} < $src_timestamp; 2098 2099 $src_ip = $ip; 2100 $src_timestamp = $computerdb->{$ip}{'timestamp'}; 2101 } 2102 2103 # keep only if float computer is the most recent 2104 next LOOP_ON_RECENT_COMPUTER if $src_timestamp == 0; 2105 next LOOP_ON_RECENT_COMPUTER if $switchportdb{$swpt}->{'timestamp'} < $src_timestamp; 2106 2107 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $switchportdb{$swpt}->{'timestamp'}; 2108 $year += 1900; 2109 $mon++; 2110 my $date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; 2111 2112 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$src_ip}{'timestamp'}; 2113 $year += 1900; 2114 $mon++; 2115 my $src_date = sprintf '%04i-%02i-%02i/%02i:%02i', $year, $mon, $mday, $hour, $min; 2116 2117 my $vlan_id = get_current_vlan_id($computerdb->{$src_ip}{'network'}); 2118 my ($switch_hostname, $port_hr) = split /\s+/, $swpt, 2; 2150 for my $item (@result) { 2119 2151 $tb_bad->add( 2120 $ switch_hostname,2121 $ port_hr,2122 $ switchportdb{$swpt}->{'vlan'},2123 $ computerdb->{$src_ip}{'network'},2124 '(' . $ vlan_id. ')',2125 $ date,2126 $ src_date,2127 $ computerdb->{$src_ip}{'mac_address'},2128 $ computerdb->{$src_ip}{'hostname_fq'},2152 $item->{'switch'}, 2153 $item->{'port_hr'}, 2154 $item->{'vlan_bad'}, 2155 $item->{'vlan_good'}, 2156 '(' . $item->{'vlan_id'} . ')', 2157 $item->{'date_last'}, 2158 $item->{'date_good'}, 2159 $item->{'mac_address'}, 2160 $item->{'hostname_fq'}, 2129 2161 ); 2130 2162 } 2163 2131 2164 print $tb_bad->title(); 2132 2165 print $tb_bad->rule('-'); 2133 2166 print $tb_bad->body(); 2167 } 2168 2169 #--------------------------------------------------------------- 2170 sub cmd_bad_vlan_id_html { 2171 @result = @_; 2172 2173 print <<'END_HTML'; 2174 <table class="sortable" summary="Klask Bad VLAN ID Database"> 2175 <caption>Klask Bad VLAN ID Database</caption> 2176 <thead> 2177 <tr> 2178 <th scope="col" class="klask-header-left">Switch ⇅</th> 2179 <th scope="col" class="sorttable_nosort">Port</th> 2180 <th scope="col" class="sorttable_alpha">VLAN-Bad ⇅</th> 2181 <th scope="col" class="sorttable_alpha">VLAN-Good ⇅</th> 2182 <th scope="col" class="sorttable_alpha">Date-Last ⇅</th> 2183 <th scope="col" class="sorttable_alpha">Date-Good ⇅</th> 2184 <th scope="col" class="sorttable_alpha">MAC-Address ⇅</th> 2185 <th scope="col" class="sorttable_alpha">Hostname-FQ ⇅</th> 2186 </tr> 2187 </thead> 2188 <tfoot> 2189 <tr> 2190 <th scope="col" class="klask-footer-left">Switch</th> 2191 <th scope="col" class="fklask-nosort">Port</th> 2192 <th scope="col" class="fklask-alpha">VLAN-Bad</th> 2193 <th scope="col" class="fklask-alpha">VLAN-Good</th> 2194 <th scope="col" class="fklask-alpha">Date-Last</th> 2195 <th scope="col" class="fklask-alpha">Date-Good</th> 2196 <th scope="col" class="fklask-alpha">MAC-Address</th> 2197 <th scope="col" class="fklask-alpha">Hostname-FQ</th> 2198 </tr> 2199 </tfoot> 2200 <tbody> 2201 END_HTML 2202 2203 my $typerow = 'even'; 2204 2205 for my $item (@result) { 2206 2207 $typerow = $typerow eq 'even' ? 'odd' : 'even'; 2208 2209 my $switch_hostname_sort = sprintf '%s %3s' ,$item->{'switch'}, $item->{'port_hr'}; 2210 my ( $host_short ) = split m/ \. /xms, $result_ip{$ip}->{'hostname_fq'}; 2211 2212 my $vlan_nameid = $item->{'vlan_good'} . ' (' . $item->{'vlan_id'} . ')'; 2213 2214 fqdn_html_breakable(my $hostname_fq_html = $item->{'hostname_fq'}); 2215 2216 print <<"END_HTML"; 2217 <tr class="$typerow"> 2218 <td sorttable_customkey="$switch_hostname_sort">$item->{'switch'}</td> 2219 <td>$item->{'port_hr'}</td> 2220 <td>!$item->{'vlan_bad'}</td> 2221 <td>$vlan_nameid</td> 2222 <td>$item->{'date_last'}</td> 2223 <td>$item->{'date_good'}</td> 2224 <td>$item->{'mac_address'}</td> 2225 <td sorttable_customkey="$host_short">$hostname_fq_html</td> 2226 </tr> 2227 END_HTML 2228 } 2229 print <<'END_HTML'; 2230 </tbody> 2231 </table> 2232 END_HTML 2134 2233 } 2135 2234
Note: See TracChangeset
for help on using the changeset viewer.