Skip to content

Instantly share code, notes, and snippets.

@mschurenko
Last active September 3, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschurenko/f965ee1dd203fab1d0deb484015bf674 to your computer and use it in GitHub Desktop.
Save mschurenko/f965ee1dd203fab1d0deb484015bf674 to your computer and use it in GitHub Desktop.
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(__init__.py:767) | Check 'statsrelay_check' instance #0 failed
Traceback (most recent call last):
File "/opt/datadog-agent/agent/checks/__init__.py", line 750, in run
self.check(copy.deepcopy(instance))
File "/etc/dd-agent/checks.d/statsrelay_check.py", line 14, in check
metrics = fetch_metrics(host, port)
File "/etc/dd-agent/checks.d/statsrelay_check.py", line 30, in fetch_metrics
t.write('status\n\n')
File "/opt/datadog-agent/embedded/lib/python2.7/telnetlib.py", line 283, in write
self.sock.sendall(buffer)
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)
t.write('status\n\n')
output = t.read_all()
t.close()
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:
check.check(instance)
print check.get_metrics()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment