forked from jedda/OSX-Monitoring-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_time_machine_currency.sh
executable file
·64 lines (50 loc) · 2.13 KB
/
check_time_machine_currency.sh
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
#!/bin/bash
# Check Time Machine Currency
# by Jedda Wignall
# http://jedda.me
# v1.1 - 3 May 2012
# Cleaned up the output to provide a last backed up date. Error checking for non-supplied flags.
# v1.0 - 3 May 2012
# Initial release.
# This script checks the time machine results file on a Mac, and reports if a backup has completed within a number of minutes of the current time.
# Takes two arguments (warning threshold in minutes, critical threshold in minutes):
# ./check_time_machine_currency.sh -w 240 -c 1440
# Very useful if you are monitoring client production systems, and want to ensure backups are occurring.
while getopts "d:w:c:" optionName; do
case "$optionName" in
w) warnMinutes=( $OPTARG );;
c) critMinutes=( $OPTARG );;
esac
done
if [ "$warnMinutes" == "" ]; then
printf "ERROR - You must provide a warning threshold with -w!\n"
exit 3
fi
if [ "$critMinutes" == "" ]; then
printf "ERROR - You must provide a critical threshold with -c!\n"
exit 3
fi
lastBackupDateString=`defaults read /private/var/db/.TimeMachine.Results BACKUP_COMPLETED_DATE`
if [ "$lastBackupDateString" == "" ]; then
printf "CRITICAL - Time Machine has not completed a backup on this Mac!\n"
exit 2
fi
lastBackupDate=$(date -j -f "%Y-%m-%e %H:%M:%S %z" "$lastBackupDateString" "+%s" )
currentDate=$(date +%s)
diff=$(( $currentDate - $lastBackupDate))
warnSeconds=$(($warnMinutes * 60))
critSeconds=$(($critMinutes * 60))
if [ "$diff" -gt "$critSeconds" ]; then
printf "CRITICAL - Time Machine has not backed up since `date -j -f %s $lastBackupDate` (more than $critMinutes minutes)!\n"
exit 2
elif [ "$diff" -gt "$warnSeconds" ]; then
printf "WARNING - Time Machine has not backed up since `date -j -f %s $lastBackupDate` (more than $warnMinutes minutes)!\n"
exit 1
fi
if [ "$lastBackupDate" != "" ]; then
printf "OK - A Time Machine backup has been taken within the last $warnMinutes minutes.\n"
exit 0
else
printf "CRITICAL - Could not determine the last backup date for this Mac."
exit 2
fi