#!/usr/bin/perl # # Plugin to monitor the number of accesses to Nginx servers. It handles # a list of ports passed in from a plugin configuration file. # # Requirements: # - Needs access to http://localhost/server-status?auto (or modify the # address for another host). # # copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/ # # location /status { # stub_status on; # access_log off; # } # # Tip: To see if it's already set up correctly, just run this plugin # with the parameter "autoconf". If you get a "yes", everything should # work like a charm already. # # Parameters supported: # # config # autoconf # # Configurable variables # # url - Override default status-url # ports - HTTP port numbers # # $Log$ # # # # Magic markers: #%# family=auto #%# capabilities=autoconf my $ret = undef; if (! eval "require LWP::UserAgent;") { $ret = "LWP::UserAgent not found"; } my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/server-status?auto"; my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80); if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) { if ($ret) { print "no ($ret)\n"; exit 1; } my $ua = LWP::UserAgent->new(timeout => 30); my @badports; foreach my $port (@PORTS) { my $url = sprintf $URL, $port; my $response = $ua->request(HTTP::Request->new('GET',$url)); push @badports, $port unless $response->is_success and $response->content =~ /^Active connections:/im; } if (@badports) { print "no (no nginx server-status on ports @badports)\n"; exit 1; } else { print "yes\n"; exit 0; } } if ( defined $ARGV[0] and $ARGV[0] eq "config" ) { print "graph_title Nginx connections\n"; print "graph_args --base 1000\n"; print "graph_vlabel connections / \${graph_period}\n"; print "graph_category nginx\n"; foreach my $port (@PORTS) { print "connections$port.label port $port\n"; print "connections$port.type DERIVE\n"; print "connections$port.max 1000000\n"; print "connections$port.min 0\n"; } exit 0; } my $ua = LWP::UserAgent->new(timeout => 30); foreach my $port (@PORTS) { my $url = sprintf $URL, $port; my $response = $ua->request(HTTP::Request->new('GET',$url)); if ($response->content =~ /^Active connections:\s+(\d+)/im) { print "connections$port.value $1\n"; } else { print "connections$port.value U\n"; } } # vim:syntax=perl