Changeset 311


Ignore:
Timestamp:
Oct 21, 2017, 6:53:47 PM (7 years ago)
Author:
g7moreau
Message:
  • Begin use module Text:Table to export switch db in txt format
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r310 r311  
    77use strict;
    88use warnings;
    9 use version; our $VERSION = qv('0.7.3');
     9use version; our $VERSION = qv('0.7.4');
    1010
    1111use Readonly;
     
    2121use List::Util 'shuffle';
    2222use Digest::SHA qw(sha512_base64);
     23use Text::Table; # libtext-table-perl http://blogs.perl.org/users/steven_haryanto/2014/07/benchmarking-several-ascii-table-generator-modules.html
    2324
    2425# apt-get install snmp fping libnet-cidr-lite-perl libnet-netmask-perl libnet-snmp-perl libnetaddr-ip-perl libyaml-perl
    25 # libcrypt-des-perl libcrypt-hcesha-perl libdigest-hmac-perl
     26# libcrypt-des-perl libcrypt-hcesha-perl libdigest-hmac-perl libtext-table-perl
    2627# arping net-tools fping bind9-host arpwatch
    2728
     
    28472848#---------------------------------------------------------------
    28482849sub cmd_exportsw_txt {
    2849 
    28502850   my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE");
    28512851
     
    28542854   my %db_switch_connected_on_port = %{$switch_connection->{'connected_on_port'}};
    28552855
    2856    print "Switch output port and parent port connection\n";
    2857    print "---------------------------------------------\n";
     2856   my $tb_child = Text::Table->new( # http://www.perlmonks.org/?node_id=988320
     2857      {align => 'left',   align_title => 'left',   title => 'Child_Switch'},
     2858      {align => 'right',  align_title => 'right',  title => 'Output_Port'},
     2859      {align => 'center', align_title => 'center', title => 'Link'},
     2860      {align => 'left',   align_title => 'left',   title => 'Input_Port'},
     2861      {align => 'left',   align_title => 'left',   title => 'Parent_Switch'},
     2862      );
     2863   #print "Switch output port and parent port connection\n";
     2864   #print "---------------------------------------------\n";
    28582865   for my $sw (sort keys %db_switch_output_port) {
    2859       my $arrow ='-->';
    2860          $arrow ='==>' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/;
     2866      my $arrow ='--->';
     2867         $arrow ='===>' if $db_switch_output_port{$sw} =~ m/^(Trk|Br|Po)/;
    28612868      if (exists $db_switch_parent{$sw}) {
    2862          printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, $db_switch_parent{$sw}->{'port_hr'}, $db_switch_parent{$sw}->{'switch'};
     2869         #printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, $db_switch_parent{$sw}->{'port_hr'}, $db_switch_parent{$sw}->{'switch'};
     2870         $tb_child->add($sw, $db_switch_output_port{$sw}, $arrow, $db_switch_parent{$sw}->{'port_hr'}, $db_switch_parent{$sw}->{'switch'});
     2871
    28632872         }
    28642873      else {
    2865          printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, '', 'router';
    2866          }
    2867       }
     2874         #printf "%-28s %8s %3s %-8s %-25s\n", $sw, $db_switch_output_port{$sw}, $arrow, '', 'router';
     2875         $tb_child->add($sw, $db_switch_output_port{$sw}, $arrow, '', 'router');
     2876         }
     2877      }
     2878   my @colrange = map { scalar $tb_child->colrange($_) } (0 .. 4); # force scaler context
     2879   $tb_child->add(map { ' ' x $_ } reverse @colrange); # add empty line to force symetric table
     2880   print $tb_child->title();
     2881   print $tb_child->rule('-');
     2882   print $tb_child->body(0, $tb_child->body_height()-1); # remove last fake line
     2883   $tb_child->clear;
     2884
    28682885   print "\n";
    2869 
    2870    print "Switch parent and children port inter-connection\n";
    2871    print "------------------------------------------------\n";
     2886   my $tb_parent = Text::Table->new( # http://www.perlmonks.org/?node_id=988320
     2887      {align => 'left',   align_title => 'left',   title => 'Parent_Switch'},
     2888      {align => 'right',  align_title => 'right',  title => 'Input_Port'},
     2889      {align => 'center', align_title => 'center', title => 'Link'},
     2890      {align => 'left',   align_title => 'left',   title => 'Output_Port'},
     2891      {align => 'left',   align_title => 'left',   title => 'Child_Switch'},
     2892      );
     2893   #print "Switch parent and children port inter-connection\n";
     2894   #print "------------------------------------------------\n";
    28722895   for my $swport (sort keys %db_switch_connected_on_port) {
    28732896      my ($sw_connect, $port_connect) = split m/ $SEP_SWITCH_PORT /xms, $swport, 2;
     
    28762899            $arrow ='<==' if $port_connect =~ m/^(Trk|Br|Po)/;
    28772900         if (exists $db_switch_output_port{$sw}) {
    2878             printf "%-28s %8s %3s %-8s %-25s\n", $sw_connect, $port_connect, $arrow, $db_switch_output_port{$sw}, $sw;
     2901            #printf "%-28s %8s %3s %-8s %-25s\n", $sw_connect, $port_connect, $arrow, $db_switch_output_port{$sw}, $sw;
     2902            $tb_parent->add($sw_connect, $port_connect, $arrow, $db_switch_output_port{$sw}, $sw);
    28792903            }
    28802904         else {
    2881             printf "%-28s %8s %3s %-8s %-25s\n", $sw_connect, $port_connect, $arrow, '', $sw;
     2905            #printf "%-28s %8s %3s %-8s %-25s\n", $sw_connect, $port_connect, $arrow, '', $sw;
     2906            $tb_parent->add($sw_connect, $port_connect, $arrow, '', $sw);
    28822907            }
    28832908         }
    28842909      }
     2910   @colrange = map { scalar $tb_parent->colrange($_) } (0 .. 4); # force scaler context
     2911   $tb_parent->add(map { ' ' x $_ } reverse @colrange); # add empty line to force symetric table
     2912   print $tb_parent->title();
     2913   print $tb_parent->rule('-');
     2914   print $tb_parent->body(0, $tb_parent->body_height()-1); # remove last fake line
     2915   $tb_parent->clear;
     2916
    28852917   return;
    28862918   }
Note: See TracChangeset for help on using the changeset viewer.