Nagios 智能化监控系统部署手册
1. 系统架构
2. 基础环境部署
2.1 硬件要求
角色 | CPU | 内存 | 磁盘 | 网络 |
Nagios Server | 8核+ | 16GB | 100GB | 1Gbps+ |
监控节点 | 2核 | 4GB | 50GB | 100Mbps+ |
2.2 软件依赖
# CentOS 8 基础环境
yum install -y epel-release
yum install -y httpd php gcc glibc glibc-common gd gd-devel \
net-snmp net-snmp-utils python3-devel
3. Nagios Core 安装
3.1 编译安装
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.12.tar.gz
tar xzf nagios-4.4.12.tar.gz
cd nagios-4.4.12
./configure --with-command-group=nagcmd \
--with-httpd-conf=/etc/httpd/conf.d
make all
make install-groups-users
make install
make install-daemoninit
make install-commandmode
make install-config
make install-webconf
3.2 Web界面配置
# 设置管理员密码
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
# 防火墙规则
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
# 启动服务
systemctl restart httpd
systemctl enable nagios
4. 各层实现
4.1 NRPE 客户端配置(被监控节点)
# 安装NRPE
yum install -y nrpe nagios-plugins-all
# 配置允许的Nagios服务器IP
echo "allowed_hosts=192.168.1.100" >> /etc/nagios/nrpe.cfg
# 示例:添加自定义内存检查
echo "command[check_mem]=/usr/lib64/nagios/plugins/check_memory -w 80 -c 90" >> /etc/nagios/nrpe.cfg
systemctl restart nrpe
Nagios 服务端配置
# 定义NRPE检查命令
echo '
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}' >> /usr/local/nagios/etc/objects/commands.cfg
# 定义主机服务
echo '
define service{
use generic-service
host_name web01
service_description Memory Usage
check_command check_nrpe!check_mem
}' >> /usr/local/nagios/etc/services.cfg
4.2 Elasticsearch 集成
# 安装ELK Stack
yum install -y elasticsearch kibana logstash
# Nagios事件转发配置(/etc/logstash/conf.d/nagios.conf)
input {
file {
path => "/usr/local/nagios/var/nagios.log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "nagios-events-%{+YYYY.MM.dd}"
}
}
Neo4j 关系存储
# 事件关系分析脚本(/opt/nagios/event_analyzer.py)
from py2neo import Graph
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password"))
def create_relation(host, service, symptom):
query = """
MERGE (h:Host {name: $host})
MERGE (s:Service {name: $service})
MERGE (sym:Symptom {desc: $symptom})
MERGE (h)-[r:AFFECTS]->(s)
MERGE (s)-[c:CAUSES]->(sym)
"""
graph.run(query, host=host, service=service, symptom=symptom)
4.3 Ansible 自愈脚本
# /etc/ansible/playbooks/restart_httpd.yml
- hosts: "{{ target_host }}"
tasks:
- name: Check httpd status
command: systemctl status httpd
register: httpd_status
ignore_errors: yes
- name: Restart httpd if failed
systemd:
name: httpd
state: restarted
when: "'active (running)' not in httpd_status.stdout"
Nagios 事件处理器
# /usr/local/nagios/etc/objects/eventhandlers.cfg
define command {
command_name handle_web_failure
command_line /usr/local/nagios/libexec/eventhandlers/web_failure.sh "$HOSTADDRESS#34; "$SERVICESTATE#34;
}
# 自动修复脚本示例(web_failure.sh)
#!/bin/bash
ansible-playbook /etc/ansible/playbooks/restart_httpd.yml -e "target_host=$1"
echo "`date` - Restarted httpd on $1" >> /var/log/nagios_autofix.log
5. 验证与调优
5.1 配置检查
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
5.2 性能优化参数
# /usr/local/nagios/etc/nagios.cfg
check_result_reaper_frequency=2
max_concurrent_checks=30
service_check_timeout=60
6. 维护与监控
6.1 日常维护
# 日志轮转配置(/etc/logrotate.d/nagios)
/usr/local/nagios/var/nagios.log {
daily
rotate 30
compress
missingok
postrotate
/bin/kill -HUP `cat /usr/local/nagios/var/nagios.lock 2>/dev/null` 2>/dev/null
endscript
}
6.2 监控看板
Grafana 配置:
- ini复制下载
[inputs.influxdb]
urls = ["http://localhost:8086"]
database = "nagios_metrics"
附录A:故障排查指南
现象 | 可能原因 | 解决方案 |
Web界面无法访问 | Apache未启动 | systemctl restart httpd |
NRPE连接超时 | 防火墙阻止5666端口 | firewall-cmd --add-port=5666/tcp |
预测模型不准确 | 训练数据不足 | 扩展历史数据采集周期 |