using pytailer to improve log reading efficiency

This commit is contained in:
mantaohuang 2020-04-10 17:26:54 -04:00
parent b87bd9c2b8
commit c9e4f87056
2 changed files with 23 additions and 13 deletions

View File

@ -14,6 +14,7 @@ import humanize
from tornado.template import Template
import stat
import shutil
import tailer
def generate_config(in_fp, cfg):
@ -141,12 +142,15 @@ class Openvpn:
self.run_cmd(proxy_cmd, group=f"vpn{self.idx}")))
self.run_task.append(self.loop.create_task(self.monitor_task()))
def get_log(self):
def get_log(self, lines=10):
# regenerate log_fp
_ = self.get_cfg()
try:
if not lines:
with open(self.log_fp, "r") as log_f:
return log_f.read()
return log_f.readlines()
else:
return tailer.tail(open(self.log_fp, "r"), lines)
except:
return ""
@ -179,22 +183,28 @@ class Openvpn:
self.stop()
self.start()
def get_io_stat(self):
def get_io_stat(self, lines=5):
try:
return open(self.io_stat_fp, "r").read()
if not lines:
return open(self.io_stat_fp, "r").readlines()
else:
return tailer.tail(open(self.io_stat_fp, "r"), lines)
except:
return ""
def get_ping_stat(self):
def get_ping_stat(self, lines=5):
try:
return open(self.ping_stat_fp, "r").read()
if not lines:
return open(self.ping_stat_fp, "r").readlines()
else:
return tailer.tail(open(self.ping_stat_fp, "r"), lines)
except:
return ""
def get_latest_speed(self):
try:
buf = open(self.io_stat_fp, "r").readlines()
buf = [[float(i) for i in l.split(",")] for l in buf[-2:]]
buf = self.get_io_stat(2)
buf = [[float(i) for i in l.split(",")] for l in buf]
assert len(buf) >= 2, "not enough points, need >=2"
delta_t = buf[1][0] - buf[0][0]
down_speed = (buf[1][1] - buf[0][1])/delta_t

View File

@ -28,12 +28,12 @@ class MainHandler(tornado.web.RequestHandler):
buf += f"pid: {op.pids} <br/>\n"
buf += "log: <br/>\n"
buf += "<pre>\n"
buf += op.get_log()
buf += "\n".join(op.get_log(lines=10))
buf += f"\nSpeed: {op.get_latest_speed()}\n"
buf += "\nio_stat\n"
buf += "\n".join(op.get_io_stat().splitlines()[-5:])
buf += "\n".join(op.get_io_stat(lines=5))
buf += "\nping_stat\n"
buf += "\n".join(op.get_ping_stat().splitlines()[-5:])
buf += "\n".join(op.get_ping_stat(lines=5))
buf += "\n</pre>\n"
buf += "</body>\n</html>"
self.write(buf)