SMS alert for you server with Google Calender

During the college days, I had much fun doing the dumbest things which had absolutely no hope of finding any practical application – but just made me feel good. To bring back the good times, this weekend I thought of doing something similar.
Have you ever:

  1. Felt getting sms reminder from your Google Calender makes you more efficient
  2. Felt the need what’s going on in your server when you are not looking at it
  3. Felt Python is the neatest language around

Wondering what’s common in the three of them? How about using Google’s free sms service to get updated about the health of your server? Google’s gdata api allows you to do that, just with a few lines of python.

What’s the big deal about that? Nagios already has that feature, you might say! But unfortunately, I’m using windows these days, and all the free sms gateways in India are worthless.

Anyway, for the win32 api functionalities, I got pywin32 [Build 212] . Next I got the gdata-python-client [gdata.py-1.2.2.zip]. Installation was hassle free.

Peeped into the sample in the samplescalendar folder, and took out the required part from the calendarExample.py and made some little changes, and it was done. Here is the script.

The script isn’t really good and just checks if the Remote Registry Service is up or not. But with more love, cool things (like sms alert on unauthorised access, low isk space etc.) can be done.
However, to remove the ugliness of the script, firstly the hardcoding needs to go.

start_time = time.strftime(‘%Y-%m-%dT%H:%M:%S+05:30’, time.localtime(time.time() + 600))

where +05:30 indicates my time zone, ie IST and the 600 seconds, the time lag between the running of the script and the creation of the event. The sms reminder is sent 1 minute before the event begins, making the time lag 9 minutes. 

If you dont have your mobile registered with Google, just move down to the Settings tab at the top of the page in Google Calendar, and then to the mobile setup tab.

You need to have your default reminder type in Google Calenda as SMS. The Javascript Api, allows you to set the type of reminder eassily – no reason to suspect that the Python Api would not be able to do that; need to check this.

But more importantly, there should be saner way of passing a password to a script. I had my dad’s cell registered with a less important google account and scheduled a .bat file  in Winodws Scheduler that calls the script to set events every 10 minutes. Unfortunately, he did not enjoy it as much as I did.

Pesky tasks with batch scripts

Scripting is an art. Nifty and subtle, wicked cool scripts can weave magic, and startle compiled languages. When it comes to getting yet-another-pesky-job done, that scripting languages are your friend.

The batch scripting language, is one of the ways Windows operating system offers for writing small scripts without the need of installing any additional language support. It is somewhat limited with multiple short comings that does not make it fun. However you can still get some interesting stuff done with it. Below are some pesky jobs that can still be done with batch scripts.

Pesky job 1 : Map a network drive

net use N:| find “OK”
if errorlevel 1 net use N: \servernamepath$ ******** /user:******* /persistent:yes

This will check if the drive N is mapped or not; in case there is an error, it will map servernamepath with proper username/password values and keep this map persistent across reboots.

Pesky job 2 : Copying files with a time stamp
Say we want to copy a few files from one directory to another file to another with the current date stamp, it could be a simple
copy help.txt Desktop%date:~10,4%%date:~7,2%%date:~4,2%-chgs-1.txt

Truly ugly? Quite right.

Normally the date command would output

C:Documents and SettingsTatha>date
The current date is: Mon 11/17/2008
Enter the new date: (mm-dd-yy)

To use the date-stamp say in an echo statement, put the command with in percentage signs. to extract part of the time stamp, the command should be followed with a “:~offset, number_of_characters”. For example

C:Documents and SettingsTatha>echo %date:~0,14%
Mon 11/17/2008

So, the copy command above would create a copy the help.txt to the path C:Documents and SettingsTathaDesktop with a name 20081711-chgs-1.txt, on 17th November 2008.

But wait, this wont work in a Windows NT box. Seems like the automatic variables DATE and TIME were not implemented until windows 2000, so if you want a time stamp in an NT box you should

time /t >> file.txt

Pesky job 3 : Starting and stopping windows services gracefully
Another glitch when running newer bat scripts in Windows NT, that I came across is controlling Windows services. Consider the following snippet to stop a service named SomeAppServer or someappserver in a Windows Xp box.

net start | find “SomeAppServer”
if errorlevel 1 goto STOPPED
if errorlevel 0 echo %date% %time% Attempting to Stop SomeAppServer >> log.txt
start /wait net stop “SomeAppServer” >> log.txt 2>&1
if errorlevel 1 echo %date% %time% SomeAppServer could not be stopped >>log.txt
:STOPPED
echo %date% %time% SomeAppServer is stopped >> log.txt
echo — >> log.txt

However, in case the name of the service is someappserver, instead of SomeAppServer as written in the script, it would fail to stop the service in a Windows NT box. NT treats the service names as case sensitive and you need to supply exactly as it is listed.

Here are some good resources for batch scripting
http://www.robvanderwoude.com/batchcommands.html
http://weblogs.asp.net/jgalloway/archive/2006/11/20/top-10-dos-batch-tips-yes-dos-batch.aspx