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
Code: Select all
$USER1$/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$ $ARG2$
Code: Select all
$ARG1$: PSCheckDataStore
$ARG2$: -a "VMW_CMSAN_CM1_UTD202 (P123) VMFS6"
Code: Select all
PSCheckDataStore = cmd /c echo scripts\\DataStoreNagiosCheck.ps1 "$ARGS"; exit($lastexitcode) | powershell.exe -command -