Commit 1c98c78a authored by Tomas Krizek's avatar Tomas Krizek

cli refactoring, use report.json from envdir

parent f5df878d
from argparse import ArgumentParser, Namespace
import logging
import os
import cfg
LOGGING_LEVEL = logging.DEBUG
CONFIG_FILENAME = 'respdiff.cfg'
REPORT_FILENAME = 'report.json'
def setup_logging(level: int = LOGGING_LEVEL) -> None:
logging.basicConfig(format='%(asctime)s %(levelname)8s %(message)s', level=level)
def add_arg_config(parser: ArgumentParser) -> None:
# parser.add_argument('-c', '--config', default=CONFIG_FILENAME, dest='cfgpath',
# help='config file (default: {})'.format(CONFIG_FILENAME))
parser.add_argument('-c', '--config', type=cfg.read_cfg,
default=CONFIG_FILENAME, dest='cfg',
help='config file (default: {})'.format(CONFIG_FILENAME))
def add_arg_envdir(parser: ArgumentParser) -> None:
parser.add_argument('envdir', type=str,
help='LMDB environment to read/write queries, answers and diffs')
def add_arg_datafile(parser: ArgumentParser) -> None:
parser.add_argument('-d', '--datafile',
help='JSON report file (default: <envdir>/{})'.format(
REPORT_FILENAME))
def get_datafile(args: Namespace) -> str:
if args.datafile is None:
return os.path.join(args.envdir, REPORT_FILENAME)
return args.datafile
......@@ -6,7 +6,7 @@ import subprocess
from typing import Any, Mapping
import sys
import cfg
import cli
from dbhelper import LMDB
import diffsum
from dataformat import Diff, DiffReport, ReproData, ResolverID
......@@ -37,24 +37,22 @@ def get_restart_scripts(config: Mapping[str, Any]) -> Mapping[ResolverID, str]:
def main():
logging.basicConfig(format='%(levelname)s %(message)s', level=logging.INFO)
cli.setup_logging()
parser = argparse.ArgumentParser(
description='attempt to reproduce original diffs from JSON report')
parser.add_argument('-d', '--datafile', default='report.json',
help='JSON report file (default: report.json)')
parser.add_argument('-c', '--config', default='respdiff.cfg', dest='cfgpath',
help='config file (default: respdiff.cfg)')
parser.add_argument('envdir', type=str,
help='LMDB environment to read queries and answers from')
cli.add_arg_envdir(parser)
cli.add_arg_config(parser)
cli.add_arg_datafile(parser)
args = parser.parse_args()
config = cfg.read_cfg(args.cfgpath)
report = DiffReport.from_json(args.datafile)
criteria = config['diff']['criteria']
timeout = config['sendrecv']['timeout']
selector, sockets = sendrecv.sock_init(get_resolvers(config))
restart_scripts = get_restart_scripts(config)
if len(sockets) < len(config['servers']['names']):
datafile = cli.get_datafile(args)
report = DiffReport.from_json(datafile)
criteria = args.cfg['diff']['criteria']
timeout = args.cfg['sendrecv']['timeout']
selector, sockets = sendrecv.sock_init(get_resolvers(args.cfg))
restart_scripts = get_restart_scripts(args.cfg)
if len(sockets) < len(args.cfg['servers']['names']):
logging.critical("Couldn't create sockets for all resolvers.")
sys.exit(1)
......@@ -78,7 +76,8 @@ def main():
wire_blobs, _ = sendrecv.send_recv_parallel(qwire, selector, sockets, timeout)
answers = msgdiff.decode_wire_dict(wire_blobs)
others_agree, mismatches = msgdiff.compare(answers, criteria, config['diff']['target'])
others_agree, mismatches = msgdiff.compare(
answers, criteria, args.cfg['diff']['target'])
reprocounter.retries += 1
if others_agree:
......@@ -86,7 +85,7 @@ def main():
if diff == Diff(diff.qid, mismatches):
reprocounter.verified += 1
report.export_json(args.datafile)
report.export_json(datafile)
if __name__ == '__main__':
......
......@@ -10,7 +10,7 @@ import dns.message
import dns.rdatatype
from tabulate import tabulate
import cfg
import cli
from dbhelper import LMDB, qid2key
from dataformat import (
DataMismatch, DiffReport, FieldLabel, Summary,
......@@ -135,23 +135,21 @@ def get_query_iterator(
def main():
logging.basicConfig(format='%(levelname)s %(message)s', level=logging.DEBUG)
cli.setup_logging()
parser = argparse.ArgumentParser(
description='read queries from LMDB, send them in parallel to servers '
'listed in configuration file, and record answers into LMDB')
parser.add_argument('-d', '--datafile', default='report.json',
help='JSON report file (default: report.json)')
parser.add_argument('-c', '--config', default='respdiff.cfg', dest='cfgpath',
help='config file (default: respdiff.cfg)')
description='create a summary report from gathered data stored in LMDB '
'and JSON datafile')
cli.add_arg_envdir(parser)
cli.add_arg_config(parser)
cli.add_arg_datafile(parser)
parser.add_argument('-l', '--limit', type=int, default=DEFAULT_LIMIT,
help='number of displayed mismatches in fields (default: {}; '
'use 0 to display all)'.format(DEFAULT_LIMIT))
parser.add_argument('envdir', type=str,
help='LMDB environment to read queries and answers from')
args = parser.parse_args()
config = cfg.read_cfg(args.cfgpath)
report = DiffReport.from_json(args.datafile)
field_weights = config['report']['field_weights']
datafile = cli.get_datafile(args)
report = DiffReport.from_json(datafile)
field_weights = args.cfg['report']['field_weights']
if not report.total_answers:
logging.error('No answers in DB!')
......@@ -160,7 +158,7 @@ def main():
logging.error('JSON report is missing diff data! Did you forget to run msgdiff?')
sys.exit(1)
report = DiffReport.from_json(args.datafile)
report = DiffReport.from_json(datafile)
report.summary = Summary.from_report(report, field_weights)
print_global_stats(report)
......@@ -184,7 +182,7 @@ def main():
queries = get_query_iterator(lmdb, qids)
print_mismatch_queries(field, mismatch, queries, args.limit)
report.export_json(args.datafile)
report.export_json(datafile)
if __name__ == '__main__':
......
......@@ -12,7 +12,7 @@ from typing import Any, Dict, Iterator, Mapping, Optional, Sequence, Tuple # no
import dns.message
import dns.exception
import cfg
import cli
from dataformat import (
DataMismatch, DiffReport, Disagreements, DisagreementsCounter, FieldLabel, MismatchValue,
Reply, ResolverID, QID)
......@@ -256,24 +256,21 @@ def export_json(filename):
def main():
global lmdb
logging.basicConfig(format='%(levelname)s %(message)s', level=logging.DEBUG)
cli.setup_logging()
parser = argparse.ArgumentParser(
description='compute diff from answers stored in LMDB and write diffs to LMDB')
parser.add_argument('-d', '--datafile', type=str, default='report.json',
help='JSON report file (default: report.json)')
parser.add_argument('-c', '--config', default='respdiff.cfg', dest='cfgpath',
help='config file (default: respdiff.cfg)')
parser.add_argument('envdir', type=str,
help='LMDB environment to read answers from and to write diffs to')
args = parser.parse_args()
config = cfg.read_cfg(args.cfgpath)
cli.add_arg_envdir(parser)
cli.add_arg_config(parser)
cli.add_arg_datafile(parser)
criteria = config['diff']['criteria']
target = config['diff']['target']
args = parser.parse_args()
datafile = cli.get_datafile(args)
criteria = args.cfg['diff']['criteria']
target = args.cfg['diff']['target']
# JSON report has to be created by orchestrator
if not os.path.exists(args.datafile):
logging.error("JSON report (%s) doesn't exist!", args.datafile)
if not os.path.exists(datafile):
logging.error("JSON report (%s) doesn't exist!", datafile)
sys.exit(1)
with LMDB(args.envdir, fast=True) as lmdb_:
......@@ -285,7 +282,7 @@ def main():
with pool.Pool() as p:
for _ in p.imap_unordered(func, qid_stream, chunksize=10):
pass
export_json(args.datafile)
export_json(datafile)
if __name__ == '__main__':
......
......@@ -11,7 +11,7 @@ import time
from typing import List, Tuple, Dict, Any, Mapping, Sequence # noqa: type hints
import sys
import cfg
import cli
from dataformat import DiffReport, ResolverID
from dbhelper import LMDB
import sendrecv
......@@ -125,21 +125,18 @@ def main():
global time_delay_min
global time_delay_max
logging.basicConfig(format='%(levelname)s %(message)s', level=logging.INFO)
cli.setup_logging()
parser = argparse.ArgumentParser(
description='read queries from LMDB, send them in parallel to servers '
'listed in configuration file, and record answers into LMDB')
parser.add_argument('-c', '--config', type=cfg.read_cfg, default='respdiff.cfg', dest='cfg',
help='config file (default: respdiff.cfg)')
cli.add_arg_envdir(parser)
cli.add_arg_config(parser)
cli.add_arg_datafile(parser)
parser.add_argument('--ignore-timeout', action="store_true",
help='continue despite consecutive timeouts from resolvers')
parser.add_argument('-d', '--datafile', type=str, default='report.json',
help='JSON report file (default: report.json)')
parser.add_argument('envdir', type=str,
help='LMDB environment to read queries from and to write answers to')
args = parser.parse_args()
args = parser.parse_args()
datafile = cli.get_datafile(args)
resolvers = get_resolvers(args.cfg)
ignore_timeout = args.ignore_timeout
timeout = args.cfg['sendrecv']['timeout']
......@@ -177,7 +174,7 @@ def main():
txn.commit()
# get query/answer statistics
export_statistics(lmdb, args.datafile, start_time)
export_statistics(lmdb, datafile, start_time)
if __name__ == "__main__":
......
......@@ -13,6 +13,7 @@ import dns.message
import dns.rdatatype
import blacklist
import cli
from dbhelper import LMDB, qid2key
REPORT_CHUNKS = 10000
......@@ -109,19 +110,19 @@ def wire_from_text(text):
def main():
logging.basicConfig(format='%(levelname)s %(message)s', level=logging.DEBUG)
cli.setup_logging()
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description='Convert queries data from standard input and store '
'wire format into LMDB "queries" DB.')
parser.add_argument('envdir', type=str, help='path where to create LMDB environment')
cli.add_arg_envdir(parser)
parser.add_argument('-f', '--in-format', type=str, choices=['text', 'pcap'], default='text',
help='define format for input data, default value is text\n'
'Expected input for "text" is: "<qname> <RR type>", '
'one query per line.\n'
'Expected input for "pcap" is content of the pcap file.')
parser.add_argument('--pcap-file', type=argparse.FileType('rb'))
args = parser.parse_args()
if args.in_format == 'text' and args.pcap_file:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment