From fd1dc3e95f444cd743bd816b7d69f5e0633536f6 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 15 Sep 2022 11:20:36 -0400 Subject: [PATCH] bin/djbdns-logparse.py: rework some of the tinydns logic. --- bin/djbdns-logparse.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/bin/djbdns-logparse.py b/bin/djbdns-logparse.py index 067d9b1..b407411 100755 --- a/bin/djbdns-logparse.py +++ b/bin/djbdns-logparse.py @@ -70,11 +70,15 @@ query_type = { # tinydns can drop a query for one of three reasons; this dictionary # maps the symbol that gets logged in each case to a human-readable -# reason. +# reason. We include the "+" case here, indicating that the query was +# NOT dropped, to avoid a special case later on when we're formatting +# the human-readable output. query_drop_reason = { + "+": None, "-": "no authority", "I": "invalid query", "C": "invalid class", + "/": "couldn't parse" } @@ -232,19 +236,22 @@ def handle_tinydns_log(line : str, match: re.Match): print(timestamp, end=' ') + reason = query_drop_reason[code] if code == "+": - print ("sent response to %s:%s (id %s): %s %s" - % (ip, port, id, type, name)) - elif code in ("-", "I", "C"): - reason = query_drop_reason[code] - print ("dropped query (%s) from %s:%s (id %s): %s %s" - % (reason, ip, port, id, type, name)) - elif code == "/": - print ("dropped query (couldn't parse) from %s:%s" - % (ip, port)) + line_tpl = "sent response to {ip}:{port} (id {id}): {type} {name}" else: - print ("%s from %s:%s (id %s): %s %s" - % (code, ip, port, id, type, name)) + line_tpl = "dropped query ({reason}) from {ip}:{port}" + if code != "/": + # If the query can actually be parsed, the log line is a + # bit more informative than it would have been otherwise. + line_tpl += " (id {id}): {type} {name}" + + print(line_tpl.format(reason=reason, + ip=ip, + port=port, + id=id, + type=type, + name=name)) def parse_logfile(file : typing.TextIO): -- 2.44.2