Skip to content

Commit

Permalink
Added rudimentary support for TUI in netstatus
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelykalman committed Mar 2, 2022
1 parent 68af025 commit 9338b3e
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 13 deletions.
68 changes: 68 additions & 0 deletions src/torspray/modules/tui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import curses


class TUI:
def __init__(self):
self.stdscr = None
self.lines = 1

def __getmax(self):
y, x = self.stdscr.getmaxyx()
return x, y

def __printat(self, x, y, line):
self.stdscr.addstr(y, x, line)

def start(self):
self.stdscr = curses.initscr()
curses.start_color()
self.stdscr.nodelay(True)
self.stdscr.keypad(True)

def stop(self):
curses.endwin()

def resetlines(self):
self.lines = 1
self.stdscr.border()

def clear(self):
self.stdscr.clear()

def getch(self):
keycode = self.stdscr.getch()
if keycode == -1:
pass
else:
c = chr(keycode)
self.print("{} {} {}".format(c, keycode, type(keycode)))
if c in ("Q", "q"):
raise KeyboardInterrupt

def print(self, line=""):
self.__printat(1, self.lines, line)
self.lines += 1

def refresh(self):
self.stdscr.refresh()

def print_header(self):
self.__printat(1, self.lines,
"hostname RX TX total RX total TX")
self.lines += 1

def print_bandwidth(self, name, diff_rx, diff_tx, total_rx, total_tx):
self.__printat(1, self.lines, "{} {:10.2f} Mbit/s {:10.2f} Mbit/s {:10.2f} GB {:10.2f} GB".format(name, diff_rx,
diff_tx,
total_rx,
total_tx))
self.lines += 1

def print_footer(self, now, delta, sleeptime, all_diff_rx, all_diff_tx, all_rx, all_tx):
self.__printat(1, self.lines, "TOTAL: {:10.2f} Mbit/s {:10.2f} Mbit/s {:10.2f} GB {:10.2f} GB".format(all_diff_rx, all_diff_tx, all_rx, all_tx))
self.lines += 1

x, y = self.__getmax()
self.__printat(1, y-2, "{} delta: {:.2f}, sleeptime: {:.2f}".format(
now, delta, sleeptime
))
51 changes: 38 additions & 13 deletions src/torspray/torspray.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from torspray.modules.db import DB
from torspray.modules.ssh_keys import generate_key, remove_hostkeys
from torspray.modules.node import Node, NodeAuthException, NodeTimeoutException
from torspray.modules.tui import TUI
from torspray.sprays.debian_11_torbridge.spray import Debian11Bridge


Expand Down Expand Up @@ -281,14 +282,16 @@ def __parse_ifconfig(self, buf):
data[match["direction"]] = int(matchdict["bytes"])
return data

@need_init
def netstatus(self, interval):
print("[+] Status every {}s:".format(interval))
def __netstatus_core(self, interval, tui):
previous = dt.now()
last = None
servers = self.__list_servers()
servernames = [s.name for s in servers]
while True:
tui.resetlines()
tui.print_header()
# tui.clear()

current = {}
for result in self.__node_exec("ifconfig eth0", servers):
server, retval, out, err = result
Expand All @@ -300,8 +303,7 @@ def netstatus(self, interval):
delta = (now-previous).total_seconds()

# print status
all_tx = 0
all_rx = 0
all_rx, all_tx, all_diff_rx, all_diff_tx, = 0, 0, 0, 0
for name in servernames:
if last is None:
continue
Expand All @@ -314,33 +316,56 @@ def netstatus(self, interval):
new_tx = new.get("TX", 0)

# TODO: make kbit/mbit etc calculation adaptive
diff_rx = (new_rx - old_rx) * 8 / 1024 / delta
diff_tx = (new_tx - old_tx) * 8 / 1024 / delta
#print("{} RX: {}, {} TX: {} {}".format(name, old_rx, new_rx, old_tx, new_tx))
diff_rx = (new_rx - old_rx) * 8 / 1024 / 1024 / delta
diff_tx = (new_tx - old_tx) * 8 / 1024 / 1024 / delta
# tui.print("{} RX: {}, {} TX: {} {}".format(name, old_rx, new_rx, old_tx, new_tx))

total_rx = new_rx / 1024/1024/1024
total_tx = new_tx / 1024/1024/1024

print("{} RX: {:10.2f} kbit/s TX: {:10.2f} kbit/s - total: RX: {:10.2f} GB TX: {:10.2f} GB".format(name, diff_rx, diff_tx, total_rx, total_tx))
# tui.print("{} RX: {:10.2f} kbit/s TX: {:10.2f} kbit/s - total: RX: {:10.2f} GB TX: {:10.2f} GB".format(name, diff_rx, diff_tx, total_rx, total_tx))
tui.print_bandwidth(name, diff_rx, diff_tx, total_rx, total_tx)

all_diff_rx += diff_rx
all_diff_tx += diff_tx

all_tx += total_tx
all_rx += total_rx
all_tx += total_tx

# calculate time to sleep
sleeptime = max(0, interval-delta)

if last is not None:
print()
print("date: {}, delta: {:.2f}, sleep time: {:.2f} TOTAL: RX: {:10.2f} GB TX: {:10.2f} GB".format(now, delta, sleeptime, all_rx, all_tx))
print("="*50)
tui.print()
# tui.print("date: {}, delta: {:.2f}, sleep time: {:.2f} TOTAL: RX: {:10.2f} GB TX: {:10.2f} GB".format(now, delta, sleeptime, all_rx, all_tx))
tui.print_footer(now, delta, sleeptime, all_diff_rx, all_diff_tx, all_rx, all_tx)

last = current

# render
tui.refresh()
tui.getch()

# sleep
# TODO: use sleeptime here, as we have to compensate for the time it takes the fetch to run
time.sleep(interval)
previous = now


@need_init
def netstatus(self, interval):
print("[+] Status every {}s:".format(interval))

tui = TUI()
tui.start()

try:
self.__netstatus_core(interval, tui)
except KeyboardInterrupt:
pass
finally:
tui.stop()

def __showpubkey(self):
print("##################################################")
print("# Use this key when creating the VM: #")
Expand Down

0 comments on commit 9338b3e

Please sign in to comment.