forked from skyscrapers/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_duply
executable file
·128 lines (87 loc) · 3.95 KB
/
check_duply
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
# By Arne Schwabe <[email protected]>
# LICENSE: BSD
from subprocess import Popen,PIPE
import sys
import time
import os
import argparse
import re
import subprocess
from datetime import timedelta, datetime
def main():
parser = argparse.ArgumentParser(description='Nagios Duplicity status checker')
parser.add_argument("-w", dest="warninc", default=28, type=int,
help="Number of hours allowed for incremential backup warning level")
parser.add_argument("-W", dest="warnfull", default=40, type=int,
help="Number of days allowed for full backup warning level")
parser.add_argument("-c", dest="critinc", default=52, type=int,
help="Number of hours allowed for incremental backup critical level")
parser.add_argument("-C", dest="critfull", default=60, type=int,
help="Number of days allowed for full backup critical level")
parser.add_argument("-P", dest="profile", default="backup", type=str,
help="duply profile name")
args = parser.parse_args()
okay = 0
s = subprocess.Popen(["ps", "axw"],stdout=subprocess.PIPE)
for x in s.stdout:
if re.search("duply_" + args.profile, x):
pid = x.split(' ')[1]
p = subprocess.Popen(["ps", "-o", "lstart=", pid],stdout=subprocess.PIPE)
for y in p.stdout:
start_time = datetime.strptime(y.strip(), '%a %b %d %H:%M:%S %Y')
if start_time < datetime.utcnow() + timedelta(hours = -12):
print "CRITICAL: duply is backing up for more than 12 hours"
okay = 2
else:
print "OK: duply is backing up"
sys.exit(okay)
output , err = Popen(["sudo", "duply", args.profile, "status"], stdout=PIPE, stderr=PIPE , env={'HOME': '/root', 'PATH': os.environ['PATH']}).communicate()
#output = Popen(["/usr/local/bin/sudo", "/usr/local/sbin/duply", args.profile, "status"], stdout=PIPE, env={'HOME': '/root', 'PATH': os.environ['PATH']}).communicate()[0]
lastfull, lastinc = findlastdates(output)
sincelastfull = time.time() - lastfull
sincelastinc = time.time() - lastinc
msg = "OK: "
if sincelastfull > (args.warnfull * 24 * 3600) or sincelastinc > (args.warninc * 3600):
okay = 1
msg = "WARNING: "
if sincelastfull > (args.critfull * 24 * 3600) or sincelastinc > (args.critinc * 3600):
okay = 2
msg = "CRITICAL: "
if not checkoutput(output):
okay = max(okay,1)
msg = "WARNING: duplicity output: %s " % repr(output)
if err:
okay=2
msg = "Unexpected output: %s, " % repr(err)
print msg, "last full %s ago, last incremential %s ago|lastfull=%d, lastinc=%d" % ( formattime(sincelastfull), formattime(sincelastinc), sincelastfull, sincelastinc)
sys.exit(okay)
def checkoutput(output):
if output.find("No orphaned or incomplete backup sets found.")==-1:
return False
return True
def formattime(seconds):
days = seconds / (3600 * 24)
hours = seconds / 3600 % 24
if days:
return "%d days %d hours" % (days,hours)
else:
return "%d hours" % hours
def findlastdates(output):
lastfull =0
lastinc = 0
for line in output.split("\n"):
parts = line.split()
# ['Incremental', 'Sun', 'Oct', '31', '03:00:04', '2010', '1']
if len (parts) == 7 and parts[0] in ["Full","Incremental"]:
foo = time.strptime(" ".join(parts[1:6]),"%a %b %d %H:%M:%S %Y")
backuptime = time.mktime(foo)
if parts[0] == "Incremental" and lastinc < backuptime:
lastinc = backuptime
elif parts[0] == "Full" and lastfull < backuptime:
lastfull = backuptime
# Count a full backup as incremental backup
lastinc = max(lastfull,lastinc)
return (lastfull, lastinc)
if __name__=='__main__':
main()