天天看點

監控之NagiosNAGIOS

一、Nagios概述

1、簡介

Nagios是插件式的結構,它本身沒有任何監控功能,所有的監控都是通過插件進行的,是以其是高度子產品化和富有彈性的。Nagios監控的對象可分為兩類:主機和服務。主機通常指的是實體主機,如伺服器、路由器、工作站和列印機等,這裡的主機也可以是虛拟裝置,如xen虛拟出的linux系統;而服務通常指某個特定的功能,如提供http服務的httpd程序等。而為了管理上的友善,主機和服務還可以分别被規劃為主機組合服務組等。

Nagios不監控任何具體數值名額(如作業系統上的程序個數),它僅用四種抽象屬性對被監控對象的狀态進行描述:OK、WARNING, CRITICAL和UNKNOWN。于是,管理者隻需要對某種被監控對象的WARNING和CRITICAL狀态的門檻值進行關注和定義即可。Nagios通過将WARTING和CRTICAL的門檻值傳遞給插件,并由插件負責某具體對象的監控及結果分析,其輸出資訊為狀态資訊(OK,WARNING,CRITICAL或UNKOWN)以及一些附加的詳細說明資訊。

2、特性

由上述說明可以,Nagios是極富彈性的,其監控功能完全可以按照管理者的期望進行。此外,它外提供了對問題的自動響應能力和一個功能強大的通知系統。所有這些功能的實作是基于一個結構明晰的對象定義系統和少數幾個對象類型實作的。

1) 指令(Commands)

“指令”用于定義Nagios如何執行某特定的監控工作。它是基于某特定的Nagios插件定義出的一個抽象層,通常包含一組要執行的操作。

2)時段(Time periods)

“時段”用于定義某“操作”可以執行或不能執行的日期和時間跨度,如工作日内的每天8:00-18:00等;

3)聯系人和聯系人組(Contacts and contact groups)

“聯系人”用于定義某監控事件的通知對象、要通知的資訊以及這些接收通知者何時及如何接收通知;一個或多個聯系人可以定義為聯系人組,而一個聯系人也可以屬于多個組;

4) 主機和主機組(host and host groups)

“主機”通常指某實體主機,其包括此主機相關的通知資訊的接收者(即聯系人)、如何及何時進行監控的定義。主機也可以分組,即主機組(host groups),一個主機可同時屬于多個組;

5) 服務(Services)

“服務”通常指某主機上可被監控的特定的功能或資源,其包括此服務相關的通知資訊的接收者、如何及何時進行監控等。服務也可以分組,即服務組(Service groups),一個服務可同時屬于多個服務組;

3、依賴關系

Nagios的強大功能還表現在其成熟的依賴關系系統上。比如,某路由裝置故障必然會導緻關聯在其上的其它主機無法被正常通路,如果不能定義這些裝置間的依賴關系,那麼監控系統上必然會出現大量的裝置故障資訊。而Nagios則通過依賴關系來描述網絡裝置的拓撲結構,并能夠實作在某裝置故障時不再對依賴于此裝置的其它裝置進行檢測,進而避免了無謂的故障資訊,友善管理者及時定位并排除故障。此外,Nagios的依賴關系還可以在服務級别上實作,如果某服務依賴于其它服務時,也能實作類似主機依賴關系的功能。

4、宏

Nagios還能夠使用宏,并且宏的定義在整個Nagios系統中具有一緻性。宏是能夠用于對象定義中的變量,其值通常依賴于上下文。在“指令”中定義的宏,相對于主機、服務或其它許多參數來說,其值會随之不同。比如,某指令可以根據向其傳遞的IP位址的不同來監控不同的主機。

5、計劃中當機

Nagios還提供了排程性計劃中的當機機制,管理者可以周期性的設定某主機或服務為計劃中的不可用狀态。這種功能可以阻止Nagios在排程當機時段通知任何資訊。當然,這也可以讓Nagios自動通知管理者該進行主機或服務維護了。

二、安裝配置Nagios

1、Nagios程式簡介

