scripts: Scripts to replay and generate samba traffic
[metze/samba/wip.git] / script / traffic_learner
1 #!/usr/bin/env python
2 # Generate a traffic model from a traffic summary file
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
20 import sys
21 import argparse
22
23 sys.path.insert(0, "bin/python")
24 from samba.emulate import traffic
25
26
27 def main():
28     parser = argparse.ArgumentParser(description=__doc__,
29                         formatter_class=argparse.RawDescriptionHelpFormatter)
30     parser.add_argument('-o', '--out', type=argparse.FileType('w'),
31                         help="write model here")
32     parser.add_argument('--dns-mode', choices=['inline', 'count'],
33                         help='how to deal with DNS', default='count')
34     parser.add_argument('SUMMARY_FILE', nargs='*', type=argparse.FileType('r'),
35                         default=[sys.stdin],
36                         help="read from this file (default STDIN)")
37     args = parser.parse_args()
38
39     if not args.out:
40         print >> sys.stdout, "No output file was specified to write the model to."
41         print >> sys.stdout, "Please specify a filename using the --out option."
42         return
43
44     if args.SUMMARY_FILE is sys.stdin:
45         print >> sys.stderr, "reading from STDIN..."
46
47     (conversations,
48      interval,
49      duration,
50      dns_counts) = traffic.ingest_summaries(args.SUMMARY_FILE,
51                                             dns_mode=args.dns_mode)
52
53     model = traffic.TrafficModel()
54     print >> sys.stderr, "learning model"
55     if args.dns_mode == 'count':
56         model.learn(conversations, dns_counts)
57     else:
58         model.learn(conversations)
59
60     model.save(args.out)
61
62
63 main()