nagios自定义简单监控脚本远程监控服务器

配置规划

IPhostname角色
192.168.100.41zserver
192.168.100.43nagios-clientclient

需要关闭selinux和firewalld
nagios的部署和其插件的添加详情请参考:https://blog.csdn.net/Bilise/article/details/105057182

一、添加一个cfg文件,使client节点能够被server节点监控,并创建一个组,其中包含server节点和client节点
(完成后能够在web界面左边的 “Host Groups” 看到所创建的组和包含的主机)

[root@z ~]# vim /usr/local/nagios/etc/objects/192.168.100.43.cfg 
define host{
       use                     linux-server
       host_name               client
       alias                   client
       address                 192.168.100.43
       }
define hostgroup {
    hostgroup_name          test
    alias                   test
    members                 localhost,client 
}
[root@z ~]# vim /usr/local/nagios/etc/nagios.cfg
添加一行
cfg_file=/usr/local/nagios/etc/objects/192.168.100.43.cfg

二、在client节点上创建监控脚本

[root@nagios-client etc]# vim /usr/local/nagios/libexec/port_httpd.sh 
#!/bin/bash
httpd=`netstat -lntp | grep http | wc -l`
if [ $httpd -ge 1 ]; then
	exit 0
else
	exit 2
fi
[root@nagios-client etc]# vim /usr/local/nagios/libexec/port_mysql.sh 
#!/bin/bash
mysql=`netstat -lntp | grep http | wc -l`
if [ $mysql -ge 1 ]; then
	exit 0
else
	exit 2
fi
[root@nagios-client etc]# vim /usr/local/nagios/libexec/port_nginx.sh 
#!/bin/bash
nginx=`netstat -lntp | grep http | wc -l`
if [ $nginx -ge 1 ]; then
	exit 0
else
	exit 2
fi

并赋予权限

[root@nagios-client ~]# chmod 755 /usr/local/nagios/libexec/port_nginx.sh
[root@nagios-client ~]# chmod 755 /usr/local/nagios/libexec/port_httpd.sh
[root@nagios-client ~]# chmod 755 /usr/local/nagios/libexec/port_mysql.sh

三、编辑nrpe.cfg文件

[root@nagios-client ~]#  vim /usr/local/nagios/etc/nrpe.cfg
##加入以下内容,如果没有加这些,server节点会识别不到脚本,从而导致web界面出错
command[port_nginx]=/usr/local/nagios/libexec/port_nginx.sh
command[port_mysql]=/usr/local/nagios/libexec/port_mysql.sh
command[port_httpd]=/usr/local/nagios/libexec/port_httpd.sh

重启nrpe

[root@nagios-client ~]# systemctl restart nrpe

四、在server端刚刚所创建的cfg文件中加入监控项

[root@z ~]# vim /usr/local/nagios/etc/objects/192.168.100.43.cfg
##加入以下内容

define service{
        use   local-service
        host_name       client
        service_description     port_nginx
        check_command           check_nrpe!port_nginx
        max_check_attempts 5
        normal_check_interval 1
}
define service{
        use   local-service
        host_name       client
        service_description     port_mysql
        check_command           check_nrpe!port_mysql
        max_check_attempts 5
        normal_check_interval 1
}
define service{
        use   local-service
        host_name       client
        service_description     port_httpd
        check_command           check_nrpe!port_httpd
        max_check_attempts 5
        normal_check_interval 1
}

五、定义command (如不定义,检查配置文件的时候会报找不到command的错误提示)

[root@z ~]# vim /usr/local/nagios/etc/objects/commands.cfg 

define command{

        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

六、检查配置文件并重启

[root@z ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Nagios Core 4.4.3
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2019-01-15
License: GPL

Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
WARNING: The normal_check_interval attribute is deprecated and will be removed in future versions. Please use check_interval instead.
WARNING: The normal_check_interval attribute is deprecated and will be removed in future versions. Please use check_interval instead.
WARNING: The normal_check_interval attribute is deprecated and will be removed in future versions. Please use check_interval instead.
   Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
	Checked 11 services.
	Checked 2 hosts.
	Checked 2 host groups.
	Checked 0 service groups.
	Checked 1 contacts.
	Checked 1 contact groups.
	Checked 25 commands.
	Checked 5 time periods.
	Checked 0 host escalations.
	Checked 0 service escalations.
Checking for circular paths...
	Checked 2 hosts
	Checked 0 service dependencies
	Checked 0 host dependencies
	Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

如果没有显示错误提示,则重启,如果有请按照提示检查配置文件

[root@z ~]# systemctl restart nagios

七、web界面展示
在这里插入图片描述
可以看到client上有三个刚刚自定义的监控项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值