#!/usr/bin/env python # rystamps -- monitor script for rybackup, that notifies the systems # administrator in case of anomalies, that is if (UTC) # timestamps of backups are outdated by NN(h) # Copyright (C) 2011 Adrian C. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. from string import join from smtplib import SMTP from socket import getfqdn from os.path import getmtime from datetime import timedelta, datetime # Configuration # # Backups directory backups = "/mnt/backup" # Alarm thresholds per client (in hours) servers = { "apollo" : { "hourly" : 36, "daily" : 168, "weekly" : 720 }, "columbia" : { "hourly" : 24, "daily" : 48, "weekly" : 168, "monthly" : 720 }, } # Alerts mailing settings host = getfqdn() user = "root@%s" % host dest = "sysadmin@example.org" relay = "mail.example.com" # Functions # # Sending out alerts def mail(FROM, TO, SUBJECT, BODY): MSG = join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % SUBJECT, "", BODY), "\r\n") try: server = SMTP(relay) server.sendmail(FROM, TO, MSG) server.quit() except: pass # Timestamps comparison def timecomp(stamp, warn): timen = datetime.utcnow() timeb = datetime.fromtimestamp(stamp) if (timen - timeb) > timedelta(hours = warn): return True # Performing checks and parsing the output def main(): alarms = [] for s in servers.keys(): for ival in servers[s].keys(): try: mtime = getmtime("%s/%s/%s.1" % (backups, s, ival)) except OSError: continue if timecomp(int(mtime), servers[s][ival]) == True: tab = "\t" if len("%s %s.1" % (s, ival)) > 15 else "\t\t" alarms.append("%s %s.1%s last modified on: %s (UTC)" % ( s, ival, tab, datetime.fromtimestamp(int(mtime)))) if alarms: body = "Outdated backups detected:\n\n%s" % "\n".join(alarms) mail(user, dest, host+": backups timestamp status report", body) if __name__ == "__main__": main()