This article applies as of PRTG 22
I want to monitor data of my Exchange database other than the default sensors of PRTG provided. Is it possible to show database size, whitespace size, and search index size per mailbox database in PRTG?
Monitoring more Exchange database values using PowerShell script
The Exchange Database (PowerShell) sensor can monitor the size of a database and can show if it is valid and mounted. If you need more or other data of your Exchange database, you can set up your individual PowerShell script and monitor it with an EXE/Script Advanced sensor.
This is also what one of our customers did who needed the following data per mailbox database:
- Size of database
- Size of whitespace (free space within the database that is used before the edb itself grows)
- Size of search index
We are happy that this customer shared the respective PowerShell script (see below) with us. Thank you!
How to use the PowerShell script
- Open a text editor.
- Copy the source code from below and paste it into the editor.
- Adjust the connection URI at the beginning of the script so that it fits your Exchange server.
- Save the file with the extension .ps1, for example, ExchangeDBstat.ps1.
- Copy this file to your PRTG program directory, subfolder \Custom Sensors\EXEXML, on the probe system that monitors your Exchange server.
- In PRTG, add an EXE/Script Advancedsensor to the Exchange database device.
- Select the file you created (here: ExchangeDBstat.ps1) from the list of scripts.
- In the section Security Context, select the option Use Windows credentials of parent device.
- You can leave the other settings unchanged.
- Click Create to add the sensor. The sensor will start to show you mailbox database size, whitespace size, and search index size after a few moments.
Note: We do not offer support for custom sensors provided by customers. Of course, you are free to adjust the script according to your needs.
Script
#-----Please adjust to your Exchange server:
$CURI="http://exchangeserver.domain.tld/PowerShell/"
#--------------
Function SizeInBytes ($itemSizeString)
{
$posOpenParen = $itemSizeString.IndexOf("(") + 1
$numCharsInSize = $itemSizeString.IndexOf(" bytes") - $posOpenParen
$SizeInBytes = $itemSizeString.SubString($posOpenParen,$numCharsInSize).Replace(",","")
return $SizeInBytes
}
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $CURI -Authentication Kerberos
Import-PSSession $Session -DisableNameChecking
$dbs = Get-MailboxDatabase -Status
$result= "<?xml version=`"1.0`" encoding=`"Windows-1252`" ?>`r`n"
$result+="<prtg>`r`n"
foreach($db in $dbs)
{
$dbname=$db.name
$dbsize=SizeInBytes($db.DatabaseSize)
$whitespace=SizeInBytes($db.availablenewmailboxspace)
$edbFilePath = ("\\"+$db.ServerName+"\"+ $db.EdbFilePath.tostring().replace(":","$"))
$i = $edbFilePath.LastIndexOf('\')
$edbFilePath = $edbFilePath.Remove($i+1)
$guid = $db.Guid.ToString()
$dir = (get-childitem $edbFilePath | where { $_.Name.Contains($guid) })
$idxdir=$edbFilePath+$dir
$idxsize=(Get-ChildItem $idxdir | Measure-Object -Property Length -Sum).Sum
$result+=" <result>`r`n"
$result+=" <channel>DB-Size "+$dbname+"</channel>`r`n"
$result+=" <unit>BytesFile</unit>`r`n"
$result+=" <value>"+$dbsize+"</value>`r`n"
$result+=" </result>`r`n"
$result+=" <result>`r`n"
$result+=" <channel>Whitespace "+$dbname+"</channel>`r`n"
$result+=" <unit>BytesFile</unit>`r`n"
$result+=" <value>"+$whitespace+"</value>`r`n"
$result+=" </result>`r`n"
$result+=" <result>`r`n"
$result+=" <channel>Size Index "+$dbname+"</channel>`r`n"
$result+=" <unit>BytesFile</unit>`r`n"
$result+=" <value>"+$idxsize+"</value>`r`n"
$result+=" </result>`r`n"
}
$result+=" <text>OK</text>`r`n"
$result+="</prtg>`r`n"
$result
remove-pssession -session $Session
Exit 0
To monitor the Content Index State use this script. Remember to set each channel to use Value Lookup from prtg.standardlookups.exchangedag.contentindexstate so it shows the correct text values.
#-----Please adjust to your Exchange server:
$CURI = "http://exchangeserver.domain.tld/PowerShell/"
#--------------
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $CURI -Authentication Kerberos
Import-PSSession $Session -DisableNameChecking
$dbs = Get-MailboxDatabase -Status
$result= "<?xml version=`"1.0`" encoding=`"Windows-1252`" ?>`r`n"
$result+="<prtg>`r`n"
foreach($db in $dbs)
{
$dbname=$db.name
$idxstate=Get-MailboxDatabaseCopyStatus -Identity $dbname | Select -ExpandProperty ContentIndexState
$idxstatevalue =
switch ($idxstate)
{
"Healthy" {0}
"Crawling" {1}
default {2}
}
$result+=" <result>`r`n"
$result+=" <channel>Index State "+$dbname+"</channel>`r`n"
$result+=" <value>"+$idxstatevalue+"</value>`r`n"
$result+=" </result>`r`n"
}
$result+="</prtg>`r`n"
$result
remove-pssession -session $Session
exit 0
You can specify the lookup in the output from the script rather than needing to configure it on each channel:
$result+=" <result>`r`n"
$result+=" <channel>Index State "+$dbname+"</channel>`r`n"
$result+=" <value>"+$idxstatevalue+"</value>`r`n"
$result+=" <valuelookup>prtg.standardlookups.exchangedag.contentindexstate</valuelookup>`r`n"
$result+=" </result>`r`n"
More
Disclaimer:
The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.