Nagios 简明教程

Nagios - Installation

本章详细讨论了在 Ubuntu 系统上安装 Nagios 的步骤。

In this chapter, the steps to setup Nagios on Ubuntu are discussed in detail.

在安装 Nagios 之前,Ubuntu 系统上必须存在 Apache、PHP、构建包等一些包。因此,让我们先安装它们。

Before you install Nagios, some packages such as Apache, PHP, building packages etc., are required to be present on your Ubuntu system. Hence, let us install them first.

Step 1 − 运行以下命令安装预先需要的包−

Step 1 − Run the following command to install pre-required packages −

sudo apt-get install wget build-essential apache2 php apache2-mod-php7.0 php-gd
libgd-dev sendmail unzip

Step 2 − 接下来,为 Nagios 创建用户和组,并将它们添加到 Apache www-data 用户中。

Step 2 − Next, create user and group for Nagios and add them to Apache www-data user.

sudo useradd nagios
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagios,nagcmd www-data

Step 3 − 下载最新的 Nagios 包。

Step 3 − Download the latest Nagios package.

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-
4.4.3.tar.gz

Step 4 − 提取 tarball 文件。

Step 4 − Extract the tarball file.

tar -xzf nagios-4.4.3.tar.gz
cd nagios-4.4.3/

Step 5 − 运行以下命令从源代码编译 Nagios。

Step 5 − Run the following command to compile Nagios from source.

./configure --with-nagios-group=nagios --with-command-group=nagcmd

Step 6 − 运行以下命令构建 Nagios 文件。

Step 6 − Run the following command to build Nagios files.

make all

Step 7 − 运行下面显示的命令安装所有 Nagios 文件。

Step 7 − Run the command shown below to install all the Nagios files.

sudo make install

Step 8 − 运行以下命令安装 init 和外部命令配置文件。

Step 8 − Run the following commands to install init and external command configuration files.

sudo make install-commandmode
sudo make install-init
sudo make install-config
sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sitesavailable/
nagios.conf

Step 9 − 现在将事件处理程序目录复制到 Nagios 目录。

Step 9 − Now copy the event handler directory to Nagios directory.

sudo cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
sudo chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

Step 10 − 下载并解压 Nagios 插件。

Step 10 − Download and extract Nagios plugins.

cd
wget https://nagios-plugins.org/download/nagiosplugins-
2.2.1.tar.gz
tar -xzf nagios-plugins*.tar.gz
cd nagios-plugins-2.2.1/

Step 11 − 使用以下命令安装 Nagios 插件。

Step 11 − Install Nagios plugins using the below command.

./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
sudo make install

Step 12 − 现在编辑 Nagios 配置文件并取消注释行号 51 → cfg_dir=/usr/local/nagios/etc/servers

Step 12 − Now edit the Nagios configuration file and uncomment line number 51 → cfg_dir=/usr/local/nagios/etc/servers

sudo gedit /usr/local/nagios/etc/nagios.cfg

Step 13 − 现在,创建一个服务器目录。

Step 13 − Now, create a server directory.

sudo mkdir -p /usr/local/nagios/etc/servers

Step 14 − 编辑联系人配置文件。

Step 14 − Edit contacts configuration file.

sudo gedit /usr/local/nagios/etc/objects/contacts.cfg
contacts configuration

Step 15 − 现在启用 Apache 模块并配置用户 nagiosadmin。

Step 15 − Now enable the Apache modules and configure a user nagiosadmin.

sudo a2enmod rewrite
sudo a2enmod cgi
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
sudo ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/

Step 16 − 现在,重新启动 Apache 和 Nagios。

Step 16 − Now, restart Apache and Nagios.

service apache2 restart
service nagios start
cd /etc/init.d/
sudo cp /etc/init.d/skeleton /etc/init.d/Nagios

Step 17 − 编辑 Nagios 文件。

Step 17 − Edit the Nagios file.

sudo gedit /etc/init.d/Nagios
DESC = "Nagios"
NAME = nagios
DAEMON = /usr/local/nagios/bin/$NAME
DAEMON_ARGS = "-d /usr/local/nagios/etc/nagios.cfg"
PIDFILE = /usr/local/nagios/var/$NAME.lock

Step 18 − 使 Nagios 文件可执行并启动 Nagios。

Step 18 − Make the Nagios file executable and start Nagios.

sudo chmod +x /etc/init.d/nagios
service apache2 restart
service nagios start

Step 19 − 现在转到你的浏览器并打开 URL → http://localhost/nagios 。现在使用用户名 nagiosadmin 登录 Nagios,并使用你之前设置的密码。Nagios 登录屏幕如下图所示 −

Step 19 − Now go to your browser and open url → http://localhost/nagios. Now login to Nagios with username nagiosadmin and use the password which you had set earlier. The login screen of Nagios is as shown in the screenshot given below −

contacts screenshot

如果你正确地按照所有步骤操作,你的 Nagios Web 界面将会显示。你可以找到如下图所示的 Nagios 仪表板 −

If you have followed all the steps correctly, you Nagios web interface will show up. You can find the Nagios dashboard as shown below −

nagios dashboard