Using Datadog to Monitor your hosts/services

Pablo Rocha Moreira
3 min readOct 7, 2018

--

Setting up the environment (Mac OS X)

Run the command below in your terminal in order to install Datadog Agent on your system. Replace {YOUR API KEY GOES HERE}with your API key.


DD_API_KEY={YOUR API KEY GOES HERE} bash -c “$(curl -L https://raw.githubusercontent.com/DataDog/datadog-agent/master/cmd/agent/install_mac_os.sh)"
Installing Datadog Agent on Mac OS X

Click here for more details.

Collecting Metrics

Set up Datadog Agent and add tags to host

In order to assign tags to your Datadog Agent. follow the steps below:

  1. Edit the file ~/.datadog-agent/datadog.yaml, and add the following to it.
tags:
— role:database
— env:prod

After adding the lines above, your config file should look like this.

~/.datadog-agent/datadog.yaml after edited

These lines add two tags to the Agent, role:database and env:prod.

2. Restart the Agent.

3. Navigate to https://app.datadoghq.com/infrastructure/map in order to see your host map. Click one of your hosts to see more details about, i.e., running services, tags, etc.

First host listed on the dashboard.

How to install a Datadog integration (PostgreSQL)

  1. Open your terminal and run the following command to install PostgreSQL.
brew install postgresql

2. After installing postgres, run the following commands to create a read-only user for datadog in the database.

create user datadog with password ‘ZIIg1Upoe9x5KhLrhfRLjx66’;
grant SELECT ON pg_stat_database to datadog;
psql -h localhost -U datadog postgres -c “select * from pg_stat_database LIMIT(1);” && \
echo -e “\e[0;32mPostgres connection — OK\e[0m” || \
echo -e “\e[0;31mCannot connect to Postgres\e[0m”
```

When prompted for a password, enter: ZIIg1Upoe9x5KhLrhfRLjx66 or any other password you have picked.

3. Configure the Agent to connect to the PostgreSQL server.
Edit ~/.datadog-agent/conf.d/postgres.d/conf.yaml and add the lines below to it.

init_config:instances:
— host: localhost
port: 5432
username: datadog
password: ZIIg1Upoe9x5KhLrhfRLjx66
tags:
— postgresql

4. Restart the Agent.

5. Go to datadog settings and install the PostgreSQL integration.

Click the button install and follow instructions.

After you installed the integration you will have access to it on the dashboard.

Click here for more details.

How to create custom metrics: my_metric sends a metric with a random value between 0 and 1000.

You need to create a script file and a config file in order to create a custom metric and send it to datadog servers through the Agent.

1. Create ~/.datadog-agent/checks.d/mymetric.py and add the following to the file.


import random
from checks import AgentCheck
class RandomCheck(AgentCheck):
def check(self, instance):
self.gauge('my_metric', random.randint(0, 1000))

2. Create ~/.datadog-agent/conf.d/mymetric.yaml and add the code below.


init_config:
instances:
[{}]

3. Restart the Agent.

On your Datadog dashboard, go to Metrics > Explorer, and search for your custom metric.

The Agent is going to run the collector in intervals of 15–20 seconds.

How to change your check’s collection interval so that it only submits the metric once every 45 seconds.

1. Edit the config file ~/.datadog-agent/conf.d/mymetric.yaml and change the min_collection_interval globally to the interval you want the agent to collect data.


init_config:
min_collection_interval: 45
instances:
[{}]

2. Restart the Agent.

--

--