scripting: Make tdb_copy a common util function in samba.tdb_util
[Samba/gbeck.git] / source4 / scripting / bin / findprovisionusnranges
blobee9da3d421e13cef998da94910a98383b3e53fb1
1 #!/usr/bin/env python
3 # Helper for determining USN ranges created of modified by provision and
4 # upgradeprovision.
5 # Copyright (C) Matthieu Patou <mat@matws.net> 2009-2011
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import sys
23 import optparse
24 sys.path.insert(0, "bin/python")
26 from samba.credentials import DONT_USE_KERBEROS
27 from samba.auth import system_session
28 from samba import Ldb
29 import ldb
31 import samba.getopt as options
32 from samba import param
33 from samba.upgradehelpers import get_paths, print_provision_ranges, findprovisionrange
34 from samba.ndr import ndr_unpack
35 from samba.dcerpc import misc
37 parser = optparse.OptionParser("provision [options]")
38 sambaopts = options.SambaOptions(parser)
39 parser.add_option_group(sambaopts)
40 parser.add_option_group(options.VersionOptions(parser))
41 parser.add_option("--storedir", type="string", help="Directory where to store result files")
42 credopts = options.CredentialsOptions(parser)
43 parser.add_option_group(credopts)
44 opts = parser.parse_args()[0]
45 lp = sambaopts.get_loadparm()
46 smbconf = lp.configfile
48 creds = credopts.get_credentials(lp)
49 creds.set_kerberos_state(DONT_USE_KERBEROS)
50 session = system_session()
51 paths = get_paths(param, smbconf=smbconf)
52 basedn="DC=" + lp.get("realm").replace(".",",DC=")
53 samdb = Ldb(paths.samdb, session_info=session, credentials=creds,lp=lp)
55 res = samdb.search(base="", scope=ldb.SCOPE_BASE, attrs=["dsServiceName"])
57 invocation = None
58 if res and len(res) == 1 and res[0]["dsServiceName"] != None:
59 dn = ldb.Dn(samdb, str(res[0]["dsServiceName"]))
60 res = samdb.search(base=str(dn), scope=ldb.SCOPE_BASE, attrs=["invocationId"],
61 controls=["search_options:1:2"])
63 if res and len(res) == 1 and res[0]["invocationId"]:
64 invocation = str(ndr_unpack(misc.GUID, res[0]["invocationId"][0]))
65 else:
66 print "Unable to find invocation ID"
67 sys.exit(1)
68 else:
69 print "Unable to find attribute dsServiceName in rootDSE"
70 sys.exit(1)
72 minobj = 5
73 (hash_id, nb_obj) = findprovisionrange(samdb, basedn)
74 print "Here is a list of changes that modified more than %d objects in 1 minute." % minobj
75 print "Usually changes made by provision and upgradeprovision are those who affect a couple"\
76 " of hundred of objects or more"
77 print "Total number of objects: %d" % nb_obj
78 print
80 print_provision_ranges(hash_id, minobj, opts.storedir, str(paths.samdb), invocation)