温馨提示×

温馨提示×

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

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

Nagios 监控系列学习 —— NRPE

发布时间:2020-06-24 16:54:44 来源:网络 阅读:674 作者:LingYi2012 栏目:移动开发


nagios服务端默认没有check_nrpe插件,需要编译安装nrpe。


nrpe提供check_nrp 以及 nrpe二进制程序和启动进程所需的配置文件。


对于nagios服务端来说,其只需要check_nrpe插件来发送监控指令,而被监控端则需要通过nrpe进程来监控服务端发送过来的指令(默认监听5666),并执行本地插件获取信息,最后回传给监控端。由此可见,被监控端需要安装nagios-plugins来获取插件,但如果被监控端不使用由plugins组件提供的插件,当然也就不需要安装了。


nrpe的编译安装总结:

默认使用用户nagios和组nagios,最好提前创建

[root@localhost nrpe-2.15]# ./configure --prefix=/mnt    #测试目录
[root@localhost nrpe-2.15]# 
[root@localhost nrpe-2.15]# make all 
[root@localhost nrpe-2.15]# make install 
[root@localhost nrpe-2.15]#
[root@localhost nrpe-2.15]# tree /mnt 
/mnt
├── bin
│   └── nrpe
└── libexec
    └── check_nrpe

已经生成了二进制文件和插件,对于nagios服务端来说,此步之后,直接拷贝check_nrpe到nagios插件存放目录即可(注意可执行权限)。


而对于被监控端来说,check_nrpe插件是没有用的,而是需要nrpe来提供监听服务,但此时安装目录并没有配置文件:

[root@localhost nrpe-2.15]# make install-daemon-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /mnt/etc
/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /mnt/etc

可以手动将源码目录下的配置文件 sample-config/nrpe.cfg 拷贝到安装目录,并修改下属主和属组即可。 


简单看下配置文件,需要修改allowed_hosts,将nagios服务端ip添加到后边,逗号隔开即可:

server_port=5666
#server_address=127.0.0.1
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,$NagiosIP

有了配置文件就可以启动nrpe进程了,有两种方式,一种是独立守护进程,一种超级守护进程的方式。

独立守护进程:

[root@localhost nrpe-2.15]# 
[root@localhost nrpe-2.15]# netstat -antp | grep 5666
[root@localhost nrpe-2.15]# /mnt/bin/nrpe  -c /mnt/etc/nrpe.cfg -n -d
[root@localhost nrpe-2.15]# netstat -antp | grep 5666
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      13643/nrpe          
tcp        0      0 :::5666                     :::*                        LISTEN      13643/nrpe          
[root@localhost nrpe-2.15]#

-c 指定配置文件   

-n 不使用ssl,默认是使用的,编译时有参数--enable-ssl可以加上,应该是默认选项

  如果使用了 -n, nagios监控端在使用check_nrpe时也需要指定此参数。

-d 已独立守护进程方式启动


超级守护进程,需要inetd or xinetd,在编辑相关配置文件时,指定“-i”参数即可。


为了启动方便,将服务添加到chkconfig

参考脚本: 

#!/bin/bash

# chkconfig: 35 78 22

LOCK_FILE='/var/lock/subsys/nrped'
NRPE_COMMAND='/usr/local/nrpe/bin/nrpe'
NRPE_CONFIGURE_FILE='/usr/local/nrpe/etc/nrpe.cfg'

start()
{
	echo -n "starting nrped ... "
	$NRPE_COMMAND -c $NRPE_CONFIGURE_FILE -d &>/dev/null && touch $LOCK_FILE &>/dev/null
	[[ $? -eq 0 ]] && echo -e "\033[1;32mOK\033[0m" || echo -e "\033[1;31mFAIL\033[0m" 
}

stop()
{
	echo -n "stopping nrped ... "
	killall nrpe &>/dev/null && rm -f $LOCK_FILE &>/dev/ull
	[[ $? -eq 0 ]] && echo -e "\033[1;32mOK\033[0m" || echo -e "\033[1;31mFAIL\033[0m"
}

status()
{
	[[ -f $LOCK_FILE ]] && echo "nrpe is running ..." || echo "nrpe isn't running ... "
}

case $1 in 
start) start ;; 
stop) stop ;;
restart) stop; start ;;
status) status ;;
esac


后期处理和测试:

[root@localhost nrpe-2.15]# chmod +x /etc/init.d/nrped
[root@localhost nrpe-2.15]# chkconfig --add nrped 
[root@localhost nrpe-2.15]# chkconfig --list| grep nrped
nrped          	0:off	1:off	2:off	3:on	4:off	5:on	6:off
[root@localhost nrpe-2.15]# service nrped start
starting nrped ... OK
[root@localhost nrpe-2.15]# service nrped stop 
stopping nrped ... OK
[root@localhost nrpe-2.15]# service nrped restart 
stopping nrped ... FAIL
starting nrped ... OK
[root@localhost nrpe-2.15]# service nrped restart 
stopping nrped ... OK
starting nrped ... OK
[root@localhost nrpe-2.15]# service nrped status
nrpe is running ...
[root@localhost  nrpe-2.15]# netstat -antp | grep nrpe
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      13940/nrpe          
tcp        0      0 :::5666                     :::*                        LISTEN      13940/nrpe          
[root@localhost nrpe-2.15]#


关于被监控端插件:

nagios监控端可以“远程”调用的指令需要在被监控端nrpe配置文件中提前定义,如下:

command[check_users]=/mnt/libexec/check_users -w 5 -c 10
command[check_load]=/mnt/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/mnt/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/mnt/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/mnt/libexec/check_procs -w 150 -c 200 

#command[check_users]=/mnt/libexec/check_users -w $ARG1$ -c $ARG2$
#command[check_load]=/mnt/libexec/check_load -w $ARG1$ -c $ARG2$
#command[check_disk]=/mnt/libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
#command[check_procs]=/mnt/libexec/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$

可以定义阈值明确的指令也可以留有“变量”供监控端指定。

以上指令都是安装nagios-plugins后生成的指令,现在用shell手动编写一个简单的监控插件。

开发插件需要符合以下规则:

插件需要一个返回值和一段输出,而且nagios只接受第一段输出。

返回值和nagios五种状态的对应关系如下:

0: OK

1: WARNNING

2: CRITICAL

3: UNKNOWN

4: PENDING


脚本功能是简单的监控本地内存, 代码如下:

#!/bin/bash

#Author: LinigYi
# "Memory check"

OK=0
WARNNING=1
CRITICAL=2
UNKNOWN=3
PENDING=4

mem_string=$(free -m | grep Mem)
totle=$(echo $mem_string | awk '{print $2}')
used=$(echo $mem_string | awk '{print $3}')
free=$(echo $mem_string | awk '{print $4}')

used_percent_string=$(echo "scale=3; ${used}/${totle}" | bc)
used_percent_aa=${used_percent_string:1:2}
used_percent_bb=${used_percent_string:3:1}
if [[ $used_percent_bb -ge 5 ]]; then
	used_percent=$((used_percent_aa+=1))
else
	used_percent=${used_percent_aa}
fi

[[ $1 == '-w' ]] && shift && warnning=$1 && shift
[[ $1 == '-c' ]] && shift && critical=$1 

[[ $warnning -gt $critical ]] && {
	echo value wrong !!
	exit $PENDING
}

if [[ $used_percent -ge $critical ]]; then
	echo CRITICAL - Totle: $totle Used: $used Free: $free, used percent: ${used_percent}%
	exit $CRITICAL
elif [[ $used_percent -ge $warnning ]]; then
	echo WARNNING - Totle: $totle Used: $used Free: $free, used percent: ${used_percent}%
	exit $WARNNING
else
	echo OK - Totle: $totle Used: $used Free: $free, used percent: ${used_percent}%
	exit $OK
fi


基本模拟plugins的插件输出格式

测试插件:

首先修改配置文件,将nagios服务端ip添加到允许列表:

[root@localhost mnt]# cat /mnt/etc/nrpe.cfg | grep allowed_hosts
allowed_hosts=127.0.0.1, 172.19.2.250
[root@localhost mnt]#
[root@localhost mnt]# ls -l /mnt/libexec/ 
total 240
-rwxr-xr-x. 1 root   root   160049 Apr 22 18:10 check_load
-rwxr-xr-x. 1 root   root     1098 Apr 22 18:14 check_mem
-rwxrwxr-x. 1 nagios nagios  76825 Apr 22 16:54 check_nrpe
[root@localhost mnt]#

插件一定要有可执行权限! 拷贝一个对比的插件check_load

[root@localhost mnt]# cat /mnt/etc/nrpe.cfg | grep '^command'
command_timeout=60
command[check_users]=/mnt/libexec/check_users -w 5 -c 10
command[check_load]=/mnt/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/mnt/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/mnt/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/mnt/libexec/check_procs -w 150 -c 200 
command[check_mem]=/mnt/libexec/check_mem -w 20 -c 40
[root@localhost mnt]#

最后一个 check_mem 指令为自己定义。

启动nrpe

[root@localhost mnt]# 
[root@localhost mnt]# killall nrpe
[root@localhost mnt]# netstat -antp | grep nrpe
[root@localhost mnt]# /mnt/bin/nrpe -c /mnt/etc/nrpe.cfg  -d 
[root@localhost mnt]# netstat -antp | grep nrpe
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      14831/nrpe          
tcp        0      0 :::5666                     :::*                        LISTEN      14831/nrpe          
[root@localhost mnt]#


在nagios服务端做测试:

[root@localhost libexec]# ip add show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1a:4a:94:7d:2e brd ff:ff:ff:ff:ff:ff
    inet 172.19.2.250/24 brd 172.19.2.255 scope global eth0
    inet6 fe80::21a:4aff:fe94:7d2e/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost libexec]# 
[root@localhost libexec]# 
[root@localhost libexec]# pwd
/usr/local/nagios/libexec
[root@localhost libexec]# ./check_nrpe  -H 192.168.2.162 -c check_load
OK - load average: 0.00, 0.00, 0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0; load15=0.000;5.000;20.000;0; 
[root@localhost libexec]# 
[root@localhost libexec]# echo $?
0
[root@localhost libexec]# 
[root@localhost libexec]# ./check_nrpe  -H 192.168.2.162 -c check_mem
WARNNING - Totle: 3828 Used: 871 Free: 2956, used percent: 23%
[root@localhost libexec]# echo $?
1
[root@localhost libexec]#

检测成功 !


现在可以在nagios监控端添加监控服务了 !

附件:http://down.51cto.com/data/2367632
向AI问一下细节

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

AI