2018-12-12 03:18:40 +03:00
#!/usr/bin/env python3
2017-06-29 02:08:37 +03:00
# Generate a traffic model from a traffic summary file
#
# Copyright (C) Catalyst IT Ltd. 2017
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import argparse
sys.path.insert(0, "bin/python")
from samba.emulate import traffic
2018-10-19 06:37:08 +03:00
from samba.logger import get_samba_logger
logger = get_samba_logger(name=__name__, level=20)
error = logger.error
info = logger.info
2017-06-29 02:08:37 +03:00
def main():
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
2018-10-19 06:39:36 +03:00
parser.add_argument('-o', '--out',
2017-06-29 02:08:37 +03:00
help="write model here")
parser.add_argument('--dns-mode', choices=['inline', 'count'],
help='how to deal with DNS', default='count')
parser.add_argument('SUMMARY_FILE', nargs='*', type=argparse.FileType('r'),
default=[sys.stdin],
help="read from this file (default STDIN)")
args = parser.parse_args()
2018-10-24 03:46:16 +03:00
if args.out is None:
2018-10-19 06:37:08 +03:00
error("No output file was specified to write the model to.")
error("Please specify a filename using the --out option.")
2018-10-24 03:46:16 +03:00
return 1
2017-06-29 02:08:37 +03:00
2018-10-19 06:39:36 +03:00
try:
outfile = open(args.out, 'w')
except IOError as e:
error("could not open %s" % args.out)
error(e)
return 1
2017-06-29 02:08:37 +03:00
if args.SUMMARY_FILE is sys.stdin:
2018-10-19 06:37:08 +03:00
info("reading from STDIN...")
2017-06-29 02:08:37 +03:00
(conversations,
interval,
duration,
dns_counts) = traffic.ingest_summaries(args.SUMMARY_FILE,
dns_mode=args.dns_mode)
model = traffic.TrafficModel()
2018-10-19 06:37:08 +03:00
info("learning model")
2017-06-29 02:08:37 +03:00
if args.dns_mode == 'count':
model.learn(conversations, dns_counts)
else:
model.learn(conversations)
model.save(args.out)
2018-10-24 03:46:16 +03:00
sys.exit(main())