[PATCH] CSV output for "samba-tool drs showrepl"

Flavio Stanchina flavio.stanchina at ies.it
Wed Oct 24 15:20:10 UTC 2018

This patch adds CSV output (option --csv) to "samba-tool drs showrepl".

The output is meant to be equivalent to:
   repadmin /showrepl /csv

We use this to monitor our Samba DCs with Check_MK; the AD replication 
plugin for Windows ("ad_replication.bat") sends CSV output and it was 
easy to add it to "drs showrepl".

For those interested, the Check_MK plugin for the Linux agent is simply:
   # ad_replication.sh
   # Needs patched samba-tool!
   echo '<<<ad_replication>>>'
   samba-tool drs showrepl --csv 2>/dev/null

The patch is against Samba version 4.6.7, currently used by Zentyal 5.1 
and Ubuntu artful. I know it's not the latest version; I can reapply it 
on a more recent version if needed, but you're probably better equipped 
than me to do that.

Hope you find it useful.

Flavio Stanchina
Informatica e Servizi
Trento - Italy

-------------- next part --------------
diff --git a/python/samba/netcmd/drs.py b/python/samba/netcmd/drs.py
index 67733ac..df793a4 100644
--- a/python/samba/netcmd/drs.py
+++ b/python/samba/netcmd/drs.py
@@ -21,6 +21,7 @@
 import samba.getopt as options
 import ldb
 import logging
+import time
 from samba.auth import system_session
 from samba.netcmd import (
@@ -30,7 +31,7 @@ from samba.netcmd import (
 from samba.samdb import SamDB
-from samba import drs_utils, nttime2string, dsdb
+from samba import drs_utils, nttime2string, nttime2unix, dsdb
 from samba.dcerpc import drsuapi, misc
 import common
 from samba.join import join_clone
@@ -94,6 +95,10 @@ class cmd_drs_showrepl(Command):
     takes_args = ["DC?"]
+    takes_options = [
+        Option("--csv", help="output CSV (compatible with repadmin /showrepl /csv)", action="store_true"),
+        ]
     def print_neighbour(self, n):
         '''print one set of neighbour information'''
         self.message("%s" % n.naming_context_dn)
@@ -120,7 +125,7 @@ class cmd_drs_showrepl(Command):
             raise CommandError("DsReplicaGetInfo of type %u failed" % info_type, e)
         return (info_type, info)
-    def run(self, DC=None, sambaopts=None,
+    def run(self, DC=None, csv=None, sambaopts=None,
             credopts=None, versionopts=None, server=None):
         self.lp = sambaopts.get_loadparm()
@@ -143,6 +148,28 @@ class cmd_drs_showrepl(Command):
             raise CommandError("Failed to search NTDS DN %s" % ntds_dn)
         conn = self.samdb.search(base=ntds_dn, expression="(objectClass=nTDSConnection)")
+        if csv:
+            self.show_repl_csv(DC, site, server, ntds, conn)
+        else:
+            self.show_repl(DC, site, server, ntds, conn)
+    def show_repl_csv(self, DC, site, server, ntds, conn):
+        self.message('showrepl_COLUMNS,Destination DSA Site,Destination DSA,Naming Context,Source DSA Site,Source DSA,Transport Type,Number of Failures,Last Failure Time,Last Success Time,Last Failure Status')
+        (info_type, info) = self.drsuapi_ReplicaInfo(drsuapi.DRSUAPI_DS_REPLICA_INFO_NEIGHBORS)
+        for n in info.array:
+            (src_site, src_server) = drs_parse_ntds_dn(n.source_dsa_obj_dn)
+            (ecode, estring) = n.result_last_attempt
+            if ecode == 0:
+                last_attempt_time = "0"
+            else:
+                last_attempt_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(nttime2unix(n.last_attempt)))
+            last_success_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(nttime2unix(n.last_success)))
+            self.message("showrepl_INFO,%s,%s,\"%s\",%s,%s,RPC,%u,%s,%s,%u" % (site, server, n.naming_context_dn, src_site, src_server, n.consecutive_sync_failures, last_attempt_time, last_success_time, ecode))
+    def show_repl(self, DC, site, server, ntds, conn):
         self.message("%s\\%s" % (site, server))
         self.message("DSA Options: 0x%08x" % int(attr_default(ntds[0], "options", 0)))
         self.message("DSA object GUID: %s" % self.samdb.schema_format_value("objectGUID", ntds[0]["objectGUID"][0]))

More information about the samba-technical mailing list