使用nagios監(jiān)控主機及服務


寫在前頭:限于個人對nagios的了解有限,寫得不夠深入與系統(tǒng),甚至可能會有些錯誤,各位看官還多包涵。本文主要涉及的是nagios daemon、nrpe及三個部分。

1. nagios系統(tǒng)的功能

  • 主機或服務狀態(tài)監(jiān)控
    nagios是一款開源的監(jiān)控軟件,從它可以監(jiān)控的設備類型上來看,主要包含網(wǎng)絡設備,服務器設備。常見的網(wǎng)絡設備如:路由器、交換機、防火墻、F5、打印機等,常見的服務器設備主要分為:UNIX類、Linux類以及Windows類。按我的理解凡是支持snmp協(xié)議的設備,包含PC都可以通過nagios進行監(jiān)控。當然,nagios實現(xiàn)對主機資源及服務的監(jiān)控并非全依靠snmp協(xié)議,它最為主要的監(jiān)控手段是通過nrpe組件來實現(xiàn)。

  • 監(jiān)控告警通知
    nagios對在網(wǎng)絡中發(fā)現(xiàn)的問題會及時產(chǎn)生告警信息并通過事先定義好的方式,如郵件、短信、微信等方式通知相關人員。隨著網(wǎng)絡運維工作更加自動化,還可以通過nagios支持的相應API接口,開發(fā)相應的程序,實現(xiàn)其自動或人干干預去對監(jiān)控發(fā)現(xiàn)的問題進行自動化的處理。

  • 監(jiān)控信息可視化
    nagios結合web服務器,可以將整個網(wǎng)絡所監(jiān)控的所有信息以web頁面的形式展現(xiàn)出來,還可以結合外部軟件實現(xiàn)監(jiān)控數(shù)據(jù)可視化,以圖表的形式展示在web頁面中,本文將介紹nagios常用的畫圖軟件包pnp,現(xiàn)在叫pnp4nagios。

  • 監(jiān)控數(shù)據(jù)存儲
    nagios監(jiān)控到的數(shù)據(jù)會存儲下來,可以直接以文件的形式存儲也可以通過NDOUtils組件存儲到如mysql類的數(shù)據(jù)庫中,從而可以很好支持監(jiān)控歷史數(shù)據(jù)的查詢。

2.nagios系統(tǒng)的組成

nagios系統(tǒng)主要包含nagios daemon、nagios plugin、nrpe、web三個組件,它還包含NDOUtils、NSCA、NSClinet++組件,它們共同組成一個完整的nagios,組成邏輯圖如下所示:

nagios系統(tǒng)組成
  • Nagios Daemon
    nagios系統(tǒng)的核心組件,它負責組織與管理各組件,將它們協(xié)調起來共同完成監(jiān)控任務,并完成監(jiān)控信息的組織與展示。

  • Nagios Plugins
    nagios plugins主要就是nagios核心組件自帶以及用戶自開發(fā)的一些插件,它們是實現(xiàn)各項監(jiān)控的具體小程序,由它們將采集到相應的數(shù)據(jù)以后,回送給nagios服務器。

  • NRPE
    nagios系統(tǒng)要想取得被監(jiān)控主機的存活狀態(tài)、http、ftp、ssh服務是否可用,可以通過程序探測的出來,但如果要想取得被監(jiān)控端上如磁盤容量,cpu負載這類本地信息時,如果沒有相應的權限就不行,所以就產(chǎn)生了代理程序,事先在被監(jiān)控機上安裝代理程序(Linux系統(tǒng)是nrpe軟件),然后通過它們來獲取監(jiān)控數(shù)據(jù),再回送給nagios服務器。當nrpe啟動以后,它會開啟5666端口。nrpe的工程原理如下圖所示:

nrpe工作原理

NRPE 總共由兩部分組成:
check_nrpe:位于nagios server上。
NRPE daemon:位于被監(jiān)控的Linux主機上。
當Nagios 需要監(jiān)控某個遠程Linux 主機時:
nagios 會運行check_nrpe 這個插件,告訴它要檢查什么
check_nrpe 插件會連接到遠程的NRPE daemon,所用的方式是SSL; NRPE daemon 會運行相應的nagios 插件來執(zhí)行檢查;
NRPE daemon 將檢查的結果返回給check_nrpe 插件,插件將其遞交給nagios做處理。

  • NSClinet++
    NSClient++這一組件是安裝在windows主機上,相當于nagios server在windows端的代理程序。

  • NSCA
    NSAC這一組件適用于部署分布式nagios監(jiān)控系統(tǒng)時使用,它可以實現(xiàn)讓被監(jiān)控端主動將需要監(jiān)控的信息發(fā)送給nagios服務端。

