Hack36.Receive Alerts on the Go


Hack 36. Receive Alerts on the Go

How can you find out if your web server is down? You can set up your desktop to notify your Palm device when something happens.

You shouldn't have to manually checkto see if your web site is up or not. Technology should be able to handle mundane chores like this. If you have a smartphone or wireless Palm device, then why not have it alert you when something goes wrong?

On your desktop machine, set up a simple script that checks to make sure your web site is alive. If the web site is down, then the script can send a text message (SMS) or an email [Hack #35] to your Palm device to let you know.

Test the following Python script out to make sure it works for you. Also, try running it with a non-existent URL to verify that the notification is working. Once the script is working, you can set up a cron job (Linux or Macintosh) or add it to Scheduled Tasks for Windows XP.

6.4.1. The Code

 """Check to see if a given web site is up or not. Usage:   python check_site.py <URL> """ import os, sys, urllib2, smtplib # The following variables should be redefined to fit your system _smtp_server = "outgoing.example.com" # If you need to use SMTP AUTH, set this to 1. # You will also need to set an SMTP user name and SMTP password _authentication_required = 0 _user = "" _password = "" _to = "user@example.com" _from = "webserver@mydomain.com" _num_tries = 3 def is_site_up(url):   """Checks to see if the given site is up"""   try: urllib2.urlopen(url) return True   except: return False def check_site(url):   """Checks several times to see if the given site is up."""   count = 0   up = False   while not up and count < _num_tries:     if is_site_up(url):   up = True count += 1   if not up: print "site %s not up." % ( url ) # Construct a message mesg = "To: " + _to + "\n" # Note the extra blank line between Subject and body mesg += "Subject: Server down\n\n" mesg += "The web site: " + url + " is down.\n" # Send email notifcation using SMTP session = smtplib.SMTP(_smtp_server) if _authentication_required:   session.login(_user, _password) result = session.sendmail(_from, _to, mesg) if result:   print "Sent mail. Status = " + str(result)   else:     print "site %s is up." % (url,) if __name__ == '__main__':   if len(sys.argv) == 2:   check_site(sys.argv[1])   else:     print __doc__ 

6.4.2. Running the Code

Save the code in a text file called check_site.py. Replace _smtp_server, _to, and _from. If your server requires SMTP authentication, then set _authentication_ required, _user, and _password as well.

Invoke the script on the command line as follows:

 % python check_site.py url 

This will test the given URL to see if it is alive or not. If not, then the script will send a message to the email you specified in _to. You might want to test with a non-existent URL to make sure that you have the script configured properly, then checkwith the URL you actually want to monitor. When the script is working, then you can add it to Scheduled Tasks in Windows or set up a cron job on Linux or Macintosh.

6.4.3. Hacking the Hack

You can send these emails as an SMS quite easily. Most wireless providers have email addresses that automatically forward emails to a phone number as SMS. Table 6-2 shows a partial list of US operators. Simply replace <number> with the 10-digit phone number (no spaces or dashes, e.g., 5035551212@messaging.nextel.com) when you send the email. Keep in mind that a single SMS may contain at most 160 characters. Be aware that the wireless providers may put a limit on the number of consecutive messages that go through their email-to-SMS gateways to cut down on SMS spam.

Table 6-2. U.S. mobile operator SMS gateways

Operator

Email

Nextel

<number>@messaging.nextel.com

Sprint PCS

<number>@messaging.sprintpcs.com

AT&T Wireless

<number>@mobile.att.net

Cingular Wireless

<number>@mobile.mycingular.com

Verizon Wireless

<number>@vtext.com

T-Mobile

<number>@tmomail.net





Palm and Treo Hacks
Palm and Treo Hacks: Tips & Tools for Mastering Your Handheld
ISBN: 059610054X
EAN: 2147483647
Year: 2006
Pages: 115

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net