Debian Codegraph @

src/apps/thales/mistral/vs9/restapi/mode/clusters.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::thales::mistral::vs9::restapi::mode::clusters;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use DateTime;
28 use POSIX;
29 use centreon::plugins::misc;
30 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
31
32 my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
33 my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
34
35 sub custom_information_output {
36 my ($self, %options) = @_;
37
38 return sprintf(
39 'virtual ip: %s, timeToSwitch: %s s',
40 $self->{result_values}->{virtualIp},
41 $self->{result_values}->{timeToSwitch}
42 );
43 }
44
45 sub custom_cluster_status_output {
46 my ($self, %options) = @_;
47
48 return sprintf(
49 'status: %s, available for switching: %s',
50 $self->{result_values}->{gatewaysClusterStatus},
51 $self->{result_values}->{availableForSwitching}
52 );
53 }
54
55 sub custom_member_status_output {
56 my ($self, %options) = @_;
57
58 return sprintf(
59 'connected status: %s [role: %s]',
60 $self->{result_values}->{connectedStatus},
61 $self->{result_values}->{role}
62 );
63 }
64
65 sub custom_contact_perfdata {
66 my ($self, %options) = @_;
67
68 $self->{output}->perfdata_add(
69 nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{time_contact_unit} },
70 unit => $self->{instance_mode}->{option_results}->{time_contact_unit},
71 instances => [$self->{result_values}->{clusterName}, $self->{result_values}->{memberName}],
72 value => floor($self->{result_values}->{contact_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_contact_unit} }),
73 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
74 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
75 min => 0
76 );
77 }
78
79 sub custom_contact_threshold {
80 my ($self, %options) = @_;
81
82 return $self->{perfdata}->threshold_check(
83 value => floor($self->{result_values}->{contact_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{time_contact_unit} }),
84 threshold => [
85 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
86 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
87 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
88 ]
89 );
90 }
91
92 sub cluster_long_output {
93 my ($self, %options) = @_;
94
95 return sprintf(
96 "checking cluster '%s'",
97 $options{instance_value}->{clusterName}
98 );
99 }
100
101 sub prefix_cluster_output {
102 my ($self, %options) = @_;
103
104 return sprintf(
105 "cluster '%s' ",
106 $options{instance_value}->{clusterName}
107 );
108 }
109
110 sub prefix_global_output {
111 my ($self, %options) = @_;
112
113 return 'Number of clusters ';
114 }
115
116 sub prefix_member_output {
117 my ($self, %options) = @_;
118
119 return sprintf(
120 "member '%s' ",
121 $options{instance_value}->{memberName}
122 );
123 }
124
125 sub set_counters {
126 my ($self, %options) = @_;
127
128 $self->{maps_counters_type} = [
129 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
130 {
131 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',
132 group => [
133 { name => 'information', type => 0, skipped_code => { -10 => 1 } },
134 { name => 'status', type => 0, skipped_code => { -10 => 1 } },
135 { name => 'members', type => 1, cb_prefix_output => 'prefix_member_output', message_multiple => 'members are ok', display_long => 1, skipped_code => { -10 => 1 } }
136 ]
137 }
138 ];
139
140 $self->{maps_counters}->{global} = [
141 { label => 'clusters-detected', display_ok => 0, nlabel => 'clusters.detected.count', set => {
142 key_values => [ { name => 'detected' } ],
143 output_template => 'detected: %s',
144 perfdatas => [
145 { template => '%s', min => 0 }
146 ]
147 }
148 }
149 ];
150
151 $self->{maps_counters}->{information} = [
152 {
153 label => 'cluster-information',
154 type => 2,
155 set => {
156 key_values => [ { name => 'virtualIp' }, { name => 'timeToSwitch' } ],
157 closure_custom_output => $self->can('custom_information_output'),
158 closure_custom_perfdata => sub { return 0; },
159 closure_custom_threshold_check => \&catalog_status_threshold_ng
160 }
161 }
162 ];
163
164 $self->{maps_counters}->{status} = [
165 {
166 label => 'cluster-status',
167 type => 2,
168 warning_default => '%{gatewaysClusterStatus} =~ /HAC_FAILOVER/i',
169 critical_default => '%{gatewaysClusterStatus} =~ /HAC_FAILURE|HAC_DOWN|HAC_BACKUP_FAILURE/i',
170 set => {
171 key_values => [ { name => 'gatewaysClusterStatus' }, { name => 'availableForSwitching' }, { name => 'clusterName' } ],
172 closure_custom_output => $self->can('custom_cluster_status_output'),
173 closure_custom_perfdata => sub { return 0; },
174 closure_custom_threshold_check => \&catalog_status_threshold_ng
175 }
176 }
177 ];
178
179 $self->{maps_counters}->{members} = [
180 {
181 label => 'member-status',
182 type => 2,
183 set => {
184 key_values => [ { name => 'role' }, { name => 'connectedStatus' }, { name => 'clusterName' }, { name => 'memberName' } ],
185 closure_custom_output => $self->can('custom_member_status_output'),
186 closure_custom_perfdata => sub { return 0; },
187 closure_custom_threshold_check => \&catalog_status_threshold_ng
188 }
189 },
190 { label => 'member-contact-last-time', nlabel => 'member.contact.last.time', set => {
191 key_values => [ { name => 'contact_seconds' }, { name => 'contact_human' }, { name => 'clusterName' }, { name => 'memberName' } ],
192 output_template => 'last contact: %s',
193 output_use => 'contact_human',
194 closure_custom_perfdata => $self->can('custom_contact_perfdata'),
195 closure_custom_threshold_check => $self->can('custom_contact_threshold')
196 }
197 }
198 ];
199 }
200
201 sub new {
202 my ($class, %options) = @_;
203 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
204 bless $self, $class;
205
206 $options{options}->add_options(arguments => {
207 'filter-cluster-name:s' => { name => 'filter_cluster_name' },
208 'time-contact-unit:s' => { name => 'time_contact_unit', default => 's' }
209 });
210
211 return $self;
212 }
213
214 sub check_options {
215 my ($self, %options) = @_;
216 $self->SUPER::check_options(%options);
217
218 if ($self->{option_results}->{time_contact_unit} eq '' || !defined($unitdiv->{$self->{option_results}->{time_contact_unit}})) {
219 $self->{option_results}->{time_contact_unit} = 's';
220 }
221 }
222
223 sub manage_selection {
224 my ($self, %options) = @_;
225
226 my $clusters = $options{custom}->get_clusters();
227
228 $self->{global} = { detected => 0 };
229 $self->{clusters} = {};
230 foreach my $cluster (@$clusters) {
231 next if (defined($self->{option_results}->{filter_cluster_name}) && $self->{option_results}->{filter_cluster_name} ne '' &&
232 $cluster->{name} !~ /$self->{option_results}->{filter_cluster_name}/);
233
234 $self->{global}->{detected}++;
235 $self->{clusters}->{ $cluster->{name} } = {
236 clusterName => $cluster->{name},
237 information => {
238 virtualIp => $cluster->{virtualIp} . '/' . $cluster->{virtualNetmask},
239 timeToSwitch => $cluster->{timeToSwitch}
240 },
241 status => {
242 clusterName => $cluster->{name},
243 gatewaysClusterStatus => $cluster->{gatewaysClusterStatus},
244 availableForSwitching => defined($cluster->{availableForSwitching}) && $cluster->{availableForSwitching} =~ /true|1/i ? 'yes' : 'no'
245 },
246 members => {}
247 };
248
249 foreach ('master', 'backup') {
250 $self->{clusters}->{ $cluster->{name} }->{members}->{ $cluster->{$_ . 'Origin'}->{name} } = {
251 clusterName => $cluster->{name},
252 memberName => $cluster->{$_ . 'Origin'}->{name},
253 connectedStatus => lc($cluster->{$_ . 'OriginStatus'}->{connectedStatus}),
254 role => $_
255 };
256
257 if ($cluster->{$_ . 'OriginStatus'}->{lastCheck} =~ /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d+([+-].*)$/) {
258 my $dt = DateTime->new(
259 year => $1,
260 month => $2,
261 day => $3,
262 hour => $4,
263 minute => $5,
264 second => $6,
265 time_zone => $7
266 );
267 $self->{clusters}->{ $cluster->{name} }->{members}->{ $cluster->{$_ . 'Origin'}->{name} }->{contact_seconds} =
268 time() - $dt->epoch();
269 $self->{clusters}->{ $cluster->{name} }->{members}->{ $cluster->{$_ . 'Origin'}->{name} }->{contact_human} = centreon::plugins::misc::change_seconds(
270 value => $self->{clusters}->{ $cluster->{name} }->{members}->{ $cluster->{$_ . 'Origin'}->{name} }->{contact_seconds}
271 );
272 }
273 }
274 }
275 }
276
277 1;
278
279 __END__
280
281 =head1 MODE
282
283 Check clusters.
284
285 =over 8
286
287 =item B<--filter-cluster-name>
288
289 Filter clusters by name.
290
291 =item B<--unknown-cluster-status>
292
293 Define the conditions to match for the status to be UNKNOWN.
294 You can use the following variables: %{gatewaysClusterStatus}, %{availableForSwitching}, %{clusterName}
295
296 =item B<--warning-cluster-status>
297
298 Define the conditions to match for the status to be WARNING (default: '%{gatewaysClusterStatus} =~ /HAC_FAILOVER/i').
299 You can use the following variables: %{gatewaysClusterStatus}, %{availableForSwitching}, %{clusterName}
300
301 =item B<--critical-cluster-status>
302
303 Define the conditions to match for the status to be CRITICAL (default: '%{gatewaysClusterStatus} =~ /HAC_FAILURE|HAC_DOWN|HAC_BACKUP_FAILURE/i').
304 You can use the following variables: %{gatewaysClusterStatus}, %{availableForSwitching}, %{clusterName}
305
306 =item B<--unknown-member-status>
307
308 Define the conditions to match for the status to be UNKNOWN.
309 You can use the following variables: %{connectedStatus}, %{role}, %{memberName}
310
311 =item B<--warning-member-status>
312
313 Define the conditions to match for the status to be WARNING.
314 You can use the following variables: %{connectedStatus}, %{role}, %{memberName}
315
316 =item B<--critical-member-status>
317
318 Define the conditions to match for the status to be CRITICAL.
319 You can use the following variables: %{connectedStatus}, %{role}, %{memberName}
320
321 =item B<--time-contact-unit>
322
323 Select the time unit for contact threshold. May be 's' for seconds, 'm' for minutes,
324 'h' for hours, 'd' for days, 'w' for weeks. Default is seconds.
325
326 =item B<--warning-*> B<--critical-*>
327
328 Thresholds.
329 Can be: 'clusters-detected', 'member-contact-last-time'.
330
331 =back
332
333 =cut