Skip to content

Instantly share code, notes, and snippets.

@joet3ch
Created January 18, 2012 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joet3ch/1633507 to your computer and use it in GitHub Desktop.
Save joet3ch/1633507 to your computer and use it in GitHub Desktop.
Simple script to check api availability and send alerts via SES
#!/usr/bin/env python
import os
import sys
import time
import random
import string
import json
import hashlib
import socket
import urllib
import urllib2
from operator import itemgetter
from pprint import pprint
import StringIO
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import boto
from boto.ses import SESConnection
## config
AWS_ACCESS_KEY = 'XXX'
AWS_SECRET_KEY = 'XXX'
TO_ADDRESS = 'XXX@example.com'
FROM_ADDRESS = 'XXX@example.com'
API_ENDPOINT = 'http://example.com/api/system/health'
def send_alert(subject, body):
## ses connect
try:
ses = SESConnection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
result = ses.send_email(FROM_ADDRESS, subject, body, TO_ADDRESS)
return True
except:
return False
def test_api():
subject = 'ALERT: api outage'
try:
url = API_ENDPOINT
headers = {}
params = {}
url = url + '?' + urllib.urlencode(params)
req = urllib2.Request(url, None, headers)
response = json.load(urllib2.urlopen(req))
if response['meta']['status'] != 'OK':
send_alert(subject, 'api not OK')
except:
send_alert(subject, 'api call failed')
return True
def main():
test_api()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment