Building a Custom Agent Check (Hands On Instructions)
In this example we will create a metric that records a value generated by a custom application. The value generated is a count of the widgets created since the last request. The purpose of the exercise is to see how to submit a metric to Datadog.
So far, we have only used remote machines that you do not have direct access to. For this exercise we need to run a new agent. You can either install the agent on your local laptop, or install to a vagrant machine.
If you want to use Vagrant (and have Vagrant and VirtualBox installed), create a new directory and run the following command:
vagrant init ubuntu/trusty64; vagrant up --provider virtualbox;vagrant ssh
- Go to the Agent installation page (https://app.datadoghq.com/account/settings#agent) and select the operating system you are using.
- Copy the command and execute it in your shell.
- Change to the
conf.ddirectory on your system. On the vagrant machine, the directory is/etc/dd-agent/conf.d. To find it for other operating systems, review the documentation - Create a new config file for your new agent check. Name the file
checkvalue.yaml. Edit the file to include the following:
init_config: instances: [{}]Create a new check file in the checks.d directory. Name the file
checkvalue.py.Edit the file to include the following:
from checks import AgentCheck class HelloCheck(AgentCheck): def check(self, instance): self.gauge('hello.world', 1)Restart the agent. On the vagrant machine, run the following command:
sudo /etc/init.d/datadog-agent restartWithin a minute, you should see a new metric show up in the Metric Summary called
hello.world.
Now lets expand the script to be something a bit more useful.
Replace the code in
checkvalue.pywith the following (replacing the value of lburl with the address of your loadbalancer:import urllib2 import simplejson from checks import AgentCheck class CheckValue(AgentCheck): def check(self, instance): lburl = 1.2.3.4 response = urllib2.urlopen("http://" + lburl + "/rest") data = simplejson.load(response) self.gauge('coreapp.update.value', data["value"])Restart the agent. Now run the info command. On the vagrant machine, the command is
sudo /etc/init.d/datadog-agent info.If you have any issues with the agent, you can debug using the following command:
sudo -u dd-agent dd-agent check checkvalue
Now let’s make the configuration file a bit more useful.
Update the
checkvalue.yamlfile as follows (replacing theipaddresswith your loadbalancer’s ip address):init_config: instances: - ipaddress: 1.2.3.4Update the
checkvalue.pyfile to be as follows:import urllib2 import simplejson from checks import AgentCheck class CheckValue(AgentCheck): def check(self, instance): lburl = instance['ipaddress'] response = urllib2.urlopen("http://" + lburl + "/rest") data = simplejson.load(response) self.gauge('coreapp.update.value', data["value"])Create a dashboard for this metric.