ScaleScaleScaleScale

Tips / Nginx


How to monitor Nginx with Munin

Munin is one of the most powerful monitoring systems for dedicated servers and cloud enviroments. It allows you to monitor your server and network activity by providing useful graphics of your CPU, Memory, Disks, Network Devices and also running system services.

Nginx on the other side, is the most popular Web server, used by millions of busy websites to boost their speed, scalability and server performance. But Nginx needs to be monitored in order to know what’s going on and how it’s working, handling requests and using resources.

How can I monitor Nginx with Munin monitoring system?

We will assume you have a fully working Nginx web server, if not check out this guides:

Installing Munin on CentOS

Let’s install Munin with yum

yum install munin munin-node -y

If you can’t find/install any munin* packages, check out this guide about installing EPEL repo, that may be needed in case the default CentOS repo doesn’t include Munin. Add munin-node to start automatically after boot

chkconfig munin-node on

Configure Munin

nano -w /etc/httpd/conf.d/munin.conf

Comment this lines by placing a # in front of them:

#AuthUserFile /etc/munin/munin-htpasswd
#AuthName "Munin"
#AuthType Basic
#require valid-user

Restart Munin:

service munin-node restart

Configuring Munin on Nginx

It’s simple, just add this location block into your virtual host configuration:

# munin configuration
location /munin {
        alias   /var/www/html/munin/;
        allow 127.0.0.1;
                allow XX.XX.XX.XX;
        deny all;
        index  index.php index.html index.htm;
        location ~* \.(png|jpg|jpeg|gif|ico)$ {
        }
}

Make sure you replace “XX.XX.XX.XX” with your real public IP, in case you have a static IP address for your internet connection, if you don’t just remove the allow/deny block.

You must also configure Nginx status so Munin can monitor it properly, you can enable it at the same file edited before by adding:

# nginx_status configuration, need for Munin
location /nginx_status {
        stub_status on;
        access_log   off;
        allow 127.0.0.1;
        deny all;
}

Fully example of a working Munin configuration inside Nginx:

server {
listen 80 default_server;
access_log off;
server_name _; 
server_name_in_redirect off;
root  /var/www/html;

# munin configuration
location /munin {
        alias   /var/www/html/munin/;
        allow 127.0.0.1;
                allow XX.XX.XX.XX;
        deny all;
        index  index.php index.html index.htm;
        location ~* \.(png|jpg|jpeg|gif|ico)$ {
        }
}

# nginx_status configuration, need for Munin
location /nginx_status {
        stub_status on;
        access_log   off;
        allow 127.0.0.1;
        deny all;
}
}

This configuration can be placed inside conf.d directory, for example: /etc/nginx/conf.d/server.conf or /etc/nginx/conf.d/munin.conf. Finally, reload Nginx to apply changes:

service nginx reload

After that, you should be able to watch Munin graphs from http://YOUR.SERVER.IP/munin/
Don’t get surprised if you don’t see any Nginx graph, we have to make sure the plugins are installed.

Nginx monitoring plugins for Munin

In order to have Munin generating graphs for Nginx, you must make sure this plugins exist in your Munin plugin directory:

ls -alh /etc/munin/plugins/nginx_request /etc/munin/plugins/nginx_status

If those doesn’t exist, simply add them to the Munin plugins directory by creating this two symbolic links:

ln -s /usr/share/munin/plugins/nginx_request /etc/munin/plugins/ -v
ln -s /usr/share/munin/plugins/nginx_status /etc/munin/plugins/ -v

Installing additional Nginx plugins for Munin

There are two additional 3rd party unofficial Nginx plugins that are going to be need: RAM monitoring and requests monitoring. Use the following commands to get it installed into your CentOS Munin installation:

wget https://github.com/perusio/nginx-munin/archive/master.zip
unzip master.zip
cd nginx-munin-master/
cp nginx_connection_request nginx_memory /etc/munin/plugins/ -fv

Restart munin to apply changes:

service munin-node restart

At this time, you should have 4 Nginx plugins working with Munin:

  1. nginx_status: shows a graphic for Nginx server status configuration.
  2. nginx_request: shows you the total requests of your Nginx web server.
  3. nginx_connection_request: presents the number of requests served by connection handled by nginx.
  4. nginx_memory: this plugins will let you know the memory used by Nginx processes, it’s calculated using ps and awk commands.

How can I check if this plugins are working?

Simple, browse http://YOUR.SERVER.IP/munin/ and you should see a section called “Nginx”, there you will find the graphs. If graphs are empty wait 10 minutes and check again.

Conclusion

As we’ve seen, having a monitoring system is really important and a critical part of your server management plan. By using Munin to monitor Nginx activity, you will be able to know more detailes about Nginx requests, memory usage & the Nginx general status. This will bring you the possibility to find problems quicky or avoid them by knowing how your Nginx is working. If you followed this guide step by step right from the top, at this time you should know how to monitor Nginx without any issues.

Further reading:

Popular search terms:

  • munin nginx
  • nginx munin
  • https://www scalescale com/tips/nginx/monitor-nginx-munin/
  • debian munin nginx
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking