diff --git a/openvpn.py b/openvpn.py index 80a01ea..1e1bc5e 100644 --- a/openvpn.py +++ b/openvpn.py @@ -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: - with open(self.log_fp, "r") as log_f: - return log_f.read() + if not lines: + with open(self.log_fp, "r") as log_f: + 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 @@ -250,7 +260,7 @@ class Openvpn: except: pass shell = f"sg {group} -c \"echo \\$\\$ > {pid_fp}; exec {cmd}\"" - #print(shell) + # print(shell) proc = await asyncio.create_subprocess_shell( shell, stdout=asyncio.subprocess.PIPE, diff --git a/test.py b/test.py index edef45f..b831b97 100644 --- a/test.py +++ b/test.py @@ -28,12 +28,12 @@ class MainHandler(tornado.web.RequestHandler): buf += f"pid: {op.pids}
\n" buf += "log:
\n" buf += "
\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
\n" buf += "\n" self.write(buf)