LAMP構築 発展
監視
Nagios
Zabbix
Prometheus
監視用VMを立てる
code:mp
~
$ mp launch --name monitoring --mem 2G --disk 20G --cpus 2 22.04
Launched: monitoring
~
$ mp list
Name State IPv4 Image
lamp Running 192.168.64.52 Ubuntu 22.04 LTS
monitoring Running 192.168.64.53 Ubuntu 22.04 LTS
Nagiosを調査する
公式
https://www.nagios.org/
https://www.nagios.com/solutions/ubuntu-monitoring/
エージェント型なので監視対象サーバーにエージェントをインストールする必要あり
ここに書いてあるのはサーバー本体の監視でLAMPなどのアプリケーションを監視をするものではない
including operating system metrics, service state, process state, file system usage, and more.
https://www.nagios.com/solutions/application-monitoring/
Nagios XIとは
企業向けの監視パッケージ
https://www.nagios.com/products/nagios-core/
自前で構築するならばNagios Coreを使う
Nagios {XI, Log Server, Fusion, Network Analyzer}は企業向けパッケージと思われる
ということでNagios Coreを構築していく
サーバの初期設定
code:time
ubuntu@monitoring:~$ timedatectl status
Local time: Mon 2023-06-05 18:45:46 JST
Universal time: Mon 2023-06-05 09:45:46 UTC
RTC time: Mon 2023-06-05 09:45:47
Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
ubuntu@monitoring:~$ sudo apt install chrony
ubuntu@monitoring:~$ sudo vim /etc/chrony/sources.d/jst-ntp-server.sources
ubuntu@monitoring:~$ sudo vim /etc/chrony/chrony.conf
ubuntu@monitoring:~$ sudo systemctl restart chronyd
ubuntu@monitoring:~$ chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* ntp1.jst.mfeed.ad.jp 2 6 17 33 +710us -184us +/- 76ms
^- ntp2.jst.mfeed.ad.jp 2 6 17 33 +1905us+1905us +/- 71ms
^+ ntp3.jst.mfeed.ad.jp 2 6 17 34 +799us -95us +/- 88ms
code:/etc/chrony/sources.d/jst-ntp-server.sources
server ntp1.jst.mfeed.ad.jp iburst
server ntp2.jst.mfeed.ad.jp iburst
server ntp3.jst.mfeed.ad.jp iburst
code:/etc/chrony/chrony.conf
下記をコメントアウト
# pool ntp.ubuntu.com iburst maxsources 4
# pool 0.ubuntu.pool.ntp.org iburst maxsources 1
# pool 1.ubuntu.pool.ntp.org iburst maxsources 1
# pool 2.ubuntu.pool.ntp.org iburst maxsources 2
code:ufw
ubuntu@monitoring:~$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
ubuntu@monitoring:~$ sudo ufw status
Status: active
ubuntu@monitoring:~$ sudo ufw allow 22
Rule added
Rule added (v6)
ubuntu@monitoring:~$ sudo ufw status
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
code:selinux
ubuntu@monitoring:~$ sudo apt install selinux-utils
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package selinux-utils
ubuntu@monitoring:~$ sudo apt update
(中略)
ubuntu@monitoring:~$ sudo apt install selinux-utils
ubuntu@monitoring:~$ getenforce
Disabled
Nagios Coreのインストール
ダウンロード
https://www.nagios.org/downloads/nagios-core/?__hstc=189745844.6e08f6a397cb939a0656bf1eea09316e.1685523985073.1685523985073.1685957151446.2&__hssc=189745844.11.1685957151446&__hsfp=1835538915
名前、メールアドレス、電話番号を入力するとダウンロードリンクが出てくる
4.4.13が最新なのでそのバージョンをダウンロードする
とりあえずwgetでサーバーにダウンロードしておく
Installation Documentation
https://support.nagios.com/kb/article/nagios-core-installing-nagios-core-from-source-96.html
Nagios Core Documentation
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/toc.html
ここを参考にインストール作業を進める
https://support.nagios.com/kb/article/nagios-core-installing-nagios-core-from-source-96.html#Ubuntu
Ubuntu 22.04向けの説明はないが、20.04を参考に進めてみる(PHPのバージョンが少し気になる)
code:install
ubuntu@monitoring:~$ sudo apt-get update
ubuntu@monitoring:~$ sudo apt-get install -y autoconf gcc libc6 make wget unzip apache2 php libapache2-mod-php7.4 libgd-dev
ubuntu@monitoring:~$ sudo apt-get install openssl libssl-dev
ubuntu@monitoring:~$ cd /tmp
ubuntu@monitoring:/tmp$ wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.6.tar.gz
ubuntu@monitoring:/tmp$ tar xzf nagioscore.tar.gz
ubuntu@monitoring:/tmp$ cd /tmp/nagioscore-nagios-4.4.6/
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo make all
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo make install-groups-users
groupadd -r nagios
useradd -g nagios nagios
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo usermod -a -G nagios www-data
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo make install
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo make install-daemoninit
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo make install-commandmode
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo make install-config
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo make install-webconf
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo a2enmod rewrite
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo a2enmod cgi
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo ufw allow Apache
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo ufw reload
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo systemctl restart apache2.service
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo systemctl start nagios.service
ブラウザでNagiosのWebコンソールにアクセスする
http://192.168.64.53/nagios/
ログインしたら開いた
https://gyazo.com/b5230b626ffaeef50960dce17f7233dc
ここまででNagios Coreのインストールは完了
次はNagios Pluginsをインストールする
Nagios Pluginsのインストール
Nagios Pluginsとは
check_ldapとかcheck_dig check_dnsとかいろいろある
check_ldapを入れればldapを監視できるようになるという感じだと思われる
https://support.nagios.com/kb/article.php?id=569#Ubuntu
ひとまずNagios Coreのインストール手順の続きをやる(Nagios Pluginsまでカバーしている)
https://support.nagios.com/kb/article/nagios-core-installing-nagios-core-from-source-96.html#Ubuntu
code:install
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ sudo apt-get install -y autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext
ubuntu@monitoring:/tmp/nagioscore-nagios-4.4.6$ cd /tmp
ubuntu@monitoring:/tmp$ wget --no-check-certificate -O nagios-plugins.tar.gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.4.5.tar.gz
ubuntu@monitoring:/tmp$ tar zxf nagios-plugins.tar.gz
ubuntu@monitoring:/tmp$ cd /tmp/nagios-plugins-release-2.4.5/
ubuntu@monitoring:/tmp/nagios-plugins-release-2.4.5$ sudo ./tools/setup
ubuntu@monitoring:/tmp/nagios-plugins-release-2.4.5$ sudo ./configure
ubuntu@monitoring:/tmp/nagios-plugins-release-2.4.5$ sudo make
ubuntu@monitoring:/tmp/nagios-plugins-release-2.4.5$ sudo make install
Hostのlocalhostを選択し,Re-schedule the next checkをCommitすると現在のいくつかの状態が表示された
https://gyazo.com/908e6de809eb406522e2fae2c01827ee
PINGとかHTTPとかが表示されている
https://gyazo.com/be5749dd4e9ebc9acb292635d34a2a58
LAMPのサーバーの接続する
設定ファイルなどは/usr/local/nagios/etcにある
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/configmain.html#cfg_dir
Object Configuration Directoryを作成する
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/configmain.html#cfg_dir
code:mkdir
ubuntu@monitoring:/usr/local/nagios/etc$ sudo mkdir hosts
ubuntu@monitoring:/usr/local/nagios/etc$ ls -la
total 88
drwxrwxr-x 4 nagios nagios 4096 Jun 5 23:19 .
drwxr-xr-x 8 root root 4096 Jun 5 19:16 ..
-rw-rw-r-- 1 nagios nagios 13710 Jun 5 19:16 cgi.cfg
drwxr-xr-x 2 root root 4096 Jun 5 23:19 hosts
-rw-r--r-- 1 root root 50 Jun 5 19:20 htpasswd.users
-rw-rw-r-- 1 nagios nagios 45843 Jun 5 19:16 nagios.cfg
drwxrwxr-x 2 nagios nagios 4096 Jun 5 19:16 objects
-rw-rw---- 1 nagios nagios 1312 Jun 5 19:16 resource.cfg
ubuntu@monitoring:/usr/local/nagios/etc$ sudo chown nagios:nagios hosts/
ubuntu@monitoring:/usr/local/nagios/etc$ sudo chmod 775 hosts/
ubuntu@monitoring:/usr/local/nagios/etc$ ls -la
total 88
drwxrwxr-x 4 nagios nagios 4096 Jun 5 23:21 .
drwxr-xr-x 8 root root 4096 Jun 5 19:16 ..
-rw-rw-r-- 1 nagios nagios 13710 Jun 5 19:16 cgi.cfg
drwxrwxr-x 2 nagios nagios 4096 Jun 5 23:21 hosts
-rw-r--r-- 1 root root 50 Jun 5 19:20 htpasswd.users
-rw-rw-r-- 1 nagios nagios 45843 Jun 5 19:16 nagios.cfg
drwxrwxr-x 2 nagios nagios 4096 Jun 5 19:16 objects
-rw-rw---- 1 nagios nagios 1312 Jun 5 19:16 resource.cfg
hostの設定ファイルを作成する
code:/usr/local/nagios/etc/hosts/lamp.cfg
define host {
use generic-host ; Inherit default values from a template
host_name lamp ; The name we're giving to this host
alias LAMP Server ; A longer name associated with the host
address 192.168.64.52 ; IP address of the host
hostgroups allhosts ; Host groups this host is associated with
}
設定ファイルのチェックを行う
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/verifyconfig.html
code:check
ubuntu@monitoring:/usr/local/nagios/etc$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.4.6
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2020-04-28
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
以下のコマンドでNagios Coreの再起動を試みたが、ディレクトリがないと言われた
code:restart
ubuntu@monitoring:/usr/local/nagios/etc$ /etc/rc.d/init.d/nagios reload
-bash: /etc/rc.d/init.d/nagios: No such file or directory
一応Webコンソールでも同じことができるようなので、そっちでやってみた
Process Info -> Restart the Nagios process -> Commit
何も変わらなかった。おそらく追加した設定が読み込まれていない。
cfg_dirを設定する必要がある。
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/configmain.html#cfg_dir
ここに書いてあるのは設定例
code:cfgdir
cfg_dir=/usr/local/nagios/etc/commands
cfg_dir=/usr/local/nagios/etc/services
cfg_dir=/usr/local/nagios/etc/hosts
code:error
ubuntu@monitoring:/usr/local/nagios/etc$ sudo vim nagios.cfg
ubuntu@monitoring:/usr/local/nagios/etc$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.4.6
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2020-04-28
License: GPL
Website: https://www.nagios.org
Reading configuration data...
Read main config file okay...
Warning: Duplicate definition found for command 'check_http' (config file '/usr/local/nagios/etc/commands/http.cfg', starting on line 1)
Error: Could not add object property in file '/usr/local/nagios/etc/commands/http.cfg' on line 3.
Error: Invalid max_check_attempts value for host 'localhost'
Error: Could not register host (config file '/usr/local/nagios/etc/objects/localhost.cfg', starting on line 21)
Error processing object config files!
***> One or more problems was encountered while processing the config files...
Check your configuration file(s) to ensure that they contain valid
directives and data definitions. If you are upgrading from a previous
version of Nagios, you should be aware that some variables/definitions
may have been removed or modified in this version. Make sure to read
the HTML documentation regarding the config files, as well as the
'Whats New' section to find out what has changed.
設定が重複しているとのことなので、commands/http.cfgを削除する
code:remove
ubuntu@monitoring:/usr/local/nagios/etc$ sudo rm commands/http.cfg
hosts/lamp.cfgを修正する
https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/objectdefinitions.html#host
code:hosts/lamp.cfg
define host {
use generic-host ; Inherit default values from a template
host_name lamp ; The name we're giving to this host
alias LAMP Server ; A longer name associated with the host
address 192.168.64.52 ; IP address of the host
max_check_attempts 5
}
設定の確認
code:check
ubuntu@monitoring:/usr/local/nagios/etc$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.4.6
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2020-04-28
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 9 services.
Warning: Host 'lamp' has no default contacts or contactgroups defined!
Checked 2 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 2 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: 1
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
Restart the Nagios processをすると、Host lampが表示された
しかしStatus PENDINGとなっている。まだcheckされていないと思われる。
https://gyazo.com/3fa22115a9d0723e4d70fafc4bb4bbb6
Serviceの方ではlampのHTTPがチェックされていることがわかる
https://gyazo.com/34bf5dec97cbafcd1f5553bc4ae32815
ここまでやって気づいたが、Nagios Coreは必ずエージェントが必要というわけではないようだ。
他のサービスを監視しようと思ったら必要になるのかもしれない。