forked from juangranados/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_veeamjobs.ps1
116 lines (115 loc) · 3.99 KB
/
check_veeamjobs.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
<#
.SYNOPSIS
Check Veeam last result and last run of all jobs.
.DESCRIPTION
Check Veeam last result and last run of all jobs.
.OUTPUTS
OK: All jobs last result is Success.
CRITICAL: Any job last result is warning or any job last run less than warning hours ago.
CRITICAL: Any job last result is error or any job last run less than critical hours ago.
.PARAMETER warning
Warning hours after last backup.
Default 24.
.PARAMETER critical
Warning hours after last backup.
Default 48.
.PARAMETER jobs
Jobs names to check.
Default: all.
Example: "Tape Backup","Hyper-V","Incremental backup"
.EXAMPLE
Checks jobs by name with default warning treshold and 50h critical treshold.
check_diskdefragstatus.ps1 -jobs "Tape Backup","Full Backup","Hyper-V Backup" -critical 50
.EXAMPLE
Checks all jobs by name with 48h warning treshold and 96h critical treshold.
check_diskdefragstatus.ps1 -warning 48 -critical 96
.NOTES
Author: Juan Granados
#>
Param(
[Parameter(Mandatory = $false, Position = 0)]
[int]$warning = 24,
[Parameter(Mandatory = $false, Position = 1)]
[int]$critical = 48,
[Parameter(Mandatory = $false, Position = 2)]
[string[]]$jobs = "all"
)
#Requires -RunAsAdministrator
$ErrorActionPreference = "Stop"
$global:nagiosStatus = 0
$global:nagiosOutput = @()
$WarningPreference = 'SilentlyContinue'
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
function Get-JobStatus ([string]$name, [string]$result, [string]$state, [datetime]$lastRun) {
$jobInfo = "Name: $name - Result: $result - State: $state - Last run ending: $("$($lastRun.ToShortDateString()) at $($lastRun.ToShortTimeString())")."
Write-Verbose $jobInfo
# https://helpcenter.veeam.com/docs/backup/powershell/enums.html?ver=110#vbrsessionresult
if ($result -eq 'Failed' -or ($lastRun -lt (Get-Date).AddHours(-$critical))) {
$global:nagiosOutput += "Critical -> $jobInfo"
$global:nagiosStatus = 2
}
elseif ($result -eq 'Warning' -or ($lastRun -lt (Get-Date).AddHours(-$warning)) -or ($state -match "waiting")) {
$global:nagiosOutput += "Warning -> $jobInfo"
if ($global:nagiosStatus -eq 0) {
$global:nagiosStatus = 1
}
}
else {
$global:nagiosOutput += "Ok: -> $jobInfo"
}
Write-Verbose "Nagios output: $global:nagiosOutput"
Write-Verbose "Nagios status: $global:nagiosStatus"
}
try {
Write-Verbose "Getting jobs"
$computerJobs = Get-VBRJob
$tapeJobs = Get-VBRTapeJob
}
catch {
Write-Output "CRITICAL: $($_.Exception.Message)"
Exit(2)
}
if ($jobs -ne "all") {
foreach ($job in $jobs) {
if (!($computerJobs.Name -like $job) -and !($tapeJobs.Name -like $job)) {
Write-Output "CRITICAL: $job not detected"
Exit(2)
}
}
}
if ($computerJobs.Length -gt 0) {
foreach ($job in $computerJobs) {
if ($jobs -eq "all" -or $jobs -like $job.Name) {
Get-JobStatus $($job.Name) $($job.GetLastResult()) $($job.findlastsession().state) $($job.FindLastbasesession().EndTime)
}
}
}
$i=0
if ($tapeJobs.Length -gt 0) {
foreach ($job in $tapeJobs) {
if ($jobs -eq "all" -or $jobs -like $job.Name) {
Get-JobStatus $($job.Name) $($job.LastResult) $($job.LastState) $((Get-VBRSession -Job $TapeJobS[$i] -Last).EndTime)
}
$i++
}
}
if ($global:nagiosStatus -eq 2) {
Write-Output "CRITICAL -> One or more jobs are in critical state"
Write-Output "--------------------------------------------------"
Write-Output $global:nagiosOutput
Exit(2)
}
if ($global:nagiosStatus -eq 1) {
Write-Output "WARNING -> One or more jobs are in warning state"
Write-Output "------------------------------------------------"
Write-Output $global:nagiosOutput
Exit(1)
}
if ($global:nagiosOutput.Length -eq 0) {
Write-Output "UNKNOWN: No jobs found"
Exit(3)
}
Write-Output "OK -> All jobs are ok"
Write-Output "---------------------"
Write-Output $global:nagiosOutput
Exit(0)