Linux筆記網路監控之nagios安裝與配置

來源:互聯網
上載者:User

標籤:nagios   linux   安裝與配置   

nagios簡介

Nagios是一款開源的電腦系統和網路監視工具,能有效監控Windows、Linux和Unix的主機狀態,交換器路由器等網路設定,印表機等。在系統或服務狀態異常時發出郵件或簡訊警示第一時間通知網站營運人員,在狀態恢複後發出正常的郵件或簡訊通知。
  Nagios原名為NetSaint,由Ethan Galstad開發並維護至今。NAGIOS是一個縮寫形式: "Nagios Ain‘t Gonna Insist On Sainthood" Sainthood 翻譯為聖徒,而"Agios"是"saint"的希臘表示方法。Nagios被開發在Linux下使用,但在Unix下也工作得非常好。

主要功能:

1.網路服務監控(SMTP、POP3、HTTP、NNTP、ICMP、SNMP、FTP、SSH)
2.主機資源監控(CPU load、disk usage、system logs),也包括Windows主機(使用NSClient++ plugin)可以指定自己編寫的Plugin通過網路收集資料來監控任何情況(溫度、警告……)可以通過配置Nagios遠程執行外掛程式遠程執行指令碼
3.遠程監控支援SSH或SSL加通道方式進行監控簡單的plugin設計允許使用者很容易的開發自己需要的檢查服務,支援很多開發語言(shell scripts、C++、Perl、ruby、Python、PHP、C#等)包含很多圖形化資料Plugins(Nagiosgraph、Nagiosgrapher、PNP4Nagios等)
4.可並行服務檢查能夠定義網路主機的層次,允許逐級檢查,就是從父主機開始向下檢查
當服務或主機出現問題時發出通告,可通過email, pager, sms 或任意使用者自訂的plugin進行通知
5、能夠自訂事件處理機制重新啟用出問題的服務或主機
? 自動日誌迴圈
? 支援冗餘監控
? 包括Web介面可以查看當前網路狀態,通知,問題曆史,記錄檔等
Nagios工作原理
  Nagios的功能是監控服務和主機,但自身並沒有該功能,所有的監控、檢測功能都是通過外掛程式來完成。
  Nagios周期性調用外掛程式檢測伺服器狀態,並維持一個隊列,所有外掛程式返回狀態資訊都進入隊列,Nagios每次從隊首開始讀取資訊,並把狀態通過web顯示。
Nagios提供了許多外掛程式,利用這些外掛程式可以監控服務狀態。安裝完成後,在nagios主目錄下/libexec放有nagios內建所有外掛程式,如check_disk檢查磁碟空間外掛程式,check_load檢查CPU負載等等。每一個外掛程式可運行./check_xxx –h 查看其使用方法和功能
工作原理圖如下:

Nagios安裝與配置一.nagios相關軟體安裝

環境準備
安裝基礎編譯環境軟體
yum install php gcc glibc glibc-common unzip gd gd-devel –y

解決perl編譯問題
echo "export LC_ALL=C" >> /etc/profile

解決時間同步問題

crontab -e
/10 * /bin/sh /usr/sbin/ntpdate.sh >/dev/null 2>&1
[[email protected] nagios-plugins-2.1.2]# cat /usr/sbin/ntpdate.sh
#!/bin/bash
#date update
ntpdate s1a.time.edu.cn

1.建立nagios使用者及相關的組

[[email protected] ~]#
useradd -m nagios
passwd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache

2.Nagios服務端安裝

下載nagios服務端軟體及外掛程式
[[email protected] ~]#
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.1.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz

3. 源碼編譯安裝nagios以及相關外掛程式

[[email protected] ~]#
tar zxf nagios-4.2.1.tar.gz
cd nagios-4.2.1
./configure --with-command-group=nagcmd

沒有提示任何錯誤出現以下畫面說明配置成功

接下來安裝提示進行編譯
[[email protected] ~]#
make all

編譯安裝並初始化設定檔
[[email protected] ~]#

make install
make install-init
make install-config
make install-webconf
make install-commandmode

安裝並編譯nagios外掛程式
[[email protected] ~]#
tar -zxvf nagios-plugins-2.1.2.tar.gz
cd nagios-plugins-2.1.2
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

4、配置郵件連絡人資訊

設定檔在目錄 /usr/local/nagios/etc 下,通過這些基礎配置資訊可以直接啟動 Nagios,現在唯一需要更改的是連絡人資訊,可以編輯 /usr/local/nagios/etc/objects/contacts.cfg ,將連絡人郵件地址換成你自己的。

若系統中需通過安裝sedmail來發送郵件
[[email protected] ~]#
yum install sendmail sendmail-cf

5.為nagios建立登入web介面的密碼

[[email protected] ~]#
htpasswd -c /usr/local/nagios/etc/htpasswd.users admin

6. 編譯安裝nagios外掛程式

[[email protected] ~]#

tar zxf nagios-plugins-2.1.2.tar.gz
cd nagios-plugins-2.1.2
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

7. 設定nagios服務開機自啟並啟動ngios

設定開機自啟
[[email protected] nagios]# systemctl enable nagios
[[email protected]]# chkconfig --list |grep nagios
nagios 0:off 1:off 2:on 3:on 4:on 5:on 6:off

啟動nagios服務

[[email protected]]# systemctl start nagios

檢查是否啟動成功
[[email protected]ver nagios]# systemctl start nagios
[[email protected] nagios]# ps -ef |grep nagios
nagios 24997 1 0 17:46 ? 00:00:02 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
nagios 25003 24997 0 17:46 ? 00:00:01 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
nagios 25036 24997 0 17:46 ? 00:00:00 [nagios] <defunct>
nagios 25037 24997 0 17:46 ? 00:00:00 [nagios] <defunct>
nagios 25038 24997 0 17:46 ? 00:00:00 [nagios] <defunct>
nagios 25039 24997 0 17:46 ? 00:00:00 [nagios] <defunct>
nagios 25094 24997 0 17:54 ? 00:00:00 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
nagios 25095 24997 0 17:54 ? 00:00:00 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
nagios 25096 24997 0 17:54 ? 00:00:00 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
nagios 25097 24997 0 17:54 ? 00:00:00 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
root 25521 25118 0 19:21 pts/0 00:00:00 grep --color=auto nagios

存在nagios進程說明已經啟動成功了

檢查nagios主設定檔文法是否正確br/>[[email protected]]#

我們只需關注以下兩項參數即可,Total Errors 為0就說明文法正確。
Total Warnings: 0
Total Errors: 0

接下來在瀏覽器輸入 http://伺服器ip地址/nagios 登陸nagios,使用者名稱和密碼為第五步用htpasswd設定的, 出現以下畫面說明您已經安裝成功了

二.驗證nagios服務端的監控功能,如實現監控伺服器對被監控伺服器磁碟負載情況的監控。

拓撲如下

環境準備

1.下載安裝nrpe

[[email protected]]#
wget https://jaist.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz
tar zxf nrpe-2.15.tar.gz
cd nrpe-2.15
./configure
make all
make install-plugin
make install-daemon

我在安裝nrpe遇到以下錯誤

安裝openssl-devel庫即可解決
[[email protected] nrpe-2.15]# yum install openssl-devel

到此為止,nagios-server服務端的實驗環境就準備好了

2.在nagios-client以同樣的方式安裝nigios-plugins外掛程式和nrpe即可,詳細安裝步驟參考上文。3. server-client端配置

1)加入可以監控當前client的nagios-server端的ip地址如下
allow_hosts=192.168.10.136
可用vi編輯nrpe的設定檔/usr/local/nagios/etc/nrpe.cfg修改大約在79行
我這裡用sed命令修改
[[email protected] ~]# sed -i ‘s/allowed_hosts=127.0.0.1/allowed_hosts=192.168.10.136/g‘ /usr/local/nagios/etc/nrpe.cfg
檢查是否修改成功

