溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Linux下Nagios的安裝與配置<< 一 >>

發(fā)布時間:2020-10-14 16:31:06 來源:網(wǎng)絡 閱讀:452 作者:echo_zou 欄目:移動開發(fā)

一、Nagios簡介

注:以下文檔跟據(jù)網(wǎng)上下載文檔進行稍做修改,聯(lián)系人使用組,主機使用組,供參考!

Nagios是一款開源的電腦系統(tǒng)和網(wǎng)絡監(jiān)視工具,能有效監(jiān)控Windows、LinuxUnix的主機狀態(tài),交換機路由器等網(wǎng)絡設置,打印機等。在系統(tǒng)或服務狀態(tài)異常時發(fā)出郵件或短信報警第一時間通知網(wǎng)站運維人員,在狀態(tài)恢復后發(fā)出正常的郵件或短信通知。

Nagios原名為NetSaint,由Ethan Galstad開發(fā)并維護至今。NAGIOS是一個縮寫形式: "NagiosAin't Gonna Insist On Sainthood" Sainthood 翻譯為圣徒,而"Agios""saint"的希臘表示方法。Nagios被開發(fā)在Linux下使用,但在Unix下也工作得非常好。

主要功能

·網(wǎng)絡服務監(jiān)控(SMTP、POP3HTTP、NNTP、ICMP、SNMP、FTP、SSH

·主機資源監(jiān)控(CPU loaddisk usage、system logs),也包括Windows主機(使用NSClient++plugin

·可以指定自己編寫的Plugin通過網(wǎng)絡收集數(shù)據(jù)來監(jiān)控任何情況(溫度、警告……

·可以通過配置Nagios遠程執(zhí)行插件遠程執(zhí)行腳本

·遠程監(jiān)控支持SSHSSL加通道方式進行監(jiān)控

·簡單的plugin設計允許用戶很容易的開發(fā)自己需要的檢查服務,支持很多開發(fā)語言(shell scripts、C++Perl、ruby、PythonPHP、C#等)

·包含很多圖形化數(shù)據(jù)PluginsNagiosgraphNagiosgrapher、PNP4Nagios等)

·可并行服務檢查

·能夠定義網(wǎng)絡主機的層次,允許逐級檢查,就是從父主機開始向下檢查

·當服務或主機出現(xiàn)問題時發(fā)出通告,可通過email, pager, sms 或任意用戶自定義的plugin進行通知

·能夠自定義事件處理機制重新激活出問題的服務或主機

·自動日志循環(huán)

·支持冗余監(jiān)控

·包括Web界面可以查看當前網(wǎng)絡狀態(tài),通知,問題歷史,日志文件等

二、Nagios工作原理

Nagios的功能是監(jiān)控服務和主機,但是他自身并不包括這部分功能,所有的監(jiān)控、檢測功能都是通過各種插件來完成的。

  啟動Nagios后,它會周期性的自動調(diào)用插件去檢測服務器狀態(tài),同時Nagios會維持一個隊列,所有插件返回來的狀態(tài)信息都進入隊列,Nagios每次都從隊首開始讀取信息,并進行處理后,把狀態(tài)結(jié)果通過web顯示出來。

Nagios提供了許多插件,利用這些插件可以方便的監(jiān)控很多服務狀態(tài)。安裝完成后,在nagios主目錄下的/libexec里放有nagios自帶的可以使用的所有插件,如,check_disk是檢查磁盤空間的插件,check_load是檢查CPU負載的,等等。每一個插件可以通過運行./check_xxx –h 來查看其使用方法和功能。

Nagios可以識別4種狀態(tài)返回信息,即 0(OK)表示狀態(tài)正常/綠色、1(WARNING)表示出現(xiàn)警告/×××、2(CRITICAL)表示出現(xiàn)非常嚴重的錯誤/紅色、3(UNKNOWN)表示未知錯誤/深×××。Nagios根據(jù)插件返回來的值,來判斷監(jiān)控對象的狀態(tài),并通過web顯示出來,以供管理員及時發(fā)現(xiàn)故障。

四種監(jiān)控狀態(tài)

  再說報警功能,如果監(jiān)控系統(tǒng)發(fā)現(xiàn)問題不能報警那就沒有意義了,所以報警也是nagios很重要的功能之一。但是,同樣的,Nagios 自身也沒有報警部分的代碼,甚至沒有插件,而是交給用戶或者其他相關開源項目組去完成的。

Nagios 安裝,是指基本平臺,也就是Nagios軟件包的安裝。它是監(jiān)控體系的框架,也是所有監(jiān)控的基礎。

  打開Nagios官方的文檔,會發(fā)現(xiàn)Nagios基本上沒有什么依賴包,只要求系統(tǒng)是Linux或者其他Nagios支持的系統(tǒng)。不過如果你沒有安裝apachehttp服務),那么你就沒有那么直觀的界面來查看監(jiān)控信息了,所以apache姑且算是一個前提條件。關于apache的安裝,網(wǎng)上有很多,照著安裝就是了。安裝之后要檢查一下是否可以正常工作。

  知道Nagios 是如何通過插件來管理服務器對象后,現(xiàn)在開始研究它是如何管理遠端服務器對象的。Nagios 系統(tǒng)提供了一個插件NRPE。Nagios 通過周期性的運行它來獲得遠端服務器的各種狀態(tài)信息。它們之間的關系如下圖所示:

Nagios 通過NRPE 來遠端管理服務

1. Nagios 執(zhí)行安裝在它里面的check_nrpe 插件,并告訴check_nrpe 去檢測哪些服務。

2. 通過SSL,check_nrpe 連接遠端機子上的NRPE daemon

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

4. 最后,NRPE 把檢測的結(jié)果傳給主機端的check_nrpecheck_nrpe 再把結(jié)果送到Nagios狀態(tài)隊列中。

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

三、實驗環(huán)境

Host Name

OS

IP

Software

Nagios-Server

CentOS release 6.3 (Final)

192.168.1.108

Apache、Php、Nagios、nagios-plugins

Nagios-Linux

CentOS release 5.8 (Final)

192.168.1.111

nagios-plugins、nrpe

Nagios-Windows

Windows XP

192.168.1.113

NSClient++

Server 安裝了nagios軟件,對監(jiān)控的數(shù)據(jù)做處理,并且提供web界面查看和管理。當然也可以對本機自身的信息進行監(jiān)控。

Client 安裝了NRPE等客戶端,根據(jù)監(jiān)控機的請求執(zhí)行監(jiān)控,然后將結(jié)果回傳給監(jiān)控機。

防火墻已關閉/iptables: Firewall is not running.

SELINUX=disabled

四、實驗目標

Linux下Nagios的安裝與配置<< 一 >>

五、Nagios服務端安裝

5.1 基礎支持套件:gcc glibc glibc-common gd gd-develxinetd openssl-devel

# rpm -q gccglibc glibc-common gd gd-devel xinetd openssl-devel

如果系統(tǒng)中沒有這些套件,使用yum 安裝

# yum install -ygcc glibc glibc-common gd gd-devel xinetd openssl-devel

5.2 創(chuàng)建nagios用戶和用戶組

#useradd -s /sbin/nologin nagios

#mkdir /usr/local/nagios

#chown -R nagios.nagios /usr/local/nagios

查看nagios 目錄的權限

# ll -d/usr/local/nagios/

5.3 編譯安裝Nagios

# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.3.tar.gz

# tar zxvfnagios-3.4.3.tar.gz

# cd nagios

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

# make all

# make install

# makeinstall-init

# makeinstall-commandmode

# makeinstall-config

# chkconfig--add nagios

# chkconfig--level 35 nagios on

# chkconfig--list nagios

5.4 驗證程序是否被正確安裝

切換目錄到安裝路徑(這里是/usr/local/nagios),看是否存在etc、binsbin、share、var 這五個目錄,如果存在則可以表明程序被正確的安裝到系統(tǒng)了。Nagios 各個目錄用途說明如下:

bin

Nagios 可執(zhí)行程序所在目錄

etc

Nagios 配置文件所在目錄

sbin

Nagios CGI 文件所在目錄,也就是執(zhí)行外部命令所需文件所在的目錄

share

Nagios網(wǎng)頁文件所在的目錄

libexec

Nagios 外部插件所在目錄

var

Nagios 日志文件、lock 等文件所在的目錄

var/archives

Nagios 日志自動歸檔目錄

var/rw

用來存放外部命令文件的目錄


5.5 安裝Nagios 插件

# wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.16.tar.gz

# tar zxvfnagios-plugins-1.4.16.tar.gz

# cdnagios-plugins-1.4.16

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

# make&& make install

5.6 安裝與配置ApachePhp

Apache Php 不是安裝nagios 所必須的,但是nagios提供了web監(jiān)控界面,通過web監(jiān)控界面可以清晰的看到被監(jiān)控主機、資源的運行狀態(tài),因此,安裝一個web服務是很必要的。
需要注意的是,nagiosnagios3.1.x版本以后,配置web監(jiān)控界面時需要php的支持。這里我們下載的nagios版本為nagios-3.4.3,因此在編譯安裝完成apache后,還需要編譯php模塊,這里選取的php版本為php5.4.10。

a. 安裝Apache

# wget http://labs.mop.com/apache-mirror//httpd/httpd-2.2.23.tar.gz

# tar zxvfhttpd-2.2.23.tar.gz

# cdhttpd-2.2.23

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

# make&& make install

若出現(xiàn)錯誤:

Linux下Nagios的安裝與配置<< 一 >>

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

b. 安裝Php

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

# tar zxvfphp-5.4.10.tar.gz

# cd php-5.4.10

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

安裝php時的報錯

checking libxml2install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation

檢查是否安裝了libxm

[root@XKWB3403php-5.3.8]# rpm -qa |grep libxml2
libxml2-2.6.26-2.1.12
libxml2-python-2.6.26-2.1.12

重新安裝libxml2libxml2-devel

yum install libxml2

yum install -ylibxml2-devel

安裝完之后查找xml2-config文件是否存在

[root@XKWB3403php-5.3.8]# find / -name "xml2-config"
/usr/bin/xml2-config

如果存在的話重新安裝php

[root@XKWB3403php-5.3.8]# ./configure

安裝成功的標志是Linux下Nagios的安裝與配置<< 一 >>之后再進行編譯

make &&make install

# make&& make install

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

Userdaemon

Groupdaemon

修改為

Usernagios

Groupnagios

然后找到

<IfModule dir_module>

DirectoryIndex index.html

</IfModule>

修改為接著增加如下內(nèi)容: AddTypeapplication/x-httpd-php .php ,注意httpd-PHP后面空格。

<IfModule dir_module>

DirectoryIndexindex.html index.php

AddTypeapplication/x-httpd-php .php

</IfModule>

為了安全起見,一般情況下要讓nagios web 監(jiān)控頁面必須經(jīng)過授權才能訪問,這需要增加驗證配置,即在httpd.conf 文件最后添加如下信息:


#settingfor 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>


d. 創(chuàng)建apache目錄驗證文件

在上面的配置中,指定了目錄驗證文件htpasswd,下面要創(chuàng)建這個文件:

#/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd david


這樣就在/usr/local/nagios/etc目錄下創(chuàng)建了一個htpasswd 驗證文件,當通過http://192.168.1.108/nagios/訪問時就需要輸入用戶名(david)和密碼(剛設置的密碼)了。

e. 查看認證文件的內(nèi)容

# cat/usr/local/nagios/etc/htpasswd

f. 啟動apache 服務

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

到這里nagios 的安裝也就基本完成了,你可以通過web來訪問了。

Linux下Nagios的安裝與配置<< 一 >>

Linux下Nagios的安裝與配置<< 一 >>

六、配置Nagios

Nagios 主要用于監(jiān)控一臺或者多臺本地主機及遠程的各種信息,包括本機資源及對外的服務等。默認的Nagios 配置沒有任何監(jiān)控內(nèi)容,僅是一些模板文件。若要讓Nagios 提供服務,就必須修改配置文件,增加要監(jiān)控的主機和服務,下面將詳細介紹。

6.1 默認配置文件介紹

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

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




文件名或目錄名

用途

cgi.cfg

控制CGI訪問的配置文件

nagios.cfg

Nagios 主配置文件

resource.cfg

變量定義文件,又稱為資源文件,在些文件中定義變量,以便由其他配置文件引用,如$USER1$

objects

objects 是一個目錄,在此目錄下有很多配置文件模板,用于定義Nagios 對象

objects/commands.cfg

命令定義配置文件,其中定義的命令可以被其他配置文件引用

objects/contacts.cfg

定義聯(lián)系人和聯(lián)系人組的配置文件

objects/localhost.cfg

定義監(jiān)控本地主機的配置文件

objects/printer.cfg

定義監(jiān)控打印機的一個配置文件模板,默認沒有啟用此文件

objects/switch.cfg

定義監(jiān)控路由器的一個配置文件模板,默認沒有啟用此文件

objects/templates.cfg

定義主機和服務的一個模板配置文件,可以在其他配置文件中引用

objects/timeperiods.cfg

定義Nagios 監(jiān)控時間段的配置文件

objects/windows.cfg

監(jiān)控Windows 主機的一個配置文件模板,默認沒有啟用此文件


6.2 配置文件之間的關系

nagios的配置過程中涉及到的幾個定義有:主機、主機組,服務、服務組,聯(lián)系人、聯(lián)系人組,監(jiān)控時間,監(jiān)控命令等,從這些定義可以看出,nagios各個配置文件之間是互為關聯(lián),彼此引用的。

成功配置出一臺nagios監(jiān)控系統(tǒng),必須要弄清楚每個配置文件之間依賴與被依賴的關系,最重要的有四點:

第一:定義監(jiān)控哪些主機、主機組、服務和服務組;
第二:定義這個監(jiān)控要用什么命令實現(xiàn);
第三:定義監(jiān)控的時間段;
第四:定義主機或服務出現(xiàn)問題時要通知的聯(lián)系人和聯(lián)系人組。

6.3 配置Nagios

首先新建一個目錄,名稱為:servers ,把objects下面的文件拷貝一份至servers下面(本實驗不監(jiān)控switchprinter,故printer.cfg,switch.cfg這兩個文件不用拷貝)。

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

·創(chuàng)建hosts.cfg文件來定義主機和主機組

·創(chuàng)建services.cfg文件來定義服務

·用默認的contacts.cfg文件來定義聯(lián)系人和聯(lián)系人組

·用默認的commands.cfg文件來定義命令

·用默認的timeperiods.cfg來定義監(jiān)控時間段

·用默認的templates.cfg文件作為資源引用文件

a. templates.cfg文件

nagios主要用于監(jiān)控主機資源以及服務,在nagios配置中稱為對象,為了不必重復定義一些監(jiān)控對象,Nagios引入了一個模板配置文件,將一些共性的屬性定義成模板,以便于多次引用。這就是templates.cfg的作用。

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

###############################################################################

# TEMPLATES.CFG - SAMPLE OBJECT TEMPLATES

#

# Last Modified: 10-03-2007

#

# NOTES: This config file provides you withsome example object definition

# templates that are refered by other host, service, contact, etc.

# definitions in other config files.

#

# You don't need to keep these definitions in a separate file from your

# other object definitions. Thishas been done just to make things

# easier to understand.

#

###################################################################

#

# CONTACT TEMPLATES

#

###############################################################################

###############################################################################


# Generic contact definition template - Thisis NOT a real contact, just a template!


define contact{

name generic-contact ; The nameof this contact template

service_notification_period 24x7 ; service notifications can be sent anytime

host_notification_period 24x7 ; hostnotifications can be sent anytime

service_notification_options w,u,c,r,f,s ; send notifications for all service states,flapping events, and scheduled downtime events

host_notification_options d,u,r,f,s ; sendnotifications for all host states, flapping events, and scheduled downtimeevents

service_notification_commands notify-service-by-email ; send service notifications via email

host_notification_commands notify-host-by-email ; sendhost notifications via email

register 0 ; DONTREGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

}


###############################################################################

#

# HOST TEMPLATES

#

###############################################################################


# Generic host definition template - This isNOT a real host, just a template!


define host{

name generic-host ; The name of thishost template

notifications_enabled 1 ; Hostnotifications are enabled

event_handler_enabled 1 ; Host eventhandler is enabled

flap_detection_enabled 1 ; Flap detectionis enabled

failure_prediction_enabled 1 ; Failureprediction is enabled

process_perf_data 1 ; Processperformance data

retain_status_information 1 ; Retain statusinformation across program restarts

retain_nonstatus_information 1 ; Retain non-statusinformation across program restarts

notification_period 24x7 ; Send host notificationsat any time

register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT AREAL HOST, JUST A TEMPLATE!

}



# Linux host definition template - This isNOT a real host, just a template!


define host{

name linux-server ; The name of thishost template

use generic-host ; This templateinherits other values from the generic-host template

check_period 24x7 ; By default, Linux hosts are checked roundthe clock

check_interval 5 ; Actively checkthe host every 5 minutes

retry_interval 1 ; Schedule hostcheck retries at 1 minute intervals

max_check_attempts 10 ; Check each Linuxhost 10 times (max)

check_command check-host-alive ; Default command to check Linux hosts

notification_period workhours ; Linux adminshate to be woken up, so we only notify during the day

; Note that the notification_period variable is being overridden from

; the value that is inherited from the generic-host template!

notification_interval 120 ; Resendnotifications every 2 hours

notification_options d,u,r ; Only sendnotifications for specific host states

contact_groups linux contact ; Notifications get sent to the admins bydefault

register 0 ; DONT REGISTERTHIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!

}


# Windows host definition template - This isNOT a real host, just a template!


define host{

name windows-server ; The name of thishost template

use generic-host ; Inherit defaultvalues from the generic-host template

check_period 24x7 ; By default, Windows servers aremonitored round the clock

check_interval 5 ; Actively check the serverevery 5 minutes

retry_interval 1 ; Schedule host check retries at1 minute intervals

max_check_attempts 10 ; Check each server 10 times(max)

check_command check-host-alive ; Defaultcommand to check if servers are "alive"

notification_period 24x7 ; Send notification out at any time- day or night

notification_interval 30 ; Resend notifications every 30minutes

notification_options d,r ; Only send notifications forspecific host states

contact_groups windowscontact ; Notifications get sent to the admins by default

hostgroups windows-servers ; Host groups that Windows servers should be a member of

register 0 ; DONT REGISTER THIS - ITS JUSTA TEMPLATE

}


# We define a generic printer template thatcan be used for most printers we monitor


define host{

name generic-printer ; The name of this host template

use generic-host ; Inherit defaultvalues from the generic-host template

check_period 24x7 ; By default, printers aremonitored round the clock

check_interval 5 ; Actively check the printerevery 5 minutes

retry_interval 1 ; Schedule host check retries at1 minute intervals

max_check_attempts 10 ; Check each printer 10 times(max)

check_command check-host-alive ; Default command to check if printersare "alive"

notification_period workhours ; Printersare only used during the workday

notification_interval 30 ; Resend notifications every 30minutes

notification_options d,r ; Only send notifications forspecific host states

contact_groups allcontact ; Notifications get sent tothe admins by default

register 0 ; DONT REGISTER THIS - ITS JUSTA TEMPLATE

}



# Define a template for switches that we canreuse

definehost{

name generic-switch ; The name of thishost template

use generic-host ; Inherit default values from thegeneric-host template

check_period 24x7 ; By default, switches aremonitored round the clock

check_interval 5 ; Switches are checked every 5 minutes

retry_interval 1 ; Schedule host check retries at1 minute intervals

max_check_attempts 10 ; Check each switch 10 times(max)

check_command check-host-alive ; Defaultcommand to check if routers are "alive"

notification_period 24x7 ; Send notifications at any time

notification_interval 30 ; Resend notifications every 30minutes

notification_options d,r ; Only send notifications forspecific host states

contact_groups allcontact ; Notifications get sent tothe admins by default

register 0 ; DONT REGISTER THIS - ITS JUSTA TEMPLATE

}

###############################################################################

# SERVICE TEMPLATES

###############################################################################


# Generic service definition template - Thisis NOT a real service, just a template!


define service{

name generic-service ; The 'name' of this service template

active_checks_enabled 1 ; Active service checksare enabled

passive_checks_enabled 1 ; Passive service checks are enabled/accepted

parallelize_check 1 ; Active service checks should be parallelized(disabling this can lead to major performance problems)

obsess_over_service 1 ; We should obsess overthis service (if necessary)

check_freshness 0 ; Default is to NOT checkservice 'freshness'

notifications_enabled 1 ;Service notifications are enabled

event_handler_enabled 1 ; Service event handler is enabled

flap_detection_enabled 1 ; Flap detection is enabled

failure_prediction_enabled 1 ; Failureprediction is enabled

process_perf_data 1 ; Process performance data

retain_status_information 1 ; Retain status information across programrestarts

retain_nonstatus_information 1 ; Retain non-status informationacross program restarts

is_volatile 0 ; The service is not volatile

check_period 24x7 ; The service can be checked at anytime of the day

max_check_attempts 3 ; Re-checkthe service up to 3 times in order to determine its final (hard) state

normal_check_interval 10 ; Checkthe service every 10 minutes under normal conditions

retry_check_interval 2 ; Re-checkthe service every two minutes until a hard state can be determined

contact_groups all contact ; Notifications get sent out to everyonein the 'admins' group

notification_options w,u,c,r ; Sendnotifications about warning, unknown, critical, and recovery events

notification_interval 60 ; Re-notify about serviceproblems every hour

notification_period 24x7 ;Notifications can be sent out at any time

register 0 ; DONTREGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!

}



# Local service definition template - This isNOT a real service, just a template!


define service{

name local-service ; The name of this service template

use generic-service ; Inheritdefault values from the generic-service definition

max_check_attempts 4 ; Re-check the service up to 4 timesin order to determine its final (hard) state

normal_check_interval 5 ; Checkthe service every 5 minutes under normal conditions

retry_check_interval 1 ; Re-check the serviceevery minute until a hard state can be determined

register 0 ; DONTREGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!

}


b. resource.cfg文件

resource.cfgnagios的變量定義文件,文件內(nèi)容只有一行:

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

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

c. commands.cfg文件

此文件默認是存在的,無需修改即可使用,當然如果有新的命令需要加入時,在此文件進行添加即可。


#notify-host-by-email命令的定義
define command{

command_name notify-host-by-email #命令名稱,即定義了一個主機異常時發(fā)送郵件的命令。

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$ #命令具體的執(zhí)行方式。

}

#notify-service-by-email命令的定義
define command{

command_name notify-service-by-email #命令名稱,即定義了一個服務異常時發(fā)送郵件的命令

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\nAdditionalInfo:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **"$CONTACTEMAIL$

}
#check-host-alive
命令的定義

definecommand{

command_name check-host-alive #命令名稱,用來檢測主機狀態(tài)。

command_line $USER1$/check_ping -H $HOSTADDRESS$ -w3000.0,80% -c 5000.0,100% -p 5
#
這里的變量$USER1$resource.cfg文件中進行定義,即$USER1$=/usr/local/nagios/libexec;
#
那么check_ping的完整路徑為/usr/local/nagios/libexec/check_ping;
# “-w 3000.0,80%”
“-w”說明后面的一對值對應的是“WARNING”狀態(tài),“80%”是其臨界值。
#“-c 5000.0,100%”
“-c”說明后面的一對值對應的是“CRITICAL”,“100%”是其臨界值。
# “-p 1”
說明每次探測發(fā)送一個包。
}

definecommand{

command_name check_local_disk

command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p$ARG3$ #$ARG1$是指在調(diào)用這個命令的時候,命令后面的第一個參數(shù)。

}

definecommand{

command_name check_local_load

command_line $USER1$/check_load -w $ARG1$ -c $ARG2$

}

definecommand{

command_name check_local_procs

command_line $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s$ARG3$

}

definecommand{

command_name check_local_users

command_line $USER1$/check_users -w $ARG1$ -c $ARG2$

}

definecommand{

command_name check_local_swap

command_line $USER1$/check_swap -w $ARG1$ -c $ARG2$

}

definecommand{

command_name check_ftp

command_line $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$

}

definecommand{

command_name check_http

command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$

}

definecommand{

command_name check_ssh

command_line $USER1$/check_ssh $ARG1$ $HOSTADDRESS$

}

definecommand{

command_name check_ping

command_line $USER1$/check_ping -H $HOSTADDRESS$ -w$ARG1$ -c $ARG2$ -p 5

}

definecommand{

command_name check_nt

command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489-v $ARG1$ $ARG2$

}


d. hosts.cfghostgroups.cfg文件

以上文件默認不存在,需要手動創(chuàng)建,hosts.cfg主要用來指定被監(jiān)控的主機地址以及相關屬性信息,hostgroups主要用來定義群組,如果需增加被監(jiān)控主機,只需在此文件中增加即可,根據(jù)實驗目標配置如下:

define host{

host_name Nagios-Linux

alias CentOSreleae 5.8(Final)

address 192.168.1.111

contact_groups linuxcontact

check_command check-host-alive

max_check_attempts 3

normal_check_interval 1

notification_period 24x7

notification_options d,u,r

}


define host{

host_name Nagios-Windows

alias Windows XP

address 192.168.1.113

contact_groups windowscontact

check_command check-host-alive

max_check_attempts 3

normal_check_interval 1

notification_period 24x7

notification_options d,u,r

}

hostgroups.cfg文件配置如下:如果增加其他主機,只需在此文件中的membersr后面增加 hosts.cfg文件中定義的主機名稱,并使用“,”隔開

define hostgroup{

hostgroup_name linux servers

alias Linux Servers

members Nagios-Linux

}

define hostgroup{

hostgroup_name windows servers

alias Windows Servers

members Nagios-Windows

}

define hostgroup{

hostgroup_name other servers

alias Linux and WindowsServers

members Nagios-Windows, Nagios-Linux

}

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


篇幅有限,還有<< 二 >>


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI