Nagios自定义监控服务

IPHostname备注
192.168.88.31servernagios服务端
192.168.88.32clientnagios客户端

环境说明:firewalld关闭,selinux关闭。

软件版本:
nagios-plugins版本:2.2.1
nrpe版本:3.2.1

上一篇:Nagios服务端和客户端安装与配置

自定义监控脚本
要求:
创建cfg文件部署客户端下的监控,参考localhost.cfg,创建一个跟本地监控项一模一样的远程监控文件;
添加监控脚本,设计一个能监控nginx端口的脚本;并添加到远程监控项目中;
添加监控脚本,设计一个能监控mysql端口的脚本;并添加到远程监控项目中;
添加监控脚本,设计一个能监控apache端口的脚本;并添加到远程监控项目中;

客户端配置
1.编写监控脚本

[root@client ~]# cd /usr/local/nagios/libexec/
[root@client libexec]# vi check_port_status   
#!/bin/bash
OK=0
CRITICAL=2

nginx=`ss -lnt | grep 81 | wc -l`
mysql=`ss -lnt | grep 3306 | wc -l`
apache=`ss -lnt | grep 80 | wc -l`

case $1 in
nginx)
if [ $nginx -ge 1 ];then
        echo "OK,nginx is working!"
        exit $OK;
else
        echo "CRITICAL,nginx is not working!"
        exit $CRITICAL
fi
;;

mysql)  
if [ $mysql -ge 1 ];then
        echo "OK,Mysql is working!"
        exit $OK;
else
        echo "CRITICAL,Mysql is not working!"
        exit $CRITICAL
fi
;;

apache)
if [ $apache -ge 1 ];then
        echo "OK,Apache is working!"
        exit $OK;
else
        echo "CRITICAL,Apache is not working!"
        exit $CRITICAL
fi
;;

*)
        echo "Usage: $0 nginx|mysql|apache"
        exit;;
esac
exit 0

2.授权并测试脚本

[root@client libexec]# chmod 755 check_port_status 
[root@client libexec]# chown nagios.nagios check_port_status
[root@client libexec]# ./check_port_status nginx
OK,nginx is working!
[root@client libexec]# ./check_port_status mysql
OK,Mysql is not working!
[root@client libexec]# ./check_port_status apache
OK,Apache is working!

3.编辑nrpe.cfg配置文件

[root@client libexec]# vi /usr/local/nagios/etc/nrpe.cfg +303   //添加以下3行
command[check_nginx]=/usr/local/nagios/libexec/check_port_status nginx -c 2 
command[check_mysql]=/usr/local/nagios/libexec/check_port_status mysql -c 2
command[check_apache]=/usr/local/nagios/libexec/check_port_status apache -c 2

4.重启nrpe

[root@client libexec]# pkill nrpe
[root@client libexec]# /usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg

服务端配置
1.测试是否能调用客户端的脚本

[root@server ~]#  /usr/local/nagios/libexec/check_nrpe -H 192.168.88.32 -c check_nginx
OK,nginx is working!

2.定义命令,在/usr/local/nagios/etc/objects/commands.cfg中增加对check_nrpe的定义

[root@server ~]# vi /usr/local/nagios/etc/objects/commands.cfg 
define command{
        command_name    check_nrpe                                  //定义命令名称为check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$   //这是定义实际运行的插件程序
        }

//-c后的 A R G 1 ARG1 ARG1参数是传给nrpe daemon执行的检测命令,它必须是nrpe.cfg中所定义的那几条命令中的一条。在定义监控服务中使用check_nrpe的时候要用!带上这个参数。
3. 在/usr/local/nagios/etc/nagios.cfg配置文件中加上如下内容

[root@server ~]# vi /usr/local/nagios/etc/nagios.cfg +34
cfg_file=/usr/local/nagios/etc/objects/192.168.88.32.cfg		//让nagios包含客户端配置文件

4.配置客户端配置文件,定义主机与check_nrpe要检测的远端command名字

[root@server ~]# vi /usr/local/nagios/etc/objects/192.168.88.32.cfg 
define host{
        use                     linux-server
        host_name               client						//显示nagios网页上的主机名
        address                 192.168.88.32				//ip
        }
define service {
    use                     local-service
    host_name               client
    service_description     Nginx port status			//nagios网页上的服务名
    check_command           check_nrpe!check_nginx
    normal_check_interval           1 					//检测的间隔
}
define service {
    use                     local-service
    host_name               client
    service_description     Mysql port status
    check_command           check_nrpe!check_mysql
    normal_check_interval           1
}
define service {
    use                     local-service
    host_name               client
    service_description     Apache port status
    check_command           check_nrpe!check_apache
    normal_check_interval           1					
}

5.重启nagios服务

[root@server ~]# systemctl restart nagios    

访问nagios网页
在这里插入图片描述
可以看到客户端client中的nginx,mysql,apache服务都已加入到监控中。

6.测试关闭client中的mysql和nginx服务
在这里插入图片描述
mysql和nginx服务监控出现了严重警告。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值