网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

交互式添加nagios主机和服务脚本-创新互联

    搭建好nagios 服务器之后,剩下的工作其实就是将欲监控的主机和服务添加到配置文件中,这过程其实有很多重复性的工作,所以我写了个交互式脚本,可以一步到位。

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、成都小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了东坡免费建站欢迎大家使用!

脚本思路分析:

首先分析下主机和服务的配置:

主机:

define host{
        use     linux-server,hosts-pnp
        host_name       client
        alias                 client
        address         192.168.56.105
}

服务:

define service{
        use     generic-service,services-pnp
        host_name       client
        service_description      check_load
        check_command           check_nrpe!check_load
        max_check_attempts 5
        normal_check_interval 1
}

注:上面红色部分为变化的部分,其余的都是固定的。也就是说需要将红色部分设置成变量。

接下来的问题就是配置服务的时候,如何一次性读入多个服务,实现一步到位。也就是说,我可以一次性添加多个服务,而无需重复性的运行脚本。下面我是通过数组来实现的。

脚本如下:

添加主机:

#!/bin/bash
echo "Please input the infomations of the server you want to set."
read -p "hostname:" HNAME
read -p "alias:" ALIAS
read -p "IP:" IP
cat << EOF > /usr/local/nagios/etc/hosts/${HNAME}.cfg
define host{
use     linux-server,hosts-pnp
host_name       ${HNAME}
alias                  ${ALIAS}
address            ${IP}
}
EOF
service nagios reload

添加服务:

#!/bin/bash
read -p "Please input your hostname :" HNAME
read -p "please input the number of the services you want to set :" n
echo "Please input the services' name :"
read -a SHELL
for ((i=0;i<=${n}-1;i++))
do
echo "${SHELL[$i]}" >> /tmp/name.txt
done
while read line
do
cat << EOF >> /usrl/local/nagios/etc/services/${HNAME}.cfg
define service{
use     generic-service,services-pnp
host_name       ${HNAME}
service_description     check_${line}
check_command           check_nrpe!check_${line}
max_check_attempts 5
normal_check_interval 1
}
EOF
echo "$line"
done < /tmp/name.txt
rm -rf /tmp/name.txt
service nagios reload

运行结果如下:(注意:下面蓝色部分为交互式输入部分)

添加主机:

[root@localhost nagios]# ./autochost.sh
Please input the infomations of the server you want to set.
hostname:slave3
alias:slave3
IP:192.168.56.110
Running configuration check...done.
Reloading nagios configuration...done

[root@localhost nagios]# cat etc/hosts/slave3.cfg
define host{
        use     linux-server,hosts-pnp
        host_name       slave3
        alias           slave3
        address         192.168.56.110
}

交互式添加nagios主机和服务脚本

添加服务:

[root@localhost nagios]# ./autocservice.sh
Please input your hostname :slave3
please input the number of the services you want to set :4
Please input the services' name :
http ssh mysql ftp

[root@localhost nagios]# cat etc/services/slave3.cfg
define service{
 use     generic-service,services-pnp
        host_name       slave3
        service_description     check_http
        check_command           check_nrpe!check_http
        max_check_attempts 5
        normal_check_interval 1
 }
define service{
 use     generic-service,services-pnp
        host_name       slave3
        service_description     check_ssh
        check_command           check_nrpe!check_ssh
        max_check_attempts 5
        normal_check_interval 1
 }
define service{
 use     generic-service,services-pnp
        host_name       slave3
        service_description     check_mysql
        check_command           check_nrpe!check_mysql
        max_check_attempts 5
        normal_check_interval 1
 }
define service{
 use     generic-service,services-pnp
        host_name       slave3
        service_description     check_ftp
        check_command           check_nrpe!check_ftp
        max_check_attempts 5
        normal_check_interval 1
 }

交互式添加nagios主机和服务脚本

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:交互式添加nagios主机和服务脚本-创新互联
转载来于:http://bjjierui.cn/article/depjis.html

其他资讯