-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsnapshotruntimecollector.py
executable file
·69 lines (63 loc) · 2.47 KB
/
rsnapshotruntimecollector.py
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
#!/usr/bin/python
"""
rsnapshot_runtime_diamond.py
A script that parses rsnapshot logs to determine runtimes between
rsnapshot execution to it's completion.
"""
import os
import logging
from subprocess import Popen, PIPE
from datetime import datetime
import diamond.collector
class RsnapshotRuntimeCollector(diamond.collector.Collector):
def get_default_config_help(self):
"""
Seting up help values for custom entry
"""
config_help = super(RsnapshotRuntimeCollector,
self).get_default_config_help()
config_help.update({
'rsnap_log_home':
'Path to base directory of rsnap logs'
' name. Defaults to "%s"' % RSNAP_LOG_HOME,
})
return config_help
def get_default_config(self):
"""
Returns the default collector settings
"""
config = super(RsnapshotRuntimeCollector, self).get_default_config()
config.update({
'rsnap_log_home': None,
})
return config
def collect(self):
"""
Looping over all rsnap logs and publishing deltas
"""
date_format = "%d/%b/%Y:%H:%M:%S"
root_dir = self.config['rsnap_log_home']
metrics = {}
for log in sorted(os.listdir(root_dir)):
for line in reversed(open(os.path.join(root_dir, log))
.readlines()):
if '.pid' in line and 'rm' in line:
end_date = line.split()[0].strip("[").strip("]")
endd = datetime.strptime(end_date, date_format)
elif 'echo' in line and '.pid' in line:
start_date = line.split()[0].strip("[").strip("]")
startd = datetime.strptime(start_date, date_format)
break
if endd and startd:
duration = endd - startd
metric_value = abs(int((duration.days * 86400) +
duration.seconds))
metrics[os.path.splitext(log)[0]] = metric_value
elif startd:
endd = datetime.datetime.now(date_format)
duration = endd - startd
metric_value = abs(int((duration.days * 86400) +
duration.seconds))
metrics[os.path.splitext(log)[0]] = metric_value
for metric_name, metric_value in metrics.iteritems():
self.publish(metric_name, metric_value)