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

  1. Go to the Agent installation page (https://app.datadoghq.com/account/settings#agent) and select the operating system you are using.
  2. Copy the command and execute it in your shell.
  3. Change to the conf.d directory 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
  4. Create a new config file for your new agent check. Name the file checkvalue.yaml.
  5. Edit the file to include the following:

    init_config:
    
    instances:
      [{}]
    
  6. Create a new check file in the checks.d directory. Name the file checkvalue.py.

  7. Edit the file to include the following:

    from checks import AgentCheck
    class HelloCheck(AgentCheck):
      def check(self, instance):
        self.gauge('hello.world', 1)
    
  8. Restart the agent. On the vagrant machine, run the following command: sudo /etc/init.d/datadog-agent restart

  9. Within 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.

  1. Replace the code in checkvalue.py with 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"])
    
  2. Restart the agent. Now run the info command. On the vagrant machine, the command is sudo /etc/init.d/datadog-agent info.

  3. 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.

  1. Update the checkvalue.yaml file as follows (replacing the ipaddress with your loadbalancer’s ip address):

    init_config:
    
    instances:
      - ipaddress: 1.2.3.4
    
  2. Update the checkvalue.py file 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"])
    
  3. Create a dashboard for this metric.

Presentation | Notes