Created
December 8, 2013 14:54
-
-
Save prakhar1989/7858537 to your computer and use it in GitHub Desktop.
Logger file with mailer for python 2.7
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/python2.7 | |
| import requests | |
| import datetime | |
| import smtplib | |
| import logging | |
| import re | |
| from email.mime.text import MIMEText | |
| from email.mime.multipart import MIMEMultipart | |
| sender = "uptimebot@test.com" | |
| rcvr = "prakhars@test.com" | |
| # setting up logging config | |
| logging.basicConfig(format='%(asctime)s: %(message)s', | |
| datefmt='%m/%d/%Y %I:%M:%S %p', | |
| filename="uptime-log.txt", level=logging.DEBUG) | |
| class XciteProperty(): | |
| def __init__(self, url, title, valid_string, is_api): | |
| self.url = url | |
| self.title = title | |
| self.valid_string = valid_string | |
| self.is_api = is_api | |
| def __str__(self): | |
| return "<%s: %s>" % (self.title, self.url) | |
| def begin_test(self): | |
| try: | |
| r = requests.get(self.url) | |
| if self.is_valid(r.status_code, r.headers["content-type"], r.text): | |
| self.success() | |
| else: | |
| self.failure(r.text) | |
| except requests.ConnectionError: | |
| self.failure("Connection Error in fetching %s" % self.url) | |
| def is_valid(self, status_code, content_type, page_content): | |
| if self.is_api: # if api then check for header - content-type | |
| s = "javascript" in content_type | |
| else: | |
| s = re.search(self.valid_string, page_content) | |
| return s and status_code == 200 | |
| def log(self, msg, level): | |
| logging.debug("|%s| %s" % (self.title, msg)) | |
| def success(self): | |
| self.log("Site is up", "SUCCESS") | |
| def failure(self, response_text): | |
| self.log("Site is down!!!", " ALERT ") | |
| self.user_notify(response_text) | |
| def user_notify(self, response_text): | |
| msg = MIMEMultipart() | |
| custom_message = "Hi! It seems that %s is down. Below is the response that I recieved is attached! \n\n\n" % self.title | |
| resp_attachment = MIMEText(custom_message + response_text.encode('utf-8')) | |
| msg.attach(resp_attachment) | |
| msg['Subject'] = "[URGENT] %s is down" % self.title | |
| msg['From'] = sender | |
| msg['To'] = rcvr | |
| try: | |
| smtpObj = smtplib.SMTP('localhost') | |
| smtpObj.sendmail(sender, rcvr, msg.as_string()) | |
| except smtplib.SMTPException: | |
| # except: | |
| self.log("Failed to send email for downtime", " ALERT ") | |
| if __name__ == "__main__": | |
| properties = [ | |
| { | |
| "url": "http://www.test.com", | |
| "title": "Xcite", | |
| "valid_string": "SITECHECK" | |
| } | |
| ] | |
| objs = [XciteProperty(item["url"], item["title"], item["valid_string"], | |
| item.get("is_api", False)) for item in properties] | |
| # bootstrap | |
| for o in objs: | |
| o.begin_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment