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