いつかエンジニアになりたい

Nothing is too late to start

CentOS7にNagios4をインストールする

f:id:berukann:20190626222204p:plain

Nagiosという有名な監視ソフトウェアがあります。最近では、Mackerel、Datadog、NewRelicなどSaaSの監視ソリューションを利用する機会が多いとは思いますが、業務上、利用方法を覚える必要があったため、今更ながら勉強しました。

Nagios とは

  • Nagios(ナギオス)は、代表的な統合監視ソフトウェアの1つです。よくZabbixと比較されることが多い
  • 1999年頃に NetSaint の名称で開発が開始。2002年に現在のNagiosという名称に変更され、現在でも開発は続いている(2019年06月現在v4.4.3)
  • Nagiosは、「OSS版のNagios」と「商用版のNagios」(Nagios XI) が存在するが、後述では「OSS版のNagios」について記載する

Nagiosの特徴

Nagios本体はあくまでもスケジューリングしか実施せず、監視自体はしない

設定、データなどはすべてテキストファイルベースで保持する

  • Nagiosは、設定、データなどをDBではなく、 すべてテキストファイルで保持するため、インストールは非常に容易。WEBサーバーが必要なくらい

非常にシンプルなGUIのみであり、設定変更等はすべて基本的にファイルを編集する必要がある

  • NagiosGUIは非常にシンプルな簡易UIしかないので、監視項目、現在の状態確認や通知の有効・無効くらいしかできない
  • デフォルトでメトリクスのグラフ等はないため、OSSの可視化ツール(例:nagiosgraph)を利用したり、Grafana等で可視化する仕組みを整える必要がある
  • この点において、GUIでほとんどの事が可能なZABBIXとは圧倒的に違いがある
  • 大規模な環境を監視する場合は、Nagiosの設定ファイルを統合的に管理する仕組み、ルール作りが必須

主要なコンポーネント

Nagiosは、大きく以下の3つのコンポーネントから構成されて

  • Nagios Core
    • Nagiosの本体部分。Nagios Pluginのスケジューリング、通知を実施する
  • Nagios Plugin
    • Nagiosの監視部分。実際に監視、値の取得を実施し、Nagios Core に結果を返す
    • 2019年現在、公式のプラグインだけで50以上、コミュニティを合わせると3000を超えるプラグインが存在している
  • Nagios Addon
    • Nagiosの機能を拡張してくれるものであり、こちらもコミュニティに100を超えるアドオンが存在している
    • 例:NRPE、NSCA、NSClient++、NDOUtils etc ...

上記の通り、Nagios Coreは、あくまでもNagios Pluginをキックするだけなので、何か監視を実施したい場合は、何かしらのプラグインを監視先サーバーにインストールする必要がある。

例えば、Webページにアクセスできるかなどの外形監視ではなく、監視対象サーバー上のCPUリソースなどを取得したい場合、Nagiosプラグインを監視先サーバーにインストール後、NRPEというNagios Addonを起動させ、監視サーバー(監視対象元)からキックするイメージ。

f:id:berukann:20190627230305p:plain

Nagios インストール方法

Nagiosをインストールする方法は(1)ソースからビルドする か(2)パッケージ管理システム経由でインストールする の2種類がある。今回、インストールするのが初めてなので、両方のパターンでインストールを実施してみる

インストール環境

今回、Vagrant経由でCentOS7のVMを起動し、VM上にNagiosCoreをインストールする。また、NagiosCoreだけでは監視ができないため、公式のNagiosPluginも合わせてインストールし、設定ファイルの場所を確認する

macOS Mojava 10.14.5(ホスト)
Vagrant 2.2.4(ホスト)
CentOS 7.6(ゲスト)

(1)ソースからビルドする

1.1. Installing The Nagios

$ mkdir centos7-source; cd centos7-source
$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
  • Nagiosを起動した際に8080ポートでアクセスできるよう、ポートフォワードの設定を有効後、VMを起動する
