-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_stats.py
executable file
·54 lines (41 loc) · 1.24 KB
/
bind_stats.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
#!/usr/bin/env python2
from rrdlib import rrdlib
import config
def get_last_stats():
with open(config.named_stats) as f:
stats = f.readlines()
last_line = stats[-1]
timestamp = last_line.split()[-1][1:-1]
search_line = last_line.replace("---", "+++")
first_line_number = stats.index(search_line)
return timestamp, stats[first_line_number:]
def build_dict(stats):
section = None
main_dict = dict()
for line in stats:
if line[0] == "[":
continue
if line[:3] == "++ ":
section = line[3:-3]
main_dict[section] = dict()
continue
if line[:4] in ["+++ ", "--- "]:
section = None
continue
line = line.split(" ", 1)
key = line[1]
value = line[0]
main_dict[section][key] = value
return main_dict
def main():
timestamp, stats = get_last_stats()
stats = map(lambda x: x.strip(), stats)
stats_dict = build_dict(stats)
for section in rrdlib.KEYINDEX:
content = stats_dict[section]
try:
rrdlib.rrd_update(section, content, timestamp)
except Exception, e:
print "Error %s: %s" % (section, e)
if __name__ == "__main__":
main()