Datadog php-fpm monitoring via nginx

It took me some time to get this set up properly, but here are my configs that finally worked to get php-fpm monitoring using datadog.

1. nginx vhost config

First, make sure you override your site’s hostname to localhost. For my site this is to make sure connections don’t go out to cloudflare but stay local on the server: /etc/hosts needs to contain this line.

127.0.0.1 www.karelbemelmans.com karelbemelmans.com

I use my port 80 vhost config for the status page. Cloudflare enforces SSL so this vhost never gets used for anything non-local on my server.

server {
  listen 80;
  listen [::]:80;
  server_name www.karelbemelmans.com karelbemelmans.com;
  server_name www.karelbemelmans.be karelbemelmans.be;
  access_log /var/log/nginx/www.karelbemelmans.com/access.log main;
  error_log /var/log/nginx/www.karelbemelmans.com/error.log error;
  location ~ ^/(status|ping) {
    access_log off;
    allow 127.0.0.1;
    deny all;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass php_www.karelbemelmans.com;
  }
}

I use “php_www.karelbemelmans.com” as fastcgi_pass value. This is defined in another file on the conf.d dir and matches the socket definition in the php-fpm pool config file.

/etc/nginx/conf.d/upstream-www.karelbemelmans.com.conf:

upstream php_www.karelbemelmans.com {
 server unix:/var/run/php-fpm/www.karelbemelmans.com.sock;
}

(This setup is inspired by Mattias’ config for the Nucleus customers)

2. php-fpm pool config

Make sure these 3 lines are present in your php-fpm pool config:

pm.status_path = /status
ping.path = /ping
ping.response = pong

Normally your acl should be fine as requests will come from localhost.

3. Datadog config file

So nginx and php-fpm are configured, all we have left is the datadog config file:

/etc/dd-agent/conf.d/php_fpm.yaml:

init_config:
instances:
 - # Get metrics from your FPM pool with this URL
   status_url: http://karelbemelmans.com/status
   # Get a reliable service check of your FPM pool with that one
   ping_url: http://karelbemelmans.com/ping
   # Set the expected reply to the ping.
   ping_reply: pong

Reload nginx, php-fpm and datadog-agent after that and your php-fpm tracking should now work. This tracks only 1 pool, it’s up to you to figure out how to track multiple pools now :)

comments powered by Disqus