Skip to content

Instantly share code, notes, and snippets.

@juzam
Created May 18, 2019 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juzam/137baadaf67d5dadef3029e77aae5609 to your computer and use it in GitHub Desktop.
Save juzam/137baadaf67d5dadef3029e77aae5609 to your computer and use it in GitHub Desktop.
Nagios/Icinga Apple Time machine check
#!/bin/sh
# based on: https://www.reddit.com/r/macsysadmin/comments/6jvsyx/finding_date_of_last_time_machine_backup_in/djind3p/ and original post
# if mac os >= 10.9 pathPlistNew="/Library/Preferences/com.apple.TimeMachine.plist"
# otherwise it's pathPlistOld="/private/var/db/.TimeMachine.Results.plist
EXIT_OK=0;
EXIT_WARNING=1;
EXIT_CRITICAL=2;
EXIT_UNKNOWN=3;
WARN=$1
CRIT=$2
enabled=`/usr/bin/defaults read /Library/Preferences/com.apple.TimeMachine AutoBackup`
macOS=$(sw_vers | awk '/ProductVersion/{print substr($2,1,5)}' | tr -d ".")
pathPlistNew="/Library/Preferences/com.apple.TimeMachine.plist"
pathPlistOld="/private/var/db/.TimeMachine.Results.plist"
if [ "$macOS" -ge "109" ]; then
pathPlist=$pathPlistNew
else
pathPlist=$pathPlistOld
fi
if [ "$enabled" == "1" ];then
now=`date -j -f "%a %b %d %T %Z %Y" "$(date)" "+%s"`
lastBackupTimestamp=`date -j -f "%a %b %d %T %Z %Y" "$(/usr/libexec/PlistBuddy -c "Print Destinations:0:SnapshotDates" $pathPlist | tail -n 2 | head -n 1 | awk '{$1=$1};1')" "+%s"`
lastBackupFormattedTime=$(date -j -f "%s" $lastBackupTimestamp "+%Y-%m-%d %H:%M:%S")
delta=$(expr $now - $lastBackupTimestamp)
if [ $delta -gt $CRIT ]; then
echo "CRITICAL - latest backup: $lastBackupFormattedTime"
exit $EXIT_CRITICAL
elif [ $delta -gt $WARN ]; then
echo "WARNING - latest backup: $lastBackupFormattedTime"
exit $EXIT_WARNING
else
echo "OK - latest backup: $lastBackupFormattedTime"
exit $EXIT_OK
fi
else
echo "UNKNOWN - TM Disabled"
exit $EXIT_UNKNOWN
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment