国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

Shell腳本實現批量生成nagios配置文件

2020-07-27 19:05:32
字體:
來源:轉載
供稿:網友

如果管理的站點和服務器較多的情況下,每次修改配置文件都相當痛苦。因而想到了用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

[root@bogon 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

  如果存在大量需要手工修改配置文件的情況下,或者批量生成一些類似的文件時可以考慮采用此種方式。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延津县| 金川县| 托克托县| 神农架林区| 定安县| 江达县| 钦州市| 钟祥市| 济宁市| 遵义市| 灵山县| 潜江市| 偏关县| 三门峡市| 东平县| 漠河县| 沽源县| 五家渠市| 涟源市| 泸水县| 壶关县| 视频| 将乐县| 石嘴山市| 玉屏| 菏泽市| 洛扎县| 梓潼县| 遵化市| 黔南| 德令哈市| 博野县| 东城区| 贡嘎县| 黄冈市| 双辽市| 滨州市| 桐柏县| 腾冲县| 三原县| 安达市|