munin-contrib/plugins/nginx/nginx-combined

162 lines
4.0 KiB
Perl
Executable File

#!/usr/bin/perl -w
# -*- cperl -*-
# Magic markers:
#%# family=auto
#%# capabilities=autoconf
# nginx_combine_ --- Determine the current status of Nginx
# using the http_stub_status module.
# extend of nginx_status_ plugin of António P. P. Almeida
# Copyright (C) 2010 António P. P. Almeida <appa@perusio.net>
# Copyright (C) 2010 Minato Miray <minatomiray@gmail.com>
# Author: António P. P. Almeida <appa@perusio.net>,
# Author: Minato Miray <minatomiray@gmail.com>
#######################################
# Nginx combined plugin to measure in one graph:
# - Request /sec
# - Connection / sec
# - Request / connection
# - Active connections
# - Reading
# - Writing
# - Waiting
########################################
# Usage:
# Copy to /usr/share/munin/plugins
# ln -s /usr/share/munin/plugins/nginx_combined_ /etc/munin/plugins/nginx_combined_[hostname OR IP address]
#examples based on nginx configuration:
#example1: ./nginx_combined_mysite.net
#example2: ./nginx_combined_10.0.0.1
########################################
my $ret = undef;
if (! eval "require LWP::UserAgent;"){
$ret = "LWP::UserAgent not found";
}
chomp(my $fqdn = `basename $0 | sed 's/^nginx_combined_//g'`);
my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://$fqdn/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);
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: $URL\n";
print "graph_args --base 1000\n";
print "graph_category webserver\n";
print "graph_vlabel Connections\n";
print "reqpsec.label Request/sec.\n";
print "reqpsec.info Request/sec.\n";
print "reqpsec.draw LINE2\n";
print "conpersec.label Connection/sec.\n";
print "conpersec.info Connection/sec.\n";
print "conpersec.draw LINE2\n";
print "reqpcon.label Request/conn.\n";
print "reqpcon.info Request/conn.\n";
print "reqpcon.draw LINE2\n";
print "total.label Active connections\n";
print "total.info Active connections\n";
print "total.draw LINE2\n";
print "reading.label Reading\n";
print "reading.info Reading\n";
print "reading.draw LINE2\n";
print "writing.label Writing\n";
print "writing.info Writing\n";
print "writing.draw LINE2\n";
print "waiting.label Waiting\n";
print "waiting.info Waiting\n";
print "waiting.draw LINE2\n";
exit 0;
}
#do requests
my $ua = LWP::UserAgent->new(timeout => 10);
my $response = $ua->request(HTTP::Request->new('GET',$URL));
sleep(1);
my $response2 = $ua->request(HTTP::Request->new('GET',$URL));
#calculate responses
$response->content =~ /Active connections:\s+(\d+).*Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/s;
my $a1 = $1;
my $r1 = $2;
my $w1 = $3;
my $wa1 = $4;
my $out1 = $response->content;
$out1 =~ s/\n/ /g;
my @vals = split(/ /, $out1);
my $tmp1_reqpsec=$vals[11];
my $tmp1_conpsec=$vals[10];
$response2->content =~ /Active connections:\s+(\d+).*Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/s;
my $a2 = $1;
my $r2 = $2;
my $w2 = $3;
my $wa2 = $4;
my $out2 = $response2->content;
$out2 =~ s/\n/ /g;
my @vals2 = split(/ /, $out2);
my $tmp2_reqpsec=$vals2[11];
my $tmp2_conpsec=$vals2[10];
my $conpersec=0;
my $reqpcon=0;
my $reqpsec=0;
if (defined $tmp2_conpsec && $tmp2_conpsec =~ /^[+-]?\d+$/ && $tmp2_conpsec > 0){
$conpersec=$tmp2_conpsec-$tmp1_conpsec;
}
if (defined $tmp2_reqpsec && $tmp2_reqpsec =~ /^[+-]?\d+$/ && $tmp2_reqpsec > 0){
$reqpsec=$tmp2_reqpsec-$tmp1_reqpsec;
}
if ($conpersec > 0){
$reqpcon=$reqpsec/$conpersec;
}
print "reqpsec.value $reqpsec\n";
print "conpersec.value $conpersec\n";
printf("reqpcon.value %.2f\n", $reqpcon);
print "total.value $a2\n";
print "reading.value $r2\n";
print "writing.value $w2\n";
print "waiting.value $wa2\n";