Debian Codegraph @

src/apps/redis/sentinel/mode/redisclusters.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::redisclusters;
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 [role: %s]",
34 $self->{result_values}->{status},
35 $self->{result_values}->{role}
36 );
37 }
38
39 sub prefix_cluster_output {
40 my ($self, %options) = @_;
41
42 return "cluster '" . $options{instance} . "' ";
43 }
44
45 sub cluster_long_output {
46 my ($self, %options) = @_;
47
48 return "checking cluster '" . $options{instance} . "'";
49 }
50
51 sub prefix_instance_output {
52 my ($self, %options) = @_;
53
54 return "instance '" . $options{instance} . "' ";
55 }
56
57 sub prefix_global_output {
58 my ($self, %options) = @_;
59
60 return 'number of ';
61 }
62
63 sub set_counters {
64 my ($self, %options) = @_;
65
66 $self->{maps_counters_type} = [
67 { 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',
68 group => [
69 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } },
70 { name => 'stddev', type => 0, skipped_code => { -10 => 1 } },
71 { name => 'instances', type => 1, display_long => 1, cb_prefix_output => 'prefix_instance_output', message_multiple => 'All redis instances are ok', skipped_code => { -10 => 1 } }
72 ]
73 }
74 ];
75
76 $self->{maps_counters}->{global} = [
77 { label => 'slaves-detected', nlabel => 'cluster.redis.slaves.detected.count', set => {
78 key_values => [ { name => 'num_slaves' } ],
79 output_template => 'detected slaves: %s',
80 perfdatas => [
81 { template => '%s', min => 0, label_extra_instance => 1 }
82 ]
83 }
84 },
85 { label => 'redis-sdown', nlabel => 'cluster.redis.subjectively_down.count', set => {
86 key_values => [ { name => 'sdown' } ],
87 output_template => 'subjectively down instances: %s',
88 perfdatas => [
89 { template => '%s', min => 0, label_extra_instance => 1 }
90 ]
91 }
92 },
93 { label => 'redis-odown', nlabel => 'cluster.redis.objectively_down.count', set => {
94 key_values => [ { name => 'odown' } ],
95 output_template => 'objectively down instances: %s',
96 perfdatas => [
97 { template => '%s', min => 0, label_extra_instance => 1 }
98 ]
99 }
100 }
101 ];
102
103 $self->{maps_counters}->{stddev} = [
104 { label => 'slave-repl-offset-stddev', nlabel => 'cluster.redis.slave_replication_offset.stddev.count', set => {
105 key_values => [ { name => 'stddev_repl_offset' } ],
106 output_template => 'slave replication offset standard deviation: %.2f',
107 perfdatas => [
108 { template => '%.2f' }
109 ]
110 }
111 }
112 ];
113
114 $self->{maps_counters}->{instances} = [
115 {
116 label => 'status',
117 type => 2,
118 critical_default => '%{status} =~ /o_down|s_down|master_down|disconnected/i',
119 set => {
120 key_values => [
121 { name => 'status' }, { name => 'role' },
122 { name => 'address' }, { name => 'port' },
123 { 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 => 'redis-ping-ok-latency', nlabel => 'cluster.redis.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 role => $options{entry}->{'role-reported'},
162 status => $options{entry}->{flags},
163 ping_ok_latency => $options{entry}->{'last-ok-ping-reply'},
164 slave_repl_offset => $options{entry}->{'slave-repl-offset'}
165 };
166 $self->{clusters}->{ $options{cluster_name} }->{global}->{sdown}++
167 if ($options{entry}->{flags} =~ /s_down/);
168 $self->{clusters}->{ $options{cluster_name} }->{global}->{odown}++
169 if ($options{entry}->{flags} =~ /o_down/);
170 }
171
172 sub stddev {
173 my ($self, %options) = @_;
174
175 my $total = 0;
176 my $num = 0;
177 foreach my $entry (values %{$self->{clusters}->{ $options{cluster_name} }->{instances}}) {
178 next if (!defined($entry->{slave_repl_offset}));
179 $total += $entry->{slave_repl_offset};
180 $num++;
181 }
182
183 return if ($num <= 1);
184
185 my $mean = $total / $num;
186 $total = 0;
187 foreach my $entry (values %{$self->{clusters}->{ $options{cluster_name} }->{instances}}) {
188 next if (!defined($entry->{slave_repl_offset}));
189 $total += ($mean - $entry->{slave_repl_offset}) ** 2;
190 }
191 $self->{clusters}->{ $options{cluster_name} }->{stddev} = { stddev_repl_offset => sqrt($total / $num) };
192 }
193
194 sub manage_selection {
195 my ($self, %options) = @_;
196
197 my $results = $options{custom}->command(command => 'sentinel masters');
198
199 $self->{clusters} = {};
200 foreach my $entry (@$results) {
201 next if (defined($self->{option_results}->{filter_cluster_name}) && $self->{option_results}->{filter_cluster_name} ne ''
202 && $entry->{name} !~ /$self->{option_results}->{filter_cluster_name}/);
203
204 $self->{clusters}->{ $entry->{name} } = {
205 global => { num_slaves => $entry->{'num-slaves'}, odown => 0, sdown => 0 },
206 instances => {}
207 };
208 $self->add_instance(cluster_name => $entry->{name}, entry => $entry);
209
210 my $replicas = $options{custom}->command(command => 'sentinel replicas ' . $entry->{name});
211 foreach (@$replicas) {
212 $self->add_instance(cluster_name => $entry->{name}, entry => $_);
213 }
214
215 $self->stddev(cluster_name => $entry->{name});
216 }
217
218 if (scalar(keys %{$self->{clusters}}) <= 0) {
219 $self->{output}->add_option_msg(short_msg => "No redis cluster found.");
220 $self->{output}->option_exit();
221 }
222
223
224 }
225
226 1;
227
228 __END__
229
230 =head1 MODE
231
232 Check redis clusters informations.
233
234 =over 8
235
236 =item B<--filter-cluster-name>
237
238 Filter clusters by name (can be a regexp).
239
240 =item B<--unknown-status>
241
242 Define the conditions to match for the status to be UNKNOWN.
243 You can use the following variables: %{status}, %{role}, %{address}, %{port}, %{cluster_name}
244
245 =item B<--warning-status>
246
247 Define the conditions to match for the status to be WARNING.
248 You can use the following variables: %{status}, %{role}, %{address}, %{port}, %{cluster_name}
249
250 =item B<--critical-status>
251
252 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /o_down|s_down|master_down|disconnected/i').
253 You can use the following variables: %{status}, %{role}, %{address}, %{port}, %{cluster_name}
254
255 =item B<--warning-*> B<--critical-*>
256
257 Thresholds.
258 Can be: 'redis-ping-ok-latency', 'redis-sdown', 'redis-odown',
259 'slave-repl-offset-stddev', 'slaves-detected'.
260
261 =back
262
263 =cut