From 3cd86ea9babaddfc1b5cc7adef591483cfb3f5ac Mon Sep 17 00:00:00 2001 From: me2digital Date: Mon, 14 Jan 2019 18:29:21 +0100 Subject: [PATCH] Add auth credentials for status url --- phpfpm_connections | 40 ++++++++++- phpfpm_connections_ | 141 ++++++++++++++++++++++++++++++++++++ phpfpm_status | 38 +++++++++- phpfpm_status_ | 169 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 382 insertions(+), 6 deletions(-) create mode 100755 phpfpm_connections_ create mode 100755 phpfpm_status_ diff --git a/phpfpm_connections b/phpfpm_connections index 16227bc..3c8ace5 100644 --- a/phpfpm_connections +++ b/phpfpm_connections @@ -22,6 +22,13 @@ configuration to override the defaults. These are the defaults: env.url http://127.0.0.1/status env.ports 80 +If your url requires authentication then you can add the following paramters +to the plugin configuration + + env.user user001 + env.pass pass001 + env.realm MyPHP-FPM-Realm + =head1 PARAMETERS: config (required) @@ -40,16 +47,26 @@ Copyright TJ Stein 2010 http://constantshift.com =cut +#use LWP::ConsoleLogger::Everywhere (); my $ret = undef; +my $name = ''; if (! eval "require LWP::UserAgent;") { $ret = "LWP::UserAgent not found"; } -my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/status"; +if (! eval "require URI;") +{ + $ret = "URI not found"; +} + +my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/status"; my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80); +my $USER = exists $ENV{'user'} ? $ENV{'user'} : undef; +my $PASS = exists $ENV{'pass'} ? $ENV{'pass'} : undef; +my $REALM = exists $ENV{'realm'} ? $ENV{'realm'} : undef; if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) { @@ -64,6 +81,12 @@ if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) my @badports; foreach my $port (@PORTS) { my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + my $response = $ua->request(HTTP::Request->new('GET',$url)); push @badports, $port unless $response->is_success and $response->content =~ /^accepted conn:/im; } @@ -76,10 +99,14 @@ if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) } } +if ($0 =~ /phpfpm_connections_(.+)*$/) { + $name = = $1; +} + if ( defined $ARGV[0] and $ARGV[0] eq "config" ) { - print('graph_title PHP-FPM Accepted Connections -graph_args --base 1024 -l 0 + print("graph_title PHP-FPM $name Accepted Connections"); + print('graph_args --base 1024 -l 0 graph_vlabel Connections graph_category PHP graph_info Plugin created by TJ Stein @@ -95,7 +122,14 @@ accepted.min 0 foreach my $port (@PORTS) { my $ua = LWP::UserAgent->new(timeout => 30); + my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + my $response = $ua->request(HTTP::Request->new('GET',$url)); if ($response->content =~ /accepted conn:\s+([0-9\.]+)/im) { print "accepted.value $1\n"; diff --git a/phpfpm_connections_ b/phpfpm_connections_ new file mode 100755 index 0000000..1bbfbed --- /dev/null +++ b/phpfpm_connections_ @@ -0,0 +1,141 @@ +#!/usr/bin/perl +# -*- perl -*- + +=encoding utf8 + +=head1 NAME + +phpfpm_connections - Munin plugin to monitor the number of accepted connections to PHP-FPM. + +=head1 CONFIGURATION + +For this plugin, you will need to enable the status feature +included in versions 5.3.2+ of PHP-FPM. The directive can be +found in the php-fpm.conf file: + + pm.status_path = /status + +You might need to specify connection parameters in the plugin +configuration to override the defaults. These are the defaults: + + [phpfpm_*] + env.url http://127.0.0.1/status + env.ports 80 + +If your url requires authentication then you can add the following paramters +to the plugin configuration + + env.user user001 + env.pass pass001 + env.realm MyPHP-FPM-Realm + +=head1 PARAMETERS: + + config (required) + autoconf (optional - used by munin-config) + +=over + +=head1 LICENSE + +Copyright TJ Stein 2010 http://constantshift.com + +=head1 MAGICK MARKERS + + #%# family=auto + #%# capabilities=autoconf + +=cut + +#use LWP::ConsoleLogger::Everywhere (); + +my $ret = undef; +my $name = ''; + +if (! eval "require LWP::UserAgent;") +{ + $ret = "LWP::UserAgent not found"; +} + +if (! eval "require URI;") +{ + $ret = "URI not found"; +} + +my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/status"; +my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80); +my $USER = exists $ENV{'user'} ? $ENV{'user'} : undef; +my $PASS = exists $ENV{'pass'} ? $ENV{'pass'} : undef; +my $REALM = exists $ENV{'realm'} ? $ENV{'realm'} : undef; + +if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) +{ + if ($ret) + { + print "no ($ret)\n"; + exit 1; + } + + my $ua = LWP::UserAgent->new(timeout => 30); + + my @badports; + foreach my $port (@PORTS) { + my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + + my $response = $ua->request(HTTP::Request->new('GET',$url)); + push @badports, $port unless $response->is_success and $response->content =~ /^accepted conn:/im; + } + if (@badports) { + print "no (phpfpm-status)\n"; + exit 1; + } else { + print "yes\n"; + exit 0; + } +} + +if ($0 =~ /phpfpm_connections_(.+)*$/) { + $name = $1; +} + +if ( defined $ARGV[0] and $ARGV[0] eq "config" ) +{ + print("graph_title PHP-FPM $name Accepted Connections\n"); + print('graph_args --base 1024 -l 0 +graph_vlabel Connections +graph_category PHP +graph_info Plugin created by TJ Stein +accepted.label Accepted +accepted.draw AREA +accepted.type DERIVE +accepted.min 0 +'); + + exit 0; +} + +foreach my $port (@PORTS) +{ + my $ua = LWP::UserAgent->new(timeout => 30); + + my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + + my $response = $ua->request(HTTP::Request->new('GET',$url)); + if ($response->content =~ /accepted conn:\s+([0-9\.]+)/im) { + print "accepted.value $1\n"; + } else { + print "accepted.value U\n"; + } +} + +# vim:syntax=perl diff --git a/phpfpm_status b/phpfpm_status index f996d30..3cb3669 100644 --- a/phpfpm_status +++ b/phpfpm_status @@ -27,6 +27,13 @@ configuration to override the defaults. These are the defaults: Critical and warning are optional settings and are by default triggered at 60% and 80% respectively of the total capacity. +If your url requires authentication then you can add the following paramters +to the plugin configuration + + env.user user001 + env.pass pass001 + env.realm MyPHP-FPM-Realm + =head1 PARAMETERS: config (required) @@ -53,8 +60,17 @@ if (! eval "require LWP::UserAgent;") $ret = "LWP::UserAgent not found"; } -my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/status"; +if (! eval "require URI;") +{ + $ret = "URI not found"; +} + +my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/status"; my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80); +my $USER = exists $ENV{'user'} ? $ENV{'user'} : undef; +my $PASS = exists $ENV{'pass'} ? $ENV{'pass'} : undef; +my $REALM = exists $ENV{'realm'} ? $ENV{'realm'} : undef; + if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) { @@ -69,6 +85,12 @@ if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) my @badports; foreach my $port (@PORTS) { my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + my $response = $ua->request(HTTP::Request->new('GET',$url)); push @badports, $port unless $response->is_success and $response->content =~ /^accepted conn:/im; } @@ -81,10 +103,14 @@ if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) } } +if ($0 =~ /phpfpm_connections_(.+)*$/) { + $name = = $1; +} + if ( defined $ARGV[0] and $ARGV[0] eq "config" ) { - print('graph_title PHP-FPM Status -graph_args --base 1024 -l 0 + print("graph_title PHP-FPM $name Status"); + print('graph_args --base 1024 -l 0 graph_vlabel Connections graph_category PHP graph_order active idle total @@ -104,6 +130,12 @@ foreach my $port (@PORTS) { my $ua = LWP::UserAgent->new(timeout => 30); my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + my $response = $ua->request(HTTP::Request->new('GET',$url)); if ($response->content =~ /idle processes:\s+([0-9\.]+)/im) { print "idle.value $1\n"; diff --git a/phpfpm_status_ b/phpfpm_status_ new file mode 100755 index 0000000..c62048d --- /dev/null +++ b/phpfpm_status_ @@ -0,0 +1,169 @@ +#!/usr/bin/perl +# -*- perl -*- + +=encoding utf8 + +=head1 NAME + +phpfpm_status - Munin plugin to monitor the staus of PHP-FPM. + +=head1 CONFIGURATION + +For this plugin, you will need to enable the status feature +included in versions 5.3.2+ of PHP-FPM. The directive can be +found in the php5-fpm.conf file: + + pm.status_path = /status + +You might need to specify connection parameters in the plugin +configuration to override the defaults. These are the defaults: + + [phpfpm_*] + env.url http://127.0.0.1/status + env.ports 80 + env.warning 0.6 + env.critical 0.8 + +Critical and warning are optional settings and are by default +triggered at 60% and 80% respectively of the total capacity. + +If your url requires authentication then you can add the following paramters +to the plugin configuration + + env.user user001 + env.pass pass001 + env.realm MyPHP-FPM-Realm + +=head1 PARAMETERS: + + config (required) + autoconf (optional - used by munin-config) + +=over + +=head1 LICENSE + +Copyright TJ Stein 2010 http://constantshift.com + +=head1 MAGICK MARKERS + + #%# family=auto + #%# capabilities=autoconf + +=cut + + +my $ret = undef; + +if (! eval "require LWP::UserAgent;") +{ + $ret = "LWP::UserAgent not found"; +} + +if (! eval "require URI;") +{ + $ret = "URI not found"; +} + +my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/status"; +my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80); +my $USER = exists $ENV{'user'} ? $ENV{'user'} : undef; +my $PASS = exists $ENV{'pass'} ? $ENV{'pass'} : undef; +my $REALM = exists $ENV{'realm'} ? $ENV{'realm'} : undef; + + +if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) +{ + if ($ret) + { + print "no ($ret)\n"; + exit 1; + } + + my $ua = LWP::UserAgent->new(timeout => 30); + + my @badports; + foreach my $port (@PORTS) { + my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + + my $response = $ua->request(HTTP::Request->new('GET',$url)); + push @badports, $port unless $response->is_success and $response->content =~ /^accepted conn:/im; + } + if (@badports) { + print "no (phpfpm-status)\n"; + exit 1; + } else { + print "yes\n"; + exit 0; + } +} + +if ($0 =~ /phpfpm_status_(.+)*$/) { + $name = $1; +} + +if ( defined $ARGV[0] and $ARGV[0] eq "config" ) +{ + print("graph_title PHP-FPM $name Status"); + print('graph_args --base 1024 -l 0 +graph_vlabel Connections +graph_category PHP +graph_order active idle total +graph_info Plugin created by TJ Stein +active.label Active +active.draw AREA +idle.label Idle +idle.draw STACK +total.label Total +total.draw LINE1 +'); + + exit 0; +} + +foreach my $port (@PORTS) +{ + my $ua = LWP::UserAgent->new(timeout => 30); + my $url = sprintf $URL, $port; + + if ( defined $USER and defined $PASS and defined $REALM) { + my $uri = new URI ($url); + $ua->credentials($uri->host.':'.$uri->port,$REALM,$USER,$PASS) + } + + my $response = $ua->request(HTTP::Request->new('GET',$url)); + if ($response->content =~ /idle processes:\s+([0-9\.]+)/im) { + print "idle.value $1\n"; + } else { + print "idle.value U\n"; + } + if ($response->content =~ /active processes:\s+([0-9\.]+)/im) { + print "active.value $1\n"; + } else { + print "active.value U\n"; + } + if ($response->content =~ /total processes:\s+([0-9\.]+)/im) { + print "total.value $1\n"; + } else { + print "total.value U\n"; + } + + my $total = $1; + my $warning_threshold = exists $ENV{warning} ? $ENV{warning} : '0.60'; + my $critical_threshold = exists $ENV{critical} ? $ENV{critical} : '0.80'; + + my $warning_level = int($total*$warning_threshold); + my $critical_level = int($total*$critical_threshold); + + print ("active.warning $warning_level\n"); + print ("active.critical $critical_level\n"); + + +} + +# vim:syntax=perl