Nagios學習筆記

1、概況php

參考http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.htmlhtml

openstack目前發展迅猛,特別是開放了big tent項目後,一時間各種以openstack爲依託的開源工具提交多達1000多個。linux

openstack網絡組建neutron以複雜晦澀著稱,其部署和運維都讓人頭疼。ios

United Stack開發了steth項目,用以解決網絡複雜問題現狀。他是一款集成了http、dhcp、ping、iperf等網絡診斷工具的開源項目,稍後研究web

nagios是一款流行的網絡運維工具,能夠經過監測和豐富的通知機制幫助運維管理。詳細介紹略。apache

2、工做原理windows

Nagios的功能是監控服務和主機,可是他自身並不包括這部分功能,全部的監控、檢測功能都是經過各類插件來完成的。 啓動Nagios後,它會週期性的自動調用插件去檢測服務器狀態,同時Nagios會維持一個隊列,全部插件返回來的狀態信息都進入隊列,Nagios每次都從隊首開始讀取信息,並進行處理後,把狀態結果經過web顯示出來。 Nagios提供了許多插件,利用這些插件能夠方便的監控不少服務狀態。安裝完成後,在nagios主目錄下的/libexec裏放有nagios自帶的可使用的全部插件,如,check_disk是檢查磁盤空間的插件,check_load是檢查CPU負載的,等等。每個插件能夠經過運行./check_xxx –h 來查看其使用方法和功能。 Nagios能夠識別4種狀態返回信息,即 0(OK)表示狀態正常/綠色、1(WARNING)表示出現警告/黃色、2(CRITICAL)表示出現很是嚴重的錯誤/紅色、3(UNKNOWN)表示未知錯誤/深黃色。Nagios根據插件返回來的值,來判斷監控對象的狀態,並經過web顯示出來,以供管理員及時發現故障。 centos

再說報警功能,若是監控系統發現問題不能報警那就沒有意義了,因此報警也是nagios很重要的功能之一。可是,一樣的,Nagios 自身也沒有報警部分的代碼,甚至沒有插件,而是交給用戶或者其餘相關開源項目組去完成的。

Nagios 安裝,是指基本平臺,也就是Nagios軟件包的安裝。它是監控體系的框架,也是全部監控的基礎。

打開Nagios官方的文檔,會發現Nagios基本上沒有什麼依賴包,只要求系統是Linux或者其餘Nagios支持的系統。不過若是你沒有安裝apache(http服務),那麼你就沒有那麼直觀的界面來查看監控信息了,因此apache姑且算是一個前提條件。關於apache的安裝,網上有不少,照着安裝就是了。安裝以後要檢查一下是否能夠正常工做。

知道Nagios 是如何經過插件來管理服務器對象後,如今開始研究它是如何管理遠端服務器對象的。Nagios 系統提供了一個插件NRPE。Nagios 經過週期性的運行它來得到遠端服務器的各類狀態信息。它們之間的關係以下圖所示:

Nagios 經過NRPE 來遠端管理服務

1. Nagios 執行安裝在它裏面的check_nrpe 插件,並告訴check_nrpe 去檢測哪些服務。

2. 經過SSL,check_nrpe 鏈接遠端機子上的NRPE daemon

3. NRPE 運行本地的各類插件去檢測本地的服務和狀態(check_disk,..etc)

4. 最後,NRPE 把檢測的結果傳給主機端的check_nrpe,check_nrpe 再把結果送到Nagios狀態隊列中。

5. Nagios 依次讀取隊列中的信息,再把結果顯示出來。

 3、安裝服務端瀏覽器

個人centos6.8安裝在kvm虛機中安全

服務端hostname vserver  ip地址192.168.122.232

查看是否安裝過如下軟件包
# rpm -q gcc glibc glibc-common gd gd-devel xinetd openssl-devel

 若是有沒安裝的就經過yum install安裝

建立用戶 用戶組 文件夾
# useradd -s /sbin/nologin nagios
# mkdir /usr/local/nagios
# chown -R nagios.nagios /usr/local/nagios

去官網downdoal最新安裝包

# tar zxvf nagios-4.2.1.tar.gz

# cd nagios

#./configure --help

#根據須要選擇本身的配置屬性

# ./configure --prefix=/usr/local/nagios

#make all
#make install
#make install-init
#make install-commandmode
#make install-config

 設置服務自啓動和運行級別

# chkconfig --add nagios

# chkconfig --level 35 nagios on

# chkconfig --list nagios

 ***目錄說明

bin     Nagios 可執行程序所在目錄
etc     Nagios 配置文件所在目錄
sbin     Nagios CGI 文件所在目錄,也就是執行外部命令所需文件所在的目錄
share     Nagios網頁文件所在的目錄
libexec     Nagios 外部插件所在目錄
var     Nagios 日誌文件、lock 等文件所在的目錄
var/archives     Nagios 日誌自動歸檔目錄
var/rw     用來存放外部命令文件的目錄

安裝Nagios 插件,一樣先去官網下載安裝包

# tar zxvf nagios-plugins-1.4.16.tar.gz

# cd nagios-plugins-1.4.16

# ./configure --prefix=/usr/local/nagios

# make && make install

 !!!

注意Apache 和Php 不是安裝nagios 所必須的,可是nagios提供了web監控界面,經過web監控界面能夠清晰的看到被監控主機、資源的運行狀態,所以,安裝一個web服務是很必要的
須要注意的是,nagios在nagios3.1.x版本之後,配置web監控界面時須要php的支持。這裏咱們下載的nagios版本爲nagios-4.2.0,所以在編譯安裝完成apache後,還須要編譯php模塊,這裏選取的php版本爲php5版本。

你能夠選擇直接yum install  也能夠自行安裝

附上參考的安裝步驟

# wget http://archive.apache.org/dist/httpd/httpd-2.2.23.tar.gz

# tar zxvf httpd-2.2.23.tar.gz

# cd httpd-2.2.23

# ./configure --prefix=/usr/local/apache2

# make && make install

若出現錯誤:

則在編譯時入加 --with-included-apr 便可解決。

 

# wget http://cn2.php.net/distributions/php-5.4.10.tar.gz

# tar zxvf php-5.4.10.tar.gz

# cd php-5.4.10

# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs

# make && make install

 

apache的配置很重要,特別是認證

配置apache
找到apache 的配置文件/usr/local/apache2/conf/httpd.conf
找到:

User daemon 
Group daemon 

修改成 

User nagios 
Group nagios 

而後找到 

<IfModule dir_module>   DirectoryIndex index.html </IfModule> 

修改成 

<IfModule dir_module>   DirectoryIndex index.html index.php </IfModule> 

接着增長以下內容: 

AddType application/x-httpd-php .php 

爲了安全起見,通常狀況下要讓nagios 的web 監控頁面必須通過受權才能訪問,這須要增長驗證配置,即在httpd.conf 文件最後添加以下信息:

#setting for nagios 
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin" 
<Directory "/usr/local/nagios/sbin"> 
     AuthType Basic 
     Options ExecCGI 
     AllowOverride None 
     Order allow,deny 
     Allow from all 
     AuthName "Nagios Access" 
     AuthUserFile /usr/local/nagios/etc/htpasswd             //用於此目錄訪問身份驗證的文件 
     Require valid-user 
</Directory> 
Alias /nagios "/usr/local/nagios/share" 
<Directory "/usr/local/nagios/share"> 
     AuthType Basic 
     Options None 
     AllowOverride None 
     Order allow,deny 
     Allow from all 
     AuthName "nagios Access" 
     AuthUserFile /usr/local/nagios/etc/htpasswd 
     Require valid-user 
</Directory>

 同時

在上面的配置中,指定了目錄驗證文件htpasswd,下面要建立這個文件:

# /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd name(隨意指定)

PS:若是忘記密碼或者由於其餘狀況不知道nagios默認的密碼(通常狀況下,默認用戶和密碼都是nagiosadmin),好比採用RDO安裝openstack時,默認會安裝nagios,mitaka版本下,使用nagios的默認密碼沒法經過驗證,因此須要重設密碼,密碼重設方法:

htpasswd -c /usr/local/nagios/etc/htpasswd(這是個人實驗環境的路徑,若是經過RDO安裝也可能在lib64目錄下) name (指定用戶名),而後從新輸入兩遍密碼便可。


這樣就在/usr/local/nagios/etc 目錄下建立了一個htpasswd 驗證文件,當經過http://192.168.122.232/nagios/ 訪問時就須要輸入用戶名和密碼了。

最後 重啓apache

#service httpd restart

# /usr/local/apache2/bin/apachectl start

 

四 配置Nagios

在安裝nagios後 訪問瀏覽器http://192.168.122.232/nagios能夠訪問到nagios頁面 可是卻不能監控任何對象 須要進行詳細配置

Nagios 安裝完畢後,默認的配置文件在/usr/local/nagios/etc目錄下。

29

每一個文件或目錄含義以下表所示:

文件名或目錄名 用途
cgi.cfg 控制CGI訪問的配置文件
nagios.cfg Nagios 主配置文件
resource.cfg 變量定義文件,又稱爲資源文件,在些文件中定義變量,以便由其餘配置文件引用,如$USER1$
objects objects 是一個目錄,在此目錄下有不少配置文件模板,用於定義Nagios 對象
objects/commands.cfg 命令定義配置文件,其中定義的命令能夠被其餘配置文件引用
objects/contacts.cfg 定義聯繫人和聯繫人組的配置文件
objects/localhost.cfg 定義監控本地主機的配置文件
objects/printer.cfg 定義監控打印機的一個配置文件模板,默認沒有啓用此文件
objects/switch.cfg 定義監控路由器的一個配置文件模板,默認沒有啓用此文件
objects/templates.cfg 定義主機和服務的一個模板配置文件,能夠在其餘配置文件中引用
objects/timeperiods.cfg 定義Nagios 監控時間段的配置文件
objects/windows.cfg 監控Windows 主機的一個配置文件模板,默認沒有啓用此文件

在nagios的配置過程當中涉及到的幾個定義有:主機、主機組,服務、服務組,聯繫人、聯繫人組,監控時間,監控命令等,從這些定義能夠看出,nagios各個配置文件之間是互爲關聯,彼此引用的。

成功配置出一臺nagios監控系統,必需要弄清楚每一個配置文件之間依賴與被依賴的關係,最重要的有四點:

第一:定義監控哪些主機、主機組、服務和服務組;
第二:定義這個監控要用什麼命令實現;
第三:定義監控的時間段;
第四:定義主機或服務出現問題時要通知的聯繫人和聯繫人組。

爲了能更清楚的說明問題,同時也爲了維護方便,建議將nagios各個定義對象建立獨立的配置文件:

  •  建立hosts.cfg文件來定義主機和主機組
  •  建立services.cfg文件來定義服務
  •  用默認的contacts.cfg文件來定義聯繫人和聯繫人組
  •  用默認的commands.cfg文件來定義命令
  •  用默認的timeperiods.cfg來定義監控時間段
  •  用默認的templates.cfg文件做爲資源引用文件

 

a. templates.cfg文件

爲了避免必重複定義一些監控對象,Nagios引入了一個模板配置文件,將一些共性的屬性定義成模板,以便於屢次引用。這就是templates.cfg的做用。

下面詳細介紹下templates.cfg文件中每一個參數的含義:

  1 define contact{
  2         name                            generic-contact    ; 聯繫人名稱
  3         service_notification_period     24x7               ; 當服務出現異常時,發送通知的時間段,這個時間段"24x7"在timeperiods.cfg文件中定義
  4         host_notification_period        24x7               ; 當主機出現異常時,發送通知的時間段,這個時間段"24x7"在timeperiods.cfg文件中定義
  5         service_notification_options    w,u,c,r            ; 這個定義的是「通知能夠被髮出的狀況」。w即warn,表示警告狀態,u即unknown,表示不明狀態;
  6                                                            ; c即critical,表示緊急狀態,r即recovery,表示恢復狀態;
  7                                                            ; 也就是在服務出現警告狀態、未知狀態、緊急狀態和從新恢復狀態時都發送通知給使用者。
  8         host_notification_options       d,u,r                   ; 定義主機在什麼狀態下須要發送通知給使用者,d即down,表示宕機狀態;
  9                                                                 ; u即unreachable,表示不可到達狀態,r即recovery,表示從新恢復狀態。
 10         service_notification_commands   notify-service-by-email ; 服務故障時,發送通知的方式,能夠是郵件和短信,這裏發送的方式是郵件;
 11                                                                 ; 其中「notify-service-by-email」在commands.cfg文件中定義。
 12         host_notification_commands      notify-host-by-email    ; 主機故障時,發送通知的方式,能夠是郵件和短信,這裏發送的方式是郵件;
 13                                                                 ; 其中「notify-host-by-email」在commands.cfg文件中定義。 
 14         register                        0                    ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
 15         }
 16 define host{
 17         name                            generic-host    ; 主機名稱,這裏的主機名,並非直接對應到真正機器的主機名;
 18                                                         ; 乃是對應到在主機配置文件裏所設定的主機名。
 19         notifications_enabled           1               ; Host notifications are enabled
 20         event_handler_enabled           1               ; Host event handler is enabled
 21         flap_detection_enabled          1               ; Flap detection is enabled
 22         failure_prediction_enabled      1               ; Failure prediction is enabled
 23         process_perf_data               1               ; 其值能夠爲0或1,其做用爲是否啓用Nagios的數據輸出功能;
 24                                                         ; 若是將此項賦值爲1,那麼Nagios就會將收集的數據寫入某個文件中,以備提取。
 25         retain_status_information       1               ; Retain status information across program restarts
 26         retain_nonstatus_information    1               ; Retain non-status information across program restarts
 27         notification_period             24x7            ; 指定「發送通知」的時間段,也就是能夠在何時發送通知給使用者。
 28         register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
 29         }
 30 define host{
 31         name                            linux-server    ; 主機名稱
 32         use                             generic-host    ; use表示引用,也就是將主機generic-host的全部屬性引用到linux-server中來;
 33                                                         ; 在nagios配置中,不少狀況下會用到引用。
 34         check_period                    24x7            ; 這裏的check_period告訴nagios檢查主機的時間段
 35         check_interval                  5               ; nagios對主機的檢查時間間隔,這裏是5分鐘。
 36         retry_interval                  1               ; 重試檢查時間間隔,單位是分鐘。
 37         max_check_attempts              10              ; nagios對主機的最大檢查次數,也就是nagios在檢查發現某主機異常時,並不立刻判斷爲異常情況;
 38                                                         ; 而是多試幾回,由於有可能只是一時網絡太擁擠,或是一些其餘緣由,讓主機受到了一點影響;
 39                                                         ; 這裏的10就是最多試10次的意思。
 40         check_command                   check-host-alive ; 指定檢查主機狀態的命令,其中「check-host-alive」在commands.cfg文件中定義。
 41         notification_period             24x7            ; 主機故障時,發送通知的時間範圍,其中「workhours」在timeperiods.cfg中進行了定義;
 42                                                         ; 下面會陸續講到。
 43         notification_interval           10              ; 在主機出現異常後,故障一直沒有解決,nagios再次對使用者發出通知的時間。單位是分鐘;
 44                                                         ; 若是你以爲,全部的事件只須要一次通知就夠了,能夠把這裏的選項設爲0
 45         notification_options            d,u,r           ; 定義主機在什麼狀態下能夠發送通知給使用者,d即down,表示宕機狀態;
 46                                                         ; u即unreachable,表示不可到達狀態;
 47                                                         ; r即recovery,表示從新恢復狀態。
 48         contact_groups                  ts              ; 指定聯繫人組,這個「admins」在contacts.cfg文件中定義。
 49         register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
 50         }
 51 define host{
 52         name                    windows-server  ; The name of this host template
 53         use                     generic-host    ; Inherit default values from the generic-host template
 54         check_period            24x7            ; By default, Windows servers are monitored round the clock
 55         check_interval          5               ; Actively check the server every 5 minutes
 56         retry_interval          1               ; Schedule host check retries at 1 minute intervals
 57         max_check_attempts      10              ; Check each server 10 times (max)
 58         check_command           check-host-alive        ; Default command to check if servers are "alive"
 59         notification_period     24x7            ; Send notification out at any time - day or night
 60         notification_interval   10              ; Resend notifications every 30 minutes
 61         notification_options    d,r             ; Only send notifications for specific host states
 62         contact_groups          ts              ; Notifications get sent to the admins by default
 63         hostgroups              windows-servers ; Host groups that Windows servers should be a member of
 64         register                0               ; DONT REGISTER THIS - ITS JUST A TEMPLATE
 65         }
 66 define service{
 67         name                            generic-service         ; 定義一個服務名稱
 68         active_checks_enabled           1                       ; Active service checks are enabled
 69         passive_checks_enabled          1                       ; Passive service checks are enabled/accepted
 70         parallelize_check               1                       ; Active service checks should be parallelized;
 71                                                                 ; (disabling this can lead to major performance problems)
 72         obsess_over_service             1                       ; We should obsess over this service (if necessary)
 73         check_freshness                 0                       ; Default is to NOT check service 'freshness'
 74         notifications_enabled           1                       ; Service notifications are enabled
 75         event_handler_enabled           1                       ; Service event handler is enabled
 76         flap_detection_enabled          1                       ; Flap detection is enabled
 77         failure_prediction_enabled      1                       ; Failure prediction is enabled
 78         process_perf_data               1                       ; Process performance data
 79         retain_status_information       1                       ; Retain status information across program restarts
 80         retain_nonstatus_information    1                       ; Retain non-status information across program restarts
 81         is_volatile                     0                       ; The service is not volatile
 82         check_period                    24x7             ; 這裏的check_period告訴nagios檢查服務的時間段。
 83         max_check_attempts              3                ; nagios對服務的最大檢查次數。
 84         normal_check_interval           5                ; 此選項是用來設置服務檢查時間間隔,也就是說,nagios這一次檢查和下一次檢查之間所隔的時間;
 85                                                          ; 這裏是5分鐘。
 86         retry_check_interval            2                ; 重試檢查時間間隔,單位是分鐘。
 87         contact_groups                  ts           ; 指定聯繫人組
 88         notification_options            w,u,c,r          ; 這個定義的是「通知能夠被髮出的狀況」。w即warn,表示警告狀態;
 89                                                          ; u即unknown,表示不明狀態;
 90                                                          ; c即criticle,表示緊急狀態,r即recover,表示恢復狀態;
 91                                                          ; 也就是在服務出現警告狀態、未知狀態、緊急狀態和從新恢復後都發送通知給使用者。
 92         notification_interval           10               ; Re-notify about service problems every hour
 93         notification_period             24x7             ; 指定「發送通知」的時間段,也就是能夠在何時發送通知給使用者。
 94         register                        0                ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
 95         }
 96 define service{
 97         name                            local-service           ; The name of this service template
 98         use                             generic-service         ; Inherit default values from the generic-service definition
 99         max_check_attempts              4             ; Re-check the service up to 4 times in order to determine its final (hard) state
100         normal_check_interval           5             ; Check the service every 5 minutes under normal conditions
101         retry_check_interval            1             ; Re-check the service every minute until a hard state can be determined
102         register                        0             ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
103         }

 

 

