My Alarm Script

Shared for Sajal. This python script wakes me up when our server is down. Just substitute the variables at the beginning to suit your needs. mpg123 is required to play mp3

#!/usr/bin/python

import urllib2,re,time,os

url = "http://www.google.com"
needle = "I'm Feeling Lucky"
sound = "~/music/Rage\ Against\ The\ Machine\ -\ Rage\ Against\ The\ Machine/07\ -\ Rage\ Against\ The\ Machine\ -\ Wake\ Up.mp3"

def is_down(url,needle):
    print "checking..."
    try:
        source=urllib2.urlopen(url)
        content=source.read()
        pattern=re.compile(needle)
        if re.search(pattern,content):
            return False
        else:
            return True
    except:
        return True

while 1:
    if is_down(url,needle):
        print "site down!"
        os.system('mpg123 '+sound)
    else:
        print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),"Up"
        time.sleep(10)

Post a Comment

Your email is never shared.