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

143 lines
3.8 KiB
Perl
Raw Normal View History

#
2021-02-08 09:55:50 +01:00
# Copyright 2021 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.
#
2021-01-07 18:22:31 +01:00
package centreon::common::powershell::exchange::databases;
use strict;
use warnings;
2021-01-07 18:22:31 +01:00
use centreon::common::powershell::exchange::powershell;
2020-10-20 17:14:08 +02:00
use centreon::common::powershell::functions;
sub get_powershell {
2014-05-15 14:41:49 +02:00
my (%options) = @_;
# options: no_mailflow, no_mapi
2014-05-15 14:41:49 +02:00
my $no_mailflow = (defined($options{no_mailflow})) ? 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;
2021-01-07 18:22:31 +01:00
my $ps = centreon::common::powershell::exchange::powershell::powershell_init(%options);
2020-10-20 17:14:08 +02:00
$ps .= centreon::common::powershell::functions::escape_jsonstring(%options);
$ps .= centreon::common::powershell::functions::convert_to_json(%options);
2014-08-28 16:55:00 +02:00
$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
}
2020-10-20 17:14:08 +02:00
$items = New-Object System.Collections.Generic.List[Hashtable];
Foreach ($DB in $MountedDB) {
2020-10-20 17:14:08 +02:00
$item = @{}
2021-01-07 18:22:31 +01:00
';
if (defined($options{filter_server}) && $options{filter_server} ne '') {
$ps .= '
if (!($DB.Server.Name -match "' . $options{filter_server} . '")) {
continue
}
';
}
2020-10-20 17:14:08 +02:00
2021-01-07 18:22:31 +01:00
$ps .= '
2020-10-20 17:14:08 +02:00
$item.database = $DB.Name
$item.server = $DB.Server.Name
$item.mounted = $DB.Mounted
$item.size = $DB.DatabaseSize.ToBytes().ToString()
$item.asize = $DB.AvailableNewMailboxSpace.ToBytes().ToString()
2014-05-15 14:41:49 +02:00
';
2021-01-07 18:22:31 +01:00
2014-05-15 14:41:49 +02:00
if (defined($options{filter_database_test}) && $options{filter_database_test} ne '') {
$ps .= '
2021-01-07 18:22:31 +01:00
if (!($DB.Name -match "' . $options{filter_database_test} . '")) {
continue
}
2014-05-15 14:41:49 +02:00
';
}
$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
2020-10-20 17:14:08 +02:00
$item.mapi_result = $MapiResult.Result
';
2014-05-15 14:41:49 +02:00
}
if ($no_mailflow == 0) {
$ps .= '
# Test Mailflow
$MailflowResult = Test-mailflow -Targetdatabase $DB.Name
2020-10-20 17:14:08 +02:00
$item.mailflow_result = $MailflowResult.testmailflowresult
$item.mailflow_latency = $MailflowResult.MessageLatencyTime.TotalMilliseconds
';
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
2020-10-20 17:14:08 +02:00
$item.copystatus_indexstate = $CopyStatusResult.ContentIndexState.value__
$item.copystatus_content_index_error_message = $CopyStatusResult.ContentIndexErrorMessage
2015-08-24 14:50:21 +02:00
';
}
2014-05-15 14:41:49 +02:00
$ps .= '
}
2020-10-20 17:14:08 +02:00
$items.Add($item)
}
2020-10-20 17:14:08 +02:00
$jsonString = $items | ConvertTo-JSON-20 -forceArray $true
Write-Host $jsonString
exit 0
';
return $ps;
2014-05-15 14:41:49 +02:00
}
1;
__END__
=head1 DESCRIPTION
2021-01-07 18:22:31 +01:00
Method to check Exchange databases.
=cut