b. resource.cfg文件

resource.cfg是nagios的變量定義文件,文件內容只有一行:

$USER1$=/usr/local/nagios/libexec

其中,變量$USER1$指定了安裝nagios插件的路徑,若是把插件安裝在了其它路徑,只需在這裏進行修改便可。須要注意的是,變量必須先定義,而後才能在其它配置文件中進行引用。

 

c. commands.cfg文件

此文件默認是存在的,無需修改便可使用,固然若是有新的命令須要加入時,在此文件進行添加便可。

 1 #notify-host-by-email命令的定義 
 2 define command{
 3         command_name    notify-host-by-email             #命令名稱,即定義了一個主機異常時發送郵件的命令。
 4         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$                                     #命令具體的執行方式。
 5         }
 6 #notify-service-by-email命令的定義 
 7 define command{
 8         command_name    notify-service-by-email          #命令名稱,即定義了一個服務異常時發送郵件的命令
 9         command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
10         }
11 #check-host-alive命令的定義
12 define command{
13         command_name    check-host-alive                 #命令名稱,用來檢測主機狀態。
14         command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5             
15                         # 這裏的變量$USER1$在resource.cfg文件中進行定義,即$USER1$=/usr/local/nagios/libexec;
16                         # 那麼check_ping的完整路徑爲/usr/local/nagios/libexec/check_ping;
17                         # 「-w 3000.0,80%」中「-w」說明後面的一對值對應的是「WARNING」狀態,「80%」是其臨界值。
18                         # 「-c 5000.0,100%」中「-c」說明後面的一對值對應的是「CRITICAL」,「100%」是其臨界值。
19                         # 「-p 1」說明每次探測發送一個包。
20         }
21 define command{
22         command_name    check_local_disk
23         command_line    $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$            #$ARG1$是指在調用這個命令的時候,命令後面的第一個參數。
24         }
25 define command{
26         command_name    check_local_load
27         command_line    $USER1$/check_load -w $ARG1$ -c $ARG2$
28         }
29 define command{
30         command_name    check_local_procs
31         command_line    $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
32         }
33 define command{
34         command_name    check_local_users
35         command_line    $USER1$/check_users -w $ARG1$ -c $ARG2$
36         }
37 define command{
38         command_name    check_local_swap
39         command_line    $USER1$/check_swap -w $ARG1$ -c $ARG2$
40         }
41 define command{
42         command_name    check_ftp
43         command_line    $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$
44         }
45 define command{
46         command_name    check_http
47         command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
48         }
49 define command{
50         command_name    check_ssh
51         command_line    $USER1$/check_ssh $ARG1$ $HOSTADDRESS$
52         }
53 define command{
54         command_name    check_ping
55         command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
56         }
57 define command{
58         command_name    check_nt
59         command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
60         }

 

 

