shell實戰:用shell實現自動接收haproxy設定檔並載入,讓配置生效

來源:互聯網
上載者:User

shell實戰:用shell實現自動接收haproxy設定檔並載入,讓配置生效 001haproxy的自動接收配置並載入002關於後台接收配置進程功能介紹:0031、是個while 1 後台進程0042、目前是30s檢查一次,是否有新的配置過來,有則繼續,沒有則休息30s,回到步驟10053、如果有,則調用ha命令檢查當前收到的設定檔是否在文法問題,如果沒問題則繼續,有問題,則發郵件警示,休息30s,回到步驟10064、沒有文法問題,則將舊的設定檔備份出去,將新收到的檔案,放到對應的位置。此時會檢查下放過去的和收到的是否大小一致。不一致,退出並警示,休息30s,回到步驟1,大小一樣,則繼續0075、此時重新reload配置,休息1s,並調用系統命令檢測ha服務是否正常存在,不正常,則重啟ha進程,並警示,直到ha進程正常 6、最後將接收目錄下的設定檔,備份到其他位置0087、休息30s,進入下一次迴圈,回到步驟1009實現如下:010# cat haconf_recive.sh011#!/bin/sh012#recive_server.sh013#haproxy014#檢測指定目錄下是否有新設定檔過來,如果有設定檔,則檢查文法,並重新載入服務015#檢測時,警示相關016#1、文法有錯誤時,郵件警示,服務載入失敗時警示017#全域變數018recive_path='/usr/sa_yunwei/recive_doc/'019backup_path='/usr/sa_yunwei/recive_backup/'020current_conf_file='/etc/haproxy/haproxy.cfg'021nowtime=`date +"%Y-%m-%d %H:%M:%S"`022push_mail()023{024tag=$1025local_ip=`ifconfig |grep "inet addr:10"|awk -F':' '{print $2}'|awk '{print $1}'`026zhengwen="the haproxy:$local_ip at $nowtime haproxy conf $tag,please check it"027echo "$zhengwen" | /usr/bin/mail -s "haproxy alert: ${zhengwen}" scpmandemain@scpman.com028}029#push_mail 'reload faild!'030check_path()031{032if [ -d $1 ]033then034echo $1035else036mkdir -p $1037fi038}039check_path $recive_path040check_path $backup_path041haproxy_shouhu()042{043pidof haproxy044if [ $? = 0 ]045then046    echo047else048    /etc/init.d/haproxy start049    sleep 1050    haproxy_shouhu051    push_mail 'ha server will start by haproxy_shouhu'052fi053}054check_recive()055{056ntime=`date +"%Y%m%d"`057newkey="new_${ntime}_haproxy.cfg"058rec_file="$recive_path$newkey"059hacmd=`which haproxy`060reload_conf()061{062cp -rp $current_conf_file ${backup_path}`date +"%Y%m%d%H%M%S"_haproxy.cfg`063cp -rp $rec_file $current_conf_file064a=`ls -l $current_conf_file |awk '{print $5}'`065b=`ls -l $rec_file |awk '{print $5}'`066if [ $a = $b ]067then068 /etc/init.d/haproxy reload069 haproxy_shouhu070 mv $rec_file ${backup_path}`date +"%Y%m%d%H%M%S"_haproxy.cfg_old`071else072    echo can not reload, $rec_file != $current_conf_file073fi074}075check_conf_parse()076{077$hacmd -f $rec_file -c078if [ $? = 0 ]079then080    echo recive file parse ok!now reload!081    reload_conf082else083    echo recive file parse faild!!084    push_mail 'ha recive conf file yufa wrong!'085fi086}087if [ -f $rec_file ]088then089echo recive file: $rec_file090check_conf_parse091else092echo no recive file093fi094}095while [ 1 ]096do097        check_recive098        sleep 30099done100運行後樣子如下101# sh haconf_recive.sh102/usr/sa_yunwei/recive_doc/103/usr/sa_yunwei/recive_backup/104no recive file #30s來一次105no recive file106no recive file107放到後台運行之108/bin/bash haconf_recive.sh 2>&1 &109這樣就好了110服務端就算啟動完成了111用戶端怎樣送配置過來呢?利用rsync推送過來112rsync設定檔如下:113# cat /etc/rsyncd.conf114uid = root115gid = root116use chroot = no117read only = true118max connections = 4119syslog facility = local5120pid file = /var/run/rsyncd.pid121log file = /var/log/rsyncd.log122  123hosts allow =10.0.0.0/8124[haconf]125path = /usr/sa_yunwei/recive_doc/126read only = no127rsync許可權已經添加,推送命令如下: rsync -av 新產生的ha設定檔 10.0.4.2::haconf/128  129 新產生的設定檔規則:  new_當天日期_haproxy.cfg130  131 舉個例子:  產生的新設定檔名new_20130827_haproxy.cfg  推送 rsync -av new_20130827_haproxy.cfg  10.0.4.2::haconf/    只要將此檔案推到對應機器,haproxy上會有後台進程(我們上面的指令碼負責)負責接收132這樣就實現的133haproxy的自動接收配置並載入。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.