Skip to content

Commit

Permalink
KCC: improve log legibility with colour; make more dot graphs
Browse files Browse the repository at this point in the history
To see the colours in less, use -R.

  bin/samba_kcc --debug  -H whatever/sam.ldb | less -R

Signed-off-by: Douglas Bagnall <[email protected]>
Reviewed-by: Garming Sam <[email protected]>
Reviewed-by: Andrew Bartlett <[email protected]>
  • Loading branch information
douglasbagnall authored and abartlet committed May 28, 2015
1 parent 7375abe commit cee3f52
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
33 changes: 27 additions & 6 deletions python/samba/kcc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
from samba.common import dsdb_Dn
from samba.ndr import (ndr_unpack, ndr_pack)

#colours for prettier logs
C_NORMAL = "\033[00m"
DARK_RED = "\033[00;31m"
RED = "\033[01;31m"
DARK_GREEN = "\033[00;32m"
GREEN = "\033[01;32m"
YELLOW = "\033[01;33m"
DARK_YELLOW = "\033[00;33m"
DARK_BLUE = "\033[00;34m"
BLUE = "\033[01;34m"
PURPLE = "\033[00;35m"
MAGENTA = "\033[01;35m"
DARK_CYAN = "\033[00;36m"
CYAN = "\033[01;36m"
GREY = "\033[00;37m"
WHITE = "\033[01;37m"
REV_RED = "\033[01;41m"

class KCCError(Exception):
pass

Expand All @@ -55,7 +73,7 @@ def __init__(self, nc_dnstr):

def __str__(self):
'''Debug dump string output of class'''
text = "%s:" % self.__class__.__name__
text = "%s%s%s:" % (CYAN, self.__class__.__name__, C_NORMAL)
text = text + "\n\tnc_dnstr=%s" % self.nc_dnstr
text = text + "\n\tnc_guid=%s" % str(self.nc_guid)

Expand Down Expand Up @@ -2328,17 +2346,20 @@ def combine_repl_info(info_a, info_b, info_c):

return True

def write_dot_file(basename, edge_list, label=None, destdir=None):
def write_dot_file(basename, edge_list, label=None, destdir=None, reformat_labels=True, directed=False):
from tempfile import NamedTemporaryFile
if label:
basename += '_' + label.translate(None, ', ') #fix DN, guid labels
f = NamedTemporaryFile(suffix='.dot', prefix=basename + '_', delete=False, dir=destdir)
graphname = ''.join(x for x in basename if x.isalnum())
print >>f, 'graph %s {' % graphname
print >>f, 'label="%s";\nfontsize=20' % (label or graphname)
print >>f, '%s %s {' % ('digraph' if directed else 'graph', graphname)
print >>f, 'label="%s";\nfontsize=20;' % (label or graphname)
for a, b in edge_list:
print >>f, '"%s" -- "%s"' % (a, b)
if reformat_labels:
a = a.replace(',', '\\n')
b = b.replace(',', '\\n')
line = '->' if directed else '--'
print >>f, '"%s" %s "%s";' % (a, line, b)
print >>f, '}'


f.close()
57 changes: 52 additions & 5 deletions source4/scripting/bin/samba_kcc
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ class KCC(object):
if mydsa.is_ro() or opts.readonly:
for dnstr, connect in mydsa.connect_table.items():
if connect.to_be_deleted:
logger.info("TO BE DELETED:\n%s" % connect)
DEBUG_GREEN("TO BE DELETED:\n%s" % connect)
if connect.to_be_added:
logger.info("TO BE ADDED:\n%s" % connect)
DEBUG_GREEN("TO BE ADDED:\n%s" % connect)

# Peform deletion from our tables but perform
# no database modification
Expand Down Expand Up @@ -1460,7 +1460,7 @@ class KCC(object):
for t_guid, transport in self.transport_table.items():
if transport.name != 'IP':
continue
logger.debug(t_guid)
DEBUG_RED(t_guid)
# FLAG_CR_NTDS_DOMAIN 0x00000002
if (vertex.is_red() and transport.name != "IP" and
vertex.part.system_flags & 0x00000002):
Expand Down Expand Up @@ -1871,7 +1871,7 @@ class KCC(object):
# partition (NC x) then continue
needed, ro, partial = nc_x.should_be_present(dc_local)

logger.debug("construct_intrasite_graph(): enter" +
DEBUG_YELLOW("construct_intrasite_graph(): enter" +
"\n\tgc_only=%d" % gc_only +
"\n\tdetect_stale=%d" % detect_stale +
"\n\tneeded=%s" % needed +
Expand All @@ -1880,6 +1880,8 @@ class KCC(object):
"\n%s" % nc_x)

if not needed:
DEBUG_RED("%s lacks 'should be present' status, aborting construct_intersite_graph!" %
nc_x.nc_dnstr)
return

# Create a NCReplica that matches what the local replica
Expand Down Expand Up @@ -2233,6 +2235,44 @@ class KCC(object):
self.load_all_transports()
self.load_all_sitelinks()

if True:
dot_edges = []
for site in self.site_table.values():
for dsa in site.dsa_table.values():
for con in dsa.connect_table.values():
dot_edges.append((dsa.dsa_dnstr, con.from_dnstr))

write_dot_file('dsa_initial', dot_edges, directed=True)


dot_edges = []
for site in self.site_table.values():
for dsa in site.dsa_table.values():
c_rep = get_dsa_config_rep(dsa)
c_rep.load_repsFrom(self.samdb)
for x in c_rep.rep_repsFrom:
#print dir(x)
dot_edges.append((c_rep.rep_dsa_dnstr, x.nc_dnstr))

write_dot_file('config_repsFrom_initial', dot_edges, directed=True)

dot_edges = []
for site in self.site_table.values():
for dsa in site.dsa_table.values():
for x in dsa.current_rep_table:
dot_edges.append((dsa.dsa_dnstr, x))

write_dot_file('dsa_repsFrom_initial', dot_edges, directed=True)

dot_edges = []
for link in self.sitelink_table.values():
for a, b in itertools.combinations(link.site_list, 2):
dot_edges.append((str(a), str(b)))

write_dot_file('dsa_sitelink_initial', dot_edges, directed=False)



# These are the published steps (in order) for the
# MS-TECH description of the KCC algorithm ([MS-ADTS] 6.2.2)

Expand Down Expand Up @@ -2263,7 +2303,7 @@ class KCC(object):
for con in dsa.connect_table.values():
dot_edges.append((dsa.dsa_dnstr, con.from_dnstr))

write_dot_file('dsa_final', dot_edges)
write_dot_file('dsa_final', dot_edges, directed=True)
except:
raise

Expand Down Expand Up @@ -2954,6 +2994,13 @@ logger = logging.getLogger("samba_kcc")
logger.addHandler(logging.StreamHandler(sys.stdout))
DEBUG = logger.debug

def DEBUG_RED(*args):
DEBUG('%s%s%s' % (RED, args[0], C_NORMAL), *args[1:])
def DEBUG_GREEN(*args):
DEBUG('%s%s%s' % (GREEN, args[0], C_NORMAL), *args[1:])
def DEBUG_YELLOW(*args):
DEBUG('%s%s%s' % (YELLOW, args[0], C_NORMAL), *args[1:])

lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp, fallback_machine=True)

Expand Down

0 comments on commit cee3f52

Please sign in to comment.