Now that you have successfully deployed our agent using Chef you want to monitor your applications using Datadog without extensive manual configuration.
In this article we will use PostgreSQL to demonstrate how to let Chef do all the work for us.
Start by importing the community's cookbook. knife cookbook site install postgresql
After running the command you should have a directory called postgresql
in your cookbooks directory.
Then, simply include the postgresql
recipe to the server's run list. This means adding the recipe to a role that the server assumes.
run_list %w{
# other recipes
recipe["postgresql::server"]
}
Once you have run chef-client
, PostgreSQL should be running on the server.
knife cookbook site install database
The Datadog agent requires a database user to be created and this cookbook allows Chef to create that user programatically.
After running the command you should have directories called database
and postgresql
in your cookbooks directory.
Start by editing cookbooks/datadog/attributes/default.rb
and add the username and password of the new database user. The Datadog agent connects using this username/password so it is good practice to make these separate from other database usernames and passwords.
default[:datadog][:postgresql][:username] = "pick_some_username"
default[:datadog][:postgresql][:password] = "a great password"
Simply create a file name dd-agent-deps.rb
with the following contents and place it in the datadog cookbook, i.e. in cookbooks/datadog/recipes/dd-agent-deps.rb
# An example of tweaking the agent configuration to monitor apps
# In this case, a postgresql database
include_recipe "database"
dbconn = {
:host => "localhost",
:port => node["postgresql"]["port"],
:username => "postgres",
:password => node["postgresql"]["password"]["postgres"]
}
# Create a user that the agent will use
database_user node["datadog"]["postgresql"]["username"] do
connection dbconn
password node["datadog"]["postgresql"]["password"]
provider Chef::Provider::Database::PostgresqlUser
action :create
end
Add this recipe to the server's role (or run list):
run_list %w{
# other recipes
recipe["postgresql::server"],
recipe["datadog::dd-agent-deps"]
}
Run chef-client and verify that the user has been created by running: psql -U datadog -h localhost postgres
You should see something this: psql (9.1.1) SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) Type "help" for help.
postgres=>
Step 3. Update the agent configuration template to monitor Postgres
Append the following to cookbooks/datadog/templates/default/datadog.conf.erb
<% if node["postgresql"] && node["datadog"]["postgresql"] %>
# -------------------------------------------------------------------------- #
# PostgreSQL #
# -------------------------------------------------------------------------- #
postgresql_server: localhost
postgresql_port: 5432
postgresql_user: <%= node["datadog"]["postgresql"]["username"] %>
postgresql_pass: <%= node["datadog"]["postgresql"]["password"] %>
<% end %>
This simply instructs the agent to monitor the local postgresql instance using the credentials we have just defined.
Run chef-client
. It will restart with agent with an updated configuration. To verify:
sudo grep postgresql /etc/dd-agent/datadog.conf
should return the credentials you have just chosen.