如何在CentOS 7上安装和配置Nagios
Nagios是最受欢迎的开源监视系统之一。 Nagios会保留整个IT基础架构的清单,并确保您的网络,服务器,应用程序,服务和流程已启动并正在运行。 如果出现故障或性能不佳,Nagios将通过各种方法发送通知警报。
本教程描述了如何在CentOS 7服务器上安装和配置Nagios Core。
先决条件
在继续本教程之前,请确保您以具有sudo特权的用户身份登录。
禁用SELinux或按照此处的说明将其设置为许可模式。
更新您的CentOS系统并安装Apache,PHP以及下载和编译Nagios主应用程序和Nagios插件所需的所有软件包:
sudo yum update
sudo yum install httpd php php-cli gcc glibc glibc-common gd gd-devel net-snmp openssl-devel wget
sudo yum install make gettext autoconf net-snmp-utils epel-release perl-Net-SNMP postfix unzip automake
在CentOS上安装Nagios#
执行以下步骤,从源代码安装最新版本的Nagios Core。
1.下载Nagios#
我们将在 /usr/src
该目录是放置源文件的常用位置。
使用以下内容导航到目录:
cd /usr/src/
使用以下wget命令从项目Github存储库下载最新版本的Nagios:
sudo wget https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.2.tar.gz
下载完成后,使用以下命令解压缩tar文件:
sudo tar zxf nagios-*.tar.gz
在继续下一步之前,请确保通过键入以下内容更改为Nagios源目录:
cd nagioscore-nagios-*/
2.编译Nagios#
要开始构建过程,请运行 configure
该脚本将执行大量检查,以确保系统上的所有依赖项都存在:
sudo ./configure
成功完成后,将在屏幕上显示以下消息:
*** Configuration summary for nagios 4.4.2 2018-08-16 ***:
General Options:
-------------------------
Nagios executable: nagios
Nagios user/group: nagios,nagios
Command user/group: nagios,nagios
Event Broker: yes
Install ${prefix}: /usr/local/nagios
Install ${includedir}: /usr/local/nagios/include/nagios
Lock file: /run/nagios.lock
Check result directory: /usr/local/nagios/var/spool/checkresults
Init directory: /lib/systemd/system
Apache conf.d directory: /etc/httpd/conf.d
Mail program: /sbin/sendmail
Host OS: linux-gnu
IOBroker Method: epoll
Web Interface Options:
------------------------
HTML URL: http://localhost/nagios/
CGI URL: http://localhost/nagios/cgi-bin/
Traceroute (used by WAP): /bin/traceroute
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.
使用以下命令开始编译过程 make
命令:
sudo make all
编译可能需要一些时间,具体取决于您的系统。 构建过程完成后,屏幕上将显示以下消息:
....
*** Compile finished ***
...
For more information on obtaining support for Nagios, visit:
Home
*************************************************************
Enjoy.
3.创建Nagios用户和组#
创建一个新系统 nagios
用户和组通过发出:
sudo make install-groups-users
输出如下所示:
groupadd -r nagios
useradd -g nagios nagios
添加Apache apache
用户到 nagios
组:
sudo usermod -a -G nagios apache
4.安装Nagios Binaries#
运行以下命令以安装Nagios二进制文件,CGI和HTML文件:
sudo make install
您应该看到以下输出:
...
*** Main program, CGIs and HTML files installed ***
...
5.创建外部命令目录
Nagios可以处理来自外部应用程序的命令。 创建外部命令目录并通过键入以下命令设置适当的权限:
sudo make install-commandmode
*** External command directory configured ***
6.安装Nagios配置文件#
使用以下命令安装样本Nagios配置文件:
sudo make install-config
...
*** Config files installed ***
Remember, these are *SAMPLE* config files. You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.
7.安装Apache配置文件#
运行以下命令以安装Apache Web服务器配置文件:
sudo make install-webconf
...
*** Nagios/Apache conf file installed ***
8.创建系统单位文件编号
以下命令将安装systemd单位文件,还将nagios服务配置为在引导时启动。
sudo make install-daemoninit
...
*** Init script installed ***
9.创建用户帐号
为了能够访问Nagios Web界面,我们将创建一个管理员用户,名为 nagiosadmin
运行以下命令 htpasswd
创建一个名为的用户的命令 nagiosadmin
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
系统将提示您输入并确认用户密码。
New password:
Re-type new password:
Adding password for user nagiosadmin
重新启动Apache服务以使更改生效:
sudo systemctl restart httpd
配置Apache服务以在启动时启动。
sudo systemctl enable httpd
10.配置防火墙#
防火墙将保护您的服务器免受不必要的流量的攻击。
如果您的服务器上未配置防火墙,则可以查看我们的指南,了解如何在centos上设置带有firewalld的防火墙
通过运行以下命令打开Apache端口:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
安装Nagios插件#
切换回 /usr/src
目录:
cd /usr/src/
从项目Github存储库下载最新版本的Nagios插件:
sudo wget -O nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz
下载完成后,解压缩tar文件:
sudo tar zxf nagios-plugins.tar.gz
转到插件源目录:
cd nagios-plugins-release-2.2.1
逐一运行以下命令以编译并安装Nagios插件:
sudo ./tools/setup
sudo ./configure
sudo make
sudo make install
启动Nagios#
现在已经安装了Nagios及其插件,请使用以下命令启动Nagios服务:
sudo systemctl start nagios
要验证Nagios是否正在运行,请使用以下命令检查服务状态:
sudo systemctl status nagios
输出应如下所示,表明Nagios服务处于活动状态并正在运行。
nagios.service - Nagios Core 4.4.2
Loaded: loaded (/usr/lib/systemd/system/nagios.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2018-12-08 14:33:35 UTC; 3s ago
Docs: https://www.nagios.org/documentation
Process: 22217 ExecStart=/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
Process: 22216 ExecStartPre=/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS)
Main PID: 22219 (nagios)
CGroup: /system.slice/nagios.service
访问Nagios Web界面#
要访问Nagios Web界面,请打开您喜欢的浏览器,然后输入服务器的域名或公共IP地址,然后输入 /nagios
:
http(s)://your_domain_or_ip_address/nagios
输入 nagiosadmin
用户登录凭据,您将被重定向到默认的Nagios主页,如下图所示:
结论#
您已成功从CentOS系统上的源代码成功安装了最新的Nagios版本。
现在,您应该查看Nagios文档,并了解有关如何配置和使用Nagios的更多信息。
如果您遇到问题或有反馈,请在下面发表评论。
centos nagios监控