-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathphpfpm-multi
More file actions
executable file
·287 lines (236 loc) · 6.66 KB
/
phpfpm-multi
File metadata and controls
executable file
·287 lines (236 loc) · 6.66 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#! /usr/bin/perl
=head1 NAME
phpfpm-multi - Munin multigraph plugin to monitor PHP FPM pools
=head1 APPLICABLE SYSTEMS
PHP FPM installations, tested with PHP 5.6, 7.0 and 7.2 FPM.
Requires the JSON perl module and the C<cgi-fcgi> tool. On Debian systems
install them using C<apt-get install libjson-perl libfcgi-bin> (older systems
don't have a C<libfcgi-bin> package, install the C<libfcgi0ldbl> package
instead).
=head1 CONFIGURATION
The plugin needs to be able to read the different pool configuration
files and needs access to the sockets. If root is the only user which
has access to these resources, the following needs to be configured:
[phpfpm-multi]
user root
The path to the socket as well as the status URL will be fetched read
from the configuration file.
In addition, the following variables can be defined:
=over
=item C<env.pooldir> (default: automatically discovered)
Directory containing the pool configuration files.
=item C<env.hide_pools> (default: empty)
Comma-separated list of pools for which no statistics should be
gathered.
=back
=head1 AUTHOR
Oliver Hitz <oliver@net-track.ch>
=head1 LICENSE
GPL
=cut
use strict;
use JSON;
use File::Spec;
use Munin::Plugin;
my $cfg_pooldir;
if ($ENV{pooldir}) {
$cfg_pooldir = $ENV{pooldir};
} else {
# Try to autodetect pool directory
my $php_version = `php -v`;
if ($php_version =~ /^PHP (\d+\.\d+)/) {
$cfg_pooldir = "/etc/php/$1/fpm/pool.d/";
# Debian 8 has the pool files of PHP 5.6 in /etc/php5/fpm/pool.d/,
# so if the directory doesn't exist, fall-back to that one.
if (! -d $cfg_pooldir && $php_version =~ /^PHP 5\./) {
$cfg_pooldir = "/etc/php5/fpm/pool.d/";
}
} else {
# Fall-back to default
$cfg_pooldir = "/etc/php/7.0/fpm/pool.d/";
}
}
my @cfg_hide_pools;
if ($ENV{hide_pools}) {
@cfg_hide_pools = split /,/, $ENV{hide_pools};
}
# autoconf
if ($ARGV[0] eq "autoconf") {
if (`which cgi-fcgi`) {
print "yes\n";
} else {
print "no (cgi-fcgi not found)\n";
}
exit 0;
}
# Get the list of pools.
my @pools;
if (opendir my $dh, $cfg_pooldir) {
while (my $entry = readdir $dh) {
if ($entry !~ /\.conf$/) {
next;
}
my $pool = {
file => File::Spec->catfile($cfg_pooldir, $entry)
};
my $poolconf;
if (!open($poolconf, $pool->{file})) {
warning("Unable to pool configuration open %s.",
$pool->{file});
next;
}
while (my $line = <$poolconf>) {
# Erase comments.
$line =~ s/;.*$//;
if ($line =~ /^\[([^\]]*)\]/) {
$pool->{pool} = $1;
} elsif ($line =~ /^listen\s*=\s*(\S+)$/) {
$pool->{socket} = $1;
} elsif ($line =~ /^pm.status_path\s*=\s*(\S+)$/) {
$pool->{statuspath} = $1;
}
}
close($poolconf);
# Check if entries are valid.
if (!$pool->{pool}) {
warning("Skipping pool %s: no pool name.",
$pool->{file});
next;
}
if (!$pool->{socket}) {
warning("Skipping pool %s: no socket.",
$pool->{file});
next;
}
if (!$pool->{statuspath}) {
warning("Skipping pool %s: no pm.status_path configured.",
$pool->{file});
next;
}
# Replace $pool in socket and pm.status_path
$pool->{socket} =~ s/\$pool/$pool->{pool}/g;
$pool->{statuspath} =~ s/\$pool/$pool->{pool}/g;
# Connect to pool.
$ENV{'SCRIPT_NAME'} = $pool->{statuspath};
$ENV{'SCRIPT_FILENAME'} = $pool->{statuspath};
$ENV{'QUERY_STRING'} = "json";
$ENV{'REQUEST_METHOD'} = "GET";
my $sh = sprintf("cgi-fcgi -bind -connect %s", $pool->{socket});
my $out_str = `$sh`;
if ($? != 0) {
printf "cgi-fcgi returned %d\n", $? >> 8;
} else {
# Strip header lines:
my @out_lines = split /\n/, $out_str;
while (@out_lines && $out_lines[0] !~ /^\r$/) {
shift @out_lines;
}
shift @out_lines;
$pool->{values} = decode_json(join "\n", @out_lines);
}
push @pools, $pool;
}
closedir($dh);
}
sort { $a->{pool} cmp $b->{pool}; } @pools;
if ($ARGV[0] eq "config") {
print <<"EOF";
multigraph phpfpm_connections
graph_title PHP-FPM Accepted Connections
graph_args --base 1024 -l 0
graph_vlabel Connections
graph_category PHP
graph_info Shows the number of accepted connections to the PHP-FPM pool.
EOF
foreach my $pool (@pools) {
my $poolname = $pool->{pool};
if (grep { $_ eq $poolname } @cfg_hide_pools) {
next;
}
print <<"EOF";
${poolname}_connections.label $poolname
${poolname}_connections.type DERIVE
${poolname}_connections.draw AREASTACK
${poolname}_connections.min 0
EOF
}
print <<"EOF";
multigraph phpfpm_active_processes
graph_title PHP-FPM Active Processes
graph_args --base 1000 -l 0
graph_vlabel Processes
graph_category PHP
graph_info Shows the number of PHP-FPM processes.
EOF
foreach my $pool (@pools) {
my $poolname = $pool->{pool};
if (grep { $_ eq $poolname } @cfg_hide_pools) {
next;
}
print <<"EOF";
${poolname}_active_processes.label $poolname
${poolname}_active_processes.type GAUGE
${poolname}_active_processes.draw AREASTACK
${poolname}_active_processes.min 0
EOF
}
print <<"EOF";
multigraph phpfpm_max_active_processes
graph_title PHP-FPM Max Active Processes
graph_args --base 1000 -l 0
graph_vlabel Max Processes
graph_category PHP
graph_info Shows the number of max PHP-FPM processes reached.
EOF
foreach my $pool (@pools) {
my $poolname = $pool->{pool};
if (grep { $_ eq $poolname } @cfg_hide_pools) {
next;
}
print <<"EOF";
${poolname}_max_active_processes.label $poolname
${poolname}_max_active_processes.type GAUGE
${poolname}_max_active_processes.draw AREASTACK
${poolname}_max_active_processes.min 0
EOF
}
exit 0;
}
my $out = "";
print "multigraph phpfpm_connections\n";
foreach my $pool (@pools) {
my $poolname = $pool->{pool};
if (grep { $_ eq $poolname } @cfg_hide_pools) {
next;
}
printf("%s_connections.value %d\n",
$pool->{pool},
$pool->{values}->{"accepted conn"});
}
print "multigraph phpfpm_active_processes\n";
foreach my $pool (@pools) {
my $poolname = $pool->{pool};
if (grep { $_ eq $poolname } @cfg_hide_pools) {
next;
}
printf("%s_active_processes.value %d\n",
$pool->{pool},
$pool->{values}->{"active processes"});
}
print "multigraph phpfpm_max_active_processes\n";
foreach my $pool (@pools) {
my $poolname = $pool->{pool};
if (grep { $_ eq $poolname } @cfg_hide_pools) {
next;
}
printf("%s_max_active_processes.value %d\n",
$pool->{pool},
$pool->{values}->{"max active processes"});
}
exit 0;
sub warning
{
my $msg = shift;
my (@args) = @_;
printf STDERR "WARNING: %s\n", sprintf($msg, @args);
}