Linux實戰教學筆記34:企業級監控Nagios實踐(上)

一,Nagios監控簡介

  • 生活中你們應該對監控已司空見慣了,例如:餐館門前的監控探頭,小區裏的視頻監控,城市道路告訴監控探頭等,這些監控的目的你們都很清楚,無須多說。那麼,企業工做中爲何要部署監控系統呢?
  • 咱們都知道軍隊裏,哨兵的角色很重要,咱們在殺敵前,基本都要先把敵人站崗的哨兵給解決了,這樣敵人就至關於眼睛瞎了,耳朵聾了,而後再進攻就能輕鬆搞定這些敵人。在互聯網企業大型網站架構裏,服務器,業務系統至關多,可達上萬甚至十萬級別,並且還很複雜,若是沒有監控系統這個「哨兵」,網絡業務出現什麼問題,咱們就很難知曉,所以,大型網站中監控系統的重要性就不言而喻了。

監控系統須要監控的數據有哪些呢?php

  • 系統本地資源:負載(uptime),CPU(top,sar),磁盤(df -hi),內存(free),I/O(iostat),Raid內磁盤故障,CPU溫度,passwd文件的變化,本地全部文件改動。
  • 網絡服務:端口,Web(URL),DB,ping包,進程,IDC帶寬網絡流量。
  • 其餘設備:路由器,交換機(端口,光衰,日誌),打印機,windows等。
  • 業務數據:用戶登陸失敗次數,用戶登錄網站次數,輸入驗證碼失敗的次數,某個API接口流量併發,網絡鏈接數,IP,PV數,電商網站訂單,支付交易的數量等。

如何獲取到這麼多的數據呢?html

Nagios監控軟件自己僅僅是一個監控平臺,理論上想監控的具體內容只要在服務器中執行命令行就能夠獲取,並歸入Nagios監控體系裏。前端

二,Nagios監控工具及原理介紹

2.1 Nagios介紹

  • Nagios是一款開源的網絡及服務的監控工具,其功能強大,靈活性強。能有效監控Windows,Linux和UNIX等系統的主機各類狀態信息,以及交換機,路由器等網絡設備和主機端口及URL服務等,它會根據不一樣業務故障級別發出告警信息(郵件,微信,短信,語音報警,飛信),當故障恢復時也會發出對應的恢復消息給管理員。
  • Nagios服務器端能夠在Linux系統和類UNIX系統上運行,但目前沒法在Windows上運行。Windows能夠做爲被監控的主機運行Nagios客戶端軟件。
  • Nagios監控軟件自己僅僅是一個監控平臺,咱們想監控的具體內容,理論上只要經過服務器命令就能夠獲取並歸入Nagios監控體系裏,因此,能夠說Nagios強大到了無所不能的地步。
  • Nagios官方網站地址爲http://www.nagios.org/.

2.2 Nagios的特色

Nagios能夠支持很是多的功能特性,這裏把一些常見的功能特性簡單介紹以下:node

  • 監控網絡服務(HTTP,TCP,PING,SMTP,POP3等)
  • 監控主機資源(CPU,負載,I/O情況,虛擬及正式內存及磁盤利用率等)
  • 簡單的插件設計模式使得用戶能夠很方便地定製符合本身服務的檢測方法
  • 並行服務檢查機制
  • 具有定義網絡分層結構的能力,用「parent」主機定義來表達網絡主機間的關係,這種關係可被用來發現和明晰主機宕機或不可達的狀態。
  • 當服務或主機問題產生與解決後將及時通報聯繫人(mail/im/sms/sound/語音)
  • 具有定義事件句柄功能,它能夠在主機或服務的事件發生時獲取更多問題定位
  • 自動的日誌回滾
  • 能夠支持並實現對主機的冗餘監控(支持分佈式監控)
  • 可選的Web界面用於查看當前的網絡狀態,通知和故障歷史,日誌文件等。

2.3 Nagios監控系統家族成員的構成

  • Nagios監控通常由一個主程序(Nagios),一個插件程序(Nagios-plugins)和一些可選的附加程序(NRPE,NSClient++,NSCA和NDOUtils)等組成。
  • Nagios自己只是一個監控的平臺而已,其具體的監控工做都是經過各種插件(例如:Nagios-plugins)來實現的,也能夠本身編寫插件。所以,Nagios主程序和Nagios-plugins插件都是Nagios服務器端必需要安裝的程序組件,不過,通常Nagios-plugins也要安裝於被監控端,用來獲取相應的數據。Nagios可選的附加組件描述以下:

(1)NRPE組件(重點)mysql

  • 存在的位置:工做於被監控端,操做系統爲Linux/UNIX系統
  • 做用:用於在被監控的遠程Linux/UNIX主機上執行腳本插件,獲取數據回傳給服務器端,以實現對這些主機資源和服務的監控。
  • 存在形式:守護進程(agent)模式,開啓的端口爲5666
  • 監控的資源:主要用於監控本地資源,包括負載(uptime),CPU(top,sar),磁盤(df -hi),內存(free),I/O(iostat),Raid內磁盤故障,CPU溫度,passwd文件的變化,以及本地全部文件的指紋識別監控,固然也能夠監控進程,端口,URL等。

下圖爲NRPE組件的運行原理圖:linux

QQ截圖20170811233501.png-40.3kB

  • 工做原理:一般由Nagios服務器端發起獲取數據請求,由check_nrpe插件攜帶要獲取的命令,傳給被監控端的nrpe守護進程(默認5666端口),nrpe進程讀取nrpe.cfg裏對應服務器端發送的命令信息,調用本地插件獲取數據,而後返回給Naios服務器端check_nrpe,進而傳給Nagios展現到Web界面中,嚴格講能夠稱之爲半被動工做模式,本文主要講解這個NRPE組件的功能,後文將稱其爲被動模式。

(2)NSClient++組件(僅瞭解)ios

NSClient++組件用於被監控端爲Windows系統的服務器。nginx

  • 做用:至關於Linux下的nrpe,監控安裝在Windows主機上的agent組件。
  • 監控的資源:主要監控Windows系統的本地資源,好比CPU,磁盤,內存,I/O等。
  • 工做形式:一般由服務器端發起獲取數據請求,由check_nt傳給被監控端的NSClient++進程,獲取數據後返回給服務器端。

下圖爲NSClient++運行原理圖:web

QQ截圖20170811234507.png-61.3kB

  • 工做原理:NSClient++的工做原理和NRPE基本相同,只不過適合於Windows被監控端的監控,且服務器端的插件爲check_nt,客戶端的進程爲NSClient++。一般由服務器端發起獲取數據請求,由check_nt傳給被監控端的NSClient++進程,獲取數據後返回給服務器端,本節內容不會涉及Windows被監控端,感興趣的同窗們自行百度研究解決。

(3)NDOUtils組件(不推薦用)sql

NDOUtils組件工做於Nagios服務器端

  • 做用:將Nagios的配置信息和各event產生的數據存入數據庫,以實現對這些數據的檢索和處理,對於中小企業,不推薦使用NDOUtils,直接使用文件記錄數據就很好。

下圖爲NDOUtils的運行原理簡圖:

QQ截圖20170811235253.png-32.8kB

(4)NSCA組件(僅瞭解)

  • 相對於nrpe,這個能夠說是純被動模式的監控組件,目前被應用的場景較少,這裏只是做爲知識點進行介紹,對於中小企業,並不推薦使用。
    NSCA須要同時安裝在Nagios的服務器端和客戶端(被監控端)
  • 做用:讓被監控的遠程Linux/UNIX主機主動將監控到的信息發送給Nagios服務器,能夠用在大規模分佈式監控集羣模式中,中小企業無需使用。

分佈式監控NSCA外部構件簡介:

爲完成從遠程主機主動提交強制檢測結果,因而就開發了NSCA外部構件。該外部構件包括兩部分,第一部分是客戶端程序(send_nsca),運行於遠程主機上,並負責將強制檢測結果發送到指定的Nagios服務器端,另外一部分是NSCA守護進程(nsca),它既能夠做爲守護進程獨立運行,也能夠註冊到inetd裏做爲一個inetd客戶程序來提供監聽。從客戶端收到服務檢測結果信息以後,守護進程將結果提交給在中心服務器的Nagios,方式是在外部命令文件裏插入一條PROCESS_SVC_CHECK_RESULT命令,以後跟上檢測結果。在Nagios服務器端下一次處理外部命令時將會找到這條由分佈式服務器送來的強制監測信息並處理它。

