Page 1 of 1

Extracting Perf Data from Powershell Output

Posted: Tue Mar 27, 2018 10:17 am
by IT-OPS-SYS
nsclient.ini
Hello everyone,

A colleague recently provided me with a PowerShell script to determine if any outgoing emails are being blocked by Office 365 from our Exchange server. I was able to tweak the script to return Nagios exit codes and attempted to code to enable performance data for graphing. The script runs successfully in Nagios but I do not see any graphs or performance data when viewing the host from the XI webpage. I tried looking online for performance data guides but I did not find any that went into details for a beginner. I looked at another plugin that provides graphing and mimicked it to no success. It seems that I'm missing something but can not figure out the last piece. I have attached the PowerShell code and the NSClient config file for review. Any help would be greatly appreciated.

Code: Select all

# Micrsoft Office 365 - Am I Blocked?
# NagiosXI Version 1.0
# Ovidiu Diaconeasa & Jose Lugo

if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.SnapIn -ErrorAction:SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
}

$NagiosStatus = "0"
$NagiosDescription = ""
$NagiosPerfData = ""

$failedlast15 = (get-messagetrackinglog -Start (Get-Date).AddMinutes(-15) -End (Get-Date) -eventid FAIL -result unlimited)
$failedcount = ($failedlast15 | Select EventId | Select-String -Pattern "FAIL" -AllMatches).count
 
$failedextern = ($failedlast15 | Select @{Name=’RecipientStatus’;Expression={[string]::join(“;”, ($_.RecipientStatus))}} | Select-String -Pattern "550 5.7.750" -AllMatches).count
 
# Query Blocked Emails - critical if greater than/equal to 5
				if ($failedextern -ge "5" ) 
				{
					# Format the output for Nagios
					if ($NagiosDescription -ne "") 
					{
						$NagiosDescription = "Microsoft Office 365 has blocked $failedcount emails."
					}
					# Set Nagios Alert to Critical.
					$NagiosStatus = "2"
		
# Query Blocked Emails - WARNING if over 1
				} 
				elseif ($failedextern -ge "1") 
				{
					# Format the output for Nagios
					if ($NagiosDescription -ne "") 	
					{
						$NagiosDescription = "Microsoft Office 365 has blocked $failedcount emails."
					}
				}

# What alerts to return to NagiosXI?
$NagiosPerfData = "|Blocked Emails=" + $failedcount + ";10;20;0"
$NagiosPerfData = $NagiosPerfData -replace " ", ""

if ($NagiosStatus -eq "2") 
{
	Write-Host "CRITICAL: " $NagiosDescription" "$NagiosPerfData
} 
elseif ($NagiosStatus -eq "1")
{
	Write-Host "WARNING: " $NagiosDescription" "$NagiosPerfData
} 
else 
{
	Write-Host "OK: All Microsoft Exchange Traffic Allowed."
}

exit $NagiosStatus

Re: Extracting Perf Data from Powershell Output

Posted: Tue Mar 27, 2018 11:46 am
by scottwilkerson
I cannot run the script, can you show what the plugin return when you run it?

Re: Extracting Perf Data from Powershell Output

Posted: Tue Mar 27, 2018 12:09 pm
by IT-OPS-SYS
Hi Scott,

The plugin returns an OK code. As far as I can tell, the power shell script itself runs with Nagios successfully. The plugin/script does not generate any performance data it seems.

[nagios@cvrmnagiosxi001 ~]$ /usr/local/nagios/libexec/check_nrpe -2 -P 8192 -H malex2013a -t 60 -c ExchangeRouting
OK: All Microsoft Exchange Traffic Allowed.

Re: Extracting Perf Data from Powershell Output

Posted: Tue Mar 27, 2018 12:15 pm
by scottwilkerson
Ok, the plugin isn't returning performance data

See the Nagios Plugin Development Guidelines section on performance data
https://nagios-plugins.org/doc/guidelines.html#AEN200

So what is echo'd back should contain a | and then perfdata

Code: Select all

OK: All Microsoft Exchange Traffic Allowed.|'label'=value[UOM];[warn];[crit];[min];[max]

Re: Extracting Perf Data from Powershell Output

Posted: Tue Apr 03, 2018 10:36 pm
by IT-OPS-SYS
Hi Scott,

This thread can be closed. The issue was with the powershell script itself having some erroneous characters from what I assume being copied/pasted while being worked on.

Re: Extracting Perf Data from Powershell Output

Posted: Wed Apr 04, 2018 8:33 am
by scottwilkerson
Glad you got it working!