Created
October 22, 2012 11:20
-
-
Save shimarin/3931087 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* * * * * wget -O /dev/null -q http://server/cgi-bin/imalive.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# m h dom mon dow command | |
* 9-18 * * 1-5 /usr/lib/cgi-bin/imalive.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import time | |
import os | |
import sys | |
import smtplib | |
from email.MIMEText import MIMEText | |
from email.Utils import formatdate | |
CHECKFILE="/tmp/imalive" | |
THRESHOLD=60 * 5 | |
GMAIL_ID="your_gmail_id@gmail.com" | |
GMAIL_PASSWORD="your_gmail_password" | |
RECIPIENT="alert_recipient@example.com" | |
def create_message(from_addr, to_addr, subject, body): | |
msg = MIMEText(body) | |
msg['Subject'] = subject | |
msg['From'] = from_addr | |
msg['To'] = to_addr | |
msg['Date'] = formatdate() | |
return msg | |
def send_via_gmail(from_addr, to_addr, msg): | |
s = smtplib.SMTP('smtp.gmail.com', 587) | |
s.ehlo() | |
s.starttls() | |
s.ehlo() | |
s.login(GMAIL_ID, GMAIL_PASSWORD) | |
s.sendmail(from_addr, [to_addr], msg.as_string()) | |
s.close() | |
def cgi(): | |
if not os.path.exists(CHECKFILE): | |
msg = create_message(GMAIL_ID, RECIPIENT, "Facility alert (recovered)", "Recovered from failing status.") | |
send_via_gmail(GMAIL_ID, RECIPIENT, msg) | |
os.system("touch %s" % CHECKFILE) | |
print "Content-type: text/plain\n" | |
print "OK" | |
def check(): | |
if not os.path.exists(CHECKFILE): return | |
if time.time() - os.stat(CHECKFILE).st_mtime < THRESHOLD: return | |
os.unlink(CHECKFILE) | |
msg = create_message(GMAIL_ID, RECIPIENT, "Facility alert (failed)", "Internet disconnected or server down.") | |
send_via_gmail(GMAIL_ID, RECIPIENT, msg) | |
if __name__ == '__main__': | |
if "REQUEST_METHOD" in os.environ: cgi() | |
else: check() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment