summaryrefslogtreecommitdiffstats
path: root/scripts/timerdb-info.py
diff options
context:
space:
mode:
authorJan Vcelak <jan.vcelak@nic.cz>2015-02-09 22:45:15 +0100
committerJan Vcelak <jan.vcelak@nic.cz>2015-02-09 22:46:12 +0100
commit7d9b10d92edbd42d3021c97358c5f1b4a533ce09 (patch)
treec02d6fd6106fa09f62f840ea42048a919ed90cbc /scripts/timerdb-info.py
parentMerge branch 'sane_addr_check' into 'master' (diff)
downloadknot-7d9b10d92edbd42d3021c97358c5f1b4a533ce09.tar.xz
knot-7d9b10d92edbd42d3021c97358c5f1b4a533ce09.zip
scripts: debug tool to dump zone timers database
Diffstat (limited to 'scripts/timerdb-info.py')
-rwxr-xr-xscripts/timerdb-info.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/scripts/timerdb-info.py b/scripts/timerdb-info.py
new file mode 100755
index 000000000..86110b950
--- /dev/null
+++ b/scripts/timerdb-info.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+# vim: et ts=4 sw=4 sts=4
+#
+# Dump content of zone timers database in user readable format.
+#
+
+import datetime
+import lmdb
+import struct
+import sys
+
+class TimerDBInfo:
+ def __init__(self, path):
+ self._path = path
+
+ # the order is significant
+ TIMERS = [ "refresh", "expire", "flush" ]
+
+ @classmethod
+ def parse_dname(cls, dname):
+ labels = []
+ while dname[0] != 0:
+ llen = dname[0]
+ label = dname[1:llen+1].decode("utf-8")
+ dname = dname[llen+1:]
+ labels.append(label)
+ return ".".join(labels)
+
+ @classmethod
+ def parse_timers(cls, binary):
+ timers = {}
+ while len(binary) > 0:
+ chunk = binary[:9]
+ binary = binary[9:]
+ id, value = struct.unpack("!BQ", chunk)
+ if id > 0 and id <= len(cls.TIMERS):
+ timers[id - 1] = value
+ return timers
+
+ @classmethod
+ def format_date(cls, timestamp):
+ if timestamp == 0:
+ return "never"
+ else:
+ return datetime.datetime.fromtimestamp(timestamp).isoformat()
+
+ @classmethod
+ def format_line(cls, zone, timers):
+ parts = [zone]
+ for id, name in enumerate(cls.TIMERS):
+ parts.append("%s %s" % (name, cls.format_date(timers.get(id, 0))))
+ return " | ".join(parts)
+
+ def run(self):
+ with lmdb.open(self._path, readonly=True) as db:
+ with db.begin() as txn:
+ cursor = txn.cursor()
+ for key, value in cursor:
+ zone = self.parse_dname(key)
+ timers = self.parse_timers(value)
+ print(self.format_line(zone, timers))
+
+if __name__ == "__main__":
+ if len(sys.argv) != 2:
+ print("usage: %s <timerdb-path>" % sys.argv[0], file=sys.stderr)
+ sys.exit(1)
+ path = sys.argv[1]
+ app = TimerDBInfo(path)
+ app.run()