Debian Codegraph @

src/hardware/server/sun/mgmt_cards/mode/showboards.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::showboards;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28 use hardware::server::sun::mgmt_cards::lib::telnet;
29 use centreon::plugins::statefile;
30
31 sub new {
32 my ($class, %options) = @_;
33 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
34 bless $self, $class;
35
36 $options{options}->add_options(arguments => {
37 "hostname:s" => { name => 'hostname' },
38 "port:s" => { name => 'port', default => 23 },
39 "username:s" => { name => 'username' },
40 "password:s" => { name => 'password' },
41 "timeout:s" => { name => 'timeout', default => 50 },
42 "memory" => { name => 'memory' },
43 "command-plink:s" => { name => 'command_plink' },
44 "ssh" => { name => 'ssh' },
45 "exclude:s" => { name => 'exclude' },
46 "no-component:s" => { name => 'no_component' }
47 });
48
49 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
50 $self->{components} = {};
51 $self->{no_components} = undef;
52
53 return $self;
54 }
55
56 sub check_options {
57 my ($self, %options) = @_;
58 $self->SUPER::init(%options);
59
60 if (!defined($self->{option_results}->{hostname})) {
61 $self->{output}->add_option_msg(short_msg => "Need to specify a hostname.");
62 $self->{output}->option_exit();
63 }
64 if (!defined($self->{option_results}->{username})) {
65 $self->{output}->add_option_msg(short_msg => "Need to specify a username.");
66 $self->{output}->option_exit();
67 }
68 if (!defined($self->{option_results}->{password})) {
69 $self->{output}->add_option_msg(short_msg => "Need to specify a password.");
70 $self->{output}->option_exit();
71 }
72
73 if (defined($self->{option_results}->{memory})) {
74 $self->{statefile_cache}->check_options(%options);
75 }
76
77 if (!defined($self->{option_results}->{ssh})) {
78 require hardware::server::sun::mgmt_cards::lib::telnet;
79 }
80
81 if (defined($self->{option_results}->{no_component})) {
82 if ($self->{option_results}->{no_component} ne '') {
83 $self->{no_components} = $self->{option_results}->{no_component};
84 } else {
85 $self->{no_components} = 'critical';
86 }
87 }
88
89 centreon::plugins::misc::check_security_command(
90 output => $self->{output},
91 command => $self->{option_results}->{command_plink}
92 );
93
94 $self->{option_results}->{command} = 'plink'
95 if (!defined($self->{option_results}->{command}) || $self->{option_results}->{command} eq '');
96 }
97
98 sub telnet_shell_plateform {
99 my ($telnet_handle) = @_;
100
101 # There are:
102 #System Controller 'sf6800':
103 # Type 0 for Platform Shell
104 # Type 1 for domain A console
105 # Type 2 for domain B console
106 # Type 3 for domain C console
107 # Type 4 for domain D console
108 # Input:
109
110 $telnet_handle->waitfor(Match => '/Input:/i', Errmode => "return") or telnet_error($telnet_handle->errmsg);
111 $telnet_handle->print("0");
112 }
113
114 sub ssh_command {
115 my ($self, %options) = @_;
116 my $username = '';
117
118 if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '') {
119 $username = $self->{option_results}->{username} . '\n';
120 }
121
122 my $cmd_in = "0" . $username . $self->{option_results}->{password} . '\nshowboards\ndisconnect\n';
123 my $cmd = "echo -e '$cmd_in' | " . $self->{option_results}->{command_plink} . " -batch " . $self->{option_results}->{hostname} . " 2>&1";
124 my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick(
125 command => $cmd,
126 timeout => $self->{option_results}->{timeout},
127 wait_exit => 1
128 );
129
130 $stdout =~ s/\r//g;
131 if ($lerror <= -1000) {
132 $self->{output}->output_add(severity => 'UNKNOWN',
133 short_msg => $stdout);
134 $self->{output}->display();
135 $self->{output}->exit();
136 }
137
138 return $stdout;
139 }
140
141 sub run {
142 my ($self, %options) = @_;
143 my ($output, @lines);
144
145 if (defined($self->{option_results}->{ssh})) {
146 $output = $self->ssh_command();
147 @lines = split /\n/, $output;
148 } else {
149 my $telnet_handle = hardware::server::sun::mgmt_cards::lib::telnet::connect(
150 username => $self->{option_results}->{username},
151 password => $self->{option_results}->{password},
152 hostname => $self->{option_results}->{hostname},
153 port => $self->{option_results}->{port},
154 timeout => $self->{option_results}->{timeout},
155 output => $self->{output},
156 closure => \&telnet_shell_plateform);
157 @lines = $telnet_handle->cmd("showboards");
158 }
159
160 if (defined($self->{option_results}->{memory})) {
161 $self->{statefile_cache}->read(statefile => 'cache_sun_mgmtcards_' . $self->{option_results}->{hostname} . '_' . $self->{mode});
162 }
163
164 #Slot Pwr Component Type State Status Domain
165 #---- --- -------------- ----- ------ ------
166 #SSC0 On System Controller Main Passed -
167 #ID0 On Sun Fire 6800 Centerplane - OK -
168 #PS0 On A212 Power Supply - OK -
169 #PS1 On A212 Power Supply - OK -
170 #PS2 On A212 Power Supply - OK -
171 #PS3 On A212 Power Supply - OK -
172 #PS4 On A212 Power Supply - OK -
173 #PS5 On A212 Power Supply - OK -
174
175 my $datas = {};
176 $self->{output}->output_add(long_msg => "Checking slots");
177 $self->{components}->{slot} = {name => 'slot', total => 0, skip => 0, known_error => 0};
178
179 foreach (@lines) {
180 chomp;
181
182 my ($id, $status);
183 $id = $1 if (/([^\s]+?)\s+/);
184 $status = $1 if (/\s+(Degraded|Failed|Not tested|Passed|OK|Under Test)\s+/i);
185 next if (!defined($status) || $status eq '');
186
187 next if ($self->check_exclude(section => 'slot', instance => $id));
188 $self->{components}->{slot}->{total}++;
189
190 $self->{output}->output_add(long_msg => sprintf("Slot '%s' status is %s.",
191 $id, $status)
192 );
193 if ($status =~ /^(Degraded|Failed)$/i) {
194 if (defined($self->{option_results}->{memory})) {
195 my $old_status = $self->{statefile_cache}->get(name => "slot_$id");
196 if (!defined($old_status) || $old_status ne $status) {
197 $self->{output}->output_add(severity => 'CRITICAL',
198 short_msg => "Slot '$id' status is '$status'");
199 } else {
200 $self->{components}->{slot}->{known_error}++;
201 }
202 $datas->{"slot_$id"} = $status;
203 } else {
204 $self->{output}->output_add(severity => 'CRITICAL',
205 short_msg => "Slot '$id' status is '$status'");
206 }
207 }
208 }
209
210 if (defined($self->{option_results}->{memory})) {
211 $self->{statefile_cache}->write(data => $datas);
212 }
213
214 my $total_components = 0;
215 my $display_by_component = '';
216 my $display_by_component_append = '';
217 foreach my $comp (sort(keys %{$self->{components}})) {
218 # Skipping short msg when no components
219 next if ($self->{components}->{$comp}->{total} == 0 && $self->{components}->{$comp}->{skip} == 0);
220 $total_components += $self->{components}->{$comp}->{total} + $self->{components}->{$comp}->{skip};
221 $display_by_component .= $display_by_component_append . $self->{components}->{$comp}->{total} . '/' . $self->{components}->{$comp}->{skip} . '/' . $self->{components}->{$comp}->{known_error} . ' ' . $self->{components}->{$comp}->{name};
222 $display_by_component_append = ', ';
223 }
224
225 $self->{output}->output_add(severity => 'OK',
226 short_msg => sprintf("All %s components [%s] are ok.",
227 $total_components,
228 $display_by_component)
229 );
230
231 if (defined($self->{option_results}->{no_component}) && $total_components == 0) {
232 $self->{output}->output_add(severity => $self->{no_components},
233 short_msg => 'No components are checked.');
234 }
235
236 $self->{output}->display();
237 $self->{output}->exit();
238 }
239
240 sub check_exclude {
241 my ($self, %options) = @_;
242
243 if (defined($options{instance})) {
244 if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)$options{instance}(\s|,|$)/) {
245 $self->{output}->output_add(long_msg => sprintf("Skipping $options{instance}."));
246 return 1;
247 }
248 }
249 return 0;
250 }
251
252 1;
253
254 __END__
255
256 =head1 MODE
257
258 Check Sun SFxxxx (sf6900, sf6800, sf3800,...) Hardware (through ScApp).
259
260 =over 8
261
262 =item B<--hostname>
263
264 Hostname to query.
265
266 =item B<--port>
267
268 telnet port (default: 23).
269
270 =item B<--username>
271
272 telnet username.
273
274 =item B<--password>
275
276 telnet password.
277
278 =item B<--memory>
279
280 Returns new errors (retention file is used by the following option).
281
282 =item B<--timeout>
283
284 Timeout in seconds for the command (default: 30).
285
286 =item B<--command-plink>
287
288 Plink command (default: plink). Use to set a path.
289
290 =item B<--ssh>
291
292 Use ssh (with plink) instead of telnet.
293
294 =item B<--exclude>
295
296 Exclude some slots (comma separated list) (example: --exclude=IDO,PS0)
297
298 =item B<--no-component>
299
300 Define the expected status if no components are found (default: critical).
301
302 =back
303
304 =cut