Skip to content

Instantly share code, notes, and snippets.

@ryochin
Created August 1, 2022 03:54
Show Gist options
  • Save ryochin/eaee5e296780b90e036652861d0526ef to your computer and use it in GitHub Desktop.
Save ryochin/eaee5e296780b90e036652861d0526ef to your computer and use it in GitHub Desktop.
macOS: Simple Time Machine Backup Checker conformed to nagios/sensu/mackerel plugin format.
#!/usr/bin/env ruby
#
# you need add 'full disk access' privilege to /opt/homebrew/opt/mackerel-agent/bin/mackerel-agent.
class Main
require 'timeout'
CRITERIA = 3
def run
if time = latest_time
if time > Time.now - (CRITERIA * 60 ** 2).to_i
ok 'latest backup time is %s' % time
else
critical 'latest backup time is too old!: %s' % time
end
else
critical 'failed to get latest backup date!'
end
end
private
def ok(msg)
puts 'OK: %s' % msg
exit 0
end
def critical(msg)
puts 'CRITICAL: %s' % msg
exit 2
end
def latest_time
if /\A(\d{4})-(\d{2})-(\d{2})-(\d{6})\n\z/ =~ status_line
year, month, day = $1, $2, $3
hour, min, sec = $4.unpack('A2 A2 A2')
Time.local(sec, min, hour, day, month, year, nil, nil, false, nil)
end
end
def status_line
Timeout.timeout(15) do
`/usr/bin/tmutil latestbackup -t`
end
rescue Timeout::Error
nil
end
end
Main.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment