How To Check MySQL Was Running With Python Script

How To Check MySQL Was Running With Python Script
Sering pertanyaan kita hinggap dipikiran kita, semakin dipikirkan semakin gak ngerti seringnya, apalagi sesuatu yang gak kliatan kerjanya seperti master kita satu ini MySQL Database. Awalnya ini script digunakan untuk keperluan deteksi pakah mysql jalan atao kagak, Kalo mysqlnya gak jalan tinggal dikasih script buat jalanin si doi, jadi kalo misalnya ada aplikasi yang mau masukin n baca data tinggal cek dulu apa mysql ini jalan ato kagak. 

Aplikasi yang baru dikembangin kebetulan pake python n jalan di Raspberry Pi Rev B, dan rencanya ini nanti dikasih di crontab nya cron buat di cek setiap menit untuk memastikan database jalan dengan sempurna, karena ane juga gak ngerti script python mungkin sering juga gua browse sana sini, eee gua nemu juga di internet script ajaib ini.

Manfaatin subprocess yang ada di Python langsung memberikan perintah melalui shell/terminal. Di raspberry penggunaan perintah ini gak perlu pake memasukkan pasword dari sudo yang dilakukan kalo memang dibutuhkan untuk menjalakn proses dengan authorisasi Super user.

Cara paling mudah untuk membuat ini bekerja adalah sebagai berikut

Script :

import subprocess
import string

msqlr = subprocess.Popen("sudo /usr/sbin/netstat -al".split(), stdout=subprocess.PIPE).stdout
grep = subprocess.Popen(["/usr/bin/grep", "mysql"], stdin=msqlr, stdout=subprocess.PIPE).stdout
msqlrLines = grep.read().split("\n")
vals = map(string.strip, msqlrLines[0].split())
print vals
if vals[-1] in ("LISTENING", "LISTEN"):
    print "OK - MySQL is running."
else:
    print "Not OK - MySQL is not running."


Yang dibawah ini mungkin agak berbeda, kalo kalian ingin mereset MySQL jalankan peritah berikut dengan cript python :

Shell script to restart MySQL server if it is killed or not workin

    #!/bin/bash # Shell script to restart MySQL server if it is killed or not working # due to ANY causes. # When script detects mysql is not running (it basically sends ping request # to MySQL) it try to start using /etc/init.d/mysql script; and it sends an # email to user indicating the status. # This script must be run from Cron Job so that it can monitor mysql server. # For more info visit following url: # http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/08/linux-mysql-server-monitoring.html # -------------------------------------------------------------------------- # Copyright (C) 2005 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # mysql root/admin username MUSER="root" # mysql admin/root password MPASS="SET-ROOT-PASSWORD" # mysql server hostname MHOST="localhost" #Shell script to start MySQL server i.e. path to MySQL daemon start/stop script. # Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS. MSTART="/etc/init.d/mysql start" # Email ID to send notification EMAILID="notification@somewhere-corp.com" # path to mail program MAILCMD="$(which mail)" # path mysqladmin MADMIN="$(which mysqladmin)" #### DO NOT CHANGE anything BELOW #### MAILMESSAGE="/tmp/mysql.fail.$$" # see if MySQL server is alive or not # 2&1 could be better but i would like to keep it simple and easy to # understand stuff :) $MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null if [ $? -ne 0 ]; then echo "" >$MAILMESSAGE echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE echo "Hostname: $(hostname)" >>$MAILMESSAGE echo "Date & Time: $(date)" >>$MAILMESSAGE # try to start mysql $MSTART>/dev/null # see if it is started or not o=$(ps cax | grep -c ' mysqld$') if [ $o -eq 1 ]; then sMess="MySQL Server MySQL server successfully restarted" else sMess="MySQL server FAILED to restart" fi # Email status too echo "Current Status: $sMess" >>$MAILMESSAGE echo "" >>$MAILMESSAGE echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE # send email $MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE else # MySQL is running :) and do nothing : fi # remove file rm -f $MAILMESSAGE
Sekian cerita ane ntar nyambung lagi...