Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Nagios 配置全流程詳解:從模板、宏到主機與服務監控

一、預設配置檔案有哪些

Nagios 安裝完成後,預設配置位於 /usr/local/nagios/etc 目錄下。每個檔案或目錄各有分工,先看清楚這張分佈圖,後面的配置就不容易迷路。

Nagios 配置全流程詳解:從模板、宏到主機與服務監控

二、配置檔案之間的引用關係

Nagios 配置中涉及的核心定義有:主機、主機組、服務、服務組、聯繫人、聯繫人組、監控時間段與監控命令。這些定義彼此引用、互相依賴,因此配好一台 Nagios 的關鍵,是搞清誰依賴誰。最要緊的四件事是:

  1. 定義要監控哪些主機、主機組、服務與服務組。
  2. 定義這些監控用什麼命令來實現。
  3. 定義監控的時間段。
  4. 定義主機或服務出問題時要通知哪些聯繫人與聯繫人組。

三、開始配置

為了清楚好維護,建議把各類定義拆成獨立檔案:自建 hosts.cfg 定義主機與主機組;自建 services.cfg 定義服務;沿用預設的 contacts.cfg 定義聯繫人與聯繫人組、commands.cfg 定義命令、timeperiods.cfg 定義時間段,以及 templates.cfg 作為資源引用(模板)檔。

(1)templates.cfg:把共性抽成模板

Nagios 主要監控主機資源與服務,這些被監控的東西統稱為「對象」。為了不必重複定義共同屬性,Nagios 引入了模板機制,把共性屬性定義成模板供反覆引用。

define contact{
    name                            generic-contact
    service_notification_period     24x7
    host_notification_period        24x7
    service_notification_options    w,u,c,r
    host_notification_options       d,u,r
    service_notification_commands   notify-service-by-email
    host_notification_commands      notify-host-by-email
    register                        0
}

參數含義:service_notification_period 與 host_notification_period 分別定義服務與主機異常時發送通知的時間段(24x7 在 timeperiods.cfg 中定義);service_notification_options 的 w/u/c/r 分別是警告、未知、緊急、恢復,代表這四種狀態都要通知;host_notification_options 的 d/u/r 則是宕機、不可達、恢復;兩個 notification_commands 指定通知方式(此處為郵件),命令本體在 commands.cfg 中定義;最後 register 0 表示這只是模板,不生成實際對象。

define host{
    name                    generic-host
    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
    notification_period     24x7
    register                0
}
define host{
    name                linux-server
    use                 generic-host
    check_period        24x7
    check_interval      5
    retry_interval      1
    max_check_attempts  10
    check_command       check-host-alive
    notification_period workhours
    notification_interval 120
    notification_options d,u,r
    contact_groups      admins
    register            0
}

use 表示引用,把 generic-host 的屬性全部繼承到 linux-server 中,這是 Nagios 配置裡最常用的技巧。check_interval 是主機檢查間隔(5 分鐘),retry_interval 是重試間隔(1 分鐘),max_check_attempts 是最大檢查次數(10 次)——之所以要多試幾次,是因為偶爾的網路擁塞也可能讓主機短暫失聯,不宜立刻判定異常。notification_interval 是故障未解決時再次通知的間隔(120 分鐘),若認為只需通知一次,可設為 0。contact_groups admins 在 contacts.cfg 中定義。

define service{
    name                    generic-service
    active_checks_enabled   1
    passive_checks_enabled  1
    parallelize_check       1
    obsess_over_service     1
    check_freshness         0
    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
    is_volatile             0
    check_period            24x7
    max_check_attempts      3
    normal_check_interval   10
    retry_check_interval    2
    contact_groups          admins
    notification_options    w,u,c,r
    notification_interval   60
    notification_period     24x7
    register                0
}

(2)resource.cfg:變量定義

內容只有一行,指定 Nagios 插件的安裝路徑:

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

若插件裝在別處,改這裡即可。變量必須先定義,才能在其它配置檔中被引用。

(3)理解 Nagios 巨集(macro)與它的工作機制

Nagios 配置非常靈活,繼承與引用是一大特徵,另一大特徵是可以在命令定義中使用巨集,藉此靈活取得主機、服務等對象的資訊。

工作機制:執行命令之前,Nagios 會把命令中的每個巨集替換成它應取得的值。這種替換發生在檢測、通知、事件處理等所有環節。巨集主要分為預設巨集、按需巨集與自訂巨集。

例如主機定義使用 check_ping:

define host{
    host_name     ixdba
    address       192.168.12.246
    check_command check_ping
}

define command{
    command_name check_ping
    command_line /usr/local/nagios/libexec/check_ping -H $HOSTADDRESS$ -w 100.0,90% -c 200.0,60%
}

實際展開後執行的是:

/usr/local/nagios/libexec/check_ping -H 192.168.12.246 -w 100.0,90% -c 200.0,60%

也可以向命令傳參,讓命令定義更通用。參數定義在對象中,用 ! 分隔:

define service{
    host_name         linuxbox
    service_description PING
    check_command     check_ping!200.0,80%!400.0,40%
}

此時 $ARG1$ 是 200.0,80%,$ARG2$ 是 400.0,40%(都不含引號)。配合這樣的命令定義:

define command{
    command_name check_ping
    command_line /usr/local/nagios/libexec/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
}

最終執行:

/usr/local/nagios/libexec/check_ping -H 192.168.12.246 -w 200.0,80% -c 400.0,40%

