欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-11 11:58:52
...

配置规划

IP hostname 角色
192.168.100.41 z server
192.168.100.43 nagios-client client

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

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

[aaa@qq.com ~]# 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 
}

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

[aaa@qq.com etc]# vim /usr/local/nagios/libexec/port_httpd.sh 
#!/bin/bash
netstat -lntp | grep http | wc -l

[aaa@qq.com etc]# vim /usr/local/nagios/libexec/port_mysql.sh 
#!/bin/bash
netstat -lntp | grep mysql | wc -l 

[aaa@qq.com etc]# vim /usr/local/nagios/libexec/port_nginx.sh 
#!/bin/bash
netstat -lntp | grep nginx | wc -l

并赋予权限

[aaa@qq.com ~]# chmod 755 /usr/local/nagios/libexec/port_nginx.sh
[aaa@qq.com ~]# chmod 755 /usr/local/nagios/libexec/port_httpd.sh
[aaa@qq.com ~]# chmod 755 /usr/local/nagios/libexec/port_mysql.sh

三、编辑nrpe.cfg文件

[aaa@qq.com ~]#  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

[aaa@qq.com ~]# systemctl restart nrpe

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

[aaa@qq.com ~]# 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的错误提示)

[aaa@qq.com ~]# vim /usr/local/nagios/etc/objects/commands.cfg 

define command{

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

六、检查配置文件并重启

[aaa@qq.com ~]# /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

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

[aaa@qq.com ~]# systemctl restart nagios

七、web界面展示
nagios自定义简单监控脚本远程监控服务器
可以看到client上有三个刚刚自定义的监控项

相关标签: linux