summaryrefslogtreecommitdiff
path: root/rystamps.py
blob: 16b381e9fa670dcb32f0168ebdb4d5fd591ceba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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. <anrxc sysphere.org>

# 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()