在 Ubuntu 16.04 上安裝 Nagios 4.x

因為 Ubuntu 16.04 套件庫中的 Nagios 是 3.x 版,所以要改用手動的方式進行安裝
# apt-cache search nagios | grep ^nagios
nagios-images – Collection of images and icons for the nagios system
nagios-nrpe-plugin – Nagios Remote Plugin Executor Plugin
nagios-nrpe-server – Nagios Remote Plugin Executor Server
nagios-plugins – transitional dummy package (nagios-plugins to monitoring-plugins)
nagios-plugins-basic – transitional dummy package
nagios-plugins-standard – transitional dummy package
nagios3 – host/service/network monitoring and management system
nagios3-cgi – cgi files for nagios3
nagios3-common – support files for nagios3
nagios3-core – host/service/network monitoring and management system core files
nagios3-dbg – debugging symbols and debug stuff for nagios3
nagios3-doc – documentation for nagios3
nagios-check-xmppng – monitoring plugin to check XMPP servers
nagios-plugin-check-multi – run nagios checks as a group
nagios-plugins-common – transitional dummy package
nagios-plugins-contrib – Plugins for nagios compatible monitoring systems
nagios-plugins-extra – transitional dummy package
nagios-plugins-rabbitmq – Set of Nagios checks useful for monitoring a RabbitMQ server
nagios-snmp-plugins – SNMP Plugins for nagios
nagios2mantis – Automatically create Mantis issues on Nagios events

參考網頁:
How to Install Nagios on Ubuntu 16.04
How To Install Nagios Core 4.1.1 In Ubuntu 15.10/16.04 | Unixmen
Install Nagios core 4.1.1 on Ubuntu 16.04 (Xenial Xerus) Server | Ubuntu Geek
How To Install Nagios on Ubuntu 16.04 – idroot[@more@]請先安裝好 Apache / MySQL / PHP
1. 安裝編譯及必要套件
# apt-get install build-essential libgd-dev apache2-utils unzip 

2. 建立使用者及群組資料
# useradd -m nagios
# groupadd nagcmd
# usermod -a -G nagcmd nagios
# usermod -a -G nagcmd www-data

3. 到 Nagios 官方網站下載所須程式
Nagios Core 下載網站:https://www.nagios.org/downloads/nagios-core/
Nagios Plugin 下載網頁:https://www.nagios.org/downloads/nagios-plugins/
下載時的最新版本 Nagios Core 4.3.1 / Nagios Plugin 2.1.4


# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
# wget https://nagios-plugins.org/download/nagios-plugins-2.1.4.tar.gz

4. 解壓縮
# tar xvzf nagios-4.3.1.tar.gz

5. 切換目錄
# cd nagios-4.3.1

6. 開始編譯
# ./configure –with-command-group=nagcmd
 General Options:
 ————————-
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagcmd
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  ${prefix}/var/nagios.lock
   Check result directory:  ${prefix}/var/spool/checkresults
           Init directory:  /etc/init.d
  Apache conf.d directory:  /etc/apache2/sites-available
             Mail program:  /bin/mail
                  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):

Review the options above for accuracy.  If they look okay,
type ‘make all’ to compile the main program and CGIs.

7. 進行安裝
# make all
# make install
You can continue with installing Nagios as follows (type ‘make’
without any arguments for a list of all possible options):

  make install-init
     – This installs the init script in /etc/init.d

  make install-commandmode
     – This installs and configures permissions on the
       directory for holding the external command file

  make install-config
     – This installs sample config files in /usr/local/nagios/etc

# make install-init
/usr/bin/install -c -m 755 -d -o root -g root /etc/init.d
/usr/bin/install -c -m 755 -o root -g root daemon-init /etc/init.d/nagios

*** Init script installed ***

# make install-commandmode
/usr/bin/install -c -m 775 -o nagios -g nagcmd -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw

*** External command directory configured ***

