Last active
August 9, 2016 03:58
-
-
Save nmanzi/851b54370d9d2e6d8e962f823009b9ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding(SupportsShouldProcess=$False)] | |
Param ( | |
[parameter( | |
Mandatory=$true, | |
HelpMessage='Filter jobs by name')] | |
[string]$Filter, | |
[parameter( | |
Mandatory=$true, | |
HelpMessage='Mail server')] | |
[string]$SMTPServer, | |
[parameter( | |
Mandatory=$true, | |
HelpMessage='Sender email address')] | |
[string]$MailFrom, | |
[parameter( | |
Mandatory=$true, | |
HelpMessage='Alert destination email address')] | |
[array]$MailTo | |
) | |
function Send-Alert ($alert, $server, $from, $to) { | |
if ($server -and $from -and $to) { | |
$subject = "Tape Backup Job Wait Alert" | |
$to = ($to -join ',').ToString() | |
$mailMessage = New-Object System.Net.Mail.MailMessage | |
$mailMessage.subject = $subject | |
$mailMessage.to.add($to) | |
$mailMessage.from = $from | |
$mailMessage.body = $alert | |
$mailMessage.IsBodyHtml = $true | |
$smtp = New-Object System.Net.Mail.SmtpClient($server, "25"); | |
$smtpSendResult = 1 | |
Try { | |
$smtp.send($mailMessage) | |
} | |
Catch | |
{ | |
Write-Error -Message "E-Mail could not be sent" | |
$smtpSendResult = 0 | |
} | |
if ($smtpSendResult -eq 1) | |
{ | |
Write-Debug -Message "E-mail has been sent to the address(es): $to" | |
} | |
} | |
} | |
Add-PSSnapin VeeamPSSnapin -ErrorAction SilentlyContinue | |
If ((Get-PSSnapin "*Veeam*" -ErrorAction SilentlyContinue) -eq $null) | |
{ Write-Verbose "Unable to load Veeam snapin, you must run this on your Veeam backup server, and the Powershell snapin must be installed.`n`n" | |
Return 999 | |
} | |
$TapeJobs = Get-VBRTapeJob | ?{$_.name -match $Filter} | |
if ($TapeJobs) { | |
Write-Verbose "Tape jobs found, processing...`n`n" | |
foreach ($TapeJob in $TapeJobs) { | |
if ($TapeJob.LastState -ne "Stopped") { | |
$counter = 0 | |
do { | |
Write-Verbose ("Tape job '{0}' is running, waiting 60 seconds then checking again..." -f $TapeJob.Name) | |
Start-sleep -s 60 | |
$State = (Get-VBRTapeJob -name $TapeJob.Name).LastState | |
$counter += 1 | |
if ($counter -ge 360) { | |
Write-Verbose "Waiting for too long, send an alert" | |
$alert = "Tape backup job {0} has been holding a backup hostage for longer than 5 hours." | |
Send-Alert($alert, $SMTPServer, $MailFrom, $MailTo) | |
} | |
} | |
while ($State -ne "Stopped") | |
} | |
} | |
Write-Verbose "Nothing running, or whatever is running has stopped." | |
return 0 | |
} else { | |
Write-Verbose "No tape jobs found. Exiting." | |
return 0 | |
} | |
Write-Verbose "Something went wrong." | |
return 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment