1、前言
在某些特定的场景中,我们可能需要使用nagios来监控某些特定的进程的运行状态,一旦出现异常就触发报警邮件,以便运维人员及时排查解决问题。
参考文章:
编译安装nagios:https://www.jianshu.com/p/22cb1ad26117
npre监控Linux主机:https://www.jianshu.com/p/bc04a9980edc
2、配置被监控主机的监控脚本
通常来说在被监控主机上,我们都会安装相应的nrpe插件来实现nagios监控。
因此首先我们需要在nrpe的配置文件中添加自定义的监控命令配置
#在文件末尾添加下述配置
[root@web ~]# vim /usr/local/nagios/etc/nrpe.cfg
command[check_nginx]=/usr/local/nagios/libexec/check_nginx #设置check_nginx监控命令所对应的监控脚本
然后创建相应的监控脚本文件:
[root@web ~]# vim /usr/local/nagios/libexec/check_nginx
#!/bin/sh
NPD=`ps aux|grep 'nginx master' | wc -l`
if [ $NPD == 1 ];then
echo "Nginx process running ok."
exit 0
else
echo "Nginx process is down."
exit 2
fi
[root@web ~]# chown nagios.nagios /usr/local/nagios/libexec/check_nginx
[root@web ~]# chmod +x /usr/local/nagios/libexec/check_nginx
最后重启被监控主机的nrpe进程:
[root@web ~]# killall -9 nrpe
[root@web ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@web ~]# ps aux | grep nrpe
nagios 26761 0.1 0.1 39272 1460 ? Ss 11:42 0:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
root 26763 0.0 0.0 103320 880 pts/0 S+ 11:43 0:00 grep nrpe
3、编辑监控主机中相应的主机配置文件
接着我们需要到监控主机上编辑配置相应的被监控主机配置文件:
[root@nagios objects]# vim /usr/local/nagios/etc/objects/newlinux.cfg #编辑相应的被监控主机的配置文件
define host{ #定义远程主机
use linux-server
host_name newlinux
alias Linux Server
address 10.10.10.8
}
define hostgroup{ #定义主机组,可以将多个具有相同关系的主机添加到同一个主机组
hostgroup_name nginx-servers
alias The servers who have nginx service
members newlinux
}
define service{
use generic-service
hostgroup_name nginx-servers
service_description check_nginx
check_command check_nrpe!check_nginx #设置检查命令为此前设置的check_nginx,nagios监控主机会将此命令传递给被监控主机的npre进程,然后执行该命令对应的脚本后,将脚本的返回值反馈给监控主机。
check_interval 0.5
retry_check_interval 0.1
notification_interval 1
contact_groups OP-group #指定邮件报警组
}
[root@nagios objects]# chown nagios:nagios /usr/local/nagios/etc/objects/newlinux.cfg
[root@nagios objects]# chmod 755 /usr/local/nagios/etc/objects/newlinux.cfg
定义邮件报警的相关联系人和组:
[root@nagios ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name charles
use generic-contact
alias ops-IT
email charles@xxxxxxxx.com.cn
}
define contactgroup{
contactgroup_name OP-group
alias OP
members charles
}
最后重启nagios服务即可:
[root@nagios objects]# service nagios restart