-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_veeam_backup.ps1
173 lines (149 loc) · 4.43 KB
/
check_veeam_backup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<#
.NAME
check_veeam_backup
.SYNOPSIS
Checks the backup
.SYNTAX
check_veeam_backup -Mode <job_status,host_backup>
.PARAMETER Mode
Choose between two modes:
job_status: Check if the given job was successful. (Default)
host_backup: Check if the last backup of the hosts in the job are corrupted or inconsistent.
. PARAMETER JobName
Name of the VEEAM Backup Job
. PARAMETER days_warning
Check if the job is older than the given days
. PARAMETER days_critical
Check if the job is older than the given days
#>
param(
[string] $Mode = 'job_status',
[string] $JobName,
[string] $Filter,
[int] $days_warning = 3,
[int] $days_critical = 5,
[switch] $Verbose
)
. "$PSScriptRoot\nagios-utils.ps1"
try {
Add-PSSnapin VeeamPSSnapin;
} catch {
Plugin-Exit $NagiosUnknown "Could not load VEEAM Backup SnapIn: $error"
}
try {
if ($Mode -eq 'job_status') {
$job = Get-VBRJob -Name "$JobName"
if ($job.IsRunning -eq $true ) {
Plugin-Exit $NagiosOK "Job is currently running: $JobName"
}
$n = Get-Date -Format "yyyy-MM-dd"
$l = $job.LatestRunLocal
$ts = (New-TimeSpan -Start $l -End $n).Days
if ($verbose){
write-Host "DayDIFF $ts Lastjob $l NOW: $n"
}
if ( $job.FindLastSession().result -ne 'success') {
Plugin-Exit $NagiosCritical "Last Job result failed: $JobName"
} Elseif ( $ts -gt $days_critical ){
Plugin-Exit $NagiosCritical "Last job run is $ts days old: $JobName"
} Elseif ( $ts -gt $days_warning ){
Plugin-Exit $NagiosWarning "Last job run is $ts days old: $JobName"
} else {
Plugin-Exit $NagiosOK "Last Job result was successful: $JobName"
}
}
} catch {
Plugin-Exit $NagiosUnknown "Get-VBRJob failed: $error"
}
try {
if ($verbose)
{
Write-Host "Mode=$Mode"
}
if ($Mode -eq 'host_backup')
{
[Array]$output = @()
$bkp_names = Get-VBRBackup -Name "$JobName" | Get-VBRRestorePoint -Name * | Select-Object Name -Unique
if ($verbose)
{
Write-Host $bkp_names
}
ForEach($n in $bkp_names)
{
$bkp = Get-VBRRestorePoint -Name $n.Name | Sort-Object –Property CreationTime –Descending | Select -First 1
$vm = $bkp.Name
if ($verbose)
{
write-Host "VM: $vm"
Write-Host "Corrupted: $($bkp.IsCorrupted)"
write-Host "Recheck: $($bkp.IsRecheckCorrupted)"
write-Host "Consistent: $($bkp.IsConsistent)"
}
if ($bkp.IsCorrupted -eq $true -or $bkp.IsRecheckCorrupted -eq $true -or $bkp.IsConsistent -ne $true ){
$failed = $true
$output += "$vm Last Backup is corrupted or not consistent."
} else {
$output += "$vm Last Backup is fine."
}
}
if ($failed -eq $true){
Plugin-Exit $NagiosCritical "Backups failed in job $JobName" ($output | out-String)
} else {
Plugin-Exit $NagiosOK "No Backups failed in job: $JobName" ($output | out-String)
}
}
} catch {
Plugin-Exit $NagiosUnknown "Get Backup Jobs failed: $error"
}
try {
if ($verbose)
{
Write-Host "Mode=$Mode"
}
if ($Mode -eq 'job_all')
{
[Array]$outputOK = @()
[Array]$outputCRT = @()
$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}
Foreach ($Job in $jobs)
{
$bkp = Get-VBRJob -Name $Job.Name | Sort-Object –Property CreationTime –Descending | Select -First 1
$Jobname = $bkp.name
if ($Filter)
{
$JobNew = "$Jobname" | Select-String -Pattern "$Filter" -CaseSensitive
if ($Jobname -eq $JobNew)
{
if ( $Job.FindLastSession().result -eq 'success')
{
$outputOK += "`nSuccess : $Jobname"
}
else {
$outputCRT += "`nCorrupted/Consistent : $Jobname"
$failed = $true
}
}
}
else
{
if ( $Job.FindLastSession().result -eq 'success')
{
$outputOK += "`nSuccess : $Jobname"
}
else {
$outputCRT += "`nCorrupted/Consistent : $Jobname"
$failed = $true
}
}
}
if ($failed -eq $true)
{
Plugin-Exit $NagiosCritical "There are some failed jobs.." ($outputCRT | out-String)
}
else {
Plugin-Exit $NagiosOK "All Jobs are fine..." ($outputOK | out-String)
}
}
} catch {
Plugin-Exit $NagiosUnknown "Get Backup Jobs failed: $error"
}