d. hosts.cfg文件

此文件默認不存在,須要手動建立,hosts.cfg主要用來指定被監控的主機地址以及相關屬性信息,根據實驗目標配置以下:

define host{   
        use                     linux-server          #引用主機linux-server的屬性信息,linux-server主機在templates.cfg文件中進行了定義。
        host_name               vserver          #主機名
        alias                   vserver          #主機別名
        address                 192.168.122.232         #被監控的主機地址,這個地址能夠是ip,也能夠是域名。
        }   
#定義一個主機組   
define hostgroup{      
        hostgroup_name          bsmart-servers        #主機組名稱,能夠隨意指定。
        alias                   bsmart servers        #主機組別名
        members                 Nagios-Linux          #主機組成員,其中「Nagios-Linux」就是上面定義的主機。     
        }

 

 注意:在/usr/local/nagios/etc/objects 下默認有localhost.cfg 和windows.cfg 這兩個配置文件,localhost.cfg 文件是定義監控主機自己的,windows.cfg 文件是定義windows 主機的,其中包括了對host 和相關services 的定義。因此在本次實驗中,將直接在localhost.cfg 中定義監控主機(Nagios-Server),在windows.cfg中定義windows 主機(Nagios-Windows)。根據本身的須要修改其中的相關配置,詳細以下:

 1 define host{
 2         use                     linux-server            ; Name of host template to use
 3                                                         ; This host definition will inherit all variables that are defined
 4                                                         ; in (or inherited by) the linux-server host template definition.
 5         host_name               vserver
 6         alias                   vserver
 7         address                 127.0.0.1
 8         }
 9 define hostgroup{
10         hostgroup_name  linux-servers ; The name of the hostgroup
11         alias           Linux Servers ; Long name of the group
12         members         vserver ; Comma separated list of hosts that belong to this group
13         }
14 define service{
15         use                             local-service         ; Name of service template to use
16         host_name                       vserver
17         service_description             PING
18         check_command                   check_ping!100.0,20%!500.0,60%
19         }
20 define service{
21         use                             local-service         ; Name of service template to use
22         host_name                       vserver
23         service_description             Root Partition
24         check_command                   check_local_disk!20%!10%!/
25         }
26 define service{
27         use                             local-service         ; Name of service template to use
28         host_name                       vserver
29         service_description             Current Users
30         check_command                   check_local_users!20!50
31         }
32 define service{
33         use                             local-service         ; Name of service template to use
34         host_name                       vserver
35         service_description             Total Processes
36         check_command                   check_local_procs!250!400!RSZDT
37         }
38 define service{
39         use                             local-service         ; Name of service template to use
40         host_name                       vserver
41         service_description             Current Load
42         check_command                   check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
43         }
44 define service{
45         use                             local-service         ; Name of service template to use
46         host_name                       vserver
47         service_description             Swap Usage
48         check_command                   check_local_swap!20!10
49         }
50 define service{
51         use                             local-service         ; Name of service template to use
52         host_name                       vserver
53         service_description             SSH
54         check_command                   check_ssh
55         notifications_enabled           0
56         }
57 define service{
58         use                             local-service         ; Name of service template to use
59         host_name                       vserver
60         service_description             HTTP
61         check_command                   check_http
62         notifications_enabled           0
63         }

 

 同時也根據參考文章附上windows的,注意 :因爲我附上了windows的的配置但並無添加windown的虛機 因此會在監控時出現相關報警信息

 1 define host{
 2         use             windows-server  ; Inherit default values from a template
 3         host_name       Nagios-Windows  ; The name we're giving to this host
 4         alias           My Windows Server       ; A longer name associated with the host
 5         address         192.168.1.113   ; IP address of the host
 6         }
 7 define hostgroup{
 8         hostgroup_name  windows-servers ; The name of the hostgroup
 9         alias           Windows Servers ; Long name of the group
10         }
11 define service{
12         use                     generic-service
13         host_name               Nagios-Windows
14         service_description     NSClient++ Version
15         check_command           check_nt!CLIENTVERSION
16         }
17 define service{
18         use                     generic-service
19         host_name               Nagios-Windows
20         service_description     Uptime
21         check_command           check_nt!UPTIME
22         }
23 define service{
24         use                     generic-service
25         host_name               Nagios-Windows
26         service_description     CPU Load
27         check_command           check_nt!CPULOAD!-l 5,80,90
28         }
29 define service{
30         use                     generic-service
31         host_name               Nagios-Windows
32         service_description     Memory Usage
33         check_command           check_nt!MEMUSE!-w 80 -c 90
34         }
35 define service{
36         use                     generic-service
37         host_name               Nagios-Windows
38         service_description     C:\ Drive Space
39         check_command           check_nt!USEDDISKSPACE!-l c -w 80 -c 90
40         }
41 define service{
42         use                     generic-service
43         host_name               Nagios-Windows
44         service_description     W3SVC
45         check_command           check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
46         }
47 define service{
48         use                     generic-service
49         host_name               Nagios-Windows
50         service_description     Explorer
51         check_command           check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
52         }

 

