refactor mode to be compliant with varnish 4

This commit is contained in:
Sims24 2017-05-23 15:57:22 +02:00
parent ee53e43879
commit d944863c8d
3 changed files with 273 additions and 472 deletions

View File

@ -1,5 +1,5 @@
# #
# Copyright 2017 Centreon (http://www.centreon.com/) # Copyright 2016 Centreon (http://www.centreon.com/)
# #
# Centreon is a full-fledged industry-strength solution that meets # Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for # the needs in IT infrastructure and application monitoring for
@ -20,80 +20,115 @@
package apps::varnish::local::mode::backend; package apps::varnish::local::mode::backend;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use centreon::plugins::misc; use strict;
use centreon::plugins::statefile; use warnings;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use JSON;
my $maps_counters = { sub set_counters {
backend_conn => { thresholds => { my ($self, %options) = @_;
warning_conn => { label => 'warning-conn', exit_value => 'warning' },
critical_conn => { label => 'critical-conn', exit_value => 'critical' }, $self->{maps_counters_type} = [
}, { name => 'backend', type => 0, skipped_code => { -10 => 1 } },
output_msg => 'Backend conn. success: %.2f', ];
factor => 1, unit => '', $self->{maps_counters}->{backend} = [
}, { label => 'conn', set => {
backend_unhealthy => { thresholds => { key_values => [ { name => 'backend_conn', diff => 1 } ],
warning_unhealthy => { label => 'warning-unhealthy', exit_value => 'warning' }, output_template => 'Backend con: %.2f/s', output_error_template => "Backend con: %s",
critical_unhealthy => { label => 'critical-unhealthy', exit_value => 'critical' }, per_second => 1,
}, perfdatas => [
output_msg => 'Backend conn. not attempted: %.2f', { label => 'backend_conn', value => 'backend_conn_per_second', template => '%.2f',
factor => 1, unit => '', min => 0 },
}, ],
backend_busy => { thresholds => { }
warning_busy => { label => 'warning-busy', exit_value => 'warning' }, },
critical_busy => { label => 'critical-busy', exit_value => 'critical' }, { label => 'unhealthy', set => {
}, key_values => [ { name => 'backend_unhealthy', diff => 1 } ],
output_msg => 'Backend conn. too many: %.2f', output_template => 'Backend unhealthy: %.2f/s', output_error_template => "Backend unhealthy: %s",
factor => 1, unit => '', per_second => 1,
}, perfdatas => [
backend_fail => { thresholds => { { label => 'backend_unhealthy', value => 'backend_unhealthy_per_second', template => '%.2f',
warning_fail => { label => 'warning-fail', exit_value => 'warning' }, min => 0 },
critical_fail => { label => 'critical-fail', exit_value => 'critical' }, ],
}, }
output_msg => 'Backend conn. failures: %.2f', },
factor => 1, unit => '', { label => 'busy', set => {
}, key_values => [ { name => 'backend_busy', diff => 1 } ],
backend_reuse => { thresholds => { output_template => 'Backend busy: %.2f/s', output_error_template => "Backend busy: %s",
warning_reuse => { label => 'warning-reuse', exit_value => 'warning' }, per_second => 1,
critical_reuse => { label => 'critical-reuse', exit_value => 'critical' }, perfdatas => [
}, { label => 'backend_miss', value => 'backend_miss_per_second', template => '%.2f',
output_msg => 'Backend conn. reuses: %.2f', min => 0 },
factor => 1, unit => '', ],
}, }
backend_toolate => { thresholds => { },
warning_toolate => { label => 'warning-toolate', exit_value => 'warning' }, { label => 'fail', set => {
critical_toolate => { label => 'critical-toolate', exit_value => 'critical' }, key_values => [ { name => 'backend_fail', diff => 1 } ],
}, output_template => 'Backend fail: %.2f/s', output_error_template => "Backend fail: %s",
output_msg => 'Backend conn. was closed: %.2f', per_second => 1,
factor => 1, unit => '', perfdatas => [
}, { label => 'backend_fail', value => 'backend_fail_per_second', template => '%.2f',
backend_recycle => { thresholds => { min => 0 },
warning_recycle => { label => 'warning-recycle', exit_value => 'warning' }, ],
critical_recycle => { label => 'critical-recycle', exit_value => 'critical' }, }
}, },
output_msg => 'Backend conn. recycles: %.2f', { label => 'reuse', set => {
factor => 1, unit => '', key_values => [ { name => 'backend_reuse', diff => 1 } ],
}, output_template => 'Backend reuse: %.2f/s', output_error_template => "Backend reuse: %s",
backend_retry => { thresholds => { per_second => 1,
warning_retry => { label => 'warning-retry', exit_value => 'warning' }, perfdatas => [
critical_retry => { label => 'critical-retry', exit_value => 'critical' }, { label => 'backend_reuse', value => 'backend_reuse_per_second', template => '%.2f',
}, min => 0 },
output_msg => 'Backend conn. retry: %.2f', ],
factor => 1, unit => '', }
}, },
backend_req => { thresholds => { { label => 'recycle', set => {
warning_req => { label => 'warning-req', exit_value => 'warning' }, key_values => [ { name => 'backend_recycle', diff => 1 } ],
critical_req => { label => 'critical-req', exit_value => 'critical' }, output_template => 'Backend recycle: %.2f/s', output_error_template => "Backend recycle: %s",
}, per_second => 1,
output_msg => 'Backend requests made: %.2f', perfdatas => [
factor => 1, unit => '', { label => 'backend_recycle', value => 'backend_recycle_per_second', template => '%.2f',
}, min => 0 },
}; ],
}
},
{ label => 'retry', set => {
key_values => [ { name => 'backend_retry', diff => 1 } ],
output_template => 'Backend retry: %.2f/s', output_error_template => "Backend retry: %s",
per_second => 1,
perfdatas => [
{ label => 'backend_retry', value => 'backend_retry_per_second', template => '%.2f',
min => 0 },
],
}
},
{ label => 'recycle', set => {
key_values => [ { name => 'backend_recycle', diff => 1 } ],
output_template => 'Backend recycle: %.2f/s', output_error_template => "Backend recycle: %s",
per_second => 1,
perfdatas => [
{ label => 'backend_recycle', value => 'backend_recycle_per_second', template => '%.2f',
min => 0 },
],
}
},
{ label => 'request', set => {
key_values => [ { name => 'backend_req', diff => 1 } ],
output_template => 'Backend requests: %.2f/s', output_error_template => "Backend requests: %s",
per_second => 1,
perfdatas => [
{ label => 'backend_req', value => 'backend_req_per_second', template => '%.2f',
min => 0 },
],
}
},
],
}
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $self->{version} = '1.0';
@ -108,39 +143,13 @@ sub new {
"sudo" => { name => 'sudo' }, "sudo" => { name => 'sudo' },
"command:s" => { name => 'command', default => 'varnishstat' }, "command:s" => { name => 'command', default => 'varnishstat' },
"command-path:s" => { name => 'command_path', default => '/usr/bin' }, "command-path:s" => { name => 'command_path', default => '/usr/bin' },
"command-options:s" => { name => 'command_options', default => ' -1 ' }, "command-options:s" => { name => 'command_options', default => ' -1 -j 2>&1' },
"command-options2:s" => { name => 'command_options2', default => ' 2>&1' },
}); });
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
$options{options}->add_options(arguments => {
$maps_counters->{$_}->{thresholds}->{$name}->{label} . ':s' => { name => $name },
});
};
};
$self->{instances_done} = {};
$self->{statefile_value} = centreon::plugins::statefile->new(%options);
return $self; return $self;
}; };
sub check_options { sub manage_selection {
my ($self, %options) = @_;
$self->SUPER::init(%options);
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
if (($self->{perfdata}->threshold_validate(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, value => $self->{option_results}->{$name})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong " . $maps_counters->{$_}->{thresholds}->{$name}->{label} . " threshold '" . $self->{option_results}->{$name} . "'.");
$self->{output}->option_exit();
};
};
};
$self->{statefile_value}->check_options(%options);
};
sub getdata {
my ($self, %options) = @_; my ($self, %options) = @_;
my $stdout = centreon::plugins::misc::execute(output => $self->{output}, my $stdout = centreon::plugins::misc::execute(output => $self->{output},
@ -148,86 +157,22 @@ sub getdata {
sudo => $self->{option_results}->{sudo}, sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command}, command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path}, command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options} . $self->{option_results}->{command_options2}); command_options => $self->{option_results}->{command_options});
#print $stdout;
foreach (split(/\n/, $stdout)) { # "MAIN.backend_hit": {"type": "MAIN", "value": 18437, "flag": "a", "description": "Cache hits"},
#client_conn 7390867 1.00 Client connections # "MAIN.backend_hitpass": {"type": "MAIN", "value": 3488, "flag": "a", "description": "Cache hits for pass"},
# - Symbolic entry name # "MAIN.backend_miss": {"type": "MAIN", "value": 5782, "flag": "a", "description": "Cache misses"},
# - Value my $json_data = decode_json($stdout);
# - Per-second average over process lifetime, or a period if the value can not be averaged
# - Descriptive text
if (/^(.\S*)\s*([0-9]*)\s*([0-9.]*)\s(.*)$/i) { $self->{cache_name} = "cache_varnish_" . $self->{mode} . '_' .
#print "FOUND: " . $1 . "=" . $2 . "\n"; (defined($self->{option_results}->{hostname}) ? md5_hex($self->{option_results}->{hostname}) : md5_hex('all'));
$self->{result}->{$1} = $2;
};
};
};
sub run { foreach my $counter (keys %{$json_data}) {
my ($self, %options) = @_; next if ($counter !~ /backend/);
my $value = $json_data->{$counter}->{value};
$self->getdata(); $counter =~ s/^([A-Z])+\.//;
$self->{backend}->{$counter} = $value;
$self->{statefile_value}->read(statefile => 'cache_apps_varnish' . '_' . $self->{mode} . '_' . (defined($self->{option_results}->{name}) ? md5_hex($self->{option_results}->{name}) : md5_hex('all')));
$self->{result}->{last_timestamp} = time();
my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp');
# Calculate
my $delta_time = $self->{result}->{last_timestamp} - $old_timestamp;
$delta_time = 1 if ($delta_time == 0); # One seconds ;)
foreach (keys %{$maps_counters}) {
#print $_ . "\n";
$self->{old_cache}->{$_} = $self->{statefile_value}->get(name => '$_'); # Get Data from Cache
$self->{old_cache}->{$_} = 0 if ( $self->{old_cache}->{$_} > $self->{result}->{$_} );
$self->{outputdata}->{$_} = ($self->{result}->{$_} - $self->{old_cache}->{$_}) / $delta_time;
};
# Write Cache if not there
$self->{statefile_value}->write(data => $self->{result});
if (!defined($old_timestamp)) {
$self->{output}->output_add(severity => 'OK',
short_msg => "Buffer creation...");
$self->{output}->display();
$self->{output}->exit();
} }
my @exits;
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
push @exits, $self->{perfdata}->threshold_check(value => $self->{outputdata}->{$_}, threshold => [ { label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, 'exit_litteral' => $maps_counters->{$_}->{thresholds}->{$name}->{exit_value} }]);
}
}
my $exit = $self->{output}->get_most_critical(status => [ @exits ]);
my $extra_label = '';
$extra_label = '_' . $instance_output if ($num > 1);
my $str_output = "";
my $str_append = '';
foreach (keys %{$maps_counters}) {
$str_output .= $str_append . sprintf($maps_counters->{$_}->{output_msg}, $self->{outputdata}->{$_} * $maps_counters->{$_}->{factor});
$str_append = ', ';
my ($warning, $critical);
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
$warning = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'warning');
$critical = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'critical');
}
$self->{output}->perfdata_add(label => $_ . $extra_label, unit => $maps_counters->{$_}->{unit},
value => sprintf("%.2f", $self->{outputdata}->{$_} * $maps_counters->{$_}->{factor}),
warning => $warning,
critical => $critical);
}
$self->{output}->output_add(severity => $exit,
short_msg => $str_output);
$self->{output}->display();
$self->{output}->exit();
}; };
@ -267,7 +212,7 @@ Parameter for Binary File (Default: ' -1 ')
=item B<--warning-*> =item B<--warning-*>
Warning Threshold for: Warning Threshold for:
conn => Backend conn. success, conn => Backend conn. success,
unhealthy => Backend conn. not attempted, unhealthy => Backend conn. not attempted,
busy => Backend conn. too many, busy => Backend conn. too many,
@ -276,11 +221,11 @@ reuse => Backend conn. reuses,
toolate => Backend conn. was closed, toolate => Backend conn. was closed,
recycle => Backend conn. recycles, recycle => Backend conn. recycles,
retry => Backend conn. retry, retry => Backend conn. retry,
req => Backend requests made request => Backend requests made
=item B<--critical-*> =item B<--critical-*>
Critical Threshold for: Critical Threshold for:
conn => Backend conn. success, conn => Backend conn. success,
unhealthy => Backend conn. not attempted, unhealthy => Backend conn. not attempted,
busy => Backend conn. too many, busy => Backend conn. too many,
@ -289,8 +234,8 @@ reuse => Backend conn. reuses,
toolate => Backend conn. was closed, toolate => Backend conn. was closed,
recycle => Backend conn. recycles, recycle => Backend conn. recycles,
retry => Backend conn. retry, retry => Backend conn. retry,
req => Backend requests made request => Backend requests made
=back =back
=cut =cut

View File

@ -1,5 +1,5 @@
# #
# Copyright 2017 Centreon (http://www.centreon.com/) # Copyright 2016 Centreon (http://www.centreon.com/)
# #
# Centreon is a full-fledged industry-strength solution that meets # Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for # the needs in IT infrastructure and application monitoring for
@ -20,38 +20,55 @@
package apps::varnish::local::mode::cache; package apps::varnish::local::mode::cache;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use centreon::plugins::misc; use strict;
use centreon::plugins::statefile; use warnings;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use JSON;
my $maps_counters = { sub set_counters {
cache_hit => { thresholds => { my ($self, %options) = @_;
warning_hit => { label => 'warning-hit', exit_value => 'warning' },
critical_hit => { label => 'critical-hit', exit_value => 'critical' }, $self->{maps_counters_type} = [
}, { name => 'cache', type => 0, skipped_code => { -10 => 1 } },
output_msg => 'Cache Hits: %.2f', ];
factor => 1, unit => '', $self->{maps_counters}->{cache} = [
}, { label => 'hit', set => {
cache_hitpass => { thresholds => { key_values => [ { name => 'cache_hit', diff => 1 } ],
warning_hitpass => { label => 'warning-hitpass', exit_value => 'warning' }, output_template => 'Cache hit: %.2f/s', output_error_template => "Cache hit: %s",
critical_hitpass => { label => 'critical-hitpass', exit_value => 'critical' }, per_second => 1,
}, perfdatas => [
output_msg => 'Cache Hits for pass: %.2f', { label => 'cache_hit', value => 'cache_hit_per_second', template => '%.2f',
factor => 1, unit => '', min => 0 },
}, ],
cache_miss => { thresholds => { }
warning_miss => { label => 'warning-miss', exit_value => 'warning' }, },
critical_miss => { label => 'critical-miss', exit_value => 'critical' }, { label => 'hitpass', set => {
}, key_values => [ { name => 'cache_hitpass', diff => 1 } ],
output_msg => 'Cache misses: %.2f', output_template => 'Cache hitpass : %.2f/s', output_error_template => "Cache hitpass : %s",
factor => 1, unit => '', per_second => 1,
}, perfdatas => [
}; { label => 'cache_hitpass', value => 'cache_hitpass_per_second', template => '%.2f',
min => 0 },
],
}
},
{ label => 'miss', set => {
key_values => [ { name => 'cache_miss', diff => 1 } ],
output_template => 'Cache miss : %.2f/s', output_error_template => "Cache miss : %s",
per_second => 1,
perfdatas => [
{ label => 'cache_miss', value => 'cache_miss_per_second', template => '%.2f',
min => 0 },
],
}
},
],
}
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $self->{version} = '1.0';
@ -66,45 +83,13 @@ sub new {
"sudo" => { name => 'sudo' }, "sudo" => { name => 'sudo' },
"command:s" => { name => 'command', default => 'varnishstat' }, "command:s" => { name => 'command', default => 'varnishstat' },
"command-path:s" => { name => 'command_path', default => '/usr/bin' }, "command-path:s" => { name => 'command_path', default => '/usr/bin' },
"command-options:s" => { name => 'command_options', default => ' -1 ' }, "command-options:s" => { name => 'command_options', default => ' -1 -j 2>&1' },
"command-options2:s" => { name => 'command_options2', default => ' 2>&1' },
}); });
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
$options{options}->add_options(arguments => {
$maps_counters->{$_}->{thresholds}->{$name}->{label} . ':s' => { name => $name },
});
};
};
$self->{instances_done} = {};
$self->{statefile_value} = centreon::plugins::statefile->new(%options);
return $self; return $self;
}; };
sub check_options { sub manage_selection {
my ($self, %options) = @_;
$self->SUPER::init(%options);
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
if (($self->{perfdata}->threshold_validate(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, value => $self->{option_results}->{$name})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong " . $maps_counters->{$_}->{thresholds}->{$name}->{label} . " threshold '" . $self->{option_results}->{$name} . "'.");
$self->{output}->option_exit();
};
};
};
$self->{statefile_value}->check_options(%options);
};
#my $stdout = '
#cache_hit 69941 0.00 Cache hits
#cache_hitpass 10 0.00 Cache hits for pass
#cache_miss 16746 0.00 Cache misses
#';
sub getdata {
my ($self, %options) = @_; my ($self, %options) = @_;
my $stdout = centreon::plugins::misc::execute(output => $self->{output}, my $stdout = centreon::plugins::misc::execute(output => $self->{output},
@ -112,86 +97,22 @@ sub getdata {
sudo => $self->{option_results}->{sudo}, sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command}, command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path}, command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options} . $self->{option_results}->{command_options2}); command_options => $self->{option_results}->{command_options});
#print $stdout;
foreach (split(/\n/, $stdout)) { # "MAIN.cache_hit": {"type": "MAIN", "value": 18437, "flag": "a", "description": "Cache hits"},
#client_conn 7390867 1.00 Client connections # "MAIN.cache_hitpass": {"type": "MAIN", "value": 3488, "flag": "a", "description": "Cache hits for pass"},
# - Symbolic entry name # "MAIN.cache_miss": {"type": "MAIN", "value": 5782, "flag": "a", "description": "Cache misses"},
# - Value my $json_data = decode_json($stdout);
# - Per-second average over process lifetime, or a period if the value can not be averaged
# - Descriptive text
if (/^(.\S*)\s*([0-9]*)\s*([0-9.]*)\s(.*)$/i) { $self->{cache_name} = "cache_varnish_" . $self->{mode} . '_' .
#print "FOUND: " . $1 . "=" . $2 . "\n"; (defined($self->{option_results}->{hostname}) ? md5_hex($self->{option_results}->{hostname}) : md5_hex('all'));
$self->{result}->{$1} = $2;
};
};
};
sub run { foreach my $counter (keys %{$json_data}) {
my ($self, %options) = @_; next if ($counter !~ /cache/);
my $value = $json_data->{$counter}->{value};
$self->getdata(); $counter =~ s/^([A-Z])+\.//;
$self->{cache}->{$counter} = $value;
$self->{statefile_value}->read(statefile => 'cache_apps_varnish' . '_' . $self->{mode} . '_' . (defined($self->{option_results}->{name}) ? md5_hex($self->{option_results}->{name}) : md5_hex('all')));
$self->{result}->{last_timestamp} = time();
my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp');
# Calculate
my $delta_time = $self->{result}->{last_timestamp} - $old_timestamp;
$delta_time = 1 if ($delta_time == 0); # One seconds ;)
foreach (keys %{$maps_counters}) {
#print $_ . "\n";
$self->{old_cache}->{$_} = $self->{statefile_value}->get(name => '$_'); # Get Data from Cache
$self->{old_cache}->{$_} = 0 if ( $self->{old_cache}->{$_} > $self->{result}->{$_} );
$self->{outputdata}->{$_} = ($self->{result}->{$_} - $self->{old_cache}->{$_}) / $delta_time;
};
# Write Cache if not there
$self->{statefile_value}->write(data => $self->{result});
if (!defined($old_timestamp)) {
$self->{output}->output_add(severity => 'OK',
short_msg => "Buffer creation...");
$self->{output}->display();
$self->{output}->exit();
} }
my @exits;
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
push @exits, $self->{perfdata}->threshold_check(value => $self->{outputdata}->{$_}, threshold => [ { label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, 'exit_litteral' => $maps_counters->{$_}->{thresholds}->{$name}->{exit_value} }]);
}
}
my $exit = $self->{output}->get_most_critical(status => [ @exits ]);
my $extra_label = '';
$extra_label = '_' . $instance_output if ($num > 1);
my $str_output = "";
my $str_append = '';
foreach (keys %{$maps_counters}) {
$str_output .= $str_append . sprintf($maps_counters->{$_}->{output_msg}, $self->{outputdata}->{$_} * $maps_counters->{$_}->{factor});
$str_append = ', ';
my ($warning, $critical);
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
$warning = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'warning');
$critical = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'critical');
}
$self->{output}->perfdata_add(label => $_ . $extra_label, unit => $maps_counters->{$_}->{unit},
value => sprintf("%.2f", $self->{outputdata}->{$_} * $maps_counters->{$_}->{factor}),
warning => $warning,
critical => $critical);
}
$self->{output}->output_add(severity => $exit,
short_msg => $str_output);
$self->{output}->display();
$self->{output}->exit();
}; };
@ -227,7 +148,7 @@ Directory Path to Varnishstat Binary File (Default: /usr/bin)
=item B<--command-options> =item B<--command-options>
Parameter for Binary File (Default: ' -1 ') Parameter for Binary File (Default: ' -1 -j 2>&1')
=item B<--warning-*> =item B<--warning-*>
@ -245,4 +166,4 @@ miss => Cache Misses
=back =back
=cut =cut

View File

@ -1,5 +1,5 @@
# #
# Copyright 2017 Centreon (http://www.centreon.com/) # Copyright 2016 Centreon (http://www.centreon.com/)
# #
# Centreon is a full-fledged industry-strength solution that meets # Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for # the needs in IT infrastructure and application monitoring for
@ -20,52 +20,75 @@
package apps::varnish::local::mode::shm; package apps::varnish::local::mode::shm;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::templates::counter);
use centreon::plugins::misc; use strict;
use centreon::plugins::statefile; use warnings;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use JSON;
my $maps_counters = { sub set_counters {
shm_records => { thresholds => { my ($self, %options) = @_;
warning_records => { label => 'warning-records', exit_value => 'warning' },
critical_records => { label => 'critical-records', exit_value => 'critical' }, $self->{maps_counters_type} = [
}, { name => 'shm', type => 0, skipped_code => { -10 => 1 } },
output_msg => 'SHM records: %.2f', ];
factor => 1, unit => '', $self->{maps_counters}->{shm} = [
}, { label => 'records', set => {
shm_writes => { thresholds => { key_values => [ { name => 'shm_records', diff => 1 } ],
warning_writes => { label => 'warning-writes', exit_value => 'warning' }, output_template => 'SHM Records: %.2f/s', output_error_template => "SHM Records: %s",
critical_writes => { label => 'critical-writes', exit_value => 'critical' }, per_second => 1,
}, perfdatas => [
output_msg => 'SHM writes: %.2f', { label => 'shm_records', value => 'shm_records_per_second', template => '%.2f',
factor => 1, unit => '', min => 0 },
}, ],
shm_flushes => { thresholds => { }
warning_flushes => { label => 'warning-flushes', exit_value => 'warning' }, },
critical_flushes => { label => 'critical-flushes', exit_value => 'critical' }, { label => 'writes', set => {
}, key_values => [ { name => 'shm_writes', diff => 1 } ],
output_msg => 'SHM flushes due to overflow: %.2f', output_template => 'SHM Writes: %.2f/s', output_error_template => "SHM Writes: %s",
factor => 1, unit => '', per_second => 1,
}, perfdatas => [
shm_cont => { thresholds => { { label => 'shm_writes', value => 'shm_writes_per_second', template => '%.2f',
warning_cont => { label => 'warning-cont', exit_value => 'warning' }, min => 0 },
critical_cont => { label => 'critical-cont', exit_value => 'critical' }, ],
}, }
output_msg => 'SHM MTX contention: %.2f', },
factor => 1, unit => '', { label => 'flushes', set => {
}, key_values => [ { name => 'shm_flushes', diff => 1 } ],
shm_cycles => { thresholds => { output_template => 'SHM Flushes: %.2f/s', output_error_template => "SHM Flushes: %s",
warning_cycles => { label => 'warning-cycles', exit_value => 'warning' }, per_second => 1,
critical_cycles => { label => 'critical-cycles', exit_value => 'critical' }, perfdatas => [
}, { label => 'shm_flushes', value => 'shm_flushes_per_second', template => '%.2f',
output_msg => 'SHM cycles through buffer: %.2f', min => 0 },
factor => 1, unit => '', ],
}, }
}; },
{ label => 'cont', set => {
key_values => [ { name => 'shm_cont', diff => 1 } ],
output_template => 'SHM Contention: %.2f/s', output_error_template => "SHM Contention: %s",
per_second => 1,
perfdatas => [
{ label => 'shm_cont', value => 'shm_cont_per_second', template => '%.2f',
min => 0 },
],
}
},
{ label => 'cycles', set => {
key_values => [ { name => 'shm_cycles', diff => 1 } ],
output_template => 'SHM Cycles: %.2f/s', output_error_template => "SHM Cycles: %s",
per_second => 1,
perfdatas => [
{ label => 'shm_cycles', value => 'shm_cycles_per_second', template => '%.2f',
min => 0 },
],
}
},
],
}
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$self->{version} = '1.0'; $self->{version} = '1.0';
@ -80,39 +103,13 @@ sub new {
"sudo" => { name => 'sudo' }, "sudo" => { name => 'sudo' },
"command:s" => { name => 'command', default => 'varnishstat' }, "command:s" => { name => 'command', default => 'varnishstat' },
"command-path:s" => { name => 'command_path', default => '/usr/bin' }, "command-path:s" => { name => 'command_path', default => '/usr/bin' },
"command-options:s" => { name => 'command_options', default => ' -1 ' }, "command-options:s" => { name => 'command_options', default => ' -1 -j 2>&1' },
"command-options2:s" => { name => 'command_options2', default => ' 2>&1' },
}); });
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
$options{options}->add_options(arguments => {
$maps_counters->{$_}->{thresholds}->{$name}->{label} . ':s' => { name => $name },
});
};
};
$self->{instances_done} = {};
$self->{statefile_value} = centreon::plugins::statefile->new(%options);
return $self; return $self;
}; };
sub check_options { sub manage_selection {
my ($self, %options) = @_;
$self->SUPER::init(%options);
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
if (($self->{perfdata}->threshold_validate(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, value => $self->{option_results}->{$name})) == 0) {
$self->{output}->add_option_msg(short_msg => "Wrong " . $maps_counters->{$_}->{thresholds}->{$name}->{label} . " threshold '" . $self->{option_results}->{$name} . "'.");
$self->{output}->option_exit();
};
};
};
$self->{statefile_value}->check_options(%options);
};
sub getdata {
my ($self, %options) = @_; my ($self, %options) = @_;
my $stdout = centreon::plugins::misc::execute(output => $self->{output}, my $stdout = centreon::plugins::misc::execute(output => $self->{output},
@ -120,89 +117,27 @@ sub getdata {
sudo => $self->{option_results}->{sudo}, sudo => $self->{option_results}->{sudo},
command => $self->{option_results}->{command}, command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path}, command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options} . $self->{option_results}->{command_options2}); command_options => $self->{option_results}->{command_options});
#print $stdout;
foreach (split(/\n/, $stdout)) { # "MAIN.shm_records": {"type": "MAIN", "value": 6992776, "flag": "a", "description": "SHM records"},
#client_conn 7390867 1.00 Client connections # "MAIN.shm_writes": {"type": "MAIN", "value": 3947244, "flag": "a", "description": "SHM writes"},
# - Symbolic entry name # "MAIN.shm_flushes": {"type": "MAIN", "value": 12354, "flag": "a", "description": "SHM flushes due to overflow"},
# - Value # "MAIN.shm_cont": {"type": "MAIN", "value": 564, "flag": "a", "description": "SHM MTX contention"},
# - Per-second average over process lifetime, or a period if the value can not be averaged # "MAIN.shm_cycles": {"type": "MAIN", "value": 3, "flag": "a", "description": "SHM cycles through buffer"},
# - Descriptive text
if (/^(.\S*)\s*([0-9]*)\s*([0-9.]*)\s(.*)$/i) { my $json_data = decode_json($stdout);
#print "FOUND: " . $1 . "=" . $2 . "\n";
$self->{result}->{$1} = $2; $self->{cache_name} = "cache_varnish_" . $self->{mode} . '_' .
}; (defined($self->{option_results}->{hostname}) ? md5_hex($self->{option_results}->{hostname}) : md5_hex('all'));
};
foreach my $counter (keys %{$json_data}) {
next if ($counter !~ /shm/);
my $value = $json_data->{$counter}->{value};
$counter =~ s/^([A-Z])+\.//;
$self->{shm}->{$counter} = $value;
}
}; };
sub run {
my ($self, %options) = @_;
$self->getdata();
$self->{statefile_value}->read(statefile => 'cache_apps_varnish' . '_' . $self->{mode} . '_' . (defined($self->{option_results}->{name}) ? md5_hex($self->{option_results}->{name}) : md5_hex('all')));
$self->{result}->{last_timestamp} = time();
my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp');
# Calculate
my $delta_time = $self->{result}->{last_timestamp} - $old_timestamp;
$delta_time = 1 if ($delta_time == 0); # One seconds ;)
foreach (keys %{$maps_counters}) {
#print $_ . "\n";
$self->{old_cache}->{$_} = $self->{statefile_value}->get(name => '$_'); # Get Data from Cache
$self->{old_cache}->{$_} = 0 if ( $self->{old_cache}->{$_} > $self->{result}->{$_} );
$self->{outputdata}->{$_} = ($self->{result}->{$_} - $self->{old_cache}->{$_}) / $delta_time;
};
# Write Cache if not there
$self->{statefile_value}->write(data => $self->{result});
if (!defined($old_timestamp)) {
$self->{output}->output_add(severity => 'OK',
short_msg => "Buffer creation...");
$self->{output}->display();
$self->{output}->exit();
}
my @exits;
foreach (keys %{$maps_counters}) {
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
push @exits, $self->{perfdata}->threshold_check(value => $self->{outputdata}->{$_}, threshold => [ { label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, 'exit_litteral' => $maps_counters->{$_}->{thresholds}->{$name}->{exit_value} }]);
}
}
my $exit = $self->{output}->get_most_critical(status => [ @exits ]);
my $extra_label = '';
$extra_label = '_' . $instance_output if ($num > 1);
my $str_output = "";
my $str_append = '';
foreach (keys %{$maps_counters}) {
$str_output .= $str_append . sprintf($maps_counters->{$_}->{output_msg}, $self->{outputdata}->{$_} * $maps_counters->{$_}->{factor});
$str_append = ', ';
my ($warning, $critical);
foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) {
$warning = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'warning');
$critical = $self->{perfdata}->get_perfdata_for_output(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}) if ($maps_counters->{$_}->{thresholds}->{$name}->{exit_value} eq 'critical');
}
$self->{output}->perfdata_add(label => $_ . $extra_label, unit => $maps_counters->{$_}->{unit},
value => sprintf("%.2f", $self->{outputdata}->{$_} * $maps_counters->{$_}->{factor}),
warning => $warning,
critical => $critical);
}
$self->{output}->output_add(severity => $exit,
short_msg => $str_output);
$self->{output}->display();
$self->{output}->exit();
};
1; 1;
__END__ __END__
@ -235,11 +170,11 @@ Directory Path to Varnishstat Binary File (Default: /usr/bin)
=item B<--command-options> =item B<--command-options>
Parameter for Binary File (Default: ' -1 ') Parameter for Binary File (Default: ' -1 -j 2>&1')
=item B<--warning-*> =item B<--warning-*>
Warning Threshold for: Warning Threshold for:
records => SHM records, records => SHM records,
writes => SHM writes, writes => SHM writes,
flushes => SHM flushes due to overflow, flushes => SHM flushes due to overflow,
@ -248,7 +183,7 @@ cycles => SHM cycles through buffer
=item B<--critical-*> =item B<--critical-*>
Critical Threshold for: Critical Threshold for:
records => SHM records, records => SHM records,
writes => SHM writes, writes => SHM writes,
flushes => SHM flushes due to overflow, flushes => SHM flushes due to overflow,
@ -257,4 +192,4 @@ cycles => SHM cycles through buffer
=back =back
=cut =cut