Nagios通常由一個主程式(Nagios)、一個插件程式(Nagios-plugins)和四個可選的ADDON(NRPE、NSCA、NSClient++和NDOUtils)組成。Nagios的監控工作都是通過插件實作的,是以,Nagios和Nagios-plugins是伺服器端工作所必須的元件。而四個ADDON中,NRPE用來在監控的遠端Linux/Unix主機上執行腳本插件以實作對這些主機資源的監控;NSCA用來讓被監控的遠端Linux/Unix主機主動将監控資訊發送給Nagios伺服器(這在備援監控模式中特别要用到);NSClient++是用來監控Windows主機時安裝在Windows主機上的元件;而NDOUtils則用來将Nagios的配置資訊和各event産生的資料存入資料庫,以實作這些資料的快速檢索和處理。這四個ADDON(附件)中,NRPE和NSClient++工作于用戶端,NDOUtils工作于伺服器端,而NSCA則需要同時安裝在伺服器端和用戶端。

目前,Nagios隻能安裝在Linux系統主機上,其編譯需要用到gcc。同時,如果打算使用web界面的管理工具的話,還需要有apache伺服器和GD圖形庫的支援。

2、安裝前的準備工作

(1)解決安裝Nagios的依賴關系:

Nagios基本元件的運作依賴于httpd、gcc和gd。可以通過以下指令來檢查nagios所依賴的rpm包是否已經完全安裝:

<code># yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server</code>

說明:以上軟體包您也可以通過編譯源代碼的方式安裝,隻是後面許多要用到的相關檔案的路徑等需要按照您的源代碼安裝時的配置逐一修改。此外,您還得按需啟動必要的服務,如httpd等。

(2)添加nagios運作所需要的使用者群組:

<code># groupadd  nagcmd</code>

<code># useradd -G nagcmd nagios</code>

<code># passwd nagios</code>

把apache加入到nagcmd組,以便于在通過web Interface操作nagios時能夠具有足夠的權限:

<code># usermod -a -G nagcmd apache</code>

3、編譯安裝nagios:

<code># tar zxf nagios-3.3.1.tar.gz </code>

<code># cd nagios-3.3.1</code>

<code># ./configure --with-command-group=nagcmd --enable-event-broker </code>

<code># make all</code>

<code># make install</code>

<code># make install-init</code>

<code># make install-commandmode</code>

<code># make install-config</code>

為email指定您想用來接收nagios警告資訊的郵件位址,預設是本機的nagios使用者:

<code># vi /usr/local/nagios/etc/objects/contacts.cfg</code>

email        nagios@localhost       #這個是預設設定

在httpd的配置檔案目錄(conf.d)中建立Nagios的Web程式配置檔案:

<code># make install-webconf</code>

建立一個登入nagios web程式的使用者,這個使用者帳号在以後通過web登入nagios認證時所用:

<code># htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin</code>

以上過程配置結束以後需要重新啟動httpd:

<code># service httpd restart</code>

4、編譯、安裝nagios-plugins

nagios的所有監控工作都是通過插件完成的,是以,在啟動nagios之前還需要為其安裝官方提供的插件。

<code># tar zxf nagios-plugins-1.4.15.tar.gz </code>

<code># cd nagios-plugins-1.4.15</code>

<code># ./configure --with-nagios-user=nagios --with-nagios-group=nagios</code>

<code># make</code>

5、配置并啟動Nagios

(1)把nagios添加為系統服務并将之加入到自動啟動服務隊列:

<code># chkconfig --add nagios</code>

<code># chkconfig nagios on</code>

(2)檢查其主配置檔案的文法是否正确:

<code># /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg</code>

(3)如果上面的文法檢查沒有問題,接下來就可以正式啟動nagios服務了:

<code># service nagios start</code>

(4)配置selinux

如果您的系統開啟了selinux服務,則預設為拒絕nagios web cgi程式的運作。您可以通過下面的指令來檢查您的系統是否開啟了selinux:

<code>#getenforce</code>

如果上面指令的結果顯示開啟了selinux服務,您可以通過下面的指令暫時性的将其關閉:

<code>#setenforce 0</code>

如果您想在以後完全關閉selinux,可以通過編輯/etc/sysconfig/selinux檔案,将其中的selinux後面的值“force”修改為“disable”即可。

當然,您也可以通過以下方式将nagios的CGI程式運作于SELinux/targeted模式而不用關閉selinux:

<code># chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin</code>

<code># chcon -R -t httpd_sys_content_t /usr/local/nagios/share</code>

(5)通過web界面檢視nagios:

<code>http:</code><code>//your_nagios_IP/nagios</code>

登入時需要指定前面設定的web認證帳号和密碼。

注意:為了nagios系統的安全,建議您使用資訊摘要強認證模式(如MD5),強制使用web的TSL/SSL安全通訊模式,并且通過通路控制清單設定能通路nagios的web界面的用戶端位址。

四、配置Nagios

1、Nagios的主配置檔案

Nagios的主配置檔案為nagios.cfg,其文法非常簡潔,通常#開頭的行為注釋行,而參數的設定格式為&lt;parameter&gt;=&lt;value&gt;;其中,有些參數是可以重複出現的。其中常用的參數說明如下:

log_file: 設定Nagios的日志檔案;

cfg_file: Nagios對象定義的相關檔案,此參數可重複使用多次以指定多個檔案;

cfg_dir:  設定Nagios對象定義的相關檔案所在的目錄,此目錄中的所有檔案都會被作為對象定義的檔案;此參數可重複使用多次以指定多個目錄;

resource_file: 設定Nagios附加的宏定義的相關檔案;

status_file: 設定Nagios存儲所有主機和服務目前狀态資訊的檔案;

status_update_interval: 設定status_file指定的檔案中狀态資訊的更新頻率;

service_check_timeout: 設定服務檢測的逾時時間,預設為60秒;

host_check_timeout: 設定主機檢測的逾時時間,預設為30秒;

notification_timeout: 設定通知資訊發送嘗試的逾時時間,預設為30秒;

2、resource_file和宏定義

在主配置檔案中,參數resource_file用于定義所有使用者變量(即“宏”)的存儲檔案,它用于存儲對象定義中的可以通路的額外資訊,如通路某服務的密碼等;是以,這些資訊通常都是些敏感資料,一般不允許通過Web接口來通路。此檔案中可以定義的宏可多達32個,它們分别為$USER1$,$USER2$...$USER32,這些宏一般在check指令中引用。通常情況下$USER1$用于引用Nagios插件所在目錄這個路徑資訊,是以,一般不建議修改其值。

Nagios事先定義了許多宏,它們的值通常依賴于其上下文。如下:

HOSTNAME: 用于引用host_name指定所定義的主機的主機名;每個主機的主機名都是唯一的;

HOSTADDRESS: 用于引用host對象中的address指令的值,它通常可以為IP位址或主機名;

HOSTDISPLAYNAME: 用于引用host對象中alias指令的值,用以描述目前主機,即主機的顯示名稱;

HOSTSTATE:某主機的目前狀态,為UP,DOWN,UNREACHABLE三者之一;

HOSTGROUPNAMES: 用于引用某主機所屬的所有主機組的簡名,主機組名稱之間以逗号分隔;

LASTHOSTCHECK:用于引用某主機上次檢測的時間和日期,Unix時間戳格式;

LISTHOSTSTATE:用于引用某主機前一次檢測時的狀态,為UP,DOWN或UNREACHABLE三者之一;

SERVICEDESC: 用于引用對應service對象中的desccription指令的值;

SERVICESTATE: 用于引用某服務的目前狀态,為OK,WARNING,UNKOWN或CRITICAL四者之一;

SERVICEGROUPNAMES: 用于引用某服務所屬的所有服務組的簡名,服務組名稱之間以逗号分隔;

CONTACTNAME: 用于引用某contact對象中contact_name指令的值;

CONTACTALIAS: 用于引用某contact對象中alias指令的值;

CONTACTEMAIL: 用于引用某contact對象中email指令的值;

CONTACTGROUPNAMES: 用于引用某contact所屬的所有contact組的簡名,contact組名稱之間以逗号分隔;

Nagios 3還支援自定義宏,隻是它的定義和使用方式比較獨特。管理者可以在某類型對象的定義中使用額外的指令,并能夠在指令中使用特别格式的宏來引用此指令的值。其引用方式根據對象類型的不同也有所不同,具體如下:

$_HOST&lt;variable&gt;$ – 引用在主機對象中定義的指令的值;

$_SERVICE&lt;variable&gt;$ – 引用在服務對象中定義的指令的值;

$_CONTACT&lt;variable&gt;$ – 引用在聯系人對象中定義的指令的值;

一個簡單的例子如下:

如某主機定義為:

<code>define host</code>

<code>  </code><code>{</code>

<code>    </code><code>host_name somemachine</code>

<code>    </code><code>address 10.0.0.1</code>

<code>    </code><code>_MAC 12:34:56:78:90:ab</code>

<code>    </code><code>check_command check-host-by-mac</code>

<code>  </code><code>}</code>

<code>對應的檢測指令則可以定義為:</code>

<code>  </code><code>define </code><code>command</code>

<code>    </code><code>command_name check-host-by-mac</code>