3.本文所用到系統(tǒng)環(huán)境

OS:CentOS release 6.8 (Final) 2.6.32-642.el6.x86_64
還有一個重要環(huán)境:互聯(lián)網(wǎng)(yum、百度、Google)。
各軟件包:

組件 軟件包名
nagios daemon nagios-4.3.1.tar.gz
nrpe nrpe-2.15.tar.gz
nagios plugin nagios-plugins-2.2.0.tar.gz

上述4個軟件包下載鏈接:
鏈接:百度網(wǎng)盤 密碼:ayhk

4.nagios系統(tǒng)軟件部署

服務器側軟件安裝

前提:使系統(tǒng)具備編譯軟件源碼包的能力,并提前解決一些包、共享庫文件之間的依賴關系,建議安裝如下兩個Group組件

yum -y groupinstall "Development Tools" "Server  Platform Development"
  • 安裝nagios依賴的軟件包
    nagios要通過web頁面展現(xiàn)監(jiān)控結果,所以nagios服務器同時還得是一個web服務器,因為nagios各種數(shù)據(jù)的實時展現(xiàn)是動態(tài)頁面呈現(xiàn)的,因此還需要用到php。nagios要實現(xiàn)畫圖還依賴于gd、rrdtool包。按照官方說明安裝如下包:
yum install httpd php
yum install gcc glibc glibc-common         #如果安裝那兩個development包組這幾個包應該已經(jīng)裝上了
yum install gd gd-devel
  • 創(chuàng)建nagios用戶及組
    很多開源軟件如果是通過源碼包來進行安裝,為后續(xù)的安裝及服務運行做準備都需要通過事先創(chuàng)建好相應服務的賬號及組。這里相關指令如下:
/usr/sbin/useradd -m nagios  #指定不創(chuàng)建家目錄  
passwd nagios    #設定nagios的密碼,也可不設定
  • 創(chuàng)建一個可以從web接口接受外部指令的用戶組并將nagios及apache添加進組
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
  • 安裝nagios core
    nagios core就是前文提到的nagios daemon。解壓軟件包后,源碼安裝的通用三步驟:
    1. configure
    2. make
    3. make install
      此外相應指令如下:
./configure --with-command-group=nagcmd  #nagios默認會安裝在/usr/local/nagios目錄下

configure完成以后若出現(xiàn)make all以完成編譯的提示則繼續(xù)執(zhí)行:

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

