Projet

Général

Profil

Révision 7a00481e

ID7a00481e94e376f810f4e772b9bba6a36d813e36
Parent 5ea89f41
Enfant 940c49f3

Ajouté par perusio il y a presque 14 ans

Addded POD and an additional User Agent environment variable

Voir les différences:

plugins/other/nginx_status
1 1
#!/usr/bin/perl -w
2
#
2
# -*- cperl -*-
3 3
# Magic markers:
4 4
#%# family=auto
5 5
#%# capabilities=autoconf
6
# nginx_status --- Determine the current status of Nginx
7
#                  using the http_stub_status module.
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

  
37
nginx_status - Munin plugin to show the connection status for nginx
38

  
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 the 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. Mantained 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
6 94

  
7 95
my $ret = undef;
8 96

  
9
if (! eval "require LWP::UserAgent;"){
10
	$ret = "LWP::UserAgent not found";
97
if (! eval "require LWP::UserAgent;") {
98
  $ret = "LWP::UserAgent not found";
11 99
}
12 100

  
13 101
chomp(my $fqdn=`hostname -f`);
14 102

  
103
## Environment defined variables.
104
## The default URL is nginx_status if different set it in the environment.
15 105
my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://$fqdn/nginx_status";
106
## The default user agent is ngnix-status-verifier/0.1 if different
107
## set it in the environment.
108
my $UA = exists $ENV{'ua'} ? $ENV{'ua'} : 'nginx-status-verifier/0.1';
16 109

  
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
	}
110

  
111
## Munin autoconf method.
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
  # This 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
    print "no (no nginx status on $URL)\n";
126
    exit 1;
127
  } else {
128
    print "yes\n";
129
    exit 0;
130
  }
37 131
}
38 132

  
39
if ( exists $ARGV[0] and $ARGV[0] eq "config" )
40
{
41
	print "graph_title NGINX status\n";
42
    print "graph_args --base 1000\n";
43
	print "graph_category nginx\n";
44
    print "graph_vlabel Connections\n";
45

  
46
    print "total.label Active connections\n";
47
	print "total.info  Active connections\n";
48
	print "total.draw LINE2\n";
49

  
50
    print "reading.label Reading\n";
51
	print "reading.info  Reading\n";
52
	print "reading.draw LINE2\n";	
53

  
54
    print "writing.label Writing\n";
55
	print "writing.info  Writing\n";
56
	print "writing.draw LINE2\n";	
57

  
58
    print "waiting.label Waiting\n";
59
	print "waiting.info  Waiting\n";
60
	print "waiting.draw LINE2\n";	
61
	
62
	exit 0;
133
## Munin config method.
134
if (exists $ARGV[0] and $ARGV[0] eq "config") {
135
  print "graph_title NGINX status\n";
136
  print "graph_args --base 1000\n";
137
  print "graph_category nginx\n";
138
  print "graph_vlabel Connections\n";
139

  
140
  print "total.label Active connections\n";
141
  print "total.info  Active connections\n";
142
  print "total.draw LINE2\n";
143

  
144
  print "reading.label Reading\n";
145
  print "reading.info  Reading\n";
146
  print "reading.draw LINE2\n";
147

  
148
  print "writing.label Writing\n";
149
  print "writing.info  Writing\n";
150
  print "writing.draw LINE2\n";
151

  
152
  print "waiting.label Waiting\n";
153
  print "waiting.info  Waiting\n";
154
  print "waiting.draw LINE2\n";
155

  
156
  exit 0;
63 157
}
64 158

  
65 159
my $ua = LWP::UserAgent->new(timeout => 30);
66

  
160
# Set the UA to something different from the libwww-perl.
161
# That UA is blocked.
162
$ua->agent($UA);
67 163
my $response = $ua->request(HTTP::Request->new('GET',$URL));
68 164

  
69
#Active connections: 1845 
70
#server accepts handled requests
71
# 4566318 4566318 84218236 
72
# Reading: 2 Writing: 278 Waiting: 1565 
165
# Active connections: 1845
166
# server accepts handled requests
167
# 4566318 4566318 84218236
168
# Reading: 2 Writing: 278 Waiting: 1565
73 169
if ($response->content =~ /Active connections:\s+(\d+).*Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/s) {
74
    print "total.value $1\n";
75
    print "reading.value $2\n";
76
    print "writing.value $3\n";
77
    print "waiting.value $4\n";
170
  print "total.value $1\n";
171
  print "reading.value $2\n";
172
  print "writing.value $3\n";
173
  print "waiting.value $4\n";
78 174
} else {
79
	foreach (qw(total reading writing waiting)){
80
	    print "$_.value U\n";
81
	}
175
  foreach (qw(total reading writing waiting)) {
176
    print "$_.value U\n";
177
  }
82 178
}

Formats disponibles : Unified diff