1、架构设计图
2、设计说明
1. 基础架构为LAMP环境,采用keepalived实现zabbix服务器高可用,保证主server的mysql或者httpd宕掉后能切换到从server。
2、数据库做主主同步,保证两边服务器数据的一致性,实现数据库的高可用。
3、采用unison同步软件保证不管修改那台服务器配置,zabbix配置目录及web目录内容的一致,实现文件双向同步。
3、网络环境
zabbix01:192.168.8.117
zabbix02:192.168.8.118
vip:192.168.8.111
4、软件版本
OS:CentOS release 6.9 X86_64
MYSQL:Percona-Server-5.6
NGINX:tengine-2.2.0
PHP: php-7.0.16
ZABBIX:zabbix3.2.6
ZLIB:zlib-1.2.11
OPENSSL:openssl-1.0.2
PCRE:pcre-8.39
CAML:ocaml-3.10.2
UNISON:unison-2.13.16
INOTIFY-TOOLS:inotify-tools-3.13
5、基础调试
yum update
yum groupinstall '开发工具'
vim /etc/selinux/config
selinux=disabled
:wq
iptables -F
/etc/init.d/iptables save
/etc/init.d/iptables stop
chkconfig iptables off
/etc/init.d/postfix stop && chkconfig postfix off
yum install gcc gcc-c++ libxml2 libxml2-devel libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel openssl-devel bzip2-devel recode-devel openldap openldap-devel net-snmp net-snmp-devel libcurl-devel libcurl vim wget ntp jemalloc -y
reboot
6、LNMP环境安装
安装pcre
tar xf pcre-8.39.tar.gz
cd pcre-8.39
./configure --prefix=/usr/local/pcre/ && make && make install
openssl安装
tar xf openssl-1.0.2.tar.gz
cd openssl-1.0.2
./config enable-shared --prefix=/usr/local/openssl/ && make && make test && make install
zlib安装
tar xf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib && make &&make install
nginx安装
groupadd web
useradd -s /sbin/nologin -g web web
tar xf tengine-2.2.0.tar.gz
./configure --prefix=/usr/local/nginx --user=web --group=web --with-pcre=/root/pcre-8.39 --with-openssl=/root/openssl-1.0.2 --with-zlib=/root/zlib-1.2.11 --with-file-aio --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module
make && make install
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
chkconfig nginx on
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
chmod +x /etc/init.d/nginx
php安装
tar xf php-7.0.16.tar.gz
cd php-7.0.16
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local/lib/libconv --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath --enable-zip --enable-pcntl --enable-exif --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-opcache --enable-session --enable-mbregex --with-fpm-user=vagrant --with-fpm-group=nogroup --enable-wddx --with-mcrypt --enable-gd-native-ttf --enable-gd-jis-conv --with-openssl --with-zlib=/usr --with-recode=/usr --with-xmlrpc --enable-inline-optimization --disable-debug --disable-rpath --with-ldap
make
make test
make install
vim /etc/init.d/php-fpm
#! /bin/sh
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
prefix=/usr/local/php
exec_prefix=${prefix}
php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"
wait_for_pid () {
try=0
while test $try -lt 35 ; do
case "$1" in
'created')
if [ -f "$2" ] ; then
try=''
break
fi
;;
'removed')
if [ ! -f "$2" ] ; then
try=''
break
fi
;;
esac
echo -n .
try=`expr $try + 1`
sleep 1
done
}
case "$1" in
start)
echo -n "Starting php-fpm "
$php_fpm_BIN --daemonize $php_opts
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
wait_for_pid created $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Gracefully shutting down php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -QUIT `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ ! -r $php_fpm_PID ] ; then
echo "php-fpm is stopped"
exit 0
fi
PID=`cat $php_fpm_PID`
if ps -p $PID | grep -q $PID; then
echo "php-fpm (pid $PID) is running..."
else
echo "php-fpm dead but pid file exists"
fi
;;
force-quit)
echo -n "Terminating php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -TERM `cat $php_fpm_PID`
wait_for_pid removed $php_fpm_PID
if [ -n "$try" ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
echo -n "Reload service php-fpm "
if [ ! -r $php_fpm_PID ] ; then
echo "warning, no pid file found - php-fpm is not running ?"
exit 1
fi
kill -USR2 `cat $php_fpm_PID`
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
exit 1
;;
esac
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on
ln -s /usr/local/php/bin/php /usr/local/bin/php
ln -s /usr/local/php/bin/php-config /usr/local/bin/php-config
ln -s /usr/local/php/bin/phpize /usr/local/bin/phpize
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp php.ini-development /usr/local/php/etc/php.ini
ln -s /user/local/php/etc/php.ini /etc
vim www.conf
将user和group替换为web
nginx和php整合
cat /user/local/nginx/conf/nginx.conf
mysql安装
tar xf Percona-Server-5.6.33-79.0-r2084bdb-el6-x86_64-bundle.tar
yum localinstall Percona-Server-*
/etc/init.d/mysql start
ps_tokudb_admin --enable -uroot
更改数据引擎
vim /etc/my.cnf
在[mysqld]下添加
default-storage-engine=TokuDB
:wq
/etc/init.d/mysql restart
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| TokuDB | DEFAULT | Percona TokuDB Storage Engine with Fractal Tree(tm) Technology | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | YES | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.03 sec)
chkconfig mysql on
启动服务
/etc/init.d/nginx restart
/etc/ini.d/php-fpm restart
7、安装zabbix(zabbix01和zabbix02相同)
groupadd -g 201 zabbix
useradd -g zabbix -u 201 -s /sbin/nologin zabbix
tar xf zabbix-3.2.6.tar.gz
cd zabbix-3.2.6
./configure --prefix=/usr/local/zabbix --enable-server --enable-proxy --enable-agent --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libcurl --with-ldap --with-libxml2
make && make install
创建并导入数据(注意顺序)
mysql -e "create database zabbix default charset utf8;"
mysql -e "grant all on zabbix.* to zabbix@localhost identified by 'zabbix';"
mysql -uzabbix -pzabbix zabbix<./database/mysql/schema.sql
mysql -uzabbix -pzabbix zabbix<./database/mysql/p_w_picpaths.sql
mysql -uzabbix -pzabbix zabbix<./database/mysql/data.sql
mkdir /var/log/zabbix
chown -R zabbix.zabbix /var/log/zabbix
软链
ln -s /usr/local/zabbix/etc/ /etc/zabbix
ln -s /usr/local/zabbix/bin/* /usr/bin/
ln -s /usr/local/zabbix/sbin/* /usr/sbin/
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
ln -s /usr/local/zabbix/bin/* /usr/local/bin/
cp misc/init.d/fedora/core/zabbix_* /etc/init.d/
cp -r frontends/php/ /usr/local/nginx/html/zabbix/
chmod 755 /etc/init.d/zabbix_*
sed -i "s@BASEDIR=/usr/local@BASEDIR=/usr/local/zabbix@g" /etc/init.d/zabbix_server
sed -i "s@BASEDIR=/usr/local@BASEDIR=/usr/local/zabbix@g“ /etc/init.d/zabbix_agentd
sed -i "s@#DBPassword=@DBPassword=zabbix@g" /etc/zabbix/zabbix_server.conf
sed -i "s@# DBPassword=@DBPassword=zabbix@g" /etc/zabbix/zabbix_server.conf
sed -i "s@Server=127.0.0.1@Server=127.0.0.1,192.168.8.118@g" /etc/zabbix/zabbix_agentd.conf
sed -i "s@ServerActive=127.0.0.1@ServerActive=$IP:10051@g" /etc/zabbix/zabbix_agentd.conf
sed -i "s@tmp/zabbix_agentd.log@var/log/zabbix/zabbix_agentd.log@g" /etc/zabbix/zabbix_agentd.conf
sed -i "s@^# UnsafeUserParameters=0@UnsafeUserParameters=1\n@g" /etc/zabbix/zabbix_agentd.conf
cp -r frontends/php/ /usr/local/nginx/html/zabbix/
/etc/init.d/zabbix_server start
sed -i 's/max_execution_time = 30/max_execution_time = 300/' /etc/php.ini
sed -i 's/max_input_time = 60/max_input_time = 300/' /etc/php.ini
sed -i 's/post_max_size = 8M/post_max_size = 16M/' /etc/php.ini
sed -i 's/;date.timezone =/date.timezone = PRC/' /etc/php.ini
sed -i 's/;always_populate_raw_post_data/always_populate_raw_post_data/' /etc/php.ini
修改zabbix_server.conf参数:
SourceIP=192.168.8.111
DBName=zabbix 数据库名称
DBUser=zabbix 连接数据库的账号,与Mysql授权账号对应
DBPassword=zabbix 连接数据库的密码,与Mysql授权账号的密码对应
修改zabbix_agentd.conf参数:
Server=192.168.8.111
ServerActive=10.10.1.98
Hostname=Zabbix1 #zabbix2修改为自己的hostname
将server1和server2的监控在web界面中添加
8、安装keepalived实现主从切换
yum install keepalived
创建keepalived状态检查脚本:
vim /etc/keepalived/keepalived_check.sh
#!/bin/bash
mysql_check=`ps -C mysqld --no-header | wc -l`
nginx_check=`ps -C nginx --no-header | wc -l`
case $1 in
mysqld)
if [ $mysql_check -eq 0 ];then
service mysqld start
sleep 3
if [ $mysql_check -eq 0 ];then
service keepalived stop
fi
fi
;;
nginx)
if [ $nginx_check -eq 0 ];then
service nginx start
sleep 3
if [ $nginx_check -eq 0 ];then
service keepalived stop
fi
fi
;;
esac
chmod 755 /etc/keepalived/keepalived_check.sh
创建zabbix重启脚本(主从切换的时候执行此脚本,必须在keepalived配置文件中增加此脚本,否则,主从切换后,web查看host的状态是错误,但是后台可以获得数据)
vi /etc/keepalived/keepalived_zabbix.sh
#!/bin/bash
sleep 60
/etc/init.d/zabbix_server restart
chmod 777 /etc/keepalived/keepalived_zabbix.sh
以上内容server1和server2配置均相同
修改keepalived配置文件(注意zabbix1/zabbix2的状态和优先级)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected] #设置报警地址,可以设置多个,每行1个
[email protected] #需安装sendmail,并开启邮件报警
}
notification_email_from [email protected]
smtp_server 127.0.0.1 #设置SMTP Server地址
smtp_connect_timeout 30
router_id okooo
}
vrrp_script mysqld #vrrp脚本命名
{
script "/etc/keepalived/keepalived_check.sh mysqld" #要执行的脚本
interval 2 #脚本指定间隔,秒
weight -30 #优先级(如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加,如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少)
fall 2 #尝试两次都成功才成功
rise 2 #尝试两次都失败才失败
}
vrrp_script nginx
{
script "/etc/keepalived/keepalived_check.sh nginx"
interval 2
weight -20
fall 2 #尝试两次都成功才成功
rise 2 #尝试两次都失败才失败
}
vrrp_instance zabbix_server{
state MASTER/BACKUP #指定Keepalived的角色,MASTER为主,BACKUP为备
interface eth0 #绑定真实的物网卡
virtual_router_id 1 #虚拟路由编号,主备要一致
priority 150/100 #定义优先级,数字越大,优先级越高,主DR必须大于备用DR
advert_int 1 #检查间隔,默认为1s
authentication {
auth_type PASS #认证方式,PASS或AH两种
auth_pass okooo #认证密码,主备服务器要一致
}
track_script #调用上面定义的检查脚本
{
mysqld
}
track_script
{
nginx
}
virtual_ipaddress {
192.168.8.111 #定义虚拟IP(VIP),可多设,每行一个
}
notify_master "/etc/keepalived/keepalived_zabbix.sh" #指定当切换到master时,执行的脚本
notify_backup "/etc/keepalived/keepalived_zabbix.sh" #指定当切换到backup时,执行的脚本
#notify_fault "/sbin/service zabbix_server stop" #故障时执行的脚本
}
/etc/init.d/keepalived start
chkconfig keepalived on
9、Mysq主主同步
修改数据库配置文件(zabbix1和zabbix2的server-id不同)
[root@zabbix02 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
default-storage-engine=TokuDB
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=2 #(zabbix01 设置为1)
log-bin=mysql-bin # 开启bin日志
expire_logs_days=365 # bin日志过期清理时间
max_binlog_size=314572800 # 每个bin日志大小
auto-increment-increment = 2 #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
slave-skip-errors = all #忽略所有复制产生的错误
binlog-do-db=zabbix # 同步原数据库名
binlog-ignore-db=mysql # 不同步数据库名
binlog-ignore-db=test
binlog-ignore-db=performance_schema
binlog-ignore-db=information_schema
replicate-do-db=zabbix #在slave库中指定同步那些库的binlog日志
replicate-ignore-db=mysql
replicate-ignore-db=test
replicate-ignore-db=performance_schema
replicate-ignore-db=information_schema
[mysqld_safe]
thp-setting=never
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
建立同步数据同步账号
server1
grant replication slave on *.* to 'tongbu'@'192.168.8.118' identified by 'tongbu@123';
flush privileges;
server2
grant replication slave on *.* to 'tongbu'@'192.168.8.117' identified by 'tongbu@123';
flush privileges;
检查同步账号
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,host from user;
+--------+---------------+
| user | host |
+--------+---------------+
| root | 127.0.0.1 |
| tongbu | 192.168.8.117 |
| root | ::1 |
| | localhost |
| root | localhost |
| zabbix | localhost |
| | zabbix02 |
| root | zabbix02 |
+--------+---------------+
8 rows in set (0.00 sec)
获取zabbix binlog文件和ID
zabbix01
mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| mysql-bin.000001 | 4290 | zabbix | mysql,test,performance_schema,information_schema | |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)
zabbix02
mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
| mysql-bin.000001 | 3305 | zabbix | mysql,test,performance_schema,information_schema | |
+------------------+----------+--------------+--------------------------------------------------+-------------------+
1 row in set (0.00 sec)
启动互为主从模式
zabbix01
mysql> change master to master_host='192.168.8.118', master_user='tongbu', master_password='tongbu@123', master_log_file='mysql-bin.000001',master_log_pos=3305;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.118
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 127078
Relay_Log_File: zabbix01-relay-bin.000002
Relay_Log_Pos: 124056
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: zabbix
Replicate_Ignore_DB: mysql,test,performance_schema,information_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
zabbix02
mysql> change master to master_host='192.168.8.117', master_user='tongbu', master_password='tongbu@123', master_log_file='mysql-bin.000001',master_log_pos=4290;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.8.117
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 76847
Relay_Log_File: zabbix02-relay-bin.000002
Relay_Log_Pos: 72840
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: zabbix
Replicate_Ignore_DB: mysql,test,performance_schema,information_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
重启服务:
# /etc/init.d/nginx restart
# /etc/init.d/zabbix_agentd restart
# /etc/init.d/zabbix_server restart
10、实现web目录双向实时同步
首先两台server间需要完成ssh的免密登陆
10.1、安装unison同步工具(zabbix1同zabbix2)
tar zxvf ocaml-3.10.2.tar.gz
cd ocaml-3.10.2
./configure
make world opt
make install
tar zxvf unison-2.13.16.tar.gz
cd unison-2.13.16
make UISTYLE=text THREADS=true STATIC=true
cp ./unison /usr/local/bin/
/usr/local/bin/unison #生成的这个文件之后,首先需要执行一次,会在当前登陆的用户家目录下生成一个.unison的目录,并且在目录下生成一个defualt.prf文件,以后unison的使用都是通过修改稿配置文件,执行unison这个命令进行。
10.2、安装inotify工具inotify-tools
[root@zabbix01 log]# uname -a
Linux zabbix01 2.6.32-696.3.1.el6.x86_64 #1 SMP Tue May 30 19:52:55 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@zabbix01 log]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jun 22 15:23 max_queued_events
-rw-r--r-- 1 root root 0 Jun 22 15:23 max_user_instances
-rw-r--r-- 1 root root 0 Jun 22 15:23 max_user_watches
以上输出代表系统内核默认有inotify,接下来安装插件即可
tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
[root@zabbix01 log]# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 38582 Jun 22 15:36 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 40353 Jun 22 15:36 /usr/local/bin/inotifywatch
inotify-tools安装完成后,会生成inotifywait和inotifywatch两个指令,其中,inotifywait用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。
10.3、修改unison配置文件,编写同步脚本进行实施监控同步(注意目录和IP外zabbix1同zabbix2)
整个过程是通过使用innotify对目录和文件进行检查,然后根据变动调用unison工具进行同步
[root@zabbix01 .unison]# cat zabbix.prf
#同步zabbix配
# Unison preferences file
root=/usr/local/zabbix/
root=ssh://[email protected]//usr/local/zabbix/ #zabbix2换成zabbix1的IP
batch = true
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
#confirmbigdel = true
log = true
logfile = /root/.unison/unison.log
maxthreads = 300
retry = 3
path=etc
path=sbin
path=lib
path=share/zabbix/
path=bin/
[root@zabbix01 .unison]# cat webapps.prf
root=/usr/local/nginx/html/
#本地文件夹
root=ssh://[email protected]//usr/local/nginx/html
#远程文件夹zabbix2换成zabbix1的IP
batch = true
#全自动模式,接受并执行默认动作
owner = true
#表示保持同步的文件属主信息
group = true
#表示保持同步的文件属组信息
perms = -1
#表示保持同步的文件读写权限。
fastcheck = false
#true表示同步时通过文件的创建时间来比较两地文件;false表示同步时通过比较两地文件内容。
rsync = false
#默认值是true,用于激活rsync传输模式。
sshargs = -C
#使用ssh的压缩传输方式。
xferbycopying = true
#优化传输参数,默认值为true。
#confirmbigdel = false
#默认值为true,表示当需要同步的两个目录有一个为空时,unison将停止。设置为false可以保证当需要同步的某个目录为空时,unison不会停止运转。
log = true
#表示在终端输出运行信息。
logfile = /root/.unison/unison.log
#指定同时输出写入log文件。
maxthreads = 300
#指定同步时最大线程数
repeat = 1
#表示间隔1秒后开始一次新的同步检查
retry = 3
#指定失败重试次数
path=zabbix/
#同步指定的子目录及文件,是相对路径
10.4、创建同步脚本(zabbix1同zabbix2)
[root@zabbix01 .unison]# cat /scripts/unison.sh
#!/bin/bash
dir1="/usr/local/nginx/html/zabbix/" #需要检测的目录#
dir2="/usr/local/zabbix/" #需要检测的目录#
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $dir1 $dir2 | while read line; do
#通过inotifywait来检测这个两个目录的改动情况#
/usr/local/bin/unison webapps #调用配置文件webapps.prf同步#
/usr/local/bin/unison zabbix #调用配置文件zabbix.prf同步#
echo -n "`whoami` `date | cut -d " " -f1-5` " >> /var/log/inotify.log #记录目录被更改的时间到日志,并记录当前登陆的用户#
echo "$line" >> /var/log/inotify.log #记录目录哪些文件被更改情况#
done
后台执行脚本:
/usr/bin/nohup bash /scripts/unison.sh &
echo "/usr/bin/nohup bash /scripts/unison.sh &" >>/etc/rc.local