Installing Nagios on Ubuntu Server 11.10 then Monitoring Windows and Exchange Servers–Part 4–Custom Exchange Monitoring Scripts…
- In Part 1
I covered how you can install Nagios Core 3.3.1 onto an existing Ubuntu 11.10 server within your environment - In Part 2
I covered how you can setup basic monitoring on your first Windows Server – making use of the NSClient++, I also covered how you could setup a basic “check_nt” monitoring service - In Part 3
I covered how you can install the NRPE daemon onto your Nagios Server – and then use the NSClient to execute a basic Powershell script and report the output back into the Nagios interface
Custom Powershell for Exchange 2010 for use with Nagios
- You need to structure your scripts so that they always return an exit code which is proceeded by a Write-Host which adds a description to the last values returned (more on this later)
- If you operate an environment that has a mix of DAG and Basic Servers (DAG and non-DAG) or for that matter – servers which operate multiple roles (MBX,CAS,HT) code your scripts so they can handle the absence of any specific features for example; if you develop a script that is designed to operate on DAG based servers, you should code in logic that can handle DAG features nothing being available – as a sample see the “GetDagnfo.ps1” script which is available for download below
Powershell exit codes for Nagios
Powershell has the ability to terminate execution with an exit code – this is demonstrated here – you can within your own scripts return any numeric decimal code that you want by using the “exit” statement.
Exit code 0:
Exit code 2:
Ok so what is the Powershell script behind the Backup Monitoring?
Within the script there is a variable called $ThreshHold which reflects the number of days that can lapse without a full backup before the script will return an error to the Nagios Interface.
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 $localServerName = Get-WmiObject -Class Win32_ComputerSystem | Select Name $ThreshHold = 2 $Results = Get-MailboxDatabase -Server $localServerName.Name -Status | Select Identity,Server,LastFullBackup | where {$_.Server -eq $localServerName.Name} foreach($itm in $Results){ if($itm -eq $null){ $Output = "OK: No Databases are active on this host" $NagiosResult = 0 }else{ if($itm.LastFullBackup -eq $null){ $lastBackupSeed = 9999 }else{ $lastBackupSeed = New-TimeSpan $($itm.LastFullBackup) $(Get-Date) } if($lastBackupSeed.days -gt $ThreshHold -or $lastBackupSeed -eq 9999){ $Res = "CRITICAL: Database Backup out of Schedule: " + $itm.Identity $Output += $Res + " " $statFlag = 1 }else{ $Output += "OK: Database: " + $itm.identity + " has a recent backup" + " " } } } Write-Host $Output if($statFlag -eq 1){ exit 2 }else{ exit 0 }
Setting your scripts up
- On your Exchange Server where you have installed the NSClient++, Copy (or save) the scripts into the NSClient “Scripts” folder (this should be in “C:\Program Files\NSClient++\Scripts”).
- Perform the following within the NSC.ini file (this should be located in “C:\Program Files\NSClient++\”)For this example we will be using the “Exchange2010BackupMonitoring.ps1” script.
Within the NSC.ini find the section entitled [ NRPE Handlers ] then add the following command:
check_exBackup=cmd /c echo scripts\Exchange2010BackupMonitoring.ps1; exit($lastexitcode) | powershell.exe -command -
- From your Nagios Server using Filezilla – download a copy of the commands.cfg (located in /usr/local/nagios/etc/objects)
Open it within Notepad++ and add in the following command definition
define command{
command_name check_exbackup
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -t 120 -c check_exBackup
}
- From your Nagios Server using Filezilla – download a copy of the windows.cfg (located in /usr/local/nagios/etc/objects)
Open it within Notepad++ and add in the following Service Definition for your Exchange Host:
define service{
use generic-service
host_name prod-ex2010-01.prepad.local
service_description Exchange DBs on Host
check_command check_exdb
}
- Logon to your Nagios Server using PuTTY and from the command line enter in the following command:
sudo /etc/init.d/nagios restart
Sample Monitoring Script Downloads
Before you use them, you will need to ensure that you have created the relevant service and command definitions (described above).










