diff --git a/pdns_redis.py b/pdns_redis.py index 3653116..f62bc7f 100755 --- a/pdns_redis.py +++ b/pdns_redis.py @@ -31,8 +31,8 @@ Flags: - -R Set the Redis back-end. - -W Set the Redis back-end for writes. + -R Set the Redis back-end (read only). + -W Set the Redis back-end for writes (query count updates and record alterations). -i Set the Redis DB ID (defaults to 0) -A Read a Redis password from the named file. -P Run as a PowerDNS pipe-backend. @@ -221,9 +221,15 @@ def __init__(self, redis_pdns, domain, record=None, data=None): self.domain = domain and domain.lower() or None self.record = record and record.upper() or None self.data = data + + def _readonly(self): + return self.redis_pdns.redis_write_host is None def BE(self): return self.redis_pdns.BE() + + def WBE(self): + return self.redis_pdns.WBE() def DSplit(self, domain, count=1024): return domain.split('.', count) @@ -239,13 +245,15 @@ def WildQuery(self, domain): def _Query(self, domain=None, wildcards=False): pdns_be = self.BE() + pdns_wbe = self.WBE() if not self._readonly() else None pdns_key = REDIS_PREFIX+(domain or self.domain) if self.record and self.data: key = "\t".join([self.record, self.data]) ttl = pdns_be.hget(pdns_key, key) if ttl is not None: - pdns_be.hincrby(pdns_key, 'TXT\tQC', 1) + if pdns_wbe is not None: + pdns_wbe.hincrby(pdns_key, 'TXT\tQC', 1) return [(self.domain, self.record, ttl, self.data)] elif wildcards: return self.WildQuery(domain or self.domain) @@ -273,7 +281,8 @@ def _Query(self, domain=None, wildcards=False): rv.append((self.domain, record, ddata[entry], data)) if rv: - pdns_be.hincrby(pdns_key, 'TXT\tQC', 1) + if pdns_wbe is not None: + pdns_wbe.hincrby(pdns_key, 'TXT\tQC', 1) return rv elif wildcards: return self.WildQuery(domain or self.domain) @@ -522,7 +531,7 @@ def Run(self): self.FlushLogBuffer() self.reply("LOG\tPowerDNS sent bad request: %s" % query) self.reply("FAIL") - except Exception, err: + except Exception as err: self.redis_pdns.Disconnect() self.FlushLogBuffer() self.reply("LOG\tInternal Error: %s" % err) @@ -666,8 +675,8 @@ def RunTasks(self): if __name__ == '__main__': try: pr = PdnsRedis().ParseArgs(sys.argv[1:]).RunTasks() - except ArgumentError, e: - print DOC - print 'Error: %s' % e + except ArgumentError as e: + print(DOC) + print('Error: %s' % e) sys.exit(1)