-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python2/3 galeranotify support (#168)
* Split galeranotify for python2 * galeranotify: Switch to python3 by default * Use galeranotify.py for a given python major version * Move galeranotify for python3
- Loading branch information
Showing
3 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
#!/usr/bin/python3 | ||
# | ||
# Script to send email notifications when a change in Galera cluster membership | ||
# occurs. | ||
# | ||
# Complies with http://www.codership.com/wiki/doku.php?id=notification_command | ||
# | ||
# Author: Gabe Guillen <[email protected]> | ||
# Version: 1.5 | ||
# Release: 3/5/2015 | ||
# Use at your own risk. No warranties expressed or implied. | ||
# | ||
|
||
# {{ ansible_managed }} | ||
|
||
import os | ||
import sys | ||
import getopt | ||
|
||
import smtplib | ||
|
||
try: from email.mime.text import MIMEText | ||
except ImportError: | ||
# Python 2.4 (CentOS 5.x) | ||
from email.MIMEText import MIMEText | ||
|
||
import socket | ||
import email.utils | ||
|
||
# Change this to some value if you don't want your server hostname to show in | ||
# the notification emails | ||
THIS_SERVER = socket.gethostname() | ||
|
||
# Server hostname or IP address | ||
SMTP_SERVER = '{{ galera_notify_smtp_server }}' | ||
SMTP_PORT = {{ galera_notify_smtp_port }} | ||
|
||
# Set to True if you need SMTP over SSL | ||
SMTP_SSL = {{ galera_notify_smtp_ssl }} | ||
|
||
# Set to True if you need SMTP over TLS | ||
SMTP_STARTTLS = {{ galera_notify_smtp_starttls }} | ||
|
||
# Set to True if you need to authenticate to your SMTP server | ||
SMTP_AUTH = {{ galera_notify_smtp_auth }} | ||
# Fill in authorization information here if True above | ||
SMTP_USERNAME = '{{ galera_notify_smtp_username }}' | ||
SMTP_PASSWORD = '{{ galera_notify_smtp_password }}' | ||
|
||
# Takes a single sender | ||
MAIL_FROM = '{{ galera_notify_mail_from }}' | ||
# Takes a list of recipients | ||
# Need Date in Header for SMTP RFC Compliance | ||
DATE = email.utils.formatdate() | ||
MAIL_TO = ['{{ galera_notify_mail_to }}'] | ||
|
||
# Edit below at your own risk | ||
################################################################################ | ||
def main(argv): | ||
str_status = '' | ||
str_uuid = '' | ||
str_primary = '' | ||
str_members = '' | ||
str_index = '' | ||
message = '' | ||
|
||
usage = "Usage: " + os.path.basename(sys.argv[0]) + " --status <status str>" | ||
usage += " --uuid <state UUID> --primary <yes/no> --members <comma-seperated" | ||
usage += " list of the component member UUIDs> --index <n>" | ||
|
||
try: | ||
opts, args = getopt.getopt(argv, "h", ["status=","uuid=",'primary=','members=','index=']) | ||
except getopt.GetoptError: | ||
print(usage) | ||
sys.exit(2) | ||
|
||
if(len(opts) > 0): | ||
message_obj = GaleraStatus(THIS_SERVER) | ||
|
||
for opt, arg in opts: | ||
if opt == '-h': | ||
print(usage) | ||
sys.exit() | ||
elif opt in ("--status"): | ||
message_obj.set_status(arg) | ||
elif opt in ("--uuid"): | ||
message_obj.set_uuid(arg) | ||
elif opt in ("--primary"): | ||
message_obj.set_primary(arg) | ||
elif opt in ("--members"): | ||
message_obj.set_members(arg) | ||
elif opt in ("--index"): | ||
message_obj.set_index(arg) | ||
try: | ||
send_notification(MAIL_FROM, MAIL_TO, 'Galera Notification: ' + THIS_SERVER, DATE, | ||
str(message_obj), SMTP_SERVER, SMTP_PORT, SMTP_SSL, SMTP_STARTTLS, SMTP_AUTH, | ||
SMTP_USERNAME, SMTP_PASSWORD) | ||
except Exception as e: | ||
print(f"Unable to send notification: {e}") | ||
sys.exit(1) | ||
else: | ||
print(usage) | ||
sys.exit(2) | ||
|
||
sys.exit(0) | ||
|
||
def send_notification(from_email, to_email, subject, date, message, smtp_server, | ||
smtp_port, use_ssl, use_starttls, use_auth, smtp_user, smtp_pass): | ||
msg = MIMEText(message) | ||
|
||
msg['From'] = from_email | ||
msg['To'] = ', '.join(to_email) | ||
msg['Subject'] = subject | ||
msg['Date'] = date | ||
msg['Message-ID'] = email.utils.make_msgid() | ||
|
||
if(use_ssl): | ||
mailer = smtplib.SMTP_SSL(smtp_server, smtp_port) | ||
else: | ||
mailer = smtplib.SMTP(smtp_server, smtp_port) | ||
|
||
if(use_starttls): | ||
mailer.starttls() | ||
|
||
if(use_auth): | ||
mailer.login(smtp_user, smtp_pass) | ||
|
||
mailer.sendmail(from_email, to_email, msg.as_string()) | ||
mailer.close() | ||
|
||
class GaleraStatus: | ||
def __init__(self, server): | ||
self._server = server | ||
self._status = "" | ||
self._uuid = "" | ||
self._primary = "" | ||
self._members = "" | ||
self._index = "" | ||
self._count = 0 | ||
|
||
def set_status(self, status): | ||
self._status = status | ||
self._count += 1 | ||
|
||
def set_uuid(self, uuid): | ||
self._uuid = uuid | ||
self._count += 1 | ||
|
||
def set_primary(self, primary): | ||
self._primary = primary.capitalize() | ||
self._count += 1 | ||
|
||
def set_members(self, members): | ||
self._members = members.split(',') | ||
self._count += 1 | ||
|
||
def set_index(self, index): | ||
self._index = index | ||
self._count += 1 | ||
|
||
def __str__(self): | ||
message = "Galera running on " + self._server + " has reported the following" | ||
message += " cluster membership change" | ||
|
||
if(self._count > 1): | ||
message += "s" | ||
|
||
message += ":\n\n" | ||
|
||
if(self._status): | ||
message += "Status of this node: " + self._status + "\n\n" | ||
|
||
if(self._uuid): | ||
message += "Cluster state UUID: " + self._uuid + "\n\n" | ||
|
||
if(self._primary): | ||
message += "Current cluster component is primary: " + self._primary + "\n\n" | ||
|
||
if(self._members): | ||
message += "Current members of the component:\n" | ||
|
||
if(self._index): | ||
for i in range(len(self._members)): | ||
if(i == int(self._index)): | ||
message += "-> " | ||
else: | ||
message += "-- " | ||
|
||
message += self._members[i] + "\n" | ||
else: | ||
message += "\n".join((" " + str(x)) for x in self._members) | ||
|
||
message += "\n" | ||
|
||
if(self._index): | ||
message += "Index of this node in the member list: " + self._index + "\n" | ||
|
||
return message | ||
|
||
if __name__ == "__main__": | ||
main(sys.argv[1:]) |