centreon-plugins/centreon/common/powershell/exchange/2010/databases.pm

315 lines
12 KiB
Perl
Raw Normal View History

#
2018-01-29 14:11:11 +01:00
# Copyright 2018 Centreon (http://www.centreon.com/)
2015-07-21 11:51:02 +02:00
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package centreon::common::powershell::exchange::2010::databases;
use strict;
use warnings;
use centreon::plugins::misc;
2014-08-28 16:55:00 +02:00
use centreon::common::powershell::exchange::2010::powershell;
sub get_powershell {
2014-05-15 14:41:49 +02:00
my (%options) = @_;
# options: no_ps, no_mailflow, no_mapi
my $no_mailflow = (defined($options{no_mailflow})) ? 1 : 0;
my $no_ps = (defined($options{no_ps})) ? 1 : 0;
my $no_mapi = (defined($options{no_mapi})) ? 1 : 0;
2015-08-24 14:50:21 +02:00
my $no_copystatus = (defined($options{no_copystatus})) ? 1 : 0;
2014-05-15 14:41:49 +02:00
return '' if ($no_ps == 1);
2014-08-28 16:55:00 +02:00
my $ps = centreon::common::powershell::exchange::2010::powershell::powershell_init(%options);
$ps .= '
# Check to make sure all databases are mounted
2014-05-15 14:41:49 +02:00
try {
$ErrorActionPreference = "Stop"
';
if (defined($options{filter_database})) {
$ps .= '
$MountedDB = Get-MailboxDatabase -Identity "' . $options{filter_database} . '" -Status
';
} else {
$ps .= '
$MountedDB = Get-MailboxDatabase -Status
';
}
2014-05-15 14:41:49 +02:00
$ps .= '
} catch {
Write-Host $Error[0].Exception
exit 1
}
Foreach ($DB in $MountedDB) {
2014-05-16 10:15:49 +02:00
Write-Host "[name=" $DB.Name "][server=" $DB.Server "][mounted=" $DB.Mounted "][size=" $DB.DatabaseSize "][asize=" $DB.AvailableNewMailboxSpace "]" -NoNewline
2014-05-15 14:41:49 +02:00
';
if (defined($options{filter_database_test}) && $options{filter_database_test} ne '') {
$ps .= '
if (!($DB.Name -match "' . $options{filter_database_test} . '")) {
Write-Host "[Skip extra test]"
continue
}
';
}
$ps .= '
If ($DB.Mounted -eq $true) {
';
2014-05-15 14:41:49 +02:00
if ($no_mapi == 0) {
$ps .= '
# Test Mapi Connectivity
$MapiResult = test-mapiconnectivity -Database $DB.Name
Write-Host "[mapi=" $MapiResult.Result "]" -NoNewline
';
2014-05-15 14:41:49 +02:00
}
if ($no_mailflow == 0) {
$ps .= '
# Test Mailflow
$MailflowResult = Test-mailflow -Targetdatabase $DB.Name
2014-05-15 16:40:41 +02:00
Write-Host "[mailflow=" $MailflowResult.testmailflowresult "][latency=" $MailflowResult.MessageLatencyTime.TotalMilliseconds "]" -NoNewline
';
2014-05-15 14:41:49 +02:00
}
2015-08-24 14:50:21 +02:00
if ($no_copystatus == 0) {
$ps .= '
# Test CopyStatus
2015-08-24 15:47:44 +02:00
$tmp_name = $DB.Name + "\" + $DB.Server
$CopyStatusResult = Get-MailboxDatabaseCopyStatus -Identity $tmp_name
2015-08-24 14:50:21 +02:00
Write-Host "[contentindexstate=" $CopyStatusResult.ContentIndexState "][[contentindexerrormessage=" $CopyStatusResult.ContentIndexErrorMessage "]]" -NoNewline
';
}
2014-05-15 14:41:49 +02:00
$ps .= '
}
Write-Host ""
}
exit 0
';
2014-05-15 14:41:49 +02:00
return centreon::plugins::misc::powershell_encoded($ps);
}
sub check_mapi {
my ($self, %options) = @_;
if (defined($self->{option_results}->{no_mapi})) {
$self->{output}->output_add(long_msg => ' Skip MAPI test connectivity');
return ;
}
if ($options{line} !~ /\[mapi=(.*?)\]/) {
$self->{output}->output_add(long_msg => ' Skip MAPI test connectivity (information not found)');
return ;
}
$self->{data}->{mapi_result} = centreon::plugins::misc::trim($1);
$self->{output}->output_add(long_msg => " MAPI Test connectivity: " . $self->{data}->{mapi_result});
2014-05-15 14:41:49 +02:00
my ($status, $message) = ('ok');
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
2014-05-15 14:41:49 +02:00
if (defined($self->{option_results}->{critical_mapi}) && $self->{option_results}->{critical_mapi} ne '' &&
eval "$self->{option_results}->{critical_mapi}") {
$status = 'critical';
} elsif (defined($self->{option_results}->{warning_mapi}) && $self->{option_results}->{warning_mapi} ne '' &&
eval "$self->{option_results}->{warning_mapi}") {
$status = 'warning';
2014-05-15 14:41:49 +02:00
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status,
short_msg => sprintf("Server '%s' Database '%s' MAPI connectivity is %s",
$self->{data}->{server}, $self->{data}->{database}, $self->{data}->{mapi_result}));
2014-05-15 14:41:49 +02:00
}
}
sub check_mailflow {
my ($self, %options) = @_;
if (defined($self->{option_results}->{no_mailflow})) {
$self->{output}->output_add(long_msg => ' Skip Mailflow test');
return ;
}
if ($options{line} !~ /\[mailflow=(.*?)\]\[latency=(.*?)\]/) {
$self->{output}->output_add(long_msg => ' Skip Mailflow test (information not found)');
return ;
}
$self->{data}->{mailflow_result} = centreon::plugins::misc::trim($1);
my $latency = centreon::plugins::misc::trim($2);
$self->{output}->output_add(long_msg => " Mailflow Test: " . $self->{data}->{mailflow_result});
2014-05-15 14:41:49 +02:00
my ($status, $message) = ('ok');
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
2014-05-15 14:41:49 +02:00
if (defined($self->{option_results}->{critical_mailflow}) && $self->{option_results}->{critical_mailflow} ne '' &&
eval "$self->{option_results}->{critical_mailflow}") {
$status = 'critical';
} elsif (defined($self->{option_results}->{warning_mailflow}) && $self->{option_results}->{warning_mailflow} ne '' &&
eval "$self->{option_results}->{warning_mailflow}") {
$status = 'warning';
2014-05-15 14:41:49 +02:00
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status,
short_msg => sprintf("Server '%s' Database '%s' Mailflow test is %s",
$self->{data}->{server}, $self->{data}->{database}, $self->{data}->{mailflow_result}));
2014-05-15 14:41:49 +02:00
}
2014-05-15 16:40:41 +02:00
if ($latency =~ /^(\d+)/) {
$self->{output}->perfdata_add(label => 'latency_' . $self->{data}->{server} . '_' . $self->{data}->{database}, unit => 's',
2014-05-15 16:40:41 +02:00
value => sprintf("%.3f", $1 / 1000),
2014-05-15 14:41:49 +02:00
min => 0);
}
}
2015-08-24 14:50:21 +02:00
sub check_copystatus {
my ($self, %options) = @_;
if (defined($self->{option_results}->{no_copystatus})) {
$self->{output}->output_add(long_msg => ' Skip copy status test');
return ;
}
if ($options{line} !~ /\[contentindexstate=(.*?)\]\[\[contentindexerrormessage=(.*?)\]\]/) {
$self->{output}->output_add(long_msg => ' Skip copystatus test (information not found)');
return ;
}
($self->{data}->{copystatus_indexstate}, $self->{data}->{copystatus_indexerror}) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2));
$self->{output}->output_add(long_msg => " Copystatus state : " . $self->{data}->{copystatus_indexstate});
my ($status, $message) = ('ok');
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($self->{option_results}->{critical_copystatus}) && $self->{option_results}->{critical_copystatus} ne '' &&
eval "$self->{option_results}->{critical_copystatus}") {
$status = 'critical';
} elsif (defined($self->{option_results}->{warning_copystatus}) && $self->{option_results}->{warning_copystatus} ne '' &&
eval "$self->{option_results}->{warning_copystatus}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(severity => $status,
short_msg => sprintf("Server '%s' Database '%s' copystatus state is %s [error: %s]",
$self->{data}->{server}, $self->{data}->{database}, $self->{data}->{copystatus_indexstate}, $self->{data}->{copystatus_indexerror}));
}
}
sub check {
2014-05-15 14:41:49 +02:00
my ($self, %options) = @_;
# options: stdout
# Following output:
2014-05-16 10:15:49 +02:00
#[name= Mailbox Database 0975194476 ][server= SRVI-WIN-TEST ][mounted= True ][size= 136.1 MB (142,671,872 bytes) ][asize= 124.4 MB (130,482,176 bytes) ][mapi= Success ][mailflow= Success ][latency= 50,00 ]
2014-05-15 14:41:49 +02:00
#...
$self->{output}->output_add(severity => 'OK',
short_msg => 'Databases are mounted');
if (!defined($self->{option_results}->{no_mapi})) {
$self->{output}->output_add(severity => 'OK',
short_msg => 'MAPI Connectivities are ok');
}
if (!defined($self->{option_results}->{no_mailflow})) {
$self->{output}->output_add(severity => 'OK',
short_msg => 'Mailflow test are ok');
}
my $checked = 0;
foreach my $line (split /\n/, $options{stdout}) {
2014-05-16 10:15:49 +02:00
next if ($line !~ /^\[name=(.*?)\]\[server=(.*?)\]\[mounted=(.*?)\]\[size=(.*?)\]\[asize=(.*?)\]/);
2014-05-15 14:41:49 +02:00
$checked++;
$self->{data} = {};
($self->{data}->{database}, $self->{data}->{server}, $self->{data}->{mounted}, $self->{data}->{size}, $self->{data}->{asize}) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2),
2014-05-16 10:15:49 +02:00
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5));
2014-05-15 14:41:49 +02:00
$self->{output}->output_add(long_msg => sprintf("Test database '%s' server '%s':", $self->{data}->{database}, $self->{data}->{server}));
if ($self->{data}->{asize} =~ /\((.*?)\s*bytes/) {
my $free_bytes = $1;
$free_bytes =~ s/[.,]//g;
my $total_bytes;
if ($self->{data}->{size} =~ /\((.*?)\s*bytes/) {
$total_bytes = $1;
$total_bytes =~ s/[.,]//g;
my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_bytes);
$self->{output}->output_add(long_msg => sprintf(" Size %s", $total_value . ' ' . $total_unit));
}
my $used_bytes = $total_bytes - $free_bytes;
$self->{output}->perfdata_add(label => 'used_' . $self->{data}->{database}, unit => 'B',
value => $used_bytes,
min => 0, max => $total_bytes);
my ($used_value, $used_unit) = $self->{perfdata}->change_bytes(value => $used_bytes);
$self->{output}->output_add(long_msg => sprintf(" Used Size %s", $used_value . ' ' . $used_unit));
2014-05-16 10:15:49 +02:00
}
# Check mounted
if ($self->{data}->{mounted} =~ /False/i) {
2014-05-15 14:41:49 +02:00
$self->{output}->output_add(long_msg => sprintf(" not mounted\n Skip mapi/mailflow test"));
2015-08-20 12:23:38 +02:00
$self->{output}->output_add(severity => 'CRITICAL',
short_msg => sprintf("Database '%s' server '%s' is not mounted", $self->{data}->{database}, $self->{data}->{server}));
2014-05-15 14:41:49 +02:00
next;
}
$self->{output}->output_add(long_msg => sprintf(" mounted"));
check_mapi($self, line => $line);
check_mailflow($self, line => $line);
2015-08-24 14:50:21 +02:00
check_copystatus($self, line => $line);
2014-05-15 14:41:49 +02:00
}
if ($checked == 0) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => 'Cannot find informations');
}
}
1;
__END__
=head1 DESCRIPTION
Method to check Exchange 2010 databases.
=cut