陳俊勇:帶你搭建nagios監控web服務、非常簡單
Nagios監控web服務
首先需要一臺安裝有web服務的服務器、我這用juny-18這臺裝有Nginx的服務器來做實驗的被監控端,juny-10來做監控機、Nginx也是一個web服務,監聽在80端口
先測試連接
[root@juny-10 ~]# cd /usr/local/nagios/libexec/
[root@juny-10 libexec]# ./check_tcp -H 192.168.1.18 -p 80
TCP OK - 0.083 second response time on 192.168.1.18 port 80|time=0.082770s;;;0.000000;10.000000
[root@juny-10 libexec]#
看到此回覆說明連接沒有問題、接下來修改配置遠程主機
配置遠程主機
如過沒有這個遠程主機自己寫一個、如果有或者配置過就可以不用配置
[root@juny-10 nagios]# vim etc/objects/hosts.cfg
define host{
use linux-server
host_name juny-18
address 192.168.1.18
}
配置好遠程主機、接下來配置遠程服務
配置遠程服務
[root@juny-10 nagios]# vim etc/objects/service.cfg
define service{
use local-service
host_name juny-18
service_description Nginx
check_command check_nginx
}
保存退出
寫服務命令
在配置命令的最後面增加一條服務命令
[root@juny-10 nagios]# vim etc/objects/commands.cfg
define command{
command_name check_nginx
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p 80
}
保存並退出
測試配置文件
[root@juny-10 nagios]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 3.5.1
Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-30-2013
License: GPL
Website: //www.nagios.org
Reading configuration data...
Read main config file okay...
Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/timeperiods.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/templates.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/localhost.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/hosts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/service.cfg'...
Read object config files okay...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
測試配置沒問題
重啟nagios服務
[root@juny-10 nagios]# /etc/init.d/nagios restart
Restarting nagios (via systemctl): [ 確定 ]
[root@juny-10 nagios]#
網頁測試
查看網頁是不是增加Nginx服務的的監控
可以看到成功發添加Nginx服務,併成功的監控到
到此Nginx的監控配置結束
Nagios監控遠程主機的私有信息
什麼是私有信息:需要我們本機登錄操作的;例如監控:硬盤、線程、分區、swap分區等等信息
安裝插件
Nrpe簡介:
NRPE是監控軟件nagios的一個擴展,它被用於被監控的服務器上,向nagios監控平臺提供該服務器的一些本地的情況。例如,cpu負載、內存使用、硬盤使用等等。NRPE可以稱為nagios的for linux 客戶端。
客戶端安裝nrpe插件
安裝依賴包
[root@juny-10 ~]# yum install openssl-devel -y
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
軟件包 1:openssl-devel-1.0.2k-8.el7.x86_64 已安裝並且是最新版本
無須任何處理
安裝nrpe
解壓併到該目錄下
[root@juny-10 Nagios]# tar zxf nrpe-2.15.tar.gz -C /usr/local/src/ ; cd /usr/local/src/nrpe-2.15
默認配置
[root@juny-10 nrpe-2.15]# ./configure
*** Configuration summary for nrpe 2.15 09-06-2013 ***:
General Options:
-------------------------
NRPE port: 5666
NRPE user: nagios
NRPE group: nagios
Nagios user: nagios
Nagios group: nagios
Review the options above for accuracy. If they look okay,
type 'make all' to compile the NRPE daemon and client.
看到上面的信息就開始編譯並安裝
[root@juny-10 nrpe-2.15]# make && make install
[root@juny-10 nrpe-2.15]# make && make install
cd ./src/; make ; cd ..
make[1]: 進入目錄"/usr/local/src/nrpe-2.15/src"
gcc -g -O2 -I/usr/include/openssl -I/usr/include -DHAVE_CONFIG_H -I ../include -I ./../include -o nrpe ./nrpe.c ./utils.c ./acl.c -L/usr/lib64 -lssl -lcrypto -lnsl
gcc -g -O2 -I/usr/include/openssl -I/usr/include -DHAVE_CONFIG_H -I ../include -I ./../include -o check_nrpe ./check_nrpe.c ./utils.c -L/usr/lib64 -lssl -lcrypto -lnsl
make[1]: 離開目錄"/usr/local/src/nrpe-2.15/src"
*** Compile finished ***
If the NRPE daemon and client compiled without any errors, you
can continue with the installation or upgrade process.
Read the PDF documentation (NRPE.pdf) for information on the next
steps you should take to complete the installation or upgrade.
cd ./src/ && make install
make[1]: 進入目錄"/usr/local/src/nrpe-2.15/src"
make install-plugin
make[2]: 進入目錄"/usr/local/src/nrpe-2.15/src"
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nagios/libexec
make[2]: 離開目錄"/usr/local/src/nrpe-2.15/src"
make install-daemon
make[2]: 進入目錄"/usr/local/src/nrpe-2.15/src"
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin
/usr/bin/install -c -m 775 -o nagios -g nagios nrpe /usr/local/nagios/bin
make[2]: 離開目錄"/usr/local/src/nrpe-2.15/src"
make[1]: 離開目錄"/usr/local/src/nrpe-2.15/src"
[root@juny-10 nrpe-2.15]#
安裝完成以後在nagios的libesec文件夾下面就會有框出來的文件
[root@juny-10 nrpe-2.15]# ls /usr/local/nagios/libexec/
被監控端安裝nrpe插件
安裝依賴
[root@juny-18 ~]# yum install openssl-devel -y
已安裝:
openssl-devel.x86_64 1:1.0.2k-8.el7
作為依賴被安裝:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7
krb5-devel.x86_64 0:1.15.1-8.el7
libcom_err-devel.x86_64 0:1.42.9-10.el7
libkadm5.x86_64 0:1.15.1-8.el7
libselinux-devel.x86_64 0:2.5-11.el7
libsepol-devel.x86_64 0:2.5-6.el7
libverto-devel.x86_64 0:0.2.5-4.el7
pcre-devel.x86_64 0:8.32-17.el7
zlib-devel.x86_64 0:1.2.7-17.el7
作為依賴被升級:
krb5-libs.x86_64 0:1.15.1-8.el7
完畢!
安裝nagios
先上傳nagios
第一步創建不需要登錄nagios用戶,
[root@juny-18 ~]# useradd -s /sbin/nologin nagios
創建組
[root@juny-18 ~]# groupadd nagcmd
把用戶添加到組裡面
[root@juny-18 ~]# usermod -G nagcmd nagios
安裝xinetd工具
[root@juny-18 ~]# yum install xinetd -y
正在安裝:
xinetd x86_64 2:2.3.15-13.el7 base 128 k
事務概要
============================================================================
安裝 1 軟件包
總下載量:128 k
安裝大小:261 k
Downloading packages:
xinetd-2.3.15-13.el7.x86_64.rpm | 128 kB 00:16
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : 2:xinetd-2.3.15-13.el7.x86_64 1/1
驗證中 : 2:xinetd-2.3.15-13.el7.x86_64 1/1
已安裝:
xinetd.x86_64 2:2.3.15-13.el7
完畢!
開始安裝nagios
解壓並進如該目錄
[root@juny-18 ~]# tar zxf nagios-plugins-2.1.1.tar.gz -C /usr/local/src/ ; cd /usr/local/src/nagios-plugins-2.1.1
開始默認配置
[root@juny-18 nagios-plugins-2.1.1]# ./configure
config.status: creating po/Makefile
--with-apt-get-command:
--with-ping6-command: /usr/sbin/ping6 -n -U -w %d -c %d %s
--with-ping-command: /usr/bin/ping -n -U -w %d -c %d %s
--with-ipv6: yes
--with-mysql: no
--with-openssl: yes
--with-gnutls: no
--enable-extra-opts: yes
--with-perl: /usr/bin/perl
--enable-perl-modules: no
--with-cgiurl: /nagios/cgi-bin
--with-trusted-path: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
--enable-libtap: no
[root@juny-18 nagios-plugins-2.1.1]#
開始編譯並安裝
[root@juny-18 nagios-plugins-2.1.1]# make && make install
make[1]: 離開目錄"/usr/local/src/nagios-plugins-2.1.1/po"
make[1]: 進入目錄"/usr/local/src/nagios-plugins-2.1.1"
make[2]: 進入目錄"/usr/local/src/nagios-plugins-2.1.1"
make[2]: 對"install-exec-am"無需做任何事。
make[2]: 對"install-data-am"無需做任何事。
make[2]: 離開目錄"/usr/local/src/nagios-plugins-2.1.1"
make[1]: 離開目錄"/usr/local/src/nagios-plugins-2.1.1"
看到上面這些信息就表示編譯和安裝結束
安裝nrpe
解壓並進入到該目錄
[root@juny-18 ~]# tar zxf nrpe-2.15.tar.gz -C /usr/local/src/ ; cd /usr/local/src/nrpe-2.15
然後進行配置、編譯和編譯安裝、這裡一步執行完
[root@juny-18 nrpe-2.15]# ./configure && make && make install
make[1]: 進入目錄"/usr/local/src/nrpe-2.15/src"
make install-plugin
make[2]: 進入目錄"/usr/local/src/nrpe-2.15/src"
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nagios/libexec
make[2]: 離開目錄"/usr/local/src/nrpe-2.15/src"
make install-daemon
make[2]: 進入目錄"/usr/local/src/nrpe-2.15/src"
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin
/usr/bin/install -c -m 775 -o nagios -g nagios nrpe /usr/local/nagios/bin
make[2]: 離開目錄"/usr/local/src/nrpe-2.15/src"
make[1]: 離開目錄"/usr/local/src/nrpe-2.15/src"
安裝nrpe配置文件
[root@juny-18 nrpe-2.15]# make install-daemon-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /usr/local/nagios/etc
安裝完成
安裝和本地服務關聯的服務
[root@juny-18 nrpe-2.15]# make install-xinetd
/usr/bin/install -c -m 644 sample-config/nrpe.xinetd /etc/xinetd.d/nrpe
[root@juny-18 nrpe-2.15]#
編輯允許nagios服務通信
[root@juny-18 nrpe-2.15]# vim /etc/xinetd.d/nrpe
增加nagios的ip
在/etc/添加nrpe服務
[root@juny-18 nrpe-2.15]# vim /etc/services
啟動xinetd
[root@juny-18 nrpe-2.15]# systemctl start xinetd
添加到開機啟動
[root@juny-18 nrpe-2.15]# systemctl enable xinetd
查看是否在監聽
[root@juny-18 nrpe-2.15]# netstat -anput
配置監控命令
[root@juny-18 ~]# vim /usr/local/nagios/etc/nrpe.cfg
本身就監控了很多的私有服務、我框出來的是新添加的監控內容
重啟xinetd服務
[root@juny-18 ~]# systemctl restart xinetd
回到監控主機
測試是不是能連接到被監控主機上的私有信息
[root@juny-10 ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.18 -c check_sda1
DISK OK - free space: /boot 844 MB (83% inode=99%);| /boot=169MB;628;659;0;1014
[root@juny-10 ~]#
定義被監控的主機
[root@juny-10 ~]# vim /usr/local/nagios/etc/objects/hosts.cfg
define host{
use linux-server
host_name juny-18
address 192.168.1.18
}
編輯添加服務
[root@juny-10 ~]# vim /usr/local/nagios/etc/objects/service.cfg
define service{
use local-service
host_name juny-18
service_description sda2_Partition
check_command check_nrpe!check_sda2
}
define service{
use local-service
host_name juny-18
service_description Total Processes
check_command check_nrpe!check_total_procs
}
define service{
use local-service
host_name juny-18
service_description Current Load
check_command check_nrpe!check_load
}
#分別是監測分區、進程總數、負載
注意:這還可以寫很多的監控服務、我在做實驗的時候只寫三項
編輯命令
[root@juny-10 ~]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
重啟nagios
[root@juny-10 ~]# /etc/init.d/nagios restart
Restarting nagios (via systemctl): [ 確定 ]
[root@juny-10 ~]#
網頁測試
可以看到剛剛配置的都進入到監控裡面,等待被監控
過了一會所有的信息都被接收到了
下一節做郵件報警和window監控的實驗