From 3fa91591b598d6bdf452cd9346743b8917a84ae1 Mon Sep 17 00:00:00 2001 From: dmachard Date: Wed, 14 Oct 2020 08:19:02 +0200 Subject: [PATCH] cumulative option on metrics output --- README.md | 24 +++++++++++++----------- dnstap_receiver/dnstap.conf | 6 ++++-- dnstap_receiver/output_metrics.py | 8 +++++++- dnstap_receiver/receiver.py | 18 ++++++++++++++++-- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 73a0e1d..1e6bfa8 100644 --- a/README.md +++ b/README.md @@ -239,14 +239,16 @@ output: # enable or disable enable: true # print every N seconds. - interval: 5 + interval: 300 + # cumulative statistics, without clearing them after printing + cumulative: false ``` -Example of output on syslog server +Example of output ``` -596 QUERIES, 0.6 QPS, 5 CLIENTS, 596 IP4, 0 IP6, 596 UDP, 0 TCP, 161 NOERROR, 435 NXDOMAIN, 587 A, 9 AAAA -614 QUERIES, 3.6 QPS, 5 CLIENTS, 614 IP4, 0 IP6, 614 UDP, 0 TCP, 164 NOERROR, 450 NXDOMAIN, 605 A, 9 AAAA +2020-10-13 05:19:35,522 18 QUERIES, 3.6 QPS, 1 CLIENTS, 18 IP4, 0 IP6, +18 UDP, 0 TCP, 17 NOERROR, 1 NXDOMAIN, 18 A, 0 AAAA ``` ## More options @@ -561,12 +563,12 @@ input(type="imtcp" port="514") ### Logstash -vim /etc/logstash/conf.d/00-dnstap.conf +Edit the file /etc/logstash/conf.d/00-dnstap.conf ``` input { tcp { - port => 8192 + port => 6000 codec => json } } @@ -586,19 +588,19 @@ output { } ``` -## Systemd service file configuration +## Systemd service file -System service file for CentOS: +Systemd service file -```bash -vim /etc/systemd/system/dnstap_receiver.service +Create the file /etc/systemd/system/dnstap_receiver.service +```bash [Unit] Description=Python DNS tap Service After=network.target [Service] -ExecStart=/usr/local/bin/dnstap_receiver -u /etc/dnsdist/dnstap.sock -f 10.0.0.2:8192 +ExecStart=/usr/local/bin/dnstap_receiver -c /etc/dnstap_receiver/dnstap.conf Restart=on-abort Type=simple User=root diff --git a/dnstap_receiver/dnstap.conf b/dnstap_receiver/dnstap.conf index 59be591..49cdb48 100644 --- a/dnstap_receiver/dnstap.conf +++ b/dnstap_receiver/dnstap.conf @@ -45,8 +45,10 @@ output: metrics: # enable or disable enable: false - # print every N seconds. - interval: 5 + # print every N seconds + interval: 300 + # cumulative statistics, without clearing them after printing + cumulative: false # forward to remote tcp destination tcp-socket: diff --git a/dnstap_receiver/output_metrics.py b/dnstap_receiver/output_metrics.py index 4ad17a7..7f35989 100644 --- a/dnstap_receiver/output_metrics.py +++ b/dnstap_receiver/output_metrics.py @@ -8,6 +8,8 @@ async def handle(cfg, metrics): while True: await asyncio.sleep(cfg["interval"]) + if not cfg["cumulative"]: + queries_prev = 0 queries_cur = metrics.stats["total-queries"] qps = (queries_cur - queries_prev ) / cfg["interval"] queries_prev = queries_cur @@ -29,4 +31,8 @@ async def handle(cfg, metrics): msg.append( "%s AAAA" % metrics.rtype.get("AAAA", 0) ) # print to stdout - logging.info(", ".join(msg)) \ No newline at end of file + logging.info(", ".join(msg)) + + # reset stats? + if not cfg["cumulative"]: + metrics.reset() \ No newline at end of file diff --git a/dnstap_receiver/receiver.py b/dnstap_receiver/receiver.py index 5b1c5ba..84f94cc 100644 --- a/dnstap_receiver/receiver.py +++ b/dnstap_receiver/receiver.py @@ -198,8 +198,8 @@ async def cb_onconnect(reader, writer, cfg, queue, metrics): logging.debug(f'Input handler: {peername} - closed') class Metrics: - def __init__(self): - """metrics class""" + def prepare(self): + """prepare stats""" self.stats = {"total-queries": 0} self.queries = {} self.rtype = {} @@ -208,6 +208,19 @@ def __init__(self): self.nxdomains = {} self.proto = {} self.family = {} + + def reset(self): + """reset statistics""" + del self.stats + del self.queries + del self.rtype + del self.rcode + del self.clients + del self.nxdomains + del self.proto + del self.family + + self.prepare() def record_dnstap(self, dnstap): """add dnstap message""" @@ -289,6 +302,7 @@ def start_receiver(): # prepare output queue = asyncio.Queue() metrics = Metrics() + metrics.prepare() if cfg["output"]["syslog"]["enable"]: logging.debug("Output handler: syslog")