forked from juangranados/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_wsb.ps1
62 lines (60 loc) · 2.53 KB
/
check_wsb.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
<#
.SYNOPSIS
Check Windows Server Backup last scheduled job status.
.DESCRIPTION
Check Windows Server Backup 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.
CRITICAL: Backup job failed.
.EXAMPLE
.\check_wsb.ps1 -Hours 96
.NOTES
Author: Juan Granados
#>
Param(
[Parameter(Mandatory=$false,Position=0)]
[ValidateNotNullOrEmpty()]
[int]$Hours=48
)
#Load PSSnapin for Windows 2008 / R2
$OperatingSystemVersion = (Get-WmiObject win32_operatingsystem).version
if (($OperatingSystemVersion -match "6.0.") -or ($OperatingSystemVersion -match "6.1.")) {
Add-PSSnapin windows.serverbackup
}
# Get backup status
try {
$BackupSummary = Get-WBSummary -ErrorAction Stop
} catch {
Write-Output "UNKNOWN: Could not get Windows Server Backup information. Try running in PowerShell console: Add-WindowsFeature -Name Backup-Tools | NumberOfVersions=0;;;;"
Exit(3)
}
if ($BackupSummary) {
# Check last backup
$LastSuccessfulBackupTime = ($BackupSummary.LastSuccessfulBackupTime).Date
# If there is a last backup
if ($LastSuccessfulBackupTime) {
# Get number of backup versions
$PerfmonOutput = " | NumberOfVersions=$($BackupSummary.NumberOfVersions);;;;"
# If last backup has been performed in time and its result is ok.
if ( (($BackupSummary.LastSuccessfulBackupTime).Date -ge (get-date).AddHours(-$($Hours))) -and $BackupSummary.LastBackupResultHR -eq '0') {
Write-Output "OK: last backup date $($BackupSummary.LastSuccessfulBackupTime). $($BackupSummary.NumberOfVersions) versions stored.$($PerfmonOutput)"
$host.SetShouldExit(0)
} else {
if ($BackupSummary.DetailedMessage){
Write-Output "CRITICAL: Last backup result on error: $($BackupSummary.DetailedMessage) Last successful backup date: $($BackupSummary.LastSuccessfulBackupTime).$($PerfmonOutput)"
} else {
Write-Output "CRITICAL: Last backup result on unspecified error. Last successful backup date: $($BackupSummary.LastSuccessfulBackupTime).$($PerfmonOutput)"
}
$host.SetShouldExit(2)
}
} else {
Write-Output "CRITICAL: There is not any successful backup yet. | NumberOfVersions=0;;;;"
Exit(2)
}
} else{
Write-Output "UNKNOWN: Could not get Windows Server Backup information. | NumberOfVersions=0;;;;"
Exit(3)
}