dottr.pl
author Peter Gervai <grin@grin.hu>
Fri, 03 Oct 2014 15:01:17 +0200
changeset 1 26fed0d59d7c
child 2 b78794f00e34
permissions -rwxr-xr-x
Add dottr.pl, a Racktables topology to GraphViz converter
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
#!/usr/bin/perl
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
##$Id: dottr.pl,v ac49839c1b85 2014/10/03 12:46:32 grin $
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
#
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
# (c) Peter 'grin' Gervai, 2012
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     5
# Released under CreativeCommons-Attribution-ShareAlike-3.0-Unported
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
#
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
# create .dot from racktables links
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
#
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    10
use DBI;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
use Encode;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    12
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    13
# if 1 we do not list reserved port descriptions
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
my $SKIP_RESERVATION_LABELS = 1;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
$|=1;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
use DatabaseLoginData; # $db_user; $db_pw; $db_host
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    19
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
my ($db_name, $db_port) = ("racktables_db", 3306);
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
&d("connect db dbi:mysql:database=$db_name;host=$DatabaseLoginData::db_host;port=$db_port");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
my $dbh = DBI->connect( "dbi:mysql:database=$db_name;host=$DatabaseLoginData::db_host;port=$db_port",
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
                           $DatabaseLoginData::db_user, $DatabaseLoginData::db_pw, 
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
                           { RaiseError =>1, AutoCommit => 1 } );
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
$dbh->do('set names utf8');	# from Alexandr Alakin
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
&d("db ok");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
#########################################################################
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
my $sth_links = $dbh->prepare("
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
SELECT ro1.name AS obj1, p1.name AS port1, Link.cable, p2.name AS port2, ro2.name AS obj2, d.dict_value AS obj1type
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
 FROM RackObject AS ro1 
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
   JOIN Port AS p1 ON(ro1.id=p1.object_id)
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
   JOIN Link       ON(p1.id=Link.porta)
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
   JOIN Port AS p2 ON(Link.portb=p2.id)
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
   JOIN RackObject AS ro2 ON(p2.object_id=ro2.id)
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
   LEFT JOIN Dictionary AS d ON(ro1.objtype_id=d.dict_key)
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
 ORDER BY obj1, port1");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
my $sth_reservations = $dbh->prepare("
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
SELECT ro.name AS obj1, p.name AS port1, 'RESERVE' AS cable, '' AS port2, 
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
 CONCAT(reservation_comment,' #',ROUND(RAND()*1000))  AS obj2,  d.dict_value AS obj1type
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
 FROM RackObject AS ro
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
   JOIN Port AS p  ON(ro.id=p.object_id)
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
   LEFT JOIN Dictionary AS d ON(ro.objtype_id=d.dict_key)
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
     WHERE p.reservation_comment IS NOT NULL
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
 ORDER BY obj1, port1");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
&d("Start query.");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
my $res = $sth_links->execute;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    52
&d("Fetch query.");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
my $a = $sth_links->fetchall_arrayref;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
my @data = @$a;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
if( !$SKIP_RESERVATION_LABELS ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
      $res = $sth_reservations->execute;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
      $a = $sth_reservations->fetchall_arrayref;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
      @data = (@data, @$a);
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
} else {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
      print STDERR "Skipping reservation label mapping.\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    62
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
# header
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
print '
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
graph rackspace_topo {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
label = "Tarr Racktables topo";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
rankdir = LR;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
       edge [ color="#0000a0", decorate=true, fontsize=9, headclip=false ];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
       node [ shape=box, headport=n, tailport=n ];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
       
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    76
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    77
# generate edges
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
foreach $a (@data) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
  # node+interface 1
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
  $node{$a->[0]} = $a->[1];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
  $nodetype{$a->[0]} = $a->[5];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
  # node+interface 2
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
  $node{$a->[4]} = $a->[3];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
  # 
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
  push @link, $a;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
# output the nods first
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
foreach my $n (keys %node) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
  # nodes with empty or "space" names
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
  next if $n =~ /^\s*$/;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
  $ntyp = $nodetype{$n};
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
  # color/style depends on node type
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
  my $color="";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
  if( $ntyp eq "Server" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
    $color='color="black"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   100
  } elsif( $ntyp eq "Network switch" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
    $color='color="red" style="bold"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
  } elsif( $ntyp eq "Server chassis" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
    $color='color="blue" style="dotted"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106
  } elsif( $ntyp eq "Network chassis" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   107
    $color='color="lightblue"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
  } elsif( $ntyp eq "MediaConverter" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   110
    $color='color="gold"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   111
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   112
  } elsif( $ntyp eq "Router" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   113
    $color='color="darkgreen"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   114
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
  } else {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   116
    #$color='color="pink"';			# Barbie syndrome FTW
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   117
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   118
  }
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   119
  
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   120
  print '"' . $n . "\" [ $color ];\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
  if( !defined($node_color{$n}) ) { $node_color{ $n } = &gen_color; }
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   122
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   123
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
# and print the edges
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   125
foreach my $i (0 .. $#link) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   126
  my $l = $link[$i];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   127
  # skip empty...
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   128
  next if $l->[0] =~ /^\s*$/;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   129
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   130
  if( defined( $link{ $l->[0] }{ $l->[4] } ) && $link{ $l->[0] }{ $l->[4] } == $l->[2] ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   131
    # skip duplicate
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   132
    # if you need warnings... here you can.
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   133
  } else {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   134
#    print '"', $l->[0], '" -- "', $l->[4], '" [ label="', $l->[2], "\" color=\"grey\"];\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   135
    my $color = $node_color{ $l->[0] } or 'grey';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   136
    print '"', $l->[0], '" -- "', $l->[4], '" [ label="', $l->[2], "\" color=\"$color\"];\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   137
    $link{ $l->[0] }{ $l->[4] } = $l->[2];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   138
  }
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   139
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   140
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   141
print "\n};\n\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   142
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   143
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   144
# generate random html colors
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   145
sub gen_color {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   146
  my $c = "#";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   147
  for (1..3) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   148
    $c .= sprintf "%02x", rand(256);
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   149
  }
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   150
  return $c;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   151
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   152
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   153
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   154
sub d {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   155
  print STDERR scalar(localtime), ' [$$] ', (shift), "\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   156
}