用sersync+rsync+inotify-tools实时监控文件变化,实时同步,将多台业务主机日志集中到一台服务器,便于研发实时grep、awk查看。
A服务器: 收集同步过来的文件
B服务器: 将产生的业务日志实时同步到A上php
平台:centos 7.4 x86_64express
A服务器配置
yum install -y epel-release
yum install -y rsynccentos
cp /etc/rsyncd.conf /etc/rsyncd.conf.bk服务器
vi /etc/rsyncd.conf
uid=root
gid=root
#最大链接数
max connections=36000
#默认为true,修改成no,增长对目录文件软链接的备份
use chroot=no
#定义日志存放位置
log file=/var/log/rsyncd.log
#忽略无关错误
ignore errors = yes
#设置rsync服务端文件为读写权限
read only = no
#认证的用户名与系统账户无关在认证文件作配置,若是没有这行则代表是匿名
auth users = rsync
#密码认证文件,格式(虚拟用户名:密码)
secrets file = /etc/rsync.pass
#这里是认证的模块名,在client端须要指定,能够设置多个模块和路径
[ultron]
#自定义注释
comment = backend ultron
#B服务器同步过来的文件存放的路径
path=/logcenter/backend/ultron
[inside]
comment = backend inside
path=/logcenter/backend/insidedom
#建立rsync认证文件 能够设置多个,每行一个用户名:密码,注意中间以“:”分割
echo "rsync:123456" > /etc/rsync.passssh
#设置文件全部者读取、写入权限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsync.pass socket
#启动服务器B上的rsync服务
#rsync --daemon -v
rsync --daemonide
#监听端口873
netstat -an | grep 873ui
#设置rsync为服务启动项(可选)
echo "/usr/local/bin/rsync --daemon" >> /etc/rc.localurl
#要 Kill rsync 进程,不要用 kill -HUP {PID} 的方式重启进程,如下3种方式任选
#ps -ef|grep rsync|grep -v grep|awk '{print $2}'|xargs kill -9
#cat /var/run/rsyncd.pid | xargs kill -9
pkill rsync
#再次启动
/usr/local/bin/rsync --daemon
B服务器上的配置
yum install -y epel-release
yum install -y rsync inotify-tools
cd /alidata/server
tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 sersync
cd sersync
#配置下密码文件,由于这个密码是要访问服务器A须要的密码和上面服务器A的密码必须一致
echo "123456" > user.pass
chmod 600 user.pass
vi confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="true"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.php"></exclude>
<exclude expression="^data/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/var/www/runtime/log"> <!-- 这里填写服务器B要同步的文件夹路径-->
<remote ip="8.8.8.8" name="ultron"/> <!-- 这里填写服务器A的IP地址和模块名-->
<remote ip="8.8.8.8" name="inside"/> <!-- 这里填写服务器A的IP地址和模块名-->
<!--<remote ip="192.168.28.39" name="tongbu"/>-->
<!--<remote ip="192.168.28.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync" passwordfile="/alidata/server/sersync/user.pass"/> <!-- rsync+密码文件 这里填写服务器B的认证信息-->
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--><!-- 修改失败日志记录(可选)-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<!-- 下面这些有关于插件的设置可忽略了 -->
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /alidata/server/sersync/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/home/demo">
<deshost ip="210.36.158.xxx" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data/htdocs/cdn.markdream.com/site/">
<cdninfo domainname="cdn.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://cdn.markdream.com/cms"/>
<regexurl regex="false" match="cdn.markdream.com/site([/a-zA-Z0-9]*).cdn.markdream.com/images"/>
</localpath>
</plugin>
</head>
启动sersync服务 nohup ./sersync2 -r -d -o ./confxml.xml > ./sersync.log 2>&1 & -d:启用守护进程模式 -r:在监控前,将监控目录与远程主机用rsync命令推送一遍 -n: 指定开启守护线程的数量,默认为10个 -o:指定配置文件,默认使用confxml.xml文件