Tuesday, July 30, 2013

Microsoft Exchange Queue monitoring Plugin for Nagios

# Test Queue Health
# Developed by Babu Dhinakaran S
# To execute from within NSClient++
#
#NSClient 0.4.1.90
#[/settings/external scripts/scripts]
#ps2 = cmd /c echo scripts\\test.ps1 | PowerShell.exe -command -

# On the check_nrpe command include the -t 30, since it takes some time to load the Exchange cmdlet's.

#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

#Add-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010



[int[]]$NagiosStatus = "3"
$NagiosDescription = ""
$key = New-Object System.Collections.ArrayList
$value = New-Object System.Collections.ArrayList
$getpos = New-Object System.Collections.ArrayList
$i=0

[String[]]$scontent = ""
[String[]]$shcontent = ""
[String[]]$otcontent = ""

#$Queue = Get-Queue -server test.test.COM


               
ForEach ($Queue in Get-Queue -Server test.test.COM)
{            
                     # ForEach Queue Handle the Identity and Message Count
                     $kv=$Queue.Identity
                     $vv=$Queue.MessageCount
                     $r_key = $key.Add("$kv")
                     $r_value = $value.Add("$vv")
}

 #Write-Host "Before Any Operation:"
#Write-Host $key
#Write-Host $value

ForEach ($scontent in $key)
{
            if ($scontent.contains("Submission"))
            {
#           Write-Host "removed Submission"
#           Write-Host "$i th Position:" $i
            break
            }
      $i++
}

#Process the submission queue and remove it from the ArrayList
[int[]]$sub_val = $value[$i]
if ($sub_val -gt "10")
{
# Format the output for Nagios
    if ($NagiosDescription -ne "")   
         {
        $NagiosDescription = $NagiosDescription + ", "                
            }
        $NagiosDescription = $NagiosDescription + $key[$i] + " queue has " + $value[$i] + " messages"
                                                
        # Set the status to failed.
        $NagiosStatus = "2"
}
elseif ($sub_val -gt "6")
{
    if ($NagiosDescription -ne "")   
         {
        $NagiosDescription = $NagiosDescription + ", "                
            }
        $NagiosDescription = $NagiosDescription + $key[$i] + " queue has " + $value[$i] + " messages"
                                               
        # Don't lower the status level if we already have a critical event
        if ($NagiosStatus -ne "2")
            {
           $NagiosStatus = "1"
            }
                               
}
$key.RemoveRange($i,1)
$value.RemoveRange($i,1)

#Write-Host "After Submission Operation:"
#Write-Host $key
#Write-Host $value

$i=0
ForEach ($shcontent in $key)
{
if ($shcontent.contains("Shadow"))
{
#Write-Host "Removed Shadow"

      #Process the Shadow queue and remove it from the ArrayList
      #Write-Host "Value of value:" $value[$i]
      [int[]]$shadow_value = $value[$i]
     
      if ( $shadow_value -gt "3" )
      {
      # Format the output for Nagios
      if ($NagiosDescription -ne "")   
                  {
                  $NagiosDescription = $NagiosDescription + ", "                
                  }
            $NagiosDescription = $NagiosDescription + $key[$i] + " queue has " + $value[$i] + " messages"
                                               
        # Set the status to failed.
        $NagiosStatus = "2"
      }
      elseif ($shadow_value -gt "2")
      {
      if ($NagiosDescription -ne "")   
            {
            $NagiosDescription = $NagiosDescription + ", "                
            }
            $NagiosDescription = $NagiosDescription + $key[$i] + " queue has " + $value[$i] + " messages"
                                               
        # Don't lower the status level if we already have a critical event
        if ($NagiosStatus -ne "2")
            {
           $NagiosStatus = "1"
            }
                               
      }
$r_getops = $getpos.Add("$i")

#$key.RemoveRange($i,1)
#$value.RemoveRange($i,1)

}
$i = $i + 1
}

#key length calculation
# write a subroutine to calculate string lenght
Function flength ([String[]]$skey)
{
ForEach ( $content in $skey )
{
$len = $len + 1
}
return $len - 1
}

ForEach ( $pos in $getpos )
{

# if the range is not null, call subroutine to calculate the length of key and get the position to delete it.
$kl = flength $key

if ( $pos -gt $kl )
      {
      $key.RemoveRange($kl,1)
      $value.RemoveRange($kl,1)
      break
      }
     
else
      {
      $key.RemoveRange($pos,1)
      $value.RemoveRange($pos,1)
      }
}

     
#Write-Host "After Shadow Operation:"
#Write-Host $key
#Write-Host $value

$i=0
ForEach ($otcontent in $key)
{

#Process the Other queue
[int[]]$ot_value = $value[$i]
if ($ot_value -gt "10")
{
# Format the output for Nagios
    if ($NagiosDescription -ne "")   
         {
        $NagiosDescription = $NagiosDescription + ", "                
            }
        $NagiosDescription = $NagiosDescription + $key[$i] + " queue has " + $value[$i] + " messages"
                                               
        # Set the status to failed.
        $NagiosStatus = "2"
}
elseif ($ot_value -gt "5")
{
    if ($NagiosDescription -ne "")   
         {
        $NagiosDescription = $NagiosDescription + ", "                 
            }
        $NagiosDescription = $NagiosDescription + $key[$i] + " queue has " + $value[$i] + " messages"
                                               
        # Don't lower the status level if we already have a critical event
        if ($NagiosStatus -ne "2")
            {
           $NagiosStatus = "1"
            }
                               
}
$i = $i + 1
}
# Output, what level should we tell our caller?
if ($NagiosStatus -eq "2") {
                Write-Host "CRITICAL: " $NagiosDescription
                        exit 2
} elseif ($NagiosStatus -eq "1") {
                Write-Host "WARNING: " $NagiosDescription
                        exit 1
} else {
                Write-Host "OK: All mail queues within limits."
                        exit 0
}

#Write-Host $key and $value
#Write-Host "After all Operation:"
#Write-Host $key

#Write-Host $value

1 comment:

  1. Use the below wrapper to invoke from the Nagios as Power Shell is unable to send exit codes to Nagios


    #Wrapper code for the power shell Queue monitoring
    #!/bin/bash
    #Developed By Babu Dhinakaran S and Ranjith Kumar Rajendran

    `/usr/local/nagios/libexec/check_nrpe -H $1 -t 60 -c queue > /usr/local/nagios/libexec/mqueue_result`
    result=`cat /usr/local/nagios/libexec/mqueue_result`
    stat=`cat /usr/local/nagios/libexec/mqueue_result | cut -d ":" -f 1`
    if [ $stat = "CRITICAL" ]
    then
    echo $result
    `rm -f /usr/local/nagios/libexec/mqueue_result`
    exit 2
    fi
    if [ $stat = "WARNING" ]
    then
    echo $result
    `rm -f /usr/local/nagios/libexec/mqueue_result`
    exit 1
    fi
    if [ $stat = "OK" ]
    then
    echo $result
    `rm -f /usr/local/nagios/libexec/mqueue_result`
    exit 0
    else
    echo "Unknown error Check with Babu or Ranjith :$result"
    `rm -f /usr/local/nagios/libexec/mqueue_result`
    exit 3
    fi

    ReplyDelete