编写了定时数据库增量同步脚本,为了能够监控每天同步脚本是否进行了同步,编写了一个py脚本,通过zabbix调用脚本实现监控。
增量同步脚本内容(shell编写):
#!/bin/bash
#通用变量
MySql_Comm='/usr/local/mysql/bin/mysql'
MySqldump_Comm='/usr/local/mysql/bin/mysqldump'
DateTime=`date +%Y-%m-%d-%H:%M:%S`
echo -e "\n\n${DateTime} -----脚本开始执行-----" >> /tmp/sourcedb.log
#源数据库信息
Source_MySql_User='root'
Source_MySql_Pass='xxxxxxxxx'
Source_MySql_Port='3306'
Source_MySql_DB='db_name'
Source_MySql_Table='tb_name'
Source_Host_IP='192.168.0.100'
#本机数据库信息
Mysql_User='root'
MySql_Pass='xxxxxxxxx'
MySql_Port='3306'
MySql_DB='db_name'
MySql_Table='tb_name'
MySql_Bak_Dir="/data/${MySql_DB}-${MySql_Table}/`date +%Y-%m-%d-%H-%M`"
MySql_Bak_Dir_2="/data/${MySql_DB}-${MySql_Table}"
#创建备份目录
mkdir -p ${MySql_Bak_Dir}
#备份本机表
if [ -d ${MySql_Bak_Dir} ];then
${MySqldump_Comm} \
-u${Mysql_User} \
-p${MySql_Pass} \
-h 127.0.0.1 \
-P${MySql_Port} \
${MySql_DB} ${MySql_Table} > ${MySql_Bak_Dir}/${MySql_DB}-${MySql_Table}.sql
else
echo "${DateTime} ERROR: ${MySql_Bak_Dir} 目录不存在" >> /tmp/sourcedb.log
echo "${DateTime} -----脚本执行完成!!!-----" >> /tmp/sourcedb.log
echo "error" > /etc/zabbix/mysql_source/${Source_MySql_DB}-${Source_MySql_Table}.txt
exit 1
fi
#获取本机表最大ID
${MySql_Comm} \
-u${Mysql_User} \
-p${MySql_Pass} \
-h 127.0.0.1 \
-P${MySql_Port} \
--compress ${MySql_DB} -e "select max(id) from ${MySql_Table}" > /tmp/tmp.txt
ID_Num=`tail -1 /tmp/tmp.txt`
if [[ ${ID_Num} -eq NULL ]];then
echo $ID_Num
ID_Num=0
fi
echo $ID_Num
#备份源表大于本机获取id的数据
if [[ ${ID_Num} -ge 0 ]];then
if [ -d ${MySql_Bak_Dir} ];then
echo "${DateTime} 开始备份原主机${Source_MySql_DB} ${Source_MySql_Table} ID大于${ID_Num}的数据..." >> /tmp/sourcedb.log
${MySqldump_Comm} -t \
-u${Source_MySql_User} \
-p${Source_MySql_Pass} \
-h${Source_Host_IP} \
-P${Source_MySql_Port} \
--single-transaction --compress ${Source_MySql_DB} ${Source_MySql_Table} --where="id > '${ID_Num}'" > ${MySql_Bak_Dir}/${Source_MySql_DB}-${Source_MySql_Table}.sql
echo "${DateTime} 数据备份完成 ${MySql_Bak_Dir}/${Source_MySql_DB}-${Source_MySql_Table}.sql" >> /tmp/sourcedb.log
#导入数据
if [ -f ${MySql_Bak_Dir}/${Source_MySql_DB}-${Source_MySql_Table}.sql ];then
echo "${DateTime} 开始导入数据..." >> /tmp/sourcedb.log
${MySql_Comm} \
-u${Mysql_User} \
-p${MySql_Pass} \
-h 127.0.0.1 \
-P${MySql_Port} \
${MySql_DB} -e "source ${MySql_Bak_Dir}/${Source_MySql_DB}-${Source_MySql_Table}.sql"
echo "${DateTime} 数据导入完成${MySql_Bak_Dir}/${Source_MySql_DB}-${Source_MySql_Table}.sql..." >> /tmp/sourcedb.log
echo "${DateTime} -----脚本执行完成!!!-----" >> /tmp/sourcedb.log
echo "ok" > /etc/zabbix/mysql_source/${Source_MySql_DB}-${Source_MySql_Table}.txt
else
echo "${DateTime} ERROR: sql文件${MySql_Bak_Dir}/${Source_MySql_DB}-${Source_MySql_Table}.sql不存在!"
echo "${DateTime} -----脚本执行完成!!!-----" >> /tmp/sourcedb.log
echo "error" > /etc/zabbix/mysql_source/${Source_MySql_DB}-${Source_MySql_Table}.txt
exit 1
fi
else
echo "${DateTime} ERROR: ${MySql_Bak_Dir} 目录不存在" >> /tmp/sourcedb.log
echo "${DateTime} -----脚本执行完成!!!-----" >> /tmp/sourcedb.log
echo "error" > /etc/zabbix/mysql_source/${Source_MySql_DB}-${Source_MySql_Table}.txt
exit 1
fi
else
echo "${DateTime} ERROR: ID 等于 NULL" >> /tmp/sourcedb.log
echo "${DateTime} -----脚本执行完成!!!-----" >> /tmp/sourcedb.log
echo "error" > /etc/zabbix/mysql_source/${Source_MySql_DB}-${Source_MySql_Table}.txt
exit 1
fi
#清理旧数据
find ${MySql_Bak_Dir_2} -name "20*" -mtime +3 | xargs -t -i rm -rf {}
脚本执行成功或失败都会往/etc/zabbix/mysql_source/目录对应文件中echo一个ok或error字符串
通过这个字符串进行监控
python脚本内容:
#!/usr/bin/python
# coding=utf-8
'''
获取数据库表同步是否完成
'''
import datetime
import time
import os
t_new = datetime.datetime.now()
t1 = t_new.strftime('%Y-%m-%d')
t2 = time.mktime(time.strptime(t1, '%Y-%m-%d'))
file_dir = '/etc/zabbix/mysql_source/'
# 读取文件目录下所有文件
for root in os.walk(file_dir):
file_list = root[2]
# 判断文件修改时间是否等于今天
for i in file_list:
file_time = os.path.getmtime(file_dir + i)
file_time2 = datetime.datetime.fromtimestamp(file_time)
file_time3 = file_time2.strftime('%Y-%m-%d')
file_time4 = time.mktime(time.strptime(file_time3, '%Y-%m-%d'))
# 等于今天,继续判断文件内容是否是ok
if file_time4 == t2:
with open(file_dir + i, 'r') as f:
content = f.read()
content = content.strip()
if str(content) == 'ok':
print('ok')
else:
print('error: ' + str(i))
f.close()
else:
print('error: ' + str(i) + '文件时间未更新,脚本可能未执行')
就是先判断文件是否是今天更新了,如果更新了就接着判断文件内容是不是ok
最后放到zabbix中
##获取表同步状态
UserParameter=mysql.source_status,/etc/zabbix/alertscripts/mysql_source_status.py
用zabbix-get测试能得出文件数量相等的ok或者夹杂着error的输出
别设置每分钟监控间隔,当第二天同步脚本还没执行的时候因为文件未更新与当天时间不匹配会一直告警的,最好是每天同步脚本完成后执行几次,例如我的同步脚本都是在半夜3点同步,那么监控可以设置在9点、12点、17点分别执行
监控间隔参考我的文档: