Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
cumulative option on metrics output
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Oct 14, 2020
1 parent 6c297fe commit 3fa9159
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions dnstap_receiver/dnstap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion dnstap_receiver/output_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
logging.info(", ".join(msg))

# reset stats?
if not cfg["cumulative"]:
metrics.reset()
18 changes: 16 additions & 2 deletions dnstap_receiver/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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"""
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 3fa9159

Please sign in to comment.