dottr.pl
author Peter Gervai <grin@grin.hu>
Wed, 15 May 2024 09:50:23 +0200
changeset 14 d2d8803e35ec
parent 2 b78794f00e34
permissions -rwxr-xr-x
pwgen.pl: support "hungarian-friendly" speaker mode, basically getting rid of 'qxy' with a given probability
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
#
2
b78794f00e34 Add dottr.pl login file and update the description a wee bit
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
     4
# (c) Peter 'grin' Gervai, 2012-2014
b78794f00e34 Add dottr.pl login file and update the description a wee bit
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
     5
# Released under CreativeCommons-Attribution-ShareAlike-4.0
1
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
#
2
b78794f00e34 Add dottr.pl login file and update the description a wee bit
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
     9
#  ./dottr.pl > map.dot
b78794f00e34 Add dottr.pl login file and update the description a wee bit
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    10
#
b78794f00e34 Add dottr.pl login file and update the description a wee bit
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    11
# create graphic from dot: 
b78794f00e34 Add dottr.pl login file and update the description a wee bit
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    12
#  dot -Tpng -o map.png map.dot
b78794f00e34 Add dottr.pl login file and update the description a wee bit
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    13
#  dot -Tsvg -o map.svg map.dot
1
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
use DBI;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
use Encode;
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
# 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
    19
my $SKIP_RESERVATION_LABELS = 1;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
$|=1;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
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
    24
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
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
    26
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
&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
    28
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
    29
                           $DatabaseLoginData::db_user, $DatabaseLoginData::db_pw, 
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
                           { RaiseError =>1, AutoCommit => 1 } );
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
$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
    32
&d("db ok");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
#########################################################################
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
my $sth_links = $dbh->prepare("
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
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
    37
 FROM RackObject AS ro1 
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
   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
    39
   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
    40
   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
    41
   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
    42
   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
    43
 ORDER BY obj1, port1");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
my $sth_reservations = $dbh->prepare("
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
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
    47
 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
    48
 FROM RackObject AS ro
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
   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
    50
   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
    51
     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
    52
 ORDER BY obj1, port1");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
&d("Start query.");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
my $res = $sth_links->execute;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
&d("Fetch query.");
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
my $a = $sth_links->fetchall_arrayref;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
my @data = @$a;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
if( !$SKIP_RESERVATION_LABELS ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    62
      $res = $sth_reservations->execute;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
      $a = $sth_reservations->fetchall_arrayref;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
      @data = (@data, @$a);
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
} else {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
      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
    67
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
# header
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
print '
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
graph rackspace_topo {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
label = "Tarr Racktables topo";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
rankdir = LR;
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
       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
    77
       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
    78
       
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
# generate edges
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
foreach $a (@data) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
  # node+interface 1
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
  $node{$a->[0]} = $a->[1];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
  $nodetype{$a->[0]} = $a->[5];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
  # node+interface 2
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
  $node{$a->[4]} = $a->[3];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
  # 
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
  push @link, $a;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
}
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
# output the nods first
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
foreach my $n (keys %node) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
  # nodes with empty or "space" names
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
  next if $n =~ /^\s*$/;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
  $ntyp = $nodetype{$n};
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
  # color/style depends on node type
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
  my $color="";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
  if( $ntyp eq "Server" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
    $color='color="black"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
  } elsif( $ntyp eq "Network switch" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106
    $color='color="red" style="bold"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   107
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
  } elsif( $ntyp eq "Server chassis" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
    $color='color="blue" style="dotted"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   110
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   111
  } elsif( $ntyp eq "Network chassis" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   112
    $color='color="lightblue"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   113
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   114
  } elsif( $ntyp eq "MediaConverter" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
    $color='color="gold"';
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   116
    
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   117
  } elsif( $ntyp eq "Router" ) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   118
    $color='color="darkgreen"';
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
  } else {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
    #$color='color="pink"';			# Barbie syndrome FTW
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
  
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   125
  print '"' . $n . "\" [ $color ];\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   126
  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
   127
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   128
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   129
# and print the edges
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   130
foreach my $i (0 .. $#link) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   131
  my $l = $link[$i];
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   132
  # skip empty...
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   133
  next if $l->[0] =~ /^\s*$/;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   134
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   135
  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
   136
    # skip duplicate
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   137
    # 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
   138
  } else {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   139
#    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
   140
    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
   141
    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
   142
    $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
   143
  }
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   144
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   145
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   146
print "\n};\n\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   147
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   148
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   149
# generate random html colors
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   150
sub gen_color {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   151
  my $c = "#";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   152
  for (1..3) {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   153
    $c .= sprintf "%02x", rand(256);
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   154
  }
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   155
  return $c;
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   156
}
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   157
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   158
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   159
sub d {
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   160
  print STDERR scalar(localtime), ' [$$] ', (shift), "\n";
26fed0d59d7c Add dottr.pl, a Racktables topology to GraphViz converter
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   161
}