inotify可以支持一对一、一对多的实时同步,支持单机实时备份、灾难切换,也可以实现在负载均衡LB下同时更新、上线多台RS服务器或者保持数据一致性。
实时同步实例:同步顺序:青岛监控机10.32.232.238(监控主机)——> 10.32.232.239(监控备机) Web1:10.32.232.238(rsyncclient,inotify) Web2:10.32.232.239(rsyncserver) 同步的目录是 /usr/local/nagios/etc , 实现监控备机配置文件与主机实时同步。 一、配置 rsync服务器端(238)1.先使用 rpm –qa 命令查看系统中是否已经安装了 rsync 软件包,如果有我们可以不用进行安装,如果没有我们可以挂载上 CentOS 的系统光盘进行安装(当然,我们也可以选择使用源码包编译安装) [iyunv@232-239-qd-monitor~]# rpm -qa | grep rsync rsync-3.0.6-9.el6_4.1.x86_64 [iyunv@232-239-qd-monitor~]# 2. Rsync安装好之后没自己的配置文件,创建 rsync 的配置文件 [iyunv@232-239-qd-monitortmp]# cat /etc/rsyncd.conf ##rsyncd.confstart## uid= root gid= root usechroot = no maxconnections = 200 ##最大连接数 timeout= 300 ##超时时间 pidfile = /var/run/rsyncd.pid lockfile = /var/run/rsync.lock logfile = /var/log/rsyncd.log ignoreerrors readonly = false ##只读为假,可以上传 list= false hostsallow = 10.32.0.0/24 ##允许网段 hostsdeny = 0.0.0.0/32 authusers = rsync_backup ##用户 secretsfile = /etc/rsync.password ##密码文件 [nagios] ##模块 path= /usr/local/nagios/etc ##模块目录 [wangxu] path= /tmp #rsync_config_______________end 3.建立 rsync 用户名和密码文件 [iyunv@232-238-qd-monitor~]# echo "rsync_backup:monitor " >> /etc/rsync.password 4.为 /etc/rsync.password 授权为 600(这个文件的权限必须是 600) [iyunv@232-238-qd-monitor~]# chmod -R 600 /etc/rsync.password 二、客户端(239)配置:1.设置 rsync 客户端的密码文件,客户端只需要设置 rsync 同步的密码即可,不用设置用户名 [iyunv@232-239-qd-monitor~]# echo "monitor" >> /etc/rsync.password 4. 为 /etc/rsync.password 授权为 600(这个文件的权限必须是 600) [iyunv@232-239-qd-monitor~]# chmod -R 600 /etc/rsync.password 三、测试rsync守护进程。[iyunv@232-239-qd-monitortmp]# rsync --daemon ####启动 [iyunv@232-239-qd-monitortmp]# netstat -lntp | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 36834/rsync tcp 0 0 :::873 :::* LISTEN 36834/rsync [iyunv@232-239-qd-monitortmp]# [iyunv@232-239-qd-monitortmp]# pwd /tmp [iyunv@232-239-qd-monitortmp]# ls 10.32.231.177_mongodb_cacti_stats.txt 10.32.232.222_mongodb_cacti_stats.txt 10.32.232.225_mongodb_cacti_stats.txt sms.log 10.32.231.186_mongodb_cacti_stats.txt 10.32.232.224_mongodb_cacti_stats.txt 10.32.232.226_mongodb_cacti_stats.txt [iyunv@232-239-qd-monitortmp]# —————————————————————————————————————————————————— [iyunv@232-238-qd-monitortmp]# pwd /tmp receivingincremental file list ./ 10.32.231.177_mongodb_cacti_stats.txt 10.32.231.186_mongodb_cacti_stats.txt 10.32.232.222_mongodb_cacti_stats.txt 10.32.232.224_mongodb_cacti_stats.txt 10.32.232.225_mongodb_cacti_stats.txt 10.32.232.226_mongodb_cacti_stats.txt sent220 bytes received 12073 bytes 24586.00 bytes/sec totalsize is 170894 speedup is 13.90 [iyunv@232-238-qd-monitortmp]# ls 10.32.231.177_mongodb_cacti_stats.txt 10.32.232.222_mongodb_cacti_stats.txt 10.32.232.225_mongodb_cacti_stats.txt 10.32.231.186_mongodb_cacti_stats.txt 10.32.232.224_mongodb_cacti_stats.txt 10.32.232.226_mongodb_cacti_stats.txt [iyunv@232-238-qd-monitortmp]# Delete 用法:本地为空,远端目录直接清空。有使远端rsync服务端丢数据的风险。 [iyunv@232-238-qd-monitortmp]# ls sendingincremental file list ./ deletingsms.log deletingmonitor_bak.sh deleting10.32.232.226_mongodb_cacti_stats.txt deleting10.32.232.225_mongodb_cacti_stats.txt deleting10.32.232.224_mongodb_cacti_stats.txt deleting10.32.232.222_mongodb_cacti_stats.txt deleting10.32.231.186_mongodb_cacti_stats.txt deleting10.32.231.177_mongodb_cacti_stats.txt sent55 bytes received 12 bytes 134.00 bytes/sec totalsize is 0 speedup is 0.00 [iyunv@232-238-qd-monitortmp]# ls [iyunv@232-238-qd-monitortmp]# [iyunv@232-239-qd-monitortmp]# ls 10.32.231.177_mongodb_cacti_stats.txt 10.32.232.222_mongodb_cacti_stats.txt 10.32.232.225_mongodb_cacti_stats.txt monitor_bak.sh 10.32.231.186_mongodb_cacti_stats.txt 10.32.232.224_mongodb_cacti_stats.txt 10.32.232.226_mongodb_cacti_stats.txt sms.log [iyunv@232-239-qd-monitortmp]# ls [iyunv@232-239-qd-monitortmp]# 四、配置initify(web1)tar -zxf inotify-tools-3.14.tar.gz cdinotify-tools-3.14 ./configure && make && makeinstall 1、详解inotify。root@232-238-qd-monitorbin]# pwd /usr/local/bin [iyunv@232-238-qd-monitorbin]# ls inotifywa* inotifywait inotifywatch [iyunv@232-238-qd-monitorbin]# inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。 Inotify 可以监视的文件系统事件包括: IN_ACCESS,即文件被访问 IN_MODIFY,文件被 write IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等 IN_CLOSE_WRITE,可写文件被 close IN_CLOSE_NOWRITE,不可写文件被 close IN_OPEN,文件被 open IN_MOVED_FROM,文件被移走,如 mv IN_MOVED_TO,文件被移来,如 mv、cp IN_CREATE,创建新文件 IN_DELETE,文件被删除,如 rm IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己 IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己 IN_UNMOUNT,宿主文件系统被 umount IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO) 备注:上面的文件也包括目录 Inotifywait是一个监控等待事件,可以配合shell脚本使用它,下面介绍一下常用的一些参数: -m,即--monitor,表示始终保持事件监听状态。
-r,即--recursive,表示递归查询目录。
-q,即--quiet,表示打印出监控事件。
-e,即--event,通过此参数可以指定要监控的事件,常见的事件有modify、delete、create、attrib等。
更详细的请参看man inotifywait。 inotify相关参数 inotify定义了下列的接口参数,可以用来限制inotify消耗kernel memory的大小。由于这些参数都是内存参数,因此,可以根据应用需求,实时的调节其大小。
/proc/sys/fs/inotify/max_queued_evnets
表示调用inotify_init时分配给inotifyinstance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
/proc/sys/fs/inotify/max_user_instances
表示每一个real user ID可创建的inotifyinstatnces的数量上限。
/proc/sys/fs/inotify/max_user_watches
表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小 16384 2、inotify监控脚本[iyunv@232-238-qd-monitor wangxu]# catrsync_inotify.sh #!/bin/bash #para host01=10.32.232.239 ##同步IP,可以设置多个 src=/usr/local/nagios/etc ##同步的本地目录 dst=nagios ##rsync服务端模块 user=rsync_backup ##rsync用户 rsync_passfile=/etc/rsync.password inotify_home=/usr/local #judge if[ ! -e "$src" ] \ ||[ ! -e "${rsync_passfile}" ] \ ||[ ! -e "${inotify_home}/bin/inotifywait" ] \ ||[ ! -e "/usr/bin/rsync" ]; then echo "Check File and Folder" exit 9 fi ${inotify_home}/bin/inotifywait -mrq--timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -eclose_write,delete,create,attrib $src \ |while read file do #rsync -avzP --delete --timeout=100--password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null2>&1 cd $src && rsync -aruz -R--delete ./ --timeout=100$user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1 done exit0 [iyunv@232-238-qd-monitorwangxu]# [ 2、执行脚本。[iyunv@232-238-qd-monitoretc]# sh /root/wangxu/rsync_inotify.sh & [iyunv@232-238-qd-monitorwangxu]# ps -ef | grep inotify root 8534 26546 0 09:56 pts/0 00:00:00 grep inotify root 30432 26546 0 09:48 pts/0 00:00:00 sh /root/wangxu/rsync_inotify.sh root 30433 30432 0 09:48 pts/0 00:00:00 /usr/local/bin/inotifywait -mrq--timefmt %d/%m/%y %H:%M --format %T %w%f -e close_write,delete,create,attrib/usr/local/nagios/etc root 30434 30432 0 09:48 pts/0 00:00:00 sh /root/wangxu/rsync_inotify.sh [iyunv@232-238-qd-monitorwangxu]# 3、检查测试。[iyunv@232-238-qd-monitor etc]# ls cgi.cfg htpasswd.bak.20140326 htpasswd.users nagios.cfg nrpe.cfg objects.yuan resource.cfg htpasswd htpasswd.bk htpasswd.users.bak nagios.cfg.yuan objects pnp server [iyunv@232-238-qd-monitor etc]# [iyunv@232-239-qd-monitor etc]# pwd /usr/local/nagios/etc [iyunv@232-239-qd-monitor etc]# ls cgi.cfg htpasswd.bak.20140326 htpasswd.users nagios.cfg nrpe.cfg objects.yuan resource.cfg htpasswd htpasswd.bk htpasswd.users.bak nagios.cfg.yuan objects pnp server [iyunv@232-239-qd-monitoretc]# [iyunv@232-238-qd-monitoretc]# mkdir test [iyunv@232-238-qd-monitoretc]# echo "123" >> wangxu.txt [iyunv@232-238-qd-monitoretc]# ls cgi.cfg htpasswd.bak.20140326 htpasswd.users nagios.cfg nrpe.cfg objects.yuan resource.cfg test htpasswd htpasswd.bk htpasswd.users.bak nagios.cfg.yuan objects pnp server wangxu.txt [iyunv@232-238-qd-monitoretc]# [iyunv@232-239-qd-monitor etc]# ls cgi.cfg htpasswd.bak.20140326 htpasswd.users nagios.cfg nrpe.cfg objects.yuan resource.cfg test htpasswd htpasswd.bk htpasswd.users.bak nagios.cfg.yuan objects pnp server wangxu.txt [iyunv@232-239-qd-monitoretc]#
|