如果上述任一指令執(zhí)行后有error發(fā)生或者某文件,某組件沒有找到,務必先解決相應的錯誤,一般可能是某些包沒有安裝導致,根據(jù)提示信息中的關鍵字找出可能的包,然后yum安裝上即可。

  • 創(chuàng)建登錄nagios web頁面的用戶及密碼
    默認系統(tǒng)中已存在名為nagiosadmin的賬戶,只需要給它指定密碼,其密碼會加密存儲,密碼文件默認為`/usr/local/nagios/etc/htpasswd.users,無需改動。
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin  #根據(jù)提示輸入密碼即可
  • 重啟web服務
service httpd restart
  • 安裝plugin及nrpe組件
    因為nagios服務器本身也同樣需要被監(jiān)控,自己監(jiān)控自己或者被其它的nagios服務器所監(jiān)控,所以它也需要安裝plugin及nrpe組件。在軟件包的解壓目錄中執(zhí)行如下指令:
cd /root/softwares/nagios-plugins-2.2.0
./configure --with-nagios-user=nagios --with-nagios-group=nagios #默認安裝在/usr/local/nagios路徑下
make
make install  

安裝完plugin以后會在/usr/local/nagios/libexec目錄下存放大量插件,就是用這些插件來實現(xiàn)最終的監(jiān)控目的的。
**按著安裝nrpe: **
nagios服務器安裝nrpe包主要目的是為了安裝check_nrpe插件,便于與被監(jiān)控Linux端的NRPE daemon程序通信,如果這臺nagios服務器也需要被其它nagios服務器監(jiān)控,那它還必須安裝nrpe daemon和nrpe daemon-config。

cd /root/softwares/nrpe-2.15
./configure    #執(zhí)行完會顯示nrpe將會使用端口號5666
make all   #configure執(zhí)行完后若提示make all編譯nrpe進程及客戶端則執(zhí)行這一指令
make install-plugin    #安裝check_nrpe插件,網(wǎng)絡里就一臺nagios服務器
                         #所以只安裝這一個check_nrpe即可
  • 設置web、nagios服務開機自啟
chkconfig --add httpd
chkconfig --add nagios
chkconfig httpd on
chkconfig nagios on
  • 通過web頁面登錄nagios
    做完以上步驟以后,即可打開瀏覽器,輸入http://nagios-server-ip/nagios,它會彈出提示框,輸入前面提到的nagiosadmin及其密碼即可登錄。登錄后的初始界面如下:
nagios登錄認證
nagios初始界面

被監(jiān)控端軟件安裝

在網(wǎng)絡中需要被監(jiān)控的Linux主機中安裝nagios pluginnrpe兩個組件,并安裝xinetd組件,用于啟動nrpe程序。

  • nagios plugin安裝
    創(chuàng)建nagios用戶:
useradd -m nagios

將tar.gz包解壓以后進入其目錄,然后執(zhí)行以下命令:

cd /root/softwares/nagios-plugins-2.2.0
./configure --with-nagios-user=nagios --with-nagios-group=nagios #默認安裝在/usr/local/nagios路徑下
make
make install  

安裝完成以后,建議修改安裝目錄/usr/local/nagios的屬主為nagios,并可查看到其下有三個目錄:

[root@test1 nagios]# ll
total 4
drwxr-xr-x 2 nagios nagios    6 Apr  7 23:30 include
drwxr-xr-x 2 nagios nagios 4096 Apr  7 23:30 libexec
drwxr-xr-x 3 nagios nagios   19 Apr  7 23:30 share
  • 安裝nrpe
    客戶端因為需要通過nrpe程序來執(zhí)行從服務器側接收到的command,因此必須安裝nrpe。
cd /root/softwares/nrpe-2.15
./configure    #執(zhí)行完會顯示nrpe將會使用端口號5666
make all   #configure執(zhí)行完后若提示make all編譯nrpe進程及客戶端則執(zhí)行
make install-plugin    #安裝check_nrpe插件
make install-daemon    #安裝nrpe守護進程
make install-daemon-config  #安裝nrpe的配置文件  

安裝xinetd
根據(jù)nrpeREADME文件的說明,可以將nrpe daemon作為xinetd下的一個服務來進行管理,所以需要事先將xinetd安裝到系統(tǒng)中:

yum install xinetd -y  #不要退出nrpe的解壓包目錄

再接著安裝nrpexinetd下的配置文件:

make install-xinetd

修改nrpe的配置文件,以允許nagios server連接它:
vim /etc/xinetd.d/nrpeonly from改成如下內容:

only_from       = 127.0.0.1 192.168.249.16 localhost  #注意多個ip間以空格分開,主要是加上nagios server的ip

nrpe服務端口號添加進去:vim /etc/services在最后一行加上nrpe 5666/tcp #Nagios-Client然后保存退出。

啟動xinetd服務并設置開機自啟:

chkconfig --add xinetd 
chkconfig xinetd on
service xinetd start

**檢查nrpe是否啟動即端口5666是否處于監(jiān)聽狀態(tài): ss -tnl **
另一個檢測nrpe是否工作正常的辦法是在nagios服務器上使用check_nrpe插件來測試與這臺客戶端的nrpe通信是否正常:

cd /usr/local/nagios/libexec
./check_nrpe -H localhost   #這里就是為何在被監(jiān)控端也安裝check_nrpe的原因
                            #這里的localhost必須在nrpe配置文件里明確允許
NRPE v2.15                      #若返回NRPE的版本號,則表示NRPE工作正常  

5.配置nagios監(jiān)控主機及服務

5.1 nagios配置文件位置

nagios要對哪些主機監(jiān)控哪些內容都是通過名為.cfg的配置文件進行定義的。
它們通常是位于/usr/local/nagios/etc目錄中的*.cfg以及位于/usr/local/nagios/etc/objects目錄下的*.cfg。
默認的文件如下:

[root@monitor-server1 etc]# ll /usr/local/nagios/etc
total 88
-rw-rw-r--. 1 nagios nagios 13023 Apr  7 15:35 cgi.cfg
-rw-r--r--. 1 nagios nagios    26 Apr  5 12:10 htpasswd.users
-rw-rw-r--. 1 nagios nagios 45139 Apr  7 19:20 nagios.cfg
-rw-r--r--. 1 nagios nagios  8060 Apr  5 20:49 nrpe.cfg
drwxrwxr-x. 2 nagios nagios  4096 Apr  6 01:28 objects
-rw-rw----. 1 nagios nagios  1312 Apr  5 11:59 resource.cfg

再看看/objects/目錄下的內容:

[root@monitor-server1 objects]# ll
total 48
-rw-rw-r--. 1 nagios nagios  7344 Apr  6 01:28 commands.cfg
-rw-rw-r--. 1 nagios nagios  2138 Apr  5 11:59 contacts.cfg
-rw-rw-r--. 1 nagios nagios  5451 Apr  5 18:29 localhost.cfg
-rw-rw-r--. 1 nagios nagios  3070 Apr  5 11:59 printer.cfg
-rw-rw-r--. 1 nagios nagios  3252 Apr  5 11:59 switch.cfg
-rw-rw-r--. 1 nagios nagios 10716 Apr  5 18:32 templates.cfg
-rw-rw-r--. 1 nagios nagios  3180 Apr  5 11:59 timeperiods.cfg
-rw-rw-r--. 1 nagios nagios  3991 Apr  5 11:59 windows.cfg

5.2 各配置文件的作用:**

  • nagios.cfg主配置文件
    nagios.cfgnagios服務端的主配置文件,它可以定義其它*.cfg配置文件的位置,是否啟用性能監(jiān)控(收集監(jiān)控數(shù)據(jù)以生成圖表),性能數(shù)據(jù)處理指令,性能數(shù)據(jù)文件格式模板及存放位置等,配置nagios日志文件存放的信息級別及位置,nagios進程的啟動用戶及組,日志文件的切割方式,顯示的日期格式等等。如果只是實現(xiàn)對主機及服務的監(jiān)控,不出監(jiān)控圖的話,默認可以不改動這一文件。

  • templates.cfg模板定義文件
    templates.cfg是監(jiān)控的模板文件,對所有主機、服務、聯(lián)系人的定義為簡化配置都可以根據(jù)需求定制相應的模板,然后在主機、服務、聯(lián)系人配置文件中去引用模板即可。下面進行簡要說明:

      define contact{                                                        ;定義一個聯(lián)系人相關的模板    
      name                            generic-contact         ; 聯(lián)系人信息的模板名稱
      service_notification_period     24x7                    ; 對服務類的監(jiān)控什么時間段可以發(fā)送告警通知
      host_notification_period        24x7                    ; 對主機類的監(jiān)控什么時間段可以發(fā)送告警通知
      service_notification_options    w,u,c,r,f,s             ; 對哪些服務狀態(tài)進
    行通知,w:警告,u:unknown未知狀態(tài),c:嚴重,r:recover恢復,f:flapping,s:scheduled downtime
      host_notification_options       d,u,r,f,s               ; 對哪些主機狀態(tài)進行
       ;通知,d:宕機了,u:主機不可達,r:主機重啟了,f:flapping,s:恢復了
      service_notification_commands   notify-service-by-email ; 發(fā)送服務通知的方式
      host_notification_commands      notify-host-by-email    ; 發(fā)送主機通知的方式
      register                        0                       ; 告訴nagios daemon不要把這一項定義注冊為服務,它只是一個模板
      }
      define host{                                                  ;定義一個用于主機的模板
      name                            generic-host    ; 主機模板的名稱
      notifications_enabled           1               ; 允許發(fā)送主機通知,0則為關閉
      event_handler_enabled           1               ; 允許處理主機事件
      flap_detection_enabled          1               ; 允許檢測主機是否頻繁發(fā)生狀態(tài)改變
      process_perf_data               1               ; 允許對主機進行性能統(tǒng)計
      retain_status_information       1               ; Retain status information across program restarts
      retain_nonstatus_information    1               ; Retain non-status information across program restarts
      notification_period             24x7            ; 任意時間都可以發(fā)送通知消息
      register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
      }
    
    define host{                                                 ;定義另一個主機模板
      name                            linux-server    ; 主機模板的名稱
      use                             generic-host    ; 先是引用了上面那個叫generic-host的主機模板,省去對通用項的重復定義工作
      check_period                    24x7            ; 定義對主機的檢測周期
      check_interval                  5               ; 定義多長時間對主機進行一次檢測
      retry_interval                  1               ; 當一次檢測失敗了隔多長時間再次檢測
      max_check_attempts              10              ; 最大幾次失敗后認定主機故障
      check_command                   check-host-alive ; 使用哪個命令對主機進行檢測,這一命令必須在commands.cfg文件中進行詳細定義
      notification_period             workhours       ; 定義什么時間段可以發(fā)送通知,這一配置在上一個叫generic_host的模板中也有,但此時此處以這個的配置為準,時間段的寫法在timeperiods.cfg文件中定義
      notification_interval           120             ; 發(fā)送通知的時間間隔
      notification_options            d,u,r           ; 哪些主機狀態(tài)可以發(fā)送通知
      contact_groups                  admins          ; Notifications get sent to the admins by default
      action_url                      /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
      register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
      }
    define service{                                                       ;定義一個服務模板
      name                            generic-service         ; 服務模板的名稱
      active_checks_enabled           1                       ; 允許進行服務狀態(tài)檢測
      passive_checks_enabled          1                       ; 被動檢測,由nagios發(fā)起,客戶端nrpe接收到請求后使用plugin程序進行檢測
      parallelize_check               1                       ; Active service checks should be parallelized (disabling this can lead to major performance problems)
      obsess_over_service             1                       ; We should obsess over this service (if necessary)
      check_freshness                 0                       ; Default is to NOT check service 'freshness'
      notifications_enabled           1                       ; 允許發(fā)送關于服務的通知
      event_handler_enabled           1                       ; Service event handler is enabled
      flap_detection_enabled          1                       ; 允許檢測服務狀態(tài)漂移
      process_perf_data               1                       ; 開啟性能數(shù)據(jù)采集
      retain_status_information       1                       ; Retain status information across program restarts
      retain_nonstatus_information    1                       ; Retain non-status information across program restarts
      is_volatile                     0                       ; The service is not volatile
      check_period                    24x7                    ; 什么時間段對服務進行檢測
      max_check_attempts              3                       ; 連續(xù)3次檢測失敗則認定服務故障
      check_interval           10                     ; 10分鐘對服務進行一次檢測
      retry_interval            2                     ; 一次檢測失敗了隔2分鐘再次進行檢測
      contact_groups                  admins                  ; 將通知發(fā)給admins組里定義的成員
      notification_options            w,u,c,r                 ; Send notifications about warning, unknown, critical, and recovery events
      notification_interval           60                      ; 對服務的告警每60分鐘發(fā)一次
      notification_period             24x7                    ; 任何時間段都可以發(fā)送告警通知
       register                        0                      ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
      }
    

以上便是templates.cfg文件中的主要內容,可以按需修改已定義的模板或者新定義模板。其中涉及到部分對command的定義,它是需要在commands.cfg文件中進行定義的。

  • commands.cfg命令定義文件
    commands.cfg文件可以包含監(jiān)控時需要用到的指令的定義,由這些指令來完成具體的監(jiān)控工作,它們可以在services.cfg文件中被調用。配置示例如下:

     # 'notify-host-by-email' command definition
      define command{
          command_name    notify-host-by-email       ;在templates.cfg文件  
                                                     ;中即有一個叫作notify-host-by-email的  
                                                     ;command引用,它引用的就是   
                         此處定義的內容。command_name就相當于變量名
      command_line    /usr/bin/printf "%b" "***** Nagios   *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$
    \nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$    
          ;定義命令具體的指令及參數(shù),它就相當于是在給前面的變量賦值
      }
    
     # 'notify-service-by-email' command definition
      define command{
      command_name    notify-service-by-email       ;在templates.cfg文件中即有一個叫作notify-service-by-email的command引用,它引用的就是此處定義的內容
      command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService:           $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTA
      DDRESS$\nState: $SERVICESTATE$\n\nDate/Time:         $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/
      $SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
     # 'check-host-alive' command definition
    define command{
      command_name    check-host-alive           ;定義檢測主機存活的指令
      command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
      }
    
    define command{
      command_name    check_http        ;檢測http服務是否可用的指令
      command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
    

這里有一個名為check_nrpe的command特別重要:需要指定它才能夠啟動本地的check_nrpe去連接被監(jiān)控端上的nrpe daemon,并在被監(jiān)控端執(zhí)行相應的命令,需要自己添加,默認commands.cfg文件里是沒有的:

#`check_nrpe` command definition
define command{
    command_name    check_nrpe
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$            
                ;此處的$ARG1$這一變量指的就是在被監(jiān)控端的nrpe.cfg文件                        
                 ;里定義的實際命令,后面會有講解
    }
  • contacts.cfg聯(lián)系人定義文件
    contracts.cfg這一文件定義了當需要發(fā)送告警通知時,需要發(fā)給哪些聯(lián)系人組,然后不同的組里可以分別定義相應的成員,默認情況下會將通知發(fā)給名為admins的組,里面包含的email地址即為接收告警通知郵件的地址。示例如下:
       define contact{
         contact_name                    nagiosadmin     ; Short name of user
        use              generic-contact     ; Inherit default values from generic-contact template (defined above)
         alias                           Nagios Admin        ; Full name of user
    
         email                           somebody@abc.com    ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
         }
    

define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}


- **timeperiods.cfg時間段模板定義文件**    
    `timeperiods.cfg`主要用來對要進行監(jiān)控的時間段以及可以發(fā)送通知的時間段進行定義,很靈活很方便。
      # notifications, etc.  The classic "24x7" support nightmare. :-)
      define timeperiod{
        timeperiod_name 24x7
        alias           24 Hours A Day, 7 Days A Week
        sunday          00:00-24:00   ;這個名為24x7的時間模板包括了一周7天每天的24小時
        monday          00:00-24:00
        tuesday         00:00-24:00
        wednesday       00:00-24:00
        thursday        00:00-24:00
        friday          00:00-24:00
        saturday        00:00-24:00
        }
      # 'workhours' timeperiod definition
        define timeperiod{
      timeperiod_name   workhours   ;這個名為workhous的時間模板只包
                                      ;含了周一至周五每天的9點到17點
      alias     Normal Work Hours
      monday        09:00-17:00
      tuesday       09:00-17:00
      wednesday 09:00-17:00
      thursday  09:00-17:00
      friday        09:00-17:00
      }

- **localhost.cfg監(jiān)控服務器配置文件**  
    `localhost.cfg`這個文件是用來定義對這臺nagios服務器的哪些資源及服務進行監(jiān)控的,它主要包含兩部分的定義,針對host級別的和針對service級別的。示例如下:  
    ```
       define host{                         ;定義一臺linux主機
        use                     linux-server            ; 針對這臺主機調用哪個host模
       板,這里是調用templates.cfg中定義的linux-server模板,可以指定多個,
       以逗號分隔

        host_name               localhost          ;這臺主機的名稱
        alias                   localhost                 ;這臺主機的別名
        address                 127.0.0.1             ;這臺主機的ip
        process_perf_data       1                   ;是否開啟性能數(shù)據(jù)采集,這項  
       配置在linux-server模板里面也配置的有,當同一配置在不同的文件中    出現(xiàn)  
       時,配置單位越小的越優(yōu)先,這里具體的單臺主機肯定比一個主機模板的  
       范圍要精細,所以這里的配置優(yōu)先。   
        }
      define hostgroup{                             ;定義一個主機組,即把可以具有  
                                 ;某些共同配置屬性的主機放到一個組中
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         localhost     ; Comma separated list of hosts that belong to this group
        }

       # Define a service to "ping" the local machine

       define service{                                        ;定義一個要監(jiān)控的服務
        use                             local-service         ; 針對這個服務調用的模  
       板,模板可以指定多個,以逗號分隔
        host_name                       localhost         ;監(jiān)控哪個或哪些個主機的這
       一服務,不同的主機間用逗事情隔開
        service_description             PING           ;服務命名,簡單明了即可,不要過長
        check_command                   check_ping!100.0,20%!500.0,60%  ;監(jiān)  
      控要使用的指令,此處表示直接使用nagios服務器上/usr/local/libexec/下的  
       check_ping插件來進行服務監(jiān)測。要查看目錄中每個插件的用法,在目錄下使
      用./plugin_name --help查看
        }

