shell指令碼批量組建組態檔案

來源:互聯網
上載者:User

標籤:

  如果管理的網站和伺服器較多的情況下,每次修改設定檔都相當痛苦。因而想到了用shell指令碼來批量組建組態檔案和配置資料。下面這個指令碼是為了批量產生nagios監控設定檔的一個shell指令碼程式。其原理是事先定義一個shell指令碼模板,然後每個需要監控的網站複製一份模板替換掉模板檔案裡面的變數。

  1、準備模板檔案webcheck.template 

more webcheck.template ###################WEBURL define start###################define service{        use                             generic-service         ; Name of service template to use        host_name                       webcheck        service_description             WEBURL        check_command           check_webpage!-H WEBURL -u INDEX        is_volatile 0        max_check_attempts 3        check_interval 1        retry_interval 1        check_period 24x7        notification_interval 5        notification_period 24x7        notification_options w,u,r,c        contact_groups admins        }###################WEBURL define end###################

  變數為WEBURL和INDEX

  2、網站列表檔案weblist.txt

www.aaa.com \\/bbs.bbb.com \\/www.ccc.com \\/

  weblist.txt有兩個field,第一個field為網域名稱,第二個field為網站對應的url。如第一個網域名稱為www.aaa.com/

  3、批量產生指令檔create.sh

[[email protected] webcheckes]# more create.sh #!/bin/bash PATH=/bin:$PATH:$HOME/bin:/sbin:/usr/bin:/usr/sbin export PATH#echo $PATHusage () {         echo -en "USAGE: $0 [web list] or $0 [template] [web list]\nFor example: $0 host.template host.list(Field : [WEB URL] [INDEX WEB PAGE])\n" 1>&2         exit 1 }  if [ $# -gt 2 ];then         usage         exit 1 fi  case "$#" in         2)                 template=$1                 host_list=$2         ;;         1)                 template=‘webcheck.template‘                 host_list=$1         ;;         0)         #       template=‘webcheck.template‘         #        host_list=‘host.list‘                 usage         ;; esac  if [ ! -f "${template}" ];then         echo "template : ${template} not exist!" 1>&2         exit 1 fi  if [ ! -f "${host_list}" ];then         echo "host list : ${host_list} not exist!" 1>&2         exit 1 fi  #echo $PWD/${host_list}WEBTEMP="wcalltemp.txt"rm $PWD/${WEBTEMP}#cat $PWD/${host_list}/bin/cat $PWD/${host_list}| while read weburl indexdo         #echo "${ip}"|grep -oP ‘^\d{1,3}(\.\d{1,3}){3}$‘ >/dev/null 2>&1 || Field=‘not ip‘         #if [ "${Field}" = ‘not ip‘ ];then         #        echo "${ip} not ip!" 1>&2         #        exit 1         #fi         #host_cfg="${hostname}-${ip}.cfg"         tmppage="webtemp.txt"        cp ${template} ${tmppage}         sed -i "s/WEBURL/${weburl}/g;s/INDEX/${index}/g" ${tmppage}        /bin/cat ${tmppage}>>${WEBTEMP} done/bin/cat webcheck_org.template>webcheck_${host_list}.cfg/bin/cat ${WEBTEMP}>>webcheck_${host_list}.cfgrm $PWD/${WEBTEMP}/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgservice nagios restart

  起作用的主要是這句,sed -i "s/WEBURL/${weburl}/g;s/INDEX/${index}/g" ${tmppage},說到底是sed命令的功勞。將weblist.txt裡面的內容替換掉模板裡的WEBURL和INDEX變數。

  4、調用方式

sh ./create.sh webcheck.template weblist.txt

  或者

sh ./create.sh weblist.txt

  如果存在大量需要手工修改設定檔的情況下,或者批量產生一些類似的檔案時可以考慮採用此種方式。

shell指令碼批量組建組態檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.