歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> shell實戰:用shell實現自動接收haproxy配置文件並加載,讓配置生效

shell實戰:用shell實現自動接收haproxy配置文件並加載,讓配置生效

日期:2017/3/1 14:40:58   编辑:SHELL編程
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}" [email protected] 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的自動接收配置並加載。
Copyright © Linux教程網 All Rights Reserved