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 paramiko
and 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[0] print output[1] print output[2] print output[3] 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:
python /path/to/your/script.py
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.
You should just drop this whole academia scam and start your own IT/web development company out there in the desert.
[…] useful, as well as the requisite couple of posts on my current workflow. We have scripts for monitoring server memory, batch renaming photos, tweeting from the command line (here and here), bursting and OCRing pdfs, […]
Looks very nice indeed! Can’t get it to work, however. When I execute the script in the Terminal, it says:
Traceback (most recent call last):
File “/Users/myusername/GeekTool/server_admin.py”, line 23, in
print ” “+output[0]
IndexError: list index out of range”
Do I have to substitute something in the section with
print ” “+output[0]
print output[1]
print output[2]
print output[3]
I’m trying to connect from a laptop with Mountain Lion to a machine running Lion Server.
Thank you!
Hi Paul–
From the looks of it, that would suggest that the script isn’t getting anything back from the server. Have you tried running it from step-by-step from an interactive session?
Yes. That is: I did every step in the script step-by-step in the Terminal. No error messages, until the “# this will display the output formatted for GeekTool”-part. Is this what you mean by “interactive session”?
I’m not familiar with Python, so I don’t know about it’s error messages behavior if there’s no connection…
Keyring works: I found the entry in my keychain.
Hmm. Yes, I mean manually entering each line from a python prompt in the terminal. What’s happening is the variable `output` is empty, so most likely you’re failing to make an actual SSH connection to your server. Do you SSH into that server normally?
Yes, normal ssh works fine. I just tried step-by-step again. The first line with a reply is the “type(stdin)”-line, and the reply is “”.
Should I substitute “set_missing_host_key_policy” too? :)
Here my full script (“leospaul” is my user account on the server):
#!/usr/bin/python
import paramiko
import keyring
# retrieve your ssh credential from your keychain.
credential = keyring.get_password(‘pattongeekt’, ‘leospaul’)
# 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(‘10.0.1.200′, username=’leospaul’, 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 formatted for GeekTool
print ” “+output[0]
print output[1]
print output[2]
print output[3]
ssh.close()
Thanks for the effort!
So, 10.0.1.200 — is this a localhost server on the same computer?
That type(stdout) line was in there originally from my own on the fly error checking and doesn’t need to be there. But, it’s telling in this case. From that line you should get “. What’s happening is that paramiko is failing to make an SSH connection to your server. That’s what you need to trouble shoot. I’ve never used Lion server, so I’m not sure how/why it would fail.
No, it’s a server in the same network. It’s also accessible over the internet, but over port 2222 instead of 22. Since I didn’t know yet how to define the custom port (adding “:2222”?), I used the subnet IP for now.
Does the server need paramiko as well?
The only thing I can think of is some colorful warnings during the installation of paramiko. They didn’t look too serious, though. The installation finished. But I’ll try to re-install paramiko anyway.
Installed pip, uninstalled both paramiko and keyring with pip, then re-installed both with pip. Did all your steps again (import keyring, keyring.set_password(‘… etc…), and ran the script with the updated keyring entry… same result. Oh well :)
Is there a way to test the ssh connection made by paramiko?
No, the server doesn’t need paramiko. paramiko is just an sash client library, like a python wrapper for OpenSSH. As per the paramiko documentation, you can pass port information in the connect method. So, you can specify port 2222.