常用巨集速查:

  • 主機:$HOSTNAME$(主機簡稱,取自 host_name)、$HOSTADDRESS$(取自 address)。
  • 服務:$SERVICESTATE$(服務狀態 w/u/c)、$SERVICEDESC$(服務描述)。
  • 聯繫人:$CONTACTNAME$。
  • 通知:$NOTIFICATIONTYPE$,取值如 PROBLEM、RECOVERY、ACKNOWLEDGEMENT、FLAPPINGSTART/STOP、DOWNTIMESTART/END 等。
  • 日期時間:$LONGDATETIME$。
  • 檔案:$LOGFILE$、$MAINCONFIGFILE$。
  • 其他:$ADMINEMAIL$(全域管理員信箱)、$ARGn$(第 n 個命令參數,最多支援 32 個)。

(4)commands.cfg:命令定義

此檔預設存在,通常不需修改;有新命令時再往裡頭加即可。

define command{
    command_name    notify-host-by-email
    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$
}

define command{
    command_name    notify-service-by-email
    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$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

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_ftp
    command_line    $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$
}

define command{
    command_name    check_http
    command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
}

define command{
    command_name    check_ssh
    command_line    $USER1$/check_ssh $ARG1$ $HOSTADDRESS$
}

其中 $USER1$ 來自 resource.cfg。-w 後的一對值對應 WARNING 臨界值,-c 對應 CRITICAL,-p 指定每次探測發送的封包數。$ARG1$ 是呼叫命令時傳入的第一個參數。

(5)hosts.cfg:定義主機與主機組

此檔預設不存在,需自行建立。

define host{
    use         linux-server      # 引用 templates.cfg 中定義的 linux-server
    host_name   web
    alias       ixdba-web
    address     192.168.12.251    # 可以是 IP,也可以是網域名稱
}

define host{
    use         linux-server
    host_name   mysql
    alias       ixdba-mysql
    address     192.168.12.237
}

define hostgroup{
    hostgroup_name  sa-servers
    alias           sa servers
    members         web,mysql
}

(6)services.cfg:定義要監控的服務

define service{
    use                 local-service   # 引用 templates.cfg 中的 local-service
    host_name           web             # 該主機在 hosts.cfg 中定義
    service_description PING
    check_command       check_ping!100.0,20%!500.0,60%
}

define service{
    use                 local-service
    host_name           web
    service_description SSH
    check_command       check_ssh
}

define service{
    use                 local-service
    host_name           web
    service_description SSHD
    check_command       check_tcp!22
}

命令與參數之間以 ! 分隔,參數會對應到 $ARG1$、$ARG2$。除了服務,磁碟空間、系統負載等主機資源也可以用同樣方式監控。

(7)contacts.cfg:聯繫人與聯繫人組

當被監控的主機或服務出故障時,Nagios 會依指定方式(郵件或簡訊)把訊息發給這裡設定的聯繫人。

define contact{
    contact_name    sasystem
    use             generic-contact
    alias           sa-system
    email           ixdba@126.com
}

define contactgroup{
    contactgroup_name   admins
    alias               system administrator group
    members             sasystem
}

(8)timeperiods.cfg:監控時間段

define timeperiod{
    timeperiod_name 24x7
    alias           24 Hours A Day, 7 Days A Week
    sunday          00:00-24:00
    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
}

define timeperiod{
    timeperiod_name workhours
    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
}

(9)cgi.cfg:Web 介面的操作權限

若希望在 Web 監控介面上執行 cgi 腳本(重啟 Nagios 進程、關閉通知、停止檢測等),就必須配置這個檔案。由於 Web 介面驗證的使用者是 ixdba,只需給該使用者相應權限:

default_user_name=ixdba
authorized_for_system_information=nagiosadmin,ixdba
authorized_for_configuration_information=nagiosadmin,ixdba
authorized_for_system_commands=ixdba
authorized_for_all_services=nagiosadmin,ixdba
authorized_for_all_hosts=nagiosadmin,ixdba
authorized_for_all_service_commands=nagiosadmin,ixdba
authorized_for_all_host_commands=nagiosadmin,ixdba

(10)nagios.cfg:核心配置檔

路徑為 /usr/local/nagios/etc/nagios.cfg。所有對象配置檔都必須在這裡被引用,否則不生效。

log_file=/usr/local/nagios/var/nagios.log          # 日誌路徑

cfg_file=/usr/local/nagios/etc/hosts.cfg           # 引用各對象配置檔
cfg_file=/usr/local/nagios/etc/services.cfg
cfg_file=/usr/local/nagios/etc/commands.cfg
cfg_file=/usr/local/nagios/etc/contacts.cfg
cfg_file=/usr/local/nagios/etc/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/templates.cfg

object_cache_file=/usr/local/nagios/var/objects.cache   # 對象快取檔
resource_file=/usr/local/nagios/etc/resource.cfg        # 資源檔,可定義多個
status_file=/usr/local/nagios/var/status.dat            # 狀態檔:當前狀態、註解、宕機資訊
status_update_interval=10                               # 狀態檔更新間隔(秒),最小 1 秒

nagios_user=nagios                # Nagios 進程的執行使用者
nagios_group=nagios               # Nagios 進程的執行群組

check_external_commands=1         # 是否允許在 Web 介面執行 cgi 命令,1 允許、0 不允許
command_check_interval=2          # 外部命令檢查間隔;加 s(如 10s)則以秒為單位
interval_length=60                # 時間單位,預設 60 秒,即所有時間配置都以分鐘為單位

把這十個檔案的關係串起來,你就有了一套可維護、可擴充的 Nagios 監控骨架:模板提供共性,hosts 與 services 描述監控對象,commands 提供手段,timeperiods 約束時間,contacts 決定通知去向,而 nagios.cfg 負責把一切串接起來。

Copyright © Linux教程網 All Rights Reserved