-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx_combined_
executable file
·160 lines (128 loc) · 3.99 KB
/
nginx_combined_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/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 <[email protected]>
# Copyright (C) 2010 Minato Miray <[email protected]>
# Author: António P. P. Almeida <[email protected]>,
# Author: Minato Miray <[email protected]>
#######################################
# 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 1;
}
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 1;
}
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 nginx\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";