I use a multisite wordpress installation to manage all of my courses, and a number of other virtual presences, hosted on a VPS at dreamhost. In the process of moving everything onto the virtual machine, and nailing down my resource needs, I spent a lot of time on a secure shell monitoring the server with
top for memory spikes. Once I figured out the minimum ram I could get away with on my setup (turned out to be 600MB), I wanted to keep track of memory usage at a glance without having to ssh from the terminal.
Enter GeekTool. GeekTool is a nifty little program that allows one to run shell scripts and have the output display on your OSX desktop. I use it to monitor my server’s memory load, and also to show my todo list using Todo.txt, a lightweight CLI todo list manager:
In order to show memory usage, you just need a script to ssh to your server, run a quick shell script to check
free -m, and then return the results as standard output. I did this with a simple python script that is called by GeekTool on a regular schedule. For this script to work, you need the
keyring python libraries installed. (Easiest way to do this is with easy_install or pip.) Paramiko is a python ssh client, and keyring allows python programs to interact with the built-in keychain of your OS. Here’s the script:
#! /usr/bin/python import paramiko import keyring # retrieve your ssh credential from your keychain. credential = keyring.get_password('example_keychain', 'user') # set the ssh client, and force it to accept new/unknown host keys. ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # connect to your remote server ssh.connect('example.com', username='your_username', password=credential) stdin, stdout, stderr = ssh.exec_command('free -m') # read each line of the free -m command for pretty printing in the next step type(stdin) output = stdout.readlines() # this will display the output formated for GeekTool print " "+output print output print output print output ssh.close()
In order for this to work, you need to set the password in the keychain from the python interpreter first:
>>> import keyring
>>> keyring.set_password('example_keychain', 'user', 'example_password')
And that’s it. That simple.
To display the output on your desktop, install GeekTool, go to system preferences and select the GeekTool preferences. From the preferences pane, drag the shell icon onto the desktop, where ever you want it to display. In the preferences panel, give it a name and then in the command dialogue box, enter:
Other options include setting the frequency with which the script is run. You can also resize the box so that the output is spaced correctly. And there you have it, a way to glance at the free memory on your remote server just by looking at your desktop.