Debian Codegraph @

src/apps/redis/sentinel/mode/sentinelclusters.pm

raw
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::mode::sentinelclusters;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 return sprintf(
33 "status: %s",
34 $self->{result_values}->{status}
35 );
36 }
37
38 sub prefix_cluster_output {
39 my ($self, %options) = @_;
40
41 return "cluster '" . $options{instance} . "' ";
42 }
43
44 sub cluster_long_output {
45 my ($self, %options) = @_;
46
47 return "checking cluster '" . $options{instance} . "'";
48 }
49
50 sub prefix_instance_output {
51 my ($self, %options) = @_;
52
53 return "instance '" . $options{instance} . "' ";
54 }
55
56 sub prefix_global_output {
57 my ($self, %options) = @_;
58
59 return 'number of sentinels ';
60 }
61
62 sub set_counters {
63 my ($self, %options) = @_;
64
65 $self->{maps_counters_type} = [
66 { name => 'clusters', type => 3, cb_prefix_output => 'prefix_cluster_output', cb_long_output => 'cluster_long_output', indent_long_output => ' ', message_multiple => 'All clusters are ok',
67 group => [
68 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } },
69 { name => 'quorum', type => 0, skipped_code => { -10 => 1 } },
70 { name => 'instances', type => 1, display_long => 1, cb_prefix_output => 'prefix_instance_output', message_multiple => 'All sentinel instances are ok', skipped_code => { -10 => 1 } }
71 ]
72 }
73 ];
74
75 $self->{maps_counters}->{global} = [
76 { label => 'sentinels-detected', nlabel => 'cluster.sentinels.detected.count', set => {
77 key_values => [ { name => 'num_sentinels' } ],
78 output_template => 'detected: %s',
79 perfdatas => [
80 { template => '%s', min => 0, label_extra_instance => 1 }
81 ]
82 }
83 },
84 { label => 'sentinels-sdown', nlabel => 'cluster.sentinels.subjectively_down.count', set => {
85 key_values => [ { name => 'sdown' } ],
86 output_template => 'subjectively down: %s',
87 perfdatas => [
88 { template => '%s', min => 0, label_extra_instance => 1 }
89 ]
90 }
91 },
92 { label => 'sentinels-odown', nlabel => 'cluster.sentinels.objectively_down.count', set => {
93 key_values => [ { name => 'odown' } ],
94 output_template => 'objectively down: %s',
95 perfdatas => [
96 { template => '%s', min => 0, label_extra_instance => 1 }
97 ]
98 }
99 }
100 ];
101
102 $self->{maps_counters}->{quorum} = [
103 {
104 label => 'quorum-status',
105 type => 2,
106 critical_default => '%{status} =~ /noQuorum/',
107 set => {
108 key_values => [ { name => 'status' }, { name => 'cluster_name' } ],
109 output_template => 'quorum status: %s',
110 closure_custom_perfdata => sub { return 0; },
111 closure_custom_threshold_check => \&catalog_status_threshold_ng
112 }
113 }
114 ];
115
116 $self->{maps_counters}->{instances} = [
117 {
118 label => 'status',
119 type => 2,
120 critical_default => '%{status} =~ /o_down|s_down|master_down|disconnected/i',
121 set => {
122 key_values => [
123 { name => 'status' }, { name => 'address' }, { name => 'port' }, { name => 'cluster_name' }
124 ],
125 closure_custom_output => $self->can('custom_status_output'),
126 closure_custom_perfdata => sub { return 0; },
127 closure_custom_threshold_check => \&catalog_status_threshold_ng
128 }
129 },
130 { label => 'sentinel-ping-ok-latency', nlabel => 'cluster.sentinel.ping_ok.latency.milliseconds', set => {
131 key_values => [ { name => 'ping_ok_latency' } ],
132 output_template => 'last ok ping: %s ms',
133 perfdatas => [
134 { template => '%d', min => 0, unit => 's', label_extra_instance => 1 }
135 ]
136 }
137 }
138 ];
139 }
140
141 sub new {
142 my ($class, %options) = @_;
143 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
144 bless $self, $class;
145
146 $options{options}->add_options(arguments => {
147 'filter-cluster-name:s' => { name => 'filter_cluster_name' }
148 });
149
150 return $self;
151 }
152
153 sub add_instance {
154 my ($self, %options) = @_;
155
156 my $key = $options{entry}->{ip} . ':' . $options{entry}->{port};
157 $self->{clusters}->{ $options{cluster_name} }->{instances}->{$key} = {
158 cluster_name => $options{cluster_name},
159 address => $options{entry}->{ip},
160 port => $options{entry}->{port},
161 status => $options{entry}->{flags},
162 ping_ok_latency => $options{entry}->{'last-ok-ping-reply'}
163 };
164 $self->{clusters}->{ $options{cluster_name} }->{global}->{sdown}++
165 if ($options{entry}->{flags} =~ /s_down/);
166 $self->{clusters}->{ $options{cluster_name} }->{global}->{odown}++
167 if ($options{entry}->{flags} =~ /o_down/);
168 }
169
170 sub manage_selection {
171 my ($self, %options) = @_;
172
173 my $results = $options{custom}->command(command => 'sentinel masters');
174
175 $self->{clusters} = {};
176 foreach my $entry (@$results) {
177 next if (defined($self->{option_results}->{filter_cluster_name}) && $self->{option_results}->{filter_cluster_name} ne ''
178 && $entry->{name} !~ /$self->{option_results}->{filter_cluster_name}/);
179
180 $self->{clusters}->{ $entry->{name} } = {
181 global => { num_sentinels => $entry->{'num-other-sentinels'}, odown => 0, sdown => 0 },
182 quorum => { status => 'unknown', cluster_name => $entry->{name} },
183 instances => {}
184 };
185
186 my $quorum = $options{custom}->ckquorum(command => 'sentinel ckquorum ' . $entry->{name});
187 if ($quorum =~ /OK \d+ usable Sentinels/m) {
188 $self->{clusters}->{ $entry->{name} }->{quorum}->{status} = 'ok';
189 } elsif ($quorum =~ /NOQUORUM \d+ usable Sentinels/m) {
190 $self->{clusters}->{ $entry->{name} }->{quorum}->{status} = 'noQuorum';
191 }
192
193 my $sentinels = $options{custom}->command(command => 'sentinel sentinels ' . $entry->{name});
194 foreach (@$sentinels) {
195 $self->add_instance(cluster_name => $entry->{name}, entry => $_);
196 }
197 }
198
199 if (scalar(keys %{$self->{clusters}}) <= 0) {
200 $self->{output}->add_option_msg(short_msg => "No sentinel cluster found.");
201 $self->{output}->option_exit();
202 }
203 }
204
205 1;
206
207 __END__
208
209 =head1 MODE
210
211 Check sentinel clusters informations.
212
213 =over 8
214
215 =item B<--filter-cluster-name>
216
217 Filter clusters by name (can be a regexp).
218
219 =item B<--unknown-status>
220
221 Define the conditions to match for the status to be UNKNOWN.
222 You can use the following variables: %{status}, %{address}, %{port}, %{cluster_name}
223
224 =item B<--warning-status>
225
226 Define the conditions to match for the status to be WARNING.
227 You can use the following variables: %{status}, %{address}, %{port}, %{cluster_name}
228
229 =item B<--critical-status>
230
231 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /o_down|s_down|master_down|disconnected/i').
232 You can use the following variables: %{status}, %{address}, %{port}, %{cluster_name}
233
234 =item B<--unknown-status>
235
236 Define the conditions to match for the status to be UNKNOWN.
237 You can use the following variables: %{status}, %{address}, %{port}, %{cluster_name}
238
239 =item B<--warning-quorum-status>
240
241 Set warning threshold for quorum status.
242 You can use the following variables: %{status}, %{address}, %{port}, %{cluster_name}
243
244 =item B<--critical-quorum-status>
245
246 Set critical threshold for quorum status (default: '%{status} =~ /noQuorum/').
247 You can use the following variables: %{status}, %{cluster_name}
248
249 =item B<--warning-*> B<--critical-*>
250
251 Thresholds.
252 Can be: 'sentinel-ping-ok-latency', 'sentinels-sdown', 'sentinels-odown', 'sentinels-detected'.
253
254 =back
255
256 =cut