自动部署Nagios Nrpe

今天花半天时间写了个shell脚本,非常方便用来部署Nagios Nrpe,能完成自动安装、自动生成插件脚本、自动修改nrpe配置文件nrpe.cfg、自动启动并自动随开机启动服务。

#!/bin/bash
#writed by sery(wx:formyz),in 2021-6-26

yum -y install gcc gcc-c++ make openssl openssl-devel wget net-tool
useradd nagios
chmod +x /etc/rc.d/rc.local

#install nrpe
cd 
if [[ ! -f nrpe-4.0.2.tar.gz ]]
  then
   wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.2/nrpe-4.0.2.tar.gz
   tar zxvf nrpe-4.0.2.tar.gz
   cd  nrpe-4.0.2
   ./configure  --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nrpe
    make all
    make install
    make install-plug
    make install-config
        cd
 fi

#install nagios-plugins
if [[ ! -f nagios-plugins-2.3.3.tar.gz ]]
  then
    wget --no-check-certificate https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
    tar zxvf nagios-plugins-2.3.3.tar.gz
    cd nagios-plugins-2.3.3
    ./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nrpe
    make
    make install
    cd
 fi

#write tcpconns script
echo > /usr/local/nrpe/libexec/check_tcpconns
cat >> /usr/local/nrpe/libexec/check_tcpconns <<done
  #!/bin/bash
  tcp_conns=\`netstat -an|grep tcp |grep EST|wc -l\`

  if [[ \$tcp_conns -lt \$1 ]]
    then
      echo "OK -connect count is \$tcp_conns"
      exit 0
  fi

  if [[ \$tcp_conns -gt $1 ]] && [[ \$tcp_conns -lt $2 ]]
    then
       echo "WARNING -connect count is \$tcp_conns"
       exit 1
  fi

  if [[ \$tcp_conns -gt $2 ]]
    then
    echo  "CRITICAL -connect count is \$tcp_conns"
    exit 2
  fi
done
  
 chmod +x /usr/local/nrpe/libexec/check_tcpconns
 
#modify nrpe.cfg
 cd /usr/local/nrpe/etc
 mon_ip=172.16.35.105
 if [[ -f nrpe.cfg ]]
    then
           ipadd=$(ip add|grep eth0|grep inet | awk '{print $2}'|awk -F / '{print $1}')
           sed -i "s/#server_address=127.0.0.1/server_address=${ipadd}/" nrpe.cfg

           is_mon=`grep $mon_ip nrpe.cfg|grep -v grep |wc -l`
            if [[ is_mon -eq 0 ]]
            then
             sed -i "s/allowed_hosts=127.0.0.1,/&$mon_ip,/" nrpe.cfg
            fi

           is_check_df=` grep check_df nrpe.cfg |grep -v grep|wc -l`
           if  [[ $is_check_df -eq 0 ]]
             then
             echo "command[check_df]=/usr/local/nrpe/libexec/check_disk -X tmpfs -X devtmpfs -w 20% -c 10%">>nrpe.cfg
           fi
           
           is_check_tcpconns=`grep check_tcpconns nrpe.cfg |grep -v grep|wc -l`
            if  [[ $is_check_tcpconns -eq 0 ]]
              then
               echo "command[check_tcpconns]=/usr/local/nrpe/libexec/check_tcpconns 800 1000">>nrpe.cfg
            fi 
           
           is_check_mem=`grep check_memuse nrpe.cfg |grep -v grep|wc -l`
            if [[ $is_check_mem -eq 0 ]]
             then
             echo "command[check_memuse]=/usr/local/nrpe/libexec/check_swap -w 40% -c 20%">>nrpe.cfg
            fi
  fi 
    
#start nrpe service
  cd 
  /usr/local/nrpe/bin/nrpe -c /usr/local/nrpe/etc/nrpe.cfg -d
  is_nrpe_start=`grep nrpe /etc/rc.local |grep -v grep|wc -l`
   if [[ $is_nrpe_start -eq 0 ]]
     then
      echo "/usr/local/nrpe/bin/nrpe -c /usr/local/nrpe/etc/nrpe.cfg -d" >>/etc/rc.local
   fi
 exit 0
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

4/5$全真龙门

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值