- **hosts.cfg定義要被監(jiān)控的主機**  
`hosts.cfg`文件可以將網(wǎng)絡中所有需要監(jiān)控的主機定義進來,并將它們根據(jù)需求分組,一臺主機可以同時屬于不同的組,默認情況下不允許組里面一臺主機都沒有,然后根據(jù)需要調用不同的主機模板(事先在templates.cfg中定義)。另一點很重要的是這個文件需要自己創(chuàng)建,不建議直接在localhost.cfg里添加。示例如下:  

define host {
use linux-server ;調用的模板
host_name server1
alias server1
address 192.168.249.11
}
define host {
use linux-server
host_name server2
alias server2
address 192.168.249.12
}
define host {
use linux-server
host_name server3
alias server3
address 192.168.249.13
}
define host {
use linux-server
host_name server4
alias server4
address 192.168.249.14
}
define host {
use linux-server
host_name server5
alias server5
address 192.168.249.15
}
define hostgroup {
hostgroup_name loadblance-servers
alias lb-servers
members server1,server2
}
define hostgroup {
hostgroup_name web-servers
alias web-servers
members server3,server4
}
define hostgroup{
hostgroup_name database-servers
alias db-servers
members server1,server2,server5 ;根據(jù)業(yè)務需求,相同
;的主機可以屬于多個不同的業(yè)務組
}


