Report Job Status from vRanger to Nagios

I have been recently creating a number of external scripts within Nagios to bring together a number of services to be monitored. I am now looking at reporting the job status from vRanger backup jobs to Nagios based on the following criteria:

  • Report the last completed run of a backup job.
  • Report the job status and information to Nagios, where successful jobs are reported with the OK status and failed jobs with Critical status.

I will be able to do this by calling a number of cmdlets from the vRanger API Powershell snap-in, which by default is installed with vRanger.

Also the script will be required to return the status of multiple backup jobs, I do not want to create multiple scripts as well as multiple services within Nagios. Therefore, the script defined a mandatory parameter for the backup job name, which is called with the JobName argument.

Param ([parameter(Mandatory = $true)][string] $JobName)

As mentioned, previously we will be using the vRanger API Powershell snap-in to query the backup job status, so we will need to import the snap-ins to the current session:

if (-not (Get-PSSnapin vRanger.API.PowerShell -ErrorAction SilentlyContinue)) 
{ 
Add-PSSnapin vRanger.API.PowerShell > $null
}

Now, we need to filter the backup job name from the job template with the parameter specified.

$Template = Get-JobTemplate | Where-Object {$_.JobName -eq $JobName}

Now that we have the job name we will return the status from the Get-Job cmdlet by comparing the ParentJobTemplateId and the job template TemplateVersionId to return matching jobs where the status is completed and also return the most recent job history.

$Job = Get-Job | Where-Object {$_.ParentJobTemplateId -eq $Template.TemplateVersionID -and $_.JobState -eq "Completed"} | Select -Last 1

Now that we return the job status, we will need to generate return codes for the service status. The criteria as mentioned above is for jobs with the status success to be returned with the service status OK (0) and for jobs not reported as successful for this to be critical (2).

If ($Job.JobStatus -eq "Success")
{ 
$returncode = 0
} 

Else 
{ 
$returncode = 2
}

Finally I want to output the service status information for the backup job being monitored and exit the session returning the error code:

"" + $Job.JobState + " with " + $Job.JobStatus + " on " + $Job.CompletedOn
exit $returncode

One issue I found was that the vRanger API Powershell snap-in is only available in a 32-bit version and therefore requires the snap-in to be imported to the 32-bit version of Windows Powershell. Therefore, this requires the external script to call, the 32-bit executable from the check command:

check_vrangerbackupstatus= cmd /c echo scripts\Get-vRangerBackupStatus.ps1 -JobName "$ARG1$"; exit($lastexitcode) | %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -command -

While the script was created to be executed as an external script within Nagios, this can be run standalone from Windows Powershell as below.

%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -command ./Get-vRangerBackupStatus.ps1 -JobName <Job Name>

If your are looking to add external scripts to Nagios such as this one see the below link for more information;

https://deangrant.wordpress.com/2013/09/12/creating-and-running-external-scripts-within-nagios-xi/

The full Windows Powershell script can be downloaded from the below link:

https://app.box.com/s/3sgeu21nxxvv1oi02zte


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s