码迷,mamicode.com
首页 > 其他好文 > 详细

三个监控小脚本

时间:2015-09-25 16:33:53      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:脚本

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

    #!/bin/bash
    # check mysql slave status

    mysql_path=/wqdata/mysql/bin/mysql
    user="root"
    password="tze@2011"
    email=110@1.com
    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" 1410@19.com
    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 from=110@139.com smtp=smtp.139.com

set smtp-auth-user=110@139.com smtp-auth-password=we

set smtp-auth=login

echo "zabbix test mail" |mail -s "标题zabbix" 15413@19.com


[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

三个监控小脚本

标签:脚本

原文地址:http://024mj.blog.51cto.com/10252785/1698234

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!