Skip to content

Instantly share code, notes, and snippets.

@valyagolev
Last active August 29, 2015 14:22
Show Gist options
  • Save valyagolev/37a4f69c09d685c9855e to your computer and use it in GitHub Desktop.
Save valyagolev/37a4f69c09d685c9855e to your computer and use it in GitHub Desktop.
datadog check for upstart, foreman-exported jobs
"""
Sends metrics about running Foreman-type jobs exported in Upstart
1. Put it into /etc/dd-agent/conf.d/datadog_upstart_check.py
2. Create /etc/dd-agent/conf.d/datadog_upstart_check.yaml
init_config:
instances:
- prefix: 'an-app'
- prefix: 'another-one'
"""
import sys
sys.path.append("/usr/local/lib/python2.7/dist-packages/")
import subprocess
import re
try:
from checks import AgentCheck
except:
print "Running in debug mode, not as a check!"
class AgentCheck(object):
def increment(self, name, value, **kwargs):
print name, value, kwargs
def get_processes():
for line in subprocess.check_output(["initctl", "list"]).strip().split('\n'):
match = re.match(r"^(?P<name>[a-zA-Z0-9_-]+) (?:\([a-zA-Z0-9_\/-]+\) )?(?P<goal>[a-z]+)/(?P<state>[a-z]+)(?:, process (?P<pid>\d+))?", line)
if not match:
print 'no match', line
continue
yield match.groupdict()
class UpstartCheck(AgentCheck):
def check(self, instance):
prefix = instance['prefix']
for proc in get_processes():
if not proc['name'].startswith(prefix):
continue
pieces = proc['name'].split('-')
if not pieces[-1].isdigit():
continue
job_type = proc['name'][len(prefix)+1:-len(pieces[-1])-1]
self.increment('upstart.job', 1, tags=['app:' + prefix, 'type:' + job_type, 'goal:' + proc['goal'], 'state:' + proc['state']])
if __name__ == '__main__':
UpstartCheck().check('prefix')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment