nodereality

node c0re

Monitoring nginx with munin

written by Ghirai, on Jan 6, 2010 11:34:00 PM.

If you ever wanted to know how to graph nginx requests, here's one way of doing it.

You can tail the logfile(s) and calculate the requests in a give time interval, but fortunately there's a much simpler way. The first thing to do is to set things up so that we can read the statistics. Edit your nginx.conf and add the following (doesn't matter in which server{} block you add it):

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

If you want to gather data from a remote machine, change the allow line, or remove it alltogether (in that case remove the deny line as well).

Now if you browse to http://127.0.0.1/my_nginx_stats you will see something like this:

Active connections: 1 
server accepts handled requests
 21 21 23 
Reading: 0 Writing: 1 Waiting: 0 

With this data it's trivial to write a munin plugin:

#!/usr/local/bin/python
import sys
import urllib

LOCATION = 'http://127.0.0.1/my_nginx_stats'

if len(sys.argv) == 2 and sys.argv[1] == "autoconf":

  print "yes"

elif len(sys.argv) == 2 and sys.argv[1] == "config":

  print 'graph_title nginx connections'
  print 'graph_vlabel current connection count'
  print 'graph_category Web'

  print 'conn.label connections'
  print 'graph_args --base 1000 -l 0'

else:

  h = urllib.urlopen(LOCATION)
  t = h.read()
  h.close()

  l = t.split('\n')[0]
  conn = int(l[20:-1])-1

  print 'conn.value %s' % conn

Finally, here's a picture to go with the code:

example nginx munin graph

Leave a Reply