我试图在python中读取每20秒更新一次的日志文件。到目前为止,我只能读取静态文件。我试图读取这些值,使用HighCharts在图表上绘制它们。我可以使用静态文件绘制值。但是我希望图表是实时的。有人可以帮我这个吗?
with open(self.filename) as f:
cpu = []
idle = []
wait = []
ld5 = []
ld10 = []
ld15 = []
load = []
swapu = []
swapf = []
#need to remove all the white space in the file
for line in f:
if line is not None:
stripped = line.strip()
dstat_tokens = self.whitespace_re.split(stripped)
#dstat_tokens = self.whitespace_re.split(stripped)
dstat_tokens = line.split(',')
(timestamp) = dstat_tokens[0:1]
(system,user,idl, wt, hwi, sfi) = dstat_tokens[1:7];
(diskread,diskwrite) = dstat_tokens[7:9]
(pagein,pageout) = dstat_tokens[9:11]
(load5,load10,load15) = dstat_tokens[11:14]
(used,buffers,cache,free) = dstat_tokens[14:18]
(swapused,swapfree) = dstat_tokens[18:20]
(interrupts,contextswitches) = dstat_tokens[20:22]
(receive,send) = dstat_tokens[22:24]
(runnable,uninterruptable,new) = dstat_tokens[24:27]
#print "Stripping %s" % timestamp[0]
ts = time.strptime(timestamp[0], "%d %b %Y %H:%M:%S CET")
dt = datetime.fromtimestamp(time.mktime(ts))
returned_time = dt.strftime("%d/%m/%Y %H:%M:%S")
#print returned_time
cpu.append({"ts": returned_time, "value": float(system)})
idle.append({"ts": returned_time, "value": float(idl)})
wait.append({"ts": returned_time, "value": float(wt)})
ld5.append({"ts": returned_time, "value": float(load5)})
ld10.append({"ts": returned_time, "value": float(load10)})
ld15.append({"ts": returned_time, "value": float(load15)})
load.append({"ts": returned_time, "value": float(load5)})
swapu.append({"ts": returned_time, "value": float(swapused)})
swapf.append({"ts": returned_time, "value": float(swapfree)})
self.data["cpu"] = cpu
self.data["idle"] = idle
self.data["load5"] = ld5
self.data["load10"] = ld10
self.data["load15"] = ld15
self.data["swapused"] = swapu
self.data["swapfree"] = swapf`
答案 0 :(得分:0)
我理解您的问题的方式是,您希望每次更新日志文件时都更新图表。您可以通过每次阅读时将文件的长度存储在txt文档中,然后每隔x
秒更新图表,检查txt文档并从中获取文件位置{{1}到那个位置。
seek()
我在我的python上生锈了,并且不记得如何准确地做到这一点,但这是一般的想法。如果有人知道如何实际执行此操作,请更新我的代码。如果您有任何问题,请告诉我,我会尝试澄清。