简介
Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设备,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知
安装前准备
本文 Nagios 的安装将从官网获取最新的源码包进行编译,因此需要服务器拥有编译环境,同时 Nagios 的 Web界面需要有 Web 服务器和 PHP 运行环境。本文 Web 服务器使用 Apache,用户也可以自行选择使用其他 Web 服务器。
#安装Apache 、 php
yum install httpd php
#安装nagios 编译环境
yum install gcc glibc glibc-common unzip
yum install gd gd-devel
#创建Nagios用户
useradd nagios -s /sbin/nologin
#将 nagios 和 apache(web 服务器的运行用户) 用户添加至该组
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
1.下载 Nagios 及其插件
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz
2.Nagios 核心的编译与安装
解压源码包
tar -zxvf nagios-4.3.1.tar.gz
cd nagios-4.3.1
编译配置信息
./configure --with-command-group=nagcmd
编译
make all
安装 Nagios,并初始化脚本及基础配置文件
make install
make install-init
make install-config
make install-commandmode
此时先不用启动 Nagios,安装步骤尚未完成
3.自定义配置信息
配置文件在目录 /usr/local/nagios/etc 下,通过这些基础配置信息可以直接启动 Nagios,现在唯一需要更改的是联系人信息,可以编辑 /usr/local/nagios/etc/objects/contacts.cfg ,将联系人邮件地址换成你自己的。
vi /usr/local/nagios/etc/objects/contacts.cfg
4.配置 web 服务器
创建 Apache 的配置文件,配置文件在目录 /etc/httpd/conf.d/
make install-webconf
为 Nagios 的 Web 界面创建 nagiosadmin 用户,并设置密码。
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
启动 Apache
systemctl start httpd
5.编译并安装 Nagios 插件包
Nagios 实际的监控脚本都是通过插件实现的,本文中的插件包是官方提供插件包(大约50个用于监控的插件),更多的插件可以去社区下载(https://exchange.nagios.org/),当然也可以自己编写插件。
解压插件包
tar -zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1
编译并安装
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
6.启动 Nagios 核心
将 Nagios 核心加入自动启动列表
chkconfig --add nagios
chkconfig nagios on
检查 Nagios 基础配置文件(在之后修改过配置文件后,都可以用下边的方式进行检查)
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果没有错误,就可以直接启动 Nagios
systemctl start nagios
7.SELinux 设置
Centos 系统默认 SELinux 是 Enforcing 模式,这会导致访问 Nagios CGIs 时出现 Internal Server Error 的错误。为避免这个错误可以直接关闭 SELinux 或者将其设置为 Permissive 模式。SELinux配置文件在/etc/selinux/config。注意,修改 SELinux 配置后需要重新启动操作系统。
如果不想关闭 SELinux 或者将其设置为 Permissive ,即在 enforcing/targeted 模式下可以使用如下命令进行设置
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
8.登录 Web 界面查看
访问 http://localhost/nagios,通过 nagiosadmin 用户及刚才设置的密码登录。
点击左侧目录中的 Service,可以查看当前监控的服务。
9.登录 Web 界面查看
至此,Nagios 的基础安装就结束了。这里还要补充说明的一下,服务器的防火墙需要允许 apache 80 端口的访问,可以通过如下命令设置防火墙,允许 80 端口的访问。
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload