Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[powerdns_recursor] restrict metrics collection
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
Showing
4 changed files
with
101 additions
and
47 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.