shell实战:用shell实现自动接收haproxy配置文件并加载,让配置
发布时间:2020-12-15 07:15:43 所属栏目:安全 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 haproxy的自动接收配置并加载关于后台接收配置进程功能介绍: 1、是个while 1 后台进程 2、目前是30s检查一次,是否有新的配置过来,有则继续,没有则
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 haproxy的自动接收配置并加载 关于后台接收配置进程功能介绍: 1、是个while 1 后台进程 2、目前是30s检查一次,是否有新的配置过来,有则继续,没有则休息30s,回到步骤1 3、如果有,则调用ha命令检查当前收到的配置文件是否在语法问题,如果没问题则继续,有问题,则发邮件告警,休息30s,回到步骤1 4、没有语法问题,则将旧的配置文件备份出去,将新收到的文件,放到对应的位置。此时会检查下放过去的和收到的是否大小一致。不一致,退出并告警,休息30s,回到步骤1,大小一样,则继续 5、此时重新reload配置,休息1s,并调用系统命令检测ha服务是否正常存在,不正常,则重启ha进程,并告警,直到ha进程正常 6、最后将接收目录下的配置文件,备份到其他位置 7、休息30s,进入下一次循环,回到步骤1 实现如下: # cat haconf_recive.sh #!/bin/sh #recive_server.sh #haproxy #检测指定目录下是否有新配置文件过来,如果有配置文件,则检查语法,并重新加载服务 #检测时,告警相关 #1、语法有错误时,邮件报警,服务加载失败时报警 #全局变量 recive_path='/usr/sa_yunwei/recive_doc/' backup_path='/usr/sa_yunwei/recive_backup/' current_conf_file='/etc/haproxy/haproxy.cfg' nowtime=`date +"%Y-%m-%d %H:%M:%S"` push_mail() { tag=$1 local_ip=`ifconfig |grep "inet addr:10"|awk -F':' '{print $2}'|awk '{print $1}'` zhengwen="the haproxy:$local_ip at $nowtime haproxy conf $tag,please check it" echo "$zhengwen" | /usr/bin/mail -s "haproxy alert: ${zhengwen}" [email?protected] } #push_mail 'reload faild!' check_path() { if [ -d $1 ] then echo $1 else mkdir -p $1 fi } check_path $recive_path check_path $backup_path haproxy_shouhu() { pidof haproxy if [ $? = 0 ] then echo else /etc/init.d/haproxy start sleep 1 haproxy_shouhu push_mail 'ha server will start by haproxy_shouhu' fi } check_recive() { ntime=`date +"%Y%m%d"` newkey="new_${ntime}_haproxy.cfg" rec_file="$recive_path$newkey" hacmd=`which haproxy` reload_conf() { cp -rp $current_conf_file ${backup_path}`date +"%Y%m%d%H%M%S"_haproxy.cfg` cp -rp $rec_file $current_conf_file a=`ls -l $current_conf_file |awk '{print $5}'` b=`ls -l $rec_file |awk '{print $5}'` if [ $a = $b ] then /etc/init.d/haproxy reload haproxy_shouhu mv $rec_file ${backup_path}`date +"%Y%m%d%H%M%S"_haproxy.cfg_old` else echo can not reload,$rec_file != $current_conf_file fi } check_conf_parse() { $hacmd -f $rec_file -c if [ $? = 0 ] then echo recive file parse ok!now reload! reload_conf else echo recive file parse faild!! push_mail 'ha recive conf file yufa wrong!' fi } if [ -f $rec_file ] then echo recive file: $rec_file check_conf_parse else echo no recive file fi } while [ 1 ] do check_recive sleep 30 done 运行后样子如下 # sh haconf_recive.sh /usr/sa_yunwei/recive_doc/ /usr/sa_yunwei/recive_backup/ no recive file #30s来一次 no recive file no recive file 放到后台运行之 /bin/bash haconf_recive.sh 2>&1 & 这样就好了 服务端就算启动完成了 客户端怎样送配置过来呢?利用rsync推送过来 rsync配置文件如下: # cat /etc/rsyncd.conf uid = root gid = root use chroot = no read only = true max connections = 4 syslog facility = local5 pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log hosts allow =10.0.0.0/8 [haconf] path = /usr/sa_yunwei/recive_doc/ read only = no rsync权限已经添加,推送命令如下: rsync -av 新生成的ha配置文件 10.0.4.2::haconf/ 新生成的配置文件规则: new_当天日期_haproxy.cfg 举个例子: 生成的新配置文件名new_20130827_haproxy.cfg 推送 rsync -av new_20130827_haproxy.cfg 10.0.4.2::haconf/ 只要将此文件推到对应机器,haproxy上会有后台进程(我们上面的脚本负责)负责接收 这样就实现的 haproxy的自动接收配置并加载。 http://www.scpman.com/article/show/110/?classid=10 哥的文章全是原创,拿去做讲课案例的请注明出处。 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |