Monitoring nginx Server Statistics With rrdtool
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
15http {
...
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
6croesus:~# 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:
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
.
Related posts:

16 Responses to this entry
Monitorix is another lightweight tool perl-based and web-based to monitorize easily your Linux/UNIX servers.
Try it!
У меня странно работает. Запросы в секунду показывает нормально, а вот с другими – беда.
Сначала показал совсем левые значения из миллионных величин (пр: Total Current: 450.0 m)
В дальнейшем так же 4300.0, что не сходится даже со строкой
print “RQ:$requests; TT:$total; RD:$reading; WR:$writing; WA:$waiting\n”;
которая все показывает правильно…
где собака порылась?
Вот здесь можно посмотреть:
http://foxtrot355.server4you.de/stat.html
Нашел ошибку!
вместо:
“DS:total:ABSOLUTE:120:0:60000″,
“DS:reading:ABSOLUTE:120:0:60000″,
“DS:writing:ABSOLUTE:120:0:60000″,
“DS:waiting:ABSOLUTE:120:0:60000″,
надо:
“DS:total:GAUGE:120:0:60000″,
“DS:reading:GAUGE:120:0:60000″,
“DS:writing:GAUGE:120:0:60000″,
“DS:waiting:GAUGE:120:0:60000″,
Теперь данные будут правильно отображаться
[...] I can only find one case of a Rails developer who is using nginx in a production setup, although he appears to be very happy with it. [...]
>У меня странно работает. Запросы в секунду >показывает нормально, а вот с другими – беда.
Число обработанных за период соединений надо брать из другого показателя, т.к. $total отображает число установленных соединений на момент запроса.
Предлагаю заменить
if (/^\s+(\d+)\s+(\d+)\s+(\d+)/) {
> $requests = $3;
> $connects = $1;
> }
Хранение реализовать аналогично хранению $requests, c типом хранилища ABSOLUTE.
(Добавить в RRDs::create
строку “DS:connects:ABSOLUTE:120:0:100000000″,)
Чтобы получать разницу в количестве обработанных соединений между запусками, предыдущее значение записывается в отдельный файл nginx.cns (аналогично хранению числа запросов в файле nginx.rqs).
Добавляемый код:
+my $connects=0;
….
+# get connects
+if (! -e “$rrd/nginx1.cns”) {
+ $cns = 0;
+} else {
+ open RQS, “;
+ close RQS;
+ $cns = $connects if ($cns $rrd/nginx1.cns”;
+print RQS $connects;
+close RQS;
Также мне не нравится поведение в случае, если новый счетчик меньше старого – это означает что он уже успел сброситься по какой-то причине. Но это не означает что его не стоит использовать.
$rqs = $requests if ($rqs
К сожалению не все в сообщении вставилось как ожидалось, но думаю основную идею донес.
So what does it mean if the graphs are never updating (i.e. stay blank). I’ve checked the .conf, nginx_status page as well as making sure the cron is running but the graphs are never being populated. I’ve tried a few things but can’t seem to get it to work.
user@machine ~> nginx -v
nginx version: nginx/0.5.10
user@machine ~> php -v
PHP 4.3.10-18 (cli) (built: Nov 3 2006 21:56:29)
[snip]
Thanks.
It’s possible to have statistic of traffic transfered via nginx?
PavelR: Can you paste all code ?
Last version at http://nikolas.ru/nginx/
ex: http://nikolas.ru/nginx/nginx-rrd_new.pl
[...] Monitoring nginx Server Statistics With rrdtool :: Homo-Adminus Blog by Alexey Kovyrin As the result, I have created perl script, which uses RRDs perl module to create and manage rrd-database and very beautiful graphs. (tags: nginx monitoring perl scripts rrd) [...]
[...] En Debian Etch tuve que instalar la librería Perl libwww-perl y re-compilar nginx con el parámetro –with-http_stub_status_module, para habilitar el módulo ngx_http_stub_status_module. Para usar este módulo hay que añadir unas líneas en el fichero de configuración. Es muy simple, pero no voy a entrar en ello. Podéis leer más en la página del wiki del módulo (el enlace anterior) o en este otro enlace. [...]
Nice article. I guess I’ll give nginx a try on my new server. Oh and..
PavelR: The links posted above gave me a 403 Forbidden.. would be nice to get the current version of your script.
2PavelR
http://nikolas.ru/nginx/nginx-rrd_new.pl – 403 Forbidden
Also error in this project – http://www.nginx.eu/nginx-rrd.html
I was facing the same problem like you.
————————————————-
app201:/home/nginx/rrd# nginx -v
nginx version: nginx/0.5.37
app201:/home/nginx/rrd# php -v
PHP 5.2.0-8+etch11 (cli) (built: May 10 2008 10:46:24)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
app201:/home/nginx/rrd# rrdtool -v
RRDtool 1.2.15 Copyright 1997-2006 by Tobias Oetiker <tobi@oetiker.ch>
Compiled Dec 14 2006 22:05:35
Usage: rrdtool [options] command command_options
Valid commands: create, update, updatev, graph, dump, restore,
last, first, info, fetch, tune, resize, xport
RRDtool is distributed under the Terms of the GNU General
Public License Version 2. (http://www.gnu.org/copyleft/gpl.html)
For more information read the RRD manpages
————————————————-
I don't assure that this method is right but my graph is OK right now. By replacing these lines
RRDs::update “$rrd/nginx.rrd”,
“-t”, “requests:total:reading:writing:waiting”,
“N:$requests:$total:$reading:$writing:$waiting”;
with
RRDs::update (“$rrd/nginx.rrd”, “N:$requests:$total:$reading:$writing:$waiting”);
Cheers,
[...] I guess. In this case, our Nginx server was not compiled with stub_status ability which might otherwise be used, so instead we fashion a shell script that can run on (each) web server and determine rps from the [...]
I use this Server Statistics tool, it gives me a live graphical display of load dn bandwidth usage.