Live Raspberry Pi System Stats

I get a lot of people asking how I've put together the live system info on this very website. It's a lot easier than it looks to pull off, and I've opted to go a slightly more complex route than is sufficient. I use Ruby, Thin, EventMachine and other things that I'm assuming a basic knowledge of in this brief guide, in most cases you're best adapting the below code to your own language and server choices, but I'm aiming to write a more complete Ruby guide soon.

In its basic form, the stats are simply pumped out as HTML with the rest of the page and update only when you refresh the page. The only complexity here is the system commands needed to extract free disk space and memory. If you're a Rubyist, you can get everything you need with the following code ( which was previously shown in my post: Using the Palm m500 as a Raspberry Pi LCD ):

def self.get() @diskinfo = `df | grep rootfs | awk '{print $2,$4,$5}'` @diskinfo = @diskinfo.split(' ') @diskinfo_size = (@diskinfo[0].to_f / 1024 / 1024).round(2) @diskinfo_free = (@diskinfo[1].to_f / 1024 / 1024).round(2) @diskinfo_perc = @diskinfo[2].sub('%','') @cpu_perc = `vmstat | awk '{print $13}'`.split("

").last @mem = `cat /proc/meminfo | grep Mem | awk '{print $2}'` @mem_total= (Float(@mem.split("

").first) / 1024).round(2) @mem_free = (Float(@mem.split("

").last) / 1024).round(2) @mem_perc = ((100/@mem_total)*(@[email protected]_free)).round.to_s @uptime = `uptime`.chomp.to_s.to_s.sub('load average','load') end

To make things more exciting, however, I wrapped up this code in a WebSocket server, using EventMachine's WebSocket library and just a little touch of MemCache to try and avoid the poor Pi being DDoS'd. You can see the full Ruby file here: http://pi.gadgetoid.co.uk/code/Socket.rb

I run this using the "Thin" Ruby server, with a yml file something like this:

rackup: /home/pi/Web/socket.ru

pid: tmp/pids/thin.pid

log: log/thin.log

timeout: 30

max_conns: 1024

port: 8070

max_persistent_conns: 512

chdir: /home/pi/Web

environment: production

servers: 1

address: 10.0.1.41

daemonize: true

« Back to index Posted on 2012-12-12 by Philip Howard

Please enable JavaScript to view the comments powered by Disqus.