Debian Codegraph @

src/hardware/server/sun/mgmt_cards/mode/showstatus.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 hardware::server::sun::mgmt_cards::mode::showstatus;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28
29 my $thresholds = [
30 ['Faulted', 'CRITICAL'],
31 ['Degraded', 'WARNING'],
32 ['Deconfigured', 'WARNING'],
33 ['Maintenance', 'OK'],
34 ['Normal', 'OK'],
35 ];
36
37 sub new {
38 my ($class, %options) = @_;
39 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
40 bless $self, $class;
41
42 $options{options}->add_options(arguments => {
43 "hostname:s" => { name => 'hostname' },
44 "username:s" => { name => 'username' },
45 "password:s" => { name => 'password' },
46 "timeout:s" => { name => 'timeout', default => 30 },
47 "command-plink:s" => { name => 'command_plink' },
48 "threshold-overload:s@" => { name => 'threshold_overload' },
49 "exclude:s@" => { name => 'exclude' }
50 });
51
52 return $self;
53 }
54
55 sub check_options {
56 my ($self, %options) = @_;
57 $self->SUPER::init(%options);
58
59 if (!defined($self->{option_results}->{hostname})) {
60 $self->{output}->add_option_msg(short_msg => "Need to specify a hostname.");
61 $self->{output}->option_exit();
62 }
63 if (!defined($self->{option_results}->{username})) {
64 $self->{output}->add_option_msg(short_msg => "Need to specify a username.");
65 $self->{output}->option_exit();
66 }
67 if (!defined($self->{option_results}->{password})) {
68 $self->{output}->add_option_msg(short_msg => "Need to specify a password.");
69 $self->{output}->option_exit();
70 }
71
72 $self->{overload_th} = [];
73 foreach my $val (@{$self->{option_results}->{threshold_overload}}) {
74 if ($val !~ /^(.*?),(.*)$/) {
75 $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'.");
76 $self->{output}->option_exit();
77 }
78 my ($status, $filter) = ($1, $2);
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 push @{$self->{overload_th}}, {filter => $filter, status => $status};
84 }
85
86 centreon::plugins::misc::check_security_command(
87 output => $self->{output},
88 command => $self->{option_results}->{command_plink}
89 );
90
91 $self->{option_results}->{command} = 'plink'
92 if (!defined($self->{option_results}->{command}) || $self->{option_results}->{command} eq '');
93 }
94
95 sub get_severity {
96 my ($self, %options) = @_;
97 my $status = 'unknown'; # default
98
99 foreach (@{$self->{overload_th}}) {
100 if ($options{value} =~ /$_->{filter}/msi) {
101 $status = $_->{status};
102 return $status;
103 }
104 }
105 foreach (@{$thresholds}) {
106 if ($options{value} =~ /$$_[0]/msi) {
107 $status = $$_[1];
108 return $status;
109 }
110 }
111
112 return $status;
113 }
114
115 sub check_exclude {
116 my ($self, %options) = @_;
117
118 foreach (@{$self->{option_results}->{exclude}}) {
119 if ($options{value} =~ /$_/i) {
120 $self->{output}->output_add(
121 long_msg => sprintf(
122 "Skip Component '%s'",
123 $options{value}
124 )
125 );
126 return 1;
127 }
128 }
129
130 return 0;
131 }
132
133 sub check_tree {
134 my ($self) = @_;
135
136 my $total_components = 0;
137 my @stack = ({ indent => 0, long_instance => '', long_status => ''});
138 while ($self->{stdout} =~ /^([* \t]+)(.*)\s+Status:(.+?);/mg) {
139 my ($indent, $unit_number, $status) = (length($1), $2, $3);
140 my ($long_instance, $long_status);
141
142 while ($indent <= $stack[$#stack]->{indent}) {
143 pop @stack;
144 }
145
146 $long_instance = $stack[$#stack]->{long_instance} . '>' . $unit_number;
147 $long_status = $stack[$#stack]->{long_status} . ' > ' . $unit_number . ' Status:' . $status;
148 if ($indent > $stack[$#stack]->{indent}) {
149 push @stack, {
150 indent => $indent,
151 long_instance => $stack[$#stack]->{long_instance} . '>' . $unit_number,
152 long_status => $stack[$#stack]->{long_status} . ' > ' . $unit_number . ' Status:' . $status
153 };
154 }
155
156 next if ($self->check_exclude(value => $long_instance));
157
158 my $exit = $self->get_severity(value => $status);
159 if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) {
160 $self->{output}->output_add(
161 severity => $exit,
162 short_msg => sprintf(
163 "Component '%s' status is '%s'",
164 $unit_number, $status
165 )
166 );
167 }
168
169 $self->{output}->output_add(
170 long_msg => sprintf(
171 "Component '%s' status is '%s' [%s] [%s]",
172 $unit_number, $status, $long_instance, $long_status
173 )
174 );
175 $total_components++;
176 }
177
178 $self->{output}->output_add(
179 severity => 'OK',
180 short_msg => sprintf(
181 "All %s components are ok.",
182 $total_components
183 )
184 );
185 }
186
187 sub run {
188 my ($self, %options) = @_;
189 my ($lerror, $exit_code);
190
191 ######
192 # Command execution
193 ######
194 ($lerror, $self->{stdout}, $exit_code) = centreon::plugins::misc::backtick(
195 command => $self->{option_results}->{command_plink},
196 timeout => $self->{option_results}->{timeout},
197 arguments => [
198 '-batch', '-l', $self->{option_results}->{username},
199 '-pw', $self->{option_results}->{password},
200 $self->{option_results}->{hostname}, 'showhardconf'
201 ],
202 wait_exit => 1,
203 redirect_stderr => 1
204 );
205
206 $self->{stdout} =~ s/\r//g;
207 if ($lerror <= -1000) {
208 $self->{output}->output_add(
209 severity => 'UNKNOWN',
210 short_msg => $self->{stdout}
211 );
212 $self->{output}->display();
213 $self->{output}->exit();
214 }
215 if ($exit_code != 0) {
216 $self->{stdout} =~ s/\n/ - /g;
217 $self->{output}->output_add(
218 severity => 'UNKNOWN',
219 short_msg => "Command error: $self->{stdout}"
220 );
221 $self->{output}->display();
222 $self->{output}->exit();
223 }
224
225 $self->check_tree();
226
227 $self->{output}->display();
228 $self->{output}->exit();
229 }
230
231 1;
232
233 __END__
234
235 =head1 MODE
236
237 Check Sun Mxxxx (M3000, M4000,...) Hardware (through XSCF).
238
239 =over 8
240
241 =item B<--hostname>
242
243 Hostname to query.
244
245 =item B<--username>
246
247 ssh username.
248
249 =item B<--password>
250
251 ssh password.
252
253 =item B<--command-plink>
254
255 Plink command (default: plink). Use to set a path.
256
257 =item B<--timeout>
258
259 Timeout in seconds for the command (default: 30).
260
261 =item B<--threshold-overload>
262
263 Use this option to override the status returned by the plugin when the status label matches a regular expression (syntax: status,regexp).
264 Example: --threshold-overload='UNKNOWN,Normal'
265
266 =item B<--exclude>
267
268 Filter components (multiple) (can be a regexp).
269 Example: --exclude='MEM#2B' --exclude='MBU_A>MEM#0B'.
270
271 =back
272
273 =cut