下圖爲NSCA分佈式監控運行原理簡圖:

QQ截圖20170812001201.png-35.6kB

2.4 Nagios監控系統完整圖解

下圖爲Nagios監控工具綜合系統簡單原理圖。該系統在中小企業用不到,也不必搞得這麼複雜,這裏會對將要講解的Nagios系統作了一個剪裁,只講解企業最多見的功能應用,對通常的企業已經足夠了。

QQ截圖20170812180938.png-121.7kB

運維思想:化繁爲簡是優秀的運維工程師必須掌握的重要技術思想。

下圖是接下來要詳細講解的Nagios監控工具實際原理簡圖。這張原理圖符合生產運維的原則:簡單,易用,高效。

QQ截圖20170812181212.png-105.3kB

三,Nagios服務器端安裝

3.1 Nagios安裝準備

(1)準備3臺服務器或VM虛擬機

下表爲Nagios服務器及客戶端服務器列表

管理IP地址 角色 備註
192.168.0.200 nagios-server Nagios服務器端
192.168.0.223 web01 被監控的客戶端服務器
192.168.0.224 web02 被監控的客戶端服務器

(2)設置yum公網安裝源

  • 默認狀況執行yum會從國外的站點下載,速度慢。所以要換成國內的提供yum源的站點,這樣安裝軟件時更快。yum是一個很是方便的RPM軟件包安裝命令,通常安裝基礎的軟件都會用到。命令語法爲:yum install軟件包名或關鍵字-y。
#阿里雲yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

(3)解決Perl軟件編譯問題

在安裝好Nagios監控服務後,還要安裝Perl插件程序,所以要提早設置相關環境變量,批量執行命令以下:

[root@Nagios ~]# echo 'export LC_ALL=C' >> /etc/profile
[root@Nagios ~]# tail -1 /etc/profile
export LC_ALL=C
[root@Nagios ~]# source /etc/profile
[root@Nagios ~]# echo $LC_ALL
C

(4)關閉Nagios server端防火牆及SELinux

在測試環境下爲了調試方便,最好關掉iptalbes防火牆及SELinux,若是是生產環境中,由於有外部IP,因此在調試完畢後須要開啓防火牆。通常容許服務經過的方法是整個局域網IP段都經過,SELinux是一個可關可開的軟件,企業能夠根據需求選擇,大部分企業仍是會選擇關閉SELinux,使用其餘保護措施,所以,這裏也關閉SELinux。

(5)解決系統時間同步問題

若是不解決服務器的時間同步問題,極可能會致使Nagios整個服務配置異常甚至失敗,這個是不少同窗初學時不太注意的地方,Nagios一度被稱爲「難夠死」,意思是太難配置了,不過只要同窗們認真聽,認真學,相信你會感受很輕鬆。

[root@Nagios ~]# which ntpdate
/usr/sbin/ntpdate
[root@Nagios ~]# /usr/sbin/ntpdate pool.ntp.org
12 Aug 11:29:43 ntpdate[1434]: step time server 51.174.131.248 offset -28704.196738 sec
#讓Nagios server的系統時間和當前的標準時間保持一致。虛擬機要能上網才行
[root@Nagios ~]# date +%F
2017-08-12
[root@Nagios ~]# echo '#time sync by Mr.chen at 2017-08-12' >> /var/spool/cron/root
[root@Nagios ~]# echo '*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1' >> /var/spool/cron/root
[root@Nagios ~]# crontab -l
#time sync by Mr.chen at 2017-08-12
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1

#<==執行命令將同步時間加入定時任務隊列。

(6)安裝Nagios服務器端所需軟件包

  • Nagios服務器端須要有Web界面展現監控效果,界面的展現主要使用PHP程序,所以,須要LAMP環境
  • 特別強調:有些網友總想安裝LNMP環境,這徹底自找麻煩,yum安裝的LAMP環境是配合Nagios服務器端展現界面的最佳環境。
[root@Nagios ~]# yum -y install gcc glibc glibc-common
#編譯軟件升級
[root@Nagios ~]# yum -y install gd gd-devel
#用於後面PHP出圖的包
[root@Nagios ~]# yum -y install mysql-server
#非必須,若是有監控數據庫,那麼須要先安裝MySQL,不然,MySQL的相關插件不會被安裝
yum -y install httpd php php-gd
#Apache,PHP環境
[root@Nagios ~]# rpm -qa mysql httpd php
httpd-2.2.15-60.el6.centos.4.x86_64
mysql-5.1.73-8.el6_8.x86_64
php-5.3.3-49.el6.x86_64
#檢查LAMP環境的版本

提示:
1)經過yum工具安裝上述全部軟件包,且這些環境通常不須要在Nagios客戶端安裝
2)上述軟件包裝好後的版本爲:Apache2.2.15,PHP5.3.3,MySQL5.1.73

(7)建立Nagios服務器端須要的用戶及組

批量執行命令以下:

[root@Nagios ~]# useradd nagios     #建立用戶(須要加目錄,不然,啓動Nagios會提醒沒家目錄)
[root@Nagios ~]# groupadd nagcmd    #添加用戶組
[root@Nagios ~]# usermod -a -G nagcmd nagios    #將nagios加入組
[root@Nagios ~]# usermod -a -G nagcmd apache    #將apache加入組
[root@Nagios ~]# id -n -G nagios    #查看用戶及用戶組
nagios nagcmd
[root@Nagios ~]# id -n -G apache    #查看用戶及用戶組
apache nagcmd

(8)上傳軟件包到指定目錄或經過URL下載

相關軟件包能夠去下載。
地址:http://sourceforge.net/projects/nagios/files/
http://www.nagios-plugins.org/download/nagios-plugins-1.4.16.tar.gz

爲了方便同窗們學習安裝Nagios,已經收集好了相關軟件包,內容以下:

[root@Nagios ~]# tree nagios/
nagios/
|-- Class-Accessor-0.31.tar.gz      #iostat插件須要
|-- Config-Tiny-2.12.tar.gz         #iostat插件須要
|-- Math-Calc-Units-1.07.tar.gz     #iostat插件須要
|-- Nagios-Plugin-0.34.tar.gz       #iostat插件須要
|-- Params-Validate-0.91.tar.gz     #iostat插件須要
|-- Regexp-Common-2010010201.tar.gz #iostat插件須要
|-- check_iostat                    #這就是上面提到iostat插件程序
|-- check_memory.pl                 #檢測內存的插件程序
|-- check_mysql                     #檢測MySQL插件程序
|-- libart_lgpl-2.3.17.tar.gz       #繪圖相關依賴庫
|-- nagios-3.5.1.tar.gz             #Nagios主程序
|-- nagios-plugins-1.4.16.tar.gz    #Nagios插件包
|-- nrpe-2.12.tar.gz                #客戶端的守護進程,agent
|-- pnp-0.4.14.tar.gz               #繪圖的Web界面軟件
|-- rrdtool-1.2.14.tar.gz           #實際繪圖軟件,PNP調用

0 directories, 15 files

如今啓動LAMP環境的HTTP服務:

[root@Nagios ~]# /etc/init.d/httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for Nagios
#可忽略或者看後文解決方案
httpd: Could not reliably determine the servers fully qualified domain name, using 127.0.0.1 for ServerName
#可忽略或者看後文解決方案
                                                           [  OK  ]
[root@Nagios ~]# netstat -antup | grep 80
tcp        0      0 :::80                       :::*                        LISTEN      1780/httpd

上面的結果代表Nagios服務器端的LAMP環境是正常的。下面針對HTTP啓動時的提示進行說明

提示一:「Starting httpd: httpd: apr_sockaddr_info_get() failed for Nagios」,表示這是hosts解析問題,在/etc/hosts中配好主機名和IP的解析就行了,配置結果以下:

[root@Nagios ~]# echo "`hostname -I` nagios-server" >> /etc/hosts
[root@Nagios ~]# tail -1 /etc/hosts
192.168.0.200  nagios-server

提示二:「httpd: Could not reliably determine the servers fully qualified domain name, using 127.0.0.1 for ServerName」,這表示httpd.conf中缺乏ServerName配置,能夠在/etc/httpd/conf/httpd.conf中加入ServerName 127.0.0.1:80

[root@Nagios ~]# echo "ServerName 127.0.0.1:80" >> /etc/httpd/conf/httpd.conf

修改好了之後,咱們再次重啓httpd服務,以下:

[root@Nagios ~]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

3.2 安裝Nagios服務器端

官方安裝文檔:http://nagios.sourceforge.net/docs/3_0/quickstart-fedora.html

[root@Nagios ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  nagios
[root@Nagios ~]# cd nagios/
[root@Nagios nagios]# ls -l nagios-3.5.1.tar.gz 
-rw-r--r--. 1 root root 1763584 Aug 12 12:22 nagios-3.5.1.tar.gz
[root@Nagios nagios]# tar xf nagios-3.5.1.tar.gz -C /usr/src/
[root@Nagios nagios]# cd /usr/src/nagios/
[root@Nagios nagios]# ./configure --with-command-group=nagcmd

#省略開頭,出現如下兩句表示配置正確
#Review the options above for accuracy.  If they look okay,
#type 'make all' to compile the main program and CGIs.

[root@Nagios nagios]# make all

#省略開頭,出現下面表示正確
#Enjoy.

[root@Nagios nagios]# 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/rc.d/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[1]: Leaving directory `/usr/src/nagios'

提示:
若是是編譯安裝httpd,能夠執行./configure --with-command-group=nagcmd --with-httpd-conf=/usr/local/apache/conf/extra 增長一個編譯參數,即指定編譯Nagios Web配置的生成路徑

下面根據make install 命令執行後的提示繼續操做

[root@Nagios nagios]# make install-init     #安裝初始化腳本到/etc/rc.d/init.d
[root@Nagios nagios]# make install-config   #生成Nagios模板配置文件到/usr/local/nagios/etc
[root@Nagios nagios]# make install-commandmode  #安裝配置目錄許可外部命令文件

(1)安裝Nagios Web配置文件及建立登陸用戶

接下來安裝Nagios Web配置文件(生成Nagios對應於Apache裏的配置文件)。

[root@Nagios nagios]# make install-webconf  #生成/etc/httpd/conf.d/nagios.conf配置文件

/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
*** Nagios/Apache conf file installed ***

建立Nagios Web監控界面後,登入時會須要用戶名及密碼,這裏分別爲yunjisuan和123123.

[root@Nagios nagios]# htpasswd -bc /usr/local/nagios/etc/htpasswd.users yunjisuan 123123
Adding password for user yunjisuan
[root@Nagios nagios]# cat /usr/local/nagios/etc/htpasswd.users 
yunjisuan:XahUhj.D9ajcc

提示:
爲何要把密碼生成到/usr/local/nagios/etc/htpasswd.users裏呢?不能改路徑嗎?
這是由於/etc/httpd/conf.d/nagios.conf配置文件已經指定了htpasswd.user路徑和文件名,若是想改能夠先修改nagios.conf配置,可是不必改,查看方式以下:

[root@Nagios nagios]# grep AuthUserFile /etc/httpd/conf.d/nagios.conf 
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   AuthUserFile /usr/local/nagios/etc/htpasswd.users

#從新加載Apache服務:
[root@Nagios nagios]# /etc/init.d/httpd reload
Reloading httpd:

(2)添加監控報警信息接收的Email地址

快速修改方法以下:

[root@Nagios nagios]# sed -i 's#nagios@localhost#215379068@qq.com#g' /usr/local/nagios/etc/objects/contacts.cfg
[root@Nagios nagios]# sed -n '35p' /usr/local/nagios/etc/objects/contacts.cfg
        email                           215379068@qq.com    #這裏是你的我的QQ不是,老師的QQ

常見的發送郵件方法有兩種,一種是啓動本機的郵件服務postfix。另一種是使用網上第三方郵件服務商提供的服務,例如:QQ郵件服務或者網易郵件服務,若是是簡單測試,推薦使用網易126或者163的smtp郵件發送服務地址配置。

第一種方法:監控服務器本地開啓郵件服務。此功能依賴本機的服務。

[root@Nagios nagios]# /etc/init.d/postfix start
Starting postfix:                                          [  OK  ]
[root@Nagios nagios]# netstat -antup | grep 25
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      6578/master         
tcp        0      0 ::1:25                      :::*                        LISTEN      6578/master
[root@Nagios nagios]# chkconfig postfix on
[root@Nagios nagios]# chkconfig --list postfix
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off

發送郵件測試:

[root@Nagios nagios]# echo "welcome to yunjisuan" | mail -s "test" 215379068@qq.com

QQ截圖20170814213112.png-47.5kB

提示:
因爲郵件服務沒有作MX記錄及反向解析,所以,所發郵件常常會收不到或者被看成垃圾郵件

第二種方法:使用第三方郵件服務商提供的郵箱。

#只須要修改/etc/mail.rc在最後增長以下兩行內容便可:

set from=215379068@qq.com

smtp=smtp.qq.comset smtp-auth-user=215379068 smtp-auth-password=123123 smtp-auto=login

這個第三方服務選擇QQ郵件服務或者網易郵件服務都可,這裏選擇了QQ郵件服務。
注意這裏的215379068@qq.com是做爲報警的發件人的,至關於使用215379068用戶和1223123密碼登錄QQ信箱,而後給人1發信,收件人就是在contact.cfg裏定義的。

(3)配置啓動Apache服務

下面啓動Apache服務並加入系統開機自啓動:

[root@Nagios nagios]# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[root@Nagios nagios]# chkconfig httpd on
[root@Nagios nagios]# chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@Nagios nagios]# netstat -antup | grep 80
tcp        0      0 :::80                       :::*                        LISTEN      6670/httpd

此時,打開客戶端計算機上的瀏覽器訪問http://192.168.0.200/nagios會出現用戶名和密碼提示窗口,以下圖所示:

QQ截圖20170814214325.png-44.6kB

輸入前面生成好的用戶名和密碼,也就是yunjisuan和123123
出現下圖所示的界面表示Nagios及Nagios的Web環境已經正常了

QQ截圖20170814214608.png-130.9kB

(4)安裝Nagios插件軟件包

Nagios插件軟件包就是一些實現獲取數據信息的命令或程序,經過這些命令或程序,Nagios能夠獲取到須要的數據,而後1進行報警和展現。具體安裝過程以下。

首先安裝基礎依賴包:

[root@Nagios nagios]# yum -y install perl-devel openssl-devel

而後安裝Nagios plugins插件包:

[root@Nagios nagios-plugins-1.4.16]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules --with-mysql
#若是一行放不下,能夠經過反斜線換行輸入。
[root@Nagios nagios-plugins-1.4.16]# make
[root@Nagios nagios-plugins-1.4.16]# make install
#此時檢查插件個數:
[root@Nagios nagios-plugins-1.4.16]# ls /usr/local/nagios/libexec/ | wc -l
57              #多一個少一個都是正常的,不要太在乎

(5)安裝nrpe軟件

可能有同窗會問,nrpe是客戶端須要安裝的軟件,爲何還要安裝在Nagios服務器端?這是由於:

  • Nagios服務器端須要check_nrpe插件作被動檢查,若是服務器端不裝nrpe軟件,就沒有check_nrpe這樣的檢查插件。
  • Nagios服務器端本地的資源也須要被監控,所以,Nagios服務器端也會被做爲客戶端。
  • nrpe軟件的安裝很是簡單,就很少費筆墨了。安裝nrpe軟件的命令集以下:
[root@Nagios nagios]# ls -l nrpe-2.12.tar.gz 
-rw-r--r--. 1 root root 405725 Aug 12 12:22 nrpe-2.12.tar.gz
[root@Nagios nagios]# tar xf nrpe-2.12.tar.gz -C /usr/src/
[root@Nagios nagios]# cd /usr/src/nrpe-2.12/
[root@Nagios nrpe-2.12]# ./configure
[root@Nagios nrpe-2.12]# make all
[root@Nagios nrpe-2.12]# make install-plugin
[root@Nagios nrpe-2.12]# make install-daemon
[root@Nagios nrpe-2.12]# make install-daemon-config
[root@Nagios ~]# ls /usr/local/nagios/libexec/check_nrpe 
/usr/local/nagios/libexec/check_nrpe
[root@Nagios ~]# ls /usr/local/nagios/libexec/ | wc -l
58
#提示:生成nrpe的配置文件爲/usr/local/nagios/etc/nrpe.cfg

到此爲止,Nagios服務器端的軟件安裝部分就配置完了。

(6)配置並啓動Nagios服務

#首先添加Nagios服務到開機自啓動
[root@Nagios ~]# chkconfig nagios on
[root@Nagios ~]# chkconfig --list nagios
nagios          0:off   1:off   2:on    3:on    4:on    5:on    6:off

#更好的設置自動開機的方法是:
[root@Nagios ~]# echo "/etc/init.d/nagios start" >> /etc/rc.local 
[root@Nagios ~]# tail -1 /etc/rc.local
/etc/init.d/nagios start

#而後驗證Nagios配置文件(檢查語法)
[root@Nagios ~]# /etc/init.d/nagios checkconfig
#此命令爲檢查語法的命令,可是隻能報告對錯沒法給出錯誤的信息
Running configuration check... OK.

#可使用命令行命令檢查報錯,並輸出信息
[root@Nagios ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
省略...
Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check
#<==Total Warning 警告和Total Errors錯誤都爲0,表示配置正常:
#<==Total Errors:若是不爲0必須解決。不然Nagios沒法啓動。
#<==Total Warnings:這個若是不爲0能夠啓動Nagios,能夠不理睬。

#能夠修改/etc/init.d/nagios實現上述命令行檢查語法的詳細輸出,以下:
[root@Nagios ~]# /etc/init.d/nagios checkconfig
Running configuration check... OK.

#<==此命令默認狀況只能判斷錯誤或正確,並不顯示錯誤的詳細信息,若是想顯示詳細錯誤信息,須要作下簡單的特殊處理,以下。
[root@Nagios ~]# grep 'checkconfig)' -n -A 2 /etc/init.d/nagios 
181:    checkconfig)
182-        printf "Running configuration check..."
183-        $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
#刪除定向到空的內容,可以讓報錯輸出詳細信息

刪除上述腳本中的>/dev/null 2>$1;便可檢查語法:

[root@Nagios ~]# /etc/init.d/nagios checkconfig
Running configuration check...
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: http://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'...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
    Checked 8 services.
Checking hosts...
    Checked 1 hosts.
Checking host groups...
    Checked 1 host groups.
Checking service groups...
    Checked 0 service groups.
Checking contacts...
    Checked 1 contacts.
Checking contact groups...
    Checked 1 contact groups.
Checking service escalations...
    Checked 0 service escalations.
Checking service dependencies...
    Checked 0 service dependencies.
Checking host escalations...
    Checked 0 host escalations.
Checking host dependencies...
    Checked 0 host dependencies.
Checking commands...
    Checked 24 commands.
Checking time periods...
    Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
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
 OK.

以後,啓動Nagios服務,命令以下:

[root@Nagios ~]# /etc/init.d/nagios start
Starting nagios: done.

檢查Nagios服務器端進程及端口

[root@Nagios ~]# ps -ef | grep nagios | grep -v grep
nagios    21663      1  0 07:48 ?        00:00:00 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
#<==grep -v grep 表示排除自身的這個帶grep命令的進程。

[root@Nagios ~]# netstat -antup | grep nagios   #沒發現Nagios服務器端有端口

最後瀏覽Nagios Web界面檢查。打開瀏覽器訪問:http://服務器端ip/nagios會出現以下圖所示提示:

QQ截圖20170815195218.png-40.8kB

若是訪問頁面打開報錯,請關閉服務器端的iptables及selinux
至此,Nagios服務器端安裝及配置就告一段落。

3.3 關於內網服務器的時間同步服務

在企業生產環境的內網環境中,通常是要有一臺服務器專門和Internet上作時間同步,而後內網中的其餘服務器都和這臺服務器進行時間同步。
那麼在咱們本次的試驗中,咱們用Nagios服務器端和internet時間同步,同時全部被監控的客戶端都和Nagios服務器端進行時間同步。

[root@Nagios ~]# yum -y install ntp ntpdate     #安裝ntp時間同步服務端,ntpdate時間同步客戶端
[root@Nagios ~]# crontab -l         #已經進行定時同步任務
#time sync by Mr.chen at 2017-08-12
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
[root@Nagios ~]# /etc/init.d/ntpd start                 #啓動ntpd服務端進程
Starting ntpd:                                             [  OK  ]

四,Nagios客戶端安裝

4.1 Nagios客戶端安裝說明

  • Nagios客戶端無需安裝LAMP環境,也無需安裝Nagios服務器端軟件包,即nagios-3.5.1.tar.gz
  • 與服務器端相比,Nagios客戶端有一些額外的軟件包須要安裝,以下:
  • Class-Accessor-0.31.tar.gz
  • Config-Tiny-2.12.tar.gz
  • Math-Calc-Units-1.0.7.tar.gz
  • Nagios-Plugin-0.34.tar.gz
  • Params-Validate-0.91.tar.gz
  • Regexp-Common-2010010201.tar.gz

4.2 Nagios客戶端安裝準備

(1)準備2臺服務器或VM虛擬機

管理IP地址 角色 備註
192.168.0.223 Web01 被監控的客戶端服務器
192.168.0.224 Web02 被監控的客戶端服務器

(2)環境準備與服務器端步驟相同

客戶端的yum源安裝,關閉Nagios Server端iptables防火牆及SElinux的方法,這裏再也不說明
如下操做再兩臺web上都進行

#nagios客戶端向Nagios服務器端進行時間同步
[root@web01 ~]# yum -y install ntpdate              #安裝時間同步客戶端
[root@web01 ~]# ntpdate 192.168.0.200               #和Nagios服務器端進行時間同步
15 Aug 08:22:26 ntpdate[5242]: step time server 192.168.0.200 offset 1517558.308966 sec
[root@web01 ~]# date +%F
2017-08-15
[root@web01 ~]# echo "*/5 * * * * /usr/sbin/ntpdate 192.168.0.200 >/dev/null 2>&1" >> /var/spool/cron/root                 #寫定時任務
[root@web01 ~]# cat /var/spool/cron/root
*/5 * * * * /usr/sbin/ntpdate 192.168.0.200 >/dev/null 2>&1

4.3 在Nagios客戶端安裝軟件

(1)安裝基礎系統軟件

命令以下:

[root@web01 nagios]# yum -y install gcc glibc glibc-common      #升級基礎工具包
[root@web01 nagios]# yum -y install mysql-server        #安裝這個目的爲了Nagios plugins,生成check_mysql插件

(2)上傳Nagios相關軟件

#去掉不用的,客戶端須要的軟件,以下:
[root@web01 ~]# tree nagios/
nagios/
├── check_iostat
├── check_memory.pl
├── check_mysql
├── Class-Accessor-0.31.tar.gz
├── Config-Tiny-2.12.tar.gz
├── libart_lgpl-2.3.17.tar.gz
├── Math-Calc-Units-1.07.tar.gz
├── Nagios-Plugin-0.34.tar.gz
├── nagios-plugins-1.4.16.tar.gz
├── nrpe-2.12.tar.gz
├── Params-Validate-0.91.tar.gz
└── Regexp-Common-2010010201.tar.gz

0 directories, 12 files

(3)添加Nagios用戶

添加方法以下:

[root@web01 ~]# useradd nagios -M -s /sbin/nologin
[root@web01 ~]# id nagios
uid=501(nagios) gid=501(nagios) groups=501(nagios)

(4)安裝nagios-plugins插件

操做過程以下:

[root@web01 nagios]# yum -y install perl-devel perl-CPAN openssl-devel
[root@web01 nagios-plugins-1.4.16]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules --with-mysql
[root@web01 nagios-plugins-1.4.16]# make
[root@web01 nagios-plugins-1.4.16]# make install

此時插件的個數以下:

[root@web01 ~]# ls /usr/local/nagios/libexec/ | wc -l
57

(5)安裝Nagios客戶端nrpe軟件

[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf nrpe-2.12.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/nrpe-2.12/
[root@web01 nrpe-2.12]# ./configure
[root@web01 nrpe-2.12]# make all
[root@web01 nrpe-2.12]# make install-plugin
[root@web01 nrpe-2.12]# make install-daemon
#生成nrpe.cfg
[root@web01 nrpe-2.12]# make install-daemon-config

(6)安裝其餘相關的插件

如下是check_iostat插件須要的依賴包,非必須安裝,若是不使用軟件包裏提供的check_iostat能夠不裝,雖然命令集不少,可是安裝簡單,所以能夠批量執行這些命令。

#安裝Params-Validate-0.91.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Params-Validate-0.91.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Params-Validate-0.91/
[root@web01 Params-Validate-0.91]# perl Makefile.PL
[root@web01 Params-Validate-0.91]# make
[root@web01 Params-Validate-0.91]# make install

#安裝Class-Accessor-0.31.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Class-Accessor-0.31.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Class-Accessor-0.31/
[root@web01 Class-Accessor-0.31]# perl Makefile.PL
[root@web01 Class-Accessor-0.31]# make && make install

#安裝Config-Tiny-2.12.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Config-Tiny-2.12.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Config-Tiny-2.12/
[root@web01 Config-Tiny-2.12]# perl Makefile.PL
[root@web01 Config-Tiny-2.12]# make && make install

#安裝Math-Calc-Units-1.07.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Math-Calc-Units-1.07.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Math-Calc-Units-1.07/
[root@web01 Math-Calc-Units-1.07]# perl Makefile.PL
[root@web01 Math-Calc-Units-1.07]# make && make install

#安裝Regexp-Common-2010010201.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Regexp-Common-2010010201.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Regexp-Common-2010010201/
[root@web01 Regexp-Common-2010010201]# perl Makefile.PL
[root@web01 Regexp-Common-2010010201]# make && make install

#安裝Nagios-Plugin-0.34.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Nagios-Plugin-0.34.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Nagios-Plugin-0.34/
[root@web01 Nagios-Plugin-0.34]# perl Makefile.PL
[root@web01 Nagios-Plugin-0.34]# make && make install

安裝sysstat系統性能分析工具,監控系統性能時程序或腳本會調用這些工具

[root@web02 ~]# yum -y install sysstat              #光盤安裝便可
[root@web02 ~]# rpm -q sysstat
sysstat-9.0.4-22.el6.x86_64

提示:
sysstat工具包中包含兩類工具,分別爲即時查看工具(iostat,mpstat,sar)和累計統計工具(sar)。能夠看出,這兩類工具中都有sar,也就是說,sar具備這兩種功能。可見,sar是sysstat中的核心工具。爲了實現sar的累計統計功能,系統必須週期性地記錄當時的信息,這是經過調用/usr/lib/sa/中的三個工具實現的:

  • sa1:收集並存儲天天的系統動態信息到一個二進制的文件中,用做sadc的前端程序。
  • sa2:收集天天的系統活躍信息寫入總結性的報告,用做sar的1前端程序。
  • sadc:系統動態數據收集工具,收集的數據被寫入一個二進制的文件中,用做sar工具的後端程序。

這裏針對監視物理組件的高級Linux命令小結以下:
內存:top,free,vmstat,mpstat,iostat,sar
CPU:top,vmstat,mpstat,iostat,sar
I/O:vmstat,mpstat,iostat,sar
進程:ipcs,ipcrm
負載:uptime

(7)配置監控內存,磁盤I/O腳本插件

批量部署命令以下:

[root@web01 ~]# yum -y install dos2unix             #光盤安裝便可
[root@web01 ~]# /bin/cp /root/nagios/check_memory.pl /usr/local/nagios/libexec/
[root@web01 ~]# /bin/cp /root/nagios/check_iostat /usr/local/nagios/libexec/
#<===以上兩個命令是將事先寫好的腳本(check_memory.pl和check_iostat)放到Nagios腳本目錄下。

[root@web01 ~]# chmod 755 /usr/local/nagios/libexec/check_memory.pl 
[root@web01 ~]# chmod 755 /usr/local/nagios/libexec/check_iostat
#以上兩條命令是受權讓腳本可執行
[root@web01 ~]# dos2unix /usr/local/nagios/libexec/check_memory.pl 
dos2unix: converting file /usr/local/nagios/libexec/check_memory.pl to UNIX format ...
[root@web01 ~]# dos2unix /usr/local/nagios/libexec/check_iostat 
dos2unix: converting file /usr/local/nagios/libexec/check_iostat to UNIX format ...
#<==使用dos2UNIX使之格式爲UNIX的腳本格式,不然執行可能出錯。

#注意:
#這個路徑是上傳的軟件包所在路徑/root/nagios

4.4 配置Nagios客戶端nrpe服務

可經過如下命令配置客戶端的nrpe.cfg

#加入Nagios Server服務端的IP,用以監控當前Client
[root@web01 ~]# cd /usr/local/nagios/etc
[root@web01 etc]# ls -l nrpe.cfg 
-rw-r--r--. 1 nagios nagios 7207 Aug 15 09:07 nrpe.cfg
[root@web01 etc]# sed -n '79p' nrpe.cfg 
allowed_hosts=127.0.0.1
[root@web01 etc]# sed -i '79 s#$#,192.168.0.200#g' nrpe.cfg
[root@web01 etc]# sed -n '79p' nrpe.cfg 
allowed_hosts=127.0.0.1,192.168.0.200

#提示:
#新加入的192.168.0.200是服務端IP不是客戶端的
#大於500臺機器作集羣或分佈式監控時,可使用多個Nagios Server,IP要用逗號隔開

註釋掉nrpe.cfg的199~203行內容

[root@web01 etc]# sed -n '199,203p' nrpe.cfg 
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200 
[root@web01 etc]# sed -i '199,203 s/^/#/' nrpe.cfg 
[root@web01 etc]# sed -n '199,203p' nrpe.cfg 
#command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
#command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
#command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
#command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

在配置文件nrpe.cfg下面添加要監控的內容:

#在文件最後添加如下5行內容
[root@web01 etc]# tail -5 /usr/local/nagios/etc/nrpe.cfg 
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10

上面依次爲對負載,內存,硬盤,虛擬內存,磁盤I/O進行監控,這些都是本地的服務(咱們通常經過nrpe去客戶端執行腳本插件獲取信息,這樣的模式稱爲被動監控,與nsca的客戶端主動提交結果不衝突),由Nagios服務器端經過nrpe插件定時在client的nrpe服務上獲取信息。
以後,啓動Nagios client nrpe守護進程

[root@web01 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@web01 etc]# netstat -antup | grep nrpe
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      21041/nrpe

此時,能夠將nrpe加入開機自啓動了,命令以下:

[root@web01 etc]# echo "#nagios nrpe process cmd by Mr.chen 2017-8-15" >> /etc/rc.local
[root@web01 etc]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local 
[root@web01 etc]# tail -2 /etc/rc.local
#nagios nrpe process cmd by Mr.chen 2017-8-15
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

重要提醒:
客戶端的nrpe.cfg配置文件,最好保留一份到計算機上,這樣之後在其餘機器上裝nrpe時,直接上傳便可,就免得費勁修改了。

五,Nagios服務器端監控

5.1 Nagios服務器端監控基礎介紹

(1)Nagios服務器端目錄結構

Nagios服務器端安裝後的目錄結構以下:

[root@Nagios ~]# ls -l /usr/local/nagios/
total 32
drwxrwxr-x.  2 nagios nagios 4096 Aug 14 10:32 bin
drwxrwxr-x.  3 nagios nagios 4096 Aug 14 10:33 etc
drwxr-xr-x.  2 root   root   4096 Aug 14 10:11 include
drwxrwxr-x.  2 nagios nagios 4096 Aug 14 10:32 libexec
drwxr-xr-x.  5 root   root   4096 Aug 14 10:11 perl
drwxrwxr-x.  2 nagios nagios 4096 Aug 14 07:36 sbin
drwxrwxr-x. 11 nagios nagios 4096 Aug 14 10:11 share
drwxrwxr-x.  5 nagios nagios 4096 Aug 15 11:11 var

具體含義請參考如下圖片:

QQ截圖20170815231149.png-194kB

QQ截圖20170815231207.png-130.7kB

QQ截圖20170815231221.png-64.6kB

全部客戶端本地服務的監控都是經過執行libexec目錄下的插件來實現的,固然,若是開啓了snmp,Nagios服務器端也能夠主動抓取。

(2)Nagios服務器端核心配置文件

Nagios主配置文件爲nagios.cfg,默認在/usr/local/nagios/etc目錄下,/usr/local/nagios/etc目錄下有個objects(相似Apache的extra)目錄,裏面放的是組配置文件nagios.cfg包含的其餘Nagios配置文件。查看命令以下:

[root@Nagios ~]# tree /usr/local/nagios/etc/
/usr/local/nagios/etc/
|-- cgi.cfg
|-- htpasswd.users
|-- nagios.cfg
|-- nrpe.cfg
|-- objects
|   |-- commands.cfg
|   |-- contacts.cfg
|   |-- localhost.cfg
|   |-- printer.cfg
|   |-- switch.cfg
|   |-- templates.cfg
|   |-- timeperiods.cfg
|   `-- windows.cfg
`-- resource.cfg

1 directory, 13 files
[root@Nagios ~]# tail -10 /usr/local/nagios/etc/resource.cfg 
# Sets $USER1$ to be the path to the plugins
$USER1$=/usr/local/nagios/libexec

# Sets $USER2$ to be the path to event handlers
#$USER2$=/usr/local/nagios/libexec/eventhandlers

# Store some usernames and passwords (hidden from the CGIs)
#$USER3$=someuser
#$USER4$=somepassword
[root@Nagios ~]# tree /usr/local/nagios/etc/objects/
/usr/local/nagios/etc/objects/
|-- commands.cfg
|-- contacts.cfg
|-- localhost.cfg
|-- printer.cfg
|-- switch.cfg
|-- templates.cfg
|-- timeperiods.cfg
`-- windows.cfg

0 directories, 8 files

在nagios.cfg中既能夠指定單獨包含一個cfg文件,也能夠指定包含一個目錄,即1該目錄下全部的cfg文件都會包含進來。
爲了讓目錄結構看起來更清晰,以及批量部署服務的須要,咱們把主配置文件包含的配置文件修改成下表的形式。

QQ截圖20170815232321.png-107.4kB

(3)配置主配置文件nagios.cfg

#首先,在nagios.cfg文件中找到cfg_file部分,進行以下設置:
[root@Nagios ~]# sed -n '34,36p' /usr/local/nagios/etc/nagios.cfg
#增長以下主機和服務的配置文件
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_dir=/usr/local/nagios/etc/objects/services

#這是爲備用增長的一個service目錄,初學者能夠忽略此行,使用目錄的優勢不少,在目錄下的文件只要符合*.cfg就能夠被Nagios加載。使用腳本批量部署時可很是方便地隨機命名配置文件
#而後在配置文件裏註釋掉以下一行
[root@Nagios ~]# sed -n '37,38p' /usr/local/nagios/etc/nagios.cfg 
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg           #註釋本行

#<==localhost.cfg這個配置爲監控Nagios服務器端本地服務的配置文件,註釋掉它,而後統一監控

操做完畢,保存nagios.cfg。接着,根據已有數據生成hosts.cfg主機文件,命令集以下:

[root@Nagios ~]# cd /usr/local/nagios/etc/objects/
[root@Nagios objects]# head -51 localhost.cfg > hosts.cfg
[root@Nagios objects]# chown nagios.nagios /usr/local/nagios/etc/objects/hosts.cfg

而後生成新的空的services.cfg服務文件,操做過程以下:

[root@Nagios objects]# touch services.cfg
[root@Nagios objects]# chown -R nagios.nagios services.cfg

**最後,生成服務的配置文件目錄,全部放到此目錄下的配置(*.cfg)都會自動被包含到主配置文件中生效。操做過程以下:**

[root@Nagios objects]# mkdir services
[root@Nagios objects]# chown -R nagios.nagios services
[root@Nagios objects]# ls -lrt
total 56
-rw-rw-r--. 1 nagios nagios 10812 Aug 14 07:46 templates.cfg
-rw-rw-r--. 1 nagios nagios  7716 Aug 14 07:46 commands.cfg
-rw-rw-r--. 1 nagios nagios  3208 Aug 14 07:46 timeperiods.cfg
-rw-rw-r--. 1 nagios nagios  5403 Aug 14 07:46 localhost.cfg
-rw-rw-r--. 1 nagios nagios  4019 Aug 14 07:46 windows.cfg
-rw-rw-r--. 1 nagios nagios  3124 Aug 14 07:46 printer.cfg
-rw-rw-r--. 1 nagios nagios  3293 Aug 14 07:46 switch.cfg
-rw-rw-r--. 1 nagios nagios  2166 Aug 14 09:15 contacts.cfg
-rw-r--r--. 1 nagios nagios  1870 Aug 15 11:49 hosts.cfg
-rw-r--r--. 1 nagios nagios     0 Aug 15 13:00 services.cfg
drwxr-xr-x. 2 nagios nagios  4096 Aug 15 13:02 services

(4)Nagios監控模式定義及監控模式選擇

根據監控的行爲,將Nagios的監控分爲主動監控和被動監控(即nrpe半被動和nsca全被動),nsca全被動暫不詳述,下面先來看看什麼是主動監控和半被動監控。

  • 主動監控:把像URL監控同樣由Nagios服務器端發出請求的主動探測監控方式,定義爲主動監控方式,也就是說不須要在客戶端安裝任何插件。固然,主動監控模式也能夠配置成被動模式。
  • (半)被動監控:由Nagios服務器端經過nrpe插件定時去鏈接client的nrpe服務獲取信息,併發回到Nagios服務器端的監控稱之爲半被動監控,這類監控一般是針對本地資源的,好比負載,內存,硬盤,虛擬內存,磁盤I/O,溫度,風扇轉速等,而非系統對外提供的服務,只要安裝了相似nrpe的插件方式的監控,都認爲是半被動監控。

如何選擇主動監控模式和(半)被動監控模式?

1)對於本地的資源性能,通常用被動監控模式(NRPE)。例如,對負載,內存,硬盤,虛擬內存,磁盤I/O,溫度,風扇等的監控(咱們也能夠經過snmp實現監控部分系統資源)。
2)對於Web服務,數據庫服務這種能對外提供服務的,通常用主動模式,例如:監控httpd,sshd,mysqld,rsyncd等服務。

主動模式和被動模式是相對的,而且是能夠互相轉換的,即主動模式的服務,能夠改爲被動模式的,被動模式的服務有時也能夠改成主動模式的。

提示:
咱們在後面把nrpe的監控統稱爲被動監控模式

5.2 配置Nagios服務器端監控項

(1)定義要監控的Nagios客戶端主機

hosts.cfg通常用來存放Nagios要監控的主機相關配置,下面是hosts.cfg中的主機定義部分的配置參數詳解。

#精簡hosts.cfg配置模板
[root@Nagios objects]# cp hosts.cfg{,.bak}
[root@Nagios objects]# egrep -v "^$|#" hosts.cfg.bak > hosts.cfg 

#將hosts.cfg裏的內容修改爲如下狀態。
[root@Nagios objects]# cat hosts.cfg
# Define a host for the local machine
define host{                #<==define host爲關鍵字,意思是定義主機,主機內容用一對大括號括起來
        use                     linux-server     #定義主機使用的模板,具體參見templates.cfg       
        host_name               web01            #直接定義主機名稱,根據服務功能可隨意定義
        alias                   web01            #直接定義主機別名,同上
        address                 192.168.0.223    #直接定義被監控服務器的IP
        }
define host{
        use                     linux-server            
        host_name               web02
        alias                   web02
        address                 192.168.0.224
        }

define hostgroup{
        hostgroup_name  linux-servers 
        alias           Linux Servers 
        members         web01,web02             #把前面定義的每個Nagios客戶端主機名在這裏用逗號隔開列出來
        }

(2)配置services.cfg,定義要監控的資源服務

services.cfg文件是配置監控服務的,是Nagios最重要的配置文件之一,若是服務器數量比較少(50臺之內),則須要監控的大部分服務配置均可以在這裏面添加。這個配置文件默認是不存在的,須要人爲定義。
先來看看services.cfg配置文件的service配置參數,詳細說明以下:

[root@Nagios objects]# cat services.cfg 

define service {                                #磁盤分區監控(被動監控)
    use generic-service
    host_name web01
    service_description Disk Partition
    check_command check_nrpe!check_disk
}
define service {                                #swap監控(被動監控)
    use generic-service
    host_name web01
    service_description Swap Useage
    check_command check_nrpe!check_swap
}
define service {                                #內存監控(被動監控)
    use generic-service
    host_name web01
    service_description MEM Useage
    check_command check_nrpe!check_mem
}
define service {                                #系統負載監控(被動監控)
    use generic-service
    host_name web01
    service_description Current Load
    check_command check_nrpe!check_load
}
define service {                                #磁盤I/O監控(被動監控)
    use generic-service
    host_name web01
    service_description Disk lostat
    check_command check_nrpe!check_iostat!5!11
}
define service {                                #ping監控(主動監控)
    use generic-service
    host_name web01
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
}

提示:
(1)上述配置中的check_nrpe是服務器端的插件(是commands.cfg裏預先定義的命令名),負責和客戶端的nrpe進程交流並執行check_nrpe歎號後的插件,因此,check_nrpe ! check_load配置中的check_load是客戶端的插件名,是在與客戶端的nrpe進程對應的配置nrpe.cfg裏定義的命令名。
(2)以上services.cfg中添加了對磁盤分區,Load,Mem,Swap,磁盤I/O,ping的監控。

Nagios軟件默認沒有提供客戶端的內存和I/O插件,但本文在配置時已經複製進去了,所以,只需在commands.cfg裏配置便可,詳細請同窗們看後面的commands.cfg說明

(3)調試hosts.cfg和service.cfg的全部配置

[root@Nagios objects]# /etc/init.d/nagios checkconfig
Running configuration check...
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: http://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/hosts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/services.cfg'...
Processing object config directory '/usr/local/nagios/etc/objects/services'...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
Error: Service check command 'check_nrpe ' specified in service 'Current Load' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk Partition' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk lostat' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'MEM Useage' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'PING' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Swap Useage' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Current Load' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk Partition' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk lostat' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'MEM Useage' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'PING' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Swap Useage' for host 'web02' not defined anywhere!
    Checked 12 services.
Checking hosts...
    Checked 2 hosts.
Checking host groups...
    Checked 1 host groups.
Checking service groups...
    Checked 0 service groups.
Checking contacts...
    Checked 1 contacts.
Checking contact groups...
    Checked 1 contact groups.
Checking service escalations...
    Checked 0 service escalations.
Checking service dependencies...
    Checked 0 service dependencies.
Checking host escalations...
    Checked 0 host escalations.
Checking host dependencies...
    Checked 0 host dependencies.
Checking commands...
    Checked 24 commands.
Checking time periods...
    Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   12

***> One or more problems was encountered while running the pre-flight check...

     Check your configuration file(s) to ensure that they contain valid
     directives and data defintions.  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.

 CONFIG ERROR!  Check your Nagios configuration.

根據錯誤提示,咱們能夠知道,是check_nrpe插件沒有定義致使的。來看一下解決方法。
首先,須要在commands.cfg中加入check_nrpe的插件配置.

[root@Nagios objects]# tail -8 commands.cfg 
#'check_nrpe'command definition

define command{

    command_name check_nrpe
    command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

此時從新執行檢查語法的命令:

[root@Nagios objects]# /etc/init.d/nagios checkconfig
#中間過程略
Checking misc settings...

Total Warnings: 0
Total Errors:   0

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

此時,警告和錯誤都爲0,則表示已經OK了,通常來說警告能夠忽略,錯誤必需要解決掉,不然,沒法繼續,調試完成後啓動Nagios服務。

[root@Nagios objects]# /etc/init.d/nagios start
Starting nagios: done.

若是啓動成功的話,那麼此時就能夠登錄192.168.0.200/nagios。查看本地各系統狀態的監控成果了。可是若是出現以下錯誤提示。

QQ截圖20170817002642.png-79.6kB

這個錯誤表示登錄Web端的用戶yunjisuan沒有被許可查看這些服務資源,可按照以下方法解決上面問題。

[root@Nagios objects]# cd /usr/local/nagios/etc/
[root@Nagios etc]# egrep -n "^authorized_for" cgi.cfg 
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin

上述authorized開頭的行對應的都是不一樣權限,能夠看出,在結尾的許可處用過戶都是nagiosadmin。此時,把在前面創建的Nagios Web登錄用戶yunjisuan加到每個許可項的後面,注意二者用逗號隔開。

[root@Nagios etc]# sed -i 's#nagiosadmin#yunjisuan#g' cgi.cfg
[root@Nagios etc]# egrep -n "^authorized_for" cgi.cfg 
119:authorized_for_system_information=yunjisuan
131:authorized_for_configuration_information=yunjisuan
144:authorized_for_system_commands=yunjisuan
157:authorized_for_all_services=yunjisuan
158:authorized_for_all_hosts=yunjisuan
171:authorized_for_all_service_commands=yunjisuan
172:authorized_for_all_host_commands=yunjisuan

提示:
1)理想狀況最好換掉默認管理員用戶nagiosadmin,替換成本身的yunjisuan等,而後根據需求加適合本身的權限,好比,給別人看只給瀏覽權限就能夠了。具體見cgi.cfg
2)遇到調試問題注意查看/usr/local/nagios/var/nagios.log,這點很重要。高手都這麼作

如今,從新加載配置使得修改生效

[root@Nagios etc]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done

正常監控後的效果圖,以下:

QQ截圖20170817004654.png-61.4kB

提示:UP表明服務器正常

服務器監控效果圖,剛配置完的主機監控圖以下:

QQ截圖20170817004255.png-41.1kB

(4)被動模式下基於Nagios監控原理排錯的案例

還記得以前的nrpe原理圖嗎?來回顧一下,見下圖:

QQ截圖20170817184148.png-52.1kB

Nagios被動模式的監控原理離不開check_nrpe這個插件,也就是說實際上是利用下面這個命令工做的:

/usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_mem

下面看兩個模擬Nagios配置錯誤的案例

第一個案例模擬:取消Web節點被監控端的check_memory.pl腳本的執行權限,摸你前面的錯誤。

#在Web端執行操做
[root@web01 ~]# cd /usr/local/nagios/libexec/
[root@web01 libexec]# chmod a-x check_memory.pl 
[root@web01 libexec]# ll check_memory.pl 
-rw-r--r--. 1 root root 4670 Aug 15 10:26 check_memory.pl

稍等一下或重啓Nagios後,此時頁面內存服務監控行出現錯誤提示。

QQ截圖20170817190656.png-26.8kB

錯誤提示爲:NRPE:Unable to read output

第二個案例模擬:把Web被監控端的nrpe.cfg中的命令名寫錯,以下:

[root@web01 libexec]# tail -3 /usr/local/nagios/etc/nrpe.cfg 
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
[root@web01 libexec]# vim /usr/local/nagios/etc/nrpe.cfg
[root@web01 libexec]# tail -3 /usr/local/nagios/etc/nrpe.cfg
command[check_disk1]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10

#提示:
#把command[check_disk]寫成command[check_disk1]

重啓Web被監控端nrpe服務後,此時頁面磁盤服務監控行出現以下錯誤提示:

[root@web01 libexec]# pkill nrpe
[root@web01 libexec]# netstat -antup | grep nrpe
[root@web01 libexec]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@web01 libexec]# netstat -antup | grep nrpe
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      22084/nrpe

QQ截圖20170817195056.png-22kB

錯誤提示爲:NRPE:Command ‘check_disk’ not defined

下面就針對上面兩個案例進行故障排查

案例1:NRPE:Unable to read output 排查

1)在Nagios服務器端執行以下命令:

[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_mem
NRPE: Unable to read output

2)在客戶端本地執行命令腳本檢查(就是command[check_mem]=後面對應的腳本)

[root@web01 libexec]# /usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
#這個命令最好是複製nrpe.cfg裏的配置,不要手敲,防止出錯
-bash: /usr/local/nagios/libexec/check_memory.pl: Permission denied

#提示拒絕,因此,緣由就找到了。由於check_memory.pl腳本無執行權限致使的NRPE:Unalbe to read output錯誤。

[root@web01 libexec]# chmod a+x /usr/local/nagios/libexec/check_memory.pl 
[root@web01 libexec]# /usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
CHECK_MEMORY OK - 845M free | free=886231040b;102851788.8:;30855536.64:

3)此時在Nagios服務器端執行以下命令:

[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_mem
CHECK_MEMORY OK - 844M free | free=885813248b;102851788.8:;30855536.64:
#提示OK了,那麼頁面應該也正常了。

QQ截圖20170817201919.png-26.1kB

案例2:NRPE:Command ‘check_disk’ not defined排查

該案例涉及的是磁盤監控故障,排查思路以下:

#在Nagios服務器端執行以下命令:
[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_disk
NRPE: Command 'check_disk' not defined

若是同窗們理解了Nagios被動監控的原理,很容易判斷上面問題的故障在於nrpe.cfg中check_disk相關命令寫錯了,或者沒配。
根據Nagios被動模式監控原理排查問題的思路以下。

1)在服務器端執行以下命令看是否返回數據

[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_disk
NRPE: Command 'check_disk' not defined

2)若是前面沒法正確返回數據,可在客戶端192.168.0.223本地執行以下命令看是否返回數據。

[root@web01 libexec]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_disk
NRPE: Command 'check_disk' not defined

3)若是前面仍是沒法正確返回數據,則執行nrpe.cfg裏下面配置的內容中等號後面的命令。

QQ截圖20170817202536.png-12.8kB

即執行以下操做:

[root@web01 libexec]# /usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
DISK OK - free space: / 15219 MB (90% inode=97%);| /=1590MB;15052;16469;0;17709

由此能夠看出是命令的定義名稱出現了錯誤,改回便可。
若是以上三步都OK,那麼就找服務器端緣由,通常是配置文件以及命令插件問題。

QQ截圖20170817204811.png-13.8kB

(5)添加http服務的URL地址及端口監控

如今增長從Nagios服務器端發起的監控,如URL地址,端口監控等。此類服務通常都開啓了對外提供服務的業務。這樣的業務,通常採用主動監控的方式,固然了,也能夠寫腳本經過被動的方式來監控,但通常不這麼作。
URL監控的實質是經過命令行理解HTTP的監控原理,以下:

[root@Nagios ~]# /usr/local/nagios/libexec/check_http -H 192.168.0.223
HTTP OK: HTTP/1.1 200 OK - 249 bytes in 0.002 second response time |time=0.002228s;;;0.000000 size=249B;;;0

下面是對域名URL地址http://www.yunjisuan.com進行監控的配置,將要監控的服務配置到services.cfg中便可。

#在Nagios服務端操做,在services.cfg中添加以下內容
[root@Nagios ~]# cat /usr/local/nagios/etc/objects/services.cfg 

define service {
    use generic-service
    host_name web01,web02
    service_description Disk Partition
    check_command check_nrpe!check_disk
}
define service {
    use generic-service
    host_name web01,web02
    service_description Swap Useage
    check_command check_nrpe!check_swap
}
define service {
    use generic-service
    host_name web01,web02
    service_description MEM Useage
    check_command check_nrpe!check_mem
}
define service {
    use generic-service
    host_name web01,web02
    service_description Current Load
    check_command check_nrpe!check_load
}
define service {
    use generic-service
    host_name web01,web02
    service_description Disk lostat
    check_command check_nrpe!check_iostat!5!11
}
define service {
    use generic-service
    host_name web01,web02
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
}

#url examples http://www.yunjisuan.com      從這裏開始是添加的內容

define service {                #用自定義命令對www.yunjisuan.com監控    

        use generic-service
        host_name web01
        service_description www_url
    check_command check_weburl! -H www.yunjisuan.com    #check_weburl是自定義命令,在command.cfg中定義。
    
}

define service {                #利用系統自帶插件對Web進行監控

    use generic-service
    host_name web01
    service_description www_url
    check_command check_http

}

define service {                #用自定義命令對複雜URL進行監控

    use generic-service
        host_name web01
        service_description www_static_url
    check_command check_weburl! -H www.yunjisuan.com -u /static/test.html   
#-H:主機IP或域名
#-u:URL的URI的部分
}


define service {                #對特殊的帶傳參的URL地址進行監控時,方式以下:

        use generic-service
        host_name web01
        service_description www_yunjisuan_url
    check_command check_weburl! -H www.yunjisuan.com -u "/article/index.phpm=article&a=list&id=670"

}

(6)配置好URL後檢查Nagios語法

#若在配置好URL後,就執行以下命令檢查Nagios語法:
[root@Nagios ~]# /etc/init.d/nagios checkconfig

#會發現報錯了

Checking services...
Error: Service check command 'check_weburl' specified in service 'www_static_url' for host 'web01' not defined anywhere!
Error: Service check command 'check_weburl' specified in service 'www_yunjisuan_url' for host 'web01' not defined anywhere!
...省略若干...

Total Warnings: 0
Total Errors:   2

根據報錯信息能夠知道,是由於在command.cfg配置文件中沒有定義check_weburl插件致使的。所以,須要在commands.cfg中加入check_weburl的插件配置。

#在commands.cfg中加入以下配置
[root@Nagios ~]# tail -7 /usr/local/nagios/etc/objects/commands.cfg
# 'check_weburl' command definition
define command {                #定義命令check_weburl

    command_name check_weburl
    command_line $USER1$/check_http $ARG1$ -w 10 -c 30

}

注意:
若是是測試域名URL監控,注意在Nagios服務器端的/etc/hosts里加入以下域名解析

[root@Nagios ~]# tail -2 /etc/hosts
192.168.0.200  nagios-server
192.168.0.223 www.yunjisuan.com bbs.yunjisuan.com

此時執行檢查語法的命令:

[root@Nagios ~]# /etc/init.d/nagios checkconfig
Total Warnings: 0
Total Errors:   0

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

使配置文件生效的命令爲:

[root@Nagios ~]# /etc/init.d/nagios reload 
Running configuration check...done.
Reloading nagios configuration...done

到這裏,能夠看到本身配置的URL監控成果了,以下圖所示:

QQ截圖20170817231009.png-72.9kB

添加測試網頁

#在Web1被監控端操做
[root@web01 ~]# cd /usr/local/nginx/html/www/
[root@web01 www]# ls
index.html  static
[root@web01 www]# ll static/
total 4
-rw-r--r--. 1 root root 13 Jul 27 06:10 index.html
[root@web01 www]# echo "welcome" >> static/test.html
[root@web01 www]# cat static/test.html
welcome

QQ截圖20170817233707.png-40.9kB

(7)監控任意TCP端口舉例

#端口檢查的實質就是1執行以下命令去監控
[root@Nagios ~]# /usr/local/nagios/libexec/check_tcp -H192.168.0.223 -p 80
TCP OK - 0.000 second response time on port 80|time=0.000216s;;;0.000000;10.000000

#在services.cfg最後加上以下定義
[root@Nagios objects]# tail -18 /usr/local/nagios/etc/objects/services.cfg
#tcp examples
define service {

    use generic-service
    host_name web01
    service_description ssh_22
    check_command check_tcp! 22

}

define service {

        use generic-service
        host_name web01
        service_description http_80
    check_command check_tcp! 80
    
}

#這裏的check_tcp爲Nagios plugin默認插件,commands.cfg會自動配置進去,不須要添加。此外,注意多端口同時監控的寫法。

說明:
從多年的監控經驗看,端口檢查也是很不錯的輔助監控方式!對於要求高的業務,必定要模擬真正用戶的訪問行爲監控纔好。

QQ截圖20170818195050.png-65.4kB

QQ截圖20170818200035.png-67.5kB

5.3 階段性小結

  • 實例暫時就說到這裏,關於如何讓不一樣服務在不一樣時間報警給不一樣用戶,如何配置nfs,rsync,drbd,MySQL,Oracle等特殊服務的監控,如何分組監控,如何讓多個運維值班協調解決問題等內容, 請見企業級監控Nagios實踐(下)。Nagios很複雜,不只能夠作上面的事,還能夠寫插件監控業務層的問題,甚至能夠監控到服務器的溫度及硬件信息。並且,Nagios給你擴展開發的機會不少,請你們多去摸索。這裏針對上面的內容小結一下:
  • 通常客戶端對外開啓的服務1,都會採用主動模式監控,例如:port,URL.
    主動模式的監控配置過程以下:
    1)在服務器端的命令行把要監控的命令先調試好。
    2)在commands.cfg裏定義Nagios命令,同時調用命令行的插件。
    3)在服務的配置文件裏定義要監控的服務,調用commands.cfg裏定義Nagios的監控命令!

六 拓展實驗內容:

  • 請用主動及被動模式分別監控MySQL主從同步(check_mysql)!
  • 根據不一樣管理員顯示不一樣的主機和服務,根據用戶分類顯示主機和服務
相關文章
相關標籤/搜索