After a while without writing anything related to computer/scripting stuff I am back presenting a script for Android devices that will help you notifying when your personal server is down.
I have a personal server running in a virtual machine that during some weeks was a bit buggy, as my personal webpage is hosted there and I needed it to be up 24/7 when system failed the host was no longer available but it took me some days to realize as I wasn’t checking it everyday. As I wanted to apply some Python knowledge under Android I came out with the idea: I have a phone that is always connected so let’s create a script that periodically (using already-available tools) check the status of the server and in case of problem notifies it, thus, I don ‘t have to worry about checking the availability of my server.
The concept is quite simple and will work as follows:
- Small script in Python that will run under SL4A.
- A tool (TaskBomb) that will run the script periodically.
- A server to be checked, http://hoyhabloyo.com in this case.
So, according to that steps, the process for creating the automatic check was:
First I installed TaskBomb (the scheduler), SL4A (scripting layer), the Python interpreter and SL4A Script Launcher which allows TaskBomb to run scripts as it can’t handle them directly. All these tools can be downloaded for free in the Android Market.
Second, I created the following script in Python:
import android import urllib2 # Web page to check web_page = 'http://hoyhabloyo.com' try: urllib2.urlopen(web_page) # print 'Page was found!' # Ok, nothing to do except (urllib2.HTTPError,urllib2.URLError) as e: # Error? Notifiy! droid = android.Android() droid.notify("Server seems down!", web_page)
Third, and finally, I configured the TaskBomb to run the script (through ScriptLauncher) everyday at 6.15:
And that is it! Now, every morning your phone will try to connect to the server, if everything is ok nothing will happen but if anything fails, it will display a small notification on the status bar (I established the rule on IPTABLES to make it fail).
In SL4A page you can find examples and a book to start developing some powerful scripts. Enjoy!