Page 1 of 1

NRPE + Powershell script Not Graphing

Posted: Wed Dec 04, 2019 2:00 pm
by alsoszaa
I am using NRPE with a custom PowerShell script which works to provide the VMWare datastore percent free to a given datastore provided in in an argument within NagiosXI. However, I am not sure how to go about getting the percent free number to be graphed. I am 99% positive our Nagios uses HighCharts. I just need to know what I need to do in either the PowerShell script or the arguments within the nagios service I configured. Below are both provided so you can assist me. Please let me know if I am missing anything for you to assist. This works to give me the monitoring I need, but it does not provide me any graphs of historical reference. Graphing is what I need now.

PowerShell Script:

Code: Select all

$DAST = $args[0]

## BEGIN Global variables ##
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
## Reset Status and Description
$NagiosStatus = "0"
$NagiosDescription = ""
$datum=Get-date -Format 'MM-dd-yyyy hh:mm'

## END Global Variables ##

Import-Module DJATools -DisableNameChecking

Connect-vSphere 

## Define the datastores to watch

$datastore = get-datastore | Where-Object {$_.name -like $DAST} 

## Add percent free and date to arrays
$datastore | ForEach-Object{
    $_ | Add-Member -MemberType NoteProperty -Name "SampleDate" -Value $datum
    $PctMath =($_.FreeSpaceGB / $_.CapacityGB *100)
    $PctFree=("{0:f2}" -f $PctMath)
    $_ | Add-Member -MemberType NoteProperty -Name "PercentFree" -Value $PctFree
}

Disconnect-VIServer -Confirm:$false

try{
    $datastorename = $datastore | select-object -ExpandProperty Name
	$datastoresize = $datastore | Select-Object  -ExpandProperty PercentFree
	$comment = "% free space"
	
    if ($NagiosDescription -ne "") {
	    ## Format the output for Nagios
		$NagiosDescription = "" 
    }
	if ($datastoresize -lt 15) {
	    if ($datastoresize -lt 10) {
	    ## Status Critical when Less than 10% free (more than 90% used)
		$NagiosDescription = $NagiosDescription + $datastorename + " has " + $datastoresize + $comment
		## Set the status to Critical.
		$NagiosStatus = "2"
        Write-Host "CRITICAL: $NagiosDescription"
	    }
		else {
	        ## Status Warning when less than 15% free (more than 85% used)
            $NagiosDescription = $NagiosDescription + $datastorename + " has " + $datastoresize + $comment
			## Set the status to Warning.
			$NagiosStatus = "1"
            Write-Host "Warning: $NagiosDescription"			
		}
	}
	if ($datastoresize -ge 15) {
	    ## Status okay when greater than or equal to 15% free (less than or equal to 85% used)
	    $NagiosDescription = $NagiosDescription + $datastorename + " has " + $datastoresize + $comment
		## Set the status to Okay.
		$NagiosStatus = "0"
        Write-Host "OK: $NagiosDescription"
	}	
}
catch{ 
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    $UnknownState= "Yes"
    Write-Output "FAILED ITEM: '$FailedItem' ERROR MESSAGE: '$ErrorMessage'"
    $NagiosStatus = "3"
}

## Exit script and send status to Nagios
exit $NagiosStatus
Nagios Command:

Code: Select all

$USER1$/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$ $ARG2$
Nagios Arguments:

Code: Select all

$ARG1$: PSCheckDataStore
$ARG2$: -a "VMW_CMSAN_CM1_UTD202 (P123) VMFS6"
nsclient.ini (under [/settings/external scripts]):

Code: Select all

PSCheckDataStore = cmd /c echo scripts\\DataStoreNagiosCheck.ps1 "$ARGS";  exit($lastexitcode) | powershell.exe -command -

Re: NRPE + Powershell script Not Graphing

Posted: Wed Dec 04, 2019 2:22 pm
by mbellerue
Have you seen this document on performance data?
https://assets.nagios.com/downloads/nag ... fdata.html

The document is for Nagios Core, but it applies to XI. The output of your plugin should have the normal human readable message, followed by a | and then the performance data information.

Once you have that, you can start collecting and graphing the performance data.

Re: NRPE + Powershell script Not Graphing

Posted: Wed Dec 04, 2019 2:43 pm
by alsoszaa
Awesome! I added the pipe followed by the variable info to the Write-Host line and now I see the graph! I did this to OK, Warn, and Critical.

Code: Select all

Write-Host "OK: $NagiosDescription | PercentFree=$datastoresize"
Thank you so much for your help!

Re: NRPE + Powershell script Not Graphing

Posted: Wed Dec 04, 2019 4:05 pm
by mbellerue
You're welcome! I'll go ahead and close this thread. Feel free to open a new one if you encounter any trouble.