Last active
January 18, 2018 12:30
-
-
Save jamil666/76f00ecbbf14457821bb11d00fbf9429 to your computer and use it in GitHub Desktop.
This script check web sites availability and send mail to administrator if web site down
This file contains 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
import urllib3.request | |
import time, datetime | |
import smtplib | |
from email.mime.text import MIMEText | |
from colorama import Fore, init | |
init() # For colored output in CMD or PowerShell | |
def SendMail(alert): | |
# Define to/from | |
sender = 'Sender email address' | |
recipient = 'Recipient email address' | |
# Create message | |
msg = MIMEText(alert) | |
msg['Subject'] = "Web Server Status Alert! %s" %Subject | |
msg['From'] = sender | |
msg['To'] = recipient | |
# Create server object with SSL option | |
server = smtplib.SMTP_SSL('smtp.zoho.com', 465) | |
# Perform operations via server | |
server.login('Your mail account login', 'Your password') | |
server.sendmail(sender, [recipient], msg.as_string()) | |
server.quit() | |
date = datetime.datetime.now() | |
currentDate = date.strftime("%d %B %Y %I:%M%p") | |
# List of monitored web sites | |
websites = ("List of web sites") | |
while True: | |
ID = 1 # Web site ID | |
for site in websites: | |
urllib3.disable_warnings() # Disable SSL warning | |
http = urllib3.PoolManager() | |
r = http.request('GET', site) | |
# Code 200 - web site is OK | |
if r.status == 200: | |
print(Fore.WHITE + ID.__str__(), Fore.GREEN + "\t %s Site %s is OK" % (currentDate, site)) | |
ID = ID + 1 | |
# Code 404 - web site is down | |
elif r.status == 404: | |
print(Fore.WHITE + ID.__str__(), Fore.RED + "\t %s Site %s is DOWN" % (currentDate, site)) | |
ID = ID + 1 | |
if r.status == 404: | |
Subject = "404 Not Found" | |
SendMail("%s Site %s is DOWN or not found" % (currentDate, site)) | |
# All other codes | |
else: | |
print(Fore.WHITE + ID.__str__(), Fore.RED + "\t %s Site %s has error code %s" % (currentDate, site, r.status)) | |
ID = ID + 1 | |
if r.status == 401: | |
Subject = "401 Unauthorized" | |
SendMail("%s Site %s has error code %s" % (currentDate, site, r.status)) | |
print(Fore.YELLOW + "\n--------------------------End of Cycle-------------------------------\n") | |
# Timeout before attempts (in seconds) | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment