nagios 服务端配置

环境要求
nagios 也需要 apache+php 的环境,CentOS7.5 默认的 yum 源里没有 nagios 相关的 rpm 包,所以要安装一个 epel 的扩展源。

[root@localhost ~]# yum install -y epel-release

安装 nagios 相关包

[root@localhost ~]# yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe

设置登录 nagios 后台的用户和密码

[root@localhost ~]# htpasswd -c /etc/nagios/passwd nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin

检查配置文件是否有问题

[root@localhost ~]# nagios -v /etc/nagios/nagios.cfg
Total Warnings: 0
Total Errors:   0
Things look okay - No serious problems were detected during the pre-flight check

浏览器访问:http://ip/nagios

输入用户名nagiosadmin,密码是手动设置的密码

此时,nagios 监控的只有 localhost,还没有其他客户端机器,要想添加监控客户机,还需要在客户端安装 nagios 相关的软件包,并且需要在服务端配置。

nagios 监控客户端

配置客户端

安装 epel 扩展源

[root@localhost ~]# yum install -y epel-release

安装 nagios 以及 nagios-plugins

[root@localhost ~]# yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe

编辑配置文件

[root@localhost ~]# vim /etc/nagios/nrpe.cfg
找到“allowed_hosts=127.0.0.1”改为“allowed_hosts=127.0.0.1,172.30.15.10”后面的 ip 为服务端 ip

找到“dont_blame_nrpe=0”改为“dont_blame_nrpe=1”

allowed_hosts=127.0.0.1,172.30.15.10
dont_blame_nrpe=1

启动客户端

/usr/sbin/nrpe -c /etc/nagios/nrpe.cfg -d

服务端配置:

客户端 ip 为 172.30.15.4,下面定义子配置文件。

[root@localhost ~]# cd /etc/nagios/objects/
[root@localhost objects]# vim 172.30.15.4.cfg
define host{ 
use linux-server
host_name 172.30.15.4
alias 172.30.15.4
address 172.30.15.4
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description load
check_command check_nrpe!check_load
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description PING
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description FTP
check_command check_ftp!21
max_check_attempts 5
normal_check_interval 1
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description SSH
check_command check_ssh
max_check_attempts 5
normal_check_interval 1
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description HTTP
check_command check_http
max_check_attempts 5
normal_check_interval 1
}

说明:“max_check_attempts 5”表示,当 nagios 检测到问题时,一共尝试检测 5 次都有问题才会告警,如果该数值为 1,那么检测到问题就立即告警。“normal_check_interval 1”表示,重新检测的时间间隔,单位是分钟,默认是 3 分钟。“notification_interval 60”表示,在服务出现异常后,故障一直没有解决,nagios 再次对使用者发出通知的时间,单位是分钟。如果认为所有的事件只需要一次通知就够了,可以把这里的选项设为 0。

以上服务不依赖客户端 nrpe 服务,比如我们在自己电脑上可以使用 ping 或者 telnet 探测远程任何一台机器是否存活、是否开启某个端口或服务。而当检测客户端上的某个具体服务的情况时,就需要借助于 nrpe 了,比如想知道客户端机器的负载或磁盘使用情况。

编辑完配置文件后,在服务端重启一下 nagios 服务。

[root@localhost objects]# systemctl restart httpd && systemctl restart nagios

然后在浏览器中访问 nagios→“Current Status”→“Services”,刷新会多出来一个主机,并且多出来三个服务。

只不过这三个服务并不是我们想要的,我想要监控负载和磁盘使用率等服务,这时候就要使用 nrpe 服务了。继续在服务端添加服务。

服务端配置

编辑配置文件

[root@localhost ~]# vim /etc/nagios/objects/commands.cfg
文档最后添加
# 'check_nrpe' command definition
define command{ 
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

编辑配置文件

[root@localhost ~]# vim /etc/nagios/objects/172.30.15.4.cfg
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description load
check_command check_nrpe!check_load
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description PING
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description SSH
check_command check_ssh
max_check_attempts 5
normal_check_interval 1
}
define service{ 
use generic-service
host_name 172.30.15.4
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}

客户端操作

(!!!以下客户端操作)

在远程主机上编辑 nrpe.cfg 配置文件

[root@localhost ~]# lsblk 
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda    253:0    0  40G  0 disk 
└─vda1 253:1    0  40G  0 part /
[root@localhost ~]# vim /etc/nagios/nrpe.cfg
command[check_hda1]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /dev/vda1

客户端重启 nrpe 服务

[root@localhost ~]# systemctl restart nrpe

服务端重启 nagios 服务

[root@localhost ~]# systemctl restart nagios

浏览器刷新,又有三个服务出来,稍等一会儿就能看到状态了。

利用NSClient++监控Window 7主机

修改配置文件

[root@localhost ~]# vim /etc/nagios/objects/windows.cfg
define host { 

    use                     windows-server          ; Inherit default values from a template
    host_name               winserver               ; The name we're giving to this host
    alias                   My Windows Server       ; A longer name associated with the host
    address                 172.30.15.3             ; IP address of the host
}

编写配置文件

echo "cfg_file=/etc/nagios/objects/windows.cfg" >> /etc/nagios/nagios.cfg

下载NSClient++软件安装至Windows7主机
链接:https://pan.baidu.com/s/1FNTLX_cvl_wsUmbSYOd32Q
提取码:1234

安装完成后修改配置文件

启动这个软件并关闭windows防火墙

测试连接(服务端执行)

[root@localhost ~]# /usr/lib64/nagios/plugins/check_nt -H 172.30.15.3 -p 12489 -v UPTIME
System Uptime - 0 day(s) 10 hour(s) 39 minute(s) |uptime=639

重启服务端服务

[root@localhost ~]# systemctl restart nagios

查看监控页面

本文地址:https://blog.csdn.net/erye_/article/details/112601125