PowerShell script exchange
Post Reply
lravelo
Influencer
Posts: 16
Liked: 2 times
Joined: Dec 28, 2017 8:04 pm
Full Name: Laz Ravelo
Location: Doral, FL
Contact:

Script for Tape Backup status - Nagios

Post by lravelo » 1 person likes this post

Hi guys,

I'm trying to modify an existing Nagios plug-in in order to be able to monitor my tape backup job but I'd like for some validation since there seems to some differences between the available properties VBRJob and VBRTapeJob. Here's the original script:

Code: Select all

#################################################################################
#################################################################################
####################  Made by Tytus Kurek on September 2012  ####################
#################################################################################
#################################################################################
###  This is a Nagios Plugin destined to check the last status and last run   ###
###        of Veeam Backup & Replication job passed as an argument.           ###
#################################################################################
#################################################################################

# Adding required SnapIn

asnp VeeamPSSnapin

# Global variables

$name = $args[0]
$period = $args[1]

# Veeam Backup & Replication job status check

$job = Get-VBRJob -Name $name
$name = "'" + $name + "'"

if ($job -eq $null)
{
    Write-Host "UNKNOWN! No such a job: $name."
exit 3
}

$status = $job.GetLastResult()

if($($job.findlastsession()).State -eq "Working"){
Write-Host "OK - Job: $name is currently in progress."
exit 0
}
if ($status -eq "Failed")
{
Write-Host "CRITICAL! Errors were encountered during the backup process of the following job: $name."
exit 2
}


if ($status -ne "Success")
{
Write-Host "WARNING! Job $name didn't fully succeed."
exit 1
}

# Veeam Backup & Replication job last run check

$now = (Get-Date).AddDays(-$period)
$now = $now.ToString("yyyy-MM-dd")
$last = $job.GetScheduleOptions()
$last = $last -replace '.*Latest run time: \[', ''
$last = $last -replace '\], Next run time: .*', ''
$last = $last.split(' ')[0]

#changed by DY on 11/04/2014 based on comment from cmot-weasel at http://exchange.nagios.org/directory/Plugins/Backup-and-Recovery/Others/check_veeam_backups/details
#if ($now -gt $last)
if((Get-Date $now) -gt (Get-Date $last))
{
Write-Host "CRITICAL! Last run of job: $name more than $period days ago."
exit 2
} 
else
{
Write-Host "OK! Backup process of job $name completed successfully."
exit 0
}
So far I've been able to write the following which works but I'd like to enrich:

Code: Select all

#Title:veeam_tape_backup.ps1
#Description:This is a Nagios plug-in that will check the last status and
#last run of a Veeam Backup & Replication tape job passed as 
#an argument.
#Author:Laz Ravelo
#Date:2018-01-10
#Version:1.0
#Usage:veeam_tape_backup.ps1 $name $period
#Notes:Credit goes to Tytus Kurek for originally creating the script
#that would check the same info for non-tape jobs in Veeam.
#=================================================================================

#Add the Veeam SnapIn
asnp VeeamPSSnapin

#Global Variables
$name = $args[0]
$period = $args[1]

#Get Tape Backup Job info

$job = Get-VBRTapeJob -Name $name
$name = "'" + $name + "'"

#Check if job argument is missing
if ($job -eq $null)
{
Write-Host "No such tape job: $name"
exit 3
}

#Check if period argument is missing
if ($period -eq $null)
{
Write-Host "Missing the period argument!"
exit 3
}

#Assign last result of job to variable
$status = $job | select -ExpandProperty "LastResult"

#Nagios output depending on status of last tape backup session
if ($status -eq "Working")
{
Write-Host "OK - Tape backup job: $name is currently in progress."
exit 0
}
if ($status -eq "Failed")
{
Write-Host "CRITICAL! Errors were encountered during the backup process of the following tape backup job: $name."
exit 2
}

if ($status -ne "Success")
{
Write-Host "WARNING! Tape backup job $name didn't fully succeed."
exit 1
}

#Date comparison
$now = (Get-Date).AddDays(-$period)
$now = $now.ToString("yyyy-MM-dd")
$last = get-vbrsession -job $job -Last | select -ExpandProperty "CreationTime"
$last = $last.ToString("yyyy-MM-dd")

#Throw warning if last backup job ran more than x days ago (depends on value of $period)
if((Get-Date $now) -gt (Get-Date $last))
{
Write-Host "CRITICAL! Last run of job $name happened more than $period days ago."
exit 2
}
else
{
Write-Host "OK! Tape backup job $name has completed successfully."
exit 0
}
The script appears to work but I'm not by any stretch a PowerShell guy so anyone can validate that the adjustments I made work exactly as the original (but in this case just for tape jobs), I would greatly appreciate it! :-)
lravelo
Influencer
Posts: 16
Liked: 2 times
Joined: Dec 28, 2017 8:04 pm
Full Name: Laz Ravelo
Location: Doral, FL
Contact:

Re: Script for Tape Backup status - Nagios

Post by lravelo » 1 person likes this post

I've added some additional code to match the section in the first script that satisfies that second argument that I'm missing in my first revision:

Code: Select all

#	Title:			veeam_tape_backup.ps1
#	Description:	This is a Nagios plug-in that will check the last status and
#					last run of a Veeam Backup & Replication tape job passed as 
#					an argument.
#	Author:			Laz Ravelo
#	Date:			2018-01-10
#	Version:		1.0
#	Usage:			veeam_tape_backup.ps1 $name $period
#	Notes:			Credit goes to Tytus Kurek for originally creating the script
#					that would check the same info for non-tape jobs in Veeam.
#=================================================================================

#	Add the Veeam SnapIn
asnp VeeamPSSnapin

#	Global Variables
$name = $args[0]
$period = $args[1]

#	Get Tape Backup Job info

$job = Get-VBRTapeJob -Name $name
$name = "'" + $name + "'"

#	Check if job argument is missing
if ($job -eq $null)
{
	Write-Host "No such tape job: $name"
	exit 3
}

#	Check if period argument is missing
if ($period -eq $null)
{
	Write-Host "Missing the period argument!"
	exit 3
}

#	Assign last result of job to variable
$status = $job | select -ExpandProperty "LastResult"

#	Nagios output depending on status of last tape backup session
if ($status -eq "Working")
{
	Write-Host "OK - Tape backup job: $name is currently in progress."
	exit 0
}
if ($status -eq "Failed")
{
	Write-Host "CRITICAL! Errors were encountered during the backup process of the following tape backup job: $name."
	exit 2
}

if ($status -ne "Success")
{
	Write-Host "WARNING! Tape backup job $name didn't fully succeed."
	exit 1
}

#	Date comparison
$now = (Get-Date).AddDays(-$period)
$now = $now.ToString("yyyy-MM-dd")
$last = get-vbrsession -job $job -Last | select -ExpandProperty "CreationTime"
$last = $last.ToString("yyyy-MM-dd")

#	Throw warning if last backup job ran more than x days ago (depends on value of $period)
if((Get-Date $now) -gt (Get-Date $last))
{
	Write-Host "CRITICAL! Last run of job $name happened more than $period days ago."
	exit 2
}
else
{
	Write-Host "OK! Tape backup job $name has completed successfully."
	exit 0
}
mikeely
Expert
Posts: 232
Liked: 71 times
Joined: Nov 07, 2016 7:39 pm
Full Name: Mike Ely
Contact:

Re: Script for Tape Backup status - Nagios

Post by mikeely »

This is friggin awesome. Thank you!
'If you truly love Veeam, then you should not let us do this :D' --Gostev, in a particularly Blazing Saddles moment
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests