Dave Heavy Industries - A journal of tech-findings and ramblings

12 Aug 2013

NGINX snmp monitoring

12 AUG/13 0

So far as I could tell, nginx doesn’t provide any stats out to snmp. This is something we like to watch closely so I put together the following based on a script I found here - http://www.kutukupret.com/2011/05/31/how-to-graph-nginx-statistics/

The script above took a few metrics out of the nginx_status stub page that you can include into your nginx config.

you can do this by including

location /nginx_status { stub_status on; access_log off; } under your server binding. you might (should) want to restrict access to this to only your internal subnets or localhost.

nginx_stats.pl

#!/usr/bin/perl
# $Revision: 2 $
# $Date: 2008-09-12 15:11:40 +0300 (Fri, 12 Sep 2008) $
 
my %opt = (
# http link to nginx stub_status, be sure turn on stub_status in nginx conf
    nginx_status   => 'http://localhost:80/nginx_status',
# path for program what may dump web page, normaly lynx -dump
#    lynx            => 'curl ',
    lynx            => 'wget -q -Y off -O -',
);
 
$opt{var} = $ARGV[0] if $ARGV[0];
$opt{nginx_status} = $ARGV[1] if $ARGV[1] and $ARGV[1]=~/^http:\/\/\w+/;
$opt{var} ||= '';
 
my $do = `$opt{lynx} $opt{nginx_status}`;
 
    $do=~/^Active connections:\s*(\d+)\s*$/ms or warn "Error! Can't find data!\nIN :\n$do";
    $opt{d1} = $1;
    $do=~/^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/ms or warn "Error! Can't find data!\nIN :\n$do";
    $opt{d2} = $1;
    $opt{d3} = $2; 
    $opt{d4} = $3;
#elsif { $do=~/^Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/; }
 
print "$opt{d1}\n";
print "$opt{d2}\n";
print "$opt{d3}\n";
print "$opt{d4}\n";
#print "$opt{up}\n" if $opt{up};
print "Nginx $opt{var}\n";
Once this has been created somewhere (I like /bin for this), you then need to link it into snmp.

you can do this with something along the lines of the following in your /etc/snmp/snmpd.conf

extend .1.3.6.1.4.1.3031.67 “exim-stats” /usr/bin/perl /bin/nginx_stats.pl output will look something like

iso.3.6.1.4.1.3031.67.1.0 = INTEGER: 1
iso.3.6.1.4.1.3031.67.2.1.2.10.101.120.105.109.45.115.116.97.116.115 = STRING: "/usr/bin/perl"
iso.3.6.1.4.1.3031.67.2.1.3.10.101.120.105.109.45.115.116.97.116.115 = STRING: "/bin/nginx_stats.pl"
iso.3.6.1.4.1.3031.67.2.1.4.10.101.120.105.109.45.115.116.97.116.115 = ""
iso.3.6.1.4.1.3031.67.2.1.5.10.101.120.105.109.45.115.116.97.116.115 = INTEGER:5
iso.3.6.1.4.1.3031.67.2.1.6.10.101.120.105.109.45.115.116.97.116.115 = INTEGER:1
iso.3.6.1.4.1.3031.67.2.1.7.10.101.120.105.109.45.115.116.97.116.115 = INTEGER:1
iso.3.6.1.4.1.3031.67.2.1.20.10.101.120.105.109.45.115.116.97.116.115 = INTEGER: 4
iso.3.6.1.4.1.3031.67.2.1.21.10.101.120.105.109.45.115.116.97.116.115 = INTEGER: 1
iso.3.6.1.4.1.3031.67.3.1.1.10.101.120.105.109.45.115.116.97.116.115 = STRING: "1"
iso.3.6.1.4.1.3031.67.3.1.2.10.101.120.105.109.45.115.116.97.116.115 = STRING: "1
360890
360890
338689
Nginx "
iso.3.6.1.4.1.3031.67.3.1.3.10.101.120.105.109.45.115.116.97.116.115 = INTEGER:5
iso.3.6.1.4.1.3031.67.3.1.4.10.101.120.105.109.45.115.116.97.116.115 = INTEGER:0
iso.3.6.1.4.1.3031.67.4.1.2.10.101.120.105.109.45.115.116.97.116.115.1 = STRING: "1"
iso.3.6.1.4.1.3031.67.4.1.2.10.101.120.105.109.45.115.116.97.116.115.2 = STRING: "360890"
iso.3.6.1.4.1.3031.67.4.1.2.10.101.120.105.109.45.115.116.97.116.115.3 = STRING: "360890"
iso.3.6.1.4.1.3031.67.4.1.2.10.101.120.105.109.45.115.116.97.116.115.4 = STRING: "338689"
iso.3.6.1.4.1.3031.67.4.1.2.10.101.120.105.109.45.115.116.97.116.115.5 = STRING: "Nginx "
comments powered by Disqus