From d9ee77c6570ade50035982ebe4ae06e61df1247d Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 25 Jan 2021 11:04:45 +0100 Subject: [PATCH] enhance php --- .../apps/php/apc/web/mode/filecache.pm | 216 +++++++++--------- .../apps/php/apc/web/mode/memory.pm | 38 +-- .../apps/php/fpm/web/mode/usage.pm | 87 +++---- 3 files changed, 173 insertions(+), 168 deletions(-) diff --git a/centreon-plugins/apps/php/apc/web/mode/filecache.pm b/centreon-plugins/apps/php/apc/web/mode/filecache.pm index 29628f17e..00596aeb9 100644 --- a/centreon-plugins/apps/php/apc/web/mode/filecache.pm +++ b/centreon-plugins/apps/php/apc/web/mode/filecache.pm @@ -27,104 +27,11 @@ use warnings; use centreon::plugins::http; use Digest::MD5 qw(md5_hex); -sub set_counters { - my ($self, %options) = @_; - - $self->{maps_counters_type} = [ - { name => 'fcache', type => 0, cb_prefix_output => 'prefix_output' } - ]; - - $self->{maps_counters}->{fcache} = [ - { label => 'request-rate', nlabel => 'requests.persecond', set => { - key_values => [ { name => 'rr' } ], - output_template => 'Request Rate (global): %.2f', - perfdatas => [ - { label => 'request_rate', template => '%.2f', - unit => 'r/s', min => 0 } - ] - } - }, - { label => 'request-rate-now', nlabel => 'requests.now.persecond', set => { - key_values => [ { name => 'hits', diff => 1 }, { name => 'misses', diff => 1 } ], - closure_custom_calc => $self->can('custom_rr_calc'), - output_template => 'Request Rate : %.2f', output_error_template => 'Request Rate : %s', - output_use => 'rr_now', threshold_use => 'rr_now', - perfdatas => [ - { label => 'request_rate_now', template => '%.2f', - unit => 'r/s', min => 0 } - ] - } - }, - { label => 'hit-rate', nlabel => 'hits.persecond', set => { - key_values => [ { name => 'hr' } ], - output_template => 'Hit Rate (global): %.2f', - perfdatas => [ - { label => 'hit_rate',template => '%.2f', - unit => 'r/s', min => 0 } - ] - } - }, - { label => 'hit-rate-now', nlabel => 'hits.persecond', set => { - key_values => [ { name => 'hits', diff => 1 } ], - closure_custom_calc => $self->can('custom_hr_calc'), - output_template => 'Hit Rate : %.2f', output_error_template => 'Hit Rate : %s', - output_use => 'hr_now', threshold_use => 'hr_now', - perfdatas => [ - { label => 'hit_rate_now', template => '%.2f', - unit => 'r/s', min => 0 } - ] - } - }, - { label => 'miss-rate', nlabel => 'misses.persecond', set => { - key_values => [ { name => 'mr' } ], - output_template => 'Miss Rate (global): %.2f', - perfdatas => [ - { label => 'miss_rate',template => '%.2f', - unit => 'r/s', min => 0 } - ] - } - }, - { label => 'miss-rate-now', nlabel => 'misses.now.persecond', set => { - key_values => [ { name => 'misses', diff => 1 } ], - closure_custom_calc => $self->can('custom_mr_calc'), - output_template => 'Miss Rate : %.2f', output_error_template => 'Miss Rate : %s', - output_use => 'mr_now', threshold_use => 'mr_now', - perfdatas => [ - { label => 'miss_rate_now', value => 'mr_now', template => '%.2f', - unit => 'r/s', min => 0 } - ] - } - }, - { label => 'hit-percent', nlabel => 'hits.percentage', set => { - key_values => [ { name => 'hits' }, { name => 'misses' } ], - closure_custom_calc => $self->can('custom_hit_percent_calc'), - output_template => 'Hit Ratio (global) : %.2f %%', output_error_template => 'Hit Ratio (global): %s', - output_use => 'hit_ratio', threshold_use => 'hit_ratio', - perfdatas => [ - { label => 'hit_ratio', value => 'hit_ratio', template => '%.2f', - unit => '%', min => 0, max => 100 } - ] - } - }, - { label => 'hit-percent-now', nlabel => 'hits.now.percentage', set => { - key_values => [ { name => 'hits', diff => 1 }, { name => 'misses', diff => 1 } ], - closure_custom_calc => $self->can('custom_hit_percent_now_calc'), - output_template => 'Hit Ratio : %.2f %%', output_error_template => 'Hit Ratio : %s', - output_use => 'hit_ratio_now', threshold_use => 'hit_ratio_now', - perfdatas => [ - { label => 'hit_ratio_now', value => 'hit_ratio_now', template => '%.2f', - unit => '%', min => 0, max => 100 } - ] - } - } - ]; -} - sub custom_rr_calc { my ($self, %options) = @_; my $total = ($options{new_datas}->{$self->{instance} . '_hits'} - $options{old_datas}->{$self->{instance} . '_hits'}) + ($options{new_datas}->{$self->{instance} . '_misses'} - $options{old_datas}->{$self->{instance} . '_misses'}); - + if ($total == 0) { $self->{error_msg} = "skipped"; return -2; @@ -191,7 +98,100 @@ sub custom_hit_percent_calc { sub prefix_output { my ($self, %options) = @_; - return "Apc File Cache Information "; + return 'Apc File Cache Information '; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'fcache', type => 0, cb_prefix_output => 'prefix_output' } + ]; + + $self->{maps_counters}->{fcache} = [ + { label => 'request-rate', nlabel => 'filecache.requests.persecond', set => { + key_values => [ { name => 'rr' } ], + output_template => 'Request Rate (global): %.2f', + perfdatas => [ + { label => 'request_rate', template => '%.2f', + unit => 'r/s', min => 0 } + ] + } + }, + { label => 'request-rate-now', nlabel => 'filecache.requests.now.persecond', set => { + key_values => [ { name => 'hits', diff => 1 }, { name => 'misses', diff => 1 } ], + closure_custom_calc => $self->can('custom_rr_calc'), + output_template => 'Request Rate : %.2f', output_error_template => 'Request Rate : %s', + output_use => 'rr_now', threshold_use => 'rr_now', + perfdatas => [ + { label => 'request_rate_now', template => '%.2f', + unit => 'r/s', min => 0 } + ] + } + }, + { label => 'hit-rate', nlabel => 'filecache.hits.persecond', set => { + key_values => [ { name => 'hr' } ], + output_template => 'Hit Rate (global): %.2f', + perfdatas => [ + { label => 'hit_rate',template => '%.2f', + unit => 'r/s', min => 0 } + ] + } + }, + { label => 'hit-rate-now', nlabel => 'filecache.hits.now.persecond', set => { + key_values => [ { name => 'hits', diff => 1 } ], + closure_custom_calc => $self->can('custom_hr_calc'), + output_template => 'Hit Rate : %.2f', output_error_template => 'Hit Rate : %s', + output_use => 'hr_now', threshold_use => 'hr_now', + perfdatas => [ + { label => 'hit_rate_now', template => '%.2f', + unit => 'r/s', min => 0 } + ] + } + }, + { label => 'miss-rate', nlabel => 'filecache.misses.persecond', set => { + key_values => [ { name => 'mr' } ], + output_template => 'Miss Rate (global): %.2f', + perfdatas => [ + { label => 'miss_rate',template => '%.2f', + unit => 'r/s', min => 0 } + ] + } + }, + { label => 'miss-rate-now', nlabel => 'filecache.misses.now.persecond', set => { + key_values => [ { name => 'misses', diff => 1 } ], + closure_custom_calc => $self->can('custom_mr_calc'), + output_template => 'Miss Rate : %.2f', output_error_template => 'Miss Rate : %s', + output_use => 'mr_now', threshold_use => 'mr_now', + perfdatas => [ + { label => 'miss_rate_now', value => 'mr_now', template => '%.2f', + unit => 'r/s', min => 0 } + ] + } + }, + { label => 'hit-percent', nlabel => 'filecache.hits.percentage', set => { + key_values => [ { name => 'hits' }, { name => 'misses' } ], + closure_custom_calc => $self->can('custom_hit_percent_calc'), + output_template => 'Hit Ratio (global) : %.2f %%', output_error_template => 'Hit Ratio (global): %s', + output_use => 'hit_ratio', threshold_use => 'hit_ratio', + perfdatas => [ + { label => 'hit_ratio', value => 'hit_ratio', template => '%.2f', + unit => '%', min => 0, max => 100 } + ] + } + }, + { label => 'hit-percent-now', nlabel => 'filecache.hits.now.percentage', set => { + key_values => [ { name => 'hits', diff => 1 }, { name => 'misses', diff => 1 } ], + closure_custom_calc => $self->can('custom_hit_percent_now_calc'), + output_template => 'Hit Ratio : %.2f %%', output_error_template => 'Hit Ratio : %s', + output_use => 'hit_ratio_now', threshold_use => 'hit_ratio_now', + perfdatas => [ + { label => 'hit_ratio_now', value => 'hit_ratio_now', template => '%.2f', + unit => '%', min => 0, max => 100 } + ] + } + } + ]; } sub new { @@ -200,15 +200,15 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => "/apc.php" }, - "credentials" => { name => 'credentials' }, - "basic" => { name => 'basic' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "timeout:s" => { name => 'timeout', default => 30 }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port', }, + 'proto:s' => { name => 'proto' }, + 'urlpath:s' => { name => 'url_path', default => "/apc.php" }, + 'credentials' => { name => 'credentials' }, + 'basic' => { name => 'basic' }, + 'username:s' => { name => 'username' }, + 'password:s' => { name => 'password' }, + 'timeout:s' => { name => 'timeout', default => 30 } }); $self->{http} = centreon::plugins::http->new(%options); @@ -225,9 +225,9 @@ sub check_options { sub manage_selection { my ($self, %options) = @_; - + my $webcontent = $self->{http}->request(); - + $self->{fcache} = {}; $self->{fcache}->{hits} = $webcontent =~ /File Cache Information.*?Hits.*?(\d+)/msi ? $1 : undef; $self->{fcache}->{misses} = $webcontent =~ /File Cache Information.*?Misses.*?(\d+)/msi ? $1 : undef; @@ -235,8 +235,8 @@ sub manage_selection { $self->{fcache}->{hr} = $webcontent =~ /File Cache Information.*?Hit Rate.*?([0-9\.]+)/msi ? $1 : undef; $self->{fcache}->{mr} = $webcontent =~ /File Cache Information.*?Miss Rate.*?([0-9\.]+)/msi ? $1 : undef; $self->{fcache}->{ir} = $webcontent =~ /File Cache Information.*?Insert Rate.*?([0-9\.]+)/msi ? $1 : undef; - - $self->{cache_name} = "apc_" . $self->{mode} . '_' . $self->{option_results}->{hostname} . '_' . $self->{http}->get_port() . '_' . + + $self->{cache_name} = 'apc_' . $self->{mode} . '_' . $self->{option_results}->{hostname} . '_' . $self->{http}->get_port() . '_' . (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); } diff --git a/centreon-plugins/apps/php/apc/web/mode/memory.pm b/centreon-plugins/apps/php/apc/web/mode/memory.pm index 864dce68d..fe6e11d63 100644 --- a/centreon-plugins/apps/php/apc/web/mode/memory.pm +++ b/centreon-plugins/apps/php/apc/web/mode/memory.pm @@ -45,10 +45,12 @@ sub custom_used_output { my ($used_value, $used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); my ($free_value, $free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); - return sprintf("Memory Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", - $total_value . " " . $total_unit, - $used_value . " " . $used_unit, $self->{result_values}->{used_prct}, - $free_value . " " . $free_unit, $self->{result_values}->{free_prct}); + return sprintf( + "Memory Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $total_value . " " . $total_unit, + $used_value . " " . $used_unit, $self->{result_values}->{used_prct}, + $free_value . " " . $free_unit, $self->{result_values}->{free_prct} + ); } sub prefix_output { @@ -73,8 +75,8 @@ sub set_counters { output_error_template => 'Memory Usage: %s', perfdatas => [ { label => 'used', template => '%d', - unit => 'B', min => 0, max => 'total', threshold_total => 'total' }, - + unit => 'B', min => 0, max => 'total', threshold_total => 'total' } + ] } }, { label => 'fragmentation', nlabel => 'memory.fragmentation.percentage', set => { @@ -96,19 +98,19 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => "/apc.php" }, - "credentials" => { name => 'credentials' }, - "basic" => { name => 'basic' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "timeout:s" => { name => 'timeout', default => 30 }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port', }, + 'proto:s' => { name => 'proto' }, + 'urlpath:s' => { name => 'url_path', default => "/apc.php" }, + 'credentials' => { name => 'credentials' }, + 'basic' => { name => 'basic' }, + 'username:s' => { name => 'username' }, + 'password:s' => { name => 'password' }, + 'timeout:s' => { name => 'timeout', default => 30 }, }); - + $self->{http} = centreon::plugins::http->new(%options); - + return $self; } @@ -123,7 +125,7 @@ sub manage_selection { my ($self, %options) = @_; my $webcontent = $self->{http}->request(); - + my ($free, $used); if ($webcontent =~ /Memory Usage.*?Free:.*?([0-9\.]+)\s*(\S*)/msi) { $free = centreon::plugins::misc::convert_bytes(value => $1, unit => $2); diff --git a/centreon-plugins/apps/php/fpm/web/mode/usage.pm b/centreon-plugins/apps/php/fpm/web/mode/usage.pm index b5f18c753..6e62f46d7 100644 --- a/centreon-plugins/apps/php/fpm/web/mode/usage.pm +++ b/centreon-plugins/apps/php/fpm/web/mode/usage.pm @@ -29,7 +29,7 @@ use Digest::MD5 qw(md5_hex); sub custom_active_calc { my ($self, %options) = @_; - + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; $self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'}; $self->{result_values}->{active_prct} = $self->{result_values}->{active} * 100 / $self->{result_values}->{total}; @@ -39,24 +39,29 @@ sub custom_active_calc { sub custom_active_output { my ($self, %options) = @_; - return sprintf("Active processes: %s (%.2f%%)", - $self->{result_values}->{active}, - $self->{result_values}->{active_prct}); + return sprintf( + 'active processes: %s (%.2f%%)', + $self->{result_values}->{active}, + $self->{result_values}->{active_prct} + ); } sub custom_active_perfdata { my ($self, %options) = @_; - $self->{output}->perfdata_add(label => 'active', - value => $self->{result_values}->{active}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1), - min => 0, max => $self->{result_values}->{total}); + $self->{output}->perfdata_add( + label => 'active', + nlabel => $self->{nlabel}, + value => $self->{result_values}->{active}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1), + min => 0, max => $self->{result_values}->{total} + ); } sub custom_idle_calc { my ($self, %options) = @_; - + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; $self->{result_values}->{idle} = $options{new_datas}->{$self->{instance} . '_idle'}; $self->{result_values}->{idle_prct} = $self->{result_values}->{idle} * 100 / $self->{result_values}->{total}; @@ -66,36 +71,41 @@ sub custom_idle_calc { sub custom_idle_output { my ($self, %options) = @_; - return sprintf("Idle processes: %s (%.2f%%)", - $self->{result_values}->{idle}, - $self->{result_values}->{idle_prct}); + return sprintf( + 'idle processes: %s (%.2f%%)', + $self->{result_values}->{idle}, + $self->{result_values}->{idle_prct} + ); } sub custom_idle_perfdata { my ($self, %options) = @_; - $self->{output}->perfdata_add(label => 'idle', - value => $self->{result_values}->{idle}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1), - min => 0, max => $self->{result_values}->{total}); + $self->{output}->perfdata_add( + label => 'idle', + nlabel => $self->{nlabel}, + value => $self->{result_values}->{idle}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1), + min => 0, max => $self->{result_values}->{total} + ); } sub prefix_output { my ($self, %options) = @_; - return "php-fpm "; + return 'php-fpm '; } sub set_counters { my ($self, %options) = @_; - + $self->{maps_counters_type} = [ - { name => 'fpm', type => 0, cb_prefix_output => 'prefix_output' } + { name => 'fpm', type => 0, cb_prefix_output => 'prefix_output', skipped_code => { -10 => 1 } } ]; - + $self->{maps_counters}->{fpm} = [ - { label => 'active-processes', nlabel => 'processes.active.count', set => { + { label => 'active-processes', nlabel => 'fpm.processes.active.count', set => { key_values => [ { name => 'active' }, { name => 'total' } ], closure_custom_calc => $self->can('custom_active_calc'), closure_custom_output => $self->can('custom_active_output'), @@ -103,7 +113,7 @@ sub set_counters { closure_custom_perfdata => => $self->can('custom_active_perfdata') } }, - { label => 'idle-processes', nlabel => 'processes.idle.count', set => { + { label => 'idle-processes', nlabel => 'fpm.processes.idle.count', set => { key_values => [ { name => 'idle' }, { name => 'total' } ], closure_custom_calc => $self->can('custom_idle_calc'), closure_custom_output => $self->can('custom_idle_output'), @@ -111,9 +121,9 @@ sub set_counters { closure_custom_perfdata => => $self->can('custom_idle_perfdata') } }, - { label => 'listen-queue', nlabel => 'queue.listen.count', set => { + { label => 'listen-queue', nlabel => 'fpm.queue.listen.count', set => { key_values => [ { name => 'listen_queue' }, { name => 'max_listen_queue' } ], - output_template => 'Listen queue : %s', + output_template => 'listen queue: %s', output_use => 'listen_queue', threshold_use => 'listen_queue', perfdatas => [ { label => 'listen_queue', template => '%s', @@ -121,9 +131,9 @@ sub set_counters { ] } }, - { label => 'requests', nlabel => 'requests.count', set => { + { label => 'requests', nlabel => 'fpm.requests.persecond', set => { key_values => [ { name => 'request', per_second => 1 } ], - output_template => 'Requests : %.2f/s', + output_template => 'requests: %.2f/s', perfdatas => [ { label => 'requests', template => '%.2f', unit => '/s', min => 0 } ] @@ -146,7 +156,7 @@ sub new { 'basic' => { name => 'basic' }, 'username:s' => { name => 'username' }, 'password:s' => { name => 'password' }, - 'timeout:s' => { name => 'timeout', default => 5 }, + 'timeout:s' => { name => 'timeout', default => 5 } }); $self->{http} = centreon::plugins::http->new(%options); @@ -163,19 +173,18 @@ sub check_options { sub manage_selection { my ($self, %options) = @_; - + my $webcontent = $self->{http}->request(); - $self->{fpm} = { request => undef, listen_queue => undef, max_listen_queue => undef, - idle => undef, active => undef, total => undef }; + $self->{fpm} = {}; $self->{fpm}->{request} = $1 if ($webcontent =~ /accepted\s+conn:\s+(\d+)/msi); $self->{fpm}->{listen_queue} = $1 if ($webcontent =~ /listen\s+queue:\s+(\d+)/msi); $self->{fpm}->{max_listen_queue} = $1 if ($webcontent =~ /max\s+listen\s+queue:\s+(\d+)/msi); $self->{fpm}->{idle} = $1 if ($webcontent =~ /idle\s+processes:\s+(\d+)/msi); $self->{fpm}->{active} = $1 if ($webcontent =~ /active\s+processes:\s+(\d+)/msi); $self->{fpm}->{total} = $1 if ($webcontent =~ /total\s+processes:\s+(\d+)/msi); - - $self->{cache_name} = "php_fpm_" . $self->{mode} . '_' . $self->{option_results}->{hostname} . '_' . $self->{http}->get_port() . '_' . + + $self->{cache_name} = 'php_fpm_' . $self->{mode} . '_' . $self->{option_results}->{hostname} . '_' . $self->{http}->get_port() . '_' . (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); } @@ -229,18 +238,12 @@ Specify this option if you access server-status page over hidden basic authentic Threshold for HTTP timeout (Default: 5) -=item B<--warning-*> +=item B<--warning-*> B<--critical-*> -Threshold warning. +Thresholds. Can be: 'active-processes' (%), 'idle-processes' (%), 'listen-queue', 'requests'. -=item B<--critical-*> - -Threshold critical. -Can be: 'active-processes' (%), 'idle-processes' (%), -'listen-queue', 'requests'. - =back =cut