Skip to content

Instantly share code, notes, and snippets.

@matthanley
Created April 4, 2022 16:32
Show Gist options
  • Save matthanley/1283d165d1b42761dd9cbc31d6a7d88c to your computer and use it in GitHub Desktop.
Save matthanley/1283d165d1b42761dd9cbc31d6a7d88c to your computer and use it in GitHub Desktop.
MariaDB Galera status notification script for Slack
#!/usr/bin/env python
# configure this in my.cnf on a Galera-based MySQL-alike server:
# wsrep_notify_cmd=/usr/local/sbin/wsrep_notify_slack
import argparse
import json
import os
import socket
import urllib2
url = "https://hooks.slack.com/services/..."
service = "wsrep_cluster_name"
if __name__ == '__main__':
if url is None:
exit()
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--status')
parser.add_argument('-u', '--uuid')
parser.add_argument('-p', '--primary')
parser.add_argument('-i', '--index')
parser.add_argument('-m', '--members')
args = parser.parse_args()
name = "%s (%s)" % (service, socket.gethostname())
colors = {
'Joined' : 'warning',
'Synced' : 'good',
'Undefined' : 'danger',
'Joiner' : 'warning',
'Donor' : 'warning'
}
color = 'good'
text = ""
if args.status:
opt = ""
if args.status == "Undefined":
opt = " (shutting down?)"
text = text+"Status changed to *"+args.status+"*"+opt+"\n"
if args.status in colors:
color = colors[args.status]
else:
color = 'danger'
if args.primary:
if args.primary == 'yes':
text = text+"Node is part of Primary Component\n"
else:
text = text+"Node is *not part of Primary Component*\n"
color = 'danger'
if args.members:
size = {{ groups[group_names[0]]|length }}
current = [i.split('/')[1] for i in filter(None,args.members.split(','))]
# 0598fb24-ded9-11e5-9ea2-fa946e7538ee/hostname/192.168.7.142:3306,...
current_size = len(current)
if current_size < size:
text = text+str(size - current_size)+" out of " + str(size) + " *cluster members missing!*"
color = 'danger'
else:
text = text+"All cluster members present\n"
q = {}
q['username'] = "galera-cluster"
a = {}
a["fallback"] = "Cluster change notification: "+text
a["text"] = text
a["pretext"] = "*"+name+"*"
a["mrkdwn_in"] = ["text", "fallback", "pretext"]
a['color'] = color
q['attachments'] = [ a ]
enc = "payload="+json.dumps(q)
urllib2.urlopen(url, enc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment