Skip to content

Instantly share code, notes, and snippets.

@qrwteyrutiyoup
Created May 27, 2014 14:52
Show Gist options
  • Save qrwteyrutiyoup/1be37a0be40b372df03b to your computer and use it in GitHub Desktop.
Save qrwteyrutiyoup/1be37a0be40b372df03b to your computer and use it in GitHub Desktop.
kimsufi notification script
#!/usr/bin/env python3
import datetime
import json
import re
import smtplib
import urllib.request
class OVH:
url = 'http://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2'
wanted_servers = '142sk[123]' # regex-based: e.g: 142sk[13] matches 142sk1 and 142sk3.
plans = {
'142sk1': 'KS-1 (Atom N2800 1.86 GHz, 2GB RAM, 500 GB disk, 4.99 euro/mo)',
'142sk2': 'KS-2 (Atom N2800 1.86 GHz, 4GB RAM, 1 TB disk, 9.99 euro/mo)',
'142sk3': 'KS-3 (Core i3 3.4 GHz, 8GB RAM, 1TB disk, 14.99 euro/mo)',
'142sk4': 'KS-4 (Core i5 2.8 GHz, 16GB RAM, 1 TB disk, 19.99 euro/mo)'
}
datacenters = {
'gra': 'GRA (Gravelines, France)',
'sbg': 'SBG (Strasbourg, France)',
'rbx': 'RBX (Roubaix, France)',
'bhs': 'BHS (Beauharnois, QC, Canada)'
}
mail = {
'server': 'smtp.gmail.com',
'port': '587',
'secure': True,
'subject': 'OVH server availability notification',
'sender': 'OVH Server Availability <no-reply@example.com>',
'user': '***EMAIL*USER*HERE***', # e.g.: foo@gmail.com.
'password': '***EMAIL*PASSWORD*HERE***', # the password of the above user to authenticate.
'recipients': [' FOO <foo@example.com>', 'BAR <bar@example.com>'],
}
def __init__(self):
self.getAvailability()
def getAvailability(self):
f = urllib.request.urlopen(self.url)
data = json.loads(f.read().decode('utf-8'))
availability = {}
for a in data['answer']['availability']:
if re.search(self.wanted_servers, a['reference']):
for z in a['zones']:
if z['availability'] != 'unavailable':
if not a['reference'] in availability:
availability[a['reference']] = []
availability[a['reference']].append(self.datacenters[z['zone']])
if len(availability) > 0:
body = []
for s in availability:
body.append("{} is available in {}.".format(self.plans[s], ', '.join(availability[s])))
print('\n'.join(body))
self.notify('\n'.join(body))
else:
print('No servers matching the specified type ({}) available at the moment.'.format(self.wanted_servers))
def notify(self, body):
headers = ['From: {}'.format(self.mail['sender']),
'Subject: {} - {}'.format(datetime.datetime.now().strftime("%B %d %Y, %H:%M"), self.mail['subject']),
'To: {}'.format(', '.join(self.mail['recipients'])),
'MIME-Version: 1.0',
'Content-type: text/plain']
session = smtplib.SMTP(self.mail['server'], self.mail['port'])
session.ehlo()
if self.mail['secure']:
session.starttls()
session.login(self.mail['user'], self.mail['password'])
data = '{}\r\n\r\n{}'.format('\r\n'.join(headers), body)
session.sendmail(self.mail['sender'], self.mail['recipients'], data)
session.quit()
ovh = OVH()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment