Secure plugins (#2691)

This commit is contained in:
qgarnier 2021-04-01 09:48:14 +02:00 committed by GitHub
parent 68c0bead7d
commit b0b55a27cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 989 additions and 1005 deletions

View File

@ -24,7 +24,6 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
sub custom_status_threshold { sub custom_status_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -37,13 +36,13 @@ sub custom_status_threshold {
# To exclude some OK # To exclude some OK
if (defined($self->{instance_mode}->{option_results}->{ok_status}) && $self->{instance_mode}->{option_results}->{ok_status} ne '' && if (defined($self->{instance_mode}->{option_results}->{ok_status}) && $self->{instance_mode}->{option_results}->{ok_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{ok_status}") { $self->eval(value => $self->{instance_mode}->{option_results}->{ok_status})) {
$status = 'ok'; $status = 'ok';
} elsif (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_status}") { $self->eval(value => $self->{instance_mode}->{option_results}->{critical_status})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_status}") { $self->eval(value => $self->{instance_mode}->{option_results}->{warning_status})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -57,7 +56,8 @@ sub custom_status_threshold {
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = sprintf('status : %s (%s) [type: %s] [remote hostname: %s] [vmname: %s] [plan name: %s] [end time: %s]', return sprintf(
'status : %s (%s) [type: %s] [remote hostname: %s] [vmname: %s] [plan name: %s] [end time: %s]',
$self->{result_values}->{status} == 1 ? 'ok' : 'failed', $self->{result_values}->{status} == 1 ? 'ok' : 'failed',
$self->{result_values}->{status}, $self->{result_values}->{status},
$self->{result_values}->{type}, $self->{result_values}->{type},
@ -66,7 +66,6 @@ sub custom_status_output {
$self->{result_values}->{plan_name}, $self->{result_values}->{plan_name},
scalar(localtime($self->{result_values}->{end_time})) scalar(localtime($self->{result_values}->{end_time}))
); );
return $msg;
} }
sub set_counters { sub set_counters {
@ -90,13 +89,14 @@ sub set_counters {
$self->{maps_counters}->{job} = [ $self->{maps_counters}->{job} = [
{ label => 'status', threshold => 0, set => { { label => 'status', threshold => 0, set => {
key_values => [ { name => 'status' }, { name => 'display' }, key_values => [
{ name => 'status' }, { name => 'display' },
{ name => 'type' }, { name => 'rhostname' }, { name => 'vmname' }, { name => 'plan_name' }, { name => 'type' }, { name => 'rhostname' }, { name => 'vmname' }, { name => 'plan_name' },
{ name => 'elapsed_time' }, { name => 'end_time' } ], { name => 'elapsed_time' }, { name => 'end_time' }
closure_custom_calc => \&catalog_status_calc, ],
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold'), closure_custom_threshold_check => $self->can('custom_status_threshold')
} }
}, },
]; ];
@ -115,7 +115,7 @@ sub new {
'ok-status:s' => { name => 'ok_status', default => '%{status} == 1' }, 'ok-status:s' => { name => 'ok_status', default => '%{status} == 1' },
'warning-status:s' => { name => 'warning_status', default => '' }, 'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '%{status} != 1' }, 'critical-status:s' => { name => 'critical_status', default => '%{status} != 1' },
'timezone:s' => { name => 'timezone' }, 'timezone:s' => { name => 'timezone' }
}); });
return $self; return $self;
@ -202,7 +202,7 @@ sub manage_selection {
plan_name => $plan_name, plan_name => $plan_name,
end_time => $row->{end_time}, end_time => $row->{end_time},
}; };
$self->{global}->{total}++; $self->{global}->{total}++;
} }
} }

View File

@ -26,35 +26,7 @@ use strict;
use warnings; use warnings;
use centreon::plugins::misc; use centreon::plugins::misc;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_threshold_ng);
sub custom_status_threshold {
my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
# To exclude some OK
if (defined($self->{instance_mode}->{option_results}->{ok_status}) && $self->{instance_mode}->{option_results}->{ok_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{ok_status}") {
$status = 'ok';
} elsif (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_status}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_status}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
return $status;
}
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -62,19 +34,9 @@ sub custom_status_output {
return 'Status : ' . $self->{result_values}->{status}; return 'Status : ' . $self->{result_values}->{status};
} }
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{type} = $options{new_datas}->{$self->{instance} . '_type'};
$self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'};
return 0;
}
sub custom_long_output { sub custom_long_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return 'Started Since: ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{elapsed}); return 'Started Since: ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{elapsed});
} }
@ -93,26 +55,9 @@ sub custom_long_calc {
} }
sub custom_frozen_threshold { sub custom_frozen_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_frozen}) && $self->{instance_mode}->{option_results}->{critical_frozen} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_frozen}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_frozen}) && $self->{instance_mode}->{option_results}->{warning_frozen} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_frozen}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
my $status = catalog_status_threshold_ng($self, %options);
$self->{instance_mode}->{last_status_frozen} = $status; $self->{instance_mode}->{last_status_frozen} = $status;
return $status; return $status;
} }
@ -167,30 +112,35 @@ sub set_counters {
$self->{maps_counters}->{job} = [ $self->{maps_counters}->{job} = [
{ label => 'status', threshold => 0, set => { { label => 'status', threshold => 0, set => {
key_values => [ { name => 'status' }, { name => 'display' }, { name => 'type' }, { name => 'state' } ], key_values => [
closure_custom_calc => $self->can('custom_status_calc'), { name => 'status' }, { name => 'display' }, { name => 'type' }, { name => 'state' }
],
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold'), closure_custom_threshold_check => \&catalog_status_threshold
} }
}, },
{ label => 'long', threshold => 0, set => { { label => 'long', type => 2, set => {
key_values => [ { name => 'status' }, { name => 'display' }, { name => 'elapsed' }, { name => 'type' }, key_values => [
{ name => 'state' } ], { name => 'status' }, { name => 'display' }, { name => 'elapsed' }, { name => 'type' },
{ name => 'state' }
],
closure_custom_calc => $self->can('custom_long_calc'), closure_custom_calc => $self->can('custom_long_calc'),
closure_custom_output => $self->can('custom_long_output'), closure_custom_output => $self->can('custom_long_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold, closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'frozen', threshold => 0, set => { { label => 'frozen', type => 2, critical_default => '%{state} =~ /active|queue/ && %{kb} == 0', set => {
key_values => [ { name => 'kb', diff => 1 }, { name => 'status' }, key_values => [
{ name => 'kb', diff => 1 }, { name => 'status' },
{ name => 'display' }, { name => 'elapsed' }, { name => 'type' }, { name => 'state' }, { name => 'display' }, { name => 'elapsed' }, { name => 'type' }, { name => 'state' },
{ name => 'parentid' }, { name => 'schedule' }, { name => 'jobid' } ], { name => 'parentid' }, { name => 'schedule' }, { name => 'jobid' }
],
closure_custom_calc => $self->can('custom_frozen_calc'), closure_custom_calc => $self->can('custom_frozen_calc'),
closure_custom_output => $self->can('custom_frozen_output'), closure_custom_output => $self->can('custom_frozen_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_frozen_threshold'), closure_custom_threshold_check => $self->can('custom_frozen_threshold')
} }
} }
]; ];
@ -219,11 +169,7 @@ sub new {
'filter-start-time:s' => { name => 'filter_start_time' }, 'filter-start-time:s' => { name => 'filter_start_time' },
'ok-status:s' => { name => 'ok_status', default => '%{status} == 0' }, 'ok-status:s' => { name => 'ok_status', default => '%{status} == 0' },
'warning-status:s' => { name => 'warning_status', default => '%{status} == 1' }, 'warning-status:s' => { name => 'warning_status', default => '%{status} == 1' },
'critical-status:s' => { name => 'critical_status', default => '%{status} > 1' }, 'critical-status:s' => { name => 'critical_status', default => '%{status} > 1' }
'warning-long:s' => { name => 'warning_long' },
'critical-long:s' => { name => 'critical_long' },
'warning-frozen:s' => { name => 'warning_frozen' },
'critical-frozen:s' => { name => 'critical_frozen', default => '%{state} =~ /active|queue/ && %{kb} == 0' },
}); });
return $self; return $self;
@ -233,10 +179,7 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->change_macros(macros => [ $self->change_macros(macros => ['ok_status', 'warning_status', 'critical_status']);
'ok_status', 'warning_status', 'critical_status', 'warning_long',
'critical_long', 'warning_frozen', 'critical_frozen']
);
} }
sub policy_long_output { sub policy_long_output {

View File

@ -26,61 +26,24 @@ use strict;
use warnings; use warnings;
use centreon::plugins::misc; use centreon::plugins::misc;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = 'status : ' . $self->{result_values}->{status};
return $msg; return 'status : ' . $self->{result_values}->{status};
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
} }
sub custom_long_output { sub custom_long_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = 'elapsed time : ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{elapsed});
return $msg; return 'elapsed time : ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{elapsed});
}
sub custom_long_calc {
my ($self, %options) = @_;
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{elapsed} = $options{new_datas}->{$self->{instance} . '_elapsed'};
return 0;
} }
sub custom_frozen_threshold { sub custom_frozen_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_frozen}) && $self->{instance_mode}->{option_results}->{critical_frozen} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_frozen}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_frozen}) && $self->{instance_mode}->{option_results}->{warning_frozen} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_frozen}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
my $status = catalog_status_threshold_ng($self, %options);
$self->{instance_mode}->{last_status_frozen} = $status; $self->{instance_mode}->{last_status_frozen} = $status;
return $status; return $status;
} }
@ -106,6 +69,12 @@ sub custom_frozen_calc {
return 0; return 0;
} }
sub prefix_job_output {
my ($self, %options) = @_;
return "job '" . $options{instance_value}->{display} . "' ";
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -116,20 +85,18 @@ sub set_counters {
]; ];
$self->{maps_counters}->{job} = [ $self->{maps_counters}->{job} = [
{ label => 'status', threshold => 0, set => { { label => 'status', type => 2, critical_default => '%{status} =~ /error/i', set => {
key_values => [ { name => 'status' }, { name => 'display' } ], key_values => [ { name => 'status' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold, closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'long', threshold => 0, set => { { label => 'long', threshold => 2, set => {
key_values => [ { name => 'status' }, { name => 'display' }, { name => 'elapsed' } ], key_values => [ { name => 'status' }, { name => 'display' }, { name => 'elapsed' } ],
closure_custom_calc => $self->can('custom_long_calc'),
closure_custom_output => $self->can('custom_long_output'), closure_custom_output => $self->can('custom_long_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold, closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'frozen', threshold => 0, set => { { label => 'frozen', threshold => 0, set => {
@ -148,54 +115,36 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'hostname:s' => { name => 'hostname' },
"hostname:s" => { name => 'hostname' }, 'remote' => { name => 'remote' },
"remote" => { name => 'remote' }, 'ssh-option:s@' => { name => 'ssh_option' },
"ssh-option:s@" => { name => 'ssh_option' }, 'ssh-path:s' => { name => 'ssh_path' },
"ssh-path:s" => { name => 'ssh_path' }, 'ssh-command:s' => { name => 'ssh_command', default => 'ssh' },
"ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, 'timeout:s' => { name => 'timeout', default => 30 },
"timeout:s" => { name => 'timeout', default => 30 }, 'sudo' => { name => 'sudo' },
"sudo" => { name => 'sudo' }, 'command:s' => { name => 'command', default => 'impexp' },
"command:s" => { name => 'command', default => 'impexp' }, 'command-path:s' => { name => 'command_path', default => '/quadstorvtl/bin' },
"command-path:s" => { name => 'command_path', default => '/quadstorvtl/bin' }, 'command-options:s' => { name => 'command_options', default => '-l' }
"command-options:s" => { name => 'command_options', default => '-l' }, });
"warning-status:s" => { name => 'warning_status' },
"critical-status:s" => { name => 'critical_status', default => '%{status} =~ /error/i' },
"warning-long:s" => { name => 'warning_long' },
"critical-long:s" => { name => 'critical_long' },
"warning-frozen:s" => { name => 'warning_frozen' },
"critical-frozen:s" => { name => 'critical_frozen' },
});
return $self; return $self;
} }
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status', 'warning_long', 'critical_long', 'warning_frozen', 'critical_frozen']);
}
sub prefix_job_output {
my ($self, %options) = @_;
return "job '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{cache_name} = "quadstor_" . $self->{mode} . '_' . (defined($self->{option_results}->{hostname}) ? $self->{option_results}->{hostname} : 'me') . '_' . $self->{cache_name} = 'quadstor_' . $self->{mode} . '_' . (defined($self->{option_results}->{hostname}) ? $self->{option_results}->{hostname} : 'me') . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
my ($stdout) = centreon::plugins::misc::execute(output => $self->{output}, my ($stdout) = centreon::plugins::misc::execute(
options => $self->{option_results}, output => $self->{output},
sudo => $self->{option_results}->{sudo}, options => $self->{option_results},
command => $self->{option_results}->{command}, sudo => $self->{option_results}->{sudo},
command_path => $self->{option_results}->{command_path}, command => $self->{option_results}->{command},
command_options => $self->{option_results}->{command_options}); command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options}
);
$self->{jobs}->{global} = { job => {} }; $self->{jobs}->{global} = { job => {} };
#JobID Type Source State Transfer Elapsed #JobID Type Source State Transfer Elapsed
#252 Import 701831L2 Error 36.00 GB 572 #252 Import 701831L2 Error 36.00 GB 572
@ -207,10 +156,10 @@ sub manage_selection {
shift @lines; shift @lines;
foreach (@lines) { foreach (@lines) {
next if (! /^(\d+)\s+\S+\s+(\S+)\s+(\S+)\s+([0-9\.]+)\s+\S+\s+(\d+)/); next if (! /^(\d+)\s+\S+\s+(\S+)\s+(\S+)\s+([0-9\.]+)\s+\S+\s+(\d+)/);
my ($job_id, $job_source, $job_state, $job_kb, $job_elapsed) = my ($job_id, $job_source, $job_state, $job_kb, $job_elapsed) =
($1, $2, $3, $4, $5); ($1, $2, $3, $4, $5);
my $name = $job_source . '.' . $job_id; my $name = $job_source . '.' . $job_id;
$self->{jobs}->{global}->{job}->{$name} = { $self->{jobs}->{global}->{job}->{$name} = {
display => $name, display => $name,
@ -219,7 +168,7 @@ sub manage_selection {
elapsed => $job_elapsed elapsed => $job_elapsed
}; };
} }
if (scalar(keys %{$self->{jobs}->{global}}) <= 0) { if (scalar(keys %{$self->{jobs}->{global}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No job found."); $self->{output}->add_option_msg(short_msg => "No job found.");
$self->{output}->option_exit(); $self->{output}->option_exit();

View File

@ -40,9 +40,8 @@ sub custom_hosts_calc {
sub custom_hosts_output { sub custom_hosts_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = '';
$msg .= "[up:".$self->{result_values}->{total_up}."][down:".$self->{result_values}->{total_down}."][unreachable:".$self->{result_values}->{total_unreachable}."]"; return "[up:".$self->{result_values}->{total_up}."][down:".$self->{result_values}->{total_down}."][unreachable:".$self->{result_values}->{total_unreachable}."]";
return $msg
} }
sub custom_hosts_perfdata { sub custom_hosts_perfdata {
@ -68,10 +67,10 @@ sub custom_hosts_threshold {
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_total}) && $self->{instance_mode}->{option_results}->{critical_total} ne '' && if (defined($self->{instance_mode}->{option_results}->{critical_total}) && $self->{instance_mode}->{option_results}->{critical_total} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_total}") { $self->eval(value => $self->{instance_mode}->{option_results}->{critical_total})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_total}) && $self->{instance_mode}->{option_results}->{warning_total} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{warning_total}) && $self->{instance_mode}->{option_results}->{warning_total} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_total}") { $self->eval(value => $self->{instance_mode}->{option_results}->{warning_total})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -123,10 +122,10 @@ sub custom_services_threshold {
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_total}) && $self->{instance_mode}->{option_results}->{critical_total} ne '' && if (defined($self->{instance_mode}->{option_results}->{critical_total}) && $self->{instance_mode}->{option_results}->{critical_total} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_total}") { $self->eval(value => $self->{instance_mode}->{option_results}->{critical_total})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_total}) && $self->{instance_mode}->{option_results}->{warning_total} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{warning_total}) && $self->{instance_mode}->{option_results}->{warning_total} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_total}") { $self->eval(value => $self->{instance_mode}->{option_results}->{warning_total})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -229,10 +228,10 @@ sub custom_groups_threshold {
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_groups}) && $self->{instance_mode}->{option_results}->{critical_groups} ne '' && if (defined($self->{instance_mode}->{option_results}->{critical_groups}) && $self->{instance_mode}->{option_results}->{critical_groups} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_groups}") { $self->eval(value => $self->{instance_mode}->{option_results}->{critical_groups})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_groups}) && $self->{instance_mode}->{option_results}->{warning_groups} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{warning_groups}) && $self->{instance_mode}->{option_results}->{warning_groups} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_groups}") { $self->eval(value => $self->{instance_mode}->{option_results}->{warning_groups})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -273,8 +272,10 @@ sub set_counters {
$self->{maps_counters}->{logicalgroups} = [ $self->{maps_counters}->{logicalgroups} = [
{ label => 'group-svc-global', threshold => 0, set => { { label => 'group-svc-global', threshold => 0, set => {
key_values => [ { name => 'ok' }, { name => 'unknown' }, { name => 'critical' }, { name => 'warning' }, key_values => [
{ name => 'up' }, { name => 'down' }, { name => 'unreachable' }, { name => 'display' } ], { name => 'ok' }, { name => 'unknown' }, { name => 'critical' }, { name => 'warning' },
{ name => 'up' }, { name => 'down' }, { name => 'unreachable' }, { name => 'display' }
],
closure_custom_calc => $self->can('custom_groups_calc'), closure_custom_calc => $self->can('custom_groups_calc'),
closure_custom_output => $self->can('custom_groups_output'), closure_custom_output => $self->can('custom_groups_output'),
closure_custom_threshold_check => $self->can('custom_groups_threshold'), closure_custom_threshold_check => $self->can('custom_groups_threshold'),

View File

@ -28,56 +28,86 @@ use List::Util qw (min max sum);
use JSON; use JSON;
my $config_data; my $config_data;
sub get_printf_vars {
my ($self, %options) = @_;
# compat
if (ref($options{printf_var}) ne 'ARRAY') {
$options{printf_var} = [split /,/, $options{printf_var}];
}
my $message;
my $vars = [];
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
foreach my $var (@{$options{printf_var}}) {
$var =~ s/\$self->\{result_values\}/\$values/;
push @$vars, $self->{output}->assign_eval(eval => $var, values => $options{values});
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'eval printf_var problem: ' . $message);
$self->{output}->option_exit();
}
return $vars;
}
sub custom_metric_output { sub custom_metric_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg; my $output;
my $message;
if ($self->{result_values}->{type} eq 'unique') { if ($self->{result_values}->{type} eq 'unique') {
if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}) && defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg})) { if (defined($config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var}) && defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", );
eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); $output = sprintf(
}; $config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_msg},
@$vars
);
} elsif (defined($config_data->{filters}->{formatting}->{printf_var}) && defined($config_data->{filters}->{formatting}->{printf_msg})) { } elsif (defined($config_data->{filters}->{formatting}->{printf_var}) && defined($config_data->{filters}->{formatting}->{printf_msg})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{filters}->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); );
}; $output = sprintf(
$config_data->{filters}->{formatting}->{printf_msg},
@$vars
);
} else { } else {
$msg = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value}); $output = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value});
} }
} }
if ($self->{result_values}->{type} eq 'global') { if ($self->{result_values}->{type} eq 'global') {
if (defined($config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting})) { if (defined($config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", );
eval "$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); $output = sprintf(
}; $config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting}->{printf_msg},
@$vars
);
} elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) { } elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); );
}; $output = sprintf(
$config_data->{formatting}->{printf_msg},
@$vars
);
} else { } else {
$msg = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value}); $output = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value});
} }
} }
if (defined($message)) { return $output;
$self->{output}->output_add(long_msg => 'printf expression problem: ' . $message);
$self->{output}->option_exit();
}
return $msg;
} }
sub custom_metric_calc { sub custom_metric_calc {
@ -333,9 +363,10 @@ sub manage_selection {
max(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'max'); max(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'max');
if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'none') { if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'none') {
$self->{vmetrics}->{$vcurve}->{aggregated_value} = ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'none' && defined($config_data->{virtualcurve}->{$vcurve}->{custom})) ? $self->{vmetrics}->{$vcurve}->{aggregated_value} = (
eval "$config_data->{virtualcurve}->{$vcurve}->{custom}" : $config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'none' && defined($config_data->{virtualcurve}->{$vcurve}->{custom})) ?
eval "$self->{vmetrics}->{$vcurve}->{aggregated_value} $config_data->{virtualcurve}->{$vcurve}->{custom}"; eval "$config_data->{virtualcurve}->{$vcurve}->{custom}" :
eval "$self->{vmetrics}->{$vcurve}->{aggregated_value} $config_data->{virtualcurve}->{$vcurve}->{custom}";
} }
$self->{vmetrics}->{$vcurve}->{unit} = (defined($config_data->{virtualcurve}->{$vcurve}->{unit})) ? $config_data->{virtualcurve}->{$vcurve}->{unit} : ''; $self->{vmetrics}->{$vcurve}->{unit} = (defined($config_data->{virtualcurve}->{$vcurve}->{unit})) ? $config_data->{virtualcurve}->{$vcurve}->{unit} : '';

View File

@ -58,7 +58,7 @@ sub change_macros {
foreach (('warning', 'critical')) { foreach (('warning', 'critical')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -57,7 +57,7 @@ sub change_macros {
foreach (('warning', 'critical')) { foreach (('warning', 'critical')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -56,7 +56,7 @@ sub change_macros {
foreach (('warning', 'critical')) { foreach (('warning', 'critical')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -57,7 +57,7 @@ sub change_macros {
foreach (('warning', 'critical')) { foreach (('warning', 'critical')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -59,7 +59,7 @@ sub change_macros {
foreach (('warning', 'critical')) { foreach (('warning', 'critical')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -55,7 +55,7 @@ sub change_macros {
foreach (('warning', 'critical')) { foreach (('warning', 'critical')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -55,7 +55,7 @@ sub change_macros {
foreach (('warning', 'critical')) { foreach (('warning', 'critical')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -70,7 +70,7 @@ sub manage_selection {
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$name !~ /$self->{option_results}->{filter_name}/) { $name !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(long_msg => "Skipping '" . $name . "': no matching filter.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next; next;
} }
@ -86,8 +86,10 @@ sub run {
$self->{output}->output_add(long_msg => "'" . $name . "' [state = " . $self->{nodes}->{$name}->{state} . "]"); $self->{output}->output_add(long_msg => "'" . $name . "' [state = " . $self->{nodes}->{$name}->{state} . "]");
} }
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => 'List Nodes:'); severity => 'OK',
short_msg => 'List nodes:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit(); $self->{output}->exit();
} }

View File

@ -121,11 +121,13 @@ sub disco_show {
$self->manage_selection(disco => 1); $self->manage_selection(disco => 1);
foreach my $id (sort keys %{$self->{resources}}) { foreach my $id (sort keys %{$self->{resources}}) {
$self->{output}->add_disco_entry(name => $self->{resources}->{$id}->{name}, $self->{output}->add_disco_entry(
state => $self->{resources}->{$id}->{state}, name => $self->{resources}->{$id}->{name},
id => $id, state => $self->{resources}->{$id}->{state},
owner_node => $self->{resources}->{$id}->{owner_node}, id => $id,
class => $self->{resources}->{$id}->{class}); owner_node => $self->{resources}->{$id}->{owner_node},
class => $self->{resources}->{$id}->{class}
);
} }
} }

View File

@ -25,82 +25,35 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use Win32::OLE; use Win32::OLE;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'rg', type => 1, cb_prefix_output => 'prefix_rg_output', message_multiple => 'All resource groups are ok' }
];
$self->{maps_counters}->{rg} = [
{ label => 'status', threshold => 0, set => {
key_values => [ { name => 'state' }, { name => 'display' }, { name => 'owner_node' }, { name => 'preferred_owners' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_threshold_output'),
}
},
];
}
my $instance_current;
sub is_preferred_node { sub is_preferred_node {
if (!defined($instance_current->{result_values}->{preferred_owners}) || my (%options) = @_;
scalar(@{$instance_current->{result_values}->{preferred_owners}}) == 0) {
if (!defined($options{preferred_owners}) ||
scalar(@{$options{preferred_owners}}) == 0) {
return 1; return 1;
} }
foreach my $pref_node (@{$instance_current->{result_values}->{preferred_owners}}) { foreach my $pref_node (@{$options{preferred_owners}}) {
if ($pref_node eq $instance_current->{result_values}->{owner_node}) { if ($pref_node eq $options{owner_node}) {
return 1; return 1;
} }
} }
return 0; return 0;
} }
sub custom_threshold_output {
my ($self, %options) = @_;
my $status = 'ok';
my $message;
$instance_current = $self;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_status}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_status}") {
$status = 'warning';
} elsif (defined($self->{instance_mode}->{option_results}->{unknown_status}) && $self->{instance_mode}->{option_results}->{unknown_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{unknown_status}") {
$status = 'unknown';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
return $status;
}
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $pref_nodes = 'any'; my $pref_nodes = 'any';
if (defined($instance_current->{result_values}->{preferred_owners}) && if (defined($self->{result_values}->{preferred_owners}) &&
scalar(@{$instance_current->{result_values}->{preferred_owners}}) > 0) { scalar(@{$self->{result_values}->{preferred_owners}}) > 0) {
$pref_nodes = join(', ', @{$instance_current->{result_values}->{preferred_owners}}); $pref_nodes = join(', ', @{$self->{result_values}->{preferred_owners}});
} }
my $msg = 'state : ' . $self->{result_values}->{state} . ' [node: ' . $self->{result_values}->{owner_node} . '] [preferred nodes: ' . $pref_nodes . ']'; return 'state: ' . $self->{result_values}->{state} . ' [node: ' . $self->{result_values}->{owner_node} . '] [preferred nodes: ' . $pref_nodes . ']';
return $msg;
} }
sub custom_status_calc { sub custom_status_calc {
@ -110,27 +63,51 @@ sub custom_status_calc {
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
$self->{result_values}->{owner_node} = $options{new_datas}->{$self->{instance} . '_owner_node'}; $self->{result_values}->{owner_node} = $options{new_datas}->{$self->{instance} . '_owner_node'};
$self->{result_values}->{preferred_owners} = $options{new_datas}->{$self->{instance} . '_preferred_owners'}; $self->{result_values}->{preferred_owners} = $options{new_datas}->{$self->{instance} . '_preferred_owners'};
$self->{result_values}->{is_preferred_node} = is_preferred_node(
preferred_owners => $self->{result_values}->{preferred_owners},
owner_node => $self->{result_values}->{owner_node}
);
return 0; return 0;
} }
sub prefix_rg_output { sub prefix_rg_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return "Resource group '" . $options{instance_value}->{display} . "' "; return "Resource group '" . $options{instance_value}->{display} . "' ";
} }
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'rg', type => 1, cb_prefix_output => 'prefix_rg_output', message_multiple => 'All resource groups are ok' }
];
$self->{maps_counters}->{rg} = [
{
label => 'status', type => 2,
unknown_default => '%{state} =~ /unknown/',
warning_default => '%{is_preferred_node} == 0',
critical_default => '%{state} =~ /failed|offline/',
set => {
key_values => [ { name => 'state' }, { name => 'display' }, { name => 'owner_node' }, { name => 'preferred_owners' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
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);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'filter-name:s' => { name => 'filter_name' }
"filter-name:s" => { name => 'filter_name' }, });
"unknown-status:s" => { name => 'unknown_status', default => '%{state} =~ /unknown/' },
"warning-status:s" => { name => 'warning_status', default => 'not is_preferred_node()' },
"critical-status:s" => { name => 'critical_status', default => '%{state} =~ /failed|offline/' },
});
return $self; return $self;
} }
@ -138,8 +115,13 @@ sub new {
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']); # compatibility
foreach (('unknown_status', 'warning_status', 'critical_status')) {
if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') {
$self->{option_results}->{$_} =~ s/is_preferred_node\(\)/\$values->{is_preferred_node}/g;
}
}
} }
my %map_state = ( my %map_state = (
@ -148,7 +130,7 @@ my %map_state = (
1 => 'offline', 1 => 'offline',
2 => 'failed', 2 => 'failed',
3 => 'partial online', 3 => 'partial online',
4 => 'pending', 4 => 'pending'
); );
sub manage_selection { sub manage_selection {
@ -160,13 +142,11 @@ sub manage_selection {
$self->{output}->add_option_msg(short_msg => "Cant create server object:" . Win32::OLE->LastError()); $self->{output}->add_option_msg(short_msg => "Cant create server object:" . Win32::OLE->LastError());
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my $query = "Select * from MSCluster_ResourceGroupToPreferredNode"; my $query = 'Select * from MSCluster_ResourceGroupToPreferredNode';
my $resultset = $wmi->ExecQuery($query); my $resultset = $wmi->ExecQuery($query);
my $preferred_nodes = {}; my $preferred_nodes = {};
foreach my $obj (in $resultset) { foreach my $obj (in $resultset) {
use Data::Dumper;
# MSCluster_ResourceGroup.Name="xxx" # MSCluster_ResourceGroup.Name="xxx"
if ($obj->GroupComponent =~ /MSCluster_ResourceGroup.Name="(.*?)"/i) { if ($obj->GroupComponent =~ /MSCluster_ResourceGroup.Name="(.*?)"/i) {
my $rg = $1; my $rg = $1;
@ -176,9 +156,9 @@ sub manage_selection {
push @{$preferred_nodes->{$rg}}, $node; push @{$preferred_nodes->{$rg}}, $node;
} }
} }
$self->{rg} = {}; $self->{rg} = {};
$query = "Select * from MSCluster_ResourceGroup"; $query = 'Select * from MSCluster_ResourceGroup';
$resultset = $wmi->ExecQuery($query); $resultset = $wmi->ExecQuery($query);
foreach my $obj (in $resultset) { foreach my $obj (in $resultset) {
my $name = $obj->{Name}; my $name = $obj->{Name};
@ -191,9 +171,11 @@ sub manage_selection {
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next; next;
} }
$self->{rg}->{$id} = { display => $name, state => $state, owner_node => $owner_node, $self->{rg}->{$id} = {
preferred_owners => defined($preferred_nodes->{$name}) ? $preferred_nodes->{$name} : [] }; display => $name, state => $state, owner_node => $owner_node,
preferred_owners => defined($preferred_nodes->{$name}) ? $preferred_nodes->{$name} : []
};
} }
} }
@ -218,7 +200,7 @@ Can used special variables like: %{state}, %{display}, %{owner_node}
=item B<--warning-status> =item B<--warning-status>
Set warning threshold for status (Default: 'not is_preferred_node()'). Set warning threshold for status (Default: '%{is_preferred_node} == 0').
Can used special variables like: %{state}, %{display}, %{owner_node} Can used special variables like: %{state}, %{display}, %{owner_node}
=item B<--critical-status> =item B<--critical-status>

View File

@ -41,7 +41,7 @@ sub custom_content_output {
my $msg = 'HTTP test(s)'; my $msg = 'HTTP test(s)';
if (!$self->{output}->is_status(value => $self->{instance_mode}->{content_status}, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $self->{instance_mode}->{content_status}, compare => 'ok', litteral => 1)) {
my $filter = $self->{instance_mode}->{option_results}->{lc($self->{instance_mode}->{content_status}) . '-content'}; my $filter = $self->{instance_mode}->{option_results}->{lc($self->{instance_mode}->{content_status}) . '-content'};
$filter =~ s/\$self->\{result_values\}->/%/g; $filter =~ s/\$values->/%/g;
$msg = sprintf("Content test [filter: '%s']", $filter); $msg = sprintf("Content test [filter: '%s']", $filter);
} }

View File

@ -28,56 +28,86 @@ use List::Util qw (min max sum);
use JSON; use JSON;
my $config_data; my $config_data;
sub get_printf_vars {
my ($self, %options) = @_;
# compat
if (ref($options{printf_var}) ne 'ARRAY') {
$options{printf_var} = [split /,/, $options{printf_var}];
}
my $message;
my $vars = [];
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
foreach my $var (@{$options{printf_var}}) {
$var =~ s/\$self->\{result_values\}/\$values/;
push @$vars, $self->{output}->assign_eval(eval => $var, values => $options{values});
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'eval printf_var problem: ' . $message);
$self->{output}->option_exit();
}
return $vars;
}
sub custom_metric_output { sub custom_metric_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg;
my $message;
my $output;
if ($self->{result_values}->{type} eq 'unique') { if ($self->{result_values}->{type} eq 'unique') {
if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}) && defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg})) { if (defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}) && defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", );
eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); $output = sprintf(
}; $config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_msg},
@$vars
);
} elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) { } elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); );
}; $output = sprintf(
$config_data->{formatting}->{printf_msg},
@$vars
);
} else { } else {
$msg = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value}); $output = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value});
} }
} }
if ($self->{result_values}->{type} eq 'global') { if ($self->{result_values}->{type} eq 'global') {
if (defined($config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting})) { if (defined($config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", );
eval "$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); $output = sprintf(
}; $config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting}->{printf_msg},
@$vars
);
} elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) { } elsif (defined($config_data->{formatting}->{printf_var}) && defined($config_data->{formatting}->{printf_msg})) {
eval { my $vars = $self->{instance_mode}->get_printf_vars(
local $SIG{__WARN__} = sub { $message = $_[0]; }; printf_var => $config_data->{formatting}->{printf_var},
local $SIG{__DIE__} = sub { $message = $_[0]; }; values => $self->{result_values}
$msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); );
}; $output = sprintf(
$config_data->{formatting}->{printf_msg},
@$vars
);
} else { } else {
$msg = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value}); $output = sprintf("Metric '%s' value is '%s'", $self->{result_values}->{instance}, $self->{result_values}->{value});
} }
} }
if (defined($message)) { return $output;
$self->{output}->output_add(long_msg => 'printf expression problem: ' . $message);
$self->{output}->option_exit();
}
return $msg;
} }
sub custom_metric_calc { sub custom_metric_calc {
@ -109,15 +139,15 @@ sub custom_metric_calc {
sub custom_metric_perfdata { sub custom_metric_perfdata {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{output}->perfdata_add(label => $self->{result_values}->{instance}, $self->{output}->perfdata_add(
value => $self->{result_values}->{perfdata_value}, label => $self->{result_values}->{instance},
warning => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance}), value => $self->{result_values}->{perfdata_value},
critical => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance}), warning => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance}),
unit => $self->{result_values}->{perfdata_unit}, critical => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance}),
min => $self->{result_values}->{min}, unit => $self->{result_values}->{perfdata_unit},
max => $self->{result_values}->{max}, min => $self->{result_values}->{min},
); max => $self->{result_values}->{max},
);
} }
sub custom_metric_threshold { sub custom_metric_threshold {
@ -126,9 +156,13 @@ sub custom_metric_threshold {
my $label_warn = ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance}; my $label_warn = ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance};
my $label_crit = ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance}; my $label_crit = ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance};
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{perfdata_value}, my $exit = $self->{perfdata}->threshold_check(
threshold => [ { label => $label_crit, exit_litteral => 'critical' }, value => $self->{result_values}->{perfdata_value},
{ label => $label_warn, exit_litteral => 'warning' } ]); threshold => [
{ label => $label_crit, exit_litteral => 'critical' },
{ label => $label_warn, exit_litteral => 'warning' }
]
);
return $exit; return $exit;
} }
@ -143,7 +177,7 @@ sub set_counters {
closure_custom_calc => $self->can('custom_metric_calc'), closure_custom_calc => $self->can('custom_metric_calc'),
closure_custom_output => $self->can('custom_metric_output'), closure_custom_output => $self->can('custom_metric_output'),
closure_custom_perfdata => $self->can('custom_metric_perfdata'), closure_custom_perfdata => $self->can('custom_metric_perfdata'),
closure_custom_threshold_check => $self->can('custom_metric_threshold'), closure_custom_threshold_check => $self->can('custom_metric_threshold')
} }
} }
]; ];
@ -154,7 +188,7 @@ sub set_counters {
closure_custom_calc => $self->can('custom_metric_calc'), closure_custom_calc => $self->can('custom_metric_calc'),
closure_custom_output => $self->can('custom_metric_output'), closure_custom_output => $self->can('custom_metric_output'),
closure_custom_perfdata => $self->can('custom_metric_perfdata'), closure_custom_perfdata => $self->can('custom_metric_perfdata'),
closure_custom_threshold_check => $self->can('custom_metric_threshold'), closure_custom_threshold_check => $self->can('custom_metric_threshold')
} }
} }
]; ];
@ -165,10 +199,10 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'config:s' => { name => 'config' }
"config:s" => { name => 'config' }, });
});
return $self; return $self;
} }
@ -291,15 +325,19 @@ sub manage_selection {
} }
next if (!defined($self->{vmetrics}->{$vcurve}->{values}) || scalar(@{$self->{vmetrics}->{$vcurve}->{values}}) == 0); next if (!defined($self->{vmetrics}->{$vcurve}->{values}) || scalar(@{$self->{vmetrics}->{$vcurve}->{values}}) == 0);
$self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, $self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf(
sum(@{$self->{vmetrics}->{$vcurve}->{values}}) / scalar(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'avg'); $config_data->{formatting}->{printf_metric_value},
$self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, sum(@{$self->{vmetrics}->{$vcurve}->{values}}) / scalar(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'avg');
sum(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'sum'); $self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf(
$self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, $config_data->{formatting}->{printf_metric_value},
min(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'min'); sum(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'sum');
$self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, $self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf(
max(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'max'); $config_data->{formatting}->{printf_metric_value},
min(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'min');
$self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf(
$config_data->{formatting}->{printf_metric_value},
max(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'max');
$self->{vmetrics}->{$vcurve}->{aggregated_value} = eval "$self->{vmetrics}->{$vcurve}->{aggregated_value} $config_data->{virtualcurve}->{$vcurve}->{custom}" if (defined($config_data->{virtualcurve}->{$vcurve}->{custom})); $self->{vmetrics}->{$vcurve}->{aggregated_value} = eval "$self->{vmetrics}->{$vcurve}->{aggregated_value} $config_data->{virtualcurve}->{$vcurve}->{custom}" if (defined($config_data->{virtualcurve}->{$vcurve}->{custom}));
$self->{vmetrics}->{$vcurve}->{unit} = (defined($config_data->{virtualcurve}->{$vcurve}->{unit})) ? $config_data->{virtualcurve}->{$vcurve}->{unit} : ''; $self->{vmetrics}->{$vcurve}->{unit} = (defined($config_data->{virtualcurve}->{$vcurve}->{unit})) ? $config_data->{virtualcurve}->{$vcurve}->{unit} : '';

View File

@ -36,14 +36,15 @@ sub custom_select_threshold {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
our $expand = $self->{result_values}->{expand};
if (defined($self->{result_values}->{config}->{critical}) && $self->{result_values}->{config}->{critical} && if (defined($self->{result_values}->{config}->{critical}) && $self->{result_values}->{config}->{critical} &&
eval "$self->{result_values}->{config}->{critical}") { $self->{safe}->reval($self->{result_values}->{config}->{critical})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{result_values}->{config}->{warning}) && $self->{result_values}->{config}->{warning} ne '' && } elsif (defined($self->{result_values}->{config}->{warning}) && $self->{result_values}->{config}->{warning} ne '' &&
eval "$self->{result_values}->{config}->{warning}") { $self->{safe}->reval($self->{result_values}->{config}->{warning})) {
$status = 'warning'; $status = 'warning';
} elsif (defined($self->{result_values}->{config}->{unknown}) && $self->{result_values}->{config}->{unknown} && } elsif (defined($self->{result_values}->{config}->{unknown}) && $self->{result_values}->{config}->{unknown} &&
eval "$self->{result_values}->{config}->{unknown}") { $self->{safe}->reval($self->{result_values}->{config}->{unknown})) {
$status = 'unknown'; $status = 'unknown';
} }
if ($@) { if ($@) {
@ -128,6 +129,8 @@ sub new {
'filter-selection:s%' => { name => 'filter_selection' }, 'filter-selection:s%' => { name => 'filter_selection' },
}); });
$self->{safe} = Safe->new();
$self->{safe}->share('$expand');
$self->{snmp_cache} = centreon::plugins::statefile->new(%options); $self->{snmp_cache} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
@ -732,7 +735,7 @@ sub prepare_variables {
my ($self, %options) = @_; my ($self, %options) = @_;
return undef if (!defined($options{value})); return undef if (!defined($options{value}));
$options{value} =~ s/%\(([a-z-A-Z0-9\.]+?)\)/\$self->{result_values}->{expand}->{'$1'}/g; $options{value} =~ s/%\(([a-z-A-Z0-9\.]+?)\)/\$expand->{'$1'}/g;
return $options{value}; return $options{value};
} }
@ -740,8 +743,14 @@ sub check_filter {
my ($self, %options) = @_; my ($self, %options) = @_;
return 0 if (!defined($options{filter}) || $options{filter} eq ''); return 0 if (!defined($options{filter}) || $options{filter} eq '');
$options{filter} =~ s/%\(([a-z-A-Z0-9\.]+?)\)/\$self->{expand}->{'$1'}/g; our $expand = $self->{expand};
return 0 if (eval "$options{filter}"); $options{filter} =~ s/%\(([a-z-A-Z0-9\.]+?)\)/\$expand->{'$1'}/g;
my $result = $self->{safe}->reval("$options{filter}");
if ($@) {
$self->{output}->add_option_msg(short_msg => 'Unsafe code evaluation: ' . $@);
$self->{output}->option_exit();
}
return 0 if ($result);
return 1; return 1;
} }

View File

@ -159,7 +159,7 @@ sub connector_response_status {
} }
foreach (('unknown_connector_status', 'warning_connector_status', 'critical_connector_status')) { foreach (('unknown_connector_status', 'warning_connector_status', 'critical_connector_status')) {
$self->{$_} =~ s/%\{(.*?)\}/\$self->{result}->{$1}/g; $self->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
# Check response # Check response
@ -170,13 +170,13 @@ sub connector_response_status {
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{critical_connector_status}) && $self->{critical_connector_status} ne '' && if (defined($self->{critical_connector_status}) && $self->{critical_connector_status} ne '' &&
eval "$self->{critical_connector_status}") { $self->{output}->test_eval(test => $self->{critical_connector_status}, values => $self->{result})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{warning_connector_status}) && $self->{warning_connector_status} ne '' && } elsif (defined($self->{warning_connector_status}) && $self->{warning_connector_status} ne '' &&
eval "$self->{warning_connector_status}") { $self->{output}->test_eval(test => $self->{warning_connector_status}, values => $self->{result})) {
$status = 'warning'; $status = 'warning';
} elsif (defined($self->{unknown_connector_status}) && $self->{unknown_connector_status} ne '' && } elsif (defined($self->{unknown_connector_status}) && $self->{unknown_connector_status} ne '' &&
eval "$self->{unknown_connector_status}") { $self->{output}->test_eval(test => $self->{unknown_connector_status}, values => $self->{result})) {
$status = 'unknown'; $status = 'unknown';
} }
}; };

View File

@ -36,14 +36,12 @@ sub custom_status_threshold {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
my $label = $self->{label}; if (defined($self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}} ne '' &&
$label =~ s/-/_/g; $self->eval(value => $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}})) {
if (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") {
$self->{instance_mode}->{dc_critical}++; $self->{instance_mode}->{dc_critical}++;
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'warning-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'warning_' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}})) {
$self->{instance_mode}->{dc_warning}++; $self->{instance_mode}->{dc_warning}++;
$status = 'warning'; $status = 'warning';
} }
@ -58,7 +56,7 @@ sub custom_status_threshold {
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = sprintf( return sprintf(
'alarm [%s] [%s] [%s] [%s] %s/%s', 'alarm [%s] [%s] [%s] [%s] %s/%s',
$self->{result_values}->{status}, $self->{result_values}->{status},
$self->{result_values}->{type}, $self->{result_values}->{type},
@ -67,8 +65,6 @@ sub custom_status_output {
$self->{result_values}->{name}, $self->{result_values}->{name},
$self->{result_values}->{description} $self->{result_values}->{description}
); );
return $msg;
} }
sub custom_dcmetrics_perfdata { sub custom_dcmetrics_perfdata {
@ -131,7 +127,11 @@ sub set_counters {
]; ];
$self->{maps_counters}->{alarm} = [ $self->{maps_counters}->{alarm} = [
{ label => 'status', threshold => 0, set => { {
label => 'status', type => 2,
warning_default => '%{status} =~ /yellow/i',
critical_default => '%{status} =~ /red/i',
set => {
key_values => [ key_values => [
{ name => 'entity_name' }, { name => 'status' }, { name => 'entity_name' }, { name => 'status' },
{ name => 'time' }, { name => 'description' }, { name => 'name' }, { name => 'type' }, { name => 'since' } { name => 'time' }, { name => 'description' }, { name => 'name' }, { name => 'type' }, { name => 'since' }
@ -144,7 +144,7 @@ sub set_counters {
]; ];
$self->{maps_counters}->{dc_metrics} = [ $self->{maps_counters}->{dc_metrics} = [
{ label => 'alarm-warning', threshold => 0, set => { { label => 'alarm-warning', type => 2, set => {
key_values => [ { name => 'name' } ], key_values => [ { name => 'name' } ],
output_template => '', output_template => '',
closure_custom_threshold_check => sub { return 'ok' }, closure_custom_threshold_check => sub { return 'ok' },
@ -152,7 +152,7 @@ sub set_counters {
closure_custom_perfdata => $self->can('custom_dcmetrics_perfdata') closure_custom_perfdata => $self->can('custom_dcmetrics_perfdata')
} }
}, },
{ label => 'alarm-critical', threshold => 0, set => { { label => 'alarm-critical', type => 2, set => {
key_values => [ { name => 'name' } ], key_values => [ { name => 'name' } ],
output_template => '', output_template => '',
closure_custom_threshold_check => sub { return 'ok' }, closure_custom_threshold_check => sub { return 'ok' },
@ -171,7 +171,7 @@ sub prefix_datacenter_output {
sub alarm_reset { sub alarm_reset {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{dc_warning} = 0; $self->{dc_warning} = 0;
$self->{dc_critical} = 0; $self->{dc_critical} = 0;
} }
@ -188,16 +188,16 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"datacenter:s" => { name => 'datacenter' }, 'datacenter:s' => { name => 'datacenter' },
"filter" => { name => 'filter' }, 'filter' => { name => 'filter' },
"filter-time:s" => { name => 'filter_time', }, 'filter-time:s' => { name => 'filter_time' },
"memory" => { name => 'memory', }, 'memory' => { name => 'memory' }
"warning-status:s" => { name => 'warning_status', default => '%{status} =~ /yellow/i' },
"critical-status:s" => { name => 'critical_status', default => '%{status} =~ /red/i' },
}); });
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse', centreon::plugins::misc::mymodule_load(
error_msg => "Cannot load module 'Date::Parse'."); output => $self->{output}, module => 'Date::Parse',
error_msg => "Cannot load module 'Date::Parse'."
);
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self; return $self;
@ -207,7 +207,6 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status']);
if (defined($self->{option_results}->{memory})) { if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->check_options(%options); $self->{statefile_cache}->check_options(%options);
} }

View File

@ -36,14 +36,12 @@ sub custom_status_threshold {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
my $label = $self->{label}; if (defined($self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}} ne '' &&
$label =~ s/-/_/g; $self->eval(value => $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}})) {
if (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") {
$self->{instance_mode}->{host_critical}++; $self->{instance_mode}->{host_critical}++;
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'warning-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'warning_' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}})) {
$self->{instance_mode}->{host_warning}++; $self->{instance_mode}->{host_warning}++;
$status = 'warning'; $status = 'warning';
} }
@ -58,7 +56,8 @@ sub custom_status_threshold {
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = sprintf("alarm [%s] [%s] [%s] [%s] %s/%s", return sprintf(
"alarm [%s] [%s] [%s] [%s] %s/%s",
$self->{result_values}->{status}, $self->{result_values}->{status},
$self->{result_values}->{type}, $self->{result_values}->{type},
$self->{result_values}->{entity_name}, $self->{result_values}->{entity_name},
@ -66,8 +65,6 @@ sub custom_status_output {
$self->{result_values}->{name}, $self->{result_values}->{name},
$self->{result_values}->{description} $self->{result_values}->{description}
); );
return $msg;
} }
sub custom_esxhost_perfdata { sub custom_esxhost_perfdata {
@ -129,9 +126,15 @@ sub set_counters {
]; ];
$self->{maps_counters}->{alarm} = [ $self->{maps_counters}->{alarm} = [
{ label => 'status', threshold => 0, set => { {
key_values => [ { name => 'entity_name' }, { name => 'status' }, label => 'status', type => 2,
{ name => 'time' }, { name => 'description' }, { name => 'name' }, { name => 'type' }, { name => 'since' } ], warning_default => '%{status} =~ /yellow/i',
critical_default => '%{status} =~ /red/i',
set => {
key_values => [
{ name => 'entity_name' }, { name => 'status' },
{ name => 'time' }, { name => 'description' }, { name => 'name' }, { name => 'type' }, { name => 'since' }
],
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold') closure_custom_threshold_check => $self->can('custom_status_threshold')
@ -140,7 +143,7 @@ sub set_counters {
]; ];
$self->{maps_counters}->{esxhost_metrics} = [ $self->{maps_counters}->{esxhost_metrics} = [
{ label => 'alarm-warning', threshold => 0, set => { { label => 'alarm-warning', type => 2, set => {
key_values => [ { name => 'name' } ], key_values => [ { name => 'name' } ],
output_template => '', output_template => '',
closure_custom_threshold_check => sub { return 'ok' }, closure_custom_threshold_check => sub { return 'ok' },
@ -148,7 +151,7 @@ sub set_counters {
closure_custom_perfdata => $self->can('custom_esxhost_perfdata') closure_custom_perfdata => $self->can('custom_esxhost_perfdata')
} }
}, },
{ label => 'alarm-critical', threshold => 0, set => { { label => 'alarm-critical', type => 2, set => {
key_values => [ { name => 'name' } ], key_values => [ { name => 'name' } ],
output_template => '', output_template => '',
closure_custom_threshold_check => sub { return 'ok' }, closure_custom_threshold_check => sub { return 'ok' },
@ -183,20 +186,19 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'esx-hostname:s' => { name => 'esx_hostname' },
"esx-hostname:s" => { name => 'esx_hostname' }, 'filter' => { name => 'filter' },
"filter" => { name => 'filter' }, 'scope-datacenter:s' => { name => 'scope_datacenter' },
"scope-datacenter:s" => { name => 'scope_datacenter' }, 'scope-cluster:s' => { name => 'scope_cluster' },
"scope-cluster:s" => { name => 'scope_cluster' }, 'filter-time:s' => { name => 'filter_time' },
"filter-time:s" => { name => 'filter_time', }, 'memory' => { name => 'memory' }
"memory" => { name => 'memory', }, });
"warning-status:s" => { name => 'warning_status', default => '%{status} =~ /yellow/i' },
"critical-status:s" => { name => 'critical_status', default => '%{status} =~ /red/i' },
});
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse', centreon::plugins::misc::mymodule_load(
error_msg => "Cannot load module 'Date::Parse'."); output => $self->{output}, module => 'Date::Parse',
error_msg => "Cannot load module 'Date::Parse'."
);
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
@ -205,7 +207,6 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status']);
if (defined($self->{option_results}->{memory})) { if (defined($self->{option_results}->{memory})) {
$self->{statefile_cache}->check_options(%options); $self->{statefile_cache}->check_options(%options);
} }
@ -216,8 +217,10 @@ sub manage_selection {
$self->{global} = { yellow => 0, red => 0 }; $self->{global} = { yellow => 0, red => 0 };
$self->{esxhost} = {}; $self->{esxhost} = {};
my $response = $options{custom}->execute(params => $self->{option_results}, my $response = $options{custom}->execute(
command => 'alarmhost'); params => $self->{option_results},
command => 'alarmhost'
);
my $last_time; my $last_time;
if (defined($self->{option_results}->{memory})) { if (defined($self->{option_results}->{memory})) {

View File

@ -73,18 +73,18 @@ sub check {
($self->{data}->{scenario}, $self->{data}->{result}, $self->{data}->{latency}, $self->{data}->{error}) = ($self->{data}->{scenario}, $self->{data}->{result}, $self->{data}->{latency}, $self->{data}->{error}) =
($self->{output}->decode($1), centreon::plugins::misc::trim($2), ($self->{output}->decode($1), centreon::plugins::misc::trim($2),
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4));
$checked++; $checked++;
my ($status, $message) = ('ok'); my ($status, $message) = ('ok');
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' && if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' &&
eval "$self->{option_results}->{critical}") { $self->{output}->test_eval(test => $self->{option_results}->{critical}, values => $self->{data})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' && } elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' &&
eval "$self->{option_results}->{warning}") { $self->{output}->test_eval(test => $self->{option_results}->{warning}, values => $self->{data})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -92,21 +92,29 @@ sub check {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message); $self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status, $self->{output}->output_add(
short_msg => sprintf("ActiveSync scenario '%s' to '%s' is '%s'", severity => $status,
$self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result})); short_msg => sprintf(
"ActiveSync scenario '%s' to '%s' is '%s'",
$self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result}
)
);
} }
if ($self->{data}->{latency} =~ /^(\d+)/) { if ($self->{data}->{latency} =~ /^(\d+)/) {
$self->{output}->perfdata_add(label => $self->{data}->{scenario}, unit => 's', $self->{output}->perfdata_add(
value => sprintf("%.3f", $1 / 1000), label => $self->{data}->{scenario}, unit => 's',
min => 0); value => sprintf("%.3f", $1 / 1000),
min => 0
);
} }
} }
if ($checked == 0) { if ($checked == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find informations'); severity => 'UNKNOWN',
short_msg => 'Cannot find informations'
);
} }
} }

View File

@ -56,13 +56,13 @@ exit 0
sub check { sub check {
my ($self, %options) = @_; my ($self, %options) = @_;
# options: stdout
# Following output: # Following output:
#[scenario= Options ][result= Failure ][latency= 52,00 ][[error=...]] #[scenario= Options ][result= Failure ][latency= 52,00 ][[error=...]]
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => "Imap to '" . $options{mailbox} . "' is ok."); severity => 'OK',
short_msg => "Imap to '" . $options{mailbox} . "' is ok."
);
my $checked = 0; my $checked = 0;
$self->{output}->output_add(long_msg => $options{stdout}); $self->{output}->output_add(long_msg => $options{stdout});
while ($options{stdout} =~ /\[scenario=(.*?)\]\[result=(.*?)\]\[latency=(.*?)\]\[\[error=(.*?)\]\]/msg) { while ($options{stdout} =~ /\[scenario=(.*?)\]\[result=(.*?)\]\[latency=(.*?)\]\[\[error=(.*?)\]\]/msg) {
@ -72,17 +72,17 @@ sub check {
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4));
$checked++; $checked++;
my ($status, $message) = ('ok'); my ($status, $message) = ('ok');
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' && if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' &&
eval "$self->{option_results}->{critical}") { $self->{output}->test_eval(test => $self->{option_results}->{critical}, values => $self->{data})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' && } elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' &&
eval "$self->{option_results}->{warning}") { $self->{output}->test_eval(test => $self->{option_results}->{warning}, values => $self->{data})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -90,11 +90,15 @@ sub check {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message); $self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status, $self->{output}->output_add(
short_msg => sprintf("Imap scenario '%s' to '%s' is '%s'", severity => $status,
$self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result})); short_msg => sprintf(
"Imap scenario '%s' to '%s' is '%s'",
$self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result}
)
);
} }
if ($self->{data}->{latency} =~ /^(\d+)/) { if ($self->{data}->{latency} =~ /^(\d+)/) {
$self->{output}->perfdata_add( $self->{output}->perfdata_add(
label => $self->{data}->{scenario}, unit => 's', label => $self->{data}->{scenario}, unit => 's',
@ -103,10 +107,12 @@ sub check {
); );
} }
} }
if ($checked == 0) { if ($checked == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find informations'); severity => 'UNKNOWN',
short_msg => 'Cannot find informations'
);
} }
} }

