三个监控小脚本

  1. 监控mysql主从,结合计划任务发邮件告警。

    #!/bin/bash
    # check mysql slave status

    mysql_path=/wqdata/mysql/bin/mysql
    user="root"
    password="tze@2011"
    [email protected]
    date=`(date +%y-%m-%d--%H:%M:%S)`
    ip=$(/sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2;}' | awk -F':' '{print $2;}' | head -1)
    Slave_IO_Running=$($mysql_path -u$user -p$password -e 'show slave status\G' | grep "Slave_IO_Running" | awk '{print $2}')
    Slave_SQL_Running=$($mysql_path -u$user -p$password -e 'show slave status\G' | grep "Slave_SQL_Running" | awk '{print $2}')
    if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]
    then
      echo "Slave is running!"
    else
      echo "#######  $date  #########">> /wqdata/check_mysql_slave.log
      echo "Slave is not running!" >> /wqdata/check_mysql_slave.log
    echo "{$ip}_replicate_error" |mail -s "warning:---83slave fail" [email protected]
    fi

  2. 监控sersync是否运行,否则重启



    #!/bin/sh

    sersync="/usr/local/sersync/sersync2"

    confxml="/usr/local/sersync/confxml.xml"

    status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)

    if [ $status -eq 0 ];

    then

    $sersync -d -r -o $confxml &

    else

    exit 0;

    fi

  3. 定时备份mysqsl



    #!/bin/bash
    #此脚本用来进行数据库的全备份,结合计划任务完成

    #注意:下面的变量值可能要修改,仔细确认下
    mysqluser=root
    rsyncuser=rsync_backup
    rsync_hostip='233'
    ip='223'               #本机ip
    mysqlpassword=t3011
    mysqlsocket='/tmp/mysql.sock'
    mysql_defaults_file='/wqdata/mysql/my.cnf'

    backuptodir='/wqdata/mofidbbak/'    #备份到目录
    binlogdir='/wqdata/mydata/data'     #二进制日志目录
    binlogname='mysql-bin.0*'       #二进制日志文件名
    rsyncmodule=mofidbbak            #rsync的模块

    if [ ! -d $backuptodir ];then
        mkdir -p $backuptodir
    fi

    #进行全备份
    innobackupex --user=$mysqluser --password=$mysqlpassword --socket=$mysqlsocket --defaults-file=$mysql_defaults_file $backuptodir 2> /dev/null || exit 1

    #删除一天前的全备份文件,即保留两个备份文件
    find $backuptodir  -type f -mtime +1 -exec rm -f {} \;

    #删除一天前的二进制日志文件,即保留两天的二进制日志文件
    cd $binlogdir
    onedayago_binlog=`find . -type f -mtime +1 -name "$binlogname" | sort | tail -n1 | awk -F'/' '{print $2}'`
    mysql -u $mysqluser -p$mysqlpassword -e "PURGE MASTER LOGS TO '$onedayago_binlog'" 2> /dev/null

    #找出最后一个全备份文件,进行压缩
    cd $backuptodir
    lastfull_backup=`ls -l | grep '^d' | awk '{print $9}' | sort | tail -n1`
    tar -zcf mysql_full_${ip}_${lastfull_backup}.tar.gz ${lastfull_backup}

    #将压缩文件rsync同步到备份机
    rsync -az --password-file=/etc/rsync.password  ${backuptodir}mysql_full_${ip}_${lastfull_backup}.tar.gz $rsyncuser@${rsync_hostip}::$rsyncmodule || exit 1

    #删除压缩文件
    rm -f ${backuptodir}mysql_full_${ip}_${lastfull_backup}.tar.gz

4.第三方发邮件软件

yum -y install mailx

vim /etc/mail.rc

set [email protected] smtp=smtp.139.com

set [email protected] smtp-auth-password=we

set smtp-auth=login

echo "zabbix test mail" |mail -s "标题zabbix" [email protected]


[root@localhost iosupload]# crontab -l
*/1 * * * * /etc/init.d/iptables start
*/5 * * * * root /wqdata/84jiaoben/check_sersync.sh > /dev/null 2>&1
* 1 * * * /wqdata/84jiaoben/mysqlfullbak.sh

你可能感兴趣的:(脚本)