| 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::dmx34::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} = '^(disk)$'; |
| 34 |
|
| 35 |
$self->{cb_hook1} = 'read_files'; |
| 36 |
$self->{cb_hook4} = 'send_email'; |
| 37 |
|
| 38 |
$self->{thresholds} = { |
| 39 |
director => [ |
| 40 |
['DD state', 'CRITICAL'], |
| 41 |
['Probe mode', 'CRITICAL'], |
| 42 |
['not comunicating', 'CRITICAL'], |
| 43 |
['unknown', 'CRITICAL'], |
| 44 |
['offline', 'OK'], |
| 45 |
['online', 'OK'], |
| 46 |
['not configured', 'OK'], |
| 47 |
], |
| 48 |
xcm => [ |
| 49 |
['emul', 'OK'], |
| 50 |
['.*', 'CRITICAL'], |
| 51 |
], |
| 52 |
memory => [ |
| 53 |
['OPER/OK', 'OK'], |
| 54 |
['\.\./.*', 'OK'], |
| 55 |
['.*', 'CRITICAL'], |
| 56 |
], |
| 57 |
fru => [ |
| 58 |
['OK', 'OK'], |
| 59 |
['.*', 'CRITICAL'], |
| 60 |
], |
| 61 |
}; |
| 62 |
|
| 63 |
$self->{components_path} = 'storage::emc::symmetrix::dmx34::local::mode::components'; |
| 64 |
$self->{components_module} = ['director', 'xcm', 'disk', 'memory', 'config', 'test', 'fru']; |
| 65 |
} |
| 66 |
|
| 67 |
sub check_options { |
| 68 |
my ($self, %options) = @_; |
| 69 |
$self->SUPER::check_options(%options); |
| 70 |
|
| 71 |
$self->{statefile_cache}->check_options(%options); |
| 72 |
} |
| 73 |
|
| 74 |
sub new { |
| 75 |
my ($class, %options) = @_; |
| 76 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, no_absent => 1); |
| 77 |
bless $self, $class; |
| 78 |
|
| 79 |
$options{options}->add_options(arguments => { |
| 80 |
'file-health:s' => { name => 'file_health' }, |
| 81 |
'file-health-env:s' => { name => 'file_health_env' }, |
| 82 |
# Email |
| 83 |
'email-warning:s' => { name => 'email_warning' }, |
| 84 |
'email-critical:s' => { name => 'email_critical' }, |
| 85 |
'email-smtp-host:s' => { name => 'email_smtp_host' }, |
| 86 |
'email-smtp-username:s' => { name => 'email_smtp_username' }, |
| 87 |
'email-smtp-password:s' => { name => 'email_smtp_password' }, |
| 88 |
'email-smtp-from:s' => { name => 'email_smtp_from' }, |
| 89 |
'email-smtp-options:s@' => { name => 'email_smtp_options' }, |
| 90 |
'email-memory' => { name => 'email_memory' }, |
| 91 |
}); |
| 92 |
|
| 93 |
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); |
| 94 |
$self->{components_exec_load} = 0; |
| 95 |
return $self; |
| 96 |
} |
| 97 |
|
| 98 |
sub read_files { |
| 99 |
my ($self, %options) = @_; |
| 100 |
|
| 101 |
if (!defined($self->{option_results}->{file_health}) || !defined($self->{option_results}->{file_health_env})) { |
| 102 |
$self->{output}->add_option_msg(short_msg => "Please set option --file-health and --file-health-env."); |
| 103 |
$self->{output}->option_exit(); |
| 104 |
} |
| 105 |
|
| 106 |
foreach (('file_health', 'file_health_env')) { |
| 107 |
$self->{'content_' . $_} = do { |
| 108 |
local $/ = undef; |
| 109 |
if (!open my $fh, "<", $self->{option_results}->{$_}) { |
| 110 |
$self->{output}->add_option_msg(short_msg => "Could not open file $self->{option_results}->{$_} : $!"); |
| 111 |
$self->{output}->option_exit(); |
| 112 |
} |
| 113 |
<$fh>; |
| 114 |
}; |
| 115 |
# We remove color syntax |
| 116 |
$self->{'content_' . $_} =~ s/\x{1b}\[.*?m|\r//msg; |
| 117 |
} |
| 118 |
|
| 119 |
# ***************************************************************** |
| 120 |
#* Health Check Run From Scheduler Version 2.0 * |
| 121 |
#* * |
| 122 |
#* Serial: 000290103984 Run Time: 03/24/2016 12:27:07 * |
| 123 |
#* Run Type: FULL Code Level: 5773-184-130 * |
| 124 |
#***************************************************************** |
| 125 |
|
| 126 |
my ($serial, $site) = ('unknown', 'unknown'); |
| 127 |
$serial = $1 if ($self->{content_file_health} =~ /Serial:\s*(\S+)/msi); |
| 128 |
|
| 129 |
$self->{output}->output_add(long_msg => sprintf('serial number: %s, site name: %s', $serial, $site)); |
| 130 |
} |
| 131 |
|
| 132 |
# |
| 133 |
# maybe we should add it in core (with cleaner code ;) |
| 134 |
# |
| 135 |
|
| 136 |
sub send_email { |
| 137 |
my ($self, %options) = @_; |
| 138 |
|
| 139 |
####### |
| 140 |
# Check SMTP options |
| 141 |
return if (!((defined($self->{option_results}->{email_warning}) && $self->{option_results}->{email_warning} ne '') |
| 142 |
|| (defined($self->{option_results}->{email_critical}) && $self->{option_results}->{email_critical} ne ''))); |
| 143 |
|
| 144 |
if (!defined($self->{option_results}->{email_smtp_host})) { |
| 145 |
$self->{output}->add_option_msg(short_msg => "Please set the --email-smtp-host option"); |
| 146 |
$self->{output}->option_exit(); |
| 147 |
} |
| 148 |
if (!defined($self->{option_results}->{email_smtp_from})) { |
| 149 |
$self->{output}->add_option_msg(short_msg => "Please set --email-smtp-from option"); |
| 150 |
$self->{output}->option_exit(); |
| 151 |
} |
| 152 |
|
| 153 |
my %smtp_options = ('-auth' => 'none'); |
| 154 |
if (defined($self->{option_results}->{email_smtp_username}) && $self->{option_results}->{email_smtp_username} ne '') { |
| 155 |
$smtp_options{-login} = $self->{option_results}->{email_smtp_username}; |
| 156 |
delete $smtp_options{-auth}; |
| 157 |
} |
| 158 |
if (defined($self->{option_results}->{email_smtp_username}) && defined($self->{option_results}->{email_smtp_password})) { |
| 159 |
$smtp_options{-pass} = $self->{option_results}->{email_smtp_password}; |
| 160 |
} |
| 161 |
|
| 162 |
####### |
| 163 |
# Get current data |
| 164 |
my $stdout; |
| 165 |
{ |
| 166 |
local *STDOUT; |
| 167 |
open STDOUT, '>', \$stdout; |
| 168 |
$self->{output}->display(force_long_output => 1); |
| 169 |
} |
| 170 |
|
| 171 |
$stdout =~ /^(.*?)(\||\n)/msi; |
| 172 |
my $subject = $1; |
| 173 |
my $status = lc($self->{output}->get_litteral_status()); |
| 174 |
|
| 175 |
foreach my $option (@{$self->{option_results}->{email_smtp_options}}) { |
| 176 |
next if ($option !~ /^(.+?)=(.+)$/); |
| 177 |
my ($label, $value) = ($1, $2); |
| 178 |
if ($label =~ /subject/i) { |
| 179 |
$value =~ s/%\{status\}/$status/g; |
| 180 |
$value =~ s/%\{short_msg\}/$subject/g; |
| 181 |
$label = lc($label); |
| 182 |
} |
| 183 |
$smtp_options{-$label} = $value; |
| 184 |
} |
| 185 |
|
| 186 |
my $send_email = 0; |
| 187 |
$send_email = 1 if ($status ne 'ok'); |
| 188 |
####### |
| 189 |
# Check memory file |
| 190 |
if (defined($self->{option_results}->{email_memory})) { |
| 191 |
$self->{new_datas} = { status => $status, output => $subject }; |
| 192 |
$self->{statefile_cache}->read(statefile => "cache_emc_symmetrix_dmx34_email"); |
| 193 |
my $prev_status = $self->{statefile_cache}->get(name => 'status'); |
| 194 |
my $prev_output = $self->{statefile_cache}->get(name => 'output'); |
| 195 |
# non-ok output is the same |
| 196 |
$send_email = 0 if ($status ne 'ok' && defined($prev_output) && $prev_output eq $subject); |
| 197 |
# recovery email |
| 198 |
$send_email = 1 if ($status eq 'ok' && defined($prev_status) && $prev_status ne 'ok'); |
| 199 |
$self->{statefile_cache}->write(data => $self->{new_datas}); |
| 200 |
} |
| 201 |
|
| 202 |
my $smtp_to = ''; |
| 203 |
$smtp_to = $self->{option_results}->{email_warning} if ($status eq 'warning' && defined($self->{option_results}->{email_warning} && $self->{option_results}->{email_warning}) ne ''); |
| 204 |
$smtp_to = $self->{option_results}->{email_critical} if ($status eq 'critical' && defined($self->{option_results}->{email_critical} && $self->{option_results}->{email_critical}) ne ''); |
| 205 |
if ($send_email == 1 && $status eq 'ok') { |
| 206 |
my $append = ''; |
| 207 |
foreach (('email_warning', 'email_critical')) { |
| 208 |
if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') { |
| 209 |
$smtp_to .= $append . $self->{option_results}->{$_}; |
| 210 |
$append .= ','; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if ($send_email == 0) { |
| 216 |
$self->{output}->add_option_msg(severity => 'OK', short_msg => "No email to send"); |
| 217 |
return ; |
| 218 |
} |
| 219 |
|
| 220 |
centreon::plugins::misc::mymodule_load( |
| 221 |
output => $self->{output}, |
| 222 |
module => 'Email::Send::SMTP::Gmail', |
| 223 |
error_msg => "Cannot load module 'Email::Send::SMTP::Gmail'." |
| 224 |
); |
| 225 |
my ($mail, $error) = Email::Send::SMTP::Gmail->new( |
| 226 |
-smtp => $self->{option_results}->{email_smtp_host}, |
| 227 |
%smtp_options |
| 228 |
); |
| 229 |
if ($mail == -1) { |
| 230 |
$self->{output}->add_option_msg(short_msg => "session error: " . $error); |
| 231 |
$self->{output}->option_exit(); |
| 232 |
} |
| 233 |
my $result = $mail->send( |
| 234 |
-to => $smtp_to, |
| 235 |
-from => $self->{option_results}->{email_smtp_from}, |
| 236 |
-subject => defined($smtp_options{-subject}) ? $smtp_options{-subject} : $subject, |
| 237 |
-body => $stdout, |
| 238 |
-attachments => $self->{option_results}->{file_health} . ',' . $self->{option_results}->{file_health_env} |
| 239 |
); |
| 240 |
$mail->bye(); |
| 241 |
if ($result == -1) { |
| 242 |
$self->{output}->add_option_msg(severity => 'UNKNOWN', short_msg => "problem to send the email"); |
| 243 |
} else { |
| 244 |
$self->{output}->add_option_msg(severity => 'OK', short_msg => "email sent"); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
1; |
| 249 |
|
| 250 |
__END__ |
| 251 |
|
| 252 |
=head1 MODE |
| 253 |
|
| 254 |
Check hardware. |
| 255 |
|
| 256 |
=over 8 |
| 257 |
|
| 258 |
=item B<--component> |
| 259 |
|
| 260 |
Which component to check (default: '.*'). |
| 261 |
Can be: 'director', 'xcm', 'disk', 'memory', 'config', 'fru', 'test' |
| 262 |
|
| 263 |
=item B<--filter> |
| 264 |
|
| 265 |
Exclude the items given as a comma-separated list (example: --filter=director --filter=xcm). |
| 266 |
You can also exclude items from specific instances: --filter=director,1 |
| 267 |
|
| 268 |
=item B<--no-component> |
| 269 |
|
| 270 |
Define the expected status if no components are found (default: critical). |
| 271 |
|
| 272 |
=item B<--threshold-overload> |
| 273 |
|
| 274 |
Use this option to override the status returned by the plugin when the status label matches a regular expression (syntax: section,[instance,]status,regexp). |
| 275 |
Example: --threshold-overload='director,CRITICAL,^(?!(online)$)' |
| 276 |
|
| 277 |
=item B<--warning> |
| 278 |
|
| 279 |
Set warning threshold for disk (syntax: type,regexp,threshold) |
| 280 |
Example: --warning='disk,.*,5:' |
| 281 |
|
| 282 |
=item B<--critical> |
| 283 |
|
| 284 |
Set critical threshold for disk (syntax: type,regexp,threshold) |
| 285 |
Example: --critical='disk,.*,3:' |
| 286 |
|
| 287 |
=item B<--file-health> |
| 288 |
|
| 289 |
The location of the global storage file status (Should be something like: C:/xxxx/HealthCheck.log). |
| 290 |
|
| 291 |
=item B<--file-health-env> |
| 292 |
|
| 293 |
The location of the environment storage file status (Should be something like: C:/xxxx/HealthCheck_ENV.log). |
| 294 |
|
| 295 |
=back |
| 296 |
|
| 297 |
=cut |