$ cp Vagrantfile Vagrantfile.org
$ vim Vagrantfile
$diff Vagrantfile Vagrantfile.org
26c26
<   config.vm.network "forwarded_port", guest: 80, host: 8080
---
>   # config.vm.network "forwarded_port", guest: 80, host: 8080

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Setting the name of the VM: centos7-source_default_1561561101101_88523
・・・
Redirecting to /bin/systemctl start vboxadd.service
Redirecting to /bin/systemctl start vboxadd-service.service
Unmounting Virtualbox Guest Additions ISO from: /mnt
==> default: Checking for guest additions in VM...
==> default: Rsyncing folder: /Users/xxxxx/work/centos7-source/ => /vagrant
$ vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[vagrant@localhost ~]$ sudo su -
Last login: 水  626 15:08:12 UTC 2019 on pts/0
[root@localhost ~]# localectl set-locale LANG=ja_JP.utf8
[root@localhost ~]# timedatectl set-timezone Asia/Tokyo
[root@localhost ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y gcc glibc glibc-common wget unzip httpd php gd gd-devel perl postfix
[root@localhost ~]# cd /tmp
[root@localhost tmp]# wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.3.tar.gz
[root@localhost tmp]# tar xzf nagioscore.tar.gz
[root@localhost tmp]# cd /tmp/nagioscore-nagios-4.4.3/
[root@localhost nagioscore-nagios-4.4.3]# ./configure
[root@localhost nagioscore-nagios-4.4.3]# make all
[root@localhost nagioscore-nagios-4.4.3]# make install-groups-users
[root@localhost nagioscore-nagios-4.4.3]# usermod -a -G nagios apache
[root@localhost nagioscore-nagios-4.4.3]# make install
[root@localhost nagioscore-nagios-4.4.3]# make install-daemoninit
[root@localhost nagioscore-nagios-4.4.3]# systemctl enable httpd.service
[root@localhost nagioscore-nagios-4.4.3]# make install-commandmode
[root@localhost nagioscore-nagios-4.4.3]# make install-config
[root@localhost nagioscore-nagios-4.4.3]# make install-webconf
[root@localhost nagioscore-nagios-4.4.3]# firewall-cmd --zone=public --add-port=80/tcp
[root@localhost nagioscore-nagios-4.4.3]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@localhost nagioscore-nagios-4.4.3]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin # NagiosのWebUIへのログインパスワードを入力
New password:
Re-type new password:
Adding password for user nagiosadmin
[root@localhost nagioscore-nagios-4.4.3]# systemctl start httpd.service
[root@localhost nagioscore-nagios-4.4.3]# systemctl start nagios.service

1.2. Installing The Nagios Plugins

  • Nagios Pluginをソースからビルドし、インストールする
[root@localhost ~]# yum install -y gcc glibc glibc-common make gettext automake autoconf wget openssl-devel net-snmp net-snmp-utils epel-release
[root@localhost ~]# yum install -y perl-Net-SNMP
[root@localhost ~]# cd /tmp
[root@localhost ~]# wget --no-check-certificate -O nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz
[root@localhost ~]# tar zxf nagios-plugins.tar.gz
[root@localhost ~]# cd /tmp/nagios-plugins-release-2.2.1/
[root@localhost ~]# ./tools/setup
[root@localhost ~]# ./configure
[root@localhost ~]# make
[root@localhost ~]# make install
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# systemctl start nagios.service

(2)パッケージ管理システム経由でインストールする

2.1. Installing The Nagios

$ mkdir centos7-yum; cd centos7-yum
$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
  • Nagiosを起動した際に8081ポートでアクセスできるよう、ポートフォワードの設定を有効後、VMを起動する
$ cp Vagrantfile Vagrantfile.org
$ vim Vagrantfile

$diff Vagrantfile Vagrantfile.org
26c26
<   config.vm.network "forwarded_port", guest: 80, host: 8081
---
>   # config.vm.network "forwarded_port", guest: 80, host: 8080

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Setting the name of the VM: centos7-source_default_1561561101101_88523
・・・
Redirecting to /bin/systemctl start vboxadd.service
Redirecting to /bin/systemctl start vboxadd-service.service
Unmounting Virtualbox Guest Additions ISO from: /mnt
==> default: Checking for guest additions in VM...
==> default: Rsyncing folder: /Users/xxxxx/work/centos7-source/ => /vagrant
$ vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
  • NagiosCoreをYUM経由でインストールする
[vagrant@localhost ~]$ sudo su -
Last login: 水  626 15:08:12 UTC 2019 on pts/0
[root@localhost ~]# localectl set-locale LANG=ja_JP.utf8
[root@localhost ~]# timedatectl set-timezone Asia/Tokyo
[root@localhost ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install -y nagios

[root@localhost ~]# htpasswd -c /etc/nagios/passwd nagiosadmin # NagiosのWebUIへのログインパスワードを入力
New password:
Re-type new password:
Adding password for user nagiosadmin
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# systemctl start nagios.service

2.2. Installing The Nagios Plugins

  • Nagios PluginをYUM経由でインストールする
[root@localhost ~]# yum install -y nagios-plugins-all
[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# systemctl start httpd.service

インストール方法ごとのインストールディレクトリ比較

項目 ソース経由 YUM経由
NagiosCore Install Directory /etc/nagios/ /usr/local/nagios/etc/
NagiosPlugin Install Directory /usr/local/nagios/libexec/ /usr/lib64/nagios/plugins/

まとめ

  • Nagiosは、予想以上にシンプルだが、柔軟性が高く、非常に強力な監視ツール
  • Nagiosは、商用版とOSS版がある。また、Nagiosという名前は総称であり、コンポーネントが3つある。中でも、実際に監視を実施しているのはプラグイン
  • 当たり前だが、ソースでビルドした場合とパッケージ管理システムでインストールした場合ではインストールディレクトリが違うので注意
  • これも当たり前だが、パッケージ管理システムでインストールすると本当にあっという間だと感じた
  • 今後は、NRPEを用いた監視やNagiosデータのグラフ化、Slack通知など実用的な部分を検証していく

補足1:参考ページ

www.atmarkit.co.jp

techblog.gmo-ap.jp

tech.pepabo.com

補足2:監視ツールの Google Trend

Zabbixは結構頑張ってるけど、Nagiosは減ってきている。枯れてるっていうのもあるけど。期間を5年でやってるから、Zabbix、Nagiosが圧倒的に見えるけど、最近だとSaaSが強すぎる・・・。