traffic_replay: --old-scale to mimic the old traffic_replay
[garming/samba-autobuild/.git] / script / traffic_replay
1 #!/usr/bin/env python3
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 import random
26
27 sys.path.insert(0, "bin/python")
28
29 from samba import gensec, get_debug_level
30 from samba.emulate import traffic
31 import samba.getopt as options
32 from samba.logger import get_samba_logger
33 from samba.samdb import SamDB
34 from samba.auth import system_session
35
36
37 def print_err(*args, **kwargs):
38     print(*args, file=sys.stderr, **kwargs)
39
40
41 def main():
42
43     desc = ("Generates network traffic 'conversations' based on a model generated"
44             " by script/traffic_learner. This traffic is sent to <dns-hostname>,"
45             " which is the full DNS hostname of the DC being tested.")
46
47     parser = optparse.OptionParser(
48         "%prog [--help|options] <model-file> <dns-hostname>",
49         description=desc)
50
51     parser.add_option('--dns-rate', type='float', default=0,
52                       help='fire extra DNS packets at this rate')
53     parser.add_option('-B', '--badpassword-frequency',
54                       type='float', default=0.0,
55                       help='frequency of connections with bad passwords')
56     parser.add_option('-K', '--prefer-kerberos',
57                       action="store_true",
58                       help='prefer kerberos when authenticating test users')
59     parser.add_option('-I', '--instance-id', type='int', default=0,
60                       help='Instance number, when running multiple instances')
61     parser.add_option('-t', '--timing-data',
62                       help=('write individual message timing data here '
63                             '(- for stdout)'))
64     parser.add_option('--preserve-tempdir', default=False, action="store_true",
65                       help='do not delete temporary files')
66     parser.add_option('-F', '--fixed-password',
67                       type='string', default=None,
68                       help=('Password used for the test users created. '
69                             'Required'))
70     parser.add_option('-c', '--clean-up',
71                       action="store_true",
72                       help='Clean up the generated groups and user accounts')
73     parser.add_option('--random-seed', type='int', default=None,
74                       help='Use to keep randomness consistent across multiple runs')
75     parser.add_option('--stop-on-any-error',
76                       action="store_true",
77                       help='abort the whole thing if a child fails')
78     model_group = optparse.OptionGroup(parser, 'Traffic Model Options',
79                                        'These options alter the traffic '
80                                        'generated by the model')
81     model_group.add_option('-S', '--scale-traffic', type='float', default=1.0,
82                            help='Increase the number of conversations by '
83                            'this factor')
84     parser.add_option('--old-scale',
85                       action="store_true",
86                       help='emulate the old scale for traffic')
87     model_group.add_option('-D', '--duration', type='float', default=60.0,
88                            help=('Run model for this long (approx). '
89                                  'Default 60s for models'))
90     model_group.add_option('--latency-timeout', type='float', default=None,
91                            help=('Wait this long for last packet to finish'))
92     model_group.add_option('-r', '--replay-rate', type='float', default=1.0,
93                            help='Replay the traffic faster by this factor')
94     model_group.add_option('--conversation-persistence', type='float',
95                            default=0.0,
96                            help=('chance (0 to 1) that a conversation waits '
97                                  'when it would have died'))
98     model_group.add_option('--traffic-summary',
99                            help=('Generate a traffic summary file and write '
100                                  'it here (- for stdout)'))
101     parser.add_option_group(model_group)
102
103     user_gen_group = optparse.OptionGroup(parser, 'Generate User Options',
104                                           "Add extra user/groups on the DC to "
105                                           "increase the DB size. These extra "
106                                           "users aren't used for traffic "
107                                           "generation.")
108     user_gen_group.add_option('-G', '--generate-users-only',
109                               action="store_true",
110                               help='Generate the users, but do not replay '
111                               'the traffic')
112     user_gen_group.add_option('-n', '--number-of-users', type='int', default=0,
113                               help='Total number of test users to create')
114     user_gen_group.add_option('--number-of-groups', type='int', default=0,
115                               help='Create this many groups')
116     user_gen_group.add_option('--average-groups-per-user',
117                               type='int', default=0,
118                               help='Assign the test users to this '
119                               'many groups on average')
120     user_gen_group.add_option('--group-memberships', type='int', default=0,
121                               help='Total memberships to assign across all '
122                               'test users and all groups')
123     user_gen_group.add_option('--max-members', type='int', default=None,
124                               help='Max users to add to any one group')
125     parser.add_option_group(user_gen_group)
126
127     sambaopts = options.SambaOptions(parser)
128     parser.add_option_group(sambaopts)
129     parser.add_option_group(options.VersionOptions(parser))
130     credopts = options.CredentialsOptions(parser)
131     parser.add_option_group(credopts)
132
133     # the --no-password credential doesn't make sense for this tool
134     if parser.has_option('-N'):
135         parser.remove_option('-N')
136
137     opts, args = parser.parse_args()
138
139     # First ensure we have reasonable arguments
140
141     if len(args) == 1:
142         model_file = None
143         host    = args[0]
144     elif len(args) == 2:
145         model_file, host = args
146     else:
147         parser.print_usage()
148         return
149
150     lp = sambaopts.get_loadparm()
151     debuglevel = get_debug_level()
152     logger = get_samba_logger(name=__name__,
153                               verbose=debuglevel > 3,
154                               quiet=debuglevel < 1)
155
156     traffic.DEBUG_LEVEL = debuglevel
157     # pass log level down to traffic module to make sure level is controlled
158     traffic.LOGGER.setLevel(logger.getEffectiveLevel())
159
160     if opts.clean_up:
161         logger.info("Removing user and machine accounts")
162         lp    = sambaopts.get_loadparm()
163         creds = credopts.get_credentials(lp)
164         creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
165         ldb   = traffic.openLdb(host, creds, lp)
166         traffic.clean_up_accounts(ldb, opts.instance_id)
167         exit(0)
168
169     if model_file:
170         if not os.path.exists(model_file):
171             logger.error("Model file %s doesn't exist" % model_file)
172             sys.exit(1)
173     # the model-file can be ommitted for --generate-users-only and
174     # --cleanup-up, but it should be specified in all other cases
175     elif not opts.generate_users_only:
176         logger.error("No model file specified to replay traffic from")
177         sys.exit(1)
178
179     if not opts.fixed_password:
180         logger.error(("Please use --fixed-password to specify a password"
181                       " for the users created as part of this test"))
182         sys.exit(1)
183
184     if opts.random_seed is not None:
185         random.seed(opts.random_seed)
186
187     creds = credopts.get_credentials(lp)
188     creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
189
190     domain = creds.get_domain()
191     if domain:
192         lp.set("workgroup", domain)
193     else:
194         domain = lp.get("workgroup")
195         if domain == "WORKGROUP":
196             logger.error(("NETBIOS domain does not appear to be "
197                           "specified, use the --workgroup option"))
198             sys.exit(1)
199
200     if not opts.realm and not lp.get('realm'):
201         logger.error("Realm not specified, use the --realm option")
202         sys.exit(1)
203
204     if opts.generate_users_only and not (opts.number_of_users or
205                                          opts.number_of_groups):
206         logger.error(("Please specify the number of users and/or groups "
207                       "to generate."))
208         sys.exit(1)
209
210     if opts.group_memberships and opts.average_groups_per_user:
211         logger.error(("--group-memberships and --average-groups-per-user"
212                       " are incompatible options - use one or the other"))
213         sys.exit(1)
214
215     if not opts.number_of_groups and opts.average_groups_per_user:
216         logger.error(("--average-groups-per-user requires "
217                       "--number-of-groups"))
218         sys.exit(1)
219
220     if opts.number_of_groups and opts.average_groups_per_user:
221         if opts.number_of_groups < opts.average_groups_per_user:
222             logger.error(("--average-groups-per-user can not be more than "
223                           "--number-of-groups"))
224             sys.exit(1)
225
226     if not opts.number_of_groups and opts.group_memberships:
227         logger.error("--group-memberships requires --number-of-groups")
228         sys.exit(1)
229
230     if opts.timing_data not in ('-', None):
231         try:
232             open(opts.timing_data, 'w').close()
233         except IOError:
234             # exception info will be added to log automatically
235             logger.exception(("the supplied timing data destination "
236                               "(%s) is not writable" % opts.timing_data))
237             sys.exit()
238
239     if opts.traffic_summary not in ('-', None):
240         try:
241             open(opts.traffic_summary, 'w').close()
242         except IOError:
243             # exception info will be added to log automatically
244             if debuglevel > 0:
245                 import traceback
246                 traceback.print_exc()
247             logger.exception(("the supplied traffic summary destination "
248                               "(%s) is not writable" % opts.traffic_summary))
249             sys.exit()
250
251     if opts.old_scale:
252         # we used to use a silly calculation based on the number
253         # of conversations; now we use the number of packets and
254         # scale traffic accurately. To roughly compare with older
255         # numbers you use --old-scale which approximates as follows:
256         opts.scale_traffic *= 0.55
257
258     # ingest the model
259     if model_file and not opts.generate_users_only:
260         model = traffic.TrafficModel()
261         try:
262             model.load(model_file)
263         except ValueError:
264             if debuglevel > 0:
265                 import traceback
266                 traceback.print_exc()
267             logger.error(("Could not parse %s, which does not seem to be "
268                           "a model generated by script/traffic_learner."
269                           % model_file))
270             sys.exit(1)
271
272         logger.info(("Using the specified model file to "
273                      "generate conversations"))
274
275         conversations = \
276             model.generate_conversation_sequences(
277                 opts.scale_traffic,
278                 opts.duration,
279                 opts.replay_rate,
280                 opts.conversation_persistence)
281     else:
282         conversations = []
283
284     if opts.number_of_users and opts.number_of_users < len(conversations):
285         logger.error(("--number-of-users (%d) is less than the "
286                       "number of conversations to replay (%d)"
287                      % (opts.number_of_users, len(conversations))))
288         sys.exit(1)
289
290     number_of_users = max(opts.number_of_users, len(conversations))
291     max_memberships = number_of_users * opts.number_of_groups
292
293     if not opts.group_memberships and opts.average_groups_per_user:
294         opts.group_memberships = opts.average_groups_per_user * number_of_users
295         logger.info(("Using %d group-memberships based on %u average "
296                      "memberships for %d users"
297                      % (opts.group_memberships,
298                         opts.average_groups_per_user, number_of_users)))
299
300     if opts.group_memberships > max_memberships:
301         logger.error(("The group memberships specified (%d) exceeds "
302                       "the total users (%d) * total groups (%d)"
303                       % (opts.group_memberships, number_of_users,
304                          opts.number_of_groups)))
305         sys.exit(1)
306
307     # Get an LDB connection.
308     try:
309         # if we're only adding users, then it's OK to pass a sam.ldb filepath
310         # as the host, which creates the users much faster. In all other cases
311         # we should be connecting to a remote DC
312         if opts.generate_users_only and os.path.isfile(host):
313             ldb = SamDB(url="ldb://{0}".format(host),
314                         session_info=system_session(), lp=lp)
315         else:
316             ldb = traffic.openLdb(host, creds, lp)
317     except:
318         logger.error(("\nInitial LDAP connection failed! Did you supply "
319                       "a DNS host name and the correct credentials?"))
320         sys.exit(1)
321
322     if opts.generate_users_only:
323         # generate computer accounts for added realism. Assume there will be
324         # some overhang with more computer accounts than users
325         computer_accounts = int(1.25 * number_of_users)
326         traffic.generate_users_and_groups(ldb,
327                                           opts.instance_id,
328                                           opts.fixed_password,
329                                           opts.number_of_users,
330                                           opts.number_of_groups,
331                                           opts.group_memberships,
332                                           opts.max_members,
333                                           machine_accounts=computer_accounts,
334                                           traffic_accounts=False)
335         sys.exit()
336
337     tempdir = tempfile.mkdtemp(prefix="samba_tg_")
338     logger.info("Using temp dir %s" % tempdir)
339
340     traffic.generate_users_and_groups(ldb,
341                                       opts.instance_id,
342                                       opts.fixed_password,
343                                       number_of_users,
344                                       opts.number_of_groups,
345                                       opts.group_memberships,
346                                       opts.max_members,
347                                       machine_accounts=len(conversations),
348                                       traffic_accounts=True)
349
350     accounts = traffic.generate_replay_accounts(ldb,
351                                                 opts.instance_id,
352                                                 len(conversations),
353                                                 opts.fixed_password)
354
355     statsdir = traffic.mk_masked_dir(tempdir, 'stats')
356
357     if opts.traffic_summary:
358         if opts.traffic_summary == '-':
359             summary_dest = sys.stdout
360         else:
361             summary_dest = open(opts.traffic_summary, 'w')
362
363         logger.info("Writing traffic summary")
364         summaries = []
365         for c in traffic.seq_to_conversations(conversations):
366             summaries += c.replay_as_summary_lines()
367
368         summaries.sort()
369         for (time, line) in summaries:
370             print(line, file=summary_dest)
371
372         exit(0)
373
374     traffic.replay(conversations,
375                    host,
376                    lp=lp,
377                    creds=creds,
378                    accounts=accounts,
379                    dns_rate=opts.dns_rate,
380                    duration=opts.duration,
381                    latency_timeout=opts.latency_timeout,
382                    badpassword_frequency=opts.badpassword_frequency,
383                    prefer_kerberos=opts.prefer_kerberos,
384                    statsdir=statsdir,
385                    domain=domain,
386                    base_dn=ldb.domain_dn(),
387                    ou=traffic.ou_name(ldb, opts.instance_id),
388                    tempdir=tempdir,
389                    stop_on_any_error=opts.stop_on_any_error,
390                    domain_sid=ldb.get_domain_sid())
391
392     if opts.timing_data == '-':
393         timing_dest = sys.stdout
394     elif opts.timing_data is None:
395         timing_dest = None
396     else:
397         timing_dest = open(opts.timing_data, 'w')
398
399     logger.info("Generating statistics")
400     traffic.generate_stats(statsdir, timing_dest)
401
402     if not opts.preserve_tempdir:
403         logger.info("Removing temporary directory")
404         shutil.rmtree(tempdir)
405     else:
406         # delete the empty directories anyway. There are thousands of
407         # them and they're EMPTY.
408         for d in os.listdir(tempdir):
409             if d.startswith('conversation-'):
410                 path = os.path.join(tempdir, d)
411                 try:
412                     os.rmdir(path)
413                 except OSError as e:
414                     logger.info("not removing %s (%s)" % (path, e))
415
416 main()