View File

@ -49,14 +49,14 @@ exit 0
sub check { sub check {
my ($self, %options) = @_; my ($self, %options) = @_;
# options: stdout
# Following output: # Following output:
#[name= Mailbox Database 0975194476 ][server= SRVI-WIN-TEST ][result= Success ][error=...] #[name= Mailbox Database 0975194476 ][server= SRVI-WIN-TEST ][result= Success ][error=...]
if ($options{stdout} !~ /^\[name=(.*?)\]\[server=(.*?)\]\[result=(.*?)\]\[error=(.*)\]$/) { if ($options{stdout} !~ /^\[name=(.*?)\]\[server=(.*?)\]\[result=(.*?)\]\[error=(.*)\]$/) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find informations'); severity => 'UNKNOWN',
short_msg => 'Cannot find informations'
);
return ; return ;
} }
$self->{data} = {}; $self->{data} = {};
@ -64,21 +64,27 @@ sub check {
(centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2),
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4));
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => "MAPI connection to '" . $options{mailbox} . "' is '" . $self->{data}->{result} . "'."); severity => 'OK',
$self->{output}->output_add(long_msg => sprintf("Database: %s, Server: %s\nError: %s", short_msg => "MAPI connection to '" . $options{mailbox} . "' is '" . $self->{data}->{result} . "'."
$self->{data}->{database}, $self->{data}->{server}, $self->{data}->{error})); );
$self->{output}->output_add(
long_msg => sprintf(
"Database: %s, Server: %s\nError: %s",
$self->{data}->{database}, $self->{data}->{server}, $self->{data}->{error}
)
);
my ($status, $message) = ('ok'); my ($status, $message) = ('ok');
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' && if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' &&
eval "$self->{option_results}->{critical}") { $self->{output}->test_eval(test => $self->{option_results}->{critical}, values => $self->{data})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' && } elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' &&
eval "$self->{option_results}->{warning}") { $self->{output}->test_eval(test => $self->{option_results}->{warning}, values => $self->{data})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -86,9 +92,13 @@ sub check {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message); $self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status, $self->{output}->output_add(
short_msg => sprintf("MAPI connection to '%s' is '%s'", severity => $status,
$options{mailbox}, $self->{data}->{result})); short_msg => sprintf(
"MAPI connection to '%s' is '%s'",
$options{mailbox}, $self->{data}->{result}
)
);
} }
} }

View File

@ -65,13 +65,14 @@ exit 0
sub check { sub check {
my ($self, %options) = @_; my ($self, %options) = @_;
# options: stdout
# Following output: # Following output:
#[id= XXXX ][type= Success][[message=...]] #[id= XXXX ][type= Success][[message=...]]
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => sprintf("Outlook webservices to '%s' are ok.", $options{mailbox})); severity => 'OK',
short_msg => sprintf("Outlook webservices to '%s' are ok.", $options{mailbox})
);
my $checked = 0; my $checked = 0;
$self->{output}->output_add(long_msg => $options{stdout}); $self->{output}->output_add(long_msg => $options{stdout});
while ($options{stdout} =~ /\[id=(.*?)\]\[type=(.*?)\]\[\[message=(.*?)\]\]/msg) { while ($options{stdout} =~ /\[id=(.*?)\]\[type=(.*?)\]\[\[message=(.*?)\]\]/msg) {
@ -80,17 +81,17 @@ sub check {
(centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), $self->{output}->decode($3)); (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), $self->{output}->decode($3));
$checked++; $checked++;
my ($status, $message) = ('ok'); my ($status, $message) = ('ok');
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' && if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' &&
eval "$self->{option_results}->{critical}") { $self->{output}->test_eval(test => $self->{option_results}->{critical}, values => $self->{data})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' && } elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' &&
eval "$self->{option_results}->{warning}") { $self->{output}->test_eval(test => $self->{option_results}->{warning}, values => $self->{data})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -98,15 +99,21 @@ sub check {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message); $self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status, $self->{output}->output_add(
short_msg => sprintf("Check id '%s' status is '%s' [message: %s]", severity => $status,
$self->{data}->{id}, $self->{data}->{type}, $self->{data}->{message})); short_msg => sprintf(
"Check id '%s' status is '%s' [message: %s]",
$self->{data}->{id}, $self->{data}->{type}, $self->{data}->{message}
)
);
} }
} }
if ($checked == 0) { if ($checked == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find informations'); severity => 'UNKNOWN',
short_msg => 'Cannot find informations'
);
} }
} }

View File

@ -57,13 +57,14 @@ exit 0
sub check { sub check {
my ($self, %options) = @_; my ($self, %options) = @_;
# options: stdout
# Following output: # Following output:
#[url= http://xxxx/ ][scenario= Options ][result= Ignored ][latency= ][[error=...]] #[url= http://xxxx/ ][scenario= Options ][result= Ignored ][latency= ][[error=...]]
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => "OWA to '" . $options{mailbox} . "' is ok."); severity => 'OK',
short_msg => "OWA to '" . $options{mailbox} . "' is ok."
);
my $checked = 0; my $checked = 0;
$self->{output}->output_add(long_msg => $options{stdout}); $self->{output}->output_add(long_msg => $options{stdout});
while ($options{stdout} =~ /\[url=(.*?)\]\[scenario=(.*?)\]\[result=(.*?)\]\[latency=(.*?)\]\[\[error=(.*?)\]\]/msg) { while ($options{stdout} =~ /\[url=(.*?)\]\[scenario=(.*?)\]\[result=(.*?)\]\[latency=(.*?)\]\[\[error=(.*?)\]\]/msg) {
@ -71,18 +72,18 @@ sub check {
($self->{data}->{url}, $self->{data}->{scenario}, $self->{data}->{result}, $self->{data}->{latency}, $self->{data}->{error}) = ($self->{data}->{url}, $self->{data}->{scenario}, $self->{data}->{result}, $self->{data}->{latency}, $self->{data}->{error}) =
($self->{output}->decode($1), $self->{output}->decode($2), centreon::plugins::misc::trim($3), ($self->{output}->decode($1), $self->{output}->decode($2), centreon::plugins::misc::trim($3),
centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5)); centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5));
$checked++; $checked++;
my ($status, $message) = ('ok'); my ($status, $message) = ('ok');
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' && if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' &&
eval "$self->{option_results}->{critical}") { $self->{output}->test_eval(test => $self->{option_results}->{critical}, values => $self->{data})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' && } elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' &&
eval "$self->{option_results}->{warning}") { $self->{output}->test_eval(test => $self->{option_results}->{warning}, values => $self->{data})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -90,21 +91,29 @@ sub check {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message); $self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status, $self->{output}->output_add(
short_msg => sprintf("OWA scenario '%s' to '%s' is '%s' [url: %s]", severity => $status,
$self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result}, $self->{data}->{url})); short_msg => sprintf(
"OWA scenario '%s' to '%s' is '%s' [url: %s]",
$self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result}, $self->{data}->{url}
)
);
} }
if ($self->{data}->{latency} =~ /^(\d+)/) { if ($self->{data}->{latency} =~ /^(\d+)/) {
$self->{output}->perfdata_add(label => $self->{data}->{url} . '_' . $self->{data}->{scenario}, unit => 's', $self->{output}->perfdata_add(
value => sprintf("%.3f", $1 / 1000), label => $self->{data}->{url} . '_' . $self->{data}->{scenario}, unit => 's',
min => 0); value => sprintf("%.3f", $1 / 1000),
min => 0
);
} }
} }
if ($checked == 0) { if ($checked == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find informations'); severity => 'UNKNOWN',
short_msg => 'Cannot find informations'
);
} }
} }

View File

@ -50,13 +50,14 @@ exit 0
sub check { sub check {
my ($self, %options) = @_; my ($self, %options) = @_;
# options: stdout
# Following output: # Following output:
#[Server= XXXX ][check= ReplayService][result= Passed ][isvalid= Yes][[error=...]] #[Server= XXXX ][check= ReplayService][result= Passed ][isvalid= Yes][[error=...]]
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => "All replication health tests are ok."); severity => 'OK',
short_msg => "All replication health tests are ok."
);
my $checked = 0; my $checked = 0;
$self->{output}->output_add(long_msg => $options{stdout}); $self->{output}->output_add(long_msg => $options{stdout});
while ($options{stdout} =~ /\[server=(.*?)\]\[check=(.*?)\]\[result=(.*?)\]\[isvalid=(.*?)\]\[\[error=(.*?)\]\]/msg) { while ($options{stdout} =~ /\[server=(.*?)\]\[check=(.*?)\]\[result=(.*?)\]\[isvalid=(.*?)\]\[\[error=(.*?)\]\]/msg) {
@ -66,17 +67,17 @@ sub check {
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5)); centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5));
$checked++; $checked++;
my ($status, $message) = ('ok'); my ($status, $message) = ('ok');
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' && if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' &&
eval "$self->{option_results}->{critical}") { $self->{output}->test_eval(test => $self->{option_results}->{critical}, values => $self->{data})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' && } elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' &&
eval "$self->{option_results}->{warning}") { $self->{output}->test_eval(test => $self->{option_results}->{warning}, values => $self->{data})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -84,15 +85,21 @@ sub check {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message); $self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status, $self->{output}->output_add(
short_msg => sprintf("Replication test '%s' status on '%s' is '%s' [error: %s]", severity => $status,
$self->{data}->{check}, $self->{data}->{server}, $self->{data}->{result}, $self->{data}->{error})); short_msg => sprintf(
"Replication test '%s' status on '%s' is '%s' [error: %s]",
$self->{data}->{check}, $self->{data}->{server}, $self->{data}->{result}, $self->{data}->{error}
)
);
} }
} }
if ($checked == 0) { if ($checked == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find informations'); severity => 'UNKNOWN',
short_msg => 'Cannot find informations'
);
} }
} }

View File

@ -52,13 +52,14 @@ exit 0
sub check { sub check {
my ($self, %options) = @_; my ($self, %options) = @_;
# options: stdout
# Following output: # Following output:
#[role= Mailbox Server Role ][requiredservicesrunning= True ][servicesrunning= IISAdmin,MSExchangeADTopology,MSExchangeSA,... ][servicesnotrunning= ] #[role= Mailbox Server Role ][requiredservicesrunning= True ][servicesrunning= IISAdmin,MSExchangeADTopology,MSExchangeSA,... ][servicesnotrunning= ]
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(
short_msg => "All role services are ok."); severity => 'OK',
short_msg => "All role services are ok."
);
my $checked = 0; my $checked = 0;
$self->{output}->output_add(long_msg => $options{stdout}); $self->{output}->output_add(long_msg => $options{stdout});
while ($options{stdout} =~ /\[role=(.*?)\]\[requiredservicesrunning=(.*?)\]\[servicesrunning=(.*?)\]\[servicesnotrunning=(.*?)\]/msg) { while ($options{stdout} =~ /\[role=(.*?)\]\[requiredservicesrunning=(.*?)\]\[servicesrunning=(.*?)\]\[servicesnotrunning=(.*?)\]/msg) {
@ -68,17 +69,17 @@ sub check {
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4));
$checked++; $checked++;
my ($status, $message) = ('ok'); my ($status, $message) = ('ok');
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' && if (defined($self->{option_results}->{critical}) && $self->{option_results}->{critical} ne '' &&
eval "$self->{option_results}->{critical}") { $self->{output}->test_eval(test => $self->{option_results}->{critical}, values => $self->{data})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' && } elsif (defined($self->{option_results}->{warning}) && $self->{option_results}->{warning} ne '' &&
eval "$self->{option_results}->{warning}") { $self->{output}->test_eval(test => $self->{option_results}->{warning}, values => $self->{data})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -86,15 +87,21 @@ sub check {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message); $self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status, $self->{output}->output_add(
short_msg => sprintf("Role '%s' services problem [services not running: %s]", severity => $status,
$self->{data}->{role}, $self->{data}->{servicesnotrunning})); short_msg => sprintf(
"Role '%s' services problem [services not running: %s]",
$self->{data}->{role}, $self->{data}->{servicesnotrunning}
)
);
} }
} }
if ($checked == 0) { if ($checked == 0) {
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => 'Cannot find informations'); severity => 'UNKNOWN',
short_msg => 'Cannot find informations'
);
} }
} }

View File

@ -23,7 +23,7 @@ package centreon::common::protocols::sql::mode::sqlstring;
use base qw(centreon::plugins::templates::counter); use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -33,26 +33,16 @@ sub set_counters {
]; ];
$self->{maps_counters}->{rows} = [ $self->{maps_counters}->{rows} = [
{ label => 'string', threshold => 0, set => { { label => 'string', type => 2, set => {
key_values => [ { name => 'key_field' }, { name => 'value_field' } ], key_values => [ { name => 'key_field' }, { name => 'value_field' } ],
closure_custom_calc => $self->can('custom_string_calc'),
closure_custom_output => $self->can('custom_string_output'), closure_custom_output => $self->can('custom_string_output'),
closure_custom_threshold_check => $self->can('custom_string_threshold'), closure_custom_threshold_check => \&catalog_status_threshold_ng,
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; }
} }
}, }
]; ];
} }
sub custom_string_calc {
my ($self, %options) = @_;
$self->{result_values}->{key_field} = $options{new_datas}->{$self->{instance} . '_key_field'};
$self->{result_values}->{value_field} = $options{new_datas}->{$self->{instance} . '_value_field'};
return 0;
}
sub custom_string_output { sub custom_string_output {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -75,46 +65,19 @@ sub custom_string_output {
return $msg; return $msg;
} }
sub custom_string_threshold {
my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_string}) && $self->{instance_mode}->{option_results}->{critical_string} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_string}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_string}) && $self->{instance_mode}->{option_results}->{warning_string} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_string}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'threshold regex issue: ' . $message);
}
return $status;
}
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);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"sql-statement:s" => { name => 'sql_statement' }, 'sql-statement:s' => { name => 'sql_statement' },
"key-column:s" => { name => 'key_column' }, 'key-column:s' => { name => 'key_column' },
"value-column:s" => { name => 'value_column' }, 'value-column:s' => { name => 'value_column' },
"warning-string:s" => { name => 'warning_string', default => '' }, 'printf-format:s' => { name => 'printf_format' },
"critical-string:s" => { name => 'critical_string', default => '' }, 'printf-value:s' => { name => 'printf_value' },
"printf-format:s" => { name => 'printf_format' }, 'dual-table' => { name => 'dual_table' },
"printf-value:s" => { name => 'printf_value' }, 'empty-sql-string:s' => { name => 'empty_sql_string', default => 'No row returned or --key-column/--value-column do not correctly match selected field' }
"dual-table" => { name => 'dual_table' },
"empty-sql-string:s" => { name => 'empty_sql_string', default => 'No row returned or --key-column/--value-column do not correctly match selected field' },
}); });
return $self; return $self;
@ -128,19 +91,17 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Need to specify '--sql-statement' option."); $self->{output}->add_option_msg(short_msg => "Need to specify '--sql-statement' option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->change_macros(macros => ['warning_string', 'critical_string']);
} }
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{sql} = $options{sql};
$self->{sql}->connect(); $options{sql}->connect();
$self->{sql}->query(query => $self->{option_results}->{sql_statement}); $options{sql}->query(query => $self->{option_results}->{sql_statement});
$self->{rows} = {}; $self->{rows} = {};
my $row_count = 0; my $row_count = 0;
while (my $row = $self->{sql}->fetchrow_hashref()) { while (my $row = $options{sql}->fetchrow_hashref()) {
if (defined($self->{option_results}->{dual_table})) { if (defined($self->{option_results}->{dual_table})) {
$row->{$self->{option_results}->{value_column}} = delete $row->{keys %{$row}}; $row->{$self->{option_results}->{value_column}} = delete $row->{keys %{$row}};
foreach (keys %{$row}) { foreach (keys %{$row}) {
@ -148,17 +109,21 @@ sub manage_selection {
} }
} }
if (!defined($self->{option_results}->{key_column})) { if (!defined($self->{option_results}->{key_column})) {
$self->{rows}->{$self->{option_results}->{value_column} . $row_count} = { key_field => $row->{$self->{option_results}->{value_column}}, $self->{rows}->{$self->{option_results}->{value_column} . $row_count} = {
value_field => $row->{$self->{option_results}->{value_column}}}; key_field => $row->{ $self->{option_results}->{value_column} },
value_field => $row->{ $self->{option_results}->{value_column} }
};
$row_count++; $row_count++;
} else { } else {
$self->{rows}->{$self->{option_results}->{key_column} . $row_count} = { key_field => $row->{$self->{option_results}->{key_column}}, $self->{rows}->{$self->{option_results}->{key_column} . $row_count} = {
value_field => $row->{$self->{option_results}->{value_column}}}; key_field => $row->{ $self->{option_results}->{key_column} },
value_field => $row->{ $self->{option_results}->{value_column} }
};
$row_count++; $row_count++;
} }
} }
$self->{sql}->disconnect(); $options{sql}->disconnect();
if (scalar(keys %{$self->{rows}}) <= 0) { if (scalar(keys %{$self->{rows}}) <= 0) {
$self->{output}->add_option_msg(short_msg => $self->{option_results}->{empty_sql_string}); $self->{output}->add_option_msg(short_msg => $self->{option_results}->{empty_sql_string});
$self->{output}->option_exit(); $self->{output}->option_exit();

View File

@ -59,7 +59,7 @@ sub check_options {
foreach (('unknown_status', 'warning_status', 'critical_status')) { foreach (('unknown_status', 'warning_status', 'critical_status')) {
if (defined($options{request}->{$_})) { if (defined($options{request}->{$_})) {
$options{request}->{$_} =~ s/%\{http_code\}/\$self->{response_code}/g; $options{request}->{$_} =~ s/%\{http_code\}/\$values->{code}/g;
} }
} }
@ -394,13 +394,13 @@ sub request {
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($options{request}->{critical_status}) && $options{request}->{critical_status} ne '' && if (defined($options{request}->{critical_status}) && $options{request}->{critical_status} ne '' &&
eval "$options{request}->{critical_status}") { $self->{output}->test_eval(test => $options{request}->{critical_status}, values => { code => $self->{response_code} })) {
$status = 'critical'; $status = 'critical';
} elsif (defined($options{request}->{warning_status}) && $options{request}->{warning_status} ne '' && } elsif (defined($options{request}->{warning_status}) && $options{request}->{warning_status} ne '' &&
eval "$options{request}->{warning_status}") { $self->{output}->test_eval(test => $options{request}->{warning_status}, values => { code => $self->{response_code} })) {
$status = 'warning'; $status = 'warning';
} elsif (defined($options{request}->{unknown_status}) && $options{request}->{unknown_status} ne '' && } elsif (defined($options{request}->{unknown_status}) && $options{request}->{unknown_status} ne '' &&
eval "$options{request}->{unknown_status}") { $self->{output}->test_eval(test => $options{request}->{unknown_status}, values => { code => $self->{response_code} })) {
$status = 'unknown'; $status = 'unknown';
} }
}; };

View File

@ -51,7 +51,7 @@ sub check_options {
foreach (('unknown_status', 'warning_status', 'critical_status')) { foreach (('unknown_status', 'warning_status', 'critical_status')) {
if (defined($options{request}->{$_})) { if (defined($options{request}->{$_})) {
$options{request}->{$_} =~ s/%\{http_code\}/\$self->{response}->code/g; $options{request}->{$_} =~ s/%\{http_code\}/\$values->{code}/g;
} }
} }
@ -257,14 +257,15 @@ sub request {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
my $code = $self->{response}->code();
if (defined($request_options->{critical_status}) && $request_options->{critical_status} ne '' && if (defined($request_options->{critical_status}) && $request_options->{critical_status} ne '' &&
eval "$request_options->{critical_status}") { $self->{output}->test_eval(test => $request_options->{critical_status}, values => { code => $code })) {
$status = 'critical'; $status = 'critical';
} elsif (defined($request_options->{warning_status}) && $request_options->{warning_status} ne '' && } elsif (defined($request_options->{warning_status}) && $request_options->{warning_status} ne '' &&
eval "$request_options->{warning_status}") { $self->{output}->test_eval(test => $request_options->{warning_status}, values => { code => $code })) {
$status = 'warning'; $status = 'warning';
} elsif (defined($request_options->{unknown_status}) && $request_options->{unknown_status} ne '' && } elsif (defined($request_options->{unknown_status}) && $request_options->{unknown_status} ne '' &&
eval "$request_options->{unknown_status}") { $self->{output}->test_eval(test => $request_options->{unknown_status}, values => { code => $code })) {
$status = 'unknown'; $status = 'unknown';
} }
}; };

View File

@ -83,6 +83,7 @@ sub new {
$self->{global_status} = 0; $self->{global_status} = 0;
$self->{encode_import} = 0; $self->{encode_import} = 0;
$self->{perlqq} = 0; $self->{perlqq} = 0;
$self->{safe_test} = 0;
$self->{disco_elements} = []; $self->{disco_elements} = [];
$self->{disco_entries} = []; $self->{disco_entries} = [];
@ -869,6 +870,60 @@ sub is_debug {
return 0; return 0;
} }
sub load_eval {
my ($self) = @_;
my ($code) = centreon::plugins::misc::mymodule_load(
output => $self->{output}, module => 'Safe',
no_quit => 1
);
if ($code == 0) {
$self->{safe} = Safe->new();
$self->{safe}->share('$values');
$self->{safe}->share('$assign_var');
}
}
sub test_eval {
my ($self, %options) = @_;
$self->load_eval() if ($self->{safe_test} == 0);
my $result;
if (defined($self->{safe})) {
our $values = $options{values};
$result = $self->{safe}->reval($options{test}, 1);
if ($@) {
die 'Unsafe code evaluation: ' . $@;
}
} else {
my $values = $options{values};
$result = eval "$options{test}";
}
return $result;
}
sub assign_eval {
my ($self, %options) = @_;
$self->load_eval() if ($self->{safe_test} == 0);
our $assign_var;
if (defined($self->{safe})) {
our $values = $options{values};
$self->{safe}->reval("\$assign_var = $options{eval}", 1);
if ($@) {
die 'Unsafe code evaluation: ' . $@;
}
} else {
my $values = $options{values};
eval "\$assign_var = $options{eval}";
}
return $assign_var;
}
sub use_new_perfdata { sub use_new_perfdata {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -1090,8 +1145,7 @@ sub apply_pfdata_math {
sub apply_pfdata_min { sub apply_pfdata_min {
my ($self, %options) = @_; my ($self, %options) = @_;
my $pattern_pf; my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\"");
eval "\$pattern_pf = \"$options{args}->{pattern_pf}\"";
my $min; my $min;
for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) {
next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/);
@ -1107,8 +1161,7 @@ sub apply_pfdata_min {
sub apply_pfdata_max { sub apply_pfdata_max {
my ($self, %options) = @_; my ($self, %options) = @_;
my $pattern_pf; my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\"");
eval "\$pattern_pf = \"$options{args}->{pattern_pf}\"";
my $max; my $max;
for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) {
next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/);
@ -1124,8 +1177,7 @@ sub apply_pfdata_max {
sub apply_pfdata_sum { sub apply_pfdata_sum {
my ($self, %options) = @_; my ($self, %options) = @_;
my $pattern_pf; my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\"");
eval "\$pattern_pf = \"$options{args}->{pattern_pf}\"";
my ($sum, $num) = (0, 0); my ($sum, $num) = (0, 0);
for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) {
next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/);
@ -1141,8 +1193,7 @@ sub apply_pfdata_sum {
sub apply_pfdata_average { sub apply_pfdata_average {
my ($self, %options) = @_; my ($self, %options) = @_;
my $pattern_pf; my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\"");
eval "\$pattern_pf = \"$options{args}->{pattern_pf}\"";
my ($sum, $num) = (0, 0); my ($sum, $num) = (0, 0);
for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) {
next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/);
@ -1387,7 +1438,7 @@ Change traffic values in percent: --change-perfdata=traffic_in,,percent()
=back =back
=item B<--extend-perfdata-group> =item B<--extend-perfdata-group>
Extend perfdata from multiple perfdatas (methods in target are: min, max, average, sum) Extend perfdata from multiple perfdatas (methods in target are: min, max, average, sum)
Syntax: --extend-perfdata-group=searchlabel,newlabel,target[,[newuom],[min],[max]] Syntax: --extend-perfdata-group=searchlabel,newlabel,target[,[newuom],[min],[max]]

View File

@ -197,7 +197,7 @@ sub read {
} }
if ($self->{storable} == 1) { if ($self->{storable} == 1) {
open FILE, $self->{statefile_dir} . '/' . $self->{statefile}; open FILE, '<', $self->{statefile_dir} . '/' . $self->{statefile};
eval { eval {
$self->{datas} = Storable::fd_retrieve(*FILE); $self->{datas} = Storable::fd_retrieve(*FILE);
}; };

View File

@ -39,16 +39,16 @@ sub catalog_status_threshold {
my $label = $self->{label}; my $label = $self->{label};
$label =~ s/-/_/g; $label =~ s/-/_/g;
if (defined($self->{instance_mode}->{option_results}->{'ok_' . $label}) && $self->{instance_mode}->{option_results}->{'ok_' . $label} ne '' && if (defined($self->{instance_mode}->{option_results}->{'ok_' . $label}) && $self->{instance_mode}->{option_results}->{'ok_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'ok_' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'ok_' . $label})) {
$status = 'ok'; $status = 'ok';
} elsif (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'critical_' . $label})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'warning_' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'warning_' . $label})) {
$status = 'warning'; $status = 'warning';
} elsif (defined($self->{instance_mode}->{option_results}->{'unknown_' . $label}) && $self->{instance_mode}->{option_results}->{'unknown_' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'unknown_' . $label}) && $self->{instance_mode}->{option_results}->{'unknown_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'unknown_' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'unknown_' . $label})) {
$status = 'unknown'; $status = 'unknown';
} }
}; };
@ -68,15 +68,14 @@ sub catalog_status_threshold_ng {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
my $label = $self->{label}; if (defined($self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}} ne '' &&
if (defined($self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'critical-' . $label} ne '' && $self->eval(value => $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}})) {
eval "$self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}") {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{'warning-' . $label}) && $self->{instance_mode}->{option_results}->{'warning-' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'warning-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'warning-' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}})) {
$status = 'warning'; $status = 'warning';
} elsif (defined($self->{instance_mode}->{option_results}->{'unknown-' . $label}) && $self->{instance_mode}->{option_results}->{'unknown-' . $label} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{'unknown-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'unknown-' . $self->{label}} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'unknown-' . $label}") { $self->eval(value => $self->{instance_mode}->{option_results}->{'unknown-' . $self->{label}})) {
$status = 'unknown'; $status = 'unknown';
} }
}; };

View File

@ -134,7 +134,7 @@ sub new {
); );
$self->{statefile_value} = centreon::plugins::statefile->new(%options); $self->{statefile_value} = centreon::plugins::statefile->new(%options);
} }
$self->{maps_counters} = {} if (!defined($self->{maps_counters})); $self->{maps_counters} = {} if (!defined($self->{maps_counters}));
$self->set_counters(%options); $self->set_counters(%options);
@ -755,7 +755,7 @@ sub change_macros {
foreach (@{$options{macros}}) { foreach (@{$options{macros}}) {
if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') { if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
} }

View File

@ -22,6 +22,7 @@ package centreon::plugins::values;
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::misc;
# Warning message with sprintf and too much arguments. # Warning message with sprintf and too much arguments.
# Really annoying. Need to disable that warning # Really annoying. Need to disable that warning
no if ($^V gt v5.22.0), 'warnings' => 'redundant'; no if ($^V gt v5.22.0), 'warnings' => 'redundant';
@ -55,7 +56,8 @@ sub new {
$self->{last_timestamp} = undef; $self->{last_timestamp} = undef;
$self->{result_values} = {}; $self->{result_values} = {};
$self->{safe_test} = 0;
return $self; return $self;
} }
@ -231,6 +233,36 @@ sub perfdata {
} }
} }
sub eval {
my ($self, %options) = @_;
if ($self->{safe_test} == 0) {
my ($code) = centreon::plugins::misc::mymodule_load(
output => $self->{output}, module => 'Safe',
no_quit => 1
);
if ($code == 0) {
$self->{safe} = Safe->new();
$self->{safe}->share('$values');
}
$self->{safe_test} = 1;
}
my $result;
if (defined($self->{safe})) {
our $values = $self->{result_values};
$result = $self->{safe}->reval($options{value}, 1);
if ($@) {
die 'Unsafe code evaluation: ' . $@;
}
} else {
my $values = $self->{result_values};
$result = eval "$options{value}";
}
return $result;
}
sub execute { sub execute {
my ($self, %options) = @_; my ($self, %options) = @_;
my $old_datas = {}; my $old_datas = {};

View File

@ -26,18 +26,18 @@ use strict;
use warnings; use warnings;
my %map_type = ( my %map_type = (
"instance" => "InstanceId", 'instance' => 'InstanceId',
"asg" => "AutoScalingGroupName", 'asg' => 'AutoScalingGroupName'
); );
my %map_status = ( my %map_status = (
0 => 'passed', 0 => 'passed',
1 => 'failed', 1 => 'failed'
); );
sub prefix_metric_output { sub prefix_metric_output {
my ($self, %options) = @_; my ($self, %options) = @_;
return ucfirst($options{instance_value}->{type}) . " '" . $options{instance_value}->{display} . "' "; return ucfirst($options{instance_value}->{type}) . " '" . $options{instance_value}->{display} . "' ";
} }
@ -49,12 +49,12 @@ sub custom_status_threshold {
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' && if (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_status}") { $self->eval(value => $self->{instance_mode}->{option_results}->{critical_status})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_status}") { $self->eval(value => $self->{instance_mode}->{option_results}->{warning_status})) {
$status = 'warning'; $status = 'warning';
} }
}; };
@ -68,13 +68,12 @@ sub custom_status_threshold {
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = $self->{result_values}->{metric} . ": " . $self->{result_values}->{status}; return $self->{result_values}->{metric} . ": " . $self->{result_values}->{status};
return $msg;
} }
sub custom_status_calc { sub custom_status_calc {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{result_values}->{status} = $map_status{$options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{metric}}}; $self->{result_values}->{status} = $map_status{$options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{metric}}};
$self->{result_values}->{metric} = $options{extra_options}->{metric}; $self->{result_values}->{metric} = $options{extra_options}->{metric};
return 0; return 0;
@ -88,15 +87,16 @@ sub set_counters {
]; ];
foreach my $metric ('StatusCheckFailed_Instance', 'StatusCheckFailed_System') { foreach my $metric ('StatusCheckFailed_Instance', 'StatusCheckFailed_System') {
my $entry = { label => lc($metric), threshold => 0, set => { my $entry = {
key_values => [ { name => $metric }, { name => 'display' } ], label => lc($metric), threshold => 0, set => {
closure_custom_calc => $self->can('custom_status_calc'), key_values => [ { name => $metric }, { name => 'display' } ],
closure_custom_calc_extra_options => { metric => $metric }, closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_calc_extra_options => { metric => $metric },
closure_custom_perfdata => sub { return 0; }, closure_custom_output => $self->can('custom_status_output'),
closure_custom_threshold_check => $self->can('custom_status_threshold'), closure_custom_perfdata => sub { return 0; },
} closure_custom_threshold_check => $self->can('custom_status_threshold')
}; }
};
push @{$self->{maps_counters}->{metric}}, $entry; push @{$self->{maps_counters}->{metric}}, $entry;
} }
} }
@ -107,10 +107,10 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"type:s" => { name => 'type' }, 'type:s' => { name => 'type' },
"name:s@" => { name => 'name' }, 'name:s@' => { name => 'name' },
"warning-status:s" => { name => 'warning_status', default => '' }, 'warning-status:s' => { name => 'warning_status', default => '' },
"critical-status:s" => { name => 'critical_status', default => '%{status} =~ /failed/i' }, 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /failed/i' }
}); });
return $self; return $self;
@ -165,7 +165,7 @@ sub manage_selection {
timeframe => $self->{aws_timeframe}, timeframe => $self->{aws_timeframe},
period => $self->{aws_period}, period => $self->{aws_period},
); );
foreach my $metric (keys %{$metric_results{$instance}}) { foreach my $metric (keys %{$metric_results{$instance}}) {
next if (!defined($metric_results{$instance}->{$metric}->{average})); next if (!defined($metric_results{$instance}->{$metric}->{average}));

View File

@ -24,6 +24,7 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use Time::Local; use Time::Local;
sub custom_active_perfdata { sub custom_active_perfdata {
@ -38,11 +39,13 @@ sub custom_active_perfdata {
$self->{result_values}->{report_date} =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/; $self->{result_values}->{report_date} =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
$self->{output}->perfdata_add(label => 'perfdate', value => timelocal(0,0,12,$3,$2-1,$1-1900)); $self->{output}->perfdata_add(label => 'perfdate', value => timelocal(0,0,12,$3,$2-1,$1-1900));
$self->{output}->perfdata_add(label => 'active_mailboxes', nlabel => 'exchange.mailboxes.active.count', $self->{output}->perfdata_add(
value => $self->{result_values}->{active}, label => 'active_mailboxes', nlabel => 'exchange.mailboxes.active.count',
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options), value => $self->{result_values}->{active},
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
unit => 'mailboxes', min => 0, max => $self->{result_values}->{total}); critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
unit => 'mailboxes', min => 0, max => $self->{result_values}->{total}
);
} }
sub custom_active_threshold { sub custom_active_threshold {
@ -52,9 +55,13 @@ sub custom_active_threshold {
if ($self->{instance_mode}->{option_results}->{units} eq '%') { if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$threshold_value = $self->{result_values}->{prct_active}; $threshold_value = $self->{result_values}->{prct_active};
} }
my $exit = $self->{perfdata}->threshold_check(value => $threshold_value, my $exit = $self->{perfdata}->threshold_check(
threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, value => $threshold_value,
{ label => 'warning-' . $self->{label}, exit_litteral => 'warning' } ]); threshold => [
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
{ label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' }
]
);
return $exit; return $exit;
} }
@ -62,12 +69,13 @@ sub custom_active_threshold {
sub custom_active_output { sub custom_active_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = sprintf("Active mailboxes on %s : %d/%d (%.2f%%)", return sprintf(
$self->{result_values}->{report_date}, 'Active mailboxes on %s : %d/%d (%.2f%%)',
$self->{result_values}->{active}, $self->{result_values}->{report_date},
$self->{result_values}->{total}, $self->{result_values}->{active},
$self->{result_values}->{prct_active}); $self->{result_values}->{total},
return $msg; $self->{result_values}->{prct_active}
);
} }
sub custom_active_calc { sub custom_active_calc {
@ -87,34 +95,13 @@ sub custom_usage_perfdata {
my $extra_label = ''; my $extra_label = '';
$extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0); $extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
$self->{output}->perfdata_add(label => 'used' . $extra_label, nlabel => $self->{result_values}->{display} . '#exchange.mailboxes.usage.bytes', $self->{output}->perfdata_add(
unit => 'B', label => 'used' . $extra_label,
value => $self->{result_values}->{used}, nlabel => $self->{result_values}->{display} . '#exchange.mailboxes.usage.bytes',
min => 0); unit => 'B',
} value => $self->{result_values}->{used},
min => 0
sub custom_status_threshold { );
my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_status}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_status}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
return $status;
} }
sub custom_status_output { sub custom_status_output {
@ -125,12 +112,13 @@ sub custom_status_output {
my ($prohibit_send_quota_value, $prohibit_send_quota_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{prohibit_send_quota}); my ($prohibit_send_quota_value, $prohibit_send_quota_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{prohibit_send_quota});
my ($prohibit_send_receive_quota_value, $prohibit_send_receive_quota_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{prohibit_send_receive_quota}); my ($prohibit_send_receive_quota_value, $prohibit_send_receive_quota_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{prohibit_send_receive_quota});
my $msg = sprintf("Used: %s Issue Warning Quota: %s Prohibit Send Quota: %s Prohibit Send/Receive Quota: %s", return sprintf(
$used_value . " " . $used_unit, "Used: %s Issue Warning Quota: %s Prohibit Send Quota: %s Prohibit Send/Receive Quota: %s",
$issue_warning_quota_value . " " . $issue_warning_quota_unit, $used_value . " " . $used_unit,
$prohibit_send_quota_value . " " . $prohibit_send_quota_unit, $issue_warning_quota_value . " " . $issue_warning_quota_unit,
$prohibit_send_receive_quota_value . " " . $prohibit_send_receive_quota_unit); $prohibit_send_quota_value . " " . $prohibit_send_quota_unit,
return $msg; $prohibit_send_receive_quota_value . " " . $prohibit_send_receive_quota_unit
);
} }
sub custom_status_calc { sub custom_status_calc {
@ -191,31 +179,39 @@ sub set_counters {
output_template => 'Usage (inactive mailboxes): %s %s', output_template => 'Usage (inactive mailboxes): %s %s',
output_change_bytes => 1, output_change_bytes => 1,
perfdatas => [ perfdatas => [
{ label => 'total_usage_inactive', value => 'storage_used_inactive', template => '%d', { label => 'total_usage_inactive', template => '%d', min => 0, unit => 'B' }
min => 0, unit => 'B' }, ]
],
} }
}, }
]; ];
$self->{maps_counters}->{mailboxes} = [ $self->{maps_counters}->{mailboxes} = [
{ label => 'usage', set => { {
key_values => [ { name => 'storage_used' }, { name => 'issue_warning_quota' }, label => 'status',
{ name => 'prohibit_send_quota' }, { name => 'prohibit_send_receive_quota' }, { name => 'name' } ], type => 2,
warning_default => '%{used} > %{issue_warning_quota}',
critical_default => '%{used} > %{prohibit_send_quota}',
set => {
key_values => [
{ name => 'storage_used' }, { name => 'issue_warning_quota' },
{ name => 'prohibit_send_quota' }, { name => 'prohibit_send_receive_quota' },
{ name => 'name' }
],
closure_custom_calc => $self->can('custom_status_calc'), closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => $self->can('custom_usage_perfdata'), closure_custom_perfdata => $self->can('custom_usage_perfdata'),
closure_custom_threshold_check => $self->can('custom_status_threshold'), closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, },
{ label => 'items', nlabel => 'exchange.mailboxes.items.count', set => { { label => 'items', nlabel => 'exchange.mailboxes.items.count', set => {
key_values => [ { name => 'items' }, { name => 'name' } ], key_values => [ { name => 'items' }, { name => 'name' } ],
output_template => 'Items: %d', output_template => 'Items: %d',
perfdatas => [ perfdatas => [
{ label => 'items', value => 'items', template => '%d', { label => 'items', template => '%d',
min => 0, label_extra_instance => 1, instance_use => 'name' }, min => 0, label_extra_instance => 1, instance_use => 'name' }
], ]
} }
}, }
]; ];
} }
@ -225,23 +221,14 @@ sub new {
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"filter-mailbox:s" => { name => 'filter_mailbox' }, 'filter-mailbox:s' => { name => 'filter_mailbox' },
"warning-status:s" => { name => 'warning_status', default => '%{used} > %{issue_warning_quota}' }, 'units:s' => { name => 'units', default => '%' },
"critical-status:s" => { name => 'critical_status', default => '%{used} > %{prohibit_send_quota}' }, 'filter-counters:s' => { name => 'filter_counters', default => 'active|total' }
"units:s" => { name => 'units', default => '%' },
"filter-counters:s" => { name => 'filter_counters', default => 'active|total' },
}); });
return $self; return $self;
} }
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status']);
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;

View File

