Skip to content

Instantly share code, notes, and snippets.

@azure365pro
Created May 13, 2020 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azure365pro/4fb5f90103f9db7f5e074e5d0d2afeb8 to your computer and use it in GitHub Desktop.
Save azure365pro/4fb5f90103f9db7f5e074e5d0d2afeb8 to your computer and use it in GitHub Desktop.
Basic Exchange Server Scripts for Check_mk Monitoring Solution
C:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe "C:\scripts\exchangehealth.ps1"
# Test Mailbox Database Health
#
# This script will look at all mailbox databases
# and determine the status of each.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Originally created by Jeff Roberson (jeffrey.roberson@gmail.com)
# at Bethel College, North Newton, KS
#
# Revision History
# 5/10/2010 Jeff Roberson Creation
# Revision History
# 05/08/2014 Satheshwaran Manoharan -Custom Output for Check_mk
# To execute from within NSClient++
#
#[NRPE Handlers]
#check_mailbox_health=cmd /c echo C:\Scripts\Nagios\MailboxHealth.ps1 | PowerShell.exe -Command -
#
# On the check_nrpe command include the -t 20, since it takes some time to load
# the Exchange cmdlet's.
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
$NagiosStatus = "0"
$NagiosDescription = ""
$MountedonServer = Get-MailboxDatabase -status
ForEach ($DataBase in Get-MailboxDatabase) {
ForEach ($Status in Get-MailboxDatabaseCopyStatus -Identity $DataBase.Name) {
switch ($Status.Status) {
"Failed" {
$NagiosStatus = "2"
if ($NagiosDescription -ne "") {
$NagiosDescription = $NagiosDescription + ", "
}
$NagiosDescription = $NagiosDescription + $Status.Name + " is " + $Status.Status
}
"Dismounted" {
$NagiosStatus = "2"
if ($NagiosDescription -ne "") {
$NagiosDescription = $NagiosDescription + ", "
}
$NagiosDescription = $NagiosDescription + $Status.Name + " is " + $Status.Status
}
"Resynchronizing" {
if ($NagiosStatus -ne "2") {
$NagiosStatus = "1"
}
if ($NagiosDescription -ne "") {
$NagiosDescription = $NagiosDescription + ", "
}
$NagiosDescription = $NagiosDescription + $Status.Name + " is " + $Status.Status
}
"Suspended" {
if ($NagiosStatus -ne "2") {
$NagiosStatus = "1"
}
if ($NagiosDescription -ne "") {
$NagiosDescription = $NagiosDescription + ", "
}
$NagiosDescription = $NagiosDescription + $Status.Name + " is " + $Status.Status
}
"Mounting" {
if ($NagiosStatus -ne "2") {
$NagiosStatus = "1"
}
if ($NagiosDescription -ne "") {
$NagiosDescription = $NagiosDescription + ", "
}
$NagiosDescription = $NagiosDescription + $Status.Name + " is " + $Status.Status
}
"Healthy" {}
"Mounted" {}
}
}
}
# Output, what level should we tell our caller?
if ($NagiosStatus -eq "2") {
Write-Host "2 ExchangeHealth V=1 CRITICAL: "$NagiosDescription""
} elseif ($NagiosStatus -eq "1") {
Write-Host "1 ExchangeHealth V=1 WARNING: "$NagiosDescription""
} else {
Write-Host "0 ExchangeHealth V=1 All Mailbox Databases are mounted and healthy."
# Write-Host "0 DB1 V=1 "($MountedonServer[0].Name)" is on "($MountedonServer[0].MountedonServer)""
# Write-Host "0 DB2 V=1 "($MountedonServer[1].Name)" is on "($MountedonServer[1].MountedonServer)""
}
exit $NagiosStatus
C:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe "C:\scripts\MailqueueHealth.ps1"
# Test Queue Health
#
# This script will look at each queue and determine the status of each.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
#
# History:
# Version 1.x
# Originally created by Joshua Kirkes (joshua@awesomejar.com)
# at AwesomeJar Consulting, LLC in San Francisco, CA
#
# Revision 1.1
# Created by Marc Koene
#
# Version 2.0 (with performance data)
# Created by Bastian W. [Bastian@gmx-ist-cool.de]
# Revision 2.2
# Created by Satheshwaran Manoharan [sathesh@careexchange.in]
# Revision History
# 2011-05-23 Joshua Kirkes Created 1.0
# 2011-07-07 Marc Koene Revision 1.1
# 2011-09-19 Bastian W. 2.0:
# * Added variable for the hubserver
# * checked if PSSnapin is loaded
#
# 2012-02-24 Bastian W. 2.1:
# * added Nagios performance counters
# 2014-05-08 Satheshwaran Manoharan 2.2:
# * Changed Result format for Check_mk
#
# To execute from within NSClient++
#
# [NRPE Handlers]
# check_exchange_mailqueue=cmd /c echo C:\Scripts\Nagios\NagiosMonitoring_ExchangeQueueHealth.ps1 | PowerShell.exe -Command -
#
# On the check_nrpe command include the -t 30, since it takes some time to load the Exchange cmdlet's.
if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}
$NagiosStatus = "0"
$NagiosDescription = ""
$NagiosPerfData = ""
ForEach ($Queue in Get-Queue -Server $env:computername | Where-Object{$_.identity -notlike "*\Shadow\*"})
{
# Look for lagged queues - critical if over 5
if ($Queue.MessageCount -gt "30" )
{
# Format the output for Nagios
if ($NagiosDescription -ne "")
{
# we already have a message queue description, so we will add a separator
$NagiosDescription = $NagiosDescription + ", "
}
$NagiosDescription = $NagiosDescription + $Queue.Identity + " queue has " + $Queue.MessageCount + " messages to " + $Queue.NextHopDomain
# Set the status to failed.
$NagiosStatus = "2"
# Look for lagged queues - warning if over 2
}
elseif ($Queue.MessageCount -gt "25")
{
# Format the output for Nagios
if ($NagiosDescription -ne "")
{
# we already have a message queue description, so we will add a separator
$NagiosDescription = $NagiosDescription + ", "
}
$NagiosDescription = $NagiosDescription + $Queue.Identity + " queue has " + $Queue.MessageCount + " messages to " + $Queue.NextHopDomain
# Don't lower the status level if we already have a critical event
if ($NagiosStatus -ne "2")
{
$NagiosStatus = "1"
}
}
}
# Output, what level should we tell our caller?
$NagiosPerfData = " | Queue=" + $Queue.MessageCount + " -Mails"
$NagiosPerfData = $NagiosPerfData -replace " ", ""
if ($NagiosStatus -eq "2")
{
Write-Host "2 MailQueue V=1 CRITICAL: " $NagiosDescription" "$NagiosPerfData
}
elseif ($NagiosStatus -eq "1")
{
Write-Host "1 MailQueue V=1 WARNING: " $NagiosDescription" "$NagiosPerfData
}
else
{
Write-Host "0 MailQueue V=1 OK: All mail queues within limits. "$NagiosPerfData
}
exit $NagiosStatus
###############
Place .bat files -
ExchangeHealth.bat
MailQueueHealth.bat
In this location
C:\Program Files (x86)\check_mk\local
##################
Place .ps1 files -
ExchangeHealth.ps1
MailQueueHealth.ps1
Note : Create a folder called scripts - if it doesnt exist
In this location
C:\scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment