Debian Codegraph @

script/traffic_learner

raw
1 #!/usr/bin/env python3
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 import sys
20 import argparse
21
22 sys.path.insert(0, "bin/python")
23 from samba.emulate import traffic
24
25 from samba.logger import get_samba_logger
26 logger = get_samba_logger(name=__name__, level=20)
27 error = logger.error
28 info = logger.info
29
30 def main():
31 parser = argparse.ArgumentParser(description=__doc__,
32 formatter_class=argparse.RawDescriptionHelpFormatter)
33 parser.add_argument('-o', '--out',
34 help="write model here")
35 parser.add_argument('--dns-mode', choices=['inline', 'count'],
36 help='how to deal with DNS', default='count')
37 parser.add_argument('SUMMARY_FILE', nargs='*', type=argparse.FileType('r'),
38 default=[sys.stdin],
39 help="read from this file (default STDIN)")
40 args = parser.parse_args()
41
42 if args.out is None:
43 error("No output file was specified to write the model to.")
44 error("Please specify a filename using the --out option.")
45 return 1
46
47 try:
48 outfile = open(args.out, 'w')
49 except IOError as e:
50 error("could not open %s" % args.out)
51 error(e)
52 return 1
53
54 if args.SUMMARY_FILE is sys.stdin:
55 info("reading from STDIN...")
56
57 (conversations,
58 interval,
59 duration,
60 dns_counts) = traffic.ingest_summaries(args.SUMMARY_FILE,
61 dns_mode=args.dns_mode)
62
63 model = traffic.TrafficModel()
64 info("learning model")
65 if args.dns_mode == 'count':
66 model.learn(conversations, dns_counts)
67 else:
68 model.learn(conversations)
69
70 model.save(args.out)
71
72 sys.exit(main())