Debian Codegraph @

src/storage/emc/symmetrix/vmax/local/mode/hardware.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::symmetrix::vmax::local::mode::hardware;
22
23 use base qw(centreon::plugins::templates::hardware);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28 use centreon::plugins::statefile;
29
30 sub set_system {
31 my ($self, %options) = @_;
32
33 $self->{regexp_threshold_numeric_check_section_option} = '^(sparedisk)$';
34
35 $self->{cb_hook1} = 'read_files';
36 $self->{cb_hook4} = 'send_email';
37
38 $self->{thresholds} = {
39 default => [
40 ['Recoverable Error', 'OK'], # Fabric
41 ['Online', 'OK'],
42 ['Up', 'OK'],
43 ['OK', 'OK'],
44 ['.*', 'CRITICAL'],
45 ],
46 };
47
48 $self->{components_path} = 'storage::emc::symmetrix::vmax::local::mode::components';
49 $self->{components_module} = ['module', 'temperature', 'director', 'cabling', 'power', 'fabric', 'voltage', 'sparedisk'];
50 }
51
52 sub find_files {
53 my ($self, %options) = @_;
54
55 if (!opendir(DIR, $self->{option_results}->{health_directory})) {
56 $self->{output}->add_option_msg(short_msg => "Cannot open directory: $!");
57 $self->{output}->option_exit();
58 }
59
60 my $save_value = 0;
61 while (my $file = readdir(DIR)) {
62 next if (! -d $self->{option_results}->{health_directory} . '/' . $file ||
63 $file !~ /$self->{option_results}->{health_directory_pattern}/);
64 if (hex($1) > $save_value) {
65 $self->{option_results}->{file_health} = $self->{option_results}->{health_directory} . '/' . $file . '/' . $self->{option_results}->{file_health_name};
66 $self->{option_results}->{file_health_env} = $self->{option_results}->{health_directory} . '/' . $file . '/' . $self->{option_results}->{file_health_env_name};
67 $save_value = hex($1);
68 }
69 }
70
71 closedir(DIR);
72 }
73
74 sub check_options {
75 my ($self, %options) = @_;
76 $self->SUPER::check_options(%options);
77
78 $self->{statefile_cache}->check_options(%options);
79
80 if (!defined($self->{option_results}->{file_health_name}) || !defined($self->{option_results}->{file_health_env_name})) {
81 $self->{output}->add_option_msg(short_msg => "Please set option --file-health-name and --file-health-env-name.");
82 $self->{output}->option_exit();
83 }
84 if (!defined($self->{option_results}->{health_directory}) || ! -d $self->{option_results}->{health_directory}) {
85 $self->{output}->add_option_msg(short_msg => "Please set right option for --health-directory.");
86 $self->{output}->option_exit();
87 }
88 if (!defined($self->{option_results}->{health_directory_pattern})) {
89 $self->{output}->add_option_msg(short_msg => "Please set option for --health-directory-pattern.");
90 $self->{output}->option_exit();
91 }
92
93 $self->find_files();
94 }
95
96 sub new {
97 my ($class, %options) = @_;
98 my $self = $class->SUPER::new(package => __PACKAGE__, %options, no_absent => 1);
99 bless $self, $class;
100
101 $options{options}->add_options(arguments => {
102 'health-directory:s' => { name => 'health_directory' },
103 'health-directory-pattern:s' => { name => 'health_directory_pattern' },
104 'file-health-name:s' => { name => 'file_health_name', default => 'HealthCheck.log' },
105 'file-health-env-name:s' => { name => 'file_health_env_name', default => 'sympl_env_health.log' },
106 # Email
107 'email-warning:s' => { name => 'email_warning' },
108 'email-critical:s' => { name => 'email_critical' },
109 'email-smtp-host:s' => { name => 'email_smtp_host' },
110 'email-smtp-username:s' => { name => 'email_smtp_username' },
111 'email-smtp-password:s' => { name => 'email_smtp_password' },
112 'email-smtp-from:s' => { name => 'email_smtp_from' },
113 'email-smtp-options:s@' => { name => 'email_smtp_options' },
114 'email-memory' => { name => 'email_memory' },
115 });
116
117 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
118 $self->{components_exec_load} = 0;
119 return $self;
120 }
121
122 sub read_files {
123 my ($self, %options) = @_;
124
125 foreach (('file_health', 'file_health_env')) {
126 $self->{'content_' . $_} = do {
127 local $/ = undef;
128 if (!open my $fh, "<", $self->{option_results}->{$_}) {
129 $self->{output}->add_option_msg(short_msg => "Could not open file $self->{option_results}->{$_} : $!");
130 $self->{output}->option_exit();
131 }
132 <$fh>;
133 };
134 # We remove color syntax
135 $self->{'content_' . $_} =~ s/\x{1b}\[.*?m|\r//msg;
136 }
137
138 #Health Check Results Log:
139 #Service Processor Date: 06/15/2016 09:26:41
140 #Symmetrix Date from director 07c: 06/15/2016 09:33:45
141 #The time difference between Service Processor and Symmetrix is : 00:07:03.672
142 #System SN: 000292602920
143 #System Model: VMAX20K
144 #mCode Level: 5876.288
145 my ($serial) = ('unknown');
146 $serial = $1 if ($self->{content_file_health} =~ /System SN:\s*(\S+)/msi);
147
148 $self->{output}->output_add(long_msg => sprintf('serial number: %s', $serial));
149 }
150
151 #
152 # maybe we should add it in core (with cleaner code ;)
153 #
154
155 sub send_email {
156 my ($self, %options) = @_;
157
158 #######
159 # Check SMTP options
160 return if (!((defined($self->{option_results}->{email_warning}) && $self->{option_results}->{email_warning} ne '')
161 || (defined($self->{option_results}->{email_critical}) && $self->{option_results}->{email_critical} ne '')));
162
163 if (!defined($self->{option_results}->{email_smtp_host})) {
164 $self->{output}->add_option_msg(short_msg => "Please set the --email-smtp-host option");
165 $self->{output}->option_exit();
166 }
167 if (!defined($self->{option_results}->{email_smtp_from})) {
168 $self->{output}->add_option_msg(short_msg => "Please set --email-smtp-from option");
169 $self->{output}->option_exit();
170 }
171
172 my %smtp_options = ('-auth' => 'none');
173 if (defined($self->{option_results}->{email_smtp_username}) && $self->{option_results}->{email_smtp_username} ne '') {
174 $smtp_options{-login} = $self->{option_results}->{email_smtp_username};
175 delete $smtp_options{-auth};
176 }
177 if (defined($self->{option_results}->{email_smtp_username}) && defined($self->{option_results}->{email_smtp_password})) {
178 $smtp_options{-pass} = $self->{option_results}->{email_smtp_password};
179 }
180
181 #######
182 # Get current data
183 my $stdout;
184 {
185 local *STDOUT;
186 open STDOUT, '>', \$stdout;
187 $self->{output}->display(force_long_output => 1);
188 }
189
190 $stdout =~ /^(.*?)(\||\n)/msi;
191 my $subject = $1;
192 my $status = lc($self->{output}->get_litteral_status());
193
194 foreach my $option (@{$self->{option_results}->{email_smtp_options}}) {
195 next if ($option !~ /^(.+?)=(.+)$/);
196 my ($label, $value) = ($1, $2);
197 if ($label =~ /subject/i) {
198 $value =~ s/%\{status\}/$status/g;
199 $value =~ s/%\{short_msg\}/$subject/g;
200 $label = lc($label);
201 }
202 $smtp_options{-$label} = $value;
203 }
204
205 my $send_email = 0;
206 $send_email = 1 if ($status ne 'ok');
207 #######
208 # Check memory file
209 if (defined($self->{option_results}->{email_memory})) {
210 $self->{new_datas} = { status => $status, output => $subject };
211 $self->{statefile_cache}->read(statefile => "cache_emc_symmetrix_vmax_email");
212 my $prev_status = $self->{statefile_cache}->get(name => 'status');
213 my $prev_output = $self->{statefile_cache}->get(name => 'output');
214 # non-ok output is the same
215 $send_email = 0 if ($status ne 'ok' && defined($prev_output) && $prev_output eq $subject);
216 # recovery email
217 $send_email = 1 if ($status eq 'ok' && defined($prev_status) && $prev_status ne 'ok');
218 $self->{statefile_cache}->write(data => $self->{new_datas});
219 }
220
221 my $smtp_to = '';
222 $smtp_to = $self->{option_results}->{email_warning} if ($status eq 'warning' && defined($self->{option_results}->{email_warning} && $self->{option_results}->{email_warning}) ne '');
223 $smtp_to = $self->{option_results}->{email_critical} if ($status eq 'critical' && defined($self->{option_results}->{email_critical} && $self->{option_results}->{email_critical}) ne '');
224 if ($send_email == 1 && $status eq 'ok') {
225 my $append = '';
226 foreach (('email_warning', 'email_critical')) {
227 if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') {
228 $smtp_to .= $append . $self->{option_results}->{$_};
229 $append .= ',';
230 }
231 }
232 }
233
234 if ($send_email == 0) {
235 $self->{output}->add_option_msg(severity => 'OK', short_msg => "No email to send");
236 return ;
237 }
238
239 centreon::plugins::misc::mymodule_load(
240 output => $self->{output},
241 module => 'Email::Send::SMTP::Gmail',
242 error_msg => "Cannot load module 'Email::Send::SMTP::Gmail'."
243 );
244 my ($mail, $error) = Email::Send::SMTP::Gmail->new(
245 -smtp => $self->{option_results}->{email_smtp_host},
246 %smtp_options
247 );
248 if ($mail == -1) {
249 $self->{output}->add_option_msg(short_msg => "session error: " . $error);
250 $self->{output}->option_exit();
251 }
252 my $result = $mail->send(
253 -to => $smtp_to,
254 -from => $self->{option_results}->{email_smtp_from},
255 -subject => defined($smtp_options{-subject}) ? $smtp_options{-subject} : $subject,
256 -body => $stdout,
257 -attachments => $self->{option_results}->{file_health} . "," . $self->{option_results}->{file_health_env}
258 );
259 $mail->bye();
260 if ($result == -1) {
261 $self->{output}->add_option_msg(severity => 'UNKNOWN', short_msg => "problem to send the email");
262 } else {
263 $self->{output}->add_option_msg(severity => 'OK', short_msg => "email sent");
264 }
265 }
266
267 1;
268
269 __END__
270
271 =head1 MODE
272
273 Check hardware.
274
275 =over 8
276
277 =item B<--component>
278
279 Which component to check (default: '.*').
280 Can be: 'module', 'temperature', 'director, 'cabling', 'power', 'voltage', 'sparedisk'.
281
282 =item B<--filter>
283
284 Exclude the items given as a comma-separated list (example: --filter=temperature --filter=module).
285 You can also exclude items from specific instances: --filter=temperature,ES-PWS-A ES-4
286
287 =item B<--no-component>
288
289 Define the expected status if no components are found (default: critical).
290
291 =item B<--threshold-overload>
292
293 Use this option to override the status returned by the plugin when the status label matches a regular expression (syntax: section,[instance,]status,regexp).
294 Example: --threshold-overload='director,WARNING,^(?!(OK)$)'
295
296 =item B<--warning>
297
298 Set warning threshold for disk (syntax: type,regexp,threshold)
299 Example: --warning='sparedisk,.*,5:'
300
301 =item B<--critical>
302
303 Set critical threshold for disk (syntax: type,regexp,threshold)
304 Example: --critical='sparedisk,.*,3:'
305
306 =item B<--health-directory>
307
308 Location of health files.
309
310 =item B<--health-directory-pattern>
311
312 Set pattern to match the most recent directory (getting the hexa value).
313
314 =item B<--file-health-name>
315
316 Name of the global storage file status (default: HealthCheck.log).
317
318 =item B<--file-health-env-name>
319
320 Name of the environment storage file status (default: sympl_env_health.log).
321
322 =back
323
324 =cut