]>
gitweb.michael.orlitzky.com - djbdns-logparse.git/blob - bin/djbdns-logparse
3 Convert tinydns and dnscache logs to human-readable form
7 from struct
import pack
8 from time
import strftime
, gmtime
9 from subprocess
import Popen
, PIPE
12 # common components of line-matching regexes
13 timestamp_pat
= r
'[\d-]+ [\d:\.]+' # output of tai64nlocal
14 hex4_pat
= r
'[0-9a-f]{4}'
15 ip_pat
= r
'[0-9a-f]{8,32}' # IPv4 or IPv6 addresses in hex
17 # discriminate between dnscache and tinydns log lines
18 tinydns_log_re
= re
.compile(
19 r
'(%s) (%s):(%s):(%s) ([\+\-IC/]) (%s) (.*)'
20 % (timestamp_pat
, ip_pat
, hex4_pat
, hex4_pat
, hex4_pat
))
21 dnscache_log_re
= re
.compile(r
'(%s) (\w+)(.*)' % timestamp_pat
)
50 """Convert a hex string representing an IP address to conventional
51 human-readable form, ie. dotted-quad decimal for IPv4, and
52 8 colon-separated hex shorts for IPv6.
55 # IPv4, eg. "7f000001" -> "127.0.0.1"
56 return "%d.%d.%d.%d" % tuple(pack(">L", int(ip
, 16)))
58 # IPv6 is actually simpler -- it's just a string-slicing operation,
59 # eg. "00000000000000000000ffff7f000001" ->
60 # "0000:0000:0000:0000:0000:ffff:7f00:0001"
61 return ":".join([ip
[(4*i
) : (4*i
+4)] for i
in range(8)])
65 return convert_ip(match
.group(1))
68 return ":" + str(int(match
.group(1), 16))
70 def decode_client(words
, i
):
71 chunks
= words
[i
].split(":")
72 if len(chunks
) == 2: # ip:port
73 words
[i
] = "%s:%d" % (convert_ip(chunks
[0]), int(chunks
[1], 16))
74 elif len(chunks
) == 3:
75 words
[i
] = "%s:%d (id %d)" % (convert_ip(chunks
[0]),
79 def decode_ip(words
, i
):
80 words
[i
] = convert_ip(words
[i
])
82 def decode_ttl(words
, i
):
83 words
[i
] = "TTL=%s" % words
[i
]
85 def decode_serial(words
, i
):
86 serial
= int(words
[i
])
87 words
[i
] = "#%d" % serial
89 def decode_type(words
, i
):
91 words
[i
] = query_type
.get(int(qt
), qt
)
93 def handle_dnscache_log(line
, match
):
94 (timestamp
, event
, data
) = match
.groups()
98 if words
[0] not in ("cname", "ns", "nxdomain"):
101 elif event
== "drop":
102 decode_serial(words
, 0)
104 elif event
== "lame":
107 elif event
== "nodata":
110 decode_type(words
, 2)
112 elif event
== "nxdomain":
116 elif event
== "query":
117 decode_serial(words
, 0)
118 decode_client(words
, 1)
119 decode_type(words
, 2)
124 if words
[2] not in ("cname", "mx", "ns", "ptr", "soa"):
125 decode_type(words
, 2)
126 if words
[2] == "a": # decode answer to an A query
128 if words
[2] == "txt": # text record
130 if response
.endswith("..."):
132 response
= response
[0:-3]
135 length
= int(response
[0:2], 16)
137 for i
in range(1, len(response
)/2):
138 chars
.append(chr(int(response
[2*i
: (2*i
)+2], 16)))
139 words
[4] = "%d:\"%s%s\"" % (length
, "".join(chars
), ellipsis
)
141 elif event
== "sent":
142 decode_serial(words
, 0)
144 elif event
== "stats":
145 words
[0] = "count=%s" % words
[0]
146 words
[1] = "motion=%s" % words
[1]
147 words
[2] = "udp-active=%s" % words
[2]
148 words
[3] = "tcp-active=%s" % words
[3]
151 words
[0] = "g=%s" % words
[0]
152 decode_type(words
, 1)
154 # words[3] = control (domain for which these servers are believed
155 # to be authoritative)
156 for i
in range(4, len(words
)):
159 elif event
in ("tcpopen", "tcpclose"):
160 decode_client(words
, 0)
162 print(timestamp
, event
, " ".join(words
))
165 def handle_tinydns_log(line
, match
):
166 (timestamp
, ip
, port
, id, code
, type, name
) = match
.groups()
170 type = int(type, 16) # "001c" -> 28
171 type = query_type
.get(type, type) # 28 -> "aaaa"
173 print(timestamp
, end
=' ')
176 print ("sent response to %s:%s (id %s): %s %s"
177 % (ip
, port
, id, type, name
))
178 elif code
in ("-", "I", "C"):
179 reason
= query_drop_reason
[code
]
180 print ("dropped query (%s) from %s:%s (id %s): %s %s"
181 % (reason
, ip
, port
, id, type, name
))
183 print ("dropped query (couldn't parse) from %s:%s"
186 print ("%s from %s:%s (id %s): %s %s"
187 % (code
, ip
, port
, id, type, name
))
190 def parse_logfile(file):
191 # Open pipe to tai64nlocal: we will write lines of our input (the
192 # raw log file) to it, and read log lines with readable timestamps
194 tai
= Popen(["tai64nlocal"], stdin
=PIPE
, stdout
=PIPE
, text
=True, bufsize
=0)
197 tai
.stdin
.write(line
)
198 line
= tai
.stdout
.readline()
200 match
= tinydns_log_re
.match(line
)
202 handle_tinydns_log(line
, match
)
205 match
= dnscache_log_re
.match(line
)
207 handle_dnscache_log(line
, match
)
213 # Create an argument parser using the file's docsctring as its
215 from argparse
import ArgumentParser
, FileType
216 parser
= ArgumentParser(description
= __doc__
)
218 # Parse zero or more positional arguments into a list of
219 # "logfiles". If none are given, read from stdin instead.
220 from sys
import stdin
221 parser
.add_argument("logfiles",
226 help="djbdns logfile to process (default: stdin)")
228 args
= parser
.parse_args()
229 for f
in args
.logfiles
:
235 if __name__
== "__main__":