Debian Codegraph @

src/storage/emc/xtremio/restapi/mode/xenvsstate.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 storage::emc::xtremio::restapi::mode::xenvsstate;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 my $thresholds = {
29 xenvs_state => [
30 ['active', 'OK'],
31 ['.*', 'CRITICAL'],
32 ],
33 };
34
35 sub new {
36 my ($class, %options) = @_;
37 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
38 bless $self, $class;
39
40 $options{options}->add_options(arguments =>
41 {
42 "filter:s@" => { name => 'filter' },
43 "threshold-overload:s@" => { name => 'threshold_overload' },
44 });
45
46 return $self;
47 }
48
49 sub check_options {
50 my ($self, %options) = @_;
51 $self->SUPER::init(%options);
52
53 $self->{filter} = [];
54 foreach my $val (@{$self->{option_results}->{filter}}) {
55 next if (!defined($val) || $val eq '');
56 my @values = split (/,/, $val);
57 push @{$self->{filter}}, { filter => $values[0], instance => $values[1] };
58 }
59
60 $self->{overload_th} = {};
61 foreach my $val (@{$self->{option_results}->{threshold_overload}}) {
62 next if (!defined($val) || $val eq '');
63 my @values = split (/,/, $val);
64 if (scalar(@values) < 3) {
65 $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'.");
66 $self->{output}->option_exit();
67 }
68 my ($section, $instance, $status, $filter);
69 if (scalar(@values) == 3) {
70 ($section, $status, $filter) = @values;
71 $instance = '.*';
72 } else {
73 ($section, $instance, $status, $filter) = @values;
74 }
75 if (!defined $thresholds->{$section}) {
76 $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload section '" . $val . "'.");
77 $self->{output}->option_exit();
78 }
79 if ($self->{output}->is_litteral_status(status => $status) == 0) {
80 $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'.");
81 $self->{output}->option_exit();
82 }
83 $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section}));
84 push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status, instance => $instance };
85 }
86 }
87
88 sub run {
89 my ($self, %options) = @_;
90 my $xtremio = $options{custom};
91
92 my $urlbase = '/api/json/types/';
93 my @items = $xtremio->get_items(url => $urlbase,
94 obj => 'xenvs');
95
96 $self->{output}->output_add(severity => 'OK',
97 short_msg => 'All Xenvs states are active');
98
99 foreach my $item (@items) {
100 next if ($self->check_filter(section => 'state', instance => $item));
101 my $details = $xtremio->get_details(url => $urlbase,
102 obj => 'xenvs',
103 name => $item);
104
105 $self->{output}->output_add(long_msg => sprintf("Xenvs '%s' state is '%s'",
106 $item,
107 $details->{'xenv-state'}));
108
109 my $exit = $self->get_severity(section => 'xenvs_state', instance => $item, value => $details->{'xenv-state'});
110 if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
111 $self->{output}->output_add(severity => $exit,
112 short_msg => sprintf("Xenvs '%s' state is '%s'",
113 $item, $details->{'xenv-state'}));
114 }
115
116 }
117
118 $self->{output}->display();
119 $self->{output}->exit();
120 }
121
122 sub check_filter {
123 my ($self, %options) = @_;
124
125 foreach (@{$self->{filter}}) {
126 if ($options{section} =~ /$_->{filter}/) {
127 if (!defined($options{instance}) && !defined($_->{instance})) {
128 $self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section."));
129 return 1;
130 } elsif (defined($options{instance}) && $options{instance} =~ /$_->{instance}/) {
131 $self->{output}->output_add(long_msg => sprintf("Skipping $options{section} section $options{instance} instance."));
132 return 1;
133 }
134 }
135 }
136
137 return 0;
138 }
139
140 sub get_severity {
141 my ($self, %options) = @_;
142 my $status = 'UNKNOWN'; # default
143
144 if (defined($self->{overload_th}->{$options{section}})) {
145 foreach (@{$self->{overload_th}->{$options{section}}}) {
146 if ($options{value} =~ /$_->{filter}/i &&
147 (!defined($options{instance}) || $options{instance} =~ /$_->{instance}/)) {
148 $status = $_->{status};
149 return $status;
150 }
151 }
152 }
153 my $label = defined($options{label}) ? $options{label} : $options{section};
154 foreach (@{$thresholds->{$label}}) {
155 if ($options{value} =~ /$$_[0]/i) {
156 $status = $$_[1];
157 return $status;
158 }
159 }
160
161 return $status;
162 }
163
164 1;
165
166 __END__
167
168 =head1 MODE
169
170 Check Xenvs state
171
172 =over 8
173
174 =item B<--filter>
175
176 Filter some parts (comma separated list)
177 You can also exclude items from specific instances: --filter=device,cluster-1_xxxx
178
179 =item B<--threshold-overload>
180
181 Use this option to override the status returned by the plugin when the status label matches a regular expression (syntax: section,[instance,]status,regexp).
182 Example: --threshold-overload='xenvs_state,CRITICAL,^(?!(ok)$)'
183
184 =back
185
186 =cut