e. services.cfg文件

此文件默認也不存在,須要手動建立,services.cfg文件主要用於定義監控的服務和主機資源,例如監控http服務、ftp服務、主機磁盤空間、主機系統負載等等。

 

f. contacts.cfg文件

contacts.cfg是一個定義聯繫人和聯繫人組的配置文件,當監控的主機或者服務出現故障,nagios會經過指定的通知方式(郵件或者短信)將信息發給這裏指定的聯繫人或者使用者。

 1 define contact{
 2         contact_name                    name             #聯繫人的名稱,這個地方不要有空格
 3         use                             generic-contact   #引用generic-contact的屬性信息,其中「generic-contact」在templates.cfg文件中進行定義
 4         alias                           Admin
 5         email                           name@emailadress.cn
 6         }
 7 
 8 define contactgroup{
 9         contactgroup_name       ts                              #聯繫人組的名稱,一樣不能空格
10         alias                   Technical Support               #聯繫人組描述
11         members                 name                           #聯繫人組成員,其中「david」就是上面定義的聯繫人,若是有多個聯繫人則以逗號相隔
12         }

 

g. timeperiods.cfg文件

此文件只要用於定義監控的時間段,下面是一個配置好的實例:

 1 #下面是定義一個名爲24x7的時間段,即監控全部時間段  
 2 define timeperiod{  
 3         timeperiod_name 24x7       #時間段的名稱,這個地方不要有空格
 4         alias           24 Hours A Day, 7 Days A Week  
 5         sunday          00:00-24:00  
 6         monday          00:00-24:00  
 7         tuesday         00:00-24:00  
 8         wednesday       00:00-24:00  
 9         thursday        00:00-24:00  
10         friday          00:00-24:00  
11         saturday        00:00-24:00  
12         }  
13 #下面是定義一個名爲workhours的時間段,即工做時間段。  
14 define timeperiod{  
15         timeperiod_name workhours   
16         alias           Normal Work Hours  
17         monday          09:00-17:00  
18         tuesday         09:00-17:00  
19         wednesday       09:00-17:00  
20         thursday        09:00-17:00  
21         friday          09:00-17:00  
22         }  

 

h. cgi.cfg文件

此文件用來控制相關cgi腳本,若是想在nagios的web監控界面執行cgi腳本,例如重啓nagios進程、關閉nagios通知、中止nagios主機檢測等,這時就須要配置cgi.cfg文件了。
因爲nagios的web監控界面驗證用戶爲name,因此只需在cgi.cfg文件中添加此用戶的執行權限就能夠了,須要修改的配置信息以下:

