Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / nginx / nginx-combined @ df5325d2

Historique | Voir | Annoter | Télécharger (3,99 ko)

1 64fc2d99 Minato
#!/usr/bin/perl -w
2
# -*- cperl -*-
3
# Magic markers:
4
#%# family=auto
5
#%# capabilities=autoconf
6
# nginx_combine_ --- Determine the current status of Nginx
7
#                  using the http_stub_status module.
8
# 					extend of nginx_status_ plugin of  Ant?nio P. P. Almeida
9
10
# Copyright (C) 2010 Ant?nio P. P. Almeida <appa@perusio.net>
11
# Copyright (C) 2010 Minato Miray <minatomiray@gmail.com>
12
13
# Author: Ant?nio P. P. Almeida <appa@perusio.net>, 
14
# Author: Minato Miray <minatomiray@gmail.com>
15
16
#######################################
17
# Nginx combined plugin to measure in one graph:
18
# - Request /sec
19
# - Connection / sec
20
# - Request / connection
21
# - Active connections
22
# - Reading
23
# - Writing
24
# - Waiting
25
########################################
26
27
# Usage:
28
# Copy to /usr/share/munin/plugins
29
# ln -s /usr/share/munin/plugins/nginx_combined_ /etc/munin/plugins/nginx_combined_[hostname OR IP address]
30
31
#examples based on nginx configuration:
32
#example1: ./nginx_combined_mysite.net
33
#example2: ./nginx_combined_10.0.0.1
34
35
########################################
36
37
my $ret = undef;
38
39
if (! eval "require LWP::UserAgent;"){
40
	$ret = "LWP::UserAgent not found";
41
}
42
43
chomp(my $fqdn = `basename $0 | sed 's/^nginx_combined_//g'`);
44
45
my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://$fqdn/nginx_status";
46
47
if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" )
48
{
49
	if ($ret){
50
		print "no ($ret)\n";
51
		exit 1;
52
	}
53
	
54
	my $ua = LWP::UserAgent->new(timeout => 30);
55
        my $response = $ua->request(HTTP::Request->new('GET',$URL));
56
57
	unless ($response->is_success and $response->content =~ /server/im)
58
	{
59
		print "no (no nginx status on $URL)\n";
60
		exit 1;
61
	}
62
	else
63
	{
64
		print "yes\n";
65
		exit 0;
66
	}
67
}
68
69
if ( exists $ARGV[0] and $ARGV[0] eq "config" )
70
{
71
	print "graph_title NGINX status: $URL\n";
72
	print "graph_args --base 1000\n";
73
	print "graph_category nginx\n";
74
    print "graph_vlabel Connections\n";
75
76
    print "reqpsec.label Request/sec.\n";
77
	print "reqpsec.info  Request/sec.\n";
78
	print "reqpsec.draw LINE2\n";	
79
80
    print "conpersec.label Connection/sec.\n";
81
	print "conpersec.info  Connection/sec.\n";
82
	print "conpersec.draw LINE2\n";	
83
84
    print "reqpcon.label Request/conn.\n";
85
	print "reqpcon.info  Request/conn.\n";
86
	print "reqpcon.draw LINE2\n";	
87
88
    print "total.label Active connections\n";
89
	print "total.info  Active connections\n";
90
	print "total.draw LINE2\n";
91
92
    print "reading.label Reading\n";
93
	print "reading.info  Reading\n";
94
	print "reading.draw LINE2\n";	
95
96
    print "writing.label Writing\n";
97
	print "writing.info  Writing\n";
98
	print "writing.draw LINE2\n";	
99
100
    print "waiting.label Waiting\n";
101
	print "waiting.info  Waiting\n";
102
	print "waiting.draw LINE2\n";	
103
	
104
	exit 0;
105
}
106
107
#do requests
108
my $ua = LWP::UserAgent->new(timeout => 10);
109
my $response = $ua->request(HTTP::Request->new('GET',$URL));
110
sleep(1);
111
my $response2 = $ua->request(HTTP::Request->new('GET',$URL));
112
113
114
#calculate responses
115
$response->content =~ /Active connections:\s+(\d+).*Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/s;
116
my $a1 = $1;
117
my $r1 = $2;
118
my $w1 = $3;
119
my $wa1 = $4;
120
121
my $out1 = $response->content;
122
$out1 =~ s/\n/ /g;
123
my @vals = split(/ /, $out1);
124
125
my $tmp1_reqpsec=$vals[11];
126
my $tmp1_conpsec=$vals[10];
127
128
$response2->content =~ /Active connections:\s+(\d+).*Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/s;
129
130
my $a2 = $1;
131
my $r2 = $2;
132
my $w2 = $3;
133
my $wa2 = $4;
134
135
my $out2 = $response2->content;
136
$out2 =~ s/\n/ /g;
137
my @vals2 = split(/ /, $out2);
138
my $tmp2_reqpsec=$vals2[11];
139
my $tmp2_conpsec=$vals2[10];
140
141 4a6ab555 Ilya Ivanov
my $conpersec=0;
142 64fc2d99 Minato
my $reqpcon=0;
143
my $reqpsec=0;
144
if (defined $tmp2_conpsec &&  $tmp2_conpsec =~ /^[+-]?\d+$/ && $tmp2_conpsec > 0){
145
	$conpersec=$tmp2_conpsec-$tmp1_conpsec;
146
}
147
if (defined $tmp2_reqpsec && $tmp2_reqpsec =~ /^[+-]?\d+$/  && $tmp2_reqpsec > 0){
148
	$reqpsec=$tmp2_reqpsec-$tmp1_reqpsec; 
149
}
150
if ($conpersec > 0){
151
	$reqpcon=$reqpsec/$conpersec;
152 4a6ab555 Ilya Ivanov
} 
153
154
print "reqpsec.value $reqpsec\n";
155
print "conpersec.value $conpersec\n";
156
printf("reqpcon.value %.2f\n", $reqpcon);
157
print "total.value $a2\n";
158
print "reading.value $r2\n";
159
print "writing.value $w2\n";
160
print "waiting.value $wa2\n";