Created
November 16, 2018 20:28
-
-
Save macielportugal/71fd4f968529de0240354e0a80d840c0 to your computer and use it in GitHub Desktop.
Alert if the site goes down by sending an email or slack.
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
#!/usr/bin/env python3.6 | |
# coding=utf-8 | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
import smtplib | |
import urllib.request | |
import subprocess | |
import shlex | |
import re | |
import json | |
import datetime | |
sites = [ | |
{'host': 'http://example.com', 'send_email': True, 'send_slack': True, 'test_ping': True, 'test_http': {'status_code': 200, 'contains': 'content body'}}, | |
{'host': 'http://example.com', 'send_email': False, 'send_slack': True, 'test_ping': True}, | |
] | |
email = { | |
'from': '[email protected]', | |
'to': ['[email protected]', '[email protected]'], | |
'subject': 'Alert', | |
'smtp': { | |
'host': 'smtp.example.com', | |
'port': 587, | |
'starttls': True, | |
'login': '[email protected]', | |
'password': '123456' | |
} | |
} | |
#https://api.slack.com/apps//incoming-webhooks | |
slack_webhook_url = 'https://hooks.slack.com/services/123456/123456/123456' | |
for site in sites: | |
errors = [] | |
print('Testing the site', site['host']) | |
if 'test_ping' in site and site['test_ping'] == True: | |
print('Ping test') | |
command = "ping -c 1 -w20 {0}".format(site['host'].replace('http://', '').replace('https://', '')) | |
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
return_code = p.wait() | |
if return_code != 0: | |
errors.append('Ping') | |
if 'test_http' in site: | |
print('Http test') | |
request = urllib.request.urlopen(site['host']) | |
if 'status_code' in site['test_http']: | |
if site['test_http']['status_code'] != request.getcode(): | |
errors.append('Code') | |
if 'contains' in site['test_http']: | |
if site['test_http']['contains'] not in request.read().decode('utf-8'): | |
errors.append('Content') | |
if errors: | |
print("Send errors") | |
message = 'Data: {} / Site: {} / Errors: {}'.format(datetime.datetime.now(), site['host'], ','.join(errors)) | |
if 'send_email' in site and site['send_email'] == True: | |
print('Send email') | |
msg = MIMEMultipart() | |
msg['From'] = email['from'] | |
msg['To'] = ','.join(email['to']) | |
msg['Subject'] = email['subject'] | |
msg.attach(MIMEText(message, 'plain')) | |
server = smtplib.SMTP(email['smtp']['host'], email['smtp']['port']) | |
if 'starttls' in email['smtp'] and email['smtp']['starttls'] == True: | |
server.starttls() | |
server.login(email['smtp']['login'], email['smtp']['password']) | |
server.send_message(msg) | |
server.quit() | |
if 'send_slack' in site and site['send_slack'] == True: | |
req = urllib.request.Request(slack_webhook_url) | |
req.add_header('Content-Type', 'application/json; charset=utf-8') | |
jsondata = json.dumps({'text': message}) | |
jsondataasbytes = jsondata.encode('utf-8') | |
req.add_header('Content-Length', len(jsondataasbytes)) | |
response = urllib.request.urlopen(req, jsondataasbytes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment