Révision 940c49f3
Added POD and an additional User Agent environment variable
| plugins/other/nginx_request | ||
|---|---|---|
| 1 | 1 |
#!/usr/bin/perl -w |
| 2 |
# |
|
| 2 |
# -*- mode: cperl; mode: autopair -*-
|
|
| 3 | 3 |
# Magic markers: |
| 4 | 4 |
#%# family=auto |
| 5 | 5 |
#%# capabilities=autoconf |
| 6 |
# nginx_request --- Determine the current connection rate for |
|
| 7 |
# nginx. Based on a nginx_request plugin by unknown |
|
| 8 |
# author. |
|
| 9 |
|
|
| 10 |
# Copyright (C) 2010 António P. P. Almeida <appa@perusio.net> |
|
| 11 |
|
|
| 12 |
# Author: António P. P. Almeida <appa@perusio.net> |
|
| 13 |
|
|
| 14 |
# Permission is hereby granted, free of charge, to any person obtaining a |
|
| 15 |
# copy of this software and associated documentation files (the "Software"), |
|
| 16 |
# to deal in the Software without restriction, including without limitation |
|
| 17 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|
| 18 |
# and/or sell copies of the Software, and to permit persons to whom the |
|
| 19 |
# Software is furnished to do so, subject to the following conditions: |
|
| 20 |
|
|
| 21 |
# The above copyright notice and this permission notice shall be included in |
|
| 22 |
# all copies or substantial portions of the Software. |
|
| 23 |
|
|
| 24 |
# Except as contained in this notice, the name(s) of the above copyright |
|
| 25 |
# holders shall not be used in advertising or otherwise to promote the sale, |
|
| 26 |
# use or other dealings in this Software without prior written authorization. |
|
| 27 |
|
|
| 28 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
| 29 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
| 30 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
| 31 |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
| 32 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
| 33 |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
| 34 |
# DEALINGS IN THE SOFTWARE. |
|
| 35 |
|
|
| 36 |
=head1 NAME |
|
| 37 |
|
|
| 38 |
nginx_request - Munin plugin to show number of requests/connection served by nginx. |
|
| 39 |
=encoding utf8 |
|
| 40 |
|
|
| 41 |
=head1 APPLICABLE SYSTEMS |
|
| 42 |
|
|
| 43 |
Any nginx host |
|
| 44 |
|
|
| 45 |
=head1 CONFIGURATION |
|
| 46 |
|
|
| 47 |
This shows the default configuration of this plugin. You can override |
|
| 48 |
the status URL and User Agent. |
|
| 49 |
|
|
| 50 |
[nginx*] |
|
| 51 |
env.url http://localhost/nginx_status |
|
| 52 |
env.ua nginx-status-verifier/0.1 |
|
| 53 |
|
|
| 54 |
Nginx must also be configured. Firstly the stub-status module must be |
|
| 55 |
compiled, and secondly it must be configured like this: |
|
| 56 |
|
|
| 57 |
server {
|
|
| 58 |
listen 127.0.0.1; |
|
| 59 |
server_name localhost; |
|
| 60 |
location /nginx_status {
|
|
| 61 |
stub_status on; |
|
| 62 |
access_log off; |
|
| 63 |
allow 127.0.0.1; |
|
| 64 |
deny all; |
|
| 65 |
} |
|
| 66 |
} |
|
| 67 |
|
|
| 68 |
=head1 MAGIC MARKERS |
|
| 69 |
|
|
| 70 |
#%# family=auto |
|
| 71 |
#%# capabilities=autoconf |
|
| 72 |
|
|
| 73 |
=head1 VERSION |
|
| 74 |
|
|
| 75 |
1.1 |
|
| 76 |
|
|
| 77 |
=head1 BUGS |
|
| 78 |
|
|
| 79 |
None known |
|
| 80 |
|
|
| 81 |
=head1 AUTHOR |
|
| 82 |
|
|
| 83 |
Unknown. Modified by António Almeida <appa@perusio.net> |
|
| 84 |
|
|
| 85 |
=head1 REPOSITORY |
|
| 86 |
|
|
| 87 |
Source code at http://github.com/perusio/nginx-munin |
|
| 88 |
|
|
| 89 |
=head1 LICENSE |
|
| 90 |
|
|
| 91 |
MIT |
|
| 92 |
|
|
| 93 |
=cut |
|
| 94 |
|
|
| 6 | 95 |
|
| 7 | 96 |
my $ret = undef; |
| 8 | 97 |
|
| 9 |
if (! eval "require LWP::UserAgent;"){
|
|
| 10 |
$ret = "LWP::UserAgent not found";
|
|
| 98 |
if (! eval "require LWP::UserAgent;") {
|
|
| 99 |
$ret = "LWP::UserAgent not found";
|
|
| 11 | 100 |
} |
| 12 | 101 |
|
| 13 | 102 |
chomp(my $fqdn=`hostname -f`); |
| 14 | 103 |
|
| 104 |
|
|
| 105 |
## Environment defined variables. |
|
| 106 |
## The default URL is nginx_status if different set it in the environment. |
|
| 15 | 107 |
my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://$fqdn/nginx_status";
|
| 108 |
## The default user agent is ngnix-status-verifier/0.1 if different |
|
| 109 |
## set it in the environment. |
|
| 110 |
my $UA = exists $ENV{'ua'} ? $ENV{'ua'} : 'nginx-status-verifier/0.1';
|
|
| 16 | 111 |
|
| 17 |
if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" )
|
|
| 18 |
{
|
|
| 19 |
if ($ret){
|
|
| 20 |
print "no ($ret)\n";
|
|
| 21 |
exit 1;
|
|
| 22 |
} |
|
| 23 |
|
|
| 24 |
my $ua = LWP::UserAgent->new(timeout => 30);
|
|
| 25 |
my $response = $ua->request(HTTP::Request->new('GET',$URL));
|
|
| 26 |
|
|
| 27 |
unless ($response->is_success and $response->content =~ /server/im)
|
|
| 28 |
{
|
|
| 29 |
print "no (no nginx status on $URL)\n";
|
|
| 30 |
exit 1;
|
|
| 31 |
}
|
|
| 32 |
else
|
|
| 33 |
{
|
|
| 34 |
print "yes\n";
|
|
| 35 |
exit 0;
|
|
| 36 |
}
|
|
| 112 |
if (exists $ARGV[0] and $ARGV[0] eq "autoconf") {
|
|
| 113 |
if ($ret) {
|
|
| 114 |
print "no ($ret)\n";
|
|
| 115 |
exit 1;
|
|
| 116 |
}
|
|
| 117 |
|
|
| 118 |
my $ua = LWP::UserAgent->new(timeout => 30); |
|
| 119 |
# Set the UA to something different from the libwww-perl.
|
|
| 120 |
# That UA is blocked.
|
|
| 121 |
$ua->agent($UA); |
|
| 122 |
my $response = $ua->request(HTTP::Request->new('GET',$URL));
|
|
| 123 |
|
|
| 124 |
unless ($response->is_success and $response->content =~ /server/im)
|
|
| 125 |
{
|
|
| 126 |
print "no (no nginx status on $URL)\n";
|
|
| 127 |
exit 1;
|
|
| 128 |
} else {
|
|
| 129 |
print "yes\n";
|
|
| 130 |
exit 0;
|
|
| 131 |
}
|
|
| 37 | 132 |
} |
| 38 | 133 |
|
| 39 |
if ( exists $ARGV[0] and $ARGV[0] eq "config" ) |
|
| 40 |
{
|
|
| 41 |
print "graph_title NGINX requests\n"; |
|
| 42 |
print "graph_args --base 1000\n"; |
|
| 43 |
print "graph_category nginx\n"; |
|
| 44 |
print "graph_vlabel Request per second\n"; |
|
| 45 |
|
|
| 46 |
print "request.label req/sec\n"; |
|
| 47 |
print "request.type DERIVE\n"; |
|
| 48 |
print "request.min 0\n"; |
|
| 49 |
print "request.label requests $port\n"; |
|
| 50 |
print "request.draw LINE2\n"; |
|
| 51 |
|
|
| 52 |
exit 0; |
|
| 134 |
## Munin config method. |
|
| 135 |
if (exists $ARGV[0] and $ARGV[0] eq "config") {
|
|
| 136 |
print "graph_title NGINX requests\n"; |
|
| 137 |
print "graph_args --base 1000\n"; |
|
| 138 |
print "graph_category nginx\n"; |
|
| 139 |
print "graph_vlabel Request per second\n"; |
|
| 140 |
print "request.label req/sec\n"; |
|
| 141 |
print "request.type DERIVE\n"; |
|
| 142 |
print "request.min 0\n"; |
|
| 143 |
print "request.draw LINE2\n"; |
|
| 144 |
|
|
| 145 |
exit 0; |
|
| 53 | 146 |
} |
| 54 | 147 |
|
| 55 | 148 |
my $ua = LWP::UserAgent->new(timeout => 30); |
| 56 |
|
|
| 149 |
# Set the UA to something different from the libwww-perl. |
|
| 150 |
# That UA is blocked. |
|
| 151 |
$ua->agent($UA); |
|
| 57 | 152 |
my $response = $ua->request(HTTP::Request->new('GET',$URL));
|
| 58 | 153 |
|
| 59 | 154 |
if ($response->content =~ /^\s+(\d+)\s+(\d+)\s+(\d+)/m) {
|
| 60 |
print "request.value $3\n";
|
|
| 155 |
print "request.value $3\n"; |
|
| 61 | 156 |
} else {
|
| 62 |
print "request.value U\n";
|
|
| 157 |
print "request.value U\n"; |
|
| 63 | 158 |
} |
Formats disponibles : Unified diff