Zabbix : monitoring Nginx / PHP-FPM / MySQL

Boris HUISGEN
Boris HUISGEN
|

Voici les scripts que j’utilise pour monitorer avec Zabbix des serveurs Nginx / MySQL / PHP-FPM. Je vous livre également une template contenant l’ensemble des items et triggers.

_zabbixagentd.conf :

UserParameter=nginx.active,/etc/zabbix/scripts/nginx.pl|cut -f1|cut -d" " -f2
UserParameter=nginx.requests,/etc/zabbix/scripts/nginx.pl|cut -f2|cut -d" " -f2
UserParameter=nginx.reading,/etc/zabbix/scripts/nginx.pl|cut -f3|cut -d" " -f2
UserParameter=nginx.writing,/etc/zabbix/scripts/nginx.pl|cut -f4|cut -d" " -f2
UserParameter=nginx.waiting,/etc/zabbix/scripts/nginx.pl|cut -f5|cut -d" " -f2
UserParameter=php.ping,/etc/zabbix/scripts/php-fpm_ping.pl
UserParameter=php.conn,/etc/zabbix/scripts/php-fpm_status.pl|cut -f1|cut -d" " -f3
UserParameter=php.idle,/etc/zabbix/scripts/php-fpm_status.pl|cut -f2|cut -d" " -f3
UserParameter=php.active,/etc/zabbix/scripts/php-fpm_status.pl|cut -f3|cut -d" " -f3
UserParameter=php.total,/etc/zabbix/scripts/php-fpm_status.pl|cut -f4|cut -d" " -f3
UserParameter=php.maxchildren,/etc/zabbix/scripts/php-fpm_status.pl|cut -f5|cut -d" " -f3
UserParameter=mysql.status[*],/etc/zabbix/scripts/mysql_status.pl $1

/etc/zabbix/scripts/nginx.pl :

#!/usr/bin/perl#
# Boris HUISGEN <bhuisgen@hbis.fr>

use LWP::UserAgent;

my $URL = "http://127.0.0.1/nginx_status";

#
# DO NOT MODIFY AFTER THIS LINE
#

my $ua = LWP::UserAgent->new(timeout => 15);
my $response = $ua->request(HTTP::Request->new('GET', $URL));

my $active =  0;
my $reading = 0;
my $writing = 0;
my $waiting = 0;
my $requests = 0;

foreach (split(/\n/, $response->content)) {
    $active = $1 if (/^Active connections:\s+(\d+)/);

    if (/^Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/) {
        $reading = $1;
        $writing = $2;
        $waiting = $3;
    }

    $requests = $3 if (/^\s+(\d+)\s+(\d+)\s+(\d+)/);
}

print "Active: $active\tRequests: $requests\tReading: $reading\tWriting: $writing\tWaiting: $waiting\n";

_php-fpmping.pl :

#!/usr/bin/perl
#
# Boris HUISGEN <bhuisgen@hbis.fr>

use LWP::UserAgent;

my $URL = "http://127.0.0.1/php5fpm-ping";

#
# DO NOT MODIFY AFTER THIS LINE
#

my $ua = LWP::UserAgent->new(timeout => 15);
my $response = $ua->request(HTTP::Request->new('GET', $URL));

if ($response->is_success) {
    print "1\n";
}
else {
    print "0\n";
}

*php-fpm_status.pl* :

#!/usr/bin/perl
#
# Boris HUISGEN <bhuisgen@hbis.fr>

use LWP::UserAgent;

my $URL = "http://127.0.0.1/php5fpm-status";

#
# DO NOT MODIFY AFTER THIS LINE
#

my $ua = LWP::UserAgent->new(timeout => 15);
my $response = $ua->request(HTTP::Request->new('GET', $URL));

my $conn = 0;
my $idle = 0;
my $active = 0;
my $total = 0;
my $maxchildren = 0;

foreach (split(/\n/, $response->content)) {
    $conn = $1 if (/^accepted conn:\s+(\d+)/);
    $idle = $1 if (/^idle processes:\s+(\d+)/);
    $active = $1 if (/^active processes:\s+(\d+)/);
    $total = $1 if (/^total processes:\s+(\d+)/);
    $maxchildren = $1 if (/^max children reached:\s+(\d+)/);
}

print "Accepted conn: $conn\tIdle proc: $idle\tActive proc: $active\tTotal proc: $total\tMax children: $maxchildren\n";

_mysqlstatus.pl :

#!/usr/bin/perl
#
# Boris HUISGEN <bhuisgen@hbis.fr>

my($mysql)   = "/usr/bin/mysql";
my($user)    = "zabbix";
my($passwd)  = "abcdef123";
my($host)    = "localhost";

#
# DO NOT MODIFY AFTER THIS LINE
#

=head1 NAME

mysql_status.pl - show MySQL variable status

=head1 SYNOPSIS

 mysql_status.pl VARIABLE

 --help                      print this help message
 --version                   print version information

=head1 DESCRIPTION

This program shows the status of a MySQL variable.

=head1 AUTHOR

Boris HUISGEN <bhuisgen@hbis.fr>

=cut

use strict;
use warnings;

use Getopt::Long qw(:config auto_help auto_version);
use Pod::Usage;

$main::VERSION = "0.1-2012040501";

exit main();

sub show_variable {
    my ($variable) = $_[0];

    my ($cmd) = "$mysql -u $user -p$passwd -h $host --execute \"SHOW GLOBAL STATUS LIKE '$variable'\" --skip-column-name";
    my ($ret) =`$cmd 2> /dev/null`;
    exit(1) unless($ret);

    $ret =~ m/^$variable.[\s\t]*(.*)/;
    print "$1\n";
}

sub main {
    GetOptions ()
    or pod2usage(1);

    pod2usage (1) unless ($#ARGV == 0);

    my ($variable) = $ARGV[0];

    show_variable($variable);
}

# end of script

nginx.conf :

server {
    listen          127.0.0.1:80;
    listen          [::1]:80;
    server_name     localhost;
    root            /var/www/default/www/html;
    access_log      /var/www/default/www/logs/access.log;
    error_log       /var/www/default/www/logs/error.log;
    index           index.html;

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }

    location = /php5fpm-status {
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  /var/www/default/www/html$fastcgi_script_name;
        include         fastcgi_params;
    }

    location = /php5fpm-ping {
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  /var/www/default/www/html$fastcgi_script_name;
        include         fastcgi_params;
}

php-fpm.conf :

pm.status_path = /php5fpm-status
ping.path = /php5fpm-ping
ping.path = /php5fpm-ping

Template Zabbix : [download id=“4” ]

Boris HUISGEN
Boris HUISGEN
Blog owner
  • #mysql
  • #nginx
  • #php
  • #zabbix