Show computer load using jquery, flot and Python's BaseHTTPServer (all is less that 70 lines of code).
#!/usr/bin/env python
'''Server to show computer load'''
import re
from os import popen
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from socket import gethostname
def load():
'''Very fancy computer load :)'''
output = popen("uptime").read()
match = re.search("load average(s)?:\\s+(\\d+\\.\\d+)", output)
return float(match.groups()[1]) * 100
HTML = '''
<html>
<head>
<script src="jquery.js"></script>
<script src="jquery.flot.js"></script>
<title>%s load</title>
</head>
<body>
<center>
<h1>%s load</h1>
<div id="chart" style="width:600px;height:400px;">
Loading ...
</div>
</center>
</body>
<script>
var samples = [];
var options = {
yaxis: {
min: 0,
max: 100
},
xaxis: {
ticks: []
}
};
function get_data() {
$.getJSON("/data", function(data) {
samples.push(data);
if (samples.length > 120) {
samples.shift();
}
var xy = [];
for (var i = 0; i < samples.length; ++i) {
xy.push([i, samples[i]]);
}
$.plot($('#chart'), [xy], options);
});
}
$(document).ready(function() {
setInterval(get_data, 1000);
});
</script>
</html>
''' % (gethostname(), gethostname())
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
self.wfile.write(HTML)
elif self.path.endswith(".js"):
self.wfile.write(open(".%s" % self.path).read())
else:
self.wfile.write("%.2f" % load())
if __name__ == "__main__":
server = HTTPServer(("", 8888), RequestHandler)
server.serve_forever()
3 comments:
----------------------------------------
top: unknown argument 'l'
usage: top -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...]
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 33963)
Traceback (most recent call last):
File "/usr/lib/python2.5/SocketServer.py", line 222, in handle_request
self.process_request(request, client_address)
File "/usr/lib/python2.5/SocketServer.py", line 241, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.5/SocketServer.py", line 254, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.5/SocketServer.py", line 522, in __init__
self.handle()
File "/usr/lib/python2.5/BaseHTTPServer.py", line 316, in handle
self.handle_one_request()
File "/usr/lib/python2.5/BaseHTTPServer.py", line 310, in handle_one_request
method()
File "computer_load.py", line 71, in do_GET
self.wfile.write("%.2f" % load())
File "computer_load.py", line 13, in load
return float(match.groups()[0]) * 100
AttributeError: 'NoneType' object has no attribute 'groups'
on my linux (ubuntu) I replaced the load function by this one:
def load():
top = popen("uptime").read()
match = re.search("load average:\s+(\d+\.\d+)", top)
return float(match.groups()[0]) * 100
Of course the top output and command line switches had to be different in OS X and Linux.
I'll fix it to use the 'uptime' command which has the same usage and a slightly different output.
Thanks!
Post a Comment