Veeam Backup & Replication product is widely used for backuping virtual machines, primary hosted on VMware vSphere infrastructure.
It could also backup physical machines through dedicated agents.
It’s very important to keep the backup status monitored: it would be nice to get the backup status through the Icinga2 agent in an easy way.
Fortunately, Veeam provides us a powerful PowerShell interface and a huge set of commands which can be used to control jobs and get the actual status:
https://helpcenter.veeam.com/docs/backup/powershell/getting_started.html?ver=110
Our main goal is to write a script which will get the overall backup job status, possibly without specifying the Veeam backup job name list.
First of all, there is a command Get-VBRJob which returns the information about all the existing Veeam backup jobs: note that this command is limited to the backup to disks and it will skip the backup to tape jobs. In our monitoring script we must skip the disabled jobs, so we can filter the command as:
Get-VBRJob|where {$_.IsScheduleEnabled}
There are a couple of values in the command result which are very important:
$($job.findlastsession()).State
which will have the value “Working” for running backup jobs and “Idle” for continuous ones. We must skip both of them because they won’t return a valid status.
$job.GetLastResult()
which will return the result status of the last completed job.
Using a global variable to store the overall status of the backup to disk jobs (0 success, 1 warning, 2 failure), a first PowerShell check routine will be:
# Global variables
[int]$globalstatus = 0
[string]$globaljobs = ""
# Veeam Backup & Replication disk job status check
$jobs = Get-VBRJob|where {$_.IsScheduleEnabled}
foreach ($job in $jobs)
{
$status = $job.GetLastResult()
$state = $($job.findlastsession()).State
if(($state -eq "Working") -or ($state -eq "Idle")){continue}
if ($status -eq "Failed")
{
$globalstatus = 2
$globaljobs += $job.Name
$globaljobs += " "
}
if ($status -ne "Success")
{
if (-not ($globalstatus)) { $globalstatus = 1 }
$globaljobs += $job.Name
$globaljobs += " "
}
}
This routine will concatenate the job name of the ones in Warning or in Error state in a string $globaljobs
As said at the beginning of this blog, the Veeam backup to Tape uses different PowerShell commands and also the status variables are a little different from the previous ones.
The Veeam global status command for Tape jobs is Get-VBRTapeJob.
The last State and Status are directly exposed into the result, without the usage of additional functions:
$job.LastResult
$job.LastState
We can use the same logic as the previous routine to get the overall job status.
We can build a single PowerShell script to get the overall Veeam backup job status both for disk and tape jobs.
This script can be easily called by Icinga2 monitoring agent once a day or more frequently to get a quick overview of the Veeam backup status.
# Adding required SnapIn
Import-Module Veeam.Backup.PowerShell -DisableNameChecking
# Global variables
[int]$globalstatus = 0
[string]$globaljobs = ""
$ErrorActionPreference = 'SilentlyContinue'
# Veeam Backup & Replication job status check
$jobs = Get-VBRJob|where {$_.IsScheduleEnabled}
foreach ($job in $jobs)
{
$status = $job.GetLastResult()
$state = $($job.findlastsession()).State
if(($state -eq "Working") -or ($state -eq "Idle")){continue}
if ($status -eq "Failed")
{
$globalstatus = 2
$globaljobs += $job.Name
$globaljobs += " "
}
if ($status -ne "Success")
{
if (-not ($globalstatus)) { $globalstatus = 1 }
$globaljobs += $job.Name
$globaljobs += " "
}
}
# Veeam Backup & Replication TAPE job status check
$jobs = Get-VBRTapeJob|where {$_.Enabled}
foreach ($job in $jobs)
{
$status = $job.LastResult
$state = $job.LastState
if(($state -eq "Working") -or ($state -eq "Idle")){continue}
if ($status -eq "Failed")
{
$globalstatus = 2
$globaljobs += $job.Name
$globaljobs += " "
}
if ($status -ne "Success")
{
if (-not ($globalstatus)) { $globalstatus = 1 }
$globaljobs += $job.Name
$globaljobs += " "
}
}
if($globalstatus -eq 2)
{
Write-Host "CRITICAL! At least one backup job in error: $globaljobs"
}
else
{
if($globalstatus)
{
Write-Host "WARNING! At least one backup job in warning: $globaljobs"
}
else
{
Write-Host "OK! All backup jobs completed successfully."
}
}
exit ($globalstatus)