Created
December 13, 2019 18:20
-
-
Save clairegraham/9d521cbf7bf5f5a46b01b1e52b30fbdd to your computer and use it in GitHub Desktop.
Simple library to pull some metrics from the Mailchannels API, which I used for alerting via DataDog
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 python | |
import requests | |
import re | |
import json | |
from datadog import initialize, api | |
class Mailchannels: | |
def __init__(self): | |
self.payload = { | |
'j_username': 'USER', | |
'j_password': 'PASS' | |
} | |
self.dd = self.dd_init() | |
self.session = requests.Session() | |
self.login() | |
def stats(self): | |
self.data = dict() | |
self.data['summary'] = self.dashboard_summary() | |
self.data['messages'] = dict() | |
self.data['messages']['sent'] = self.messages("messages") | |
self.data['messages']['queued'] = self.messages("queued") | |
self.data['messages']['delivered'] = self.messages("delivered") | |
self.dd.Metric.send([ | |
{'metric':'mc.messages.delivered', 'points': | |
self.data['messages']['delivered']}, | |
{'metric':'mc.messages.queued', 'points': | |
self.data['messages']['queued']}, | |
{'metric':'mc.messages.sent', 'points': | |
self.data['messages']['sent']}, | |
{'metric':'mc.summary.canSend', 'points': | |
self.data['summary']['canSend']}, | |
{'metric':'mc.summary.percentUsed', 'points': | |
self.data['summary']['percentUsed']}, | |
{'metric':'mc.summary.messagesSent', 'points': | |
self.data['summary']['messagesSent']} | |
]) | |
return self.data | |
def login(self): | |
self.session.post('https://console.mailchannels.net/j_spring_security_check', | |
data=self.payload) | |
def dashboard_summary(self): | |
page = self.session.get('https://console.mailchannels.net/dashboard/summary').content | |
return self.parse_response(page) | |
# messages, queued, delivered | |
def messages(self, status): | |
url = 'https://console.mailchannels.net/dashboard/countForPeriod?period=900&type={}'.format(status) | |
resp = self.session.get(url) | |
return json.loads(resp.content)['count'] | |
def top_senders(self, page=None): | |
url = 'https://console.mailchannels.net/topN?type=Sender+Domain&sortBy=Volume&period=24&page={}&includeTotal=true'.format(page) | |
resp = self.session.get(url) | |
return json.loads(resp.content) | |
def get_sender_data(self): | |
self.data['top_senders'] = [] | |
page = 1 | |
while True: | |
sender = self.top_senders(page) | |
self.data['top_senders'].extend(sender['data']) | |
if sender['isLastPage']: | |
break | |
else: | |
page += 1 | |
next | |
def parse_response(self, body): | |
match = re.search('var summary = (.*);', body) | |
return json.loads(match.group(1)) | |
def dd_init(self): | |
dd_options = { | |
'api_key': 'API', | |
'app_key': 'APP' | |
} | |
initialize(**dd_options) | |
return api | |
mc = Mailchannels() | |
print(mc.stats()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment