Skip to content

Commit

Permalink
[powerdns_recursor] restrict metrics collection
Browse files Browse the repository at this point in the history
This check now only collects metrics deemed useful.
It also sends a service check.

The configuration file is cleaned, and a few comments are added for
documentation purposes.
  • Loading branch information
degemer committed May 13, 2016
1 parent 33401f7 commit ca7a6aa
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 47 deletions.
41 changes: 0 additions & 41 deletions checks.d/powerdns-recursor.py

This file was deleted.

77 changes: 77 additions & 0 deletions checks.d/powerdns_recursor.py
@@ -0,0 +1,77 @@
# stdlib
from collections import namedtuple

# Datadog
from checks import AgentCheck

# 3p
import requests


class PowerDNSRecursorCheck(AgentCheck):
# See https://doc.powerdns.com/md/recursor/stats/ for metrics explanation
GAUGE_METRICS = [
'cache-entries',
'concurrent-queries',
]
RATE_METRICS = [
'all-outqueries',
'answers-slow',
'answers0-1',
'answers1-10',
'answers10-100',
'answers100-1000',
'cache-hits',
'cache-misses',
'noerror-answers',
'outgoing-timeouts',
'questions',
'servfail-answers',
'tcp-outqueries',
'tcp-questions',
]

SERVICE_CHECK_NAME = 'powerdns.recursor.can_connect'

def check(self, instance):
config, tags = self._get_config(instance)
stats = self._get_pdns_stats(config)
for stat in stats:
if stat['name'] in PowerDNSRecursorCheck.GAUGE_METRICS:
self.gauge('powerdns.recursor.{}'.format(stat['name']), float(stat['value']), tags=tags)
elif stat['name'] in PowerDNSRecursorCheck.RATE_METRICS:
self.rate('powerdns.recursor.{}'.format(stat['name']), float(stat['value']), tags=tags)

def _get_config(self, instance):
required = ['host', 'port', 'api_key']
for param in required:
if not instance.get(param):
raise Exception("powerdns_recursor instance missing %s. Skipping." % (param))

host = instance.get('host')
port = int(instance.get('port'))
api_key = instance.get('api_key')
tags = instance.get('tags', [])

Config = namedtuple('Config', [
'host',
'port',
'api_key']
)

return Config(host, port, api_key), tags

def _get_pdns_stats(self, config):
url = "http://{}:{}/servers/localhost/statistics".format(config.host, config.port)
service_check_tags = ['recursor_host:{}'.format(config.host), 'recursor_port:{}'.format(config.port)]
headers = {"X-API-Key": config.api_key}
try:
request = requests.get(url, headers=headers)
request.raise_for_status()
except Exception:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
tags=service_check_tags)
raise
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK,
tags=service_check_tags)
return request.json()
24 changes: 24 additions & 0 deletions conf.d/powerdns-recursor.yaml.example
@@ -0,0 +1,24 @@
init_config:

instances:
# The PowerDNS Recursor check retrieves metrics from the Recursor experimental
# web server.
# See https://doc.powerdns.com/3/recursor/settings/#experimental-webserver
# The API key has to be set as well:
# https://doc.powerdns.com/3/recursor/settings/#experimental-api-key
#
# This check works with PowerDNS Recursor 3.x.



# Host running the recursor.
- host: 127.0.0.1
# Recursor web server port.
port: 8082
# Recursor web server api key.
api_key: pdns_api_key

# Optional tags to be applied to every emitted metric.
# tags:
# - key:value
# - instance:production
6 changes: 0 additions & 6 deletions conf.d/powerdns-recursor.yaml.j2

This file was deleted.

0 comments on commit ca7a6aa

Please sign in to comment.