nagios的另一強大功能是它能同時監測主機和服務。例如,它可以同時監測到ip位址和tcp/udp端口号。為進一步闡述此功能,我們假定有台需要監測的web伺服器,nagios可運用在伺服器上基于ip/伺服器名運作ping指令的方法檢測伺服器是否線上,同時當伺服器的rtt(往返時延)增加時,nagios會随時告警。另外,nagios還能檢測tcp的80端口(web伺服器)是否可達,比如可能出現伺服器線上但apache/iis沒有響應的情況。
本段教程将展示linux下nagios的安裝與配置.
<a target="_blank"></a>
debian系統使用者使用apt-get指令就可安裝nagios:
root@mrtg:~# apt-get install nagios3
nagios安裝過程中可以設定郵件伺服器,安裝後也可以進行自定義設定。
提示:為使nagios可正常發送郵件通知,需正确配置smtp。

可以看到nagios支援多種郵件傳送方式。最常見的莫過于伺服器(internet site)直接向接收者發送郵件,另一種常見的形式是用智能主機或中繼伺服器,将郵件傳送至中繼郵件伺服器,然後它負責将郵件傳送給接收者。
進行下一步操作時伺服器的域名需要包含進去。
最後,設定一下nagios管理者‘nagiosadmin’的密碼,使用者可以之後自行修改密碼。
[root@mrtg ~]# yum install nagios nagios-plugins
本段教程中,我們希望完成以下監測内容:
每三分鐘監測一次所有linux伺服器。
每三分鐘監測一次所有思科路由器。
發出告警前nagios都将進行3次驗證以確定問題确有發生。
當裝置的往返時延超過100ms并且/或包丢失量超過20%将發出郵件告警。
餘下的教程将指導您如何在linux系統中配置nagios。
明确nagios配置檔案的位址至關重要,以下路徑指明了debian系統下nagios的配置檔案位址。
/etc/nagios-plugins 實作監測功能的專有腳本存放位址
/etc/nagios3 添加主機、服務,定義檢測和定時器的配置檔案
/usr/lib/nagios/plugins 用于監測的可執行檔案
接下來的步驟互相關聯,由此開始定義主機,主機組及向主機組添加服務操作。
我們對同一類型的主機定義了對應的模闆,這裡使用安裝檔案作舉例說明。
首先,為linux裝置定義主機模闆。
root@mrtg:~# cd /etc/nagios3/conf.d
root@mrtg:/etc/nagios3/conf.d/# cp generic-host_nagios2.cfg linux-server.cfg
如下編輯linux-server.cfg。需要修改部分已經标出。
root@mrtg:/etc/nagios3/conf.d/# vim linux-server.cfg
-
define host{
name linux-server ; 名稱,需修改
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
check_command example-host-check ; 檢查所用腳本,需修改
check_interval 3 ; 連續檢查的間隔,需修改
max_check_attempts 3 ; 産生郵件告警前的自檢次數,需修改
notification_interval 0
notification_period 24x7
notification_options d,u,r
contact_groups admins ; 郵件将要發送至的組,需修改
register0
}
接下來,為cisco裝置定義主機模闆。
root@mrtg:/etc/nagios3/conf.d/# cp linux-server.cfg cisco-device.cfg
如下修改cisco-device.cfg。需要修改部分已經标出。
root@mrtg:/etc/nagios3/conf.d/# vim cisco-device.cfg
name cisco-device ;名稱,需修改
check_command example-host-check ; 檢查時使用的腳本,需修改
check_interval 3 ; 連續檢查間隔,需修改
max_check_attempts 3 ; 産生郵件告警前的自檢次數,需修改
notification_period 24x7
contact_groups admins ; 郵件将要發至的組,需修改
register 0
現在已定義主機模闆,就可以添加需要監測的主機。以預設的檔案作例子展示如下内容。
root@mrtg:/etc/nagios3/conf.d/# cp localhost_nagios2.cfg example.cfg
root@mrtg:/etc/nagios3/conf.d/# vim example.cfg
# host 1
use linux-server ; 使用的主機模闆名
host_name our-server ; nagios使用的主機名
alias our-server
address 172.17.1.23 ; 主機的ip位址
# host 2
use cisco-device ; 使用的主機模闆名
host_name our-router ; nagios使用的主機名
alias our-router
address 172.17.1.1 ; 主機的ip位址
當有多個主機時,為友善管理,建議将相似的主機組成一組。
root@mrtg:/etc/nagios3/conf.d/# vim hostgroups_nagios2.cfg
define hostgroup {
hostgroup_name linux-server ; 主機組名
alias linux servers
members our-server ; 組員清單
hostgroup_name cisco-device ; 主機組名
alias cisco devices
members our-server ; comma separated list of members
首先,定義一個服務example-host-check:當往返時延達到100ms預警值并且有20%包丢失時發出警告,而緊急告警設定為5000ms且包丢失比率為100%,隻執行一個ipv4的ping請求檢測。
root@mrtg:~# vim /etc/nagios-plugins/config/ping.cfg
define command{
command_name example-host-check
command_line /usr/lib/nagios/plugins/check_ping -h '$hostaddress$' -w 100,20% -c 5000,100% -p 1 -4
然後,将指令關聯至主機組。
root@mrtg:/etc/nagios3/conf.d/# vim services_nagios2.cfg
define service {
hostgroup_name linux-server
service_description linux servers
check_command example-host-check
use generic-service
notification_interval 0 ; 初始化設定為0
hostgroup_name cisco-device
service_description cisco devices
進行如下定義将發送郵件需要的位址添加至nagios。
root@mrtg:/etc/nagios3/conf.d/# vim contacts.cfg
define contact{
contact_name root
alias root
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email root@localhost, [email protected]
最後,試運作初始化檢測是否有配置錯誤。如果沒有錯誤,nagios開始安全運作。
root@mrtg:~# nagios -v /etc/nagios3/nagios.cfg
root@mrtg:~# service nagios3 restart
redhat系統中nagios的配置檔案位址如下所示。
/etc/nagios/objects 添加主機、服務,定義監測和定時器的配置檔案
/usr/lib/nagios/plugins 實作監測的可執行檔案
為特定類型的主機建立所需的模闆,相應修改安裝所需的檔案。
[root@mrtg objects]# cd /etc/nagios/objects/
[root@mrtg objects]# vim templates.cfg
name linux-server
use generic-host
check_period 24x7
check_interval 3
retry_interval 1
max_check_attempts 3
check_command example-host-check
notification_period 24x7
notification_interval 0
notification_options d,u,r
contact_groups admins
register 0
name cisco-router
這裡以預設的配置檔案為例,主機和主機組添加至配置檔案中。
[root@mrtg objects]# cp localhost.cfg example.cfg
[root@mrtg objects]# vim example.cfg
#adding linux server
use linux-server
host_name our-server
alias our-server
address 172.17.1.23
#adding cisco router
use cisco-router
host_name our-router
alias our-router
address 172.17.1.1
# host group definition
define hostgroup{
hostgroup_name linux-servers
members our-server
hostgroup_name cisco-router
alias cisco-router
members our-router
定義一個服務example-host-check:當往返時延達到100ms預警值并且有20%包丢失時發出警告,而緊急告警設定為5000ms且包丢失比率為100%,隻執行一個ipv4的ping請求檢測。
[root@mrtg objects]# vim commands.cfg
command_line $user1$/check_ping -h $hostaddress$ -w 100.0,20% -c 5000.0,100% -p 1 -4
告警要發送的郵件位址添加至nagios中。
[root@objects objects]# vim contacts.cfg
contact_name nagiosadmin
use generic-contact
alias nagios admin
email nagios@localhost, [email protected]
最後,準備啟動nagios服務,可先試運作檢測配置是否出錯。
[root@mrtg ~]# nagios –v /etc/nagios/nagios.cfg
[root@mrtg ~]# service nagios restart
[root@mrtg ~]# chkconfig nagios on
若nagios沒有依原設定運作,首先要做的是建立一個測試運作(dry run)。
debian或ubuntu系統:
# nagios3 -v /etc/nagios3/nagios.cfg
centos或rhel系統:
# nagios -v /etc/nagios/nagios.cfg
原文釋出時間為:2014-01-03
本文來自雲栖社群合作夥伴“linux中國”