VnLamp

Go Back   VnLamp > Linux Resources > Linux Forums - Press Releases

Trả lời
 
LinkBack Ðiều Chỉnh Xếp Bài
Linux MySQL server monitoring
Old
  (#1 (permalink))
 
Status: Offline
Posts: 510
Tham gia: Dec 2007
BQT VNLAMP
 
goldenbook's Avatar
Default Linux MySQL server monitoring - 15-09-2009, 13:15 15-09-2009

Some time due to high load MySQL server gets killed by OS. So we need to make sure that whenever it gets killed restart the mysql server and send an email notification along with status of mysql server. This is simple and yet effective script.

Please see the script
Trích:
# 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
Customizing the script. Open the script in text editor such as vi, you can customize following 5 directives (read as variable):

1) MUSER="root"
Change this if your mysql admin user is not root.

2) MPASS="SET-ROOT-PASSWORD"
Setup password, this the ONLY one you need to setup.

3)MHOST="localhost"
Setup hostname if it is not localhost

4) MSTART="/etc/init.d/mysql start"
This is default on Debain Linux, you need to setup this according to your UNIX/Linux/BSD OS.

5) EMAILID="monitor@vnlamp.com"
Set Email ID to send notification.

Once all done save the file and install script as cron job. For example run this script every minute
Trích:
* * * * * /path/to/mysql.sh


Success inlife isn't a given. It's costs attitude, ambition and acceptance.
   
Trả Lời Với Trích Dẫn
Trả lời


Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt
Trackbacks are Mở
Pingbacks are Mở
Refbacks are Mở


Múi giờ GMT. Hiện tại là 20:29 29-01-2012.