forked from juangranados/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_bejobs.ps1
74 lines (70 loc) · 2.08 KB
/
check_bejobs.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
<#
.SYNOPSIS
Check Backup Exec last scheduled job status.
.DESCRIPTION
Check Backup Exec last scheduled job status and returns Nagios output and code.
.PARAMETER Hours
Number of hours since now to check for backup jobs.
Default 48.
.OUTPUTS
OK: All last backups jobs within $Hours successful.
WARNING: Backup job status succeeded with exceptions.
CRITICAL: Backup job failed.
.EXAMPLE
.\check_bejobs.ps1 -Hours 96
.NOTES
Author: Juan Granados
Date: December 2017
#>
Param(
[Parameter(Mandatory=$false,Position=0)]
[ValidateNotNullOrEmpty()]
[int]$Hours=48
)
if (Get-Module -ListAvailable -Name BEMCLI) {
Import-Module BEMCLI
} else {
Write-Output "UNKNOWN: Module BEMCLI does not exist."
Exit 3
}
$OkStatus = 'Succeeded','Active'
$StartTime = Get-Date
$Jobs = Get-BEJobHistory | Where-Object { ($_.JobType -eq "Backup") -and ($_.StartTime -ge $StartTime.AddHours(-$Hours))} | Sort-Object -Property {$_.StartTime -as [datetime]} -Descending
$ExitCode = 0
$Output=""
$TotalDataSizeBytes = 0
$SuccessJobs = 0
$FailedJobs = 0
$JobsNames = New-Object System.Collections.ArrayList
foreach ($Job in $Jobs) {
if ( $JobsNames -contains $Job.Name) {
continue
} else {
$JobsNames.Add($Job.Name) | Out-Null
}
$TotalDataSizeBytes += $Job.TotalDataSizeBytes
if ($Job.JobStatus -eq "SucceededWithExceptions") {
if ($ExitCode -lt 1) {
$ExitCode = 1
}
$SuccessJobs++
} elseif (!($OkStatus -match $Job.JobStatus)) {
$ExitCode = 2
$FailedJobs++
} else {
$SuccessJobs++
continue
}
$Output += "$($Job.Name) exited with status $($Job.JobStatus) at $($Job.EndTime)."
}
$PerformanceOutput = " | SuccessJobs=$($SuccessJobs);;;; FailedJobs=$($FailedJobs);1;1;; BackupSize=$([math]::round($TotalDataSizeBytes/1GB,3))GB;;;;"
if ($ExitCode -eq 0) {
Write-Host "All last backups jobs within $($Hours) hours successful.$($PerformanceOutput)"
Exit(0)
} elseif ($ExitCode -eq 1) {
Write-Host "WARNING: $($Output)$($PerformanceOutput)"
Exit(1)
} else {
Write-Host "CRITICAL: $($Output)$($PerformanceOutput)"
Exit(2)
}