/
gen_online.py
39 lines (31 loc) · 966 Bytes
/
gen_online.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
#!/usr/bin/env python
import rrdtool , time , random, calendar
start_time = '2010-06-01 00:00'
start_time = time.strptime(start_time, "%Y-%m-%d %H:%M")
start_time = int(time.mktime(start_time))
fname = 'stc_online.rrd'
rrdtool.create(fname,
'--start', str(start_time),
'--step', '60',
'DS:online:GAUGE:60:0:U',
'RRA:LAST:0.5:1:1576800'
)
def round(timestamp):
reminder = timestamp % 60
timestamp = timestamp - reminder
if reminder > 30:
timestamp = timestamp + 60
return timestamp
def get_rounded_timestamp(date_string):
log_date = time.strptime(date_string, "%Y-%m-%d %H:%M:%S")
log_date = int(time.mktime(log_date))
return round(log_date)
f = file("reports.log")
for l in f:
log_date = get_rounded_timestamp(l[0:19]) + 60*60*8
data = eval(l[29:-1].strip())
s = '%d:%d' % (log_date, data["STCOnlineUsers"])
try:
rrdtool.update(fname , s)
except:
print l