2)/usr/local/nagios/etc/nrpe.cfg檔案結尾添加要監控的外掛程式

畫圈部分為預設監控的外掛程式,如果需要監控其他內容,可添加並安裝相關的外掛程式,本實驗監控的是磁碟負載check_load,已包含在預設監控的內容裡面了

4) 啟動nrpe

[[email protected] ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

設定開機自啟
echo “/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d” >> /etc/rc.local

檢查服務是否啟動成功

[[email protected] ~]# netstat -lnt |grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0: LISTEN
tcp6 0 0 :::5666 :::
LISTEN

[[email protected] ~]# lsof -i :5666
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nrpe 46042 nagios 4u IPv4 55448 0t0 TCP :5666 (LISTEN)
nrpe 46042 nagios 5u IPv6 55449 0t0 TCP
:5666 (LISTEN)

nrpe連接埠和進程都存在說明nrpe啟動成功。

提示:重啟nrpe 可以先用killall殺掉nrpe進程再啟動即可

4. Nagios-server端配置。

1)建立host.cfg主機檔案添加被監控的主機
這裡使用lohosthost.cfg作為模板取前51行作為host.cfg的模板
[[email protected] ~]#
head -51 /usr/local/nagios/etc/objects/localhost.cfg > /usr/local/nagios/etc/objects/hosts.cfg

