Let’s just assume that you have some important stuff being done on your server. A computation running, like I do, something getting compiled for a long time, or whatever else you might need. You don’t want to be sitting all the time, sshed in and running tail -f dump.log So you think to yourself: “If only I could have this file posted to a website so I could just check it here and there from my cellphone”. That’s your wish (believe me — I can read minds). Well, I had the same wish a while back and this little script came out of it.
Of course, I like to at least try to code stuff up, instead of just writing to Santa. However, I usually go and try to generalize a bit. Let’s say that I want to monitor a few files or outputs of various commands at the same time. Moreover, let’s say I want to add these things as I go. Enter
remotespy.
Remotespy is a script that parses a list of commands to run and compiles their output into a HTML webpage. The page is then saved to a local folder, e.g. public_html. Additionally, it can be scp-ed to a destination of your choice. This is useful if your “workhorse” server doesn’t have a http server setup, but you do have some other place to post your output to.
The syntax is simple:
remotespy COMMANDFILE HTMLFILE"
The spirit of the command file is similar to the one of crontab scripts: every line represents a command that is run. Syntax of every
line is:
TITLE ## COMMAND ## COMMENT
A sample script looks like this:
Hostname ## hostname
OS ## uname -orpi ## OS version
Disk ## df -h ## Free disk space
Top ## top -b -n 1 -i ## Process state
Vmstat ## vmstat ## Memory state
Every command is run as-is and its output captured and formatted into HTML. The HTML code is hardcoded in the script, but, script being a script, it can be easily edited. To make remotespy into a tool satisfying the initial desire of remote monitoring, it can be paired with cron and ssh to periodically run commands from the script and send the files to a (remote) public_html folder which is available to the internets. For the SCP upload to be useful, you should set up a ssh key pair between the source server (one running remotespy) and destination http server. These two articles can help if you don’t know how to do it yet:
- http://www.linuxjournal.com/article/8600
- http://mindspill.net/computing/linux-notes/ssh-or-scp-without-password.html
The refresh meta tag is also set for the page so you don’t have to refresh the page manually to check if cron ran the command.
Remotespy was written in Python and tested on versions 2.4 - 2.6. I don’t know how far back the compatibility actually extends. I like to use it as it allows me to edit the command script without having to reinsert anything into cron. The HTML output is simple enough to be quickly read from mobile devices so I can check on my simulations state whenever I feel like it. You can find it in my Mercurial repository among other scripts I’ve written:
http://bitbucket.org/mbudisic/cl-goodies/src/tip/remotespy
(The repo name was taken from el-goodies collection for Emacs).
Let me know if you have suggestions for improvements. Future versions should probably migrate from “commands” to “subprocess” Python module. Also, I might make remotespy into a daemon to make it cron-independent, but that’s a low-priority for me. This does the job well. If you would like to extend remotespy, please let me know, I’d love to hear from you. If once wasn’t enough, you can read this post at http://mbudisic.wordpress.com/2009/04/03/monitoring-from-afar/ too.
Great idea.
You can even turn this into a self-refreshing web page using cherrypy.
http://pastebin.com/f5dac95d4
the code has two further dependencies:
1) cherrypy package
2) a file called cmds.txt which has all the commands to run.
Run the code and browse to http://localhost:8080/
Thanks for your comment ducky! I can see how this is useful in general. However, I don’t see how this solution handles the situation where you’re monitoring a server which is “invisible” outside of the local network. In that case, the code should still resort to compiling a HTML file and scping it to a remote public server. Or am I mistaken?
Thanks for the pastebin link to, I haven’t heard about it before.
I agree with your assessment. What I hacked together only works for local networks. But I just wanted to illustrate how the code can be extended.
I can see this script being easily combined with a python ssh like the one mentioned here:
http://commandline.org.uk/python/sftp-python-really-simple-ssh/
You can easily add the ability to ssh into another machine and then get its stats. Could be useful if you want to monitor a bunch of machines.
Man, I appreciate the link and all the feedback! I came into python from scientific programming side so I never took much interest in network related stuff. Having such an easy access to SSH functionality seems great! And it’s a nice client-side complement to the idea that I’ve had.
In general, your comments are spot on - I never knew about any of this fancy web-related functionality (I’m referring to both of your posts). Truth be told, I only learned about the meta refresh tag from guys that own this blog (the old versions of code had a javascript refresh instead X) ).
Thanks
No problem at all. I also don’t come from a networking/server background. So my comments aren’t the most informed on these topic. I know we are trying to reinvent the wheel here but all the other tools are just so complicated to install and use etc.