#!/usr/bin/perl -w

=head1 NAME

nginx_request - Munin plugin to show the request rate of an nginx
server

=head1 APPLICABLE SYSTEMS

Any nginx host

=head1 CONFIGURATION

This shows the default configuration of this plugin.  You can override
the status URL.

  [nginx*]
      env.url http://localhost/nginx_status

Nginx must also be configured.  Firstly the stub-status module must be
compiled, and secondly it must be configured like this:

  server {
        listen 127.0.0.1;
        server_name localhost;
        location /nginx_status {
                stub_status on;
                access_log   off;
                allow 127.0.0.1;
                deny all;
        }
  }

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 BUGS

None known

=head1 AUTHOR

Unknown

=head1 LICENSE

Unknown.  Not specified by the unknown author.  Nginx has a BSD
license.  Munin is GPLv2 licensed.

=cut

use Munin::Plugin::HTTP;

my $ret = undef;

if (! eval "require LWP::UserAgent;"){
	$ret = "LWP::UserAgent not found";
}

my $UA = Munin::Plugin::HTTP->new;
my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://localhost/nginx_status";

if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" )
{
	my $response = $UA->get($URL);

	unless ($response->is_success and $response->content =~ /server/im)
	{
		print "no (no nginx status on $URL)\n";
		exit 0;
	}
	else
	{
		print "yes\n";
		exit 0;
	}
}

if ( exists $ARGV[0] and $ARGV[0] eq "config" )
{
	print "graph_title Nginx requests\n";
	print "graph_args --base 1000\n";
	print "graph_category webserver\n";
	print "graph_vlabel Requests per \${graph_period}\n";
	print "request.type DERIVE\n";
	print "request.min 0\n";
	print "request.label requests\n";
	print "request.draw LINE\n";

	exit 0;
}

my $response = $UA->get($URL);

if ($response->content =~ /^\s+(\d+)\s+(\d+)\s+(\d+)/m) {
	print "request.value $3\n";
} else {
	print "request.value U\n";
}