用vi編輯hosts.cfg改為以下內容

授權
[[email protected] ~]# chwon -R nagios.nagios /usr/local/nagios/etc/objects/hosts.cfg

2)建立services.cfg監控服務檔案添加要監控的服務
[[email protected] ~]#
touch /usr/local/nagios/etc/objects/services.cfg
chwon -R nagios.nagios /usr/local/nagios/etc/objects/hosts.cfg

編輯 /usr/local/nagios/etc/objects/services.cfg檔案輸入如下內容

[[email protected] objects]# vi /usr/local/nagios/etc/objects/services.cfg
define service {
use generic-service ;使用templates.cfg模板的監控參數
host_name nagios-client ;被監控服務端的主機名稱
service_description check load
check_command check_nrpe!check_load ;要監控的服務命令
}

注意:預設使用templates.cfg模板監控的參數,若不需要,可自行定義

3)在commands.cfg 中加入check_nrp外掛程式配置
編輯/usr/local/nagios/etc/objects/commands.cfg 在檔案結尾添加以下內容

define command{
command_name check_nrpe
command_line $USER1$check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

4) 檢查nagios主設定檔並重啟

[[email protected] ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Total Warnings: 0
Total Errors: 0

重啟

[[email protected] ~]# /etc/init.d/nagios reload
Reloading nagios configuration (via systemctl): [ OK ]

5) 用戶端測試是否有監控的主機及服務
開啟瀏覽器出現了以下畫面並沒有成功監控到主機以及相關的服務

細讀原來是許可權出了問題,我們之前配置登入名稱admin沒有足夠的許可權來訪問cgi

解決方案:
把cgi.cfg的nagiosadmin全部替換成admin(我們之前設定的登入名稱)

[[email protected] ~]# sed -i ‘s/nagiosadmin/admin/g‘ /usr/local/nagios/etc/cgi.cfg

檢查文法並重啟nagios
[[email protected] ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Total Warnings: 0
Total Errors: 0

重啟

[[email protected] ~]# /etc/init.d/nagios reload
Reloading nagios configuration (via systemctl): [ OK ]

開啟瀏覽器訪問
可以看到被監控的主機的相關資訊

但是查看監控的服務時出現了以下異常

4.排錯

1).查看nagios的錯誤記錄檔

[[email protected] ~]# tail -100 /usr/local/nagios/var/nagios.log
[1523721600] LOG ROTATION: DAILY
[1523721600] LOG VERSION: 2.0
[1523721600] CURRENT HOST STATE: nagios-client;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 1.16 ms
[1523721600] CURRENT SERVICE STATE: nagios-client;check load;CRITICAL;HARD;3;(Return code of 255 is out of bounds)
[1523721614] Warning: Return code of 255 for check of service ‘check load‘ on host ‘nagios-client‘ was out of bounds.
[1523721945] Caught SIGHUP, restarting...
日誌大概說不能用check_load不能返回結果,看一下是不是許可權問題

