# 搭建 nagios 监控 - 服务 - Linux | ☆∵∴Hito∴∵★ = 涛声依旧 = 天下事有难易乎？为之，则难者亦易矣
Keywords: nagios,服务监控

#### # 监控端服务安装与配置

1. nagios 需要安装主程序 core 和 nrpe（nagios 和各被监控主机都必须安装）。如需使用自研前端可通过安装 ndoutils (用于把 nagios 监控信息写入数据库) 和 mysql 实现。具体安装见官网
2. nagios 默认监控命令脚本放置在 libexec 中，自定义脚本也放到此处
3. etc/objects/commands.cfg 用于保存 nagios 默认监控命令
4. etc/nrpe.cfg 文件中需要添加用于被监控执行命令项（各被监控主机中都需要添加）
5. etc/objects/hosts 目录下配置需要被监控的主机信息
6. etc/objects/servers 目录下配置需要在被监控主机上执行的监控命令（第 4 项中的命令）

#### # 被监控端服务安装与配置

1. 被监控端需要安装 nrpe
2. 被监控端需要把监控端的命令写入到 nrpe 的配置文件中
3. 被监控端需要把命令执行脚本放入 libexec 目录中
4. 被监控端自动安装脚本（可借鉴）

    1、请先修改脚本中的服务端IP。

    2、如被监控端不支持let计算命令请执行sudo dpkg-reconfigure dash命令，弹出选择窗口后选择no。

    3、需要在脚本同目录下创建CentOS、Ubuntu、sh和conf目录，目录下分别放置nrpe的tar安装包、监控脚本和nrpe配置文件。

    #!/bin/bash

    #服务端 ip

    SERVER_IP=10.10.10.121

    #安装目录

    INSTALL_HOME=`pwd`

    #安装 ubuntu 版本

    INSTALL_UBUNTU()

    {

    	sudo apt-get update

    	sudo apt-get install -y autoconf automake gcc libc6 libmcrypt-dev make libssl-dev wget openssl

    	cd /tmp

    	#wget --no-check-certificate -O nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/archive/nrpe-4.0.3.tar.gz

    	cp $INSTALL_HOME/Ubuntu/nrpe.tar.gz ./

    	tar xzf nrpe.tar.gz

    	cd /tmp/nrpe-nrpe-4.0.3/

    	sudo ./configure --enable-command-args --with-ssl-lib=/usr/lib/x86_64-linux-gnu/

    	sudo make all

    	sudo make install-groups-users

    	sudo make install

    	sudo make install-config

    	sudo sh -c "echo >> /etc/services"

    	sudo sh -c "sudo echo '# Nagios services' >> /etc/services"

    	sudo sh -c "sudo echo 'nrpe    5666/tcp' >> /etc/services"

    	#判断系统是高版本还是低版本

    	VERSION=`lsb_release -r --short`

    	IS_LOW_VERSION=`echo "$VERSION < 15" | bc`

    	if [ $IS_LOW_VERSION = 1 ]; then

    		#低版本

    		sudo make install-init

    	else

    		#高版本

    		sudo make install-init

    		sudo systemctl enable nrpe.service

    	fi

    	sudo mkdir -p /etc/ufw/applications.d

    	sudo sh -c "echo '[NRPE]' > /etc/ufw/applications.d/nagios"

    	sudo sh -c "echo 'title=Nagios Remote Plugin Executor' >> /etc/ufw/applications.d/nagios"

    	sudo sh -c "echo 'description=Allows remote execution of Nagios plugins' >> /etc/ufw/applications.d/nagios"

    	sudo sh -c "echo 'ports=5666/tcp' >> /etc/ufw/applications.d/nagios"

    	sudo ufw allow NRPE

    	sudo ufw reload

    	sudo sh -c "sed -i '/^allowed_hosts=/s/$/,$SERVER_IP/' /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "sed -i 's/^dont_blame_nrpe=.*/dont_blame_nrpe=1/g' /usr/local/nagios/etc/nrpe.cfg"

    	cd $INSTALL_HOME

    	sudo cp ./sh/* /usr/local/nagios/libexec/

    	sudo chmod +xr /usr/local/nagios/libexec/*

    	sudo sh -c "echo 'command[check_ping]=/usr/local/nagios/libexec/check_ping -H 127.0.0.1 -w 3000.0,80% -c 5000.0,100% -p 5' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_mem]=/usr/local/nagios/libexec/check_mem.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_disk]=/usr/local/nagios/libexec/check_disk.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_my_service]=/usr/local/nagios/libexec/check_my_service.sh \$ARG1\$' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_system_info]=/usr/local/nagios/libexec/check_system_info.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	NETWORK_INDEX=1

    	for NETWORK_NAME in `cat /proc/net/dev | awk '{i++; if(i>2){print $1}}' | sed 's/^[\t]*//g' | sed 's/[:]*$//g'`;do

    			if [ $NETWORK_NAME != 'lo' ]; then

    					sudo sh -c "echo 'command[check_network$NETWORK_INDEX]=/usr/local/nagios/libexec/check_network.sh $NETWORK_NAME' >> /usr/local/nagios/etc/nrpe.cfg"

    					NETWORK_INDEX=`expr $NETWORK_INDEX + 1 `

    			fi

    	done

    	if [ $IS_LOW_VERSION = 1 ]; then

    		#低版本

    		sudo start nrpe

    	else

    		#高版本

    		sudo systemctl start nrpe.service

    	fi

    }

    #安装 centos 版本

    INSTALL_CENTOS()

    {

    	yum install -y gcc glibc glibc-common openssl openssl-devel perl wget

    	cd /tmp

    	#wget --no-check-certificate -O nrpe.tar.gz https://github.com/NagiosEnterprises/nrpe/archive/nrpe-4.0.3.tar.gz

    	cp $INSTALL_HOME/CentOS/nrpe.tar.gz ./

    	tar xzf nrpe.tar.gz

    	cd /tmp/nrpe-nrpe-4.0.3/

    	./configure --enable-command-args

    	make all

    	make install-groups-users

    	make install

    	make install-config

    	echo >> /etc/services

    	echo '# Nagios services' >> /etc/services

    	echo 'nrpe    5666/tcp' >> /etc/services

    	#判断系统是高版本还是低版本

    	VERSION=`rpm -q centos-release|cut -d- -f3`

    	#安装 bc 命令

    	yum -y install bc

    	IS_LOW_VERSION=`echo "$VERSION < 7" | bc`

    	if [ $IS_LOW_VERSION = 1 ]; then

    		#低版本

    		make install-init

    		iptables -I INPUT -p tcp --destination-port 5666 -j ACCEPT

    		service iptables save

    		ip6tables -I INPUT -p tcp --destination-port 5666 -j ACCEPT

    		service ip6tables save

    	else

    		#高版本

    		make install-init

    		systemctl enable nrpe.service

    		firewall-cmd --zone=public --add-port=5666/tcp

    		firewall-cmd --zone=public --add-port=5666/tcp --permanent

    	fi

    	sudo sh -c "sed -i '/^allowed_hosts=/s/$/,$SERVER_IP/' /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "sed -i 's/^dont_blame_nrpe=.*/dont_blame_nrpe=1/g' /usr/local/nagios/etc/nrpe.cfg"

    	cd $INSTALL_HOME

    	sudo cp ./sh/* /usr/local/nagios/libexec/

    	sudo chmod +xr /usr/local/nagios/libexec/*

    	sudo sh -c "echo 'command[check_ping]=/usr/local/nagios/libexec/check_ping -H 127.0.0.1 -w 3000.0,80% -c 5000.0,100% -p 5' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_mem]=/usr/local/nagios/libexec/check_mem.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_disk]=/usr/local/nagios/libexec/check_disk.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_my_service]=/usr/local/nagios/libexec/check_my_service.sh \$ARG1\$' >> /usr/local/nagios/etc/nrpe.cfg"

    	sudo sh -c "echo 'command[check_system_info]=/usr/local/nagios/libexec/check_system_info.sh' >> /usr/local/nagios/etc/nrpe.cfg"

    	NETWORK_INDEX=1

    	for NETWORK_NAME in `cat /proc/net/dev | awk '{i++; if(i>2){print $1}}' | sed 's/^[\t]*//g' | sed 's/[:]*$//g'`;do

    			if [ $NETWORK_NAME != 'lo' ]; then

    					sudo sh -c "echo 'command[check_network$NETWORK_INDEX]=/usr/local/nagios/libexec/check_network.sh $NETWORK_NAME' >> /usr/local/nagios/etc/nrpe.cfg"

    					NETWORK_INDEX=`expr $NETWORK_INDEX + 1 `

    			fi

    	done

    	if [ $IS_LOW_VERSION = 1 ]; then

    		#低版本

    		if [ $(echo "$VERSION < 6" | bc) -eq 1 ]; then

    			service nrpe start

    		else

    			start nrpe

    		fi

    	else

    		#高版本

    		systemctl start nrpe.service

    	fi

    }

    #安装其它版本

    INSTALL_OTHER()

    {

    	echo "Not supported at the moment."

    }

    #根据不同系统安装不同版本

    INSTALL()

    {

    	if grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then

    		DISTRO='CentOS'

            PM='yum'

    		INSTALL_CENTOS

        elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then

            DISTRO='RHEL'

            PM='yum'

    		INSTALL_OTHER

        elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then

            DISTRO='Aliyun'

            PM='yum'

    		INSTALL_OTHER

        elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then

            DISTRO='Fedora'

            PM='yum'

    		INSTALL_OTHER

        elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then

            DISTRO='Debian'

            PM='apt'

    		INSTALL_OTHER

        elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then

            DISTRO='Ubuntu'

            PM='apt'

    		INSTALL_UBUNTU

        elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then

            DISTRO='Raspbian'

            PM='apt'

    		INSTALL_OTHER

        else

            echo "unknow linux."

            exit 1

        fi

        echo $DISTRO

    }

    INSTALL

    exit 0

#### # 登录

地址：http:// 服务器 IP/nagios
用户名：nagiosadmin
密码：nagiosadmin

#### # 常见问题

1. 监控日志未写入 mysql（可能是 ndoutils 服务不正常导致）。采用以下脚本命令解决

    #!/bin/bash

    sudo rm -f /usr/local/nagios/var/ndo2db.pid

    sudo rm -f /usr/local/nagios/var/ndo.sock

    sudo systemctl restart ndo2db.service

    sudo systemctl status ndo2db.service

nagios 服务监控