Nagios监控系统快速部署与基础配置指南

一、Nagios技术选型与部署准备

1.1 监控系统选型依据

在分布式架构日益复杂的今天,企业需要一套能够统一监控服务器、网络设备、应用服务的解决方案。Nagios作为开源领域的标杆产品,具备三大核心优势:

  • 跨平台支持:可运行在主流Linux发行版(CentOS/Ubuntu/Debian)
  • 插件生态:通过NRPE、NSClient++等扩展实现多样化监控
  • 告警策略:支持阈值触发、依赖关系、告警升级等高级规则

1.2 环境准备清单

部署前需完成以下基础配置:

  1. # 系统要求检查示例
  2. cat /etc/redhat-release # 确认CentOS 7.x/8.x
  3. uname -m # 验证x86_64架构
  4. free -h # 至少2GB可用内存
  5. df -h /opt # 确保/opt分区有5GB空间

建议关闭SELinux并配置防火墙规则:

  1. setenforce 0
  2. systemctl stop firewalld
  3. # 或开放特定端口(根据实际配置调整)
  4. firewall-cmd --permanent --add-port={80/tcp,5666/tcp}

二、核心组件安装流程

2.1 依赖环境配置

安装编译工具链和开发库:

  1. yum install -y gcc glibc glibc-common make httpd php \
  2. gd gd-devel perl postfix

创建专用运行用户:

  1. useradd -m nagios
  2. groupadd nagcmd
  3. usermod -a -G nagcmd nagios
  4. usermod -a -G nagcmd apache

2.2 Nagios核心安装

从官方托管仓库获取最新稳定版:

  1. wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.6.tar.gz
  2. tar zxvf nagios-*.tar.gz
  3. cd nagios-*

执行编译安装三步曲:

  1. ./configure --with-nagios-user=nagios \
  2. --with-nagios-group=nagcmd \
  3. --with-command-group=nagcmd \
  4. --prefix=/usr/local/nagios
  5. make all
  6. make install
  7. make install-init
  8. make install-config
  9. make install-commandmode

2.3 Web界面配置

安装Nagios插件集:

  1. wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
  2. tar zxvf nagios-plugins-*.tar.gz
  3. cd nagios-plugins-*
  4. ./configure --with-nagios-user=nagios \
  5. --with-nagios-group=nagcmd
  6. make && make install

配置Apache虚拟主机:

  1. # /etc/httpd/conf.d/nagios.conf
  2. Alias /nagios "/usr/local/nagios/share"
  3. <Directory "/usr/local/nagios/share">
  4. Options None
  5. AllowOverride All
  6. Require all granted
  7. AuthName "Nagios Access"
  8. AuthType Basic
  9. AuthUserFile /usr/local/nagios/etc/htpasswd.users
  10. Require valid-user
  11. </Directory>

创建认证用户:

  1. htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
  2. systemctl restart httpd

三、基础监控配置实践

3.1 主机与服务定义

编辑/usr/local/nagios/etc/objects/hosts.cfg

  1. define host{
  2. use linux-server
  3. host_name web01
  4. alias Web Server
  5. address 192.168.1.100
  6. max_check_attempts 5
  7. check_period 24x7
  8. notification_interval 30
  9. notification_period 24x7
  10. }

配置服务检查项services.cfg

  1. define service{
  2. use generic-service
  3. host_name web01
  4. service_description HTTP
  5. check_command check_http
  6. max_check_attempts 3
  7. check_interval 5
  8. retry_interval 1
  9. check_period 24x7
  10. notification_interval 60
  11. notification_period 24x7
  12. }

3.2 插件扩展配置

通过NRPE实现本地监控:

  1. # 在被监控节点安装NRPE
  2. yum install -y epel-release
  3. yum install -y nrpe nagios-plugins-all

配置/etc/nagios/nrpe.cfg

  1. allowed_hosts=192.168.1.10 # Nagios服务器IP
  2. command[check_load]=/usr/lib64/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
  3. command[check_disk]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /

在Nagios服务器定义检查命令:

  1. define command{
  2. command_name check_nrpe
  3. command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  4. }

3.3 告警策略优化

配置通知依赖关系:

  1. define hostdependency{
  2. dependent_host_name web01
  3. dependent_hostgroup_name webservers
  4. host_name router01
  5. notification_failure_criteria d,u
  6. }

设置告警升级规则:

  1. define serviceescalation{
  2. host_name web01
  3. service_description HTTP
  4. first_notification 1
  5. last_notification 0
  6. notification_interval 10
  7. escalation_period 24x7
  8. escalation_options c,r
  9. contact_groups admins,managers
  10. }

四、运维常见问题处理

4.1 服务启动失败排查

  1. 检查日志文件:

    1. tail -f /usr/local/nagios/var/nagios.log
    2. journalctl -u nagios -n 50 --no-pager
  2. 验证配置文件语法:

    1. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

4.2 插件执行异常处理

常见错误及解决方案:

  • CHECK_NRPE: Error - Could not complete SSL handshake
    解决方案:统一NRPE版本或关闭SSL验证

    1. # 在nrpe.cfg中添加
    2. ssl_version=TLSv1.2
    3. # 或
    4. dont_use_ssl=1
  • Return code of 127 for check
    解决方案:检查插件路径权限

    1. chown nagios:nagcmd /usr/lib64/nagios/plugins/*
    2. chmod 755 /usr/lib64/nagios/plugins/*

4.3 性能优化建议

  1. 调整检查间隔:对非关键服务设置normal_check_interval=10
  2. 启用结果缓存:在nagios.cfg中设置:
    1. cache_file=/usr/local/nagios/var/objects.cache
    2. object_cache_file=/usr/local/nagios/var/objects.precache
  3. 实施分布式监控:通过NSCA实现多站点数据汇总

五、进阶功能探索

5.1 监控数据可视化

集成Grafana展示历史数据:

  1. 配置InfluxDB时序数据库
  2. 使用pnp4nagiosGraphite作为数据源
  3. 创建自定义仪表盘展示关键指标

5.2 自动化运维集成

通过API实现自动化:

  1. # 使用curl提交被动检查结果
  2. curl -X POST "http://nagios-server/nagios/cgi-bin/cmd.cgi" \
  3. -d "cmd_typ=34&cmd_mod=2&host=web01&service=HTTP&status=0&output=OK"

5.3 容器化部署方案

使用Docker快速部署:

  1. FROM centos:7
  2. RUN yum install -y epel-release && \
  3. yum install -y nagios nagios-plugins httpd php
  4. COPY nagios.cfg /etc/nagios/
  5. CMD ["/usr/sbin/nagios", "/etc/nagios/nagios.cfg"]

通过本文的详细指导,运维人员可以完成从环境准备到高级配置的全流程操作。建议在实际部署前先在测试环境验证配置,逐步扩展监控范围。对于大型企业环境,可考虑结合CMDB系统实现动态主机发现,或通过配置管理工具实现监控配置的版本化管理。