From e8409298c4acad48a65d30ab3aa2ee09d39c5c88 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Tue, 12 Apr 2011 21:57:06 +0200 Subject: rystamps: imported first version Backups timestamps check script that notifies the systems administrator in case of anomalies, that is if (UTC) timestamps of backups are outdated by NN(h). --- rystamps.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 rystamps.py (limited to 'rystamps.py') diff --git a/rystamps.py b/rystamps.py new file mode 100755 index 0000000..9cc8429 --- /dev/null +++ b/rystamps.py @@ -0,0 +1,86 @@ +#!/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(): + mtime = getmtime("%s/%s/%s.1" % (backups, s, ival)) + + if timecomp(int(mtime), servers[s][ival]) == True: + alarms.append("%s %s.1\t last modified on: %s (UTC)" % ( + s, ival, 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() -- cgit v1.2.3