diff --git a/centreon-plugins/database/mysql/mode/replication.pm b/centreon-plugins/database/mysql/mode/replication.pm new file mode 100644 index 000000000..14c5f4166 --- /dev/null +++ b/centreon-plugins/database/mysql/mode/replication.pm @@ -0,0 +1,405 @@ +# +# Copyright 2020 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package database::mysql::mode::replication; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_connection_output { + my ($self, %options) = @_; + + return sprintf( + 'connection status: %s%s', + $self->{result_values}->{status}, + $self->{result_values}->{status} ne 'ok' ? ' [error message: ' . $self->{result_values}->{error_message} . ']' : '' + ); +} + +sub custom_thread_sql_output { + my ($self, %options) = @_; + + return sprintf( + 'thread sql running: %s%s', + $self->{result_values}->{running}, + $self->{result_values}->{running} ne 'yes' ? ' [last error message: ' . $self->{result_values}->{error_message} . ']' : '' + ); +} + +sub custom_thread_io_output { + my ($self, %options) = @_; + + return sprintf( + 'thread io running: %s%s', + $self->{result_values}->{running}, + $self->{result_values}->{running} ne 'yes' ? ' [last error message: ' . $self->{result_values}->{error_message} . ']' : '' + ); +} + +sub custom_replication_output { + my ($self, %options) = @_; + + return sprintf( + 'replication status: %s', + $self->{result_values}->{replication_status} + ); +} + +sub server_long_output { + my ($self, %options) = @_; + + return "checking database instance '" . $options{instance_value}->{display} . "'"; +} + +sub prefix_server_output { + my ($self, %options) = @_; + + return "database instance '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'servers', type => 3, cb_prefix_output => 'prefix_server_output', cb_long_output => 'server_long_output', indent_long_output => ' ', message_multiple => 'All database instances are ok', + group => [ + { name => 'connection', type => 0, skipped_code => { -10 => 1 } }, + { name => 'thread_sql', type => 0, skipped_code => { -10 => 1 } }, + { name => 'thread_io', type => 0, skipped_code => { -10 => 1 } }, + { name => 'position', type => 0, skipped_code => { -10 => 1 } } + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'slaves-running', nlabel => 'instance.slaves.running.count', set => { + key_values => [ { name => 'slaves_running' }, { name => 'total' } ], + output_template => 'number of slave instances running: %s', + perfdatas => [ + { template => '%s', min => 0, max => 'total' } + ] + } + } + ]; + + $self->{maps_counters}->{connection} = [ + { + label => 'connection-status', + type => 2, + critical_default => '%{status} ne "ok"', + set => { + key_values => [ { name => 'status' }, { name => 'error_message' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_connection_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{thread_sql} = [ + { + label => 'thread-sql-status', + type => 2, + set => { + key_values => [ { name => 'running' }, { name => 'error_message' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_thread_sql_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{thread_io} = [ + { + label => 'thread-io-status', + type => 2, + set => { + key_values => [ { name => 'running' }, { name => 'error_message' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_thread_io_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{position} = [ + { label => 'slave-latency', nlabel => 'instance.slave.latency.seconds', set => { + key_values => [ { name => 'latency' } ], + output_template => 'slave has %s seconds latency behind master', + perfdatas => [ + { template => '%d', unit => 's', label_extra_instance => 1 } + ] + } + }, + { + label => 'replication-status', + unknown_default => '%{replication_status} =~ /configurationIssue/i', + warning_default => '%{replication_status} =~ /inProgress/i', + critical_default => '%{replication_status} =~ /connectIssueToMaster/i', + type => 2, + set => { + key_values => [ { name => 'replication_status' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_replication_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, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + }); + + return $self; +} + +sub sql_query_show_slave_status { + my ($self, %options) = @_; + + if ($options{sql}->is_mariadb() && $options{sql}->is_version_minimum(version => '10.2.x')) { + $options{sql}->query(query => q{ + SHOW ALL SLAVES STATUS + }); + } else { + $options{sql}->query(query => q{ + SHOW SLAVE STATUS + }); + } +} + +sub check_connection { + my ($self, %options) = @_; + + my ($exit, $msg_error) = $options{sql}->connect(dontquit => 1); + if ($exit == -1) { + $self->{servers}->{ $options{name} }->{connection}->{status} = 'error'; + $self->{servers}->{ $options{name} }->{connection}->{error_message} = $msg_error; + } +} + +sub check_slave { + my ($self, %options) = @_; + + return if ($self->{servers}->{ $options{name} }->{connection}->{status} ne 'ok'); + + $self->sql_query_show_slave_status(sql => $options{sql}); + + my $result = $options{sql}->fetchrow_hashref(); + my $slave_running = 0; + if (defined($result->{Slave_IO_Running})) { + my $running = 'no'; + + if ($result->{Slave_IO_Running} =~ /^yes$/i) { + $slave_running = 1; + $running = 'yes'; + } + $self->{servers}->{ $options{name} }->{thread_io} = { + display => $options{name}, + running => $running, + error_message => defined($result->{Last_Error}) ? $result->{Last_Error} : '' + }; + } + if (defined($result->{Slave_SQL_Running})) { + my $running = 'no'; + + if ($result->{Slave_SQL_Running} =~ /^yes$/i) { + $slave_running = 1; + $running = 'yes'; + } + $self->{servers}->{ $options{name} }->{thread_sql} = { + display => $options{name}, + running => $running, + error_message => defined($result->{Last_Error}) ? $result->{Last_Error} : '' + }; + } + + $self->{servers}->{ $options{name} }->{is_slave} = $slave_running; + $self->{global}->{slaves_running} += $slave_running; +} + +sub check_master_slave_position { + my ($self, %options) = @_; + + return if ($self->{servers}->{ $options{name_master} }->{connection}->{status} ne 'ok'); + return if ($self->{servers}->{ $options{name_slave} }->{connection}->{status} ne 'ok'); + return if ($self->{servers}->{ $options{name_slave} }->{is_slave} == 0); + + $options{sql_master}->query(query => q{ + SHOW MASTER STATUS + }); + my $master_result = $options{sql_master}->fetchrow_hashref(); + + $self->sql_query_show_slave_status(sql => $options{sql}); + my $slave_result = $options{sql_slave}->fetchrow_hashref(); + + $self->{servers}->{ $options{name_slave} }->{position} = { + display => $options{name_slave}, + latency => $slave_result->{Seconds_Behind_Master}, + replication_status => 'ok' + }; + + $options{sql_slave}->query(query => q{ + SHOW FULL PROCESSLIST + }); + + my ($slave_sql_thread_ko, $slave_sql_thread_warning, $slave_sql_thread_ok) = (1, 1, 1); + while ((my $row = $options{sql_slave}->fetchrow_hashref())) { + my $state = $row->{State}; + $slave_sql_thread_ko = 0 if (defined($state) && $state =~ /^(Waiting to reconnect after a failed binlog dump request|Connecting to master|Reconnecting after a failed binlog dump request|Waiting to reconnect after a failed master event read|Waiting for the slave SQL thread to free enough relay log space)$/i); + $slave_sql_thread_warning = 0 if (defined($state) && $state =~ /^Waiting for the next event in relay log|Reading event from the relay log$/i); + $slave_sql_thread_ok = 0 if (defined($state) && $state =~ /^Has read all relay log; waiting for the slave I\/O thread to update it$/i); + } + + if ($slave_sql_thread_ko == 0) { + $self->{servers}->{ $options{name_slave} }->{position}->{replication_status} = 'connectIssueToMaster'; + } elsif (($master_result->{File} ne $slave_result->{Master_Log_File} || + $master_result->{Position} != $slave_result->{Read_Master_Log_Pos}) && + ($slave_sql_thread_warning == 0 || $slave_sql_thread_ok == 0) + ) { + $self->{servers}->{ $options{name_slave} }->{position}->{replication_status} = 'inProgress'; + } else { + $master_result->{File} =~ /(\d+)$/; + my $master_bin_num = $1; + $slave_result->{Master_Log_File} =~ /(\d+)$/; + my $slave_bin_num = $1; + my $diff_binlog = abs($master_bin_num - $slave_bin_num); + + # surely of missconfiguration of the plugin + if ($diff_binlog > 1 && $slave_result->{Seconds_Behind_Master} < 10) { + $self->{servers}->{ $options{name_slave} }->{position}->{replication_status} = 'configurationIssue'; + } + } +} + +sub manage_selection { + my ($self, %options) = @_; + + if (ref($options{sql}) ne 'ARRAY') { + $self->{output}->add_option_msg(short_msg => "Need to use --multiple options."); + $self->{output}->option_exit(); + } + if (scalar(@{$options{sql}}) < 2) { + $self->{output}->add_option_msg(short_msg => "Need to specify two MySQL Server."); + $self->{output}->option_exit(); + } + + my ($sql_server1, $sql_server2) = @{$options{sql}}; + my ($server1_name, $server2_name) = ($sql_server1->get_id(), $sql_server2->get_id()); + + $self->{global} = { + total => 2, + slaves_running => 0 + }; + $self->{servers} = { + $server1_name => { + display => $server1_name, + connection => { + display => $server1_name, + status => 'ok', + error_message => '' + } + }, + $server2_name => { + display => $server2_name, + connection => { + display => $server2_name, + status => 'ok', + error_message => '' + } + } + }; + + $self->check_connection(name => $server1_name, sql => $sql_server1); + $self->check_connection(name => $server2_name, sql => $sql_server2); + + $self->check_slave(name => $server1_name, sql => $sql_server1); + $self->check_slave(name => $server2_name, sql => $sql_server2); + + $self->check_master_slave_position( + name_master => $server1_name, + name_slave => $server2_name, + sql_master => $sql_server1, + sql_slave => $sql_server2 + ); + $self->check_master_slave_position( + name_master => $server2_name, + name_slave => $server1_name, + sql_master => $sql_server2, + sql_slave => $sql_server1 + ); +} + +1; + +__END__ + +=head1 MODE + +Check MySQL replication (need to use --multiple). + +=over 8 + +=item B<--unknown-connection-status> + +Set unknown threshold for status. +Can used special variables like: %{status}, %{error_message}, %{display} + +=item B<--warning-connection-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{error_message}, %{display} + +=item B<--critical-connection-status> + +Set critical threshold for status (Default: '%{status} ne "ok"'). +Can used special variables like: %{status}, %{error_message}, %{display} + +=item B<--unknown-replication-status> + +Set unknown threshold for status (Default: '%{replication_status} =~ /configurationIssue/i'). +Can used special variables like: %{replication_status}, %{display} + +=item B<--warning-replication-status> + +Set warning threshold for status (Default: '%{replication_status} =~ /inProgress/i'). +Can used special variables like: %{replication_status}, %{display} + +=item B<--critical-replication-status> + +Set critical threshold for status (Default: '%{replication_status} =~ /connectIssueToMaster/i'). +Can used special variables like: %{replication_status}, %{display} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'slaves-running', 'slave-latency' (s). + +=back + +=cut diff --git a/centreon-plugins/database/mysql/mode/replicationmastermaster.pm b/centreon-plugins/database/mysql/mode/replicationmastermaster.pm deleted file mode 100644 index 33e85583c..000000000 --- a/centreon-plugins/database/mysql/mode/replicationmastermaster.pm +++ /dev/null @@ -1,270 +0,0 @@ -# -# Copyright 2020 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package database::mysql::mode::replicationmastermaster; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); - - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } -} - -sub check_replication { - my ($self, %options) = @_; - - my ($master, $slave) = ($options{master}, $options{slave}); - - my ($slave_status, $slave_status_error) = (0, ""); - my ($position_status, $position_status_error) = (0, ""); - - my ($total_srv, $last_error); - - my ($io_thread_status_srv, $sql_thread_status_srv); - if ($self->{$slave->get_id()}->{exit} != -1) { - $slave->query(query => q{ - SHOW SLAVE STATUS - }); - my $result = $slave->fetchrow_hashref(); - my $slave_io_running = $result->{Slave_IO_Running}; - my $slave_sql_running = $result->{Slave_SQL_Running}; - $last_error = $result->{Last_Error}; - - if (defined($slave_io_running) && $slave_io_running =~ /^yes$/i) { - $io_thread_status_srv = 0; - } else { - $io_thread_status_srv = 1; - } - if (defined($slave_sql_running) && $slave_sql_running =~ /^yes$/i) { - $sql_thread_status_srv = 0; - } else { - $sql_thread_status_srv = 1; - } - } else { - $io_thread_status_srv = 100; - $sql_thread_status_srv = 100; - } - - $total_srv = $io_thread_status_srv + $sql_thread_status_srv; - - # Check if a thread is down - if ($total_srv == 1) { - $slave_status = -1; - $slave_status_error = "A Replication thread is down on '" . $slave->get_id() . "'."; - if ($sql_thread_status_srv != 0) { - if (defined($last_error) && $last_error ne "") { - $slave_status = 1; - $slave_status_error .= " SQL Thread is stopped because of an error (error='" . $last_error . "')."; - } - } - } - - # Check if we need to SKIP - if ($io_thread_status_srv == 100) { - $slave_status = -1; - $slave_status_error .= " Skip check on '" . $slave->get_id() . "'."; - } - - if ($total_srv > 1) { - $slave_status = 1; - $slave_status_error .= " not a slave '" . $slave->get_id() . "' (maybe because we cannot check the server)."; - } - - #### - # Check Slave position - #### - if ($self->{$master->get_id()}->{exit} == -1) { - $position_status = -1; - $position_status_error = "Can't get master position on '" . $master->get_id() . "'."; - } else { - # Get Master Position - $master->query(query => q{ - SHOW MASTER STATUS - }); - my $result = $master->fetchrow_hashref(); - my $master_file = $result->{File}; - my $master_position = $result->{Position}; - - $slave->query(query => q{ - SHOW SLAVE STATUS - }); - my $result2 = $slave->fetchrow_hashref(); - my $slave_file = $result2->{Master_Log_File}; # 'Master_Log_File' - my $slave_position = $result2->{Read_Master_Log_Pos}; # 'Read_Master_Log_Pos' - my $num_sec_lates = $result2->{Seconds_Behind_Master}; - - my $exit_code_sec = $self->{perfdata}->threshold_check(value => $num_sec_lates, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - if (!$self->{output}->is_status(value => $exit_code_sec, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit_code_sec, - short_msg => sprintf("Slave '%s' has %d seconds latency behind master", $slave->get_id(), $num_sec_lates)); - } - $self->{output}->perfdata_add(label => 'slave_latency_' . $slave->get_id(), unit => 's', - value => $num_sec_lates, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); - - my $slave_sql_thread_ko = 1; - my $slave_sql_thread_warning = 1; - my $slave_sql_thread_ok = 1; - - $slave->query(query => q{ - SHOW FULL PROCESSLIST - }); - while ((my $row = $slave->fetchrow_hashref())) { - my $state = $row->{State}; - $slave_sql_thread_ko = 0 if (defined($state) && $state =~ /^(Waiting to reconnect after a failed binlog dump request|Connecting to master|Reconnecting after a failed binlog dump request|Waiting to reconnect after a failed master event read|Waiting for the slave SQL thread to free enough relay log space)$/i); - $slave_sql_thread_warning = 0 if (defined($state) && $state =~ /^Waiting for the next event in relay log|Reading event from the relay log$/i); - $slave_sql_thread_ok = 0 if (defined($state) && $state =~ /^Has read all relay log; waiting for the slave I\/O thread to update it$/i); - } - - if ($slave_sql_thread_ko == 0) { - $position_status = 1; - $position_status_error .= " Slave replication has connection issue with the master."; - } elsif (($master_file ne $slave_file || $master_position != $slave_position) && $slave_sql_thread_warning == 0) { - $position_status = -1; - $position_status_error .= " Slave replication is late but it's progressing.."; - } elsif (($master_file ne $slave_file || $master_position != $slave_position) && $slave_sql_thread_ok == 0) { - $position_status = -1; - $position_status_error .= " Slave replication is late but it's progressing.."; - } - } - - $self->replication_add($slave_status, "Slave Thread Status '" . $slave->get_id() . "'", $slave_status_error); - $self->replication_add($position_status, "Position Status '" . $slave->get_id() . "'", $position_status_error); -} - -sub run { - my ($self, %options) = @_; - # $options{sql} = sqlmode object - - if (ref($options{sql}) ne 'ARRAY') { - $self->{output}->add_option_msg(short_msg => "Need to use --multiple options."); - $self->{output}->option_exit(); - } - if (scalar(@{$options{sql}}) < 2) { - $self->{output}->add_option_msg(short_msg => "Need to specify two MySQL Server."); - $self->{output}->option_exit(); - } - - my ($msg_error1, $msg_error2); - my ($sql_one, $sql_two) = @{$options{sql}}; - - ($self->{$sql_one->get_id()}->{exit}, $msg_error1) = $sql_one->connect(dontquit => 1); - ($self->{$sql_two->get_id()}->{exit}, $msg_error2) = $sql_two->connect(dontquit => 1); - $self->{output}->output_add(severity => 'OK', - short_msg => "No problems. Replication is ok."); - if ($self->{$sql_one->get_id()}->{exit} == -1) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "Connection Status '" . $sql_one->get_id() . "': " . $msg_error1); - } else { - $self->{output}->output_add(long_msg => "Connection Status '" . $sql_one->get_id() . "' [OK]"); - } - if ($self->{$sql_two->get_id()}->{exit} == -1) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "Connection Status '" . $sql_two->get_id() . "': " . $msg_error2); - } else { - $self->{output}->output_add(long_msg => "Connection Status '" . $sql_two->get_id() . "' [OK]"); - } - - $self->check_replication(master => $sql_one, slave => $sql_two); - $self->check_replication(master => $sql_two, slave => $sql_one); - - $self->{output}->display(); - $self->{output}->exit(); -} - -sub replication_add { - my ($self, $lstate, $str_display, $lerr) = @_; - my $status; - my $status_msg; - - if ($lstate == 0) { - $status = 'OK'; - } elsif ($lstate == -1) { - $status = 'WARNING'; - } elsif ($lstate == -2) { - $status = 'CRITICAL'; - $status_msg = 'SKIP'; - } else { - $status = 'CRITICAL'; - } - - my $output; - if (defined($lerr) && $lerr ne "") { - $output = $str_display . " [" . (defined($status_msg) ? $status_msg : $status) . "] [" . $lerr . "]"; - } else { - $output = $str_display . " [" . (defined($status_msg) ? $status_msg : $status) . "]"; - } - if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => $output); - } - - $self->{output}->output_add(long_msg => $output); -} - -1; - -__END__ - -=head1 MODE - -Check MySQL replication master/master (need to use --multiple). - -=over 8 - -=item B<--warning> - -Threshold warning in seconds (slave latency). - -=item B<--critical> - -Threshold critical in seconds (slave latency). - -=back - -=cut diff --git a/centreon-plugins/database/mysql/mode/replicationmasterslave.pm b/centreon-plugins/database/mysql/mode/replicationmasterslave.pm deleted file mode 100644 index 63d54c17d..000000000 --- a/centreon-plugins/database/mysql/mode/replicationmasterslave.pm +++ /dev/null @@ -1,342 +0,0 @@ -# -# Copyright 2020 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package database::mysql::mode::replicationmasterslave; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $options{options}->add_options(arguments => - { - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - }); - - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } -} - -sub run { - my ($self, %options) = @_; - - if (ref($options{sql}) ne 'ARRAY') { - $self->{output}->add_option_msg(short_msg => "Need to use --multiple options."); - $self->{output}->option_exit(); - } - if (scalar(@{$options{sql}}) < 2) { - $self->{output}->add_option_msg(short_msg => "Need to specify two MySQL Server."); - $self->{output}->option_exit(); - } - - my ($sql_one, $sql_two) = @{$options{sql}}; - my ($slave_status, $slave_status_error) = (0, ""); - my ($position_status, $position_status_error) = (0, ""); - my ($connection_status_name_srv1, $connection_status_name_srv2) = ($sql_one->get_id(), $sql_two->get_id()); - my ($master_save, $slave_save); - - my ($exit1, $msg_error1) = $sql_one->connect(dontquit => 1); - my ($exit2, $msg_error2) = $sql_two->connect(dontquit => 1); - $self->{output}->output_add(severity => 'OK', - short_msg => "No problems. Replication is ok."); - if ($exit1 == -1) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "Connection Status '" . $sql_one->get_id() . "': " . $msg_error1); - } else { - $self->{output}->output_add(long_msg => "Connection Status '" . $sql_one->get_id() . "' [OK]"); - } - if ($exit2 == -1) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "Connection Status '" . $sql_two->get_id() . "': " . $msg_error2); - } else { - $self->{output}->output_add(long_msg => "Connection Status '" . $sql_two->get_id() . "' [OK]"); - } - - ##### - # Find SLAVE - ##### - my ($total_srv1, $total_srv2); - my ($last_error1, $last_error2); - - my ($io_thread_status_srv1, $sql_thread_status_srv1); - if ($exit1 != -1) { - $sql_one->query(query => q{ - SHOW SLAVE STATUS - }); - my $result = $sql_one->fetchrow_hashref(); - my $slave_io_running = $result->{Slave_IO_Running}; - my $slave_sql_running = $result->{Slave_SQL_Running}; - $last_error1 = $result->{Last_Error}; - - if (defined($slave_io_running) && $slave_io_running =~ /^yes$/i) { - $io_thread_status_srv1 = 0; - } else { - $io_thread_status_srv1 = 1; - } - if (defined($slave_sql_running) && $slave_sql_running =~ /^yes$/i) { - $sql_thread_status_srv1 = 0; - } else { - $sql_thread_status_srv1 = 1; - } - } else { - $io_thread_status_srv1 = 100; - $sql_thread_status_srv1 = 100; - } - - my ($io_thread_status_srv2, $sql_thread_status_srv2); - if ($exit2 != -1) { - $sql_two->query(query => q{ - SHOW SLAVE STATUS - }); - my $result = $sql_two->fetchrow_hashref(); - my $slave_io_running = $result->{Slave_IO_Running}; - my $slave_sql_running = $result->{Slave_SQL_Running}; - $last_error2 = $result->{Last_Error}; - - if (defined($slave_io_running) && $slave_io_running =~ /^yes$/i) { - $io_thread_status_srv2 = 0; - } else { - $io_thread_status_srv2 = 1; - } - if (defined($slave_sql_running) && $slave_sql_running =~ /^yes$/i) { - $sql_thread_status_srv2 = 0; - } else { - $sql_thread_status_srv2 = 1; - } - } else { - $io_thread_status_srv2 = 100; - $sql_thread_status_srv2 = 100; - } - - $total_srv1 = $io_thread_status_srv1 + $sql_thread_status_srv1; - $total_srv2 = $io_thread_status_srv2 + $sql_thread_status_srv2; - - # Check If there is two slave - if ($total_srv1 < 2 && $total_srv2 < 2) { - $slave_status = 1; - $slave_status_error = "Two slave. Need to have only one."; - } else { - # Check if a thread is down - if ($total_srv1 == 1) { - $slave_status = -1; - $slave_status_error = "A Replication thread is down on '" . $sql_one->get_id() . "'."; - if ($sql_thread_status_srv1 != 0) { - if (defined($last_error1) && $last_error1 ne "") { - $slave_status = 1; - $slave_status_error .= " SQL Thread is stopped because of an error (error='" . $last_error1 . "')."; - } - } - } - if ($total_srv2 == 1) { - $slave_status = -1; - $slave_status_error = "A Replication thread is down on '" . $sql_two->get_id() . "'."; - if ($sql_thread_status_srv2 != 0) { - if (defined($last_error2) && $last_error2 ne "") { - $slave_status = 1; - $slave_status_error .= " SQL Thread is stopped because of an error (error='" . $last_error2 . "')."; - } - } - } - - # Check if we need to SKIP - if ($io_thread_status_srv1 == 100) { - $slave_status = -1; - $slave_status_error .= " Skip check on '" . $sql_one->get_id() . "'."; - } - if ($io_thread_status_srv2 == 100) { - $slave_status = -1; - $slave_status_error .= " Skip check on '" . $sql_two->get_id() . "'."; - } - - # Save Slave - if ($total_srv1 < 2) { - $slave_save = $sql_one; - $master_save = $sql_two; - } - if ($total_srv2 < 2) { - $slave_save = $sql_two; - $master_save = $sql_one; - } - - if ($total_srv2 > 1 && $total_srv1 > 1) { - $slave_status = 1; - $slave_status_error .= " No slave (maybe because we cannot check a server)."; - } - } - - #### - # Check Slave position - #### - if (!defined($slave_save)) { - $position_status = -2; - $position_status_error = "Skip because we can't identify a unique slave."; - } else { - if ($master_save->get_id() eq $connection_status_name_srv1 && $exit1 == -1) { - $position_status = -1; - $position_status_error = "Can't get master position on '" . $master_save->get_id() . "'."; - } elsif ($master_save->get_id() eq $connection_status_name_srv2 && $exit2 == -1) { - $position_status = -1; - $position_status_error = "Can't get master position on '" . $master_save->get_id() . "'."; - } else { - # Get Master Position - $master_save->query(query => q{ - SHOW MASTER STATUS - }); - my $result = $master_save->fetchrow_hashref(); - my $master_file = $result->{File}; - my $master_position = $result->{Position}; - - $slave_save->query(query => q{ - SHOW SLAVE STATUS - }); - my $result2 = $slave_save->fetchrow_hashref(); - my $slave_file = $result2->{Master_Log_File}; # 'Master_Log_File' - my $slave_position = $result2->{Read_Master_Log_Pos}; # 'Read_Master_Log_Pos' - my $num_sec_lates = $result2->{Seconds_Behind_Master}; - - my $exit_code_sec = $self->{perfdata}->threshold_check(value => $num_sec_lates, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - if (!$self->{output}->is_status(value => $exit_code_sec, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit_code_sec, - short_msg => sprintf("Slave has %d seconds latency behind master", $num_sec_lates)); - } - $self->{output}->perfdata_add(label => 'slave_latency', unit => 's', - value => $num_sec_lates, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); - - my $slave_sql_thread_ko = 1; - my $slave_sql_thread_warning = 1; - my $slave_sql_thread_ok = 1; - - $slave_save->query(query => q{ - SHOW FULL PROCESSLIST - }); - while ((my $row = $slave_save->fetchrow_hashref())) { - my $state = $row->{State}; - $slave_sql_thread_ko = 0 if (defined($state) && $state =~ /^(Waiting to reconnect after a failed binlog dump request|Connecting to master|Reconnecting after a failed binlog dump request|Waiting to reconnect after a failed master event read|Waiting for the slave SQL thread to free enough relay log space)$/i); - $slave_sql_thread_warning = 0 if (defined($state) && $state =~ /^Waiting for the next event in relay log|Reading event from the relay log$/i); - $slave_sql_thread_ok = 0 if (defined($state) && $state =~ /^Has read all relay log; waiting for the slave I\/O thread to update it$/i); - } - - if ($slave_sql_thread_ko == 0) { - $position_status = 1; - $position_status_error .= " Slave replication has connection issue with the master."; - } elsif (($master_file ne $slave_file || $master_position != $slave_position) && $slave_sql_thread_warning == 0) { - $position_status = -1; - $position_status_error .= " Slave replication is late but it's progressing."; - } elsif (($master_file ne $slave_file || $master_position != $slave_position) && $slave_sql_thread_ok == 0) { - $position_status = -1; - $position_status_error .= " Slave replication is late but it's progressing."; - } else { - $master_file =~ /(\d+)$/; - my $master_bin_num = $1; - $slave_file =~ /(\d+)$/; - my $slave_bin_num = $1; - my $diff_binlog = abs($master_bin_num - $slave_bin_num); - - # surely of missconfiguration of the plugin - if ($diff_binlog > 1 && $num_sec_lates < 10) { - $position_status = -3; - $position_status_error .= " Surely a configuration problem of the plugin (not good master and slave server used)"; - } - } - } - } - - $self->replication_add($slave_status, "Slave Thread Status", $slave_status_error); - $self->replication_add($position_status, "Position Status", $position_status_error); - - $self->{output}->display(); - $self->{output}->exit(); -} - -sub replication_add { - my ($self, $lstate, $str_display, $lerr) = @_; - my $status; - my $status_msg; - - if ($lstate == 0) { - $status = 'OK'; - } elsif ($lstate == -1) { - $status = 'WARNING'; - } elsif ($lstate == -2) { - $status = 'CRITICAL'; - $status_msg = 'SKIP'; - } elsif ($lstate == -3) { - $status = 'UNKNOWN'; - } else { - $status = 'CRITICAL'; - } - - my $output; - if (defined($lerr) && $lerr ne "") { - $output = $str_display . " [" . (defined($status_msg) ? $status_msg : $status) . "] [" . $lerr . "]"; - } else { - $output = $str_display . " [" . (defined($status_msg) ? $status_msg : $status) . "]"; - } - if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $status, - short_msg => $output); - } - - $self->{output}->output_add(long_msg => $output); -} - -1; - -__END__ - -=head1 MODE - -Check MySQL replication master/slave (need to use --multiple). - -=over 8 - -=item B<--warning> - -Threshold warning in seconds (slave latency). - -=item B<--critical> - -Threshold critical in seconds (slave latency). - -=back - -=cut diff --git a/centreon-plugins/database/mysql/plugin.pm b/centreon-plugins/database/mysql/plugin.pm index 99d8b7325..09693038f 100644 --- a/centreon-plugins/database/mysql/plugin.pm +++ b/centreon-plugins/database/mysql/plugin.pm @@ -31,21 +31,20 @@ sub new { $self->{version} = '0.1'; %{$self->{modes}} = ( - 'connection-time' => 'centreon::common::protocols::sql::mode::connectiontime', - 'databases-size' => 'database::mysql::mode::databasessize', - 'innodb-bufferpool-hitrate' => 'database::mysql::mode::innodbbufferpoolhitrate', - 'long-queries' => 'database::mysql::mode::longqueries', - 'myisam-keycache-hitrate' => 'database::mysql::mode::myisamkeycachehitrate', - 'open-files' => 'database::mysql::mode::openfiles', - 'qcache-hitrate' => 'database::mysql::mode::qcachehitrate', - 'queries' => 'database::mysql::mode::queries', - 'replication-master-slave' => 'database::mysql::mode::replicationmasterslave', - 'replication-master-master' => 'database::mysql::mode::replicationmastermaster', - 'slow-queries' => 'database::mysql::mode::slowqueries', - 'sql' => 'centreon::common::protocols::sql::mode::sql', - 'sql-string' => 'centreon::common::protocols::sql::mode::sqlstring', - 'threads-connected' => 'database::mysql::mode::threadsconnected', - 'uptime' => 'database::mysql::mode::uptime' + 'connection-time' => 'centreon::common::protocols::sql::mode::connectiontime', + 'databases-size' => 'database::mysql::mode::databasessize', + 'innodb-bufferpool-hitrate' => 'database::mysql::mode::innodbbufferpoolhitrate', + 'long-queries' => 'database::mysql::mode::longqueries', + 'myisam-keycache-hitrate' => 'database::mysql::mode::myisamkeycachehitrate', + 'open-files' => 'database::mysql::mode::openfiles', + 'qcache-hitrate' => 'database::mysql::mode::qcachehitrate', + 'queries' => 'database::mysql::mode::queries', + 'replication' => 'database::mysql::mode::replication', + 'slow-queries' => 'database::mysql::mode::slowqueries', + 'sql' => 'centreon::common::protocols::sql::mode::sql', + 'sql-string' => 'centreon::common::protocols::sql::mode::sqlstring', + 'threads-connected' => 'database::mysql::mode::threadsconnected', + 'uptime' => 'database::mysql::mode::uptime' ); $self->{sql_modes}->{dbi} = 'database::mysql::dbi'; @@ -72,15 +71,15 @@ sub init { @{$self->{sqldefault}->{dbi}} = (); @{$self->{sqldefault}->{mysqlcmd}} = (); for (my $i = 0; $i < scalar(@{$options_result->{db_host}}); $i++) { - $self->{sqldefault}->{dbi}[$i] = { data_source => 'mysql:host=' . $options_result->{db_host}[$i] }; - $self->{sqldefault}->{mysqlcmd}[$i] = { host => $options_result->{db_host}[$i] }; + $self->{sqldefault}->{dbi}->[$i] = { data_source => 'mysql:host=' . $options_result->{db_host}[$i] }; + $self->{sqldefault}->{mysqlcmd}->[$i] = { host => $options_result->{db_host}[$i] }; if (defined($options_result->{db_port}[$i])) { - $self->{sqldefault}->{dbi}[$i]->{data_source} .= ';port=' . $options_result->{db_port}[$i]; - $self->{sqldefault}->{mysqlcmd}[$i]->{port} = $options_result->{db_port}[$i]; + $self->{sqldefault}->{dbi}->[$i]->{data_source} .= ';port=' . $options_result->{db_port}[$i]; + $self->{sqldefault}->{mysqlcmd}->[$i]->{port} = $options_result->{db_port}[$i]; } if (defined($options_result->{db_socket}[$i])) { - $self->{sqldefault}->{dbi}[$i]->{data_source} .= ';mysql_socket=' . $options_result->{db_socket}[$i]; - $self->{sqldefault}->{mysqlcmd}[$i]->{socket} = $options_result->{db_socket}[$i]; + $self->{sqldefault}->{dbi}->[$i]->{data_source} .= ';mysql_socket=' . $options_result->{db_socket}[$i]; + $self->{sqldefault}->{mysqlcmd}->[$i]->{socket} = $options_result->{db_socket}[$i]; } } }