@ -26,20 +26,19 @@ use strict;
use warnings; use warnings;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use centreon::plugins::statefile; use centreon::plugins::statefile;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'global', type => 0 }, { name => 'global', type => 0 },
{ name => 'sap', type => 1, cb_prefix_output => 'prefix_sap_output', message_multiple => 'All SAP are ok', skipped_code => { -10 => 1 } }, { name => 'sap', type => 1, cb_prefix_output => 'prefix_sap_output', message_multiple => 'All SAP are ok', skipped_code => { -10 => 1 } }
]; ];
$self->{maps_counters}->{sap} = [ $self->{maps_counters}->{sap} = [
{ label => 'status', threshold => 0, set => { { label => 'status', type => 2, critical_default => '%{admin} =~ /up/i and %{status} !~ /up/i', set => {
key_values => [ { name => 'status' }, { name => 'admin' }, { name => 'display' } ], key_values => [ { name => 'status' }, { name => 'admin' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold') closure_custom_threshold_check => $self->can('custom_status_threshold')
@ -60,7 +59,7 @@ sub set_counters {
closure_custom_perfdata => $self->can('custom_sap_perfdata'), closure_custom_perfdata => $self->can('custom_sap_perfdata'),
closure_custom_threshold_check => $self->can('custom_sap_threshold') closure_custom_threshold_check => $self->can('custom_sap_threshold')
} }
}, }
]; ];
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
@ -87,7 +86,7 @@ sub set_counters {
sub custom_total_traffic_perfdata { sub custom_total_traffic_perfdata {
my ($self, %options) = @_; my ($self, %options) = @_;
my ($warning, $critical); my ($warning, $critical);
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) { if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1); $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
@ -96,7 +95,7 @@ sub custom_total_traffic_perfdata {
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}); $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel});
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}); $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel});
} }
$self->{output}->perfdata_add( $self->{output}->perfdata_add(
label => 'total_traffic_' . $self->{result_values}->{label}, unit => 'b/s', label => 'total_traffic_' . $self->{result_values}->{label}, unit => 'b/s',
nlabel => $self->{nlabel}, nlabel => $self->{nlabel},
@ -109,7 +108,7 @@ sub custom_total_traffic_perfdata {
sub custom_total_traffic_threshold { sub custom_total_traffic_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $exit = 'ok'; my $exit = 'ok';
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) { if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
@ -121,18 +120,19 @@ sub custom_total_traffic_threshold {
sub custom_total_traffic_output { sub custom_total_traffic_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_traffic}, network => 1); my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_traffic}, network => 1);
my ($total_value, $total_unit); my ($total_value, $total_unit);
if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) { if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) {
($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1); ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1);
} }
my $msg = sprintf("Total Traffic %s : %s/s (%s on %s)", return sprintf(
ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit, "Total Traffic %s : %s/s (%s on %s)",
defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-', ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit,
defined($total_value) ? $total_value . $total_unit : '-'); defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-',
return $msg; defined($total_value) ? $total_value . $total_unit : '-'
);
} }
sub custom_total_traffic_calc { sub custom_total_traffic_calc {
@ -165,49 +165,19 @@ sub custom_total_traffic_calc {
sub custom_status_threshold { sub custom_status_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval { my $status = catalog_status_threshold_ng($self, %options);
local $SIG{__WARN__} = sub { $message = $_[0]; }; $self->{instance_mode}->{last_status} = 0;
local $SIG{__DIE__} = sub { $message = $_[0]; }; if ($self->{result_values}->{admin} eq 'up') {
$self->{instance_mode}->{last_status} = 1;
my $label = $self->{label};
$label =~ s/-/_/g;
if (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'warning_' . $label}") {
$status = 'warning';
}
$self->{instance_mode}->{last_status} = 0;
if ($self->{result_values}->{admin} eq 'up') {
$self->{instance_mode}->{last_status} = 1;
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
} }
return $status; return $status;
} }
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = 'Status : ' . $self->{result_values}->{status} . ' (admin: ' . $self->{result_values}->{admin} . ')';
return $msg; return 'Status : ' . $self->{result_values}->{status} . ' (admin: ' . $self->{result_values}->{admin} . ')';
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{admin} = $options{new_datas}->{$self->{instance} . '_admin'};
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
} }
sub custom_sap_perfdata { sub custom_sap_perfdata {
@ -221,7 +191,7 @@ sub custom_sap_perfdata {
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}); $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel});
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}); $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel});
} }
$self->{output}->perfdata_add( $self->{output}->perfdata_add(
label => 'traffic_' . $self->{result_values}->{label}, unit => 'b/s', label => 'traffic_' . $self->{result_values}->{label}, unit => 'b/s',
nlabel => $self->{nlabel}, nlabel => $self->{nlabel},
@ -235,7 +205,7 @@ sub custom_sap_perfdata {
sub custom_sap_threshold { sub custom_sap_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $exit = 'ok'; my $exit = 'ok';
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) { if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
@ -247,23 +217,24 @@ sub custom_sap_threshold {
sub custom_sap_output { sub custom_sap_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic}, network => 1); my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic}, network => 1);
my ($total_value, $total_unit); my ($total_value, $total_unit);
if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) { if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) {
($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1); ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1);
} }
my $msg = sprintf("Traffic %s : %s/s (%s on %s)", return sprintf(
ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit, "Traffic %s : %s/s (%s on %s)",
defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-', ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit,
defined($total_value) ? $total_value . $total_unit : '-'); defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-',
return $msg; defined($total_value) ? $total_value . $total_unit : '-'
);
} }
sub custom_sap_calc { sub custom_sap_calc {
my ($self, %options) = @_; my ($self, %options) = @_;
return -10 if (defined($self->{instance_mode}->{last_status}) && $self->{instance_mode}->{last_status} == 0); return -10 if (defined($self->{instance_mode}->{last_status}) && $self->{instance_mode}->{last_status} == 0);
$self->{result_values}->{label} = $options{extra_options}->{label_ref}; $self->{result_values}->{label} = $options{extra_options}->{label_ref};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
@ -279,7 +250,7 @@ sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'reload-cache-time:s' => { name => 'reload_cache_time', default => 300 }, 'reload-cache-time:s' => { name => 'reload_cache_time', default => 300 },
'display-name:s' => { name => 'display_name', default => '%{SvcDescription}.%{IfName}.%{SapEncapName}' }, 'display-name:s' => { name => 'display_name', default => '%{SvcDescription}.%{IfName}.%{SapEncapName}' },
@ -288,11 +259,9 @@ sub new {
'speed-out:s' => { name => 'speed_out' }, 'speed-out:s' => { name => 'speed_out' },
'speed-total-in:s' => { name => 'speed_total_in' }, 'speed-total-in:s' => { name => 'speed_total_in' },
'speed-total-out:s' => { name => 'speed_total_out' }, 'speed-total-out:s' => { name => 'speed_total_out' },
'units-traffic:s' => { name => 'units_traffic', default => '%' }, 'units-traffic:s' => { name => 'units_traffic', default => '%' }
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '%{admin} =~ /up/i and %{status} !~ /up/i' },
}); });
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self; return $self;
} }
@ -300,8 +269,7 @@ sub new {
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status']);
$self->{statefile_cache}->check_options(%options); $self->{statefile_cache}->check_options(%options);
} }
@ -313,7 +281,7 @@ sub prefix_sap_output {
sub get_display_name { sub get_display_name {
my ($self, %options) = @_; my ($self, %options) = @_;
my $display_name = $self->{option_results}->{display_name}; my $display_name = $self->{option_results}->{display_name};
$display_name =~ s/%\{(.*?)\}/$options{$1}/ge; $display_name =~ s/%\{(.*?)\}/$options{$1}/ge;
return $display_name; return $display_name;
@ -338,14 +306,16 @@ my $oid_ifName = '.1.3.6.1.2.1.31.1.1.1.1';
sub reload_cache { sub reload_cache {
my ($self, %options) = @_; my ($self, %options) = @_;
my $datas = { last_timestamp => time() }; my $datas = { last_timestamp => time() };
my $snmp_result = $options{snmp}->get_multiple_table(oids => [ my $snmp_result = $options{snmp}->get_multiple_table(
oids => [
{ oid => $oid_sapDescription }, { oid => $oid_sapDescription },
{ oid => $oid_svcDescription }, { oid => $oid_svcDescription },
{ oid => $oid_ifName }, { oid => $oid_ifName }
], ],
nothing_quit => 1); nothing_quit => 1
);
$datas->{snmp_result} = $snmp_result; $datas->{snmp_result} = $snmp_result;
if (scalar(keys %{$datas->{snmp_result}->{$oid_sapDescription}}) <= 0) { if (scalar(keys %{$datas->{snmp_result}->{$oid_sapDescription}}) <= 0) {
@ -358,12 +328,12 @@ sub reload_cache {
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
if ($options{snmp}->is_snmpv1()) { if ($options{snmp}->is_snmpv1()) {
$self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3."); $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_alcatel_isam_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode}); my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_alcatel_isam_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode});
my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp'); my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp');
if ($has_cache_file == 0 || !defined($timestamp_cache) || if ($has_cache_file == 0 || !defined($timestamp_cache) ||
@ -376,7 +346,6 @@ sub manage_selection {
$self->{global} = {}; $self->{global} = {};
$self->{sap} = {}; $self->{sap} = {};
foreach my $oid (keys %{$snmp_result->{$oid_sapDescription}}) { foreach my $oid (keys %{$snmp_result->{$oid_sapDescription}}) {
next if ($oid !~ /^$oid_sapDescription\.(.*?)\.(.*?)\.(.*?)$/); next if ($oid !~ /^$oid_sapDescription\.(.*?)\.(.*?)\.(.*?)$/);
# $SvcId and $SapEncapValue is the same. We use service table # $SvcId and $SapEncapValue is the same. We use service table
@ -391,7 +360,7 @@ sub manage_selection {
$snmp_result->{$oid_ifName}->{$oid_ifName . '.' . $SapPortId} : $SapPortId; $snmp_result->{$oid_ifName}->{$oid_ifName . '.' . $SapPortId} : $SapPortId;
my $SapEncapName = defined($snmp_result->{$oid_svcDescription}->{$oid_svcDescription . '.' . $SapEncapValue}) && $snmp_result->{$oid_svcDescription}->{$oid_svcDescription . '.' . $SapEncapValue} ne '' ? my $SapEncapName = defined($snmp_result->{$oid_svcDescription}->{$oid_svcDescription . '.' . $SapEncapValue}) && $snmp_result->{$oid_svcDescription}->{$oid_svcDescription . '.' . $SapEncapValue} ne '' ?
$snmp_result->{$oid_svcDescription}->{$oid_svcDescription . '.' . $SapEncapValue} : $SapEncapValue; $snmp_result->{$oid_svcDescription}->{$oid_svcDescription . '.' . $SapEncapValue} : $SapEncapValue;
my $name = $self->get_display_name( my $name = $self->get_display_name(
SapDescription => $SapDescription, SapDescription => $SapDescription,
SvcDescription => $SvcDescription, SvcDescription => $SvcDescription,
@ -399,24 +368,26 @@ sub manage_selection {
IfName => $IfName, IfName => $IfName,
SvcId => $SvcId, SvcId => $SvcId,
SapPortId => $SapPortId, SapPortId => $SapPortId,
SapEncapValue => $SapEncapValue); SapEncapValue => $SapEncapValue
);
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$name !~ /$self->{option_results}->{filter_name}/) { $name !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
next; next;
} }
$self->{sap}->{$instance} = { display => $name }; $self->{sap}->{$instance} = { display => $name };
} }
$options{snmp}->load(oids => [$mapping->{fadSapStatsIngressOctets}->{oid}, $options{snmp}->load(oids => [$mapping->{fadSapStatsIngressOctets}->{oid},
$mapping->{fadSapStatsEgressOctets}->{oid}, $mapping->{fadSapStatsEgressOctets}->{oid},
$mapping->{sapAdminStatus}->{oid}, $mapping->{sapOperStatus}->{oid}], $mapping->{sapAdminStatus}->{oid}, $mapping->{sapOperStatus}->{oid}],
instances => [keys %{$self->{sap}}], instance_regexp => '(\d+\.\d+\.\d+)$'); instances => [keys %{$self->{sap}}], instance_regexp => '(\d+\.\d+\.\d+)$'
);
$snmp_result = $options{snmp}->get_leef(nothing_quit => 1); $snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
foreach (keys %{$self->{sap}}) { foreach (keys %{$self->{sap}}) {
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_); my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_);
$self->{sap}->{$_}->{in} = $result->{fadSapStatsIngressOctets} * 8; $self->{sap}->{$_}->{in} = $result->{fadSapStatsIngressOctets} * 8;
@ -433,7 +404,7 @@ sub manage_selection {
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->{cache_name} = "alcatel_isam_" . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{cache_name} = 'alcatel_isam_' . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')); (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
} }

View File

@ -192,19 +192,19 @@ sub default_global_admin_up_rule {
sub default_global_admin_down_rule { sub default_global_admin_down_rule {
my ($self, %options) = @_; my ($self, %options) = @_;
return '%{admstatus} ne "enable"'; return '%{admstatus} ne "enable"';
} }
sub default_global_oper_up_rule { sub default_global_oper_up_rule {
my ($self, %options) = @_; my ($self, %options) = @_;
return '%{opstatus} eq "enabled"'; return '%{opstatus} eq "enabled"';
} }
sub default_global_oper_down_rule { sub default_global_oper_down_rule {
my ($self, %options) = @_; my ($self, %options) = @_;
return '%{opstatus} ne "enabled"'; return '%{opstatus} ne "enabled"';
} }
@ -318,16 +318,18 @@ sub add_result_global {
$self->{global} = { total_port => 0, global_link_up => 0, global_link_down => 0, global_admin_up => 0, $self->{global} = { total_port => 0, global_link_up => 0, global_link_down => 0, global_admin_up => 0,
global_admin_down => 0, global_oper_up => 0, global_oper_down => 0}; global_admin_down => 0, global_oper_up => 0, global_oper_down => 0};
foreach (@{$self->{array_interface_selected}}) { foreach (@{$self->{array_interface_selected}}) {
my $linkstatus = $self->{oid_linkstatus_mapping}->{$self->{results}->{$self->{oid_linkstatus} . '.' . $_}}; my $values = {
my $opstatus = $self->{oid_opstatus_mapping}->{$self->{results}->{$self->{oid_opstatus} . '.' . $_}}; linkstatus => $self->{oid_linkstatus_mapping}->{$self->{results}->{$self->{oid_linkstatus} . '.' . $_}},
my $admstatus = $self->{oid_adminstatus_mapping}->{$self->{results}->{$self->{oid_adminstatus} . '.' . $_}}; opstatus => $self->{oid_opstatus_mapping}->{$self->{results}->{$self->{oid_opstatus} . '.' . $_}},
admstatus => $self->{oid_adminstatus_mapping}->{$self->{results}->{$self->{oid_adminstatus} . '.' . $_}}
};
foreach (('global_link_up', 'global_link_down', 'global_admin_up', 'global_admin_down', 'global_oper_up', 'global_oper_down')) { foreach (('global_link_up', 'global_link_down', 'global_admin_up', 'global_admin_down', 'global_oper_up', 'global_oper_down')) {
eval { eval {
local $SIG{__WARN__} = sub { return ; }; local $SIG{__WARN__} = sub { return ; };
local $SIG{__DIE__} = sub { return ; }; local $SIG{__DIE__} = sub { return ; };
if (defined($self->{option_results}->{$_ . '_rule'}) && $self->{option_results}->{$_ . '_rule'} ne '' && if (defined($self->{option_results}->{$_ . '_rule'}) && $self->{option_results}->{$_ . '_rule'} ne '' &&
eval "$self->{option_results}->{$_ . '_rule'}") { $self->{output}->test_eval(test => $self->{option_results}->{$_ . '_rule'}, values => $values)) {
$self->{global}->{$_}++; $self->{global}->{$_}++;
} }
}; };
@ -385,11 +387,11 @@ sub add_result_cast {
$self->{int}->{$options{instance}}->{obcast} = defined($self->{results}->{$self->{oid_ifHCOutBroadcastPkts} . '.' . $options{instance}}) ? $self->{results}->{$self->{oid_ifHCOutBroadcastPkts} . '.' . $options{instance}} : 0; $self->{int}->{$options{instance}}->{obcast} = defined($self->{results}->{$self->{oid_ifHCOutBroadcastPkts} . '.' . $options{instance}}) ? $self->{results}->{$self->{oid_ifHCOutBroadcastPkts} . '.' . $options{instance}} : 0;
$self->{int}->{$options{instance}}->{mode_cast} = 64; $self->{int}->{$options{instance}}->{mode_cast} = 64;
} }
foreach (('iucast', 'imcast', 'ibcast', 'oucast', 'omcast', 'obcast')) { foreach (('iucast', 'imcast', 'ibcast', 'oucast', 'omcast', 'obcast')) {
$self->{int}->{$options{instance}}->{$_} = 0 if (!defined($self->{int}->{$options{instance}}->{$_})); $self->{int}->{$options{instance}}->{$_} = 0 if (!defined($self->{int}->{$options{instance}}->{$_}));
} }
$self->{int}->{$options{instance}}->{total_in_packets} = $self->{int}->{$options{instance}}->{iucast} + $self->{int}->{$options{instance}}->{imcast} + $self->{int}->{$options{instance}}->{ibcast}; $self->{int}->{$options{instance}}->{total_in_packets} = $self->{int}->{$options{instance}}->{iucast} + $self->{int}->{$options{instance}}->{imcast} + $self->{int}->{$options{instance}}->{ibcast};
$self->{int}->{$options{instance}}->{total_out_packets} = $self->{int}->{$options{instance}}->{oucast} + $self->{int}->{$options{instance}}->{omcast} + $self->{int}->{$options{instance}}->{obcast}; $self->{int}->{$options{instance}}->{total_out_packets} = $self->{int}->{$options{instance}}->{oucast} + $self->{int}->{$options{instance}}->{omcast} + $self->{int}->{$options{instance}}->{obcast};
} }

View File

@ -25,6 +25,93 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_threshold {
my ($self, %options) = @_;
$self->{instance_mode}->{last_status} = 0;
if ($self->{result_values}->{status} ne 'down') {
$self->{instance_mode}->{last_status} = 1;
}
return catalog_status_threshold_ng($self, %options);
}
sub custom_status_output {
my ($self, %options) = @_;
return 'Status : ' . $self->{result_values}->{status};
}
sub custom_ib_perfdata {
my ($self, %options) = @_;
my ($warning, $critical);
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') {
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel});
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel});
}
$self->{output}->perfdata_add(
label => 'traffic_' . $self->{result_values}->{label}, unit => 'b/s',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
value => sprintf("%.2f", $self->{result_values}->{traffic}),
warning => $warning,
critical => $critical,
min => 0, max => $self->{result_values}->{speed}
);
}
sub custom_ib_threshold {
my ($self, %options) = @_;
my $exit = 'ok';
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') {
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
}
return $exit;
}
sub custom_ib_output {
my ($self, %options) = @_;
my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic}, network => 1);
my ($total_value, $total_unit);
if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) {
($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1);
}
return sprintf(
"Traffic %s : %s/s (%s on %s)",
ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit,
defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-',
defined($total_value) ? $total_value . $total_unit : '-'
);
}
sub custom_ib_calc {
my ($self, %options) = @_;
return -10 if (defined($self->{instance_mode}->{last_status}) && $self->{instance_mode}->{last_status} == 0);
$self->{result_values}->{label} = $options{extra_options}->{label_ref};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
my $diff_traffic = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}} - $options{old_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}};
$self->{result_values}->{traffic} = $diff_traffic / $options{delta_time};
if ($options{new_datas}->{$self->{instance} . '_speed_' . $self->{result_values}->{label}} > 0) {
$self->{result_values}->{speed} = $options{new_datas}->{$self->{instance} . '_speed_' . $self->{result_values}->{label}} * 1000 * 1000;
$self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic} * 100 / $self->{result_values}->{speed};
} elsif (defined($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}}) && $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} =~ /[0-9]/) {
$self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic} * 100 / ($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000);
$self->{result_values}->{speed} = $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000;
}
return 0;
}
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -35,9 +122,8 @@ sub set_counters {
]; ];
$self->{maps_counters}->{ib} = [ $self->{maps_counters}->{ib} = [
{ label => 'ib-status', threshold => 0, set => { { label => 'ib-status', type => 2, critical_default => '%{status} !~ /active/i', set => {
key_values => [ { name => 'status' }, { name => 'display' } ], key_values => [ { name => 'status' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold') closure_custom_threshold_check => $self->can('custom_status_threshold')
@ -62,9 +148,8 @@ sub set_counters {
]; ];
$self->{maps_counters}->{ibgw} = [ $self->{maps_counters}->{ibgw} = [
{ label => 'ibgw-status', threshold => 0, set => { { label => 'ibgw-status', type => 2, critical_default => '%{status} !~ /up/i', set => {
key_values => [ { name => 'status' }, { name => 'display' } ], key_values => [ { name => 'status' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; }, closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold') closure_custom_threshold_check => $self->can('custom_status_threshold')
@ -89,148 +174,22 @@ sub set_counters {
]; ];
} }
sub custom_status_threshold {
my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
my $label = $self->{label};
$label =~ s/-/_/g;
if (defined($self->{instance_mode}->{option_results}->{'critical_' . $label}) && $self->{instance_mode}->{option_results}->{'critical_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'critical_' . $label}") {
$status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{'warning_' . $label}) && $self->{instance_mode}->{option_results}->{'warning_' . $label} ne '' &&
eval "$self->{instance_mode}->{option_results}->{'warning_' . $label}") {
$status = 'warning';
}
$self->{instance_mode}->{last_status} = 0;
if ($self->{result_values}->{status} ne 'down') {
$self->{instance_mode}->{last_status} = 1;
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
return $status;
}
sub custom_status_output {
my ($self, %options) = @_;
my $msg = 'Status : ' . $self->{result_values}->{status};
return $msg;
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
}
sub custom_ib_perfdata {
my ($self, %options) = @_;
my ($warning, $critical);
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1);
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') {
$warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel});
$critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel});
}
$self->{output}->perfdata_add(
label => 'traffic_' . $self->{result_values}->{label}, unit => 'b/s',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
value => sprintf("%.2f", $self->{result_values}->{traffic}),
warning => $warning,
critical => $critical,
min => 0, max => $self->{result_values}->{speed}
);
}
sub custom_ib_threshold {
my ($self, %options) = @_;
my $exit = 'ok';
if ($self->{instance_mode}->{option_results}->{units_traffic} eq '%' && defined($self->{result_values}->{speed})) {
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
} elsif ($self->{instance_mode}->{option_results}->{units_traffic} eq 'b/s') {
$exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
}
return $exit;
}
sub custom_ib_output {
my ($self, %options) = @_;
my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic}, network => 1);
my ($total_value, $total_unit);
if (defined($self->{result_values}->{speed}) && $self->{result_values}->{speed} =~ /[0-9]/) {
($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{speed}, network => 1);
}
my $msg = sprintf("Traffic %s : %s/s (%s on %s)",
ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit,
defined($self->{result_values}->{traffic_prct}) ? sprintf("%.2f%%", $self->{result_values}->{traffic_prct}) : '-',
defined($total_value) ? $total_value . $total_unit : '-');
return $msg;
}
sub custom_ib_calc {
my ($self, %options) = @_;
return -10 if (defined($self->{instance_mode}->{last_status}) && $self->{instance_mode}->{last_status} == 0);
$self->{result_values}->{label} = $options{extra_options}->{label_ref};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
my $diff_traffic = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}} - $options{old_datas}->{$self->{instance} . '_' . $self->{result_values}->{label}};
$self->{result_values}->{traffic} = $diff_traffic / $options{delta_time};
if ($options{new_datas}->{$self->{instance} . '_speed_' . $self->{result_values}->{label}} > 0) {
$self->{result_values}->{speed} = $options{new_datas}->{$self->{instance} . '_speed_' . $self->{result_values}->{label}} * 1000 * 1000;
$self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic} * 100 / $self->{result_values}->{speed};
} elsif (defined($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}}) && $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} =~ /[0-9]/) {
$self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic} * 100 / ($self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000);
$self->{result_values}->{speed} = $self->{instance_mode}->{option_results}->{'speed_' . $self->{result_values}->{label}} * 1000 * 1000;
}
return 0;
}
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'filter-ib-name:s' => { name => 'filter_ib_name' }, 'filter-ib-name:s' => { name => 'filter_ib_name' },
'filter-ibgw-name:s' => { name => 'filter_ibgw_name' }, 'filter-ibgw-name:s' => { name => 'filter_ibgw_name' },
'warning-ib-status:s' => { name => 'warning_ib_status', default => '' }, 'speed-in:s' => { name => 'speed_in' },
'critical-ib-status:s' => { name => 'critical_ib_status', default => '%{status} !~ /active/i' }, 'speed-out:s' => { name => 'speed_out' },
'warning-ibgw-status:s' => { name => 'warning_ibgw_status', default => '' }, 'units-traffic:s' => { name => 'units_traffic', default => '%' }
'critical-ibgw-status:s' => { name => 'critical_ibgw_status', default => '%{status} !~ /up/i' },
'speed-in:s' => { name => 'speed_in' },
'speed-out:s' => { name => 'speed_out' },
'units-traffic:s' => { name => 'units_traffic', default => '%' }
}); });
return $self; return $self;
} }
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_ib_status', 'critical_ib_status', 'warning_ibgw_status', 'critical_ibgw_status']);
}
sub prefix_ib_output { sub prefix_ib_output {
my ($self, %options) = @_; my ($self, %options) = @_;

View File

@ -67,7 +67,7 @@ sub new {
"ntlm" => { name => 'ntlm' }, "ntlm" => { name => 'ntlm' },
"username:s" => { name => 'username' }, "username:s" => { name => 'username' },
"password:s" => { name => 'password' }, "password:s" => { name => 'password' },
"timeout:s" => { name => 'timeout' }, "timeout:s" => { name => 'timeout' }
}); });
$self->{http} = centreon::plugins::http->new(%options); $self->{http} = centreon::plugins::http->new(%options);
$self->{payload_attachment} = { fields => [] }; $self->{payload_attachment} = { fields => [] };
@ -91,11 +91,10 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "You need to specify --host-name option."); $self->{output}->add_option_msg(short_msg => "You need to specify --host-name option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
foreach (('graph_url', 'link_url')) { foreach (('graph_url', 'link_url')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{option_results}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/$self->{option_results}->{$1}/eg;
eval "\$self->{option_results}->{\$_} = \"$self->{option_results}->{$_}\"";
} }
} }

View File

@ -61,7 +61,7 @@ sub new {
"link-url:s" => { name => 'link_url' }, "link-url:s" => { name => 'link_url' },
"centreon-url:s" => { name => 'centreon_url' }, "centreon-url:s" => { name => 'centreon_url' },
"centreon-token:s" => { name => 'centreon_token' }, "centreon-token:s" => { name => 'centreon_token' },
"timeout:s" => { name => 'timeout' }, "timeout:s" => { name => 'timeout' }
}); });
$self->{http} = centreon::plugins::http->new(%options); $self->{http} = centreon::plugins::http->new(%options);
@ -87,8 +87,7 @@ sub check_options {
foreach (('graph_url', 'link_url')) { foreach (('graph_url', 'link_url')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{option_results}->{$1}/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/$self->{option_results}->{$1}/eg;
eval "\$self->{option_results}->{\$_} = \"$self->{option_results}->{$_}\"";
} }
} }
@ -161,9 +160,11 @@ sub format_payload {
my $json = JSON::XS->new->utf8; my $json = JSON::XS->new->utf8;
my $payload = { chat_id =>$self->{option_results}->{chat_id}, my $payload = {
parse_mode => 'HTML', chat_id =>$self->{option_results}->{chat_id},
text => $self->{message} }; parse_mode => 'HTML',
text => $self->{message}
};
eval { eval {
$self->{payload_str} = $json->encode($payload); $self->{payload_str} = $json->encode($payload);
}; };
@ -184,15 +185,16 @@ sub run {
$self->format_payload(); $self->format_payload();
my $url_path = '/bot' . $self->{option_results}->{bot_token} . $self->{option_results}->{url_path}; my $url_path = '/bot' . $self->{option_results}->{bot_token} . $self->{option_results}->{url_path};
my $response = $self->{http}->request(url_path => $url_path, my $response = $self->{http}->request(
method => 'POST', query_form_post => $self->{payload_str}); url_path => $url_path,
method => 'POST', query_form_post => $self->{payload_str}
);
my $decoded; my $decoded;
eval { eval {
$decoded = JSON::XS->new->utf8->decode($response); $decoded = JSON::XS->new->utf8->decode($response);
}; };
if ($@) { if ($@) {
$self->{output}->output_add(long_msg => $response, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }

View File

@ -38,12 +38,12 @@ sub custom_threshold_output {
eval { eval {
local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' && if (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{critical_status}") { $self->eval(value => $self->{instance_mode}->{option_results}->{critical_status})) {
$status = 'critical'; $status = 'critical';
} elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' && } elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' &&
eval "$self->{instance_mode}->{option_results}->{warning_status}") { $self->eval(value => $self->{instance_mode}->{option_results}->{warning_status})) {
$status = 'warning'; $status = 'warning';
} }
@ -1324,27 +1324,29 @@ sub manage_selection {
sub add_result_global { sub add_result_global {
my ($self, %options) = @_; my ($self, %options) = @_;
foreach (('global_admin_up_rule', 'global_admin_down_rule', 'global_oper_up_rule', 'global_oper_down_rule')) { foreach (('global_admin_up_rule', 'global_admin_down_rule', 'global_oper_up_rule', 'global_oper_down_rule')) {
if (defined($self->{option_results}->{$_})) { if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$$1/g; $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g;
} }
} }
$self->{global} = { $self->{global} = {
total_port => 0, global_admin_up => 0, global_admin_down => 0, total_port => 0, global_admin_up => 0, global_admin_down => 0,
global_oper_up => 0, global_oper_down => 0 global_oper_up => 0, global_oper_down => 0
}; };
foreach (@{$self->{array_interface_selected}}) { foreach (@{$self->{array_interface_selected}}) {
my $opstatus = $self->{oid_opstatus_mapping}->{$self->{results}->{$self->{oid_opstatus} . '.' . $_}}; my $values = {
my $admstatus = $self->{oid_adminstatus_mapping}->{$self->{results}->{$self->{oid_adminstatus} . '.' . $_}}; opstatus => $self->{oid_opstatus_mapping}->{ $self->{results}->{$self->{oid_opstatus} . '.' . $_} },
admstatus => $self->{oid_adminstatus_mapping}->{ $self->{results}->{$self->{oid_adminstatus} . '.' . $_} }
};
foreach (('global_admin_up', 'global_admin_down', 'global_oper_up', 'global_oper_down')) { foreach (('global_admin_up', 'global_admin_down', 'global_oper_up', 'global_oper_down')) {
eval { eval {
local $SIG{__WARN__} = sub { return ; }; local $SIG{__WARN__} = sub { return ; };
local $SIG{__DIE__} = sub { return ; }; local $SIG{__DIE__} = sub { return ; };
if (defined($self->{option_results}->{$_ . '_rule'}) && $self->{option_results}->{$_ . '_rule'} ne '' && if (defined($self->{option_results}->{$_ . '_rule'}) && $self->{option_results}->{$_ . '_rule'} ne '' &&
eval "$self->{option_results}->{$_ . '_rule'}") { $self->{output}->test_eval(test => $self->{option_results}->{$_ . '_rule'}, values => $values)) {
$self->{global}->{$_}++; $self->{global}->{$_}++;
} }
}; };

View File

@ -211,7 +211,7 @@ sub store_ok {
sub checking_exist { sub checking_exist {
my ($self, %options) = @_; my ($self, %options) = @_;
foreach my $severity (('critical', 'warning', 'unknown')) { foreach my $severity (('critical', 'warning', 'unknown')) {
foreach my $absent (@{$self->{option_results}->{$severity . '_absent'}}) { foreach my $absent (@{$self->{option_results}->{$severity . '_absent'}}) {
my $match = 0; my $match = 0;
@ -226,7 +226,7 @@ sub checking_exist {
$self->{instances}->{$severity}->{$absent} = $absent; $self->{instances}->{$severity}->{$absent} = $absent;
} }
} }
foreach my $present (@{$self->{option_results}->{$severity . '_present'}}) { foreach my $present (@{$self->{option_results}->{$severity . '_present'}}) {
my $match = 0; my $match = 0;
foreach (keys %{$self->{instances}}) { foreach (keys %{$self->{instances}}) {
@ -243,15 +243,15 @@ sub change_macros {
my $value = $self->{option_results}->{'format_' . $options{severity}}; my $value = $self->{option_results}->{'format_' . $options{severity}};
while ($value =~ /%\{(.*?)\}/g) { while ($value =~ /%\{(.*?)\}/g) {
$value =~ s/%\{($1)\}/\$self->{macros}->{$1}/g; $value =~ s/%\{($1)\}/$self->{macros}->{$1}/eg;
} }
return $value; return $value;
} }
sub build_format_details { sub build_format_details {
my ($self, %options) = @_; my ($self, %options) = @_;
foreach my $severity (('ok', 'critical', 'warning', 'unknown')) { foreach my $severity (('ok', 'critical', 'warning', 'unknown')) {
$self->{macros}->{'details_' . $severity} = ''; $self->{macros}->{'details_' . $severity} = '';
my $append = ''; my $append = '';
@ -261,7 +261,7 @@ sub build_format_details {
$details =~ s/%\{filter_rows\}/$self->{macros}->{filter_rows}/g; $details =~ s/%\{filter_rows\}/$self->{macros}->{filter_rows}/g;
$details =~ s/%\{instance\}/$instance/g; $details =~ s/%\{instance\}/$instance/g;
$details =~ s/%\{value\}/$self->{instances}->{$severity}->{$instance}/g; $details =~ s/%\{value\}/$self->{instances}->{$severity}->{$instance}/g;
$self->{macros}->{'details_' . $severity} .= $append . $details; $self->{macros}->{'details_' . $severity} .= $append . $details;
$append = $self->{option_results}->{'format_details_separator_' . $severity}; $append = $self->{option_results}->{'format_details_separator_' . $severity};
} }
@ -270,15 +270,16 @@ sub build_format_details {
sub display_severity { sub display_severity {
my ($self, %options) = @_; my ($self, %options) = @_;
if (!(defined($options{force}) && $options{force} == 1) && scalar(keys %{$self->{instances}->{$options{severity}}}) == 0) { if (!(defined($options{force}) && $options{force} == 1) && scalar(keys %{$self->{instances}->{$options{severity}}}) == 0) {
return 0; return 0;
} }
my $display = $self->change_macros(severity => $options{severity}); my $display = $self->change_macros(severity => $options{severity});
eval "\$display = \"$display\""; $self->{output}->output_add(
$self->{output}->output_add(severity => $options{severity}, severity => $options{severity},
short_msg => $display); short_msg => $display
);
} }
sub display_result { sub display_result {
@ -296,7 +297,7 @@ sub run {
$self->{snmp} = $options{snmp}; $self->{snmp} = $options{snmp};
$self->get_snmp_values(); $self->get_snmp_values();
foreach (keys %{$self->{instances}}) { foreach (keys %{$self->{instances}}) {
$self->checking_regexp(severity => 'critical', instance => $_, value => $self->{instances}->{$_}) || $self->checking_regexp(severity => 'critical', instance => $_, value => $self->{instances}->{$_}) ||
$self->checking_regexp(severity => 'warning', instance => $_, value => $self->{instances}->{$_}) || $self->checking_regexp(severity => 'warning', instance => $_, value => $self->{instances}->{$_}) ||