| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package apps::redis::sentinel::custom::cli; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use centreon::plugins::ssh; |
| 26 |
use centreon::plugins::misc; |
| 27 |
use Digest::MD5 qw(md5_hex); |
| 28 |
|
| 29 |
sub new { |
| 30 |
my ($class, %options) = @_; |
| 31 |
my $self = {}; |
| 32 |
bless $self, $class; |
| 33 |
|
| 34 |
if (!defined($options{output})) { |
| 35 |
print "Class Custom: Need to specify 'output' argument.\n"; |
| 36 |
exit 3; |
| 37 |
} |
| 38 |
if (!defined($options{options})) { |
| 39 |
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); |
| 40 |
$options{output}->option_exit(); |
| 41 |
} |
| 42 |
|
| 43 |
if (!defined($options{noptions})) { |
| 44 |
$options{options}->add_options(arguments => { |
| 45 |
'ssh-hostname:s' => { name => 'ssh_hostname' }, |
| 46 |
'server:s' => { name => 'server' }, |
| 47 |
'port:s' => { name => 'port' }, |
| 48 |
'username:s' => { name => 'username' }, |
| 49 |
'password:s' => { name => 'password' }, |
| 50 |
'tls' => { name => 'tls' }, |
| 51 |
'cacert:s' => { name => 'cacert' }, |
| 52 |
'insecure' => { name => 'insecure' }, |
| 53 |
'timeout:s' => { name => 'timeout' } |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
$options{options}->add_help(package => __PACKAGE__, sections => 'REDIS SENTINEL OPTIONS', once => 1); |
| 58 |
|
| 59 |
$self->{output} = $options{output}; |
| 60 |
$self->{ssh} = centreon::plugins::ssh->new(%options); |
| 61 |
|
| 62 |
return $self; |
| 63 |
} |
| 64 |
|
| 65 |
sub set_options { |
| 66 |
my ($self, %options) = @_; |
| 67 |
|
| 68 |
$self->{option_results} = $options{option_results}; |
| 69 |
} |
| 70 |
|
| 71 |
sub set_defaults {} |
| 72 |
|
| 73 |
sub check_options { |
| 74 |
my ($self, %options) = @_; |
| 75 |
|
| 76 |
$self->{ssh_hostname} = defined($self->{option_results}->{ssh_hostname}) && $self->{option_results}->{ssh_hostname} ne '' ? $self->{option_results}->{ssh_hostname} : ''; |
| 77 |
$self->{server} = defined($self->{option_results}->{server}) && $self->{option_results}->{server} ne '' ? $self->{option_results}->{server} : ''; |
| 78 |
$self->{port} = defined($self->{option_results}->{port}) && $self->{option_results}->{port} ne '' ? $self->{option_results}->{port} : 26379; |
| 79 |
$self->{username} = defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '' ? $self->{option_results}->{username} : ''; |
| 80 |
$self->{password} = defined($self->{option_results}->{password}) && $self->{option_results}->{password} ne '' ? $self->{option_results}->{password} : ''; |
| 81 |
$self->{timeout} = defined($self->{option_results}->{timeout}) && $self->{option_results}->{timeout} =~ /(\d+)/ ? $1 : 10; |
| 82 |
$self->{tls} = defined($self->{option_results}->{tls}) ? 1 : 0; |
| 83 |
$self->{insecure} = defined($self->{option_results}->{insecure}) ? 1 : 0; |
| 84 |
$self->{cacert} = defined($self->{option_results}->{cacert}) && $self->{option_results}->{cacert} ne '' ? $self->{option_results}->{cacert} : ''; |
| 85 |
|
| 86 |
if ($self->{server} eq '') { |
| 87 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --server option.'); |
| 88 |
$self->{output}->option_exit(); |
| 89 |
} |
| 90 |
if ($self->{ssh_hostname} ne '') { |
| 91 |
$self->{option_results}->{hostname} = $self->{ssh_hostname}; |
| 92 |
$self->{ssh}->check_options(option_results => $self->{option_results}); |
| 93 |
} |
| 94 |
if ($self->{username} ne '' && $self->{option_results}->{password} eq '') { |
| 95 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --password option.'); |
| 96 |
$self->{output}->option_exit(); |
| 97 |
} |
| 98 |
|
| 99 |
$self->{server} = centreon::plugins::misc::sanitize_command_param(value => $self->{server}); |
| 100 |
$self->{port} = centreon::plugins::misc::sanitize_command_param(value => $self->{port}); |
| 101 |
$self->{cacert} = centreon::plugins::misc::sanitize_command_param(value => $self->{cacert}); |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
sub get_connection_info { |
| 107 |
my ($self, %options) = @_; |
| 108 |
|
| 109 |
my $id = $self->{server} . ':' . $self->{port}; |
| 110 |
return md5_hex($id); |
| 111 |
} |
| 112 |
|
| 113 |
sub execute_command { |
| 114 |
my ($self, %options) = @_; |
| 115 |
|
| 116 |
my $command_options = "-h '" . $self->{server} . "' -p " . $self->{port}; |
| 117 |
$command_options .= $self->get_extra_options(); |
| 118 |
$command_options .= ' --no-raw ' . $options{command}; |
| 119 |
|
| 120 |
my $timeout = $self->{timeout}; |
| 121 |
if (!defined($timeout)) { |
| 122 |
$timeout = defined($options{timeout}) ? $options{timeout} : 10; |
| 123 |
} |
| 124 |
|
| 125 |
my ($stdout, $exit_code); |
| 126 |
if ($self->{ssh_hostname} ne '') { |
| 127 |
($stdout, $exit_code) = $self->{ssh}->execute( |
| 128 |
hostname => $self->{ssh_hostname}, |
| 129 |
sudo => $self->{option_results}->{sudo}, |
| 130 |
command => 'redis-cli', |
| 131 |
command_options => $command_options, |
| 132 |
timeout => $timeout, |
| 133 |
no_quit => $options{no_quit} |
| 134 |
); |
| 135 |
} else { |
| 136 |
($stdout, $exit_code) = centreon::plugins::misc::execute( |
| 137 |
output => $self->{output}, |
| 138 |
sudo => $self->{option_results}->{sudo}, |
| 139 |
options => { timeout => $timeout }, |
| 140 |
command => 'redis-cli', |
| 141 |
command_options => $command_options, |
| 142 |
no_quit => $options{no_quit} |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$self->{output}->output_add(long_msg => "command response: $stdout", debug => 1); |
| 147 |
|
| 148 |
if ($stdout =~ /^NOPERM/m) { |
| 149 |
$self->{output}->add_option_msg(short_msg => 'Permissions issue'); |
| 150 |
$self->{output}->option_exit(); |
| 151 |
} |
| 152 |
|
| 153 |
return ($stdout, $exit_code); |
| 154 |
} |
| 155 |
|
| 156 |
sub get_extra_options { |
| 157 |
my ($self, %options) = @_; |
| 158 |
|
| 159 |
my $options = ''; |
| 160 |
$options .= ' --tls' if ($self->{tls} == 1); |
| 161 |
$options .= " --cacert '" . $self->{cacert} . "'" if ($self->{cacert} ne ''); |
| 162 |
$options .= ' --insecure' if ($self->{insecure} == 1); |
| 163 |
$options .= " --user '" . $self->{username} . "'" if ($self->{username} ne ''); |
| 164 |
$options .= " -a '" . $self->{password} . "'" if ($self->{password} ne ''); |
| 165 |
return $options; |
| 166 |
} |
| 167 |
|
| 168 |
sub ckquorum { |
| 169 |
my ($self, %options) = @_; |
| 170 |
|
| 171 |
my ($stdout) = $self->execute_command(command => $options{command}); |
| 172 |
return $stdout; |
| 173 |
} |
| 174 |
|
| 175 |
sub command { |
| 176 |
my ($self, %options) = @_; |
| 177 |
|
| 178 |
my ($stdout) = $self->execute_command(command => $options{command}); |
| 179 |
my $results = []; |
| 180 |
while ($stdout =~ /^\d+\)(.*?)(?=\n\d+\)|\Z$)/msg) { |
| 181 |
my @lines = split(/\n/, $1); |
| 182 |
my $entry = {}; |
| 183 |
while (scalar(@lines) > 0) { |
| 184 |
my $label = shift(@lines); |
| 185 |
my $value = shift(@lines); |
| 186 |
if (defined($label) && defined($value) && $label =~ /\d+\) "(.*)"/) { |
| 187 |
$label = $1; |
| 188 |
$entry->{$label} = $1 if ($value =~ /\d+\) "(.*)"/); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
push @$results, $entry; |
| 193 |
} |
| 194 |
return $results; |
| 195 |
} |
| 196 |
|
| 197 |
1; |
| 198 |
|
| 199 |
__END__ |
| 200 |
|
| 201 |
=head1 NAME |
| 202 |
|
| 203 |
redis-cli. |
| 204 |
|
| 205 |
=head1 SYNOPSIS |
| 206 |
|
| 207 |
redis-cli. |
| 208 |
|
| 209 |
=head1 REDIS SENTINEL OPTIONS |
| 210 |
|
| 211 |
=over 8 |
| 212 |
|
| 213 |
=item B<--server> |
| 214 |
|
| 215 |
Sentinel server. |
| 216 |
|
| 217 |
=item B<--port> |
| 218 |
|
| 219 |
Sentinel port (default: 26379). |
| 220 |
|
| 221 |
=item B<--tls> |
| 222 |
|
| 223 |
Establish a secure TLS connection (redis-cli >= 6.x mandatory). |
| 224 |
|
| 225 |
=item B<--cacert> |
| 226 |
|
| 227 |
CA Certificate file to verify with (redis-cli >= 6.x mandatory). |
| 228 |
|
| 229 |
=item B<--insecure> |
| 230 |
|
| 231 |
Allow insecure TLS connection by skipping cert validation (since redis-cli 6.2.0). |
| 232 |
|
| 233 |
=item B<--username> |
| 234 |
|
| 235 |
Sentinel username (redis-cli >= 6.x mandatory). |
| 236 |
|
| 237 |
=item B<--password> |
| 238 |
|
| 239 |
Sentinel password. |
| 240 |
|
| 241 |
=item B<--ssh-hostname> |
| 242 |
|
| 243 |
Remote ssh redis-cli execution. |
| 244 |
|
| 245 |
=item B<--timeout> |
| 246 |
|
| 247 |
Timeout in seconds for the command (default: 10). |
| 248 |
|
| 249 |
=back |
| 250 |
|
| 251 |
=head1 DESCRIPTION |
| 252 |
|
| 253 |
B<custom>. |
| 254 |
|
| 255 |
=cut |