2 # Generate a traffic model from a traffic summary file
4 # Copyright (C) Catalyst IT Ltd. 2017
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.
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.
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/>.
19 from __future__ import print_function
23 sys.path.insert(0, "bin/python")
24 from samba.emulate import traffic
26 from samba.logger import get_samba_logger
27 logger = get_samba_logger(name=__name__, level=20)
32 parser = argparse.ArgumentParser(description=__doc__,
33 formatter_class=argparse.RawDescriptionHelpFormatter)
34 parser.add_argument('-o', '--out',
35 help="write model here")
36 parser.add_argument('--dns-mode', choices=['inline', 'count'],
37 help='how to deal with DNS', default='count')
38 parser.add_argument('SUMMARY_FILE', nargs='*', type=argparse.FileType('r'),
40 help="read from this file (default STDIN)")
41 args = parser.parse_args()
44 error("No output file was specified to write the model to.")
45 error("Please specify a filename using the --out option.")
49 outfile = open(args.out, 'w')
51 error("could not open %s" % args.out)
55 if args.SUMMARY_FILE is sys.stdin:
56 info("reading from STDIN...")
61 dns_counts) = traffic.ingest_summaries(args.SUMMARY_FILE,
62 dns_mode=args.dns_mode)
64 model = traffic.TrafficModel()
65 info("learning model")
66 if args.dns_mode == 'count':
67 model.learn(conversations, dns_counts)
69 model.learn(conversations)