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