<code>    </code><code>command_line $USER1$</code><code>/check_hostmac</code> <code>-H $HOSTADDRESS$ -m $_HOSTMAC$</code>

3、定義主機對象

“主機”指的是被監控的機器,可是實體主機,也可以是虛拟裝置。一個主機對象的定義至少應該包含一個簡名(short name)、一個别名、一個IP位址和用到的檢測指令。此外,很多時候,其定義中還應該包含監控時段、聯系人及要通知的相關問題、檢測的頻率、重試檢測的方式、發送通知的頻率等。具體的各指令及說明請參見官方文檔:http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#host。

一個主機定義的例子:

<code>{</code>

<code>host_name webserver1</code>

<code>hostgroups webservers</code>

<code>alias</code> <code>www.xxx.com</code>

<code>address 172.16.100.11</code>

<code>check_command check-host-alive</code>

<code>check_interval 5</code>

<code>retry_interval 1</code>

<code>max_check_attempts 5</code>

<code>check_period 24x7</code>

<code>contact_groups linux-admins</code>

<code>notification_interval 30</code>

<code>notification_period 24x7</code>

<code>notification_options d,u,r</code>

<code>}</code>

其中的notification_options用于指定當主機處于什麼狀态時應該發送通知。其各狀态及其表示符如下:

d —— DOWN

u —— UNREACHABLE

r —— UP(host recovery)

f —— flapping

s —— 調試當機時間開始或結束

主機可以被劃分成組,這些組即主機組。每一個主機組對象一般包含一個全局唯一的簡名、一個描述名以及屬于這個組的成員。此外,一個主機組的成員也可以是其它主機組。主機組的定義例子如下:

<code>define hostgroup</code>

<code>hostgroup_name webservers</code>

<code>alias</code> <code>Linux web servers</code>

<code>members webserver1</code>

4、定義服務對象

“服務”即某“主機”所提供的功能或資源對象,如HTTP服務、存儲空間資源或CPU負載等。服務附屬于主機,每一個服務使用服務名來辨別,此服務名要求在特定的主機上具有唯一性。每一個服務對象還通常定義一個檢測指令及如何進行問題通知等。

<code>define service</code>

<code>service_description www</code>

<code>check_command check_http</code>

<code>check_interval 10</code>

<code>retry_interval 3</code>

<code>max_check_attempts 3</code>

<code>notification_options w,c,u,r</code>

其中的notification_options用于指定當服務處于什麼狀态時應該發送通知。其各狀态及其表示符如下:

w —— WARNING

u —— UNKNOWN

c —— CRITICAL

r —— OK(recovery)

與主機對象有所不同的是,有時個,多個主機可能會提供同樣的服務,比如多台伺服器同時提供Web等。是以,在定義服務對象時,其host_name可以為逗号隔開的多個主機。

服務可以被劃分成組,這些組即服務組。每一個服務組對象一般包含一個全局唯一的簡名、一個描述名以及屬于這個組的成員。此外,一個服務組的成員通常是某主機上的某服務,其指定時使用&lt;host&gt;,&lt;service&gt;的格式,多個服務也使用逗号分隔。服務組的定義例子如下:

<code>define servicegroup</code>

<code>servicegroup_name webservices</code>

<code>alias</code> <code>All services related to web</code>

<code>members webserver1,www,webserver2,www</code>

5、定義指令對象

“指令”用于描述如何對主機或服務進行狀态檢測。服務對象的定義包含兩個指令:名字(command_name)和指令行(command_line);名字用于辨別此指令對象,指令行則是執行檢測時真正要執行的指令。

當指令對象用于檢測其它對象時,其通常需要用到額外的參數以辨別要檢測的某特定對象,此時,指令對象需要以command_name[!arg1][!arg2][...]的文法格式進行引用。是以,指令對象的定義中,指令行指令中通常會用到宏$ARG1$, $ARG2$...,對應用于接收[!arg1][!arg2][...]傳遞而來的參數。

如下指令對象的定義:

<code>define </code><code>command</code>

<code>command_namecheck_local_swap</code>

<code>command_line$USER1$</code><code>/check_swap</code> <code>-w $ARG1$ -c $ARG2$</code>

如下的服務中使用上面定義的指令對象來檢測服務對象:

<code>    </code><code>host_name  localhost</code>

<code>    </code><code>service_description  Swap Usage</code>

<code>check_command check_local_swap!20!10</code>

6、定義“時段”對象

