Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Created March 17, 2017 17:13
Show Gist options
  • Save dgulinobw/a4a5a3efd8c51355158b4442b6ef1cc2 to your computer and use it in GitHub Desktop.
Save dgulinobw/a4a5a3efd8c51355158b4442b6ef1cc2 to your computer and use it in GitHub Desktop.
Report on cache utilization of dnsmasq
#!//usr/bin/env python
from __future__ import print_function
import sys
from collections import defaultdict
filename = sys.argv[1]
queries = defaultdict(lambda: defaultdict(float))
totals= defaultdict(float)
with open(filename) as f:
for line in f.readlines():
lsplit = line.split(" ")
if lsplit[3].startswith("dnsmasq"):
if lsplit[4] == "query[A]":
name = lsplit[5]
queries[name]["query"] += 1
totals["query"] += 1
elif lsplit[4] == "reply":
name = lsplit[5]
type = lsplit[7].strip()
if type == "<CNAME>":
type = "C"
else:
type = "A"
queries[name]["type"] = type
totals[type] += 1
elif lsplit[4] == "forwarded":
name = lsplit[5]
queries[name]["forwarded"] += 1
totals["forwarded"] += 1
elif lsplit[4] == "cached":
name = lsplit[5]
queries[name]["cached"] += 1
totals["cached"] += 1
print ("%50s | %9s | %9s | %9s | %9s | %9s" % ("name", "type", "queries", "forwarded", "cached", "%cached"))
keys = queries.keys()
keys.sort()
for key in keys:
value = queries[key]
if value["query"] != 0:
avg = (value["cached"]/value["query"])*100.0
else:
avg = 0.0
print ("%50s | % 9s | %9d | %9d | %9d | %9.2f" % (key, value["type"], value["query"], value["forwarded"], value["cached"], avg))
print ("")
print ("%50s | %9s | %9d | %9d | %9d | %9.2f" % ("totals", "", totals["query"], totals["forwarded"], totals["cached"], (totals["cached"]/totals["query"])*100.0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment