From b0b55a27cc3043a4414e6dae8752543e221e68ad Mon Sep 17 00:00:00 2001 From: qgarnier Date: Thu, 1 Apr 2021 09:48:14 +0200 Subject: [PATCH] Secure plugins (#2691) --- .../arcserve/udp/mssql/mode/jobstatus.pm | 24 +- apps/backup/netbackup/local/mode/jobstatus.pm | 101 ++------ .../quadstor/local/mode/vtljobstatus.pm | 133 ++++------ apps/centreon/sql/mode/multiservices.pm | 23 +- apps/centreon/sql/mode/virtualservice.pm | 107 +++++--- .../exchange/local/mode/activesyncmailbox.pm | 2 +- .../exchange/local/mode/imapmailbox.pm | 2 +- .../exchange/local/mode/mapimailbox.pm | 2 +- .../exchange/local/mode/outlookwebservices.pm | 2 +- .../exchange/local/mode/owamailbox.pm | 2 +- .../exchange/local/mode/replicationhealth.pm | 2 +- .../microsoft/exchange/local/mode/services.pm | 2 +- apps/microsoft/mscs/local/mode/listnodes.pm | 8 +- .../mscs/local/mode/listresources.pm | 12 +- .../mscs/local/mode/resourcegroupstatus.pm | 146 +++++------ apps/protocols/http/mode/expectedcontent.pm | 2 +- apps/protocols/modbus/mode/numericvalue.pm | 158 +++++++----- apps/protocols/snmp/mode/collection.pm | 21 +- apps/vmware/connector/custom/connector.pm | 8 +- apps/vmware/connector/mode/alarmdatacenter.pm | 43 ++-- apps/vmware/connector/mode/alarmhost.pm | 63 ++--- .../powershell/exchange/activesyncmailbox.pm | 34 ++- .../common/powershell/exchange/imapmailbox.pm | 36 +-- .../common/powershell/exchange/mapimailbox.pm | 42 ++-- .../powershell/exchange/outlookwebservices.pm | 35 +-- .../common/powershell/exchange/owamailbox.pm | 45 ++-- .../powershell/exchange/replicationhealth.pm | 35 +-- .../common/powershell/exchange/services.pm | 33 ++- .../common/protocols/sql/mode/sqlstring.pm | 85 ++----- centreon/plugins/backend/http/curl.pm | 8 +- centreon/plugins/backend/http/lwp.pm | 9 +- centreon/plugins/output.pm | 69 +++++- centreon/plugins/statefile.pm | 2 +- .../plugins/templates/catalog_functions.pm | 21 +- centreon/plugins/templates/counter.pm | 4 +- centreon/plugins/values.pm | 34 ++- cloud/aws/ec2/mode/status.pm | 48 ++-- .../office365/exchange/mode/mailboxusage.pm | 131 +++++----- network/alcatel/isam/snmp/mode/hubsapusage.pm | 137 +++++------ .../mrv/optiswitch/snmp/mode/interfaces.pm | 22 +- .../infiniband/snmp/mode/infinibandusage.pm | 229 +++++++----------- notification/slack/mode/alert.pm | 7 +- notification/telegram/mode/alert.pm | 20 +- snmp_standard/mode/interfaces.pm | 22 +- snmp_standard/mode/stringvalue.pm | 23 +- 45 files changed, 989 insertions(+), 1005 deletions(-) diff --git a/apps/backup/arcserve/udp/mssql/mode/jobstatus.pm b/apps/backup/arcserve/udp/mssql/mode/jobstatus.pm index a4ce7cff2..8166801ed 100644 --- a/apps/backup/arcserve/udp/mssql/mode/jobstatus.pm +++ b/apps/backup/arcserve/udp/mssql/mode/jobstatus.pm @@ -24,7 +24,6 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); sub custom_status_threshold { my ($self, %options) = @_; @@ -37,13 +36,13 @@ sub custom_status_threshold { # 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}") { + $self->eval(value => $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}") { + $self->eval(value => $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}") { + $self->eval(value => $self->{instance_mode}->{option_results}->{warning_status})) { $status = 'warning'; } }; @@ -57,7 +56,8 @@ sub custom_status_threshold { sub custom_status_output { 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}, $self->{result_values}->{type}, @@ -66,7 +66,6 @@ sub custom_status_output { $self->{result_values}->{plan_name}, scalar(localtime($self->{result_values}->{end_time})) ); - return $msg; } sub set_counters { @@ -90,13 +89,14 @@ sub set_counters { $self->{maps_counters}->{job} = [ { 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 => 'elapsed_time' }, { name => 'end_time' } ], - closure_custom_calc => \&catalog_status_calc, + { name => 'elapsed_time' }, { name => 'end_time' } + ], closure_custom_output => $self->can('custom_status_output'), 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' }, 'warning-status:s' => { name => 'warning_status', default => '' }, 'critical-status:s' => { name => 'critical_status', default => '%{status} != 1' }, - 'timezone:s' => { name => 'timezone' }, + 'timezone:s' => { name => 'timezone' } }); return $self; @@ -202,7 +202,7 @@ sub manage_selection { plan_name => $plan_name, end_time => $row->{end_time}, }; - + $self->{global}->{total}++; } } diff --git a/apps/backup/netbackup/local/mode/jobstatus.pm b/apps/backup/netbackup/local/mode/jobstatus.pm index caa2a7ce5..2a9c853b1 100644 --- a/apps/backup/netbackup/local/mode/jobstatus.pm +++ b/apps/backup/netbackup/local/mode/jobstatus.pm @@ -26,35 +26,7 @@ use strict; use warnings; use centreon::plugins::misc; use Digest::MD5 qw(md5_hex); -use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); - -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; -} +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_threshold_ng); sub custom_status_output { my ($self, %options) = @_; @@ -62,19 +34,9 @@ sub custom_status_output { 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 { my ($self, %options) = @_; - + 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 { - 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 ($self, %options) = @_; + my $status = catalog_status_threshold_ng($self, %options); $self->{instance_mode}->{last_status_frozen} = $status; return $status; } @@ -167,30 +112,35 @@ sub set_counters { $self->{maps_counters}->{job} = [ { label => 'status', threshold => 0, set => { - key_values => [ { name => 'status' }, { name => 'display' }, { name => 'type' }, { name => 'state' } ], - closure_custom_calc => $self->can('custom_status_calc'), + key_values => [ + { name => 'status' }, { name => 'display' }, { name => 'type' }, { name => 'state' } + ], closure_custom_output => $self->can('custom_status_output'), 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 => { - key_values => [ { name => 'status' }, { name => 'display' }, { name => 'elapsed' }, { name => 'type' }, - { name => 'state' } ], + { label => 'long', type => 2, set => { + key_values => [ + { name => 'status' }, { name => 'display' }, { name => 'elapsed' }, { name => 'type' }, + { name => 'state' } + ], closure_custom_calc => $self->can('custom_long_calc'), closure_custom_output => $self->can('custom_long_output'), 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 => { - key_values => [ { name => 'kb', diff => 1 }, { name => 'status' }, + { label => 'frozen', type => 2, critical_default => '%{state} =~ /active|queue/ && %{kb} == 0', set => { + key_values => [ + { name => 'kb', diff => 1 }, { name => 'status' }, { 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_output => $self->can('custom_frozen_output'), 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' }, 'ok-status:s' => { name => 'ok_status', default => '%{status} == 0' }, 'warning-status:s' => { name => 'warning_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' }, + 'critical-status:s' => { name => 'critical_status', default => '%{status} > 1' } }); return $self; @@ -233,10 +179,7 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); - $self->change_macros(macros => [ - 'ok_status', 'warning_status', 'critical_status', 'warning_long', - 'critical_long', 'warning_frozen', 'critical_frozen'] - ); + $self->change_macros(macros => ['ok_status', 'warning_status', 'critical_status']); } sub policy_long_output { diff --git a/apps/backup/quadstor/local/mode/vtljobstatus.pm b/apps/backup/quadstor/local/mode/vtljobstatus.pm index d3cde3b77..b3c2e4d74 100644 --- a/apps/backup/quadstor/local/mode/vtljobstatus.pm +++ b/apps/backup/quadstor/local/mode/vtljobstatus.pm @@ -26,61 +26,24 @@ use strict; use warnings; use centreon::plugins::misc; 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 { 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; + return 'status : ' . $self->{result_values}->{status}; } sub custom_long_output { my ($self, %options) = @_; - my $msg = 'elapsed time : ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{elapsed}); - return $msg; -} - -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; + return 'elapsed time : ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{elapsed}); } sub custom_frozen_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_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 ($self, %options) = @_; + my $status = catalog_status_threshold_ng($self, %options); $self->{instance_mode}->{last_status_frozen} = $status; return $status; } @@ -106,6 +69,12 @@ sub custom_frozen_calc { return 0; } +sub prefix_job_output { + my ($self, %options) = @_; + + return "job '" . $options{instance_value}->{display} . "' "; +} + sub set_counters { my ($self, %options) = @_; @@ -116,20 +85,18 @@ sub set_counters { ]; $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' } ], - 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, + 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' } ], - closure_custom_calc => $self->can('custom_long_calc'), closure_custom_output => $self->can('custom_long_output'), 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 => { @@ -148,54 +115,36 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); bless $self, $class; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "remote" => { name => 'remote' }, - "ssh-option:s@" => { name => 'ssh_option' }, - "ssh-path:s" => { name => 'ssh_path' }, - "ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "sudo" => { name => 'sudo' }, - "command:s" => { name => 'command', default => 'impexp' }, - "command-path:s" => { name => 'command_path', default => '/quadstorvtl/bin' }, - "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' }, - }); - + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'remote' => { name => 'remote' }, + 'ssh-option:s@' => { name => 'ssh_option' }, + 'ssh-path:s' => { name => 'ssh_path' }, + 'ssh-command:s' => { name => 'ssh_command', default => 'ssh' }, + 'timeout:s' => { name => 'timeout', default => 30 }, + 'sudo' => { name => 'sudo' }, + 'command:s' => { name => 'command', default => 'impexp' }, + 'command-path:s' => { name => 'command_path', default => '/quadstorvtl/bin' }, + 'command-options:s' => { name => 'command_options', default => '-l' } + }); + 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 { 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')); - - my ($stdout) = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - sudo => $self->{option_results}->{sudo}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => $self->{option_results}->{command_options}); + + my ($stdout) = centreon::plugins::misc::execute( + output => $self->{output}, + options => $self->{option_results}, + sudo => $self->{option_results}->{sudo}, + command => $self->{option_results}->{command}, + command_path => $self->{option_results}->{command_path}, + command_options => $self->{option_results}->{command_options} + ); $self->{jobs}->{global} = { job => {} }; #JobID Type Source State Transfer Elapsed #252 Import 701831L2 Error 36.00 GB 572 @@ -207,10 +156,10 @@ sub manage_selection { shift @lines; foreach (@lines) { 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) = ($1, $2, $3, $4, $5); - + my $name = $job_source . '.' . $job_id; $self->{jobs}->{global}->{job}->{$name} = { display => $name, @@ -219,7 +168,7 @@ sub manage_selection { elapsed => $job_elapsed }; } - + if (scalar(keys %{$self->{jobs}->{global}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No job found."); $self->{output}->option_exit(); diff --git a/apps/centreon/sql/mode/multiservices.pm b/apps/centreon/sql/mode/multiservices.pm index 531c22b7c..c78a8b765 100644 --- a/apps/centreon/sql/mode/multiservices.pm +++ b/apps/centreon/sql/mode/multiservices.pm @@ -40,9 +40,8 @@ sub custom_hosts_calc { sub custom_hosts_output { 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 $msg + + return "[up:".$self->{result_values}->{total_up}."][down:".$self->{result_values}->{total_down}."][unreachable:".$self->{result_values}->{total_unreachable}."]"; } sub custom_hosts_perfdata { @@ -68,10 +67,10 @@ sub custom_hosts_threshold { local $SIG{__DIE__} = sub { $message = $_[0]; }; 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'; } 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'; } }; @@ -123,10 +122,10 @@ sub custom_services_threshold { local $SIG{__DIE__} = sub { $message = $_[0]; }; 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'; } 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'; } }; @@ -229,10 +228,10 @@ sub custom_groups_threshold { local $SIG{__DIE__} = sub { $message = $_[0]; }; 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'; } 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'; } }; @@ -273,8 +272,10 @@ sub set_counters { $self->{maps_counters}->{logicalgroups} = [ { label => 'group-svc-global', threshold => 0, set => { - key_values => [ { name => 'ok' }, { name => 'unknown' }, { name => 'critical' }, { name => 'warning' }, - { name => 'up' }, { name => 'down' }, { name => 'unreachable' }, { name => 'display' } ], + key_values => [ + { 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_output => $self->can('custom_groups_output'), closure_custom_threshold_check => $self->can('custom_groups_threshold'), diff --git a/apps/centreon/sql/mode/virtualservice.pm b/apps/centreon/sql/mode/virtualservice.pm index b47136355..21ec7d695 100644 --- a/apps/centreon/sql/mode/virtualservice.pm +++ b/apps/centreon/sql/mode/virtualservice.pm @@ -28,56 +28,86 @@ use List::Util qw (min max sum); use JSON; 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 { my ($self, %options) = @_; - my $msg; - my $message; + my $output; 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})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", - eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); - }; + if (defined($config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var}) && defined($config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg})) { + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $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})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{filters}->{formatting}->{printf_msg}", eval "$config_data->{filters}->{formatting}->{printf_var}"); - }; + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{filters}->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $output = sprintf( + $config_data->{filters}->{formatting}->{printf_msg}, + @$vars + ); } 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 (defined($config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", - eval "$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); - }; + if (defined($config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting})) { + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $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})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); - }; + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $output = sprintf( + $config_data->{formatting}->{printf_msg}, + @$vars + ); } 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)) { - $self->{output}->output_add(long_msg => 'printf expression problem: ' . $message); - $self->{output}->option_exit(); - } - - return $msg; - + return $output; } sub custom_metric_calc { @@ -333,9 +363,10 @@ sub manage_selection { max(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'max'); 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})) ? - eval "$config_data->{virtualcurve}->{$vcurve}->{custom}" : - eval "$self->{vmetrics}->{$vcurve}->{aggregated_value} $config_data->{virtualcurve}->{$vcurve}->{custom}"; + $self->{vmetrics}->{$vcurve}->{aggregated_value} = ( + $config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'none' && defined($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} : ''; diff --git a/apps/microsoft/exchange/local/mode/activesyncmailbox.pm b/apps/microsoft/exchange/local/mode/activesyncmailbox.pm index 79c6c95f0..dc302333f 100644 --- a/apps/microsoft/exchange/local/mode/activesyncmailbox.pm +++ b/apps/microsoft/exchange/local/mode/activesyncmailbox.pm @@ -58,7 +58,7 @@ sub change_macros { foreach (('warning', 'critical')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } } diff --git a/apps/microsoft/exchange/local/mode/imapmailbox.pm b/apps/microsoft/exchange/local/mode/imapmailbox.pm index 0772bacb3..6232c1333 100644 --- a/apps/microsoft/exchange/local/mode/imapmailbox.pm +++ b/apps/microsoft/exchange/local/mode/imapmailbox.pm @@ -57,7 +57,7 @@ sub change_macros { foreach (('warning', 'critical')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } } diff --git a/apps/microsoft/exchange/local/mode/mapimailbox.pm b/apps/microsoft/exchange/local/mode/mapimailbox.pm index a35aa4666..64b534b1b 100644 --- a/apps/microsoft/exchange/local/mode/mapimailbox.pm +++ b/apps/microsoft/exchange/local/mode/mapimailbox.pm @@ -56,7 +56,7 @@ sub change_macros { foreach (('warning', 'critical')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } } diff --git a/apps/microsoft/exchange/local/mode/outlookwebservices.pm b/apps/microsoft/exchange/local/mode/outlookwebservices.pm index 8ad641826..027906f49 100644 --- a/apps/microsoft/exchange/local/mode/outlookwebservices.pm +++ b/apps/microsoft/exchange/local/mode/outlookwebservices.pm @@ -57,7 +57,7 @@ sub change_macros { foreach (('warning', 'critical')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } } diff --git a/apps/microsoft/exchange/local/mode/owamailbox.pm b/apps/microsoft/exchange/local/mode/owamailbox.pm index 2600dd5f9..dc14c35f9 100644 --- a/apps/microsoft/exchange/local/mode/owamailbox.pm +++ b/apps/microsoft/exchange/local/mode/owamailbox.pm @@ -59,7 +59,7 @@ sub change_macros { foreach (('warning', 'critical')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } } diff --git a/apps/microsoft/exchange/local/mode/replicationhealth.pm b/apps/microsoft/exchange/local/mode/replicationhealth.pm index a591115af..c0818fe2d 100644 --- a/apps/microsoft/exchange/local/mode/replicationhealth.pm +++ b/apps/microsoft/exchange/local/mode/replicationhealth.pm @@ -55,7 +55,7 @@ sub change_macros { foreach (('warning', 'critical')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } } diff --git a/apps/microsoft/exchange/local/mode/services.pm b/apps/microsoft/exchange/local/mode/services.pm index 6941aee9b..75c6f67c4 100644 --- a/apps/microsoft/exchange/local/mode/services.pm +++ b/apps/microsoft/exchange/local/mode/services.pm @@ -55,7 +55,7 @@ sub change_macros { foreach (('warning', 'critical')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{data}->{$1}/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } } diff --git a/apps/microsoft/mscs/local/mode/listnodes.pm b/apps/microsoft/mscs/local/mode/listnodes.pm index 755308c6c..611290cec 100644 --- a/apps/microsoft/mscs/local/mode/listnodes.pm +++ b/apps/microsoft/mscs/local/mode/listnodes.pm @@ -70,7 +70,7 @@ sub manage_selection { if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $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; } @@ -86,8 +86,10 @@ sub run { $self->{output}->output_add(long_msg => "'" . $name . "' [state = " . $self->{nodes}->{$name}->{state} . "]"); } - $self->{output}->output_add(severity => 'OK', - short_msg => 'List Nodes:'); + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List nodes:' + ); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->exit(); } diff --git a/apps/microsoft/mscs/local/mode/listresources.pm b/apps/microsoft/mscs/local/mode/listresources.pm index 80905ba2e..fb1b93c1c 100644 --- a/apps/microsoft/mscs/local/mode/listresources.pm +++ b/apps/microsoft/mscs/local/mode/listresources.pm @@ -121,11 +121,13 @@ sub disco_show { $self->manage_selection(disco => 1); foreach my $id (sort keys %{$self->{resources}}) { - $self->{output}->add_disco_entry(name => $self->{resources}->{$id}->{name}, - state => $self->{resources}->{$id}->{state}, - id => $id, - owner_node => $self->{resources}->{$id}->{owner_node}, - class => $self->{resources}->{$id}->{class}); + $self->{output}->add_disco_entry( + name => $self->{resources}->{$id}->{name}, + state => $self->{resources}->{$id}->{state}, + id => $id, + owner_node => $self->{resources}->{$id}->{owner_node}, + class => $self->{resources}->{$id}->{class} + ); } } diff --git a/apps/microsoft/mscs/local/mode/resourcegroupstatus.pm b/apps/microsoft/mscs/local/mode/resourcegroupstatus.pm index 41794d524..bb3185a3a 100644 --- a/apps/microsoft/mscs/local/mode/resourcegroupstatus.pm +++ b/apps/microsoft/mscs/local/mode/resourcegroupstatus.pm @@ -25,82 +25,35 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; use Win32::OLE; - -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; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); sub is_preferred_node { - if (!defined($instance_current->{result_values}->{preferred_owners}) || - scalar(@{$instance_current->{result_values}->{preferred_owners}}) == 0) { + my (%options) = @_; + + if (!defined($options{preferred_owners}) || + scalar(@{$options{preferred_owners}}) == 0) { return 1; } - - foreach my $pref_node (@{$instance_current->{result_values}->{preferred_owners}}) { - if ($pref_node eq $instance_current->{result_values}->{owner_node}) { + + foreach my $pref_node (@{$options{preferred_owners}}) { + if ($pref_node eq $options{owner_node}) { return 1; } } - + 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 { my ($self, %options) = @_; my $pref_nodes = 'any'; - if (defined($instance_current->{result_values}->{preferred_owners}) && - scalar(@{$instance_current->{result_values}->{preferred_owners}}) > 0) { - $pref_nodes = join(', ', @{$instance_current->{result_values}->{preferred_owners}}); + if (defined($self->{result_values}->{preferred_owners}) && + scalar(@{$self->{result_values}->{preferred_owners}}) > 0) { + $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 $msg; + return 'state: ' . $self->{result_values}->{state} . ' [node: ' . $self->{result_values}->{owner_node} . '] [preferred nodes: ' . $pref_nodes . ']'; } 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}->{owner_node} = $options{new_datas}->{$self->{instance} . '_owner_node'}; $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; } sub prefix_rg_output { my ($self, %options) = @_; - + 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 { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - - $options{options}->add_options(arguments => - { - "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/' }, - }); + + $options{options}->add_options(arguments => { + 'filter-name:s' => { name => 'filter_name' } + }); return $self; } @@ -138,8 +115,13 @@ sub new { sub check_options { my ($self, %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 = ( @@ -148,7 +130,7 @@ my %map_state = ( 1 => 'offline', 2 => 'failed', 3 => 'partial online', - 4 => 'pending', + 4 => 'pending' ); 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}->option_exit(); } - - my $query = "Select * from MSCluster_ResourceGroupToPreferredNode"; + + my $query = 'Select * from MSCluster_ResourceGroupToPreferredNode'; my $resultset = $wmi->ExecQuery($query); my $preferred_nodes = {}; foreach my $obj (in $resultset) { - use Data::Dumper; - # MSCluster_ResourceGroup.Name="xxx" if ($obj->GroupComponent =~ /MSCluster_ResourceGroup.Name="(.*?)"/i) { my $rg = $1; @@ -176,9 +156,9 @@ sub manage_selection { push @{$preferred_nodes->{$rg}}, $node; } } - + $self->{rg} = {}; - $query = "Select * from MSCluster_ResourceGroup"; + $query = 'Select * from MSCluster_ResourceGroup'; $resultset = $wmi->ExecQuery($query); foreach my $obj (in $resultset) { my $name = $obj->{Name}; @@ -191,9 +171,11 @@ sub manage_selection { $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); next; } - - $self->{rg}->{$id} = { display => $name, state => $state, owner_node => $owner_node, - preferred_owners => defined($preferred_nodes->{$name}) ? $preferred_nodes->{$name} : [] }; + + $self->{rg}->{$id} = { + 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> -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} =item B<--critical-status> diff --git a/apps/protocols/http/mode/expectedcontent.pm b/apps/protocols/http/mode/expectedcontent.pm index c41b1e5a5..0f6c55eec 100644 --- a/apps/protocols/http/mode/expectedcontent.pm +++ b/apps/protocols/http/mode/expectedcontent.pm @@ -41,7 +41,7 @@ sub custom_content_output { my $msg = 'HTTP test(s)'; 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'}; - $filter =~ s/\$self->\{result_values\}->/%/g; + $filter =~ s/\$values->/%/g; $msg = sprintf("Content test [filter: '%s']", $filter); } diff --git a/apps/protocols/modbus/mode/numericvalue.pm b/apps/protocols/modbus/mode/numericvalue.pm index 3c37f0a7c..573df3544 100644 --- a/apps/protocols/modbus/mode/numericvalue.pm +++ b/apps/protocols/modbus/mode/numericvalue.pm @@ -28,56 +28,86 @@ use List::Util qw (min max sum); use JSON; 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 { my ($self, %options) = @_; - my $msg; - my $message; + my $output; 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})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", - eval "$config_data->{selection}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); - }; + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{selection}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $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})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); - }; + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $output = sprintf( + $config_data->{formatting}->{printf_msg}, + @$vars + ); } 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 (defined($config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_msg}", - eval "$config_data->{virtualcurve}->{$self->{result_values}->{instance}}->{formatting}->{printf_var}"); - }; + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{virtualcurve}->{ $self->{result_values}->{instance} }->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $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})) { - eval { - local $SIG{__WARN__} = sub { $message = $_[0]; }; - local $SIG{__DIE__} = sub { $message = $_[0]; }; - $msg = sprintf("$config_data->{formatting}->{printf_msg}", eval "$config_data->{formatting}->{printf_var}"); - }; + my $vars = $self->{instance_mode}->get_printf_vars( + printf_var => $config_data->{formatting}->{printf_var}, + values => $self->{result_values} + ); + $output = sprintf( + $config_data->{formatting}->{printf_msg}, + @$vars + ); } 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)) { - $self->{output}->output_add(long_msg => 'printf expression problem: ' . $message); - $self->{output}->option_exit(); - } - - return $msg; - + return $output; } sub custom_metric_calc { @@ -109,15 +139,15 @@ sub custom_metric_calc { sub custom_metric_perfdata { my ($self, %options) = @_; - $self->{output}->perfdata_add(label => $self->{result_values}->{instance}, - value => $self->{result_values}->{perfdata_value}, - warning => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance}), - critical => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance}), - unit => $self->{result_values}->{perfdata_unit}, - min => $self->{result_values}->{min}, - max => $self->{result_values}->{max}, - ); - + $self->{output}->perfdata_add( + label => $self->{result_values}->{instance}, + value => $self->{result_values}->{perfdata_value}, + warning => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'warning-metric' : 'warning-global-'.$self->{result_values}->{instance}), + critical => $self->{perfdata}->get_perfdata_for_output(label => ($self->{result_values}->{type} eq 'unique') ? 'critical-metric' : 'critical-global-'.$self->{result_values}->{instance}), + unit => $self->{result_values}->{perfdata_unit}, + min => $self->{result_values}->{min}, + max => $self->{result_values}->{max}, + ); } 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_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}, - threshold => [ { label => $label_crit, exit_litteral => 'critical' }, - { label => $label_warn, exit_litteral => 'warning' } ]); + my $exit = $self->{perfdata}->threshold_check( + value => $self->{result_values}->{perfdata_value}, + threshold => [ + { label => $label_crit, exit_litteral => 'critical' }, + { label => $label_warn, exit_litteral => 'warning' } + ] + ); return $exit; } @@ -143,7 +177,7 @@ sub set_counters { closure_custom_calc => $self->can('custom_metric_calc'), closure_custom_output => $self->can('custom_metric_output'), 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_output => $self->can('custom_metric_output'), 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); bless $self, $class; - $options{options}->add_options(arguments => - { - "config:s" => { name => 'config' }, - }); + $options{options}->add_options(arguments => { + 'config:s' => { name => 'config' } + }); + return $self; } @@ -291,15 +325,19 @@ sub manage_selection { } 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}, - sum(@{$self->{vmetrics}->{$vcurve}->{values}}) / scalar(@{$self->{vmetrics}->{$vcurve}->{values}})) if ($config_data->{virtualcurve}->{$vcurve}->{aggregation} eq 'avg'); - $self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf($config_data->{formatting}->{printf_metric_value}, - 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}, - 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} = 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'); + $self->{vmetrics}->{$vcurve}->{aggregated_value} = sprintf( + $config_data->{formatting}->{printf_metric_value}, + 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}, + 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}->{unit} = (defined($config_data->{virtualcurve}->{$vcurve}->{unit})) ? $config_data->{virtualcurve}->{$vcurve}->{unit} : ''; diff --git a/apps/protocols/snmp/mode/collection.pm b/apps/protocols/snmp/mode/collection.pm index 5a3732087..2aacca923 100644 --- a/apps/protocols/snmp/mode/collection.pm +++ b/apps/protocols/snmp/mode/collection.pm @@ -36,14 +36,15 @@ sub custom_select_threshold { local $SIG{__WARN__} = 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} && - eval "$self->{result_values}->{config}->{critical}") { + $self->{safe}->reval($self->{result_values}->{config}->{critical})) { $status = 'critical'; } 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'; } 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'; } if ($@) { @@ -128,6 +129,8 @@ sub new { 'filter-selection:s%' => { name => 'filter_selection' }, }); + $self->{safe} = Safe->new(); + $self->{safe}->share('$expand'); $self->{snmp_cache} = centreon::plugins::statefile->new(%options); return $self; } @@ -732,7 +735,7 @@ sub prepare_variables { my ($self, %options) = @_; 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}; } @@ -740,8 +743,14 @@ sub check_filter { my ($self, %options) = @_; return 0 if (!defined($options{filter}) || $options{filter} eq ''); - $options{filter} =~ s/%\(([a-z-A-Z0-9\.]+?)\)/\$self->{expand}->{'$1'}/g; - return 0 if (eval "$options{filter}"); + our $expand = $self->{expand}; + $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; } diff --git a/apps/vmware/connector/custom/connector.pm b/apps/vmware/connector/custom/connector.pm index 0a223293e..da9bad032 100644 --- a/apps/vmware/connector/custom/connector.pm +++ b/apps/vmware/connector/custom/connector.pm @@ -159,7 +159,7 @@ sub connector_response_status { } foreach (('unknown_connector_status', 'warning_connector_status', 'critical_connector_status')) { - $self->{$_} =~ s/%\{(.*?)\}/\$self->{result}->{$1}/g; + $self->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } # Check response @@ -170,13 +170,13 @@ sub connector_response_status { local $SIG{__DIE__} = sub { $message = $_[0]; }; 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'; } 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'; } 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'; } }; diff --git a/apps/vmware/connector/mode/alarmdatacenter.pm b/apps/vmware/connector/mode/alarmdatacenter.pm index 55e85d4e7..af3513159 100644 --- a/apps/vmware/connector/mode/alarmdatacenter.pm +++ b/apps/vmware/connector/mode/alarmdatacenter.pm @@ -36,14 +36,12 @@ sub custom_status_threshold { 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}") { + if (defined($self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}} ne '' && + $self->eval(value => $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}})) { $self->{instance_mode}->{dc_critical}++; $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}") { + } elsif (defined($self->{instance_mode}->{option_results}->{'warning-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}} ne '' && + $self->eval(value => $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}})) { $self->{instance_mode}->{dc_warning}++; $status = 'warning'; } @@ -58,7 +56,7 @@ sub custom_status_threshold { sub custom_status_output { my ($self, %options) = @_; - my $msg = sprintf( + return sprintf( 'alarm [%s] [%s] [%s] [%s] %s/%s', $self->{result_values}->{status}, $self->{result_values}->{type}, @@ -67,8 +65,6 @@ sub custom_status_output { $self->{result_values}->{name}, $self->{result_values}->{description} ); - - return $msg; } sub custom_dcmetrics_perfdata { @@ -131,7 +127,11 @@ sub set_counters { ]; $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 => [ { name => 'entity_name' }, { name => 'status' }, { name => 'time' }, { name => 'description' }, { name => 'name' }, { name => 'type' }, { name => 'since' } @@ -144,7 +144,7 @@ sub set_counters { ]; $self->{maps_counters}->{dc_metrics} = [ - { label => 'alarm-warning', threshold => 0, set => { + { label => 'alarm-warning', type => 2, set => { key_values => [ { name => 'name' } ], output_template => '', closure_custom_threshold_check => sub { return 'ok' }, @@ -152,7 +152,7 @@ sub set_counters { closure_custom_perfdata => $self->can('custom_dcmetrics_perfdata') } }, - { label => 'alarm-critical', threshold => 0, set => { + { label => 'alarm-critical', type => 2, set => { key_values => [ { name => 'name' } ], output_template => '', closure_custom_threshold_check => sub { return 'ok' }, @@ -171,7 +171,7 @@ sub prefix_datacenter_output { sub alarm_reset { my ($self, %options) = @_; - + $self->{dc_warning} = 0; $self->{dc_critical} = 0; } @@ -188,16 +188,16 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - "datacenter:s" => { name => 'datacenter' }, - "filter" => { name => 'filter' }, - "filter-time:s" => { name => 'filter_time', }, - "memory" => { name => 'memory', }, - "warning-status:s" => { name => 'warning_status', default => '%{status} =~ /yellow/i' }, - "critical-status:s" => { name => 'critical_status', default => '%{status} =~ /red/i' }, + 'datacenter:s' => { name => 'datacenter' }, + 'filter' => { name => 'filter' }, + 'filter-time:s' => { name => 'filter_time' }, + 'memory' => { name => 'memory' } }); - centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse', - error_msg => "Cannot load module 'Date::Parse'."); + centreon::plugins::misc::mymodule_load( + output => $self->{output}, module => 'Date::Parse', + error_msg => "Cannot load module 'Date::Parse'." + ); $self->{statefile_cache} = centreon::plugins::statefile->new(%options); return $self; @@ -207,7 +207,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); - $self->change_macros(macros => ['warning_status', 'critical_status']); if (defined($self->{option_results}->{memory})) { $self->{statefile_cache}->check_options(%options); } diff --git a/apps/vmware/connector/mode/alarmhost.pm b/apps/vmware/connector/mode/alarmhost.pm index 49701c3eb..538b1ad08 100644 --- a/apps/vmware/connector/mode/alarmhost.pm +++ b/apps/vmware/connector/mode/alarmhost.pm @@ -36,14 +36,12 @@ sub custom_status_threshold { 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}") { + if (defined($self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}} ne '' && + $self->eval(value => $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}})) { $self->{instance_mode}->{host_critical}++; $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}") { + } elsif (defined($self->{instance_mode}->{option_results}->{'warning-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}} ne '' && + $self->eval(value => $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}})) { $self->{instance_mode}->{host_warning}++; $status = 'warning'; } @@ -58,7 +56,8 @@ sub custom_status_threshold { sub custom_status_output { 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}->{type}, $self->{result_values}->{entity_name}, @@ -66,8 +65,6 @@ sub custom_status_output { $self->{result_values}->{name}, $self->{result_values}->{description} ); - - return $msg; } sub custom_esxhost_perfdata { @@ -129,9 +126,15 @@ sub set_counters { ]; $self->{maps_counters}->{alarm} = [ - { label => 'status', threshold => 0, set => { - key_values => [ { name => 'entity_name' }, { name => 'status' }, - { name => 'time' }, { name => 'description' }, { name => 'name' }, { name => 'type' }, { name => 'since' } ], + { + label => 'status', type => 2, + 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_perfdata => sub { return 0; }, closure_custom_threshold_check => $self->can('custom_status_threshold') @@ -140,7 +143,7 @@ sub set_counters { ]; $self->{maps_counters}->{esxhost_metrics} = [ - { label => 'alarm-warning', threshold => 0, set => { + { label => 'alarm-warning', type => 2, set => { key_values => [ { name => 'name' } ], output_template => '', closure_custom_threshold_check => sub { return 'ok' }, @@ -148,7 +151,7 @@ sub set_counters { closure_custom_perfdata => $self->can('custom_esxhost_perfdata') } }, - { label => 'alarm-critical', threshold => 0, set => { + { label => 'alarm-critical', type => 2, set => { key_values => [ { name => 'name' } ], output_template => '', closure_custom_threshold_check => sub { return 'ok' }, @@ -183,20 +186,19 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "esx-hostname:s" => { name => 'esx_hostname' }, - "filter" => { name => 'filter' }, - "scope-datacenter:s" => { name => 'scope_datacenter' }, - "scope-cluster:s" => { name => 'scope_cluster' }, - "filter-time:s" => { name => 'filter_time', }, - "memory" => { name => 'memory', }, - "warning-status:s" => { name => 'warning_status', default => '%{status} =~ /yellow/i' }, - "critical-status:s" => { name => 'critical_status', default => '%{status} =~ /red/i' }, - }); + $options{options}->add_options(arguments => { + 'esx-hostname:s' => { name => 'esx_hostname' }, + 'filter' => { name => 'filter' }, + 'scope-datacenter:s' => { name => 'scope_datacenter' }, + 'scope-cluster:s' => { name => 'scope_cluster' }, + 'filter-time:s' => { name => 'filter_time' }, + 'memory' => { name => 'memory' } + }); - centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse', - error_msg => "Cannot load module 'Date::Parse'."); + centreon::plugins::misc::mymodule_load( + output => $self->{output}, module => 'Date::Parse', + error_msg => "Cannot load module 'Date::Parse'." + ); $self->{statefile_cache} = centreon::plugins::statefile->new(%options); return $self; } @@ -205,7 +207,6 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); - $self->change_macros(macros => ['warning_status', 'critical_status']); if (defined($self->{option_results}->{memory})) { $self->{statefile_cache}->check_options(%options); } @@ -216,8 +217,10 @@ sub manage_selection { $self->{global} = { yellow => 0, red => 0 }; $self->{esxhost} = {}; - my $response = $options{custom}->execute(params => $self->{option_results}, - command => 'alarmhost'); + my $response = $options{custom}->execute( + params => $self->{option_results}, + command => 'alarmhost' + ); my $last_time; if (defined($self->{option_results}->{memory})) { diff --git a/centreon/common/powershell/exchange/activesyncmailbox.pm b/centreon/common/powershell/exchange/activesyncmailbox.pm index 302ab068c..c42d0bf7b 100644 --- a/centreon/common/powershell/exchange/activesyncmailbox.pm +++ b/centreon/common/powershell/exchange/activesyncmailbox.pm @@ -73,18 +73,18 @@ sub check { ($self->{data}->{scenario}, $self->{data}->{result}, $self->{data}->{latency}, $self->{data}->{error}) = ($self->{output}->decode($1), centreon::plugins::misc::trim($2), centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); - + $checked++; my ($status, $message) = ('ok'); eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - + 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'; } 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'; } }; @@ -92,21 +92,29 @@ sub check { $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => sprintf("ActiveSync scenario '%s' to '%s' is '%s'", - $self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result})); + $self->{output}->output_add( + severity => $status, + short_msg => sprintf( + "ActiveSync scenario '%s' to '%s' is '%s'", + $self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result} + ) + ); } if ($self->{data}->{latency} =~ /^(\d+)/) { - $self->{output}->perfdata_add(label => $self->{data}->{scenario}, unit => 's', - value => sprintf("%.3f", $1 / 1000), - min => 0); + $self->{output}->perfdata_add( + label => $self->{data}->{scenario}, unit => 's', + value => sprintf("%.3f", $1 / 1000), + min => 0 + ); } } - + if ($checked == 0) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Cannot find informations'); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => 'Cannot find informations' + ); } } diff --git a/centreon/common/powershell/exchange/imapmailbox.pm b/centreon/common/powershell/exchange/imapmailbox.pm index 3dbed772f..57974b265 100644 --- a/centreon/common/powershell/exchange/imapmailbox.pm +++ b/centreon/common/powershell/exchange/imapmailbox.pm @@ -56,13 +56,13 @@ exit 0 sub check { my ($self, %options) = @_; - # options: stdout # Following output: #[scenario= Options ][result= Failure ][latency= 52,00 ][[error=...]] - $self->{output}->output_add(severity => 'OK', - short_msg => "Imap to '" . $options{mailbox} . "' is ok."); - + $self->{output}->output_add( + severity => 'OK', + short_msg => "Imap to '" . $options{mailbox} . "' is ok." + ); my $checked = 0; $self->{output}->output_add(long_msg => $options{stdout}); while ($options{stdout} =~ /\[scenario=(.*?)\]\[result=(.*?)\]\[latency=(.*?)\]\[\[error=(.*?)\]\]/msg) { @@ -72,17 +72,17 @@ sub check { centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); $checked++; - + my ($status, $message) = ('ok'); eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - + 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'; } 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'; } }; @@ -90,11 +90,15 @@ sub check { $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => sprintf("Imap scenario '%s' to '%s' is '%s'", - $self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result})); + $self->{output}->output_add( + severity => $status, + short_msg => sprintf( + "Imap scenario '%s' to '%s' is '%s'", + $self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result} + ) + ); } - + if ($self->{data}->{latency} =~ /^(\d+)/) { $self->{output}->perfdata_add( label => $self->{data}->{scenario}, unit => 's', @@ -103,10 +107,12 @@ sub check { ); } } - + if ($checked == 0) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Cannot find informations'); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => 'Cannot find informations' + ); } } diff --git a/centreon/common/powershell/exchange/mapimailbox.pm b/centreon/common/powershell/exchange/mapimailbox.pm index 676e2c371..b2153d395 100644 --- a/centreon/common/powershell/exchange/mapimailbox.pm +++ b/centreon/common/powershell/exchange/mapimailbox.pm @@ -49,14 +49,14 @@ exit 0 sub check { my ($self, %options) = @_; - # options: stdout - + # Following output: #[name= Mailbox Database 0975194476 ][server= SRVI-WIN-TEST ][result= Success ][error=...] - if ($options{stdout} !~ /^\[name=(.*?)\]\[server=(.*?)\]\[result=(.*?)\]\[error=(.*)\]$/) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Cannot find informations'); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => 'Cannot find informations' + ); return ; } $self->{data} = {}; @@ -64,21 +64,27 @@ sub check { (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); - $self->{output}->output_add(severity => 'OK', - short_msg => "MAPI connection to '" . $options{mailbox} . "' is '" . $self->{data}->{result} . "'."); - $self->{output}->output_add(long_msg => sprintf("Database: %s, Server: %s\nError: %s", - $self->{data}->{database}, $self->{data}->{server}, $self->{data}->{error})); - + $self->{output}->output_add( + severity => 'OK', + short_msg => "MAPI connection to '" . $options{mailbox} . "' is '" . $self->{data}->{result} . "'." + ); + $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'); eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - + 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'; } 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'; } }; @@ -86,9 +92,13 @@ sub check { $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => sprintf("MAPI connection to '%s' is '%s'", - $options{mailbox}, $self->{data}->{result})); + $self->{output}->output_add( + severity => $status, + short_msg => sprintf( + "MAPI connection to '%s' is '%s'", + $options{mailbox}, $self->{data}->{result} + ) + ); } } diff --git a/centreon/common/powershell/exchange/outlookwebservices.pm b/centreon/common/powershell/exchange/outlookwebservices.pm index 6e29217f6..1b2acf2c8 100644 --- a/centreon/common/powershell/exchange/outlookwebservices.pm +++ b/centreon/common/powershell/exchange/outlookwebservices.pm @@ -65,13 +65,14 @@ exit 0 sub check { my ($self, %options) = @_; - # options: stdout # Following output: #[id= XXXX ][type= Success][[message=...]] - $self->{output}->output_add(severity => 'OK', - short_msg => sprintf("Outlook webservices to '%s' are ok.", $options{mailbox})); - + $self->{output}->output_add( + severity => 'OK', + short_msg => sprintf("Outlook webservices to '%s' are ok.", $options{mailbox}) + ); + my $checked = 0; $self->{output}->output_add(long_msg => $options{stdout}); 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)); $checked++; - + my ($status, $message) = ('ok'); eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - + 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'; } 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'; } }; @@ -98,15 +99,21 @@ sub check { $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => sprintf("Check id '%s' status is '%s' [message: %s]", - $self->{data}->{id}, $self->{data}->{type}, $self->{data}->{message})); + $self->{output}->output_add( + severity => $status, + short_msg => sprintf( + "Check id '%s' status is '%s' [message: %s]", + $self->{data}->{id}, $self->{data}->{type}, $self->{data}->{message} + ) + ); } } - + if ($checked == 0) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Cannot find informations'); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => 'Cannot find informations' + ); } } diff --git a/centreon/common/powershell/exchange/owamailbox.pm b/centreon/common/powershell/exchange/owamailbox.pm index 64dac980c..b7252fd4d 100644 --- a/centreon/common/powershell/exchange/owamailbox.pm +++ b/centreon/common/powershell/exchange/owamailbox.pm @@ -57,13 +57,14 @@ exit 0 sub check { my ($self, %options) = @_; - # options: stdout - + # Following output: #[url= http://xxxx/ ][scenario= Options ][result= Ignored ][latency= ][[error=...]] - $self->{output}->output_add(severity => 'OK', - short_msg => "OWA to '" . $options{mailbox} . "' is ok."); - + $self->{output}->output_add( + severity => 'OK', + short_msg => "OWA to '" . $options{mailbox} . "' is ok." + ); + my $checked = 0; $self->{output}->output_add(long_msg => $options{stdout}); 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->{output}->decode($1), $self->{output}->decode($2), centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5)); - + $checked++; my ($status, $message) = ('ok'); eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - + 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'; } 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'; } }; @@ -90,21 +91,29 @@ sub check { $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => sprintf("OWA scenario '%s' to '%s' is '%s' [url: %s]", - $self->{data}->{scenario}, $options{mailbox}, $self->{data}->{result}, $self->{data}->{url})); + $self->{output}->output_add( + severity => $status, + 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+)/) { - $self->{output}->perfdata_add(label => $self->{data}->{url} . '_' . $self->{data}->{scenario}, unit => 's', - value => sprintf("%.3f", $1 / 1000), - min => 0); + $self->{output}->perfdata_add( + label => $self->{data}->{url} . '_' . $self->{data}->{scenario}, unit => 's', + value => sprintf("%.3f", $1 / 1000), + min => 0 + ); } } if ($checked == 0) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Cannot find informations'); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => 'Cannot find informations' + ); } } diff --git a/centreon/common/powershell/exchange/replicationhealth.pm b/centreon/common/powershell/exchange/replicationhealth.pm index 6d612d5fa..98694cfdf 100644 --- a/centreon/common/powershell/exchange/replicationhealth.pm +++ b/centreon/common/powershell/exchange/replicationhealth.pm @@ -50,13 +50,14 @@ exit 0 sub check { my ($self, %options) = @_; - # options: stdout - + # Following output: #[Server= XXXX ][check= ReplayService][result= Passed ][isvalid= Yes][[error=...]] - $self->{output}->output_add(severity => 'OK', - short_msg => "All replication health tests are ok."); - + $self->{output}->output_add( + severity => 'OK', + short_msg => "All replication health tests are ok." + ); + my $checked = 0; $self->{output}->output_add(long_msg => $options{stdout}); 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)); $checked++; - + my ($status, $message) = ('ok'); eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; - + 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'; } 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'; } }; @@ -84,15 +85,21 @@ sub check { $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - 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})); + $self->{output}->output_add( + severity => $status, + 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) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Cannot find informations'); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => 'Cannot find informations' + ); } } diff --git a/centreon/common/powershell/exchange/services.pm b/centreon/common/powershell/exchange/services.pm index 6647a61ec..c3db6dfc0 100644 --- a/centreon/common/powershell/exchange/services.pm +++ b/centreon/common/powershell/exchange/services.pm @@ -52,13 +52,14 @@ exit 0 sub check { my ($self, %options) = @_; - # options: stdout - + # Following output: #[role= Mailbox Server Role ][requiredservicesrunning= True ][servicesrunning= IISAdmin,MSExchangeADTopology,MSExchangeSA,... ][servicesnotrunning= ] - $self->{output}->output_add(severity => 'OK', - short_msg => "All role services are ok."); - + $self->{output}->output_add( + severity => 'OK', + short_msg => "All role services are ok." + ); + my $checked = 0; $self->{output}->output_add(long_msg => $options{stdout}); while ($options{stdout} =~ /\[role=(.*?)\]\[requiredservicesrunning=(.*?)\]\[servicesrunning=(.*?)\]\[servicesnotrunning=(.*?)\]/msg) { @@ -68,17 +69,17 @@ sub check { centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4)); $checked++; - + my ($status, $message) = ('ok'); eval { local $SIG{__WARN__} = sub { $message = $_[0]; }; local $SIG{__DIE__} = sub { $message = $_[0]; }; 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'; } 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'; } }; @@ -86,15 +87,21 @@ sub check { $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => sprintf("Role '%s' services problem [services not running: %s]", - $self->{data}->{role}, $self->{data}->{servicesnotrunning})); + $self->{output}->output_add( + severity => $status, + short_msg => sprintf( + "Role '%s' services problem [services not running: %s]", + $self->{data}->{role}, $self->{data}->{servicesnotrunning} + ) + ); } } if ($checked == 0) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => 'Cannot find informations'); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => 'Cannot find informations' + ); } } diff --git a/centreon/common/protocols/sql/mode/sqlstring.pm b/centreon/common/protocols/sql/mode/sqlstring.pm index b715f4067..39c0caf03 100644 --- a/centreon/common/protocols/sql/mode/sqlstring.pm +++ b/centreon/common/protocols/sql/mode/sqlstring.pm @@ -23,7 +23,7 @@ package centreon::common::protocols::sql::mode::sqlstring; use base qw(centreon::plugins::templates::counter); use strict; 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 { my ($self, %options) = @_; @@ -33,26 +33,16 @@ sub set_counters { ]; $self->{maps_counters}->{rows} = [ - { label => 'string', threshold => 0, set => { + { label => 'string', type => 2, set => { 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_threshold_check => $self->can('custom_string_threshold'), - closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng, + 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 { my ($self, %options) = @_; @@ -75,46 +65,19 @@ sub custom_string_output { 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 { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; $options{options}->add_options(arguments => { - "sql-statement:s" => { name => 'sql_statement' }, - "key-column:s" => { name => 'key_column' }, - "value-column:s" => { name => 'value_column' }, - "warning-string:s" => { name => 'warning_string', default => '' }, - "critical-string:s" => { name => 'critical_string', default => '' }, - "printf-format:s" => { name => 'printf_format' }, - "printf-value:s" => { name => 'printf_value' }, - "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' }, + 'sql-statement:s' => { name => 'sql_statement' }, + 'key-column:s' => { name => 'key_column' }, + 'value-column:s' => { name => 'value_column' }, + 'printf-format:s' => { name => 'printf_format' }, + 'printf-value:s' => { name => 'printf_value' }, + '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; @@ -128,19 +91,17 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Need to specify '--sql-statement' option."); $self->{output}->option_exit(); } - - $self->change_macros(macros => ['warning_string', 'critical_string']); } sub manage_selection { my ($self, %options) = @_; - $self->{sql} = $options{sql}; - $self->{sql}->connect(); - $self->{sql}->query(query => $self->{option_results}->{sql_statement}); + + $options{sql}->connect(); + $options{sql}->query(query => $self->{option_results}->{sql_statement}); $self->{rows} = {}; 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})) { $row->{$self->{option_results}->{value_column}} = delete $row->{keys %{$row}}; foreach (keys %{$row}) { @@ -148,17 +109,21 @@ sub manage_selection { } } if (!defined($self->{option_results}->{key_column})) { - $self->{rows}->{$self->{option_results}->{value_column} . $row_count} = { key_field => $row->{$self->{option_results}->{value_column}}, - value_field => $row->{$self->{option_results}->{value_column}}}; + $self->{rows}->{$self->{option_results}->{value_column} . $row_count} = { + key_field => $row->{ $self->{option_results}->{value_column} }, + value_field => $row->{ $self->{option_results}->{value_column} } + }; $row_count++; } else { - $self->{rows}->{$self->{option_results}->{key_column} . $row_count} = { key_field => $row->{$self->{option_results}->{key_column}}, - value_field => $row->{$self->{option_results}->{value_column}}}; + $self->{rows}->{$self->{option_results}->{key_column} . $row_count} = { + key_field => $row->{ $self->{option_results}->{key_column} }, + value_field => $row->{ $self->{option_results}->{value_column} } + }; $row_count++; } } - $self->{sql}->disconnect(); + $options{sql}->disconnect(); if (scalar(keys %{$self->{rows}}) <= 0) { $self->{output}->add_option_msg(short_msg => $self->{option_results}->{empty_sql_string}); $self->{output}->option_exit(); diff --git a/centreon/plugins/backend/http/curl.pm b/centreon/plugins/backend/http/curl.pm index 2967d7234..550ee5984 100644 --- a/centreon/plugins/backend/http/curl.pm +++ b/centreon/plugins/backend/http/curl.pm @@ -59,7 +59,7 @@ sub check_options { foreach (('unknown_status', 'warning_status', 'critical_status')) { 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]; }; 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'; } 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'; } 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'; } }; diff --git a/centreon/plugins/backend/http/lwp.pm b/centreon/plugins/backend/http/lwp.pm index 568ca5ca3..3829acdc1 100644 --- a/centreon/plugins/backend/http/lwp.pm +++ b/centreon/plugins/backend/http/lwp.pm @@ -51,7 +51,7 @@ sub check_options { foreach (('unknown_status', 'warning_status', 'critical_status')) { 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{__DIE__} = sub { $message = $_[0]; }; + my $code = $self->{response}->code(); 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'; } 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'; } 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'; } }; diff --git a/centreon/plugins/output.pm b/centreon/plugins/output.pm index dcce213e1..9970b4dd5 100644 --- a/centreon/plugins/output.pm +++ b/centreon/plugins/output.pm @@ -83,6 +83,7 @@ sub new { $self->{global_status} = 0; $self->{encode_import} = 0; $self->{perlqq} = 0; + $self->{safe_test} = 0; $self->{disco_elements} = []; $self->{disco_entries} = []; @@ -869,6 +870,60 @@ sub is_debug { 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 { my ($self, %options) = @_; @@ -1090,8 +1145,7 @@ sub apply_pfdata_math { sub apply_pfdata_min { my ($self, %options) = @_; - my $pattern_pf; - eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\""); my $min; for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); @@ -1107,8 +1161,7 @@ sub apply_pfdata_min { sub apply_pfdata_max { my ($self, %options) = @_; - my $pattern_pf; - eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\""); my $max; for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); @@ -1124,8 +1177,7 @@ sub apply_pfdata_max { sub apply_pfdata_sum { my ($self, %options) = @_; - my $pattern_pf; - eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\""); my ($sum, $num) = (0, 0); for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); @@ -1141,8 +1193,7 @@ sub apply_pfdata_sum { sub apply_pfdata_average { my ($self, %options) = @_; - my $pattern_pf; - eval "\$pattern_pf = \"$options{args}->{pattern_pf}\""; + my $pattern_pf = $self->assign_eval(eval => "\"$options{args}->{pattern_pf}\""); my ($sum, $num) = (0, 0); for (my $i = 0; $i < scalar(@{$self->{perfdatas}}); $i++) { next if ($self->{perfdatas}->[$i]->{label} !~ /$pattern_pf/); @@ -1387,7 +1438,7 @@ Change traffic values in percent: --change-perfdata=traffic_in,,percent() =back -=item B<--extend-perfdata-group> +=item B<--extend-perfdata-group> Extend perfdata from multiple perfdatas (methods in target are: min, max, average, sum) Syntax: --extend-perfdata-group=searchlabel,newlabel,target[,[newuom],[min],[max]] diff --git a/centreon/plugins/statefile.pm b/centreon/plugins/statefile.pm index efe80d359..d0c239b8d 100644 --- a/centreon/plugins/statefile.pm +++ b/centreon/plugins/statefile.pm @@ -197,7 +197,7 @@ sub read { } if ($self->{storable} == 1) { - open FILE, $self->{statefile_dir} . '/' . $self->{statefile}; + open FILE, '<', $self->{statefile_dir} . '/' . $self->{statefile}; eval { $self->{datas} = Storable::fd_retrieve(*FILE); }; diff --git a/centreon/plugins/templates/catalog_functions.pm b/centreon/plugins/templates/catalog_functions.pm index 484c93105..e18917ccc 100644 --- a/centreon/plugins/templates/catalog_functions.pm +++ b/centreon/plugins/templates/catalog_functions.pm @@ -39,16 +39,16 @@ sub catalog_status_threshold { my $label = $self->{label}; $label =~ s/-/_/g; 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'; } 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'; } 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'; } 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'; } }; @@ -68,15 +68,14 @@ sub catalog_status_threshold_ng { local $SIG{__WARN__} = 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-' . $label} ne '' && - eval "$self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}") { + if (defined($self->{instance_mode}->{option_results}->{'critical-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'critical-' . $self->{label}} ne '' && + $self->eval(value => $self->{instance_mode}->{option_results}->{'critical-' . $self->{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}") { + } elsif (defined($self->{instance_mode}->{option_results}->{'warning-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}} ne '' && + $self->eval(value => $self->{instance_mode}->{option_results}->{'warning-' . $self->{label}})) { $status = 'warning'; - } 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}") { + } elsif (defined($self->{instance_mode}->{option_results}->{'unknown-' . $self->{label}}) && $self->{instance_mode}->{option_results}->{'unknown-' . $self->{label}} ne '' && + $self->eval(value => $self->{instance_mode}->{option_results}->{'unknown-' . $self->{label}})) { $status = 'unknown'; } }; diff --git a/centreon/plugins/templates/counter.pm b/centreon/plugins/templates/counter.pm index 73e1b24fa..49f3b21bd 100644 --- a/centreon/plugins/templates/counter.pm +++ b/centreon/plugins/templates/counter.pm @@ -134,7 +134,7 @@ sub new { ); $self->{statefile_value} = centreon::plugins::statefile->new(%options); } - + $self->{maps_counters} = {} if (!defined($self->{maps_counters})); $self->set_counters(%options); @@ -755,7 +755,7 @@ sub change_macros { foreach (@{$options{macros}}) { 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; } } } diff --git a/centreon/plugins/values.pm b/centreon/plugins/values.pm index 0cffa844b..22964cb73 100644 --- a/centreon/plugins/values.pm +++ b/centreon/plugins/values.pm @@ -22,6 +22,7 @@ package centreon::plugins::values; use strict; use warnings; +use centreon::plugins::misc; # Warning message with sprintf and too much arguments. # Really annoying. Need to disable that warning no if ($^V gt v5.22.0), 'warnings' => 'redundant'; @@ -55,7 +56,8 @@ sub new { $self->{last_timestamp} = undef; $self->{result_values} = {}; - + $self->{safe_test} = 0; + 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 { my ($self, %options) = @_; my $old_datas = {}; diff --git a/cloud/aws/ec2/mode/status.pm b/cloud/aws/ec2/mode/status.pm index 6363234b8..b6f0ff897 100644 --- a/cloud/aws/ec2/mode/status.pm +++ b/cloud/aws/ec2/mode/status.pm @@ -26,18 +26,18 @@ use strict; use warnings; my %map_type = ( - "instance" => "InstanceId", - "asg" => "AutoScalingGroupName", + 'instance' => 'InstanceId', + 'asg' => 'AutoScalingGroupName' ); my %map_status = ( 0 => 'passed', - 1 => 'failed', + 1 => 'failed' ); sub prefix_metric_output { my ($self, %options) = @_; - + return ucfirst($options{instance_value}->{type}) . " '" . $options{instance_value}->{display} . "' "; } @@ -49,12 +49,12 @@ sub custom_status_threshold { 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}") { + $self->eval(value => $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}") { + $self->eval(value => $self->{instance_mode}->{option_results}->{warning_status})) { $status = 'warning'; } }; @@ -68,13 +68,12 @@ sub custom_status_threshold { sub custom_status_output { my ($self, %options) = @_; - my $msg = $self->{result_values}->{metric} . ": " . $self->{result_values}->{status}; - return $msg; + return $self->{result_values}->{metric} . ": " . $self->{result_values}->{status}; } sub custom_status_calc { my ($self, %options) = @_; - + $self->{result_values}->{status} = $map_status{$options{new_datas}->{$self->{instance} . '_' . $options{extra_options}->{metric}}}; $self->{result_values}->{metric} = $options{extra_options}->{metric}; return 0; @@ -88,15 +87,16 @@ sub set_counters { ]; foreach my $metric ('StatusCheckFailed_Instance', 'StatusCheckFailed_System') { - my $entry = { label => lc($metric), threshold => 0, set => { - key_values => [ { name => $metric }, { name => 'display' } ], - closure_custom_calc => $self->can('custom_status_calc'), - closure_custom_calc_extra_options => { metric => $metric }, - closure_custom_output => $self->can('custom_status_output'), - closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => $self->can('custom_status_threshold'), - } - }; + my $entry = { + label => lc($metric), threshold => 0, set => { + key_values => [ { name => $metric }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_calc_extra_options => { metric => $metric }, + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold') + } + }; push @{$self->{maps_counters}->{metric}}, $entry; } } @@ -107,10 +107,10 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - "type:s" => { name => 'type' }, - "name:s@" => { name => 'name' }, - "warning-status:s" => { name => 'warning_status', default => '' }, - "critical-status:s" => { name => 'critical_status', default => '%{status} =~ /failed/i' }, + 'type:s' => { name => 'type' }, + 'name:s@' => { name => 'name' }, + 'warning-status:s' => { name => 'warning_status', default => '' }, + 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /failed/i' } }); return $self; @@ -165,7 +165,7 @@ sub manage_selection { timeframe => $self->{aws_timeframe}, period => $self->{aws_period}, ); - + foreach my $metric (keys %{$metric_results{$instance}}) { next if (!defined($metric_results{$instance}->{$metric}->{average})); diff --git a/cloud/microsoft/office365/exchange/mode/mailboxusage.pm b/cloud/microsoft/office365/exchange/mode/mailboxusage.pm index 20a5de925..4b228e539 100644 --- a/cloud/microsoft/office365/exchange/mode/mailboxusage.pm +++ b/cloud/microsoft/office365/exchange/mode/mailboxusage.pm @@ -24,6 +24,7 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); use Time::Local; 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->{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', - value => $self->{result_values}->{active}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), - unit => 'mailboxes', min => 0, max => $self->{result_values}->{total}); + $self->{output}->perfdata_add( + label => 'active_mailboxes', nlabel => 'exchange.mailboxes.active.count', + value => $self->{result_values}->{active}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options), + 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 { @@ -52,9 +55,13 @@ sub custom_active_threshold { if ($self->{instance_mode}->{option_results}->{units} eq '%') { $threshold_value = $self->{result_values}->{prct_active}; } - my $exit = $self->{perfdata}->threshold_check(value => $threshold_value, - threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, - { label => 'warning-' . $self->{label}, exit_litteral => 'warning' } ]); + my $exit = $self->{perfdata}->threshold_check( + value => $threshold_value, + threshold => [ + { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, + { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } + ] + ); return $exit; } @@ -62,12 +69,13 @@ sub custom_active_threshold { sub custom_active_output { my ($self, %options) = @_; - my $msg = sprintf("Active mailboxes on %s : %d/%d (%.2f%%)", - $self->{result_values}->{report_date}, - $self->{result_values}->{active}, - $self->{result_values}->{total}, - $self->{result_values}->{prct_active}); - return $msg; + return sprintf( + 'Active mailboxes on %s : %d/%d (%.2f%%)', + $self->{result_values}->{report_date}, + $self->{result_values}->{active}, + $self->{result_values}->{total}, + $self->{result_values}->{prct_active} + ); } sub custom_active_calc { @@ -87,34 +95,13 @@ sub custom_usage_perfdata { my $extra_label = ''; $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', - 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; + $self->{output}->perfdata_add( + label => 'used' . $extra_label, + nlabel => $self->{result_values}->{display} . '#exchange.mailboxes.usage.bytes', + unit => 'B', + value => $self->{result_values}->{used}, + min => 0 + ); } 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_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", - $used_value . " " . $used_unit, - $issue_warning_quota_value . " " . $issue_warning_quota_unit, - $prohibit_send_quota_value . " " . $prohibit_send_quota_unit, - $prohibit_send_receive_quota_value . " " . $prohibit_send_receive_quota_unit); - return $msg; + return sprintf( + "Used: %s Issue Warning Quota: %s Prohibit Send Quota: %s Prohibit Send/Receive Quota: %s", + $used_value . " " . $used_unit, + $issue_warning_quota_value . " " . $issue_warning_quota_unit, + $prohibit_send_quota_value . " " . $prohibit_send_quota_unit, + $prohibit_send_receive_quota_value . " " . $prohibit_send_receive_quota_unit + ); } sub custom_status_calc { @@ -191,31 +179,39 @@ sub set_counters { output_template => 'Usage (inactive mailboxes): %s %s', output_change_bytes => 1, perfdatas => [ - { label => 'total_usage_inactive', value => 'storage_used_inactive', template => '%d', - min => 0, unit => 'B' }, - ], + { label => 'total_usage_inactive', template => '%d', min => 0, unit => 'B' } + ] } - }, + } ]; + $self->{maps_counters}->{mailboxes} = [ - { label => 'usage', set => { - key_values => [ { name => 'storage_used' }, { name => 'issue_warning_quota' }, - { name => 'prohibit_send_quota' }, { name => 'prohibit_send_receive_quota' }, { name => 'name' } ], + { + label => 'status', + 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_output => $self->can('custom_status_output'), 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 => { key_values => [ { name => 'items' }, { name => 'name' } ], output_template => 'Items: %d', perfdatas => [ - { label => 'items', value => 'items', template => '%d', - min => 0, label_extra_instance => 1, instance_use => 'name' }, - ], + { label => 'items', template => '%d', + min => 0, label_extra_instance => 1, instance_use => 'name' } + ] } - }, + } ]; } @@ -225,23 +221,14 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - "filter-mailbox:s" => { name => 'filter_mailbox' }, - "warning-status:s" => { name => 'warning_status', default => '%{used} > %{issue_warning_quota}' }, - "critical-status:s" => { name => 'critical_status', default => '%{used} > %{prohibit_send_quota}' }, - "units:s" => { name => 'units', default => '%' }, - "filter-counters:s" => { name => 'filter_counters', default => 'active|total' }, + 'filter-mailbox:s' => { name => 'filter_mailbox' }, + 'units:s' => { name => 'units', default => '%' }, + 'filter-counters:s' => { name => 'filter_counters', default => 'active|total' } }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); - - $self->change_macros(macros => ['warning_status', 'critical_status']); -} - sub manage_selection { my ($self, %options) = @_; diff --git a/network/alcatel/isam/snmp/mode/hubsapusage.pm b/network/alcatel/isam/snmp/mode/hubsapusage.pm index 6a2226bf7..6d0fc62ec 100644 --- a/network/alcatel/isam/snmp/mode/hubsapusage.pm +++ b/network/alcatel/isam/snmp/mode/hubsapusage.pm @@ -26,20 +26,19 @@ use strict; use warnings; use Digest::MD5 qw(md5_hex); 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 { my ($self, %options) = @_; - + $self->{maps_counters_type} = [ { 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} = [ - { 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' } ], - 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_status_threshold') @@ -60,7 +59,7 @@ sub set_counters { closure_custom_perfdata => $self->can('custom_sap_perfdata'), closure_custom_threshold_check => $self->can('custom_sap_threshold') } - }, + } ]; $self->{maps_counters}->{global} = [ @@ -87,7 +86,7 @@ sub set_counters { sub custom_total_traffic_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); @@ -96,7 +95,7 @@ sub custom_total_traffic_perfdata { $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 => 'total_traffic_' . $self->{result_values}->{label}, unit => 'b/s', nlabel => $self->{nlabel}, @@ -109,7 +108,7 @@ sub custom_total_traffic_perfdata { sub custom_total_traffic_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' } ]); @@ -121,18 +120,19 @@ sub custom_total_traffic_threshold { sub custom_total_traffic_output { my ($self, %options) = @_; - + my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_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("Total 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; + + return sprintf( + "Total 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_total_traffic_calc { @@ -165,49 +165,19 @@ sub custom_total_traffic_calc { 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}->{admin} eq 'up') { - $self->{instance_mode}->{last_status} = 1; - } - }; - 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} = 0; + if ($self->{result_values}->{admin} eq 'up') { + $self->{instance_mode}->{last_status} = 1; } - return $status; } sub custom_status_output { my ($self, %options) = @_; - my $msg = 'Status : ' . $self->{result_values}->{status} . ' (admin: ' . $self->{result_values}->{admin} . ')'; - return $msg; -} - -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; + return 'Status : ' . $self->{result_values}->{status} . ' (admin: ' . $self->{result_values}->{admin} . ')'; } sub custom_sap_perfdata { @@ -221,7 +191,7 @@ sub custom_sap_perfdata { $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', nlabel => $self->{nlabel}, @@ -235,7 +205,7 @@ sub custom_sap_perfdata { sub custom_sap_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' } ]); @@ -247,23 +217,24 @@ sub custom_sap_threshold { sub custom_sap_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; + 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_sap_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'}; @@ -279,7 +250,7 @@ sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); bless $self, $class; - + $options{options}->add_options(arguments => { 'reload-cache-time:s' => { name => 'reload_cache_time', default => 300 }, 'display-name:s' => { name => 'display_name', default => '%{SvcDescription}.%{IfName}.%{SapEncapName}' }, @@ -288,11 +259,9 @@ sub new { 'speed-out:s' => { name => 'speed_out' }, 'speed-total-in:s' => { name => 'speed_total_in' }, 'speed-total-out:s' => { name => 'speed_total_out' }, - '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' }, + 'units-traffic:s' => { name => 'units_traffic', default => '%' } }); - + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); return $self; } @@ -300,8 +269,7 @@ sub new { sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); - - $self->change_macros(macros => ['warning_status', 'critical_status']); + $self->{statefile_cache}->check_options(%options); } @@ -313,7 +281,7 @@ sub prefix_sap_output { sub get_display_name { my ($self, %options) = @_; - + my $display_name = $self->{option_results}->{display_name}; $display_name =~ s/%\{(.*?)\}/$options{$1}/ge; return $display_name; @@ -338,14 +306,16 @@ my $oid_ifName = '.1.3.6.1.2.1.31.1.1.1.1'; sub reload_cache { my ($self, %options) = @_; - + 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_svcDescription }, - { oid => $oid_ifName }, + { oid => $oid_ifName } ], - nothing_quit => 1); + nothing_quit => 1 + ); $datas->{snmp_result} = $snmp_result; if (scalar(keys %{$datas->{snmp_result}->{$oid_sapDescription}}) <= 0) { @@ -358,12 +328,12 @@ sub reload_cache { sub manage_selection { my ($self, %options) = @_; - + if ($options{snmp}->is_snmpv1()) { $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3."); $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 $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp'); if ($has_cache_file == 0 || !defined($timestamp_cache) || @@ -376,7 +346,6 @@ sub manage_selection { $self->{global} = {}; $self->{sap} = {}; - foreach my $oid (keys %{$snmp_result->{$oid_sapDescription}}) { next if ($oid !~ /^$oid_sapDescription\.(.*?)\.(.*?)\.(.*?)$/); # $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; 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; - + my $name = $self->get_display_name( SapDescription => $SapDescription, SvcDescription => $SvcDescription, @@ -399,24 +368,26 @@ sub manage_selection { IfName => $IfName, SvcId => $SvcId, SapPortId => $SapPortId, - SapEncapValue => $SapEncapValue); + SapEncapValue => $SapEncapValue + ); if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $name !~ /$self->{option_results}->{filter_name}/) { $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); next; } - + $self->{sap}->{$instance} = { display => $name }; } $options{snmp}->load(oids => [$mapping->{fadSapStatsIngressOctets}->{oid}, $mapping->{fadSapStatsEgressOctets}->{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); - + foreach (keys %{$self->{sap}}) { my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_); $self->{sap}->{$_}->{in} = $result->{fadSapStatsIngressOctets} * 8; @@ -433,7 +404,7 @@ sub manage_selection { $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_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all')); } diff --git a/network/mrv/optiswitch/snmp/mode/interfaces.pm b/network/mrv/optiswitch/snmp/mode/interfaces.pm index e350f8bc1..b26ffccf9 100644 --- a/network/mrv/optiswitch/snmp/mode/interfaces.pm +++ b/network/mrv/optiswitch/snmp/mode/interfaces.pm @@ -192,19 +192,19 @@ sub default_global_admin_up_rule { sub default_global_admin_down_rule { my ($self, %options) = @_; - + return '%{admstatus} ne "enable"'; } sub default_global_oper_up_rule { my ($self, %options) = @_; - + return '%{opstatus} eq "enabled"'; } sub default_global_oper_down_rule { my ($self, %options) = @_; - + 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, global_admin_down => 0, global_oper_up => 0, global_oper_down => 0}; foreach (@{$self->{array_interface_selected}}) { - my $linkstatus = $self->{oid_linkstatus_mapping}->{$self->{results}->{$self->{oid_linkstatus} . '.' . $_}}; - my $opstatus = $self->{oid_opstatus_mapping}->{$self->{results}->{$self->{oid_opstatus} . '.' . $_}}; - my $admstatus = $self->{oid_adminstatus_mapping}->{$self->{results}->{$self->{oid_adminstatus} . '.' . $_}}; + my $values = { + linkstatus => $self->{oid_linkstatus_mapping}->{$self->{results}->{$self->{oid_linkstatus} . '.' . $_}}, + 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')) { eval { local $SIG{__WARN__} = sub { return ; }; local $SIG{__DIE__} = sub { return ; }; - + 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}->{$_}++; } }; @@ -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}}->{mode_cast} = 64; } - + foreach (('iucast', 'imcast', 'ibcast', 'oucast', 'omcast', 'obcast')) { $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_out_packets} = $self->{int}->{$options{instance}}->{oucast} + $self->{int}->{$options{instance}}->{omcast} + $self->{int}->{$options{instance}}->{obcast}; } diff --git a/network/oracle/infiniband/snmp/mode/infinibandusage.pm b/network/oracle/infiniband/snmp/mode/infinibandusage.pm index aff05e225..526979282 100644 --- a/network/oracle/infiniband/snmp/mode/infinibandusage.pm +++ b/network/oracle/infiniband/snmp/mode/infinibandusage.pm @@ -25,6 +25,93 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; 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 { my ($self, %options) = @_; @@ -35,9 +122,8 @@ sub set_counters { ]; $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' } ], - 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_status_threshold') @@ -62,9 +148,8 @@ sub set_counters { ]; $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' } ], - 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_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 { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1); bless $self, $class; $options{options}->add_options(arguments => { - 'filter-ib-name:s' => { name => 'filter_ib_name' }, - 'filter-ibgw-name:s' => { name => 'filter_ibgw_name' }, - 'warning-ib-status:s' => { name => 'warning_ib_status', default => '' }, - 'critical-ib-status:s' => { name => 'critical_ib_status', default => '%{status} !~ /active/i' }, - 'warning-ibgw-status:s' => { name => 'warning_ibgw_status', 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 => '%' } + 'filter-ib-name:s' => { name => 'filter_ib_name' }, + 'filter-ibgw-name:s' => { name => 'filter_ibgw_name' }, + 'speed-in:s' => { name => 'speed_in' }, + 'speed-out:s' => { name => 'speed_out' }, + 'units-traffic:s' => { name => 'units_traffic', default => '%' } }); 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 { my ($self, %options) = @_; diff --git a/notification/slack/mode/alert.pm b/notification/slack/mode/alert.pm index ba2aa2421..1f289ae6f 100644 --- a/notification/slack/mode/alert.pm +++ b/notification/slack/mode/alert.pm @@ -67,7 +67,7 @@ sub new { "ntlm" => { name => 'ntlm' }, "username:s" => { name => 'username' }, "password:s" => { name => 'password' }, - "timeout:s" => { name => 'timeout' }, + "timeout:s" => { name => 'timeout' } }); $self->{http} = centreon::plugins::http->new(%options); $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}->option_exit(); } - + foreach (('graph_url', 'link_url')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{option_results}->{$1}/g; - eval "\$self->{option_results}->{\$_} = \"$self->{option_results}->{$_}\""; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/$self->{option_results}->{$1}/eg; } } diff --git a/notification/telegram/mode/alert.pm b/notification/telegram/mode/alert.pm index 34594e298..b1a7586c4 100644 --- a/notification/telegram/mode/alert.pm +++ b/notification/telegram/mode/alert.pm @@ -61,7 +61,7 @@ sub new { "link-url:s" => { name => 'link_url' }, "centreon-url:s" => { name => 'centreon_url' }, "centreon-token:s" => { name => 'centreon_token' }, - "timeout:s" => { name => 'timeout' }, + "timeout:s" => { name => 'timeout' } }); $self->{http} = centreon::plugins::http->new(%options); @@ -87,8 +87,7 @@ sub check_options { foreach (('graph_url', 'link_url')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{option_results}->{$1}/g; - eval "\$self->{option_results}->{\$_} = \"$self->{option_results}->{$_}\""; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/$self->{option_results}->{$1}/eg; } } @@ -161,9 +160,11 @@ sub format_payload { my $json = JSON::XS->new->utf8; - my $payload = { chat_id =>$self->{option_results}->{chat_id}, - parse_mode => 'HTML', - text => $self->{message} }; + my $payload = { + chat_id =>$self->{option_results}->{chat_id}, + parse_mode => 'HTML', + text => $self->{message} + }; eval { $self->{payload_str} = $json->encode($payload); }; @@ -184,15 +185,16 @@ sub run { $self->format_payload(); my $url_path = '/bot' . $self->{option_results}->{bot_token} . $self->{option_results}->{url_path}; - my $response = $self->{http}->request(url_path => $url_path, - method => 'POST', query_form_post => $self->{payload_str}); + my $response = $self->{http}->request( + url_path => $url_path, + method => 'POST', query_form_post => $self->{payload_str} + ); my $decoded; eval { $decoded = JSON::XS->new->utf8->decode($response); }; if ($@) { - $self->{output}->output_add(long_msg => $response, debug => 1); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->option_exit(); } diff --git a/snmp_standard/mode/interfaces.pm b/snmp_standard/mode/interfaces.pm index 8538d113d..4c2250eb5 100644 --- a/snmp_standard/mode/interfaces.pm +++ b/snmp_standard/mode/interfaces.pm @@ -38,12 +38,12 @@ sub custom_threshold_output { 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}") { + $self->eval(value => $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}") { + $self->eval(value => $self->{instance_mode}->{option_results}->{warning_status})) { $status = 'warning'; } @@ -1324,27 +1324,29 @@ sub manage_selection { sub add_result_global { my ($self, %options) = @_; - + foreach (('global_admin_up_rule', 'global_admin_down_rule', 'global_oper_up_rule', 'global_oper_down_rule')) { if (defined($self->{option_results}->{$_})) { - $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$$1/g; + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$values->{$1}/g; } } - + $self->{global} = { total_port => 0, global_admin_up => 0, global_admin_down => 0, global_oper_up => 0, global_oper_down => 0 }; foreach (@{$self->{array_interface_selected}}) { - my $opstatus = $self->{oid_opstatus_mapping}->{$self->{results}->{$self->{oid_opstatus} . '.' . $_}}; - my $admstatus = $self->{oid_adminstatus_mapping}->{$self->{results}->{$self->{oid_adminstatus} . '.' . $_}}; + my $values = { + 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')) { eval { local $SIG{__WARN__} = sub { return ; }; local $SIG{__DIE__} = sub { return ; }; - + 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}->{$_}++; } }; diff --git a/snmp_standard/mode/stringvalue.pm b/snmp_standard/mode/stringvalue.pm index 40b2825ca..43f173cfd 100644 --- a/snmp_standard/mode/stringvalue.pm +++ b/snmp_standard/mode/stringvalue.pm @@ -211,7 +211,7 @@ sub store_ok { sub checking_exist { my ($self, %options) = @_; - + foreach my $severity (('critical', 'warning', 'unknown')) { foreach my $absent (@{$self->{option_results}->{$severity . '_absent'}}) { my $match = 0; @@ -226,7 +226,7 @@ sub checking_exist { $self->{instances}->{$severity}->{$absent} = $absent; } } - + foreach my $present (@{$self->{option_results}->{$severity . '_present'}}) { my $match = 0; foreach (keys %{$self->{instances}}) { @@ -243,15 +243,15 @@ sub change_macros { my $value = $self->{option_results}->{'format_' . $options{severity}}; while ($value =~ /%\{(.*?)\}/g) { - $value =~ s/%\{($1)\}/\$self->{macros}->{$1}/g; + $value =~ s/%\{($1)\}/$self->{macros}->{$1}/eg; } - + return $value; } sub build_format_details { my ($self, %options) = @_; - + foreach my $severity (('ok', 'critical', 'warning', 'unknown')) { $self->{macros}->{'details_' . $severity} = ''; my $append = ''; @@ -261,7 +261,7 @@ sub build_format_details { $details =~ s/%\{filter_rows\}/$self->{macros}->{filter_rows}/g; $details =~ s/%\{instance\}/$instance/g; $details =~ s/%\{value\}/$self->{instances}->{$severity}->{$instance}/g; - + $self->{macros}->{'details_' . $severity} .= $append . $details; $append = $self->{option_results}->{'format_details_separator_' . $severity}; } @@ -270,15 +270,16 @@ sub build_format_details { sub display_severity { my ($self, %options) = @_; - + if (!(defined($options{force}) && $options{force} == 1) && scalar(keys %{$self->{instances}->{$options{severity}}}) == 0) { return 0; } my $display = $self->change_macros(severity => $options{severity}); - eval "\$display = \"$display\""; - $self->{output}->output_add(severity => $options{severity}, - short_msg => $display); + $self->{output}->output_add( + severity => $options{severity}, + short_msg => $display + ); } sub display_result { @@ -296,7 +297,7 @@ sub run { $self->{snmp} = $options{snmp}; $self->get_snmp_values(); - + foreach (keys %{$self->{instances}}) { $self->checking_regexp(severity => 'critical', instance => $_, value => $self->{instances}->{$_}) || $self->checking_regexp(severity => 'warning', instance => $_, value => $self->{instances}->{$_}) ||