檢查用戶端nrpe設定檔是否允許伺服器位址進行訪問


發現是服務端的地址並沒有什麼問題

2).在服務用check_load指令碼手動檢查一下結果
[[email protected] ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.10.135 -c check_load
connect to address 192.168.10.135 port 5666: No route to host

提示沒有主機的路由,查看路由表
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.10.2 0.0.0.0 UG 0 0 0 eno16777728
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eno16777728
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eno16777728

這就奇怪了,明明是有預設路由的,互相ping也通

3).檢查防火牆和SELinux
[[email protected] ~]# systemctl status firewall.service
Unit firewall.service could not be found.

[[email protected] ~]# systemctl status setenfore.service
Unit setenfore.service could not be found.

[[email protected] ~]# systemctl status iptables

  • iptables.service - IPv4 firewall with iptables
    Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
    Active: active (exited) since Sun 2018-04-15 01:26:52 CST; 9s ago
    Process: 75135 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS)
    Process: 75653 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
    Main PID: 75653 (code=exited, status=0/SUCCESS)

Apr 15 01:26:52 nagios-client systemd[1]: Starting IPv4 firewall with iptables...
Apr 15 01:26:52 nagios-client iptables.init[75653]: iptables: Applying firewall rules: [ 確定 ]
Apr 15 01:26:52 nagios-client systemd[1]: Started IPv4 firewall with iptables.

最後發現是防火牆iptables沒關,會不會是防火牆的問題?先把它關了試試

[[email protected] ~]# systemctl stop iptables
[[email protected] ~]# systemctl status iptables

  • iptables.service - IPv4 firewall with iptables
    Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
    Active: inactive (dead) since Sun 2018-04-15 01:30:07 CST; 12s ago
    Process: 75764 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS)
    Process: 75653 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
    Main PID: 75653 (code=exited, status=0/SUCCESS)

Apr 15 01:26:52 nagios-client systemd[1]: Starting IPv4 firewall with iptables...
Apr 15 01:26:52 nagios-client iptables.init[75653]: iptables: Applying firewall rules: [ 確定 ]
Apr 15 01:26:52 nagios-client systemd[1]: Started IPv4 firewall with iptables.
Apr 15 01:30:06 nagios-client systemd[1]: Stopping IPv4 firewall with iptables...
Apr 15 01:30:06 nagios-client iptables.init[75764]: iptables: Setting chains to policy ACCEPT: filter [ 確定 ]
Apr 15 01:30:06 nagios-client iptables.init[75764]: iptables: Flushing firewall rules: [ 確定 ]
Apr 15 01:30:07 nagios-client iptables.init[75764]: iptables: Unloading modules: [ 確定 ]
Apr 15 01:30:07 nagios-client systemd[1]: Stopped IPv4 firewall with iptables.

4.在服務端再次用命令檢測一下
[[email protected] ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.10.135 -c check_load
OK - load average: 0.97, 0.91, 0.88|load1=0.970;15.000;30.000;0; load5=0.910;10.000;25.000;0; load15=0.880;5.000;20.000;0;

可以看到結果了,果然是防火牆的問題,配置容易,排錯難,遇到問題是好事,多點查閱相關的文檔,多角度去考慮問題,會讓你成長地更快!

我們開啟瀏覽器再次查看,Status是OK,這恭喜你配置完成了

其實,很多其他的服務也可以通過此種方式來監控,以上我列舉的只是其中一種服務而已,我就不一一舉例了。

學習小提示:學習需要總結,不然時間長了很快忘記。做筆記很重要,人腦的記憶是有限的,不可能記住全部的知識,這就需要我們用硬碟來幫我們記住具體的知識細節,我們只需記住概要即可,以後忘了再翻翻筆記,你會發現以前的知識又重新回到了腦海中。

Linux筆記網路監控之nagios安裝與配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.