Monitoring nginx Server Statistics With rrdtool
29 Apr2006

Few days ago I have installed nginx on one of our adult projects as reverse proxy server and for static files management. Yesterday this server got 200Mbit/sec traffic and because all admins like to create miscellaneous graphs, I have decided to draw nginx stats on graphs to see server load not only in megabits and load averages. As the result, I have created perl script, which uses RRDs perl module to create and manage rrd-database and very beautiful graphs.

This script is very easy to install and configure. You can use following steps to get pretty stats graph for your nginx-powered server:

  • Modify your nginx config file and add following location to it:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    http {
        ...
        server {
            listen SOME.IP.ADD.RESS;
            ...
            location /nginx_status {
                stub_status on;
                access_log   off;
                allow SOME.IP.ADD.RESS;
                deny all;
            }
            ...
        }
        ...
    }
  • Test your config file with following command:
    1
    2
    3
    4
    # /your/installation/sbin/nginx -t
    2006/04/29 04:24:36 [info] 31676#0: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
    2006/04/29 04:24:36 [info] 31676#0: the configuration file /opt/nginx/conf/nginx.conf was tested successfully
    #

    I you will get error about stub_status directive, check your nginx – it should be configured with ----with-http_stub_status_module option.

  • If your configuration is OK, you can restart nginx and test it:
    1
    2
    3
    4
    5
    6
    croesus:~# GET http://your-domain.com/nginx_status
    Active connections: 1492
    server accepts handled requests
     2124355 2124355 8278635
    Reading: 6 Writing: 405 Waiting: 1081
    croesus:~#
  • Download my perl script: rrd_nginx.pl and make it executable with
    1
    # chmod +x rrd_nginx.pl
  • Change settings in rrd_nginx.pl to let script know where it will save rrd-base and images:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #!/usr/bin/perl
    use RRDs;
    use LWP::UserAgent;

    # define location of rrdtool databases
    my $rrd = '/opt/rrd';
    # define location of images
    my $img = '/opt/rrd/html';
    # define your nginx stats URL
    my $URL = "http://your-domain.com/nginx_status";
    ...
  • Last step is to insert following string to /etc/crontab file and to restart cron daemon:
    * *     * * *   root    /some/path/rrd_nginx.pl
    

When all preparations will be finished, you get following images in your $img directory:

  • Nginx connections statistics:
  • Nginx requests statistics:

That is all. If you have some comments or suggestions, feel free to post them in comments section and, it you like this howto, you can click on advertisements ;-).