#!@@PERL@@ -w
# -*- cperl -*-

=head1 NAME

nginx_status - Munin plugin to show connection status for nginx

=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;


my $ret = undef;

if (! eval "require LWP::UserAgent;"){
	$ret = "LWP::UserAgent not found";
}
if ($0 =~ /^nginx6_/) {
        if (! eval "require Net::INET6Glue::INET_is_INET6;"){
                $ret = "Net::INET6Glue::INET_is_INET6 not found";
        }
}

my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://localhost/nginx_status";

if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" ) {
    if ($ret){
	print "no ($ret)\n";
	exit 0;
    }

    my $ua = LWP::UserAgent->new(timeout => 30,
			agent => sprintf("munin/%s (libwww-perl/%s)", $Munin::Common::Defaults::MUNIN_VERSION, $LWP::VERSION));
    my $response = $ua->request(HTTP::Request->new('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 status\n";
    print "graph_args --base 1000\n";
    print "graph_category nginx\n";
    print "graph_vlabel Connections\n";

    print "total.label Active connections\n";
    print "total.info  Active connections\n";
    print "total.draw LINE\n";

    print "reading.label Reading\n";
    print "reading.info  Reading\n";
    print "reading.draw LINE\n";

    print "writing.label Writing\n";
    print "writing.info  Writing\n";
    print "writing.draw LINE\n";

    print "waiting.label Waiting\n";
    print "waiting.info  Waiting\n";
    print "waiting.draw LINE\n";

    exit 0;
}

# generate a useful error message instead of just bailing out later
die "Missing dependency: '$ret'" if ($ret);

my $ua = LWP::UserAgent->new(timeout => 30,
		agent => sprintf("munin/%s (libwww-perl/%s)", $Munin::Common::Defaults::MUNIN_VERSION, $LWP::VERSION));

my $response = $ua->request(HTTP::Request->new('GET',$URL));

#Active connections: 1845
#server accepts handled requests
# 4566318 4566318 84218236
# Reading: 2 Writing: 278 Waiting: 1565
if ($response->content =~ /Active connections:\s+(\d+).*Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/s) {
    print "total.value $1\n";
    print "reading.value $2\n";
    print "writing.value $3\n";
    print "waiting.value $4\n";
} else {
	foreach (qw(total reading writing waiting)){
	    print "$_.value U\n";
	}
}
