Collecting Metrics

Check Status of Agent

  1. Check status of Datadog Agent
    vagrant@vagrant:~$ sudo service datadog-agent status
    
    _images/datadog_agent_status.gif
  2. Check to see if there is activity being displayed in a web-dashboard

    _images/system_metrics_web_dashboard.png

Adding Host Tags

There are several ways to setup tags within the Datadog Platform. I decided to set them up via the agent configuration files as guided in the challenge’s instructions.

Note

You can assign tags through the configuration files, the Datadog site (UI), Datadog’s API, and DogStatsD

  1. Open the Agent’s main configuration file

    vagrant@vagrant:~$ sudo vim /etc/datadog-agent/datadog.yaml
    

_images/datadog.yaml_config_file.gif

Note

Make sure the API key looks correct and that the agent is pointing to the correct Datadog site

  1. I created a custom hostname in this file and set it to: hostname:BMELLO

  2. I then added the host tags in the same configuration file

    _images/datadog_tags.png

Note

Make sure you have the correct indentation in the datadog.yaml (configuration) file. I ran into this issue and the Datadog Agent would not restart.

  1. Restart Datadog Agent: vagrant@vagrant:~$ sudo service datadog-agent restart

  2. You should now see your host and its’ associated tags on the Host Map page in Datadog

    _images/datadog_host_map_with_tags.png
_images/tags.png

Database Installation (MySQL)

  1. Install MySQL Server on Ubuntu VM

    vagrant@vagrant:~$ sudo apt install mysql-server
    
_images/install_mysql.gif

  1. Configure MySQL

    • Run the included security script for MySQL as this will change some of the less secure default options for things like remote root logins and sample users. Even though this is a test environment, we do not want it becoming vulnerable running unsecured services.

    vagrant@vagrant:~$ sudo mysql_secure_installation
    
_images/mysql_secure.png

Note

As you can see in the image above, I left the test database in place so I have data to query when instrumenting with Datadog.

  1. Check Status of MySQL to make sure it is up and running

    vagrant@vagrant:~$ sudo service mysql status
    
_images/mysql_status.gif

Add user to MySQL for Datadog

  1. Change to MySQL shell

vagrant@vagrant:~$ mysql -u root -p
_images/mysql_shell.gif

  1. Add Datadog user

mysql > CREATE USER 'datadog'@'%' IDENTIFIED BY '<UNIQUEPASSWORD>';
_images/create_user_mysql.png

Note

Check to see what version of MySQL you are running as the scripts are different for adding a user to Datadog

_images/mysql_version.png

  1. Test to make sure user was added successfuly

vagrant@vagrant:~$ mysql -u datadog --password=<UNIQUEPASSWORD> -e "show status" | \
grep Uptime && echo -e "\033[0;32mMySQL user - OK\033[0m" || \
echo -e "\033[0;31mCannot connect to MySQL\033[0m"
_images/mysql_user_ok.png

  1. Add privileges to the user so the Agent can collect metrics

mysql> GRANT REPLICATION CLIENT ON *.* TO 'datadog'@'%' WITH MAX_USER_CONNECTIONS 5;
_images/grant_replication_client_mysql.png
mysql> GRANT PROCESS ON *.* TO 'datadog'@'%';
_images/grant_process_on_mysql.png

  1. To collect additional metrics, enable performance_schema

mysql> GRANT SELECT ON performance_schema.* TO 'datadog'@'%';
_images/mysql_performance_schema.png

  1. Reload the grant tables in the MySQL database so the privilges granted to the user can take effect wihtout reloading or restarting the MySQL service.

mysql> FLUSH PRIVILEGES;

  1. Check to make sure privilges were added to Datadog user

mysql> show grants for 'datadog'@'%';

_images/show_grants_user_mysql.png

Add MySQL Configuration to Datadog Agent

  1. Add configuration block to the mysql.d/conf.yaml file to collect the MySQL metrics

vagrant@vagrant:~$ sudo vim /etc/datadog-agent/conf.d/mysql.d/conf.yaml
_images/mysql.d:conf.png

Note

Wrap your password in single quotes in case a special character is present

  1. Restart the Agent to start sending MySQL metrics to Datadog:

vagrant@vagrant:~$ sudo service datadog-agent restart

  1. Make MySQL logs more accessible - Edit /etc/mysql/conf.d/mysqld_safe_syslog.cnf and remove or comment the lines.

  • Edit /etc/mysql/my.cnf and add following lines to enable general, error, and slow query logs

[mysqld_safe]
log_error = /var/log/mysql/mysql_error.log

[mysqld]
general_log = on
general_log_file = /var/log/mysql/mysql.log
log_error = /var/log/mysql/mysql_error.log
slow_query_log = on
slow_query_log_file = /var/log/mysql/mysql_slow.log
long_query_time = 2

  1. Restart MySQL: sudo service mysql restart

  1. Make sure Agent has read access on var/log/mysql

6. In /etc/logrotate.d/mysql-server there should be something similar to:

_images/:etc:logrotate.d:mysql-server.png

7. Enable Log Collection in the Agent’s datadog.yaml file

_images/logs=enabled_datadog_yaml.png

8. Add Configuration block to the /etc/datadog-agent/conf.d/mysql.d/conf.yaml file to start collecting MySQL logs

_images/add_logs_configblock_to_confyaml.png

  1. Restart the Agent: vagrant@vagrant:~$ sudo service datadog-agent restart

Check MySQL Integration in Datadog UI

1. Datadog UI now recognizes the MySQL Integration

_images/mysql_integration_installed.png

2. The Host Map now shows MySQL with Metrics and a Status Check

_images/mysql_host_map_metrics.png

_images/mysql_host_map_statuscheck.png

3. MySQL Overview Dashboard

_images/mysql_overview_dashboard.png

Create Custom Agent Check

1. Change to the conf.d directory of the Datadog Agent and create a new config file for the custom Agent check

vagrant@vagrant:~$ sudo vim /etc/datadog-agent/conf.d/custom_my_metric_check.yaml

2. Edit the custom_my_metric_check.yaml file to include the config

_images/custom_my_metric_check_yaml.png

3. Create a new Python Check file my_metric in the checks.d directory of the Agent

vagrant@vagrant:~$ sudo vim /etc/datadog-agent/checks.d/custom_my_metric_check.py

_images/custom_my_metric_check_py_new.png

  1. Restart the Agent: vagrant@vagrant:~$ sudo service datadog-agent restart

Note

The names of the configuration and check files must match. If your check is called custom_my_metric_check.py the configuration file must be name custom_my_metric_check.yaml

  1. Change back to the sudo vim /etc/datadog-agent/conf.d/custom_my_metric_check.yaml file and edit the min_collection_interval to 45 seconds.

_images/min_collection_interval=45.png

  1. Verify that your check is running

sudo -u dd-agent -- datadog-agent check custom_my_metric_check

_images/verifying_custom_check.png

7. You will now see the my_metric graph show up in the Metrics Explorer

_images/my_metric_explorer.png

Bonus : It is possible to change the collection interval through the configuration file created in /etc/datadog-agent/conf.d/custom_my_metric_check.yaml without modifying the python code. This was executed in Step 5 of this section.