Changeset 45 for trunk


Ignore:
Timestamp:
Jul 23, 2009, 12:06:45 AM (15 years ago)
Author:
g7moreau
Message:
  • Add html export format
  • Add network to the computer database data
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r44 r45  
    5656   updatesw   => \&cmd_updatesw,
    5757   exportsw   => \&cmd_exportsw,
    58    dotsw      => \&cmd_exportsw_dot,
    5958   iplocation => \&cmd_iplocation,
    6059   'search-mac-on-switch' => \&cmd_search_mac_on_switch,
     
    503502
    504503 klask updatedb
    505  klask exportdf
     504 klask exportdb
     505
     506 klask updatesw
     507 klask exportsw
    506508
    507509 klask searchdb computer
     
    693695               switch_port        => $where{switch_port},
    694696               timestamp          => $timestamp,
     697               network            => $net,
    695698               };
    696699            next LOOP_ON_COMPUTER;
     
    707710               switch_port        => $where{switch_port},
    708711               timestamp          => $resol_arp{timestamp},
     712               network            => $net,
    709713               };
    710714            }
     
    765769
    766770sub cmd_exportdb {
     771   my @ARGV   = @_;
     772
     773   my $format = 'txt';
     774
     775   my $ret = GetOptions(
     776      'format|f=s'  => \$format,
     777      );
     778
     779   my %possible_format = (
     780      txt  => \&cmd_exportdb_txt,
     781      html => \&cmd_exportdb_html,
     782      );
     783
     784   $format = 'txt' if not defined $possible_format{$format};
     785   
     786   $possible_format{$format}->(@ARGV);
     787   }
     788
     789sub cmd_exportdb_txt {
    767790   test_maindb_environnement();
    768791
     
    796819         $date;
    797820      }
     821   }
     822
     823sub cmd_exportdb_html {
     824   test_maindb_environnement();
     825
     826   my $computerdb = YAML::Syck::LoadFile("$KLASK_DB_FILE");
     827
     828#<link rel="stylesheet" type="text/css" href="style-klask.css" />
     829#<script src="sorttable-klask.js"></script>
     830
     831   print <<END;
     832<table class="sortable" summary="Klask export database">
     833 <caption>Klask database</caption>
     834 <thead>
     835  <tr>
     836   <th scope="col" class="hklask-switch">Switch</th>
     837   <th scope="col" class="sorttable_nosort">Port</th>
     838   <th scope="col" class="sorttable_nosort">Link</th>
     839   <th scope="col" class="sorttable_alpha">Hostname</th>
     840   <th scope="col" class="hklask-ipv4">IPv4-Address</th>
     841   <th scope="col" class="hklask-mac">MAC-Address</th>
     842   <th scope="col" class="hklask-date">Date</th>
     843  </tr>
     844 </thead>
     845 <tfoot>
     846  <tr>
     847   <th scope="col" class="fklask-switch">Switch</th>
     848   <th scope="col" class="fklask-port">Port</th>
     849   <th scope="col" class="fklask-link">Link</th>
     850   <th scope="col" class="fklask-hostname">Hostname</th>
     851   <th scope="col" class="fklask-ipv4">IPv4-Address</th>
     852   <th scope="col" class="fklask-mac">MAC-Address</th>
     853   <th scope="col" class="fklask-date">Date</th>
     854  </tr>
     855 </tfoot>
     856 <tbody>
     857END
     858
     859   my %mac_count = ();
     860   LOOP_ON_IP_ADDRESS:
     861   foreach my $ip (keys %$computerdb) {
     862   
     863      # to be improve in the future
     864      next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !
     865     
     866      $mac_count{$computerdb->{$ip}{mac_address}}++;
     867      }
     868
     869   my $typerow = 'even';
     870
     871   LOOP_ON_IP_ADDRESS:
     872   foreach my $ip (Net::Netmask::sort_by_ip_address(keys %$computerdb)) {
     873   
     874      # to be improve in the future
     875      next LOOP_ON_IP_ADDRESS if $computerdb->{$ip}{hostname_fq} eq ($computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description}); # switch on himself !
     876
     877      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($computerdb->{$ip}{timestamp});
     878      $year += 1900;
     879      $mon++;
     880      my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min;
     881
     882#      $odd_or_even++;
     883#      my $typerow = $odd_or_even % 2 ? 'odd' : 'even';
     884      $typerow = $typerow eq 'even' ? 'odd' : 'even';
     885
     886      my $switch_hostname = $computerdb->{$ip}{switch_hostname} || $computerdb->{$ip}{switch_description} || 'unkown';
     887      chomp $switch_hostname;
     888      my $switch_hostname_sort = sprintf "%s %3s" ,$switch_hostname, $computerdb->{$ip}{switch_port};
     889
     890      my $ip_sort = sprintf "%03i%03i%03i%03i", split( /\./, $ip);
     891
     892      my $mac_sort = sprintf "%04i-%s", 9999 - $mac_count{$computerdb->{$ip}{mac_address}}, $computerdb->{$ip}{mac_address};
     893
     894      $computerdb->{$ip}{hostname_fq} = 'unknow' if $computerdb->{$ip}{hostname_fq} =~ m/^\d+\.\d+\.\d+\.\d+$/;
     895      my ( $host_short ) = split /\./, $computerdb->{$ip}{hostname_fq};
     896
     897      print <<END;
     898  <tr class="$typerow">
     899   <td sorttable_customkey="$switch_hostname_sort">$switch_hostname</td>
     900   <td class="bklask-port">$computerdb->{$ip}{switch_port}</td>
     901   <td><-------</td>
     902   <td sorttable_customkey="$host_short">$computerdb->{$ip}{hostname_fq}</td>
     903   <td sorttable_customkey="$ip_sort">$ip</td>
     904   <td sorttable_customkey="$mac_sort">$computerdb->{$ip}{mac_address}</td>
     905   <td>$date</td>
     906  </tr>
     907END
     908      }
     909
     910   my $switch_connection = YAML::Syck::LoadFile("$KLASK_SW_FILE");
     911
     912   my %db_switch_output_port       = %{$switch_connection->{output_port}};
     913   my %db_switch_parent            = %{$switch_connection->{parent}};
     914   my %db_switch_connected_on_port = %{$switch_connection->{connected_on_port}};
     915   my %db_switch                   = %{$switch_connection->{switch_db}};
     916
     917   for my $sw (sort keys %db_switch_output_port) {
     918
     919      my $switch_hostname_sort = sprintf "%s %3s" ,$sw, $db_switch_output_port{$sw};
     920
     921      $typerow = $typerow eq 'even' ? 'odd' : 'even';
     922
     923      if (exists $db_switch_parent{$sw}) {
     924
     925      my $mac_address = $db_switch{$db_switch_parent{$sw}->{switch}}->{mac_address};
     926      my $ipv4_address = $db_switch{$db_switch_parent{$sw}->{switch}}->{ipv4_address};
     927      my $timestamp = $db_switch{$db_switch_parent{$sw}->{switch}}->{timestamp};
     928
     929      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp;
     930      $year += 1900;
     931      $mon++;
     932      my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min;
     933
     934      my $ip_sort = sprintf "%03i%03i%03i%03i", split( /\./, $ipv4_address);
     935
     936      my $mac_sort = sprintf "%04i-%s", 9999, $mac_address;
     937
     938
     939         my ( $host_short ) = sprintf "%s %3s" , split(/\./, $db_switch_parent{$sw}->{switch}, 1), $db_switch_parent{$sw}->{port};
     940
     941print <<END;
     942  <tr class="$typerow">
     943   <td sorttable_customkey="$switch_hostname_sort">$sw</td>
     944   <td class="bklask-port">$db_switch_output_port{$sw}</>
     945   <td>+--> $db_switch_parent{$sw}->{port}</td>
     946   <td sorttable_customkey="$host_short">$db_switch_parent{$sw}->{switch}</>
     947   <td sorttable_customkey="$ip_sort">$ipv4_address</td>
     948   <td sorttable_customkey="$mac_sort">$mac_address</td>
     949   <td>$date</td>
     950  </tr>
     951END
     952         }
     953      else {
     954#         printf "%-25s  %2s  +-->  router\n", $sw, $db_switch_output_port{$sw};
     955print <<END;
     956  <tr class="$typerow">
     957   <td sorttable_customkey="$switch_hostname_sort">$sw</td>
     958   <td class="bklask-port">$db_switch_output_port{$sw}</>
     959   <td>+--></td>
     960   <td sorttable_customkey="router">router</>
     961   <td sorttable_customkey="999999999999"></td>
     962   <td sorttable_customkey="99999"></td>
     963   <td></td>
     964  </tr>
     965END
     966         }
     967      }
     968
     969   for my $swport (sort keys %db_switch_connected_on_port) {
     970      my ($sw_connect,$port_connect) = split ':', $swport;
     971      for my $sw (keys %{$db_switch_connected_on_port{$swport}}) {
     972
     973         my $switch_hostname_sort = sprintf "%s %3s" ,$sw_connect, $port_connect;
     974
     975      my $mac_address = $db_switch{$sw}->{mac_address};
     976      my $ipv4_address = $db_switch{$sw}->{ipv4_address};
     977      my $timestamp = $db_switch{$sw}->{timestamp};
     978
     979      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $timestamp;
     980      $year += 1900;
     981      $mon++;
     982      my $date = sprintf "%04i-%02i-%02i %02i:%02i", $year,$mon,$mday,$hour,$min;
     983
     984      my $ip_sort = sprintf "%03i%03i%03i%03i", split( /\./, $ipv4_address);
     985
     986      my $mac_sort = sprintf "%04i-%s", 9999, $mac_address;
     987
     988      $typerow = $typerow eq 'even' ? 'odd' : 'even';
     989
     990         if (exists $db_switch_output_port{$sw}) {
     991
     992             my ( $host_short ) = sprintf "%s %3s" , split(/\./, $sw, 1), $db_switch_output_port{$sw};
     993
     994print <<END;
     995  <tr class="$typerow">
     996   <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td>
     997   <td class="bklask-port">$port_connect</>
     998   <td>&lt;--+ $db_switch_output_port{$sw}</td>
     999   <td sorttable_customkey="$host_short">$sw</>
     1000   <td sorttable_customkey="$ip_sort">$ipv4_address</td>
     1001   <td sorttable_customkey="$mac_sort">$mac_address</td>
     1002   <td>$date</td>
     1003  </tr>
     1004END
     1005            }
     1006         else {
     1007print <<END;
     1008  <tr class="$typerow">
     1009   <td sorttable_customkey="$switch_hostname_sort">$sw_connect</td>
     1010   <td class="bklask-port">$port_connect</>
     1011   <td>&lt;--+</td>
     1012   <td sorttable_customkey="$sw">$sw</>
     1013   <td sorttable_customkey="">$ipv4_address</td>
     1014   <td sorttable_customkey="">$mac_address</td>
     1015   <td>$date</td>
     1016  </tr>
     1017END
     1018            }
     1019         }
     1020      }
     1021
     1022
     1023   print <<END;
     1024 </tbody>
     1025</table>
     1026END
    7981027   }
    7991028
     
    8991128#   for my $one_computer ('194.254.66.254') {
    9001129   for my $one_router ( get_list_main_router(get_list_network()) ) {
    901 print "TT$one_router\n";     
     1130#print "TT$one_router\n";     
    9021131      my %resol_arp = resolve_ip_arp_host($one_router,'*','low');            # resolution arp
    9031132      next DETECT_ALL_ROUTER if $resol_arp{mac_address} eq 'unknow';
    904 print "$one_router\n";     
     1133#print "$one_router\n";     
    9051134      $where{$resol_arp{ipv4_address}} = find_all_switch_port($resol_arp{mac_address}); # retrouve les emplacements des routeurs
    9061135      }
     
    9371166      push @list_switch_ip, $sw->{hostname};
    9381167      }
     1168
     1169   my $timestamp = time;
    9391170
    9401171   ALL_SWITCH:
     
    9481179
    9491180      $db_switch_ip_hostname{$resol_arp{ipv4_address}} = $resol_arp{hostname_fq};
     1181
     1182      $SWITCH_DB{$one_computer}->{ipv4_address} = $resol_arp{ipv4_address};
     1183      $SWITCH_DB{$one_computer}->{mac_address}  = $resol_arp{mac_address};
     1184      $SWITCH_DB{$one_computer}->{timestamp}    = $timestamp;
    9501185      }
    9511186     
     
    12171452__END__
    12181453
    1219 
    12201454=head1 NAME
    12211455
     
    12261460
    12271461 klask updatedb
    1228  klask exportdb
     1462 klask exportdb --format [txt|html]
    12291463
    12301464 klask updatesw
     
    13651599
    13661600GPL version 2 or later and Perl equivalent
     1601
Note: See TracChangeset for help on using the changeset viewer.