在Debian系统上配置Nginx监控告警,可以通过以下步骤实现:
首先,确保你的系统上已经安装了Nginx和相关的监控工具。你可以使用以下命令来安装它们:
sudo apt update
sudo apt install nginx
sudo apt install nagios3 nagios-plugins-all nagios-nrpe-server nagios-plugins-nrpe
Nagios是一个强大的监控系统,可以用来监控Nginx的状态。
编辑Nagios的主配置文件 /etc/nagios/nagios.cfg,确保以下内容正确:
# The working directory of Nagios.
# This directory must exist and be absolutely writable by the Nagios user.
working_directory=/var/spool/nagios3
# The location of the object code that Nagios needs to execute.
# These plugins must be present in the /usr/lib/nagios/plugins directory or
# you must specify the exact location of the plugin directory using the
# 'plugin_directory' directive.
executable=/usr/lib/nagios/plugins
# The Nagios command file.
command_file=/etc/nagios3/commands.cfg
# The location of the timeperiod definitions.
timeperiod_file=/etc/nagios3/timeperiods.cfg
# The location of the host and service templates.
host_template_file=/etc/nagios3/templates.cfg
service_template_file=/etc/nagios3/templates.cfg
# The location of the host and service configuration files.
host_conf_file=/etc/nagios3/hosts.cfg
service_conf_file=/etc/nagios3/services.cfg
# The location of the contact group configuration files.
contactgroup_conf_file=/etc/nagios3/contactgroups.cfg
# The location of the contact configuration files.
contact_conf_file=/etc/nagios3/contacts.cfg
# The location of the event broker configuration files.
eventbroker.conf=/etc/nagios3/eventbroker.conf
# The location of the notifications configuration files.
notifications.conf=/etc/nagios3/notifications.conf
# The location of the state information file.
statefile=/var/spool/nagios3/state.dat
# The location of the history file.
historyfile=/var/spool/nagios3/history.dat
# The location of the object cache file.
objcache=/var/spool/nagios3/objcache.dat
# The location of the uptime information file.
uptime_file=/var/log/nagios3/uptime.log
# The location of the log file.
logfile=/var/log/nagios3/nagios.log
# The location of the lock file.
lockfile=/var/lock/subsys/nagios3
编辑 /etc/nagios3/objects/localhost.cfg 文件,添加Nginx的监控配置:
define host{
use generic-host
host_name localhost
alias localhost
}
define service{
use generic-service
host_name localhost
service_description HTTP
check_command check_http!-I localhost -p 80
}
NRPE允许Nagios服务器远程执行插件。
确保NRPE已经安装:
sudo apt install nagios-nrpe-server nagios-plugins-nrpe
编辑 /etc/nagios/nrpe.cfg 文件,添加以下内容:
allowed_hosts=127.0.0.1
command[check_nginx]=/usr/lib/nagios/plugins/check_nginx.sh
创建一个NRPE插件脚本来检查Nginx的状态:
sudo nano /usr/lib/nagios/plugins/check_nginx.sh
添加以下内容:
#!/bin/bash
# Check if Nginx is running
if pgrep nginx > /dev/null; then
echo "OK - Nginx is running"
exit 0
else
echo "CRITICAL - Nginx is not running"
exit 2
fi
赋予脚本执行权限:
sudo chmod +x /usr/lib/nagios/plugins/check_nginx.sh
确保防火墙允许Nagios服务器和NRPE之间的通信。
sudo ufw allow nagios
sudo ufw allow nagios-nrpe-server
启动并启用Nagios和NRPE服务:
sudo systemctl start nagios3
sudo systemctl enable nagios3
sudo systemctl start nagios-nrpe-server
sudo systemctl enable nagios-nrpe-server
编辑 /etc/nagios3/contacts.cfg 文件,添加告警通知配置:
define contact{
contact_name nagiosadmin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email your_email@example.com
}
编辑 /etc/nagios3/commands.cfg 文件,添加邮件通知命令:
define command{
command_name notify-service-by-email
command_line /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTEMAIL$
}
使用以下命令测试Nagios配置是否正确:
sudo nagios3 -v /etc/nagios3/nagios.cfg
如果没有错误,重启Nagios服务:
sudo systemctl restart nagios3
现在,Nagios将会监控Nginx的状态,并在检测到问题时发送告警通知。