Advertisement
Guest User

Veeam: Check for failed backups with Nagios

a guest
Jun 10th, 2016
3,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #http://www.sightunseen.org/files/vm_backup_status.ps1
  2. asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue
  3.  
  4. ####################################################################
  5. # Configuration
  6. # vCenter server
  7. $vcenter = "vcenter.yourdomain.com"
  8. # To Exclude VMs from report add VM names to be excluded as follows
  9. $excludevms=@("")
  10.  
  11. ### reset variables for debugging only
  12. $backups_with_errors = $nul
  13. $number_of_backups_with_errors = $nul
  14. $failedbackups = $nul
  15. $backups_with_errors = $nul
  16. $numberofprotectedvms = $nul
  17. $vms=""
  18. ####################################################################
  19.  
  20. $vcenterobj = Get-VBRServer -Name $vcenter
  21. $backups_with_errors = $nul
  22.  
  23. # Build hash table with excluded VMs
  24. $excludedvms=@{}
  25. foreach ($vm in $excludevms) {
  26.     $excludedvms.Add($vm, "Excluded")
  27. }
  28.  
  29. # Get a list of all VMs from vCenter and add to hash table, assume Unprotected
  30. $vms=@{}
  31.  
  32. # only monitor those VMs on Production. To monitor all VMs registered on vCenter, just remove the and -match
  33. foreach ($vm in (Find-VBRObject -Server $vcenterobj -WarningAction "SilentlyContinue"  | Where-Object {$_.Type -eq "VirtualMachine"  -and $_.Datastores -match "DATASTORE_PRODUCTION" }))  {
  34.     if (!($excludedvms.ContainsKey($vm.Name) -or $vms.ContainsKey($vm.Name))) {
  35.         $vms.Add($vm.Name, "Unprotected")
  36.     }
  37. }
  38.  
  39. # Find all backup job sessions that have ended in the last 24 hours
  40.  
  41. $vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).addhours(-24)}
  42.  
  43. # Find all successfully backed up VMs in selected sessions (i.e. VMs not ending in failure) and update status to "Protected"
  44. foreach ($session in $vbrsessions) {
  45.     foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ })) {
  46.         if($vms.ContainsKey($vm.Name)) {
  47.             $vms[$vm.Name]="Protected"
  48.         }
  49.     }
  50. }
  51.  
  52. #$failedbackups=""
  53. #$number_of_backups_with_errors = 0
  54.  
  55.  
  56. # Output VMs in color coded format based on status.
  57. foreach ($vm in $vms.Keys)
  58. {
  59.   if ($vms[$vm] -eq "Protected") {
  60.       #write-host -foregroundcolor green "$vm is backed up"
  61.      
  62.            
  63.   } else {
  64.       #write-host -foregroundcolor red "$vm is NOT backed up"
  65.       $backups_with_errors = $true
  66.       $failedbackups=$failedbackups+":"+$vm
  67.       $number_of_backups_with_errors = $number_of_backups_with_errors+1
  68.   }
  69. }
  70.  
  71.  
  72. if ($backups_with_errors -eq $false){
  73.  $numberofprotectedvms = (Get-VBRRestorePoint | select {$_.VmName} -uniq).Count
  74.  Write-Host "OK: $numberofprotectedvms VMs are protected with Veeam"
  75.  exit 0
  76.  
  77. } else {
  78.  if ($number_of_backups_with_errors -eq $nul)
  79.  {Write-Host "No failed backups on DATASTORE_PRODUCTION."
  80.  exit 0
  81.  } else {
  82.    Write-Host "$number_of_backups_with_errors missing Veeam Backups $failedbackups"
  83.  }
  84.  exit 1
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement