Shell practice: use shell to automatically receive and load haproxy configuration files to make the configuration take effect.

Source: Internet
Author: User
Tags haproxy

Shell practice: use shell to automatically receive and load the haproxy configuration file, so that the configuration takes effect. haproxy automatically receives the configuration and loads 002. Introduction to the background receiving configuration process: 0031. It is a while 1 background process 0042. Currently, it is a 30 s check. Is there any new configuration? If yes, continue. If not, rest for 30 s. Go back to step 10053. If yes, then, call the ha command to check whether the current configuration file is syntactically incorrect. If there is no problem, continue. If there is a problem, send an email with an alarm and wait for 30 s. Go back to step 10064 without Syntax problems, back up the old configuration file and place the new file to the corresponding location. In this case, check whether the size of the passed and received commands is the same. Inconsistent, exit and alert, rest for 30 s, return to step 1, the size is the same, continue 0075, re-reload configuration, rest for 1 s, call the system command to check whether the ha service exists normally. If the ha process is abnormal, restart the ha process and send an alarm until the ha process is normal. 6. Finally, the configuration file in the directory is received, back up to another location 0087, rest for 30 s, enter the next cycle, go back to step 1009 implementation as follows: 010 # cat haconf_recive.sh011 #! /Bin/sh012 # recive_server.sh013 # haproxy014 # check whether a new configuration file exists in the specified directory. If a configuration file exists, check the syntax and reload the service 015 # during detection, alarm-related 016 #1. Email alarm when syntax error occurs, alarm 017 # global variable expiration = '/usr/sa_yunwei/recive_doc/'019backup _ path ='/usr/sa_yunwei/recive_backup/'020current _ conf_file = '/etc /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 "027 echo" $ zhengwen "|/usr/bin/mail-s" haproxy alert: $ {zhengwen} "scpmandemain@scpman.com028} 029 # push_mail 'reload faild! '030check _ path () 031 {032if [-d $1] 033then034echo $ signature-p $1037fi038} 039check_path $ signature () 042 {043 pidof 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 {056 ntime = 'date + "% Y % m % d" '057newkey = "new _ $ {ntime} _ haproxy. cfg "058rec_file =" $ recive_path $ newkey "059 hacmd = 'which haproxy' 060reload _ conf () 061 {062cp-rp $ current_conf_file $ {backup_path} 'date + "% Y % m % d % H % M % S" _ haproxy. cfg '063cp-rp $ rec_f Ile $ 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. pai_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: $ restart no recive Failed} 095 while [1] After running zookeeper sleep 30099done100, the following 101 # sh restart/usr/sa_yunwei/recive_doc/103/usr/sa_yunwei/recive_backup file # 30s for one time 105no recive file=no recive file107 put it in the background to run 108/bin/bash haconf_recive.sh 2> & 1 & 109. This will make the Server Ready even if it is started. 111 how does the client Send configuration? The configuration file of javasrsync pushed by rsync is as follows: 113 # cat/etc/rsyncd. conf114uid = root115gid = root1_use chroot = no117read only = true118max connections = 4119 syslog facility = local5120pid file =/var/run/rsyncd. pid121log file =/var/log/rsyncd. log122 123 hosts allow = 10.0.0.0/8124 [haconf] 125 path =/usr/sa_yunwei/recive_doc/126 read only = now.rsync permission already added. The push command is as follows: rsync-av new ha configuration file 10.0.4.2: haconf/128 129 new configuration file rules Then: new _ day date _ haproxy. 2017130 131 for example: the generated new configuration file name new_20130827_haproxy.cfg pushes rsync-av new_20130827_haproxy.cfg 10.0.4.2: haconf/as long as the file is pushed to the corresponding machine, on haproxy, there will be background processes (the script above is responsible for) to receive and load the 133haproxy automatic receiving configuration implemented by 132.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.