This post shows a Powershell script I use to monitor the status of my Exchange mailbox database copies and raises a critical alert if any of them is not "Mounted" or "Healthy".

Exchange Nagios Database Copies OK
Exchange Nagios Database Copies Alert

The script takes advantage of the Nagios passive checks and the configurations shown on my previous post "Nagios – Using passive checks without agent".

Here is the code (can also be downloaded here):

$SCRIPT_PATH="D:\SCRIPTS\NAGIOS\DatabaseCopy"
$NC_PATH="D:\SCRIPTS\NAGIOS"
$EXCHANGE_SERVER="MYEXCHANGESERVER"
$NAGIOS_SERVER="MYNAGIOSSERVER"
$NAGIOS_PORT="3333"

Get-MailboxDatabaseCopyStatus -Server $EXCHANGE_SERVER | fl status > $SCRIPT_PATH\TEMP.txt
$STATUS = Get-Content $SCRIPT_PATH\TEMP.txt

$DATETIMESTAMP = "[{0:G}]" -f [int][double]::Parse((Get-Date -UFormat %s))

$LASTRESULTFILE = "$SCRIPT_PATH\LASTRESULT.txt"
$RESULT = 0

foreach ($STAT in $STATUS)
{
	if ($STAT.Length -gt 0)
	{
		if ((-not $STAT.Endswith("Healthy")) -and (-not $STAT.Endswith("Mounted")))
		{
			$RESULT+=1
		}
	}
}

if ($RESULT -gt 0)
{
	$MSGTMP = " PROCESS_SERVICE_CHECK_RESULT;$EXCHANGE_SERVER;DatabaseCopy;2;There are $RESULT database copies in unexpected status"
}
else
{
	$MSGTMP = " PROCESS_SERVICE_CHECK_RESULT;$EXCHANGE_SERVER;DatabaseCopy;0;All database copies are OK"
}

$MSG = $DATETIMESTAMP + $MSGTMP
If (Test-Path $LASTRESULTFILE)
{
	$MSGTMP2 = Get-Content $LASTRESULTFILE
	if ($MSGTMP.CompareTo($MSGTMP2) -ne 0)
	{
		$MSG | $NC_PATH\nc.exe $NAGIOS_SERVER $NAGIOS_PORT
		$MSGTMP > $LASTRESULTFILE
	}
}
else
{
	$MSG | $NC_PATH\nc.exe $NAGIOS_SERVER $NAGIOS_PORT
	$MSGTMP > $LASTRESULTFILE
}

Note that you have to:

  • Configure the parameters (in red at the beginning of the script)
  • Configure a Nagios host and service (in this example the name of the service would DatabaseCopy) that accepts passive copies
  • Configure Nagios to accept passive checks remotely ("Nagios – Using passive checks without agent".)

Schedule it to be executed every some minutes and your database copies in this Exchange server are monitored.