- **services.cfg定義要被監(jiān)控的服務**  
    `services.cfg`這個文件默認也是不存在的,需要手工創(chuàng)建,可以以localhost.cfg里定義的service部分作為模板進行修改。在它里面可以針對不同的服務調用不同的模板,以及把這些服務應用到哪些主機或主機組上。示例如下:  

      # Define a service to "ping" the local machine

      define service{
        use                             local-service         ; Name of service                 template to use
        hostgroup_name          web-servers,loadblance-servers,database-servers
        service_description             PING
        check_command               check_ping!100.0,20%!500.0,60%  
        check_interval          1
        }
      # Define a service to check the disk space of the root partition
      # on the local machine.  Warning if < 20% free, critical if
      # < 10% free space on partition.
      define service{
        use                             local-service         ; Name of service template to use
        hostgroup_name          web-servers,loadblance-servers,database-servers
        service_description             Root Partition
        check_command           check_nrpe!check_local_disk  ;注意這種命令寫法,凡是前面以check_nrpe!開頭后面再跟上命令
      ;名稱的,必須在commands.cfg中先定義check_nrpe這一命 令,前
      ;文有寫,然后在到客戶端上nrpe.cfg文件中定義check_nrpe!后面跟
      ;的命令的詳細指令參數(shù)
        }
      # Define a service to check the number of currently logged in
      # users on the local machine.  Warning if > 20 users, critical
      # if > 50 users.
      define service{
         use                             local-service         ; Name of service           template to use
        hostgroup_name          web-servers,loadblance-servers,database-servers 
        service_description             Current Users
    check_command           check_nrpe!check_local_users
        }
      # Define a service to check the number of currently running     procs
      # on the local machine.  Warning if > 250 processes, critical if
      # > 400 processes.

      define service{
        use                             local-service         ; Name of service template to use
        hostgroup_name          web-servers,loadblance-servers,database-servers
        service_description             Zombie Processes
        check_command           check_nrpe!check_local_zombie_procs
        }
      define service{
       use              local-service
       hostgroup_name           web-servers,loadblance-servers,database-servers
       service_description      Total Processes
       check_command            check_nrpe!check_local_total_procs
       }
      # Define a service to check the load on the local machine. 
       define service{
        use                             local-service         ; Name of service template to use
       hostgroup_name           web-servers,loadblance-servers,database-servers
        service_description             Current Load
       check_command            check_nrpe!check_local_load
        }
      # Define a service to check the swap usage the local machine. 
       # Critical if less than 10% of swap is free, warning if less than 20% is free
      define service{
        use                             local-service         ; Name of service template to use
        hostgroup_name          web-servers,loadblance-servers,database-servers
        service_description             Swap Usage
       check_command            check_nrpe!check_local_swap
        }
      # Define a service to check SSH on the local machine.
      # Disable notifications for this service by default, as not all users may have SSH enabled.
      define service{
        use                             local-service         ; Name of service template to use
        hostgroup_name          web-servers,loadblance-servers,database-servers
        service_description             SSH
         check_command          check_ssh
        notifications_enabled       0
        }
      # Define a service to check HTTP on the local machine.
      # Disable notifications for this service by default, as not all users may have HTTP enabled.
      define service{
        use                             local-service         ; Name of service template to use
      hostgroup_name            web-servers,loadblance-servers,database-servers
        service_description             HTTP
       check_command            check_http
       notifications_enabled        0
        }