“時段”用于定義某“操作”可以執行或不能執行的日期和時間跨度,如工作日内的每天8:00-18:00等,其可以在多個不同的操作中重複引用。一個時段對象的定義包含一個全局唯一的名稱辨別及一個或多個時間跨度。例如:

<code>define timeperiod</code>

<code>timeperiod_name workinghours</code>

<code>alias</code> <code>Working Hours, from Monday to Friday</code>

<code>monday 09:00-17:00</code>

<code>tuesday 09:00-17:00</code>

<code>wednesday 09:00-17:00</code>

<code>thursday 09:00-17:00</code>

<code>friday 09:00-17:00</code>

其中,時間的指定格式有許多方式:

月曆時間:格式為YYYY-MM-DD,如2012-04-21;

日期:如 April 21;

每月的某一天:如 day 21,指每月的21号;

每月的第幾個周幾:如 saturday 1,指每月的第一個星期六;

星期幾:如monday, tuesday等;

7、定義聯系人對象

“聯系人”對象用于定義某主機裝置的擁有者或某問題出現時接受通知者。聯系人對象的定義包含一個全局唯一的辨別名稱、一個描述名及一個或多個郵件位址等。此外,其通常還應該包括對相應的主機或服務出現故障時所用到的通知指令。例如:

<code>define contact</code>

<code>contact_name A</code>

<code>alias</code> <code>XXX Company </code>

<code>email [email protected]</code>

<code>host_notification_period  workinghours</code>

<code>service_notification_period  workinghours</code>

<code>host_notification_options  d,u,r</code>

<code>service_notification_options  w,u,c,r</code>

<code>host_notification_commands     host-notify-by-email</code>

<code>service_notification_commands   notify-by-email</code>

聯系人也可劃分為組,即聯系人組。一個聯系人組對象包含一個全局惟一的辨別名稱,一個描述名稱和屬于此聯系人組的聯系人成員(members)或其人聯系人組成員(contactgroup_members)。例如:

<code>define contactgroup</code>

<code>contactgroup_name linux-admins</code>

<code>alias</code> <code>Linux Administrators</code>

<code>members XXX-1, XXX-2</code>

在主機或服務對象的定義中,既可以指定聯系人,也可以指定聯系人組。當然,某主機的問題聯系人與其上運作的服務的聯系人也可以不同。

8、模闆及對象繼承

Nagios通過功能強大的繼承引擎來實作基于模闆的對象繼承。這就意味着可以定義将某類型的對象的通用屬性組織起來定義為對象模闆,并在定義其類型中的對象時直接從此模闆繼承其相關屬性的定義。定義對象模闆的方法很簡單,通常隻需要在定義某類型對象時使用register指令并将其值設定為0即可。對象模闆的名稱通常使用name指令定義,這與某特定類型對象使用的指令也有所不同。而定義此種類型的對象時,隻需要使用use指令并将其值設定為對應模闆的名稱即可。例如:

<code>name generic-server</code>

<code>register 0</code>

<code>use generic-server</code>

<code>name webserver1</code>

<code>alias</code> <code>Web Server 01</code>

一個對象在定義時也以同時繼承多個模闆,此時隻需要為use指令指定以逗号分隔的多個模闆名稱即可。同時,Nagios也支援模闆的多級繼承。

9、依賴關系

為了描述Nagios對象間的依賴關系,這裡要用到兩個術語:master(被依賴的主機或服務)和dependent(依賴關系中的依賴于master的Nagios對象)。Nagios可以定義對象間的彼此依賴性,也可以為某對象定義其父對象,甚至也可以指定此依賴關系生效的時段。下面是一個關于依賴關系定義的例子:

<code>define hostdependency</code>

<code>dependent_host_name backuphost</code>

<code>host_name vpnserver1</code>

<code>dependency_period maintenancewindows</code>

其中host_name用于定義master主機,dependent_host_name定義dependent主機。而在依賴關系的定義中,通常還會用到execution_failure_criteria定義master主機為何種狀态時不再對依賴于此master的主機進行檢測,notification_failure_criteria用于定義master處于何種狀态時不會發送dependent相關的主機問題通知到聯系人。

服務間依賴關系的定義類似于主機間的依賴關系,例如:

<code>define servicedependency</code>

<code>host_name mysqlserver</code>

<code>service_description mysql</code>

<code>dependent_hostgroup_name apacheservers</code>

<code>dependent_service_description webservice</code>

<code>execution_failure_criteria c,u</code>

<code>notification_failure_criteria c,u,w</code>

三、基于NRPE監控遠端Linux主機

1、NRPE簡介

Nagios監控遠端主機的方法有多種,其方式包括SNMP、NRPE、SSH和NCSA等。這裡介紹其通過NRPE監控遠端Linux主機的方式。

NRPE(Nagios Remote Plugin Executor)是用于在遠端伺服器上運作檢測指令的守護程序,它用于讓Nagios監控端基于安裝的方式觸發遠端主機上的檢測指令,并将檢測結果輸出至監控端。而其執行的開銷遠低于基于SSH的檢測方式,而且檢測過程并不需要遠端主機上的系統帳号等資訊,其安全性也高于SSH的檢測方式。

2、安裝配置被監控端

1)先添加nagios使用者

<code># useradd -s /sbin/nologin nagios</code>

2)NRPE依賴于nagios-plugins,是以,需要先安裝之

<code># make instal</code>

3)安裝NRPE

<code># tar -zxvf nrpe-2.12.tar.gz</code>

<code># cd nrpe-2.12.tar.gz</code>

<code># ./configure --with-nrpe-user=nagios \</code>

<code>     </code><code>--with-nrpe-group=nagios \</code>

<code>     </code><code>--with-nagios-user=nagios \</code>

<code>     </code><code>--with-nagios-group=nagios \</code>

<code>     </code><code>--</code><code>enable</code><code>-</code><code>command</code><code>-args \</code>

<code>     </code><code>--</code><code>enable</code><code>-ssl</code>

<code># make install-plugin</code>

<code># make install-daemon</code>

<code># make install-daemon-config</code>

4)配置NRPE

<code># vim /usr/local/nagios/etc/nrpe.conf</code>

<code>log_facility=daemon</code>

<code>pid_file=</code><code>/var/run/nrpe</code><code>.pid</code>

<code>server_address=172.16.100.11</code>

<code>server_port=5666</code>

<code>nrpe_user=nagios</code>

<code>nrpe_group=nagios</code>

<code>allowed_hosts=172.16.100.1</code>

<code>command_timeout=60</code>

<code>connection_timeout=300</code>

<code>debug=0</code>

上述配置指令可以做到見名知義,是以,配置過程中根據實際需要進行修改即可。其中,需要特定說明的是allowed_hosts指令用于定義本機所允許的監控端的IP位址。

5)啟動NRPE

<code># /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d</code>

為了便于NRPE服務的啟動,可以将如下内容定義為/etc/init.d/nrped腳本:

<code>#!/bin/bash</code>

<code># chkconfig: 2345 88 12</code>

<code># description: NRPE DAEMON</code>

<code>NRPE=</code><code>/usr/local/nagios/bin/nrpe</code>

<code>NRPECONF=</code><code>/usr/local/nagios/etc/nrpe</code><code>.cfg</code>

<code>case</code> <code>"$1"</code> <code>in</code>

<code>start)</code>

<code>echo</code> <code>-n </code><code>"Starting NRPE daemon..."</code>

<code>$NRPE -c $NRPECONF -d</code>

<code>echo</code> <code>" done."</code>

<code>;;</code>

<code>stop)</code>

<code>echo</code> <code>-n </code><code>"Stopping NRPE daemon..."</code>

<code>pkill -u nagios nrpe</code>

<code>restart)</code>

<code>$0 stop</code>

<code>sleep</code> <code>2</code>

<code>$0 start</code>

<code>*)</code>

<code>echo</code> <code>"Usage: $0 start|stop|restart"</code>

<code>esac</code>

<code>exit</code> <code>0</code>

或者,也可以在/etc/xinetd.d目錄中建立nrpe檔案,使其成為一個基于非獨立守護程序的服務,檔案内容如下:

<code>service nrpe</code>

<code>flags = REUSE</code>

<code>socket_type = stream</code>

<code>wait = no</code>

<code>user = nagios</code>

<code>group = nagios</code>

<code>server = </code><code>/usr/local/nagios/bin/nrpe</code>

<code>server_args = -c </code><code>/etc/nagios/nrpe</code><code>.cfg -i</code>

<code>log_on_failure += USERID</code>

<code>disable = no</code>

此種情況下啟動NRPE程序需要通過重新開機xinetd來實作。

6)配置允許遠端主機監控的對象

在被監控端,可以通過NRPE監控的服務或資源需要通過nrpe.conf檔案使用指令進行定義,定義指令的文法格式為:

<code>command</code><code>[&lt;command_name&gt;]=&lt;command_to_execute&gt;。比如:</code>

<code>command</code><code>[check_rootdisk]=</code><code>/usr/local/nagios/libexec/check_disk</code> <code>-w 20% -c 10% -p /</code>

<code>command</code><code>[check_swap]=</code><code>/usr/local/nagios/libexec/check_disk</code> <code>-w 40% -c 20%</code>

<code>command</code><code>[check_sensors]=</code><code>/usr/local/nagios/libexec/check_sensors</code>

<code>command</code><code>[check_users]=</code><code>/usr/local/nagios/libexec/check_users</code> <code>-w 10 -c 20</code>

<code>command</code><code>[check_load]=</code><code>/usr/local/nagios/libexec/check_load</code> <code>-w 10,8,5 -c 20,18,15</code>

<code>command</code><code>[check_zombies]=</code><code>/usr/local/nagios/libexec/check_procs</code> <code>-w 5 -c 10 -s Z</code>

<code>command</code><code>[check_all_procs]=</code><code>/usr/local/nagios/libexec/check_procs</code> <code>-w 150 -c 200</code>

3、配置監控端

1)安裝NRPE

2)定義如何監控遠端主機及服務:

通過NRPE監控遠端Linux主機要使用chech_nrpe插件進行,其文法格式如下:

<code>check_nrpe -H &lt;host&gt; [-n] [-u] [-p &lt;port&gt;] [-t &lt;timeout&gt;] [-c &lt;</code><code>command</code><code>&gt;] [-a &lt;arglist...&gt;]</code>

使用示例1:

定義監控遠端Linux主機swap資源的指令:

<code>command_name check_swap_nrpe</code>

<code>command_line $USER1$</code><code>/check_nrpe</code> <code>–H </code><code>"$HOSTADDRESS$"</code> <code>-c </code><code>"check_swap"</code>

定義遠端Linux主機的swap資源:

<code>use generic-service</code>

<code>host_name linuxserver1,linuxserver2</code>

<code>hostgroup_name linux-servers</code>

<code>service_description SWAP</code>

<code>check_command check_swap_nrpe</code>

<code>normal_check_interval 30</code>

使用示例2:

如果希望上面的command定義更具有通用性,那麼上面的定義也可以修改為如下:

定義監控遠端Linux主機的指令:

<code>command_name check_nrpe</code>

<code>command_line $USER1$</code><code>/check_nrpe</code> <code>–H </code><code>"$HOSTADDRESS$"</code> <code>-c $ARG1$</code>

<code>check_command check_nrpe!check_swap</code>

使用示例3:

如果還希望在監控遠端Linux主機時還能向其傳遞參數,則可以使用類似如下方式進行:

定義監控遠端Linux主機disk資源的指令:

<code>command_line $USER1$</code><code>/check_nrpe</code> <code>–H </code><code>"$HOSTADDRESS$"</code> <code>-c </code><code>"check_swap"</code> <code>-a $ARG1$ $ARG2$</code>

<code>check_command check_swap_nrpe!20!10</code>

五、基于NSClinet++監控Windows主機

1、基于check_nt

Windows端要啟用的子產品:

<code>[modules]</code>

<code>CheckSystem.dll</code>

<code>CheckDisk.dll</code>

<code>FileLogger.dll</code>

<code>NSClientListener.dll</code>

<code>[settings]</code>

<code>allowed_hosts =</code>

修改配置後要重新開機服務:

<code>nsclient++ </code><code>/stop</code>

<code>nsclient++ </code><code>/start</code>

在nagios端使用如下指令測試:

<code>check_nt -H &lt;client ip&gt; -p &lt;port&gt; -</code><code>v</code> <code>&lt;</code><code>command</code><code>&gt; ...</code>

<code># check_nt -H 172.16.100.66 -p 12489 -v CPULOAD -w 80 -c 90 -l 5,80,90</code>

<code># vim /usr/local/nagios/etc/objects/windows.cfg</code>

<code>define host </code>

<code>usewindows-server</code>

<code>host_namewinserver</code>

<code>aliasMy Windows machine</code>

<code>address172.16.100.66</code>

<code>define service{</code>

<code>host_name winserver</code>

<code>service_description NSClient++ Version</code>

<code>check_command check_nt!CLIENTVERSION</code>

<code>define service {</code>

<code>usegeneric-service</code>

<code>service_descriptionUptime</code>

<code>check_commandcheck_nt!UPTIME</code>

<code>service_descriptionCPU Load</code>

<code>check_commandcheck_nt!CPULOAD!-l 5,80,90</code>

<code>service_descriptionMemory Usage</code>

<code>check_commandcheck_nt!MEMUSE!-w 80 -c 90</code>

<code>Password Protection</code>

If you specified a password in the NSClient++ configuration file on the Windows machine, you'll need to modify the check_nt command definition to include the password. Open the commands.cfg file for editing.

<code>vi</code> <code>/usr/local/nagios/etc/objects/commands</code><code>.cfg</code>

Change the definition of the check_nt command to include the "-s &lt;PASSWORD&gt;" argument (where PASSWORD is the password you specified on the Windows machine) like this:

<code>define </code><code>command</code><code>{</code>

<code>command_namecheck_nt</code>

<code>command_line$USER1$</code><code>/check_nt</code> <code>-H $HOSTADDRESS$ -p 12489 -s PASSWORD -</code><code>v</code> <code>$ARG1$ $ARG2$</code>

Save the file.

2、基于NRPE

NSClient++要啟用如下子產品:

<code>CheckExternalScripts.dll</code>

<code>NRPEListener.dll</code>

NRPE specific setting in NSClient++

<code>use_ssl</code>

<code>allow_arguments</code>

<code>allow_nasty_meta_chars</code>

check_nrpe文法:

<code>check_nrpe ... -c &lt;</code><code>command</code><code>&gt; [-a &lt;argument&gt; &lt;argument&gt; &lt;argument&gt;]</code>

check_nrpe的内置指令:

<code>· CheckAlwaysCRITICAL (check)</code>

<code>· CheckAlwaysOK (check)</code>

<code>· CheckAlwaysWARNING (check)</code>

<code>· CheckCPU (check)</code>

<code>· CheckCRITICAL (check)</code>

<code>· CheckCounter (check)</code>

<code>· CheckEventLog</code><code>/CheckEventLog</code> <code>(check)</code>

<code>· CheckFile (check)</code>

<code>· CheckFileSize (check)</code>

<code>· CheckMem (check)</code>

<code>· CheckMultiple (check)</code>

<code>· CheckOK (check)</code>

<code>· CheckProcState (check)</code>

<code>· CheckServiceState (check)</code>

<code>· CheckTaskSched</code><code>/CheckTaskSched</code> <code>(check)</code>

<code>· CheckUpTime (check)</code>

<code>· CheckVersion (check)</code>

<code>· CheckWARNING (check)</code>

<code>· CheckWMI</code><code>/CheckWMI</code> <code>(check)</code>

<code>· CheckWMIValue (check)</code>

用法如:

<code># check_nrpe ... -c CheckCPU -a warn=80 crit=90 time=20m time=10s time=4</code>

Nagios端的配置:

1) Template

<code>define host{</code>

<code>name tpl-windows-servers </code>

<code>use generic-host </code>

<code>max_check_attempts 10</code>

<code>notification_options d,r</code>

<code>contact_groups admins</code>

<code>register 0 </code>

2) 定義主機:

<code>use tpl-windows-servers </code>

<code>host_name windowshost</code>

<code>alias</code> <code>My First Windows Server</code>

<code>address 172.16.100.66</code>

3) 定義服務:

<code>service_description CPU Load</code>

<code>check_command check_nrpe!alias_cpu</code>

<code>service_description Free Space</code>

<code>check_command check_nrpe!alias_disk</code>

3、基于NSCA

[modules]

<code>CheckHelpers.dll</code>

<code>NSCAAgent.dll</code>

NSClient++配置

<code>interval</code>

<code>encryption_method</code>

<code>password</code>

<code>nsca_host</code>

修改配置後要重新開機服務

1) 模闆

<code>name tpl-windows-servers ; Name of this template</code>

<code>use generic-host ; Inherit default values</code>

<code>register 0 ; DONT REGISTER THIS - ITS A TEMPLATE</code>

2)主機配置

<code>use tpl-windows-servers</code>

<code>host_name windowshost </code>

<code>address 172.16.100.66 </code>

<code>active_checks_enabled 0 </code>

<code>passive_checks_enabled 1 </code>

3)服務配置

<code></code>

本文轉自 SoulMio 51CTO部落格,原文連結:http://blog.51cto.com/bovin/1860316,如需轉載請自行聯系原作者

繼續閱讀