Shell脚本怎么实现批量生成nagios配置文件

发布时间:2021-08-03 21:11:56 作者:chen
来源:亿速云 阅读:135

这篇文章主要讲解了“Shell脚本怎么实现批量生成nagios配置文件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Shell脚本怎么实现批量生成nagios配置文件”吧!

如果管理的站点和服务器较多的情况下,每次修改配置文件都相当痛苦。因而想到了用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 $PATH

usage () { 
    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 index
do 
    #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}.cfg
rm $PWD/${WEBTEMP}

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

service 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脚本怎么实现批量生成nagios配置文件”的内容了,经过本文的学习后,相信大家对Shell脚本怎么实现批量生成nagios配置文件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. nagios配置文件详解
  2. 修正nagios主机配置文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shell

上一篇:php怎么生成固定长度纯数字编码

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》