# make install-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc/objects
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/nagios.cfg /usr/local/nagios/etc/nagios.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/cgi.cfg /usr/local/nagios/etc/cgi.cfg
/usr/bin/install -c -b -m 660 -o nagios -g nagios sample-config/resource.cfg /usr/local/nagios/etc/resource.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/templates.cfg /usr/local/nagios/etc/objects/templates.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/commands.cfg /usr/local/nagios/etc/objects/commands.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/timeperiods.cfg /usr/local/nagios/etc/objects/timeperiods.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/localhost.cfg /usr/local/nagios/etc/objects/localhost.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/windows.cfg /usr/local/nagios/etc/objects/windows.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/printer.cfg /usr/local/nagios/etc/objects/printer.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/switch.cfg /usr/local/nagios/etc/objects/switch.cfg

*** 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.

8. 安裝 Nagios Web interface:
# make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
if [ 1 -eq 1 ]; then
        ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/nagios.conf;
fi

*** Nagios/Apache conf file installed ***

9. 建立密碼
# /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin

10. 安裝  Nagios plugins:
切換目錄
# cd ..
解壓縮
# tar xvzf nagios-plugins-2.1.4.tar.gz
切換目錄
# cd nagios-plugins-2.1.4
進行編譯
# ./configure –with-nagios-user=nagios –with-nagios-group=nagios

安裝
# make
# make install

11. 修改收件者
# sed -i ‘s/nagios@localhost/xxxxx@gmail.com/’ /usr/local/nagios/etc/objects/contacts.cfg

12. 開啟 rewrite / cgi 功能
# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  service apache2 restart

# a2enmod cgi
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message
Enabling module cgi.
To activate the new configuration, you need to run:
  service apache2 restart

13. 重新啟動 Apache Web Server
# systemctl restart apache2.service

14. 檢查 Nagios 設定檔
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Nagios Core 4.3.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 02-23-2017
License: GPL

Website: https://www.nagios.org
Reading configuration data…
   Read main config file okay…
   Read object config files okay…

Running pre-flight check on configuration data…

Checking objects…
        Checked 8 services.
        Checked 1 hosts.
        Checked 1 host groups.
        Checked 0 service groups.
        Checked 1 contacts.
        Checked 1 contact groups.
        Checked 24 commands.
        Checked 5 time periods.
        Checked 0 host escalations.
        Checked 0 service escalations.
Checking for circular paths…
        Checked 1 hosts
        Checked 0 service dependencies
        Checked 0 host dependencies
        Checked 5 timeperiods
Checking global event handlers…
Checking obsessive compulsive processor commands…
Checking misc settings…

Total Warnings: 0
Total Errors:   0

Things look okay – No serious problems were detected during the pre-flight check

15. 啟動 Nagios Server
# /etc/init.d/nagios start
[….] Starting nagios (via systemctl): nagios.serviceFailed to start nagios.service: Unit nagios.service not found.
 failed!

如果出現錯誤訊息,進行下面修改
備份原檔
# mv /etc/init.d/nagios /etc/init.d/nagios.$(date +%F)
複製 /etc/init.d/skeleton 成為 /etc/init.d/nagios
# cp /etc/init.d/skeleton /etc/init.d/nagios
# vim /etc/init.d/nagios
把以下二行刪除
DESC=”Description of the service”
DAEMON=/usr/sbin/daemonexecutablename
改成
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

更改檔案執行權限
# chmod +x /etc/init.d/nagios


# mv /etc/init.d/nagios /etc/init.d/nagios.$(date +%F)
# vim /etc/systemd/system/nagios.service
[Unit]
Description=Nagios
BindTo=network.target

[Install]
WantedBy=multi-user.target

[Service]
User=nagios
Group=nagios
Type=simple
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg

# systemctl enable /etc/systemd/system/nagios.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nagios.service to /etc/systemd/system/nagios.service.

# systemctl start nagios.service

16. 啟動 Nagios Server
# /etc/init.d/nagios start
 * Starting Nagios nagios                                                [ OK ]

17. 建立用來存其它偵測主機的目錄
# mkdir /usr/local/nagios/etc/conf.d
# sed -i ‘/#cfg_dir=/usr/local/nagios/etc/routers/acfg_dir=/usr/local/nagios/etc/conf.d’ /usr/local/nagios/etc/nagios.cfg