ScaleScaleScaleScale

Tips / Nginx


Nginx Configuration For MRTG

MRTG means Multi Router Traffic Grapher. This is a tool to graph and analyze the traffic load on networks.

MRTG generates static HTML pages with simple graphs in PNG format, so you can have a clear view of what’s happening inside your network.

In this tutorial, you will find out how to install MRTG and configure Nginx to serve its pages.

If you already have MRTG running on your server, you can skip the first part. The assumption here is that you are running CentOS, RHEL or Fedora Linux.

Install MRTG and the net-snmp packages

yum install mrtg net-snmp net-snmp-utils

Configure SNMPD

This is necessary if you want to monitor localhost’s interface and other important things, like CPU and memory usage.

nano -w /etc/snmp/snmpd.conf

The following settings should be placed inside:

com2sec local localhost public
group MyRWGroup v1 local
group MyRWGroup v2c local
group MyRWGroup usm local
view all included .1 80
access MyRWGroup "" any noauth exact all all none
syslocation LA, United States
syscontact Root < your@email.com >

This will allow only SNMPD to be accesed from localhost, blocking any other external requests.

Start SNMPD and configure it at boot time:

chkconfig snmpd on
service snmpd restart

Configure MRTG using cfgmaker

The cfgmaker is a tool that allows you to configure MRTG by creating the /etc/mrtg/mrtg.cfg file:

cfgmaker --global 'WorkDir: /var/www/html/mrtg' --output /etc/mrtg/mrtg.cfg public@localhost

Explanation of each option:

--global 'WorkDir: /var/www/html/mrtg': place where MRTG will store the pages and network data.
--output /etc/mrtg/mrtg.cfg: the main mrtg configuration file
public@localhost: public is the name of the device you want to create the configuration for.

Finally, run indexmaker to create the MRTG web pages:

indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg

Set up a cron job to update the traffic graphs every four minutes:

nano -w /etc/cron.d/mrtg

Paste this inside:

*/4 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok

How can you configure MRTG with Nginx?

Configuring MRTG with Nginx is pretty easy. You need to define an alias for MRTG, as in this example, creating an mrtg.conf file:

nano -w /etc/nginx/conf.d/mrtg.conf

Paste this content inside:

server {
listen 80 default_server;
access_log off;
server_name _; 
server_name_in_redirect off;
access_log logs/server-access_log;
error_log  logs/server-error_log info;

root  /var/www/html;
        index  index.php index.html index.htm;

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

Remember to replace XX.XX.XX.XX with your own public internet IP. This will allow MRTG access from localhost (127.0.0.1) and local network public access.

Reload Nginx to apply the changes:

service nginx reload

Testing MRTG

Launch http://your.server.ip.here/mrtg/. You should see the MRTG graphs without any problem.

Conclusion

Network monitoring is essential for any company, even if you have only one server. This can help you mitigate traffic problems or recognize attacks when they hit your websites.

Though MRTG is a powerful app to have a visual map of your network traffic, it’s not the only one. What other tools and apps do you use for network monitoring? Please share your comments with us.

 

Popular search terms:

  • mrtg nginx
  • nginx mrtg
  • centos nginx mrtg
  • centos7 mrtg nginx
profile

Esteban Borges

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