shell实战:用shell实现自动接收haproxy配置文件并加载,让配置生效


shell实战:用shell实现自动接收haproxy配置文件并加载,让配置生效
 
001
haproxy的自动接收配置并加载
002
关于后台接收配置进程功能介绍:
003
1、是个while 1 后台进程
004
2、目前是30s检查一次,是否有新的配置过来,有则继续,没有则休息30s,回到步骤1
005
3、如果有,则调用ha命令检查当前收到的配置文件是否在语法问题,如果没问题则继续,有问题,则发邮件告警,休息30s,回到步骤1
006
4、没有语法问题,则将旧的配置文件备份出去,将新收到的文件,放到对应的位置。此时会检查下放过去的和收到的是否大小一致。不一致,退出并告警,休息30s,回到步骤1,大小一样,则继续
007
5、此时重新reload配置,休息1s,并调用系统命令检测ha服务是否正常存在,不正常,则重启ha进程,并告警,直到ha进程正常 6、最后将接收目录下的配置文件,备份到其他位置
008
7、休息30s,进入下一次循环,回到步骤1
009
实现如下:
010
# cat haconf_recive.sh
011
#!/bin/sh
012
#recive_server.sh
013
#haproxy
014
#检测指定目录下是否有新配置文件过来,如果有配置文件,则检查语法,并重新加载服务
015
#检测时,告警相关
016
#1、语法有错误时,邮件报警,服务加载失败时报警
017
#全局变量
018
recive_path='/usr/sa_yunwei/recive_doc/'
019
backup_path='/usr/sa_yunwei/recive_backup/'
020
current_conf_file='/etc/haproxy/haproxy.cfg'
021
nowtime=`date +"%Y-%m-%d %H:%M:%S"`
022
push_mail()
023
{
024
tag=$1
025
local_ip=`ifconfig |grep "inet addr:10"|awk -F':' '{print $2}'|awk '{print $1}'`
026
zhengwen="the haproxy:$local_ip at $nowtime haproxy conf $tag,please check it"
027
echo "$zhengwen" | /usr/bin/mail -s "haproxy alert: ${zhengwen}" scpmandemain@scpman.com
028
}
029
#push_mail 'reload faild!'
030
check_path()
031
{
032
if [ -d $1 ]
033
then
034
echo $1
035
else
036
mkdir -p $1
037
fi
038
}
039
check_path $recive_path
040
check_path $backup_path
041
haproxy_shouhu()
042
{
043
pidof haproxy
044
if [ $? = 0 ]
045
then
046
    echo
047
else
048
    /etc/init.d/haproxy start
049
    sleep 1
050
    haproxy_shouhu
051
    push_mail 'ha server will start by haproxy_shouhu'
052
fi
053
}
054
check_recive()
055
{
056
ntime=`date +"%Y%m%d"`
057
newkey="new_${ntime}_haproxy.cfg"
058
rec_file="$recive_path$newkey"
059
hacmd=`which haproxy`
060
reload_conf()
061
{
062
cp -rp $current_conf_file ${backup_path}`date +"%Y%m%d%H%M%S"_haproxy.cfg`
063
cp -rp $rec_file $current_conf_file
064
a=`ls -l $current_conf_file |awk '{print $5}'`
065
b=`ls -l $rec_file |awk '{print $5}'`
066
if [ $a = $b ]
067
then
068
 /etc/init.d/haproxy reload
069
 haproxy_shouhu
070
 mv $rec_file ${backup_path}`date +"%Y%m%d%H%M%S"_haproxy.cfg_old`
071
else
072
    echo can not reload, $rec_file != $current_conf_file
073
fi
074
}
075
check_conf_parse()
076
{
077
$hacmd -f $rec_file -c
078
if [ $? = 0 ]
079
then
080
    echo recive file parse ok!now reload!
081
    reload_conf
082
else
083
    echo recive file parse faild!!
084
    push_mail 'ha recive conf file yufa wrong!'
085
fi
086
}
087
if [ -f $rec_file ]
088
then
089
echo recive file: $rec_file
090
check_conf_parse
091
else
092
echo no recive file
093
fi
094
}
095
while [ 1 ]
096
do
097
        check_recive
098
        sleep 30
099
done
100
运行后样子如下
101
# sh haconf_recive.sh
102
/usr/sa_yunwei/recive_doc/
103
/usr/sa_yunwei/recive_backup/
104
no recive file #30s来一次
105
no recive file
106
no recive file
107
放到后台运行之
108
/bin/bash haconf_recive.sh 2>&1 &
109
这样就好了
110
服务端就算启动完成了
111
客户端怎样送配置过来呢?利用rsync推送过来
112
rsync配置文件如下:
113
# cat /etc/rsyncd.conf
114
uid = root
115
gid = root
116
use chroot = no
117
read only = true
118
max connections = 4
119
syslog facility = local5
120
pid file = /var/run/rsyncd.pid
121
log file = /var/log/rsyncd.log
122
  
123
hosts allow =10.0.0.0/8
124
[haconf]
125
path = /usr/sa_yunwei/recive_doc/
126
read only = no
127
rsync权限已经添加,推送命令如下: rsync -av 新生成的ha配置文件 10.0.4.2::haconf/
128
  
129
 新生成的配置文件规则:  new_当天日期_haproxy.cfg
130
  
131
 举个例子:  生成的新配置文件名new_20130827_haproxy.cfg  推送 rsync -av new_20130827_haproxy.cfg  10.0.4.2::haconf/    只要将此文件推到对应机器,haproxy上会有后台进程(我们上面的脚本负责)负责接收
132
这样就实现的
133
haproxy的自动接收配置并加载。
 

相关内容

    暂无相关文章