]> gitweb.michael.orlitzky.com - djbdns-logparse.git/commitdiff
bin/djbdns-logparse.py: rework some of the tinydns logic.
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 15 Sep 2022 15:20:36 +0000 (11:20 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Thu, 15 Sep 2022 15:20:36 +0000 (11:20 -0400)
bin/djbdns-logparse.py

index 067d9b17749cbc87b9b786dcb11f4054cb68ba79..b4074110956b807f187b56ead1b1677b11b17d37 100755 (executable)
@@ -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):