配置信息
IP | 主机名 | 节点类型 |
---|---|---|
192.168.100.41 | z | 主节点 |
192.168.100.43 | nagios-client | 从节点 |
建议关闭防火墙和selinux
nagios的部署:https://blog.csdn.net/Bilise/article/details/105057182
nagios自定义远程监控项:https://blog.csdn.net/Bilise/article/details/105077813
一、在主节点上检查是否有nrpe进程
[root@z ~]# /usr/local/nagios/libexec//check_nagios -e 5 -F /usr/local/nagios/var/status.dat -C /usr/local/nagios/bin/nagios
NAGIOS OK: 6 processes, status log updated 8 seconds ago
二、修改两个节点的nrpe配置文件
[root@z ~]# vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,::1,192.168.100.43 //添加从节点的IP地址或网段
command[check_nagios]=/usr/local/nagios/libexec/check_nagios -e 5 -F /usr/local/nagios/var/status.dat -C /usr/local/nagios/bin/nagios
在从节点进行测试
[root@nagios-client ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.100.41 -c check_nagios
NAGIOS OK: 6 processes, status log updated 6 seconds ago
一、修改nrpe.cfg文件
[root@z ~]# vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,::1,192.168.100.41 //添加主节点的IP地址或网段
二、复制相关文件到/usr/local/nagios/libexec/eventhandlers/下
[root@nagios-client eventhandlers]# cp enable_notifications /usr/local/nagios/libexec/eventhandlers/
[root@nagios-client eventhandlers]# cp disable_notifications /usr/local/nagios/libexec/eventhandlers/
[root@nagios-client eventhandlers]# cp redundancy-scenario1/handle-master-host-event /usr/local/nagios/libexec/eventhandlers/
[root@nagios-client eventhandlers]# cp redundancy-scenario1/handle-master-proc-event /usr/local/nagios/libexec/eventhandlers/
[root@nagios-client eventhandlers]# sed -i 's/active_service_checks/notifications/g' /usr/local/nagios/libexec/eventhandlers/handle-master-proc-event
授权
[root@nagios-client eventhandlers]# chown nagios.nagios /usr/local/nagios/libexec/eventhandlers/*
[root@nagios-client eventhandlers]# chmod 755 /usr/local/nagios/libexec/eventhandlers/*
[root@nagios-client eventhandlers]# ll /usr/local/nagios/libexec/eventhandlers/
total 16
-rwxr-xr-x 1 nagios nagios 759 Mar 25 15:28 disable_notifications
-rwxr-xr-x 1 nagios nagios 760 Mar 25 15:28 enable_notifications
-rwxr-xr-x 1 nagios nagios 2038 Mar 25 15:28 handle-master-host-event
-rwxr-xr-x 1 nagios nagios 1216 Mar 25 15:29 handle-master-proc-event
三、配置添加命令command
[root@nagios-client eventhandlers]# vim /usr/local/nagios/etc/objects/commands.cfg
define command {
command_name handle-master-host-event
command_line $USER1$/eventhandlers/handle-master-host-event $HOSTSTATE$ $HOSTSTATETYPE$ $HOSTATTEMPT$
}
define command {
command_name handle-master-proc-event
command_line $USER1$/eventhandlers/handle-master-proc-event $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}
define command {
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
四、修改本地主机文件
[root@nagios-client eventhandlers]# vim /usr/local/nagios/etc/objects/localhost.cfg
define host {
use critical-host
host_name nagiosMaster
alias nagios master
address 192.168.100.41
event_handler handle-master-host-event
}
define service {
use critical-service
host_name nagiosMaster
service_description NAGIOS
check_command check_nrpe!check_nagios
event_handler handle-master-proc-event
}
五、修改模板文件
define host{
name critical-host
use generic-host
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 10
check_command check-host-alive
notification_period workhours
notification_interval 120
notification_options d,u,r
contact_groups admins
register 0
}
define service{
name critical-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
contact_groups admins
notification_options w,u,c,r
notification_interval 60
notification_period 24x7
register 0
}
六、修改 /usr/local/nagios/etc/nagios.cfg
[root@nagios-client eventhandlers]# vim /usr/local/nagios/etc/nagios.cfg
# Values: 1 = enable notifications, 0 = disable notifications
enable_notifications=0
#改成不发告警
use_retained_program_state=0
#状态保持改成0,否则Nagios在启动和重启时将忽略notifications的设置,并采用最近的一个设置(比如你已经切换过一次发告警的状态)
七、重启nagios生效配置
[root@nagios-client eventhandlers]# systemctl restart nagios
八、配置邮件服务
[root@client ~]# vim /etc/mail.rc
##添加以下内容,此前下载了mailx和sendmail
set from=[email protected]
set smtp=smtp.qq.com
set smtp-auth-user=[email protected] //此为QQ邮箱账号
set smtp-auth-password=XXXXXXX //此为QQ邮箱smtp授权码
set smtp-auth=login
[root@client ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
##修改此行,后为QQ邮箱账号
email [email protected]
九、测试
如需发送邮件通知告警则需要开启邮件告警
[root@nagios-client eventhandlers]# vim /usr/local/nagios/etc/nagios.cfg
s
enable_notifications=1 //1为开启,0为关闭
关闭主节点后,查看从节点的/var/log/message
[root@nagios-client eventhandlers]# tail -f /var/log/messages
##可以在后面看到以下内容
Mar 25 23:16:37 nagios-client nagios: SERVICE ALERT: nagiosMaster;NAGIOS;CRITICAL;HARD;1;NAGIOS CRITICAL: Cannot open status log for reading!
Mar 25 23:16:37 nagios-client nagios: SERVICE EVENT HANDLER: nagiosMaster;NAGIOS;CRITICAL;HARD;1;handle-master-proc-event
Mar 25 23:16:37 nagios-client nagios: EXTERNAL COMMAND: ENABLE_NOTIFICATIONS;1585149397