Informatics

monitoring php-fpm threads with cacti (via snmp)

PHP-FPM is a technology integrated in PHP since v5.4.0RC2.

Its most outstanding feature is its ability to start workers with different uid/gid/chroot/environment. This allows for example for the separation in filesystem (chroot) and processes of different VirtualHosts — as long as they use php.

If implemented this way, one can end with multiple separated php-fpm environments, so the usual cacti monitoring available implementations are not useful, as they suppose a single php-fpm configuration — but a multi site php-fpm environment have multiple VirtualHosts with a separate php-fpm socket for each one. In this case, it is tricky to count all that available data… maybe the most feasible goal is to graph the total number of php-fpm threads running at a given specific moment: they’ll be a mix of all available sites, but it is useful as a load measurement.

So that’s what I ended up doing: I extended snmp as in a previous post about Maxscale monitoring, with a crafted shell script, by adding this line in snmpd.conf:

extend php-fpm /usr/local/bin/php-fpm_stats.sh

The script will just filter and output the number of php-fpm processes seen by ps:

$ ps aux| grep php-fpm| grep -v grep| wc -l

This can be used as a measurement of load in the server, or to change the configuration of php-fpm thread numbers if they’re too low or too high.

So a sample output of the script would be for example:

$ /usr/local/bin/php-fpm_stats.sh
9

And this output can be accessed via snmp on our server with this OID:

iso.3.6.1.4.1.8072.1.3.2.4.1.2.7.112.104.112.45.102.112.109.1

If you see these numbers carefully, “112.104.112.45.102.112.109” is just “php-fpm” in ASCII — the string we used after “extend” in our previous snmpd configuration.

Check from your cacti server that snmpd is correctly returning the data:

$ snmpwalk -v 2c -c yourPublicCommunity yourMaxscaleServerIP .1.3.6.1.4.1.8072.1.3.2

iso.3.6.1.4.1.8072.1.3.2.1.0 = INTEGER: 1
iso.3.6.1.4.1.8072.1.3.2.2.1.2.7.112.104.112.45.102.112.109 = STRING: “/usr/local/bin/php-fpm_stats.sh”

iso.3.6.1.4.1.8072.1.3.2.4.1.2.7.112.104.112.45.102.112.109.1 = STRING: “9”

You can browse the NET-SNMP-EXTEND-MIB OID top structure here.

Now cacti can be configured to read (with cacti’s “Data Templates”) and graphic (with a cacti “Graphic Template”) these values. This requires some obscure knowledge… (you can consult this, or this) but after everything is done, the configuration can be exported so anyone can easily use it:

The “php-fpm threads monitoring with cacti” bundle with instructions is available here.

Instructions, in brief:

  • On your php-fpm server, install and configure snmpd if not yet available.
  • Add this line at the end of /etc/snmp/snmpd.conf and restart the service
    extend php-fpm /usr/local/bin/php-fpm_stats.sh
  • Copy the file php-fpm_stats.sh in /usr/local/bin. Add exec permissions:
    chmod a+x /usr/local/bin/php-fpm_stats.sh
  • On your cacti server, using cacti’s admin Console, Import the xml file included.
  • Create a cacti Device for your php-fpm server, if not yet done.
  • Under Device / Associated Graph Templates,
    Add “SNMP – php-fpm – threads”,
    click “Create Graphs for this Host”, and
    select SNMP – php-fpm – threads Information.
  • Done!

cacti-php-fpm-threads

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s