Skip to content

Instantly share code, notes, and snippets.

@mjbommar
Created December 20, 2012 18:00
Show Gist options
  • Save mjbommar/4347285 to your computer and use it in GitHub Desktop.
Save mjbommar/4347285 to your computer and use it in GitHub Desktop.
This simple python script checks to see if MySQL is alive based on mysqladmin output. Tested on 5.5.28-0ubuntu0.12.04.3 under ubuntu user. (c) Bommarito Consulting, LLC; http://bommaritollc.com You might want to run in cron every minute: $ EDITOR=emacs crontab -e */1 * * * * /path/to/check-mysql.py
#!/usr/bin/python
#@author Bommarito Consulting, LLC; http://bommaritollc.com
#@date 2012-12-20
import subprocess
import smtplib
from email.mime.text import MIMEText
fromAddress = 'root@bommaritollc.com'
adminEmail = 'michael@bommaritollc.com'
def sendMessage(emailAddress, errorBuffer):
msg = MIMEText("MySQL down.\nError: " + errorBuffer)
msg['Subject'] = 'BCLLC MySQL Down'
msg['From'] = fromAddress
msg['To'] = emailAddress
s = smtplib.SMTP('localhost')
s.sendmail(fromAddress, emailAddress, msg.as_string())
def main():
statusProc = subprocess.Popen(['mysqladmin', 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
outputBuffer = statusProc.stdout.read().strip()
errorBuffer = statusProc.stderr.read().strip()
if 'uptime' not in outputBuffer.lower() or len(errorBuffer) > 0:
sendMessage(adminEmail, errorBuffer)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment