forked from chrisridd/opendj-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology2dot
executable file
·303 lines (251 loc) · 7.85 KB
/
topology2dot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/perl -w
#
# Extract OpenDJ replication topology information from a dump of the
# <cn=monitor> subtree in LDIF format, and output a Graphviz DOT format
# representation.
#
# Limitations:
# From 2.6.0 it is possible to see more information about each DS. However it is
# not possible to determine which DSes are part of the same OpenDJ server, so
# the output shows more DSes than you would initially expect. Ignoring certain
# suffixes like <cn=admin data> and <cn=schema> can be helpful.
#
# The output is not particularly beautiful, as I usually import it into
# OmniGraffle which only interprets a subset of DOT styles.
use strict;
use warnings;
use Net::LDAP::LDIF;
# Values for the DOT fillcolor, line and fontcolor attributes for RSes and DSes
my $rsfill = qq{"#4ea3df"};
my $rsline = qq{"#011993"};
my $rstext = qq{"#ffffff"};
my $dsfill = qq{"#caf3d9"};
my $dsline = qq{"#9ebfab"};
my $dstext = qq{"#000000"};
package RS;
sub new {
my ($self,$hostname,$port,$rsid) = @_;
my $type = ref($self) || $self;
my $obj = bless {}, $type;
$obj->{name} = "$hostname:$port";
$obj->{rsid} = $rsid;
$obj->{domains} = [];
return $obj;
}
sub get_name {
my $self = shift;
return $self->{name};
}
sub get_id {
my $self = shift;
return $self->{rsid};
}
sub add_domain {
my ($self,$domain) = @_;
push @{$self->{domains}}, $domain;
}
sub get_domains {
my ($self,$rsid) = @_;
return @{$self->{domains}};
}
1;
package DS;
sub new {
my ($self,$hostname,$dsid,$domain) = @_;
my $type = ref($self) || $self;
my $obj = bless {}, $type;
$obj->{name} = $hostname;
$obj->{dsid} = $dsid;
$obj->{domain} = $domain;
return $obj;
}
sub get_name {
my $self = shift;
return $self->{name};
}
sub get_id {
my $self = shift;
return $self->{dsid};
}
sub get_domain {
my $self = shift;
return $self->{domain};
}
sub set_rs {
my ($self,$rs) = @_;
$self->{rs} = $rs;
}
sub get_rs {
my $self = shift;
return $self->{rs};
}
1;
package main;
sub html {
my $str = shift;
$str =~ s/&/&/g;
$str =~ s/</</g;
$str =~ s/>/>/g;
return $str;
}
my %ignore;
foreach (@ARGV) {
$ignore{$_} = 1;
}
@ARGV = ();
my $ldif = Net::LDAP::LDIF->new(shift, "r", onerror => 'undef');
my %rs;
my %rsbyid;
my %ds;
my %domain;
while (not $ldif->eof()) {
my $entry = $ldif->read_entry();
unless ($ldif->error()) {
my $dn = $entry->dn();
my $serverid = $entry->get_value('server-id');
my $domainname = $entry->get_value('domain-name');
next if defined($domainname) && exists $ignore{$domainname};
my $connectedto = $entry->get_value('connected-to');
# 2.6.0: cn=Replication server RS(...) hostname:port
if ($dn =~ m{^cn=Replication server RS\((\d+)\) ([^:]+):(\d+),}i) {
# This server is the RS
my $server = $rs{"$2:$3"};
$server = new RS($2, $3, $1) unless defined($server);
$rs{"$2:$3"} = $server;
$rsbyid{$1} = $server;
$server->add_domain($domainname);
# 2.5.x:
} elsif ($dn =~ m{^cn=Replication Server\s+([^, ]+)\s+([^, ]+)\s+([^, ]+),}i) {
# This server is an RS
# port hostname rsid
my $server = $rs{"$2:$1"};
$server = new RS($2, $1, $3) unless defined($server);
$rs{"$2:$1"} = $server;
$rsbyid{$3} = $server;
$server->add_domain($domainname);
# 2.6.0: cn=Connected replication server RS(...) hostname:port
} elsif ($dn =~ m{^cn=Connected replication server RS\((\d+)\) ([^:]+):(\d+),}i) {
# Another RS (not this one)
my $server = $rs{"$2:$3"};
$server = new RS($2, $3, $1) unless defined($server);
$rs{"$2:$3"} = $server;
$rsbyid{$1} = $server;
$server->add_domain($domainname);
# 2.5.x
} elsif ($dn =~ m{^cn=Connected Replication Server\s+([^:]+):(\d+)\s+([^,]+),}i) {
# This entry is another RS
# hostname port rsid
my $server = $rs{"$1:$2"};
$server = new RS($1, $2, $3) unless defined($server);
$rs{"$1:$2"} = $server;
$rsbyid{$3} = $server;
$server->add_domain($domainname);
# 2.6.0: cn=Directory server DS(...) hostname:port
} elsif ($dn =~ m{^cn=Directory Server DS\((\d+)\) ([^:]+):(\d+),}i) {
# 2.6.0 DS connected to that server
my $server = new DS("$2:$3", $1, $domainname);
$ds{"$2:$3"} = $server;
# 2.5.x
} elsif ($dn =~ m{^cn=Connected Replica ([^, ]+)\s+(\d+),}i) {
# This entry is a DS connected to this server
# hostname dsid
# no other server info
my $server = new DS($1, $2, $domainname);
$ds{"$1 $2"} = $server;
if ($connectedto =~ m{^Replication Server (\d+) (\d+)}) {
$server->set_rs($rsbyid{$2});
}
# 2.6.0: cn=Connected directory server DS(...) hostname:port
} elsif ($dn =~ m{^cn=Connected directory server DS\((\d+)\) ([^:]+):(\d+),}i) {
my $server = new DS("$2:$3", $1, $domainname);
$ds{"$2:$3"} = $server;
if ($connectedto =~ m{Connected replication server RS\((\d+)\) }) {
$server->set_rs($1);
} elsif ($connectedto =~ m{Replication Server (\d+) (\d+)}) {
$server->set_rs($2);
}
# 2.5.x
} elsif ($dn =~ m{^cn=Undirect Replica\s+([^,]+),}i) {
# This entry is a DS connected to another RS
# dsid
my $server = new DS("unknown", $1, $domainname);
$ds{"unknown $1"} = $server;
if ($connectedto =~ m{Connected Replication Server\s+([^, ]+)\s(\d+),}) {
$server->set_rs($rsbyid{$2});
}
}
}
}
# Post-process the DSes so they point to RS objects
# (DSes could be written before the RSes in the monitor output)
foreach (keys(%ds)) {
my $server = $ds{$_};
my $rsid = $server->get_rs();
my $rs = undef;
unless (ref($rsid)) {
$rs = $rsbyid{$rsid};
$server->set_rs($rs) if defined $rs;
}
}
print "// OpenDJ Replication topology\n";
print "digraph replication {\n";
my @rslinks;
foreach (sort(keys(%rs))) {
my $server = $rs{$_};
my $rsid = $server->get_id();
print " rs$rsid [shape=box,style=filled,fillcolor=$rsfill,fontcolor=$rstext]\n";
print " rs$rsid [label=<", html($server->get_name()), " (", html($server->get_id()), ")";
foreach my $domain (sort $server->get_domains()) {
print "<BR/>", html($domain);
}
print ">]\n";
print "\n";
push @rslinks, "rs$rsid";
}
# Fully meshed RSes
while (scalar(@rslinks) > 1) {
my $t = shift @rslinks;
foreach (@rslinks) {
print " $t -> $_ [color=$rsline]\n";
}
}
foreach (sort(keys(%ds))) {
my $server = $ds{$_};
my $dsid = $server->get_id();
print "\n";
print " ds$dsid [shape=box,style=filled,fillcolor=$dsfill,fontcolor=$dstext]\n";
print " ds$dsid [label=<", html($server->get_name()), " (", html($server->get_id()), ")<BR/>", html($server->get_domain()), ">]\n";
my $rsid = $server->get_rs()->get_id();
print " ds$dsid -> rs$rsid [color=$dsline]\n";
}
print "}\n";
exit 0;
__END__
=head1 NAME
topology2dot - display OpenDJ replication topology in GraphViz DOT format
=head1 SYNOPSIS
topology2dot [ignored-suffix [...]] < monitor.ldif
=head1 DESCRIPTION
The topology of a network of replicated OpenDJ servers can be determined by
analysis of a complete dump of the E<lt>cn=monitorE<gt> subtree.
This tool extracts as much useful information as possible and outputs a
graph in GraphViz DOT format. The replication servers are drawn in blue boxes,
and the directory servers are drawn in green boxes.
The first line of each label contains the hostname and administration port
number of the server (if available) and the server-id in parentheses. The
remaining lines list the suffixes being replicated.
=head1 NOTE
The format of the entries in the E<lt>cn=monitorE<gt> subtree is subject to
change. This tool has been tested with OpenDJ 2.5 and 2.6.0.
Full details of the connected directory servers are not currently available.
One directory server would typically have 3 or more separate server-ids, each
replicating a separate suffix.
GraphViz's circo tool is recommended for rendering the output.
=head1 SEE ALSO
L<http://opendj.forgerock.org>
=head1 AUTHOR
Chris Ridd E<lt>[email protected]<gt>
=head1 COPYRIGHT
Copyright (c) 2013-2017 Chris Ridd. All rights reserved. This tool is free
software; you can redistribute it and/or modify it under the same terms as Perl
itself.