Commit 0c62da64 authored by Luke Macken's avatar Luke Macken

Merge pull request #6 from ralphbean/feature/sparklines

Feature/sparklines

I'm an idiot.
parents 2002fae9 26bb63d7
...@@ -27,13 +27,16 @@ from StringIO import StringIO ...@@ -27,13 +27,16 @@ from StringIO import StringIO
from pyrasite.ipc import PyrasiteIPC from pyrasite.ipc import PyrasiteIPC
class ReverseConnection(threading.Thread, PyrasiteIPC): class ReverseConnection(threading.Thread, PyrasiteIPC):
"""A payload that connects to a given host:port and receives commands"""
host = '127.0.0.1' host = '127.0.0.1'
port = 9001 port = 9001
def __init__(self, port=None): def __init__(self, host=None, port=None):
super(ReverseConnection, self).__init__() super(ReverseConnection, self).__init__()
self.sock = None self.sock = None
if host:
self.host = host
if port: if port:
self.port = port self.port = port
...@@ -64,7 +67,10 @@ class ReverseConnection(threading.Thread, PyrasiteIPC): ...@@ -64,7 +67,10 @@ class ReverseConnection(threading.Thread, PyrasiteIPC):
class ReversePythonConnection(ReverseConnection): class ReversePythonConnection(ReverseConnection):
"""A reverse Python connection payload.
Executes Python commands and returns the output.
"""
def on_command(self, cmd): def on_command(self, cmd):
buffer = StringIO() buffer = StringIO()
sys.stdout = buffer sys.stdout = buffer
......
...@@ -33,7 +33,7 @@ from meliae import loader ...@@ -33,7 +33,7 @@ from meliae import loader
from gi.repository import GLib, GObject, Pango, Gtk, WebKit from gi.repository import GLib, GObject, Pango, Gtk, WebKit
import pyrasite import pyrasite
from pyrasite.utils import setup_logger, run from pyrasite.utils import setup_logger, run, humanize_bytes
log = logging.getLogger('pyrasite') log = logging.getLogger('pyrasite')
...@@ -321,12 +321,12 @@ class PyrasiteWindow(Gtk.Window): ...@@ -321,12 +321,12 @@ class PyrasiteWindow(Gtk.Window):
cpu_user = cputimes.user, cpu_user = cputimes.user,
cpu_sys = cputimes.system, cpu_sys = cputimes.system,
mem = p.get_memory_percent(), mem = p.get_memory_percent(),
mem_rss = meminfo.rss, mem_rss = humanize_bytes(meminfo.rss),
mem_vms = meminfo.vms, mem_vms = humanize_bytes(meminfo.vms),
read_count = io.read_count, read_count = io.read_count,
read_bytes = io.read_bytes, read_bytes = humanize_bytes(io.read_bytes),
write_count = io.write_count, write_count = io.write_count,
write_bytes = io.write_bytes, write_bytes = humanize_bytes(io.write_bytes),
) )
open_files = p.get_open_files() open_files = p.get_open_files()
...@@ -417,19 +417,17 @@ class PyrasiteWindow(Gtk.Window): ...@@ -417,19 +417,17 @@ class PyrasiteWindow(Gtk.Window):
def test(): def test():
# Inject jQuery # Inject jQuery
jquery = file('jquery-1.7.1.min.js') jquery = file('jquery-1.7.1.min.js')
self.details_view.execute_script(jquery.read()) self.info_view.execute_script(jquery.read())
jquery.close() jquery.close()
# Inject Sparkline # Inject Sparkline
sparkline = file('jquery.sparkline.min.js') sparkline = file('jquery.sparkline.min.js')
self.details_view.execute_script(sparkline.read()) self.info_view.execute_script(sparkline.read())
sparkline.close() sparkline.close()
# FIXME: alert works, sparkline does not... self.info_view.execute_script("""
self.details_view.execute_script("""
jQuery(document).ready(function() { jQuery(document).ready(function() {
jQuery('#cpu_graph').sparkline(); jQuery('#cpu_graph').sparkline([10,8,3,7,4,4,1]);
alert('FOO');
}); });
""") """)
......
# http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/
from __future__ import division
def humanize_bytes(bytes, precision=1):
"""Return a humanized string representation of a number of bytes."""
abbrevs = (
(1<<50L, 'PB'),
(1<<40L, 'TB'),
(1<<30L, 'GB'),
(1<<20L, 'MB'),
(1<<10L, 'kB'),
(1, 'bytes')
)
if bytes == 1:
return '1 byte'
for factor, suffix in abbrevs:
if bytes >= factor:
break
return '%.*f %s' % (precision, bytes / factor, suffix)
# Some useful functions based on code from Will Maier's 'ideal Python script' # Some useful functions based on code from Will Maier's 'ideal Python script'
# https://github.com/wcmaier/python-script # https://github.com/wcmaier/python-script
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment