Skip to content

Instantly share code, notes, and snippets.

@kannan4k
Last active August 29, 2015 14:19
Show Gist options
  • Save kannan4k/1ddfa7350ee56d76a3db to your computer and use it in GitHub Desktop.
Save kannan4k/1ddfa7350ee56d76a3db to your computer and use it in GitHub Desktop.
Backup health check
#!/usr/bin/env python
import os
import sys
import datetime
#rsync config path
rsync_secrets = "/etc/rsyncd.lg-secrets"
backup_root = "/backup"
# Exit statuses recognized by Nagios
UNKNOWN = -1
OK = 0
WARNING = 1
CRITICAL = 2
#CUTOFFHOURS To create warning and critical error
CUTOFFHOUR_WARN = 48
CUTOFFHOUR_CRIT = 72
message = ""
CRIT_SERVERS = []
WARN_SERVERS = []
seconds_per_hour = 3600.0
nagios_templates = {'OK':'%s', 'WARNING':'%s', 'CRITICAL':'%s'}
nagios_exit_codes = {'OK':0,'WARNING':1,'CRITICAL':2,'UNKNOWN':3}
def nagios_print_and_exit(state, message):
print "%s: %s" % (state, message)
sys.exit(nagios_exit_codes[state])
SERVERS = [line.strip().split(':')[0] for line in open(rsync_secrets) if line]
def main():
for server in SERVERS:
# Determine the age of the path in hours.
path = os.path.join(backup_root, server)
if os.path.exists(path):
path_stat = max(os.stat(root).st_mtime for root,_,_ in os.walk(path))
path_ctime = datetime.datetime.fromtimestamp(path_stat)
path_age = ((datetime.datetime.now() - path_ctime).total_seconds()) / seconds_per_hour
#print path, path_age
if path_age > CUTOFFHOUR_CRIT:
CRIT_SERVERS.append(server)
elif path_age > CUTOFFHOUR_WARN:
WARN_SERVERS.append(server)
else:
current_state = 'OK'
if CRIT_SERVERS:
message = "BACKUP CRIT:" + ','.join(CRIT_SERVERS)
current_state = 'CRITICAL'
if WARN_SERVERS:
message += " Additional Warning:" + ','.join(WARN_SERVERS)
elif WARN_SERVERS:
message = "BACKUP WARN:" + ','.join(WARN_SERVERS)
current_state = 'WARNING'
else:
current_state = 'OK'
message = "BACKUP Health is OK"
nagios_print_and_exit(current_state, nagios_templates[current_state] % message)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment