Changeset 326 for trunk


Ignore:
Timestamp:
Oct 31, 2017, 3:40:44 PM (6 years ago)
Author:
g7moreau
Message:
  • Create bad-vlan-id format html
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r325 r326  
    20122012
    20132013   my $days_before_alert = $DEFAULT{'days-before-alert'} || 15;
     2014   my $format = 'txt';
    20142015   my $verbose;
    20152016
    20162017   GetOptions(
    2017       'day|d=i'   => \$days_before_alert,
     2018      'day|d=i'      => \$days_before_alert,
     2019      'format|f=s'   => \$format,
    20182020      );
     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};
    20192028
    20202029   test_maindb_environnement();
     
    20632072      }
    20642073
    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#---------------------------------------------------------------
     2127sub cmd_bad_vlan_id_txt {
     2128   @result = @_;
     2129
    20662130   my $tb_bad = Text::Table->new(
    20672131      {align  => 'left',   align_title => 'left',   title => 'Switch'},
     
    20842148      );
    20852149
    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) {
    21192151      $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'},
    21292161         );
    21302162      }
     2163
    21312164   print $tb_bad->title();
    21322165   print $tb_bad->rule('-');
    21332166   print $tb_bad->body();
     2167   }
     2168
     2169#---------------------------------------------------------------
     2170sub 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 &#8645</th>
     2179   <th scope="col" class="sorttable_nosort">Port</th>
     2180   <th scope="col" class="sorttable_alpha">VLAN-Bad &#8645</th>
     2181   <th scope="col" class="sorttable_alpha">VLAN-Good &#8645</th>
     2182   <th scope="col" class="sorttable_alpha">Date-Last &#8645</th>
     2183   <th scope="col" class="sorttable_alpha">Date-Good &#8645</th>
     2184   <th scope="col" class="sorttable_alpha">MAC-Address &#8645</th>
     2185   <th scope="col" class="sorttable_alpha">Hostname-FQ &#8645</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>
     2201END_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>
     2227END_HTML
     2228      }
     2229   print <<'END_HTML';
     2230 </tbody>
     2231</table>
     2232END_HTML
    21342233   }
    21352234
Note: See TracChangeset for help on using the changeset viewer.