Skip to content

Instantly share code, notes, and snippets.

@benburry
Created May 17, 2013 12:11
Show Gist options
  • Save benburry/5598652 to your computer and use it in GitHub Desktop.
Save benburry/5598652 to your computer and use it in GitHub Desktop.
Use boto to pull the latest Amazon SES statistic values and report them to Graphite
#!/usr/bin/env python
import dateutil.parser
import socket
from time import mktime
import boto
GRAPHITE_KEY_TMPL='services.email.ses.%s.last15minutes'
GRAPHITE_HOST=('graphite.example.com', 2003)
def build_graphite_message_list(stats):
# AWS returns an unsorted list of 15-minute moving windows of data for the last 2 weeks
# We sort, grab the last one and parse its timestamp into something that graphite understands
stat_point = sorted(stats['GetSendStatisticsResponse']['GetSendStatisticsResult']['SendDataPoints'], key=lambda x: x['Timestamp'])[-1]
timestamp = mktime(dateutil.parser.parse(stat_point['Timestamp']).astimezone(dateutil.tz.gettz()).timetuple())
graphite_timestamp = str(int(timestamp))
messages = []
for key in ('Complaints', 'Bounces', 'Rejects', 'DeliveryAttempts'):
messages.append(' '.join([GRAPHITE_KEY_TMPL % key, stat_point[key], graphite_timestamp]))
return messages
def main():
aws_access_key_id = boto.config.get('SESCredentials', 'aws_access_key_id')
aws_secret_access_key = boto.config.get('SESCredentials', 'aws_secret_access_key')
conn = boto.connect_ses(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
if conn is not None:
stats = conn.get_send_statistics()
if stats is not None:
graphite_socket = socket.socket()
graphite_socket.connect(GRAPHITE_HOST)
graphite_message = '\n'.join(build_graphite_message_list(stats)) + '\n'
graphite_socket.sendall(graphite_message)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment