Debian Codegraph @

src/hardware/server/sun/mgmt_cards/mode/showfaulty.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::showfaulty;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28 use centreon::plugins::statefile;
29
30 sub new {
31 my ($class, %options) = @_;
32 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
33 bless $self, $class;
34
35 $options{options}->add_options(arguments => {
36 'hostname:s' => { name => 'hostname' },
37 'username:s' => { name => 'username' },
38 'password:s' => { name => 'password' },
39 'timeout:s' => { name => 'timeout', default => 30 },
40 'memory' => { name => 'memory' },
41 'command-plink:s' => { name => 'command_plink' }
42 });
43
44 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
45 return $self;
46 }
47
48 sub check_options {
49 my ($self, %options) = @_;
50 $self->SUPER::init(%options);
51
52 if (!defined($self->{option_results}->{hostname})) {
53 $self->{output}->add_option_msg(short_msg => "Need to specify a hostname.");
54 $self->{output}->option_exit();
55 }
56 if (!defined($self->{option_results}->{username})) {
57 $self->{output}->add_option_msg(short_msg => "Need to specify a username.");
58 $self->{output}->option_exit();
59 }
60 if (!defined($self->{option_results}->{password})) {
61 $self->{output}->add_option_msg(short_msg => "Need to specify a password.");
62 $self->{output}->option_exit();
63 }
64
65 if (defined($self->{option_results}->{memory})) {
66 $self->{statefile_cache}->check_options(%options);
67 }
68
69 centreon::plugins::misc::check_security_command(
70 output => $self->{output},
71 command => $self->{option_results}->{command_plink}
72 );
73
74 $self->{option_results}->{command_plink} = 'plink'
75 if (!defined($self->{option_results}->{command_plink}) || $self->{option_results}->{command_plink} eq '');
76 }
77
78 sub run {
79 my ($self, %options) = @_;
80
81 ######
82 # Command execution
83 ######
84 my $cmd_in = 'show -o table -level all /SP/faultmgmt';
85 my $cmd = "echo '$cmd_in' | " . $self->{option_results}->{command_plink} . " -T -l '" . $self->{option_results}->{username} . "' -batch -pw '" . $self->{option_results}->{password} . "' " . $self->{option_results}->{hostname} . " 2>&1";
86 my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick(
87 command => $cmd,
88 timeout => $self->{option_results}->{timeout},
89 wait_exit => 1
90 );
91 $stdout =~ s/\r//g;
92 if ($lerror <= -1000) {
93 $self->{output}->output_add(severity => 'UNKNOWN',
94 short_msg => $stdout);
95 $self->{output}->display();
96 $self->{output}->exit();
97 }
98 if ($exit_code != 0) {
99 $stdout =~ s/\n/ - /g;
100 $self->{output}->output_add(severity => 'UNKNOWN',
101 short_msg => "Command error: $stdout");
102 $self->{output}->display();
103 $self->{output}->exit();
104 }
105
106 ######
107 # Command treatment
108 ######
109 my ($otp1, $otp2) = split(/\Q$cmd_in\E\n/, $stdout);
110 my $long_msg = $otp2;
111 $long_msg =~ s/\|/~/mg;
112
113 if (!defined($otp2) || $otp2 !~ /Target.*?Property.*?Value/mi) {
114 $self->{output}->output_add(long_msg => $stdout);
115 $self->{output}->output_add(severity => 'UNKNOWN',
116 short_msg => "Command '$cmd_in' problems (see additional info).");
117 $self->{output}->display();
118 $self->{output}->exit();
119 }
120
121 $self->{output}->output_add(long_msg => $long_msg);
122 if (defined($self->{option_results}->{memory})) {
123 $self->{statefile_cache}->read(statefile => 'cache_sun_mgmtcards_' . $self->{option_results}->{hostname} . '_' . $self->{mode});
124 $self->{output}->output_add(severity => 'OK',
125 short_msg => "No new problems on system.");
126 } else {
127 $self->{output}->output_add(severity => 'OK',
128 short_msg => "No problems on system.");
129 }
130
131 # Check showfaults
132 # Format:
133 # Target | Property | Value
134 #--------------------+------------------------+---------------------------------
135 #/SP/faultmgmt/0 | fru | /SYS
136 #/SP/faultmgmt/0/ | timestamp | Sep 20 08:08:07
137 # faults/0 | |
138 #/SP/faultmgmt/0/ | sp_detected_fault | Input power unavailable for PSU
139 # faults/0 | | at PS1
140
141 my @lines = split(/\n/, $otp2);
142 my $num_lines = $#lines;
143 my $datas = {};
144 if ($num_lines > 3) {
145 my $severity;
146
147 while ($otp2 =~ m/\s+timestamp\s+\|\s+(.*?)\n\s+faults\/([0-9]+)/mg) {
148 my $timestamp = $1;
149 $timestamp = centreon::plugins::misc::trim($1);
150 my $num = $2;
151
152 if (defined($self->{option_results}->{memory})) {
153 my $old_timestamp = $self->{statefile_cache}->get(name => "fault_$num");
154 if (!defined($old_timestamp) || $old_timestamp ne $timestamp) {
155 $severity = 'CRITICAL';
156 }
157 $datas->{"fault_$num"} = $timestamp;
158 } else {
159 $severity = 'CRITICAL';
160 }
161 }
162
163 if (defined($severity)) {
164 if (defined($self->{option_results}->{memory})) {
165 $self->{output}->output_add(severity => 'CRITICAL',
166 short_msg => "Some new errors on system (see additional info).");
167 } else {
168 $self->{output}->output_add(severity => 'CRITICAL',
169 short_msg => "Some errors on system (see additional info).");
170 }
171 }
172 }
173
174 if (defined($self->{option_results}->{memory})) {
175 $self->{statefile_cache}->write(data => $datas);
176 }
177
178 $self->{output}->display();
179 $self->{output}->exit();
180 }
181
182 1;
183
184 __END__
185
186 =head1 MODE
187
188 Check Sun C<T3-x>, C<T4-x> and C<T5xxx> management cards hardware (using C<ILOM>).
189
190 =over 8
191
192 =item B<--hostname>
193
194 Define the hostname to query.
195
196 =item B<--username>
197
198 Define the ssh username.
199
200 =item B<--password>
201
202 Define the ssh password.
203
204 =item B<--command-plink>
205
206 Define the C<plink> command (default: C<plink>).
207
208 =item B<--memory>
209
210 Returns new errors (retention file is used by the following option).
211
212 =item B<--timeout>
213
214 Timeout in seconds for the command (default: 30).
215
216 =back
217
218 =cut