Refactoring to allow more graphs
This commit is contained in:
parent
fa1ee61731
commit
ab8792f388
117
netwatch.py
117
netwatch.py
|
@ -20,17 +20,23 @@ locale.setlocale(locale.LC_ALL, '')
|
||||||
encoding = locale.getpreferredencoding()
|
encoding = locale.getpreferredencoding()
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
host = "8.8.8.8"
|
internet_host = "8.8.8.8"
|
||||||
ping_frequency = 5
|
ping_frequency = 5
|
||||||
|
|
||||||
class Pinger:
|
class Pinger:
|
||||||
ping_log = []
|
internet_log = []
|
||||||
paused = False
|
|
||||||
autostart = False
|
|
||||||
icon_height = 22
|
|
||||||
|
|
||||||
def ping(self):
|
def run(self):
|
||||||
self.draw()
|
internet_latency = self.ping(internet_host,self.internet_log)
|
||||||
|
self.draw("Internet:",self.internet_log,internet_latency,0)
|
||||||
|
|
||||||
|
# Schedule next run
|
||||||
|
GObject.timeout_add_seconds(self.timeout, self.run)
|
||||||
|
|
||||||
|
|
||||||
|
def ping(self,host,log):
|
||||||
|
|
||||||
|
# Do the ping
|
||||||
ping = subprocess.Popen(
|
ping = subprocess.Popen(
|
||||||
["ping", "-c", "1", host],
|
["ping", "-c", "1", host],
|
||||||
stdout = subprocess.PIPE,
|
stdout = subprocess.PIPE,
|
||||||
|
@ -38,63 +44,84 @@ class Pinger:
|
||||||
)
|
)
|
||||||
out, error = ping.communicate()
|
out, error = ping.communicate()
|
||||||
m = re.search('time=(.*) ms', out)
|
m = re.search('time=(.*) ms', out)
|
||||||
|
|
||||||
if error or m == None:
|
if error or m == None:
|
||||||
self.log_ping(-1)
|
latency = 9999
|
||||||
|
logged = -1
|
||||||
else:
|
else:
|
||||||
latency = "%.2f" % float(m.group(1))
|
latency = float(m.group(1))
|
||||||
self.log_ping(latency)
|
logged = latency
|
||||||
self.count += 1
|
|
||||||
GObject.timeout_add_seconds(self.timeout, self.ping)
|
|
||||||
self.stdscr.addstr(0,0,"Internet: "+str(latency), curses.color_pair(3))
|
|
||||||
self.draw()
|
|
||||||
|
|
||||||
def draw(self):
|
# store [graph, color, subjective] in array
|
||||||
for i,p in enumerate(self.ping_log):
|
log.append(self.interpret_ping(float(logged)))
|
||||||
if float(p) == -1:
|
|
||||||
graph = "E "#u'\u2847' # Error
|
|
||||||
color = curses.color_pair(1)
|
|
||||||
elif float(p) < 30:
|
|
||||||
graph = u'\u2840'
|
|
||||||
color = curses.color_pair(2)
|
|
||||||
elif float(p) < 100:
|
|
||||||
graph = u'\u2844'
|
|
||||||
color = curses.color_pair(2)
|
|
||||||
elif float(p) < 200:
|
|
||||||
graph = u'\u2846'
|
|
||||||
color = curses.color_pair(1)
|
|
||||||
else:
|
|
||||||
graph = u'\u2847'
|
|
||||||
color = curses.color_pair(1)
|
|
||||||
self.stdscr.addstr(1,i,graph.encode(encoding), color)
|
|
||||||
|
|
||||||
|
# limit the size of the log
|
||||||
|
if len(log) >= self.ping_log_max_size:
|
||||||
|
# remove the earliest ping, not the latest
|
||||||
|
log.pop(0)
|
||||||
|
|
||||||
|
return latency
|
||||||
|
|
||||||
|
def draw(self,title,log,latency,line):
|
||||||
|
|
||||||
|
# Draw internet heading on specified line
|
||||||
|
self.stdscr.addstr(line,0,title, curses.color_pair(3))
|
||||||
|
interpret = self.interpret_ping(latency)
|
||||||
|
self.stdscr.addstr(line,10,interpret['subjective'], interpret['color'])
|
||||||
|
right_align = self.ping_log_max_size-16
|
||||||
|
self.stdscr.addstr(line,right_align,str(round(latency/1000,3))+" second lag", interpret['color'])
|
||||||
|
|
||||||
|
# Draw graph on next line
|
||||||
|
for idx,entry in enumerate(log):
|
||||||
|
self.stdscr.addstr(line+1,idx,entry['graph'].encode(encoding), entry['color'])
|
||||||
self.stdscr.refresh()
|
self.stdscr.refresh()
|
||||||
|
|
||||||
def log_ping(self, value):
|
def interpret_ping(self, ping):
|
||||||
self.ping_log.append(float(value))
|
ping = float(ping)
|
||||||
#self.update_log_menu()
|
if ping == -1:
|
||||||
# limit the size of the log
|
graph = "E "#u'\u2847' # Error
|
||||||
if len(self.ping_log) >= self.ping_log_max_size:
|
color = curses.color_pair(1)
|
||||||
# remove the earliest ping, not the latest
|
subjective = "Down"
|
||||||
self.ping_log.pop(0)
|
elif ping < 30:
|
||||||
|
graph = u'\u2840'
|
||||||
|
color = curses.color_pair(2)
|
||||||
|
subjective = "Great"
|
||||||
|
elif ping < 70:
|
||||||
|
graph = u'\u2844'
|
||||||
|
color = curses.color_pair(2)
|
||||||
|
subjective = "Good"
|
||||||
|
elif ping < 120:
|
||||||
|
graph = u'\u2846'
|
||||||
|
color = curses.color_pair(1)
|
||||||
|
subjective = "Fair"
|
||||||
|
else:
|
||||||
|
graph = u'\u2847'
|
||||||
|
color = curses.color_pair(1)
|
||||||
|
subjective = "Poor"
|
||||||
|
return {'graph': graph, 'color': color, 'subjective': subjective, 'ping': float(ping)}
|
||||||
|
|
||||||
def __init__(self, stdscr):
|
def __init__(self, stdscr):
|
||||||
# start the ping process
|
# initialize screen and parameters
|
||||||
self.stdscr = stdscr
|
self.stdscr = stdscr
|
||||||
self.window_size = stdscr.getmaxyx() # returns an array [height,width]
|
self.window_size = stdscr.getmaxyx() # returns an array [height,width]
|
||||||
self.ping_log_max_size = self.window_size[1] # max width
|
self.ping_log_max_size = self.window_size[1] # max width
|
||||||
self.timeout = ping_frequency
|
self.timeout = ping_frequency
|
||||||
self.count = 1
|
|
||||||
self.ping()
|
# start the ping process
|
||||||
|
self.run()
|
||||||
|
|
||||||
self.loop = GLib.MainLoop()
|
self.loop = GLib.MainLoop()
|
||||||
self.loop.run()
|
self.loop.run()
|
||||||
|
|
||||||
|
|
||||||
def main(stdscr):
|
def main(stdscr):
|
||||||
|
|
||||||
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK);
|
# Initialize color pairs
|
||||||
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK);
|
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
|
||||||
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK);
|
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
|
||||||
|
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
|
||||||
|
|
||||||
|
# Create runtime class
|
||||||
pinger = Pinger(stdscr)
|
pinger = Pinger(stdscr)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user