#!/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

此脚本命名nrpe_install.sh,适用于Centos 7或者RedHat RHEL 7,仅仅需要把mon_ip改成你所在系统的IP,然后执行 sh nrpe_install.sh,就算多次执行此脚本,也不会有重复信息。如果有多个网卡,并且网口设备名不是ethX,请自行稍许修改脚本,也能很好的工作。