traffic_relay: bulk port print to modern py3 style
[garming/samba-autobuild/.git] / script / traffic_replay
1 #!/usr/bin/env python
2 # Generates samba network traffic
3 #
4 # Copyright (C) Catalyst IT Ltd. 2017
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 from __future__ import print_function
20 import sys
21 import os
22 import optparse
23 import tempfile
24 import shutil
25
26 sys.path.insert(0, "bin/python")
27
28 from samba.emulate import traffic
29 import samba.getopt as options
30
31
32 def print_err(*args, **kwargs):
33     print(*args, file=sys.stderr, **kwargs)
34
35
36 def main():
37
38     desc = ("Generates network traffic 'conversations' based on <summary-file>"
39             " (which should the output file produced by either traffic_learner"
40             " or traffic_summary.pl). This traffic is sent to <dns-hostname>,"
41             " which is the full DNS hostname of the DC being tested.")
42
43     parser = optparse.OptionParser(
44         "%prog [--help|options] <summary-file> <dns-hostname>",
45         description=desc)
46
47     parser.add_option('--dns-rate', type='float', default=0,
48                       help='fire extra DNS packets at this rate')
49     parser.add_option('-B', '--badpassword-frequency',
50                       type='float', default=0.0,
51                       help='frequency of connections with bad passwords')
52     parser.add_option('-K', '--prefer-kerberos',
53                       action="store_true",
54                       help='prefer kerberos when authenticating test users')
55     parser.add_option('-I', '--instance-id', type='int', default=0,
56                       help='Instance number, when running multiple instances')
57     parser.add_option('-t', '--timing-data',
58                       help=('write individual message timing data here '
59                             '(- for stdout)'))
60     parser.add_option('--preserve-tempdir', default=False, action="store_true",
61                       help='do not delete temporary files')
62     parser.add_option('-F', '--fixed-password',
63                       type='string', default=None,
64                       help=('Password used for the test users created. '
65                             'Required'))
66     parser.add_option('-c', '--clean-up',
67                       action="store_true",
68                       help='Clean up the generated groups and user accounts')
69
70     model_group = optparse.OptionGroup(parser, 'Traffic Model Options',
71                                        'These options alter the traffic '
72                                        'generated when the summary-file is a '
73                                        'traffic-model (produced by '
74                                        'traffic_learner)')
75     model_group.add_option('-S', '--scale-traffic', type='float', default=1.0,
76                            help='Increase the number of conversations by '
77                            'this factor')
78     model_group.add_option('-D', '--duration', type='float', default=None,
79                            help=('Run model for this long (approx). '
80                                  'Default 60s for models'))
81     model_group.add_option('-r', '--replay-rate', type='float', default=1.0,
82                            help='Replay the traffic faster by this factor')
83     model_group.add_option('--traffic-summary',
84                            help=('Generate a traffic summary file and write '
85                                  'it here (- for stdout)'))
86     parser.add_option_group(model_group)
87
88     user_gen_group = optparse.OptionGroup(parser, 'Generate User Options',
89                                           "Add extra user/groups on the DC to "
90                                           "increase the DB size. These extra "
91                                           "users aren't used for traffic "
92                                           "generation.")
93     user_gen_group.add_option('-G', '--generate-users-only',
94                               action="store_true",
95                               help='Generate the users, but do not replay '
96                               'the traffic')
97     user_gen_group.add_option('-n', '--number-of-users', type='int', default=0,
98                               help='Total number of test users to create')
99     user_gen_group.add_option('--number-of-groups', type='int', default=0,
100                               help='Create this many groups')
101     user_gen_group.add_option('--average-groups-per-user',
102                               type='int', default=0,
103                               help='Assign the test users to this '
104                               'many groups on average')
105     user_gen_group.add_option('--group-memberships', type='int', default=0,
106                               help='Total memberships to assign across all '
107                               'test users and all groups')
108     parser.add_option_group(user_gen_group)
109
110     sambaopts = options.SambaOptions(parser)
111     parser.add_option_group(sambaopts)
112     parser.add_option_group(options.VersionOptions(parser))
113     credopts = options.CredentialsOptions(parser)
114     parser.add_option_group(credopts)
115
116     # the --no-password credential doesn't make sense for this tool
117     if parser.has_option('-N'):
118         parser.remove_option('-N')
119
120     opts, args = parser.parse_args()
121
122     # First ensure we have reasonable arguments
123
124     if len(args) == 1:
125         summary = None
126         host    = args[0]
127     elif len(args) == 2:
128         summary, host = args
129     else:
130         parser.print_usage()
131         return
132
133     if opts.clean_up:
134         print_err("Removing user and machine accounts")
135         lp    = sambaopts.get_loadparm()
136         creds = credopts.get_credentials(lp)
137         ldb   = traffic.openLdb(host, creds, lp)
138         traffic.clean_up_accounts(ldb, opts.instance_id)
139         exit(0)
140
141     if summary:
142         if not os.path.exists(summary):
143             print_err("Summary file %s doesn't exist" % summary)
144             sys.exit(1)
145     # the summary-file can be ommitted for --generate-users-only and
146     # --cleanup-up, but it should be specified in all other cases
147     elif not opts.generate_users_only:
148         print_err("No summary-file specified to replay traffic from")
149         sys.exit(1)
150
151     if not opts.fixed_password:
152         print_err(("Please use --fixed-password to specify a password"
153                              " for the users created as part of this test"))
154         sys.exit(1)
155
156     lp = sambaopts.get_loadparm()
157     creds = credopts.get_credentials(lp)
158
159     domain = opts.workgroup
160     if domain:
161         lp.set("workgroup", domain)
162     else:
163         domain = lp.get("workgroup")
164         if domain == "WORKGROUP":
165             print_err(("NETBIOS domain does not appear to be "
166                        "specified, use the --workgroup option"))
167             sys.exit(1)
168
169     if not opts.realm and not lp.get('realm'):
170         print_err("Realm not specified, use the --realm option")
171         sys.exit(1)
172
173     if opts.generate_users_only and not (opts.number_of_users or
174                                          opts.number_of_groups):
175         print_err(("Please specify the number of users and/or groups "
176                    "to generate."))
177         sys.exit(1)
178
179     if opts.group_memberships and opts.average_groups_per_user:
180         print_err(("--group-memberships and --average-groups-per-user"
181                    " are incompatible options - use one or the other"))
182         sys.exit(1)
183
184     if not opts.number_of_groups and opts.average_groups_per_user:
185         print_err(("--average-groups-per-user requires "
186                    "--number-of-groups"))
187         sys.exit(1)
188
189     if not opts.number_of_groups and opts.group_memberships:
190         print_err("--group-memberships requires --number-of-groups")
191         sys.exit(1)
192
193     if opts.timing_data not in ('-', None):
194         try:
195             open(opts.timing_data, 'w').close()
196         except IOError as e:
197             print_err(("the supplied timing data destination "
198                        "(%s) is not writable" % opts.timing_data))
199             print_err(e)
200             sys.exit()
201
202     if opts.traffic_summary not in ('-', None):
203         try:
204             open(opts.traffic_summary, 'w').close()
205         except IOError as e:
206             print_err(("the supplied traffic summary destination "
207                        "(%s) is not writable" % opts.traffic_summary))
208             print_err(e)
209             sys.exit()
210
211     traffic.DEBUG_LEVEL = opts.debuglevel
212
213     duration = opts.duration
214     if duration is None:
215         duration = 60.0
216
217     # ingest the model or traffic summary
218     if summary:
219         try:
220             conversations, interval, duration, dns_counts = \
221                                             traffic.ingest_summaries([summary])
222
223             print_err(("Using conversations from the traffic summary "
224                        "file specified"))
225
226             # honour the specified duration if it's different to the
227             # capture duration
228             if opts.duration is not None:
229                 duration = opts.duration
230
231         except ValueError as e:
232             if not e.message.startswith('need more than'):
233                 raise
234
235             model = traffic.TrafficModel()
236
237             try:
238                 model.load(summary)
239             except ValueError:
240                 print_err(("Could not parse %s. The summary file "
241                            "should be the output from either the "
242                            "traffic_summary.pl or "
243                            "traffic_learner scripts."
244                            % summary))
245                 sys.exit()
246
247             print_err(("Using the specified model file to "
248                        "generate conversations"))
249
250             conversations = model.generate_conversations(opts.scale_traffic,
251                                                          duration,
252                                                          opts.replay_rate)
253
254     else:
255         conversations = []
256
257     if opts.debuglevel > 5:
258         for c in conversations:
259             for p in c.packets:
260                 print("    ", p)
261
262         print('=' * 72)
263
264     if opts.number_of_users and opts.number_of_users < len(conversations):
265         print_err(("--number-of-users (%d) is less than the "
266                    "number of conversations to replay (%d)"
267                    % (opts.number_of_users, len(conversations))))
268         sys.exit(1)
269
270     number_of_users = max(opts.number_of_users, len(conversations))
271     max_memberships = number_of_users * opts.number_of_groups
272
273     if not opts.group_memberships and opts.average_groups_per_user:
274         opts.group_memberships = opts.average_groups_per_user * number_of_users
275         print_err(("Using %d group-memberships based on %u average "
276                    "memberships for %d users"
277                    % (opts.group_memberships,
278                       opts.average_groups_per_user, number_of_users)))
279
280     if opts.group_memberships > max_memberships:
281         print_err(("The group memberships specified (%d) exceeds "
282                    "the total users (%d) * total groups (%d)"
283                    % (opts.group_memberships, number_of_users,
284                       opts.number_of_groups)))
285         sys.exit(1)
286
287     try:
288         ldb = traffic.openLdb(host, creds, lp)
289     except:
290         print_err(("\nInitial LDAP connection failed! Did you supply "
291                    "a DNS host name and the correct credentials?"))
292         sys.exit(1)
293
294     if opts.generate_users_only:
295         traffic.generate_users_and_groups(ldb,
296                                           opts.instance_id,
297                                           opts.fixed_password,
298                                           opts.number_of_users,
299                                           opts.number_of_groups,
300                                           opts.group_memberships)
301         sys.exit()
302
303     tempdir = tempfile.mkdtemp(prefix="samba_tg_")
304     print_err("Using temp dir %s" % tempdir)
305
306     traffic.generate_users_and_groups(ldb,
307                                       opts.instance_id,
308                                       opts.fixed_password,
309                                       number_of_users,
310                                       opts.number_of_groups,
311                                       opts.group_memberships)
312
313     accounts = traffic.generate_replay_accounts(ldb,
314                                                 opts.instance_id,
315                                                 len(conversations),
316                                                 opts.fixed_password)
317
318     statsdir = traffic.mk_masked_dir(tempdir, 'stats')
319
320     if opts.traffic_summary:
321         if opts.traffic_summary == '-':
322             summary_dest = sys.stdout
323         else:
324             summary_dest = open(opts.traffic_summary, 'w')
325
326         print_err("Writing traffic summary")
327         summaries = []
328         for c in conversations:
329             summaries += c.replay_as_summary_lines()
330
331         summaries.sort()
332         for (time, line) in summaries:
333             print(line, file=summary_dest)
334
335         exit(0)
336
337     traffic.replay(conversations, host,
338                    lp=lp,
339                    creds=creds,
340                    accounts=accounts,
341                    dns_rate=opts.dns_rate,
342                    duration=duration,
343                    badpassword_frequency=opts.badpassword_frequency,
344                    prefer_kerberos=opts.prefer_kerberos,
345                    statsdir=statsdir,
346                    domain=domain,
347                    base_dn=ldb.domain_dn(),
348                    ou=traffic.ou_name(ldb, opts.instance_id),
349                    tempdir=tempdir,
350                    domain_sid=ldb.get_domain_sid())
351
352     if opts.timing_data == '-':
353         timing_dest = sys.stdout
354     elif opts.timing_data is None:
355         timing_dest = None
356     else:
357         timing_dest = open(opts.timing_data, 'w')
358
359     print_err("Generating statistics")
360     traffic.generate_stats(statsdir, timing_dest)
361
362     if not opts.preserve_tempdir:
363         print_err("Removing temporary directory")
364         shutil.rmtree(tempdir)
365
366
367 main()