由于新增加了兩個配置 文件(hosts.cfg和services.cfg),所以需要在nagios主配置文件(nagios.cfg)中指明它們的位置,只需要在nagios.cfg中加上兩行,搜索下`cfg_file`加到它們的下面即可:

cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg


### 5.3 檢查配置文件  
nagios自帶的就有配置文件語法及邏輯檢測工具,檢測方式:  

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

檢測如果沒有問題,則可以重載nagios服務,使用最新配置文件來工作了。

Total Warnings: 0
Total Errors: 0

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

**重載服務**`service nagios reload`  

### 5.4 客戶端配置監(jiān)測命令及參數(shù)  
要實現(xiàn)對Linux主機的監(jiān)控還差非常關鍵的一步,去到各被監(jiān)控linux主機上配置實際要執(zhí)行的指令,它們是在`/etc/usr/local/nagios/etc/nrpe.cfg`文件中定義的。  示例文件如下:  

command[check_local_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
#注意[]中的內容,這些字符要和nagios服務端上
#services.cfg里定義command時check_nrpe!后面的字符完全一樣,等號中的內容則通過
#前面介紹的方法查看每個plugin的具體用法
command[check_local_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_local_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda3
command[check_local_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_local_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_local_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

**注意**:[]中的內容,這些字符要和nagios服務端上
      services.cfg里定義command時check_nrpe!后面的字符完全一樣,等號中的內容則前面介紹的方法查看每個plugin的具體用法。  

客戶端在修改完nrpe.cfg文件以后,需要重啟nrpe進程:  

service xinetd restart #nrpe daemon是作為xinetd下的一個服務
#的,所以重啟它也就重啟了nrpe


## 6. 登錄nagios web頁面查看監(jiān)控數(shù)據(jù)  
在nagios客戶端還沒有配置好nrpe.cfg文件時,nagios server是無法監(jiān)控到客戶端的,此時應該可以通過web頁面查看到一堆NRPE:undefined command之類的錯誤信息,當客戶端nrpe服務修改完成并重啟之后,過會兒就能夠正常監(jiān)控上這些客戶端了。效果展示如下:  

  ![主機監(jiān)控視圖](http://upload-images.jianshu.io/upload_images/1920135-036cfb0f1f6e4977.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


![服務監(jiān)控視圖](http://upload-images.jianshu.io/upload_images/1920135-a25b2059fa5626e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
細心的讀者可能已經(jīng)發(fā)現(xiàn)兩圖中,日期格式好像更易讀,默認會顯示為`04-08-2017 22:30:00`這種,這是因為筆者修改了nagios.cfg的`date_format`為`iso8601`,具體可查看nagios.cfg文件獲知。    

## 7. 結尾  
在前期安裝及配置過程中要仔細留意可能出現(xiàn)的警告及錯誤信息,盡可能修復它們之后在后續(xù)操作,nagios跑起來以后,可以通過查看`/var/log/messages`或者`/usr/local/nagios/var/nagios.log`來觀察錯誤信息,以找到解決辦法。   
比如:  
在系統(tǒng)運行過程中因為nagios server和客戶端nrpe間需要建立ssl會話,所以openssl,openssl-devel包是需要安裝的。如果要使用snmp監(jiān)控主機及服務則需要安裝net-snmp包。如果對防火墻不熟悉建議關閉其服務,關閉SELinux。這些在文中未提到,但也是需要注意的地方。 

通過nagios實現(xiàn)對主機和服務的監(jiān)控基本操作方法就寫到這兒,在日常使用中有很多可可以深入的地方,特別是對現(xiàn)網(wǎng)各種服務各項指標的監(jiān)控工作,還需要開發(fā)插件然后部署才能夠實現(xiàn),這應該才是監(jiān)控類軟件應用真正有挑戰(zhàn)的地方,考驗運維工程師的開發(fā)能力,nagios支持使用perl、python、shell,C等語言開發(fā)插件以滿足實際業(yè)務監(jiān)控需求。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容