-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd.py
125 lines (95 loc) · 3.34 KB
/
rrd.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
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
import time
import sys
from db_utils import DB
from buff_utils import trunc_to_min, wnd_1, wnd_24
class RRD:
def __init__(self,db_name=None):
if db_name is None:
db_name = 'rrd.db'
self.db = DB(db_name)
self.last_day = [None]*24
self.last_hour = [None]*60
t = time.time()
self.start_h, self.end_h = wnd_1(t)
self.start_d, self.end_d = wnd_24(t)
def update_time(func):
def inner(self,*args, **kwargs):
t = time.time()
self.start_h, self.end_h = wnd_1(t)
self.start_d, self.end_d = wnd_24(t)
return func(self,*args, **kwargs)
return inner
@update_time
def save(self,epoch,value):
"""Save new data received to database.
:param: epoch timestamp of data to be saved
:param: value float value to be saved
timestamp older than 24 hours or in future date
will be discarded.
"""
m = self.db.fetch_from_epoch('minutes',trunc_to_min(epoch))
if m is None and ( self._last_hour(epoch) or self._last_day(epoch)) :
self.db.insert('minutes',data=(trunc_to_min(epoch), value))
@update_time
def query(self,*args):
"""Query informations stored on the database.
Valid params are 'hours' or 'minutes'.
Parameter 'hours' return values store in the
last 24 hours, together with min value stored,
max value stored and average of the values for the
last 24 hours.
'minutes' parameter will return values store in the
last 60 minutes, together with min value stored,
max value stored and average of the values for the
last 60 minutes.
"""
if args[0] == 'minutes':
data = self.db.fetch_all('minutes',self.start_h,self.end_h)
elif args[0] == 'hours':
data = self.db.fetch_all('hours',self.start_d,self.end_d)
self.print_time_epoch(args[0], data)
data_float = [e[1] for e in data]
if len(data_float) > 0:
print "min:{} max:{} avg:{} ".format(min(data_float),max(data_float),sum(data_float)/len(data_float))
else:
print "Not enough informations to evaluate min, max, avg. (Empty sequence)"
def _last_hour(self,epoch):
epoch = trunc_to_min(epoch)
return int(self.end_h) <= int(epoch) <= int(self.start_h)
def _last_day(self,epoch):
epoch = trunc_to_min(epoch)
return int(self.end_d) <= int(epoch) <= int(self.start_d)
def print_time_epoch(self,table,data):
print "".format('-'*5,str(table.upper()),'-'*5)
for epoch,value in data:
print '({}): <{}> ==> {}'.format(epoch, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epoch)),value)
def print_usage():
"""Print help message for command line usage."""
help = """ OPTIONS
======================================================
save [epoch][value] ............. save value with epoch
query [hours] ................... query last 24 hours data saved
query [minutes] ................... query last 60 minutes data saved
"""
print help
if __name__ == "__main__":
usage_error = False
try:
cmd = sys.argv[1]
if cmd == 'save':
epoch = sys.argv[2]
value = sys.argv[3]
elif cmd == 'query':
param = sys.argv[2]
else:
print_usage()
usage_error = True
except IndexError:
usage_error = True
print_usage()
if not usage_error:
r = RRD()
if cmd == 'save':
r.save(float(epoch),float(value))
elif cmd == 'query':
r.query(param)