使用zabbix监控rsync的同步是否执行成功

使用Zabbix监控rsync的同步是否执行成功

我们有个业务需要使用 rsync 同步软件到国内,业务较为关键,网络不稳定可能会同步失败,需要对rsync同步是否成功进行监控,方便提前人工介入处理

1.编写监控rsync的脚本

# cat /usr/local/zabbix_agents_3.2.0/scripts/rsync_download_toalisz.sh

#!/bin/bash
# 同步download.chinasoft.com到深圳的download02
source /etc/profile

pronum=`ps -ef|grep aliszdownload|grep -v grep|wc -l`

if [[ $pronum -gt 0 ]];then
    ps -ef|grep aliszdownload|grep -v grep|awk '{print $2}'|xargs kill -9
    sleep 5
fi


rsync -zavP --password-file="/data/www/.rsync/rsyncd.aliszdownload" --exclude=cbs_down --delete /data/www/vhosts/download.chinasoft.com/httpdocs/ apache@1.1.1.1::apache/data/www/vhosts/download.chinasoft.com/httpdocs/ > /data/www/logs/rsync_log/rsync.log
# Check if rsync was executed with success
if [ $? = 0 ];then
# If true, send a random number to log file and status=ok message
echo $[ 1 + $[ RANDOM % 1000 ]] >> /data/www/logs/rsync_log/rsync.log
echo "rsync_download_status=ok" >> /data/www/logs/rsync_log/rsync.log
# If false, send a random number to log file and status=ERROR message
else
echo $[ 1 + $[ RANDOM % 1000 ]] >> /data/www/logs/rsync_log/rsync.log
echo "rsync_download_status=error" >> /data/www/logs/rsync_log/rsync.log
fi

日志格式
# cat /data/www/logs/rsync_log/rsync.log

sending incremental file list

sent 224,471 bytes  received 486 bytes  89,982.80 bytes/sec
total size is 273,587,291,994  speedup is 1,216,175.94
705
rsync_download_status=ok

2.将该脚本加入crontab执行

# 供监控rsync状态使用

*/8 * * * * /bin/bash /usr/local/zabbix_agents_3.2.0/scripts/rsync_download_toalisz.sh rsync_ws_to_szdownload > /dev/null 2>&1

3.在Zabbix上创建两个Items

a. rsync.log的check_sum (这就是为什么脚本必须有随机数的原因,这样你确定自上次检查后日志文件已被修改

Zabbix的监控项

vfs.file.cksum[/data/www/logs/rsync_log/rsync.log]

b.检查日志文件以获取确定消息

Zabbix的监控项

vfs.file.regmatch[/data/www/logs/rsync_log/rsync.log,rsync_download_status=ok]

4.创建触发器

{Template rsync status:vfs.file.cksum[/data/www/logs/rsync_log/rsync.log].change()}=0
 or 
{Template rsync status:vfs.file.regmatch[/data/www/logs/rsync_log/rsync.log,rsync_download_status=ok].last()}=0

如果日志文件未更改或没有显示“rsync_download_status=ok”消息,则表示它们是以 error (失败)执行的,或者没有运行(可能是cron问题)

原文地址:https://www.cnblogs.com/reblue520/p/14434909.html