1 default_user_name=name
2 authorized_for_system_information=nagiosadmin,name
3 authorized_for_configuration_information=nagiosadmin,name
4 authorized_for_system_commands=name
5 authorized_for_all_services=nagiosadmin,name
6 authorized_for_all_hosts=nagiosadmin,name
7 authorized_for_all_service_commands=nagiosadmin,name 
8 authorized_for_all_host_commands=nagiosadmin,name

 

i. nagios.cfg文件

nagios.cfg默認的路徑爲/usr/local/nagios/etc/nagios.cfg,是nagios的核心配置文件,全部的對象配置文件都必須在這個文件中進行定義才能發揮其做用,這裏只需將對象配置文件在Nagios.cfg文件中進行引用便可。

 1 log_file=/usr/local/nagios/var/nagios.log                  # 定義nagios日誌文件的路徑
 2 cfg_file=/usr/local/nagios/etc/objects/commands.cfg        # 「cfg_file」變量用來引用對象配置文件,若是有更多的對象配置文件,在這裏依次添加便可。
 3 cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
 4 cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
 5 cfg_file=/usr/local/nagios/etc/objects/services.cfg
 6 cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
 7 cfg_file=/usr/local/nagios/etc/objects/templates.cfg
 8 cfg_file=/usr/local/nagios/etc/objects/localhost.cfg       # 本機配置文件
 9 cfg_file=/usr/local/nagios/etc/objects/windows.cfg         # windows 主機配置文件
10 object_cache_file=/usr/local/nagios/var/objects.cache      # 該變量用於指定一個「全部對象配置文件」的副本文件,或者叫對象緩衝文件
11 precached_object_file=/usr/local/nagios/var/objects.precache
12 resource_file=/usr/local/nagios/etc/resource.cfg           # 該變量用於指定nagios資源文件的路徑,能夠在nagios.cfg中定義多個資源文件。
13 status_file=/usr/local/nagios/var/status.dat               # 該變量用於定義一個狀態文件,此文件用於保存nagios的當前狀態、註釋和宕機信息等。
14 status_update_interval=10                                  # 該變量用於定義狀態文件(即status.dat)的更新時間間隔,單位是秒,最小更新間隔是1秒。
15 nagios_user=nagios                                         # 該變量指定了Nagios進程使用哪一個用戶運行。
16 nagios_group=nagios                                        # 該變量用於指定Nagios使用哪一個用戶組運行。
17 check_external_commands=1                                  # 該變量用於設置是否容許nagios在web監控界面運行cgi命令;
18                                                            # 也就是是否容許nagios在web界面下執行重啓nagios、中止主機/服務檢查等操做;
19                                                            # 「1」爲運行,「0」爲不容許。
20 command_check_interval=10s                                 # 該變量用於設置nagios對外部命令檢測的時間間隔,若是指定了一個數字加一個"s"(如10s);
21                                                            # 那麼外部檢測命令的間隔是這個數值以秒爲單位的時間間隔;
22                                                            # 若是沒有用"s",那麼外部檢測命令的間隔是以這個數值的「時間單位」的時間間隔。
23 interval_length=60                                         # 該變量指定了nagios的時間單位,默認值是60秒,也就是1分鐘;
24                                                            # 即在nagios配置中全部的時間單位都是分鐘。

 

 5、驗證

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
當初先相似界面則驗證經過
 
Nagios提供的這個驗證功能很是有用,在錯誤信息中一般會打印出錯誤的配置文件以及文件中的哪一行,這使得nagios的配置變得很是容易,報警信息一般是能夠忽略的,由於通常那些只是建議性的。 

看到上面這些信息就說明沒問題了,而後啓動Nagios 服務。

 

6、啓動

# /etc/init.d/nagios start
or
# service nagios start

經過nagios命令的「-d」參數來啓動nagios守護進程:
# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

當修改了配置文件讓其生效時,須要重啓/重載Nagios服務。

經過初始化腳原本重啓nagios

# /etc/init.d/nagios reload
or
# /etc/init.d/nagios restart
or
# service nagios restart

or

經過web監控頁重啓nagios

能夠經過web監控頁的 "Process Info" -> "Restart the Nagios process"來重啓nagios

 

接下來會重點研究 NSCA(被動監控),NRPE(遠程監控插件), cacti(界面強化)

相關文章
相關標籤/搜索