Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
AttributeError when custom agent check run by datadog agent
# output in /var/log/datadog/collector.log
2016-09-03 00:25:46 UTC | ERROR | dd.collector | checks.statsrelay_check( | Check 'statsrelay_check' instance #0 failed
Traceback (most recent call last):
File "/opt/datadog-agent/agent/checks/", line 750, in run
File "/etc/dd-agent/checks.d/", line 14, in check
metrics = fetch_metrics(host, port)
File "/etc/dd-agent/checks.d/", line 30, in fetch_metrics
File "/opt/datadog-agent/embedded/lib/python2.7/", line 283, in write
AttributeError: 'NoneType' object has no attribute 'sendall'
#!/usr/bin/env python
from telnetlib import Telnet
from checks import AgentCheck
CONFIG_FILE = '/etc/dd-agent/conf.d/statsrelay_check.yaml'
class StatsRelayCheck(AgentCheck):
def check(self, instance):
host = instance.get('host')
port = instance.get('port')
metrics = fetch_metrics(host, port)
prefix = self.init_config.get('prefix')
for k, v in metrics.iteritems():
if prefix:
metric_name = prefix + k
type_ = v[0]
value = v[-1]
getattr(self, type_)(metric=metric_name, value=value)
def fetch_metrics(host, port):
type_overrides = {
'boolean': 'gauge',
'timestamp': 'gauge'
metrics = dict()
t = Telnet(host, port, 5)
output = t.read_all()
for l in output.splitlines():
if l:
scope, metric, type_, value = l.rstrip().split()
if type_ in type_overrides.keys():
type_ = type_overrides.get(type_)
scope = scope.replace('-', '_').replace('.', '_').replace(':','_')
metric_name = '{}.{}.{}'.format(metric_prefix, scope, metric)
metric_val = value
metrics[metric_name] = [type_, value]
return metrics
if __name__ == '__main__':
check, instances = StatsRelayCheck.from_yaml(CONFIG_FILE)
for instance in instances:
print check.get_metrics()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment