diff --git a/network/checkpoint/mode/hastate.pm b/network/checkpoint/mode/hastate.pm index 61fa3f2cd..a2f3a8bec 100644 --- a/network/checkpoint/mode/hastate.pm +++ b/network/checkpoint/mode/hastate.pm @@ -20,70 +20,131 @@ package network::checkpoint::mode::hastate; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; +my $instance_mode; + +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($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' && + eval "$instance_mode->{option_results}->{critical_status}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' && + eval "$instance_mode->{option_results}->{warning_status}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + my $msg = "HA State: '" . $self->{result_values}->{hastate} . "' "; + $msg .= "Role: '" . $self->{result_values}->{role} . "' "; + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + $self->{result_values}->{hastate} = $options{new_datas}->{$self->{instance} . '_hastate'}; + $self->{result_values}->{role} = $options{new_datas}->{$self->{instance} . '_role'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'high_availability', type => 0 }, + ]; + $self->{maps_counters}->{high_availability} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'hastate' }, { name => 'role' } ], + 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'), + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '%{hastate} !~ /(UP|working)/' }, + "no-ha-status:s" => { name => 'no_ha_status', default => 'UNKNOWN' }, + }); + + return $self; +} + +sub change_macros { + my ($self, %options) = @_; + + foreach ('warning_status', 'critical_status') { + if (defined($self->{option_results}->{$_})) { + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; + } + } +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; + $self->change_macros(); +} + my %map_status = ( 0 => 'Member is UP and working', 1 => 'Problem preventing role switching', 2 => 'HA is down', ); -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); -} - -sub run { +sub manage_selection { my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - + my $oid_haInstalled = '.1.3.6.1.4.1.2620.1.5.2.0'; my $oid_haState = '.1.3.6.1.4.1.2620.1.5.6.0'; my $oid_haStatCode = '.1.3.6.1.4.1.2620.1.5.101.0'; my $oid_haStarted = '.1.3.6.1.4.1.2620.1.5.5.0'; - - my $result = $self->{snmp}->get_leef(oids => [$oid_haInstalled, $oid_haState, $oid_haStatCode, $oid_haStarted], nothing_quit => 1); - + + $self->{high_availability} = {}; + + my $result = $options{snmp}->get_leef(oids => [$oid_haInstalled, $oid_haState, $oid_haStatCode, $oid_haStarted], + nothing_quit => 1); + if ($result->{$oid_haInstalled} < 1 or $result->{$oid_haStarted} eq "no") { - $self->{output}->output_add(severity => 'UNKNOWN', + $self->{output}->output_add(severity => $self->{option_results}->{no_ha_state}, short_msg => sprintf("Looks like HA is not started, or not installed .."), - long_msg => sprintf("HA Installed : '%u' HA Started : '%s'", $result->{$oid_haInstalled}, $result->{$oid_haStarted}), + long_msg => sprintf("HA Installed : '%u' HA Started : '%s'", + $result->{$oid_haInstalled}, $result->{$oid_haStarted}), ); $self->{output}->display(); $self->{output}->exit(); } - - my $status = $result->{$oid_haStatCode}; - - if ($status < 1 ) { - $self->{output}->output_add(severity => 'OK', - short_msg => sprintf("'%s'. State : '%s'", $map_status{$status}, $result->{$oid_haState}), - ); - } elsif ($status == 1) { - $self->{output}->output_add(severity => 'WARNING', - short_msg => sprintf("'%s'. State : '%s'", $map_status{$status}, $result->{$oid_haState}), - ); - - } else { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => sprintf("'%s'. State : '%s'", $map_status{$status}, $result->{$oid_haState}), - ); - } - - $self->{output}->display(); - $self->{output}->exit(); + $self->{high_availability} = { hastate => $map_status{$result->{$oid_haStatCode}}, + role => $result->{$oid_haState} }; } 1; @@ -94,7 +155,16 @@ __END__ Check HA State of a Checkpoint node (chkpnt.mib). +=item B<--warning-status> + +Trigger warning on %{role} or %{hastate} values +e.g --warning-status '%{role} !~ /master/' will warn when failover occurs + +=item B<--critical-status> + +Trigger critical on %{role} or %{hastate} values +(default: '%{hastate} !~ /(UP|working)/') + =back =cut -