一、nagios簡介
nagios是一款開源的電腦系統(tǒng)和網(wǎng)絡(luò)監(jiān)視工具,能有效監(jiān)控windows、linux和unix的主機狀態(tài),交換機路由器等網(wǎng)絡(luò)設(shè)置,打印機等。在系統(tǒng)或服務(wù)狀態(tài)異常時發(fā)出郵件或短信報警第一時間通知網(wǎng)站運維人員,在狀態(tài)恢復(fù)后發(fā)出正常的郵件或短信通知。
nagios原名為netsaint,由ethan galstad開發(fā)并維護至今。nagios是一個縮寫形式: "nagios ain't gonna insist on sainthood" sainthood 翻譯為圣徒,而"agios"是"saint"的希臘表示方法。nagios被開發(fā)在linux下使用,但在unix下也工作得非常好。
主要功能
網(wǎng)絡(luò)服務(wù)監(jiān)控(smtp、pop3、http、nntp、icmp、snmp、ftp、ssh)主機資源監(jiān)控(cpu load、disk usage、system logs),也包括windows主機(使用nsclient++ plugin)可以指定自己編寫的plugin通過網(wǎng)絡(luò)收集數(shù)據(jù)來監(jiān)控任何情況(溫度、警告……)可以通過配置nagios遠程執(zhí)行插件遠程執(zhí)行腳本遠程監(jiān)控支持ssh或ssl加通道方式進行監(jiān)控簡單的plugin設(shè)計允許用戶很容易的開發(fā)自己需要的檢查服務(wù),支持很多開發(fā)語言(shell scripts、c++、perl、ruby、python、php、c#等)包含很多圖形化數(shù)據(jù)plugins(nagiosgraph、nagiosgrapher、pnp4nagios等)可并行服務(wù)檢查能夠定義網(wǎng)絡(luò)主機的層次,允許逐級檢查,就是從父主機開始向下檢查當(dāng)服務(wù)或主機出現(xiàn)問題時發(fā)出通告,可通過email, pager, sms 或任意用戶自定義的plugin進行通知能夠自定義事件處理機制重新激活出問題的服務(wù)或主機自動日志循環(huán)支持冗余監(jiān)控包括web界面可以查看當(dāng)前網(wǎng)絡(luò)狀態(tài),通知,問題歷史,日志文件等
二、nagios工作原理
nagios的功能是監(jiān)控服務(wù)和主機,但是他自身并不包括這部分功能,所有的監(jiān)控、檢測功能都是通過各種插件來完成的。
啟動nagios后,它會周期性的自動調(diào)用插件去檢測服務(wù)器狀態(tài),同時nagios會維持一個隊列,所有插件返回來的狀態(tài)信息都進入隊列,nagios每次都從隊首開始讀取信息,并進行處理后,把狀態(tài)結(jié)果通過web顯示出來。
nagios提供了許多插件,利用這些插件可以方便的監(jiān)控很多服務(wù)狀態(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 自身也沒有報警部分的代碼,甚至沒有插件,而是交給用戶或者其他相關(guān)開源項目組去完成的。
nagios 安裝,是指基本平臺,也就是nagios軟件包的安裝。它是監(jiān)控體系的框架,也是所有監(jiān)控的基礎(chǔ)。
打開nagios官方的文檔,會發(fā)現(xiàn)nagios基本上沒有什么依賴包,只要求系統(tǒng)是linux或者其他nagios支持的系統(tǒng)。不過如果你沒有安裝apache(http服務(wù)),那么你就沒有那么直觀的界面來查看監(jiān)控信息了,所以apache姑且算是一個前提條件。關(guān)于apache的安裝,網(wǎng)上有很多,照著安裝就是了。安裝之后要檢查一下是否可以正常工作。
知道nagios 是如何通過插件來管理服務(wù)器對象后,現(xiàn)在開始研究它是如何管理遠端服務(wù)器對象的。nagios 系統(tǒng)提供了一個插件nrpe。nagios 通過周期性的運行它來獲得遠端服務(wù)器的各種狀態(tài)信息。它們之間的關(guān)系如下圖所示:
nagios 通過nrpe 來遠端管理服務(wù)
1. nagios 執(zhí)行安裝在它里面的check_nrpe 插件,并告訴check_nrpe 去檢測哪些服務(wù)。
2. 通過ssl,check_nrpe 連接遠端機子上的nrpe daemon
3. nrpe 運行本地的各種插件去檢測本地的服務(wù)和狀態(tài)(check_disk,..etc)
4. 最后,nrpe 把檢測的結(jié)果傳給主機端的check_nrpe,check_nrpe 再把結(jié)果送到nagios狀態(tài)隊列中。
5. nagios 依次讀取隊列中的信息,再把結(jié)果顯示出來。
三、實驗環(huán)境
host nameosipsoftwarenagios-servercentos release 6.3 (final)192.168.1.108apache、php、nagios、nagios-pluginsnagios-linuxcentos release 5.8 (final)192.168.1.111nagios-plugins、nrpenagios-windowswindows xp192.168.1.113 nsclient++
server 安裝了nagios軟件,對監(jiān)控的數(shù)據(jù)做處理,并且提供web界面查看和管理。當(dāng)然也可以對本機自身的信息進行監(jiān)控。
client 安裝了nrpe等客戶端,根據(jù)監(jiān)控機的請求執(zhí)行監(jiān)控,然后將結(jié)果回傳給監(jiān)控機。
防火墻已關(guān)閉/iptables: firewall is not running.
selinux=disabled
四、實驗?zāi)繕?biāo)
五、nagios服務(wù)端安裝
5.1 基礎(chǔ)支持套件:gcc glibc glibc-common gd gd-devel xinetd openssl-devel
# rpm -q gcc glibc glibc-common gd gd-devel xinetd openssl-devel
如果系統(tǒng)中沒有這些套件,使用yum 安裝
# yum install -y gcc 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 目錄的權(quán)限
# ll -d /usr/local/nagios/
5.3 編譯安裝nagios
# wget
# tar zxvf nagios-3.4.3.tar.gz
# cd nagios
# ./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
5.4 驗證程序是否被正確安裝
切換目錄到安裝路徑(這里是/usr/local/nagios),看是否存在etc、bin、sbin、share、var 這五個目錄,如果存在則可以表明程序被正確的安裝到系統(tǒng)了。nagios 各個目錄用途說明如下:
binnagios 可執(zhí)行程序所在目錄etcnagios 配置文件所在目錄sbinnagios cgi 文件所在目錄,也就是執(zhí)行外部命令所需文件所在的目錄sharenagios網(wǎng)頁文件所在的目錄libexecnagios 外部插件所在目錄varnagios 日志文件、lock 等文件所在的目錄var/archivesnagios 日志自動歸檔目錄var/rw用來存放外部命令文件的目錄
5.5 安裝nagios 插件
# wget
# tar zxvf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --prefix=/usr/local/nagios
# make && make install
5.6 安裝與配置apache和php
apache 和php 不是安裝nagios 所必須的,但是nagios提供了web監(jiān)控界面,通過web監(jiān)控界面可以清晰的看到被監(jiān)控主機、資源的運行狀態(tài),因此,安裝一個web服務(wù)是很必要的。
需要注意的是,nagios在nagios3.1.x版本以后,配置web監(jiān)控界面時需要php的支持。這里我們下載的nagios版本為nagios-3.4.3,因此在編譯安裝完成apache后,還需要編譯php模塊,這里選取的php版本為php5.4.10。
a. 安裝apache
# wget
# tar zxvf httpd-2.2.23.tar.gz
# cd httpd-2.2.23
# ./configure --prefix=/usr/local/apache2
# make && make install
若出現(xiàn)錯誤:
則在編譯時入加 --with-included-apr 即可解決。
b. 安裝php
# wget
# 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
c. 配置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>
接著增加如下內(nèi)容:
addtype application/x-httpd-php .php
為了安全起見,一般情況下要讓nagios 的web 監(jiān)控頁面必須經(jīng)過授權(quán)才能訪問,這需要增加驗證配置,即在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>
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 驗證文件,當(dāng)通過 訪問時就需要輸入用戶名和密碼了。
e. 查看認證文件的內(nèi)容
# cat /usr/local/nagios/etc/htpasswd
f. 啟動apache 服務(wù)
# /usr/local/apache2/bin/apachectl start
到這里nagios 的安裝也就基本完成了,你可以通過web來訪問了。
六、配置nagios
nagios 主要用于監(jiān)控一臺或者多臺本地主機及遠程的各種信息,包括本機資源及對外的服務(wù)等。默認的nagios 配置沒有任何監(jiān)控內(nèi)容,僅是一些模板文件。若要讓nagios 提供服務(wù),就必須修改配置文件,增加要監(jiān)控的主機和服務(wù),下面將詳細介紹。
6.1 默認配置文件介紹
nagios 安裝完畢后,默認的配置文件在/usr/local/nagios/etc目錄下。
每個文件或目錄含義如下表所示:
文件名或目錄名用途 cgi.cfg控制cgi訪問的配置文件 nagios.cfgnagios 主配置文件 resource.cfg變量定義文件,又稱為資源文件,在些文件中定義變量,以便由其他配置文件引用,如$user1$ objectsobjects 是一個目錄,在此目錄下有很多配置文件模板,用于定義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定義主機和服務(wù)的一個模板配置文件,可以在其他配置文件中引用 objects/timeperiods.cfg定義nagios 監(jiān)控時間段的配置文件 objects/windows.cfg監(jiān)控windows 主機的一個配置文件模板,默認沒有啟用此文件
6.2 配置文件之間的關(guān)系
在nagios的配置過程中涉及到的幾個定義有:主機、主機組,服務(wù)、服務(wù)組,聯(lián)系人、聯(lián)系人組,監(jiān)控時間,監(jiān)控命令等,從這些定義可以看出,nagios各個配置文件之間是互為關(guān)聯(lián),彼此引用的。
成功配置出一臺nagios監(jiān)控系統(tǒng),必須要弄清楚每個配置文件之間依賴與被依賴的關(guān)系,最重要的有四點:
第一:定義監(jiān)控哪些主機、主機組、服務(wù)和服務(wù)組;
第二:定義這個監(jiān)控要用什么命令實現(xiàn);
第三:定義監(jiān)控的時間段;
第四:定義主機或服務(wù)出現(xiàn)問題時要通知的聯(lián)系人和聯(lián)系人組。
6.3 配置nagios
為了能更清楚的說明問題,同時也為了維護方便,建議將nagios各個定義對象創(chuàng)建獨立的配置文件:
創(chuàng)建hosts.cfg文件來定義主機和主機組 創(chuàng)建services.cfg文件來定義服務(wù) 用默認的contacts.cfg文件來定義聯(lián)系人和聯(lián)系人組 用默認的commands.cfg文件來定義命令 用默認的timeperiods.cfg來定義監(jiān)控時間段 用默認的templates.cfg文件作為資源引用文件
a. templates.cfg文件
nagios主要用于監(jiān)控主機資源以及服務(wù),在nagios配置中稱為對象,為了不必重復(fù)定義一些監(jiān)控對象,nagios引入了一個模板配置文件,將一些共性的屬性定義成模板,以便于多次引用。這就是templates.cfg的作用。
下面詳細介紹下templates.cfg文件中每個參數(shù)的含義:
define contact{
name generic-contact ; 聯(lián)系人名稱
service_notification_period 24x7 ; 當(dāng)服務(wù)出現(xiàn)異常時,發(fā)送通知的時間段,這個時間段"24x7"在timeperiods.cfg文件中定義
host_notification_period 24x7 ; 當(dāng)主機出現(xiàn)異常時,發(fā)送通知的時間段,這個時間段"24x7"在timeperiods.cfg文件中定義
service_notification_options w,u,c,r ; 這個定義的是“通知可以被發(fā)出的情況”。w即warn,表示警告狀態(tài),u即unknown,表示不明狀態(tài);
; c即criticle,表示緊急狀態(tài),r即recover,表示恢復(fù)狀態(tài);
; 也就是在服務(wù)出現(xiàn)警告狀態(tài)、未知狀態(tài)、緊急狀態(tài)和重新恢復(fù)狀態(tài)時都發(fā)送通知給使用者。
host_notification_options d,u,r ; 定義主機在什么狀態(tài)下需要發(fā)送通知給使用者,d即down,表示宕機狀態(tài);
; u即unreachable,表示不可到達狀態(tài),r即recovery,表示重新恢復(fù)狀態(tài)。
service_notification_commands notify-service-by-email ; 服務(wù)故障時,發(fā)送通知的方式,可以是郵件和短信,這里發(fā)送的方式是郵件;
; 其中“notify-service-by-email”在commands.cfg文件中定義。
host_notification_commands notify-host-by-email ; 主機故障時,發(fā)送通知的方式,可以是郵件和短信,這里發(fā)送的方式是郵件;
; 其中“notify-host-by-email”在commands.cfg文件中定義。
register 0 ; dont register this definition - its not a real contact, just a template!
}
define host{
name generic-host ; 主機名稱,這里的主機名,并不是直接對應(yīng)到真正機器的主機名;
; 乃是對應(yīng)到在主機配置文件里所設(shè)定的主機名。
notifications_enabled 1 ; host notifications are enabled
event_handler_enabled 1 ; host event handler is enabled
flap_detection_enabled 1 ; flap detection is enabled
failure_prediction_enabled 1 ; failure prediction is enabled
process_perf_data 1 ; 其值可以為0或1,其作用為是否啟用nagios的數(shù)據(jù)輸出功能;
; 如果將此項賦值為1,那么nagios就會將收集的數(shù)據(jù)寫入某個文件中,以備提取。
retain_status_information 1 ; retain status information across program restarts
retain_nonstatus_information 1 ; retain non-status information across program restarts
notification_period 24x7 ; 指定“發(fā)送通知”的時間段,也就是可以在什么時候發(fā)送通知給使用者。
register 0 ; dont register this definition - its not a real host, just a template!
}
define host{
name linux-server ; 主機名稱
use generic-host ; use表示引用,也就是將主機generic-host的所有屬性引用到linux-server中來;
; 在nagios配置中,很多情況下會用到引用。
check_period 24x7 ; 這里的check_period告訴nagios檢查主機的時間段
check_interval 5 ; nagios對主機的檢查時間間隔,這里是5分鐘。
retry_interval 1 ; 重試檢查時間間隔,單位是分鐘。
max_check_attempts 10 ; nagios對主機的最大檢查次數(shù),也就是nagios在檢查發(fā)現(xiàn)某主機異常時,并不馬上判斷為異常狀況;
; 而是多試幾次,因為有可能只是一時網(wǎng)絡(luò)太擁擠,或是一些其他原因,讓主機受到了一點影響;
; 這里的10就是最多試10次的意思。
check_command check-host-alive ; 指定檢查主機狀態(tài)的命令,其中“check-host-alive”在commands.cfg文件中定義。
notification_period 24x7 ; 主機故障時,發(fā)送通知的時間范圍,其中“workhours”在timeperiods.cfg中進行了定義;
; 下面會陸續(xù)講到。
notification_interval 10 ; 在主機出現(xiàn)異常后,故障一直沒有解決,nagios再次對使用者發(fā)出通知的時間。單位是分鐘;
; 如果你覺得,所有的事件只需要一次通知就夠了,可以把這里的選項設(shè)為0
notification_options d,u,r ; 定義主機在什么狀態(tài)下可以發(fā)送通知給使用者,d即down,表示宕機狀態(tài);
; u即unreachable,表示不可到達狀態(tài);
; r即recovery,表示重新恢復(fù)狀態(tài)。
contact_groups ts ; 指定聯(lián)系人組,這個“admins”在contacts.cfg文件中定義。
register 0 ; dont register this definition - its not a real host, just a template!
}
define host{
name windows-server ; the name of this host template
use generic-host ; inherit default values from the generic-host template
check_period 24x7 ; by default, windows servers are monitored round the clock
check_interval 5 ; actively check the server every 5 minutes
retry_interval 1 ; schedule host check retries at 1 minute intervals
max_check_attempts 10 ; check each server 10 times (max)
check_command check-host-alive ; default command to check if servers are "alive"
notification_period 24x7 ; send notification out at any time - day or night
notification_interval 10 ; resend notifications every 30 minutes
notification_options d,r ; only send notifications for specific host states
contact_groups ts ; 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 just a template
}
define service{
name generic-service ; 定義一個服務(wù)名稱
active_checks_enabled 1 ; active service checks are 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 over this service (if necessary)
check_freshness 0 ; default is to not check service '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 ; failure prediction is enabled
process_perf_data 1 ; process performance data
retain_status_information 1 ; retain status information across program restarts
retain_nonstatus_information 1 ; retain non-status information across program restarts
is_volatile 0 ; the service is not volatile
check_period 24x7 ; 這里的check_period告訴nagios檢查服務(wù)的時間段。
max_check_attempts 3 ; nagios對服務(wù)的最大檢查次數(shù)。
normal_check_interval 5 ; 此選項是用來設(shè)置服務(wù)檢查時間間隔,也就是說,nagios這一次檢查和下一次檢查之間所隔的時間;
; 這里是5分鐘。
retry_check_interval 2 ; 重試檢查時間間隔,單位是分鐘。
contact_groups ts ; 指定聯(lián)系人組
notification_options w,u,c,r ; 這個定義的是“通知可以被發(fā)出的情況”。w即warn,表示警告狀態(tài);
; u即unknown,表示不明狀態(tài);
; c即criticle,表示緊急狀態(tài),r即recover,表示恢復(fù)狀態(tài);
; 也就是在服務(wù)出現(xiàn)警告狀態(tài)、未知狀態(tài)、緊急狀態(tài)和重新恢復(fù)后都發(fā)送通知給使用者。
notification_interval 10 ; re-notify about service problems every hour
notification_period 24x7 ; 指定“發(fā)送通知”的時間段,也就是可以在什么時候發(fā)送通知給使用者。
register 0 ; dont register this definition - its not a real service, just a template!
}
define service{
name local-service ; the name of this service template
use generic-service ; inherit default values from the generic-service definition
max_check_attempts 4 ; re-check the service up to 4 times in order to determine its final (hard) state
normal_check_interval 5 ; check the service every 5 minutes under normal conditions
retry_check_interval 1 ; re-check the service every minute until a hard state can be determined
register 0 ; dont register this definition - its not a real service, just a template!
}
b. resource.cfg文件
resource.cfg是nagios的變量定義文件,文件內(nèi)容只有一行:
$user1$=/usr/local/nagios/libexec
其中,變量$user1$指定了安裝nagios插件的路徑,如果把插件安裝在了其它路徑,只需在這里進行修改即可。需要注意的是,變量必須先定義,然后才能在其它配置文件中進行引用。
c. commands.cfg文件
此文件默認是存在的,無需修改即可使用,當(dāng)然如果有新的命令需要加入時,在此文件進行添加即可。
#notify-host-by-email命令的定義
define command{
command_name notify-host-by-email #命令名稱,即定義了一個主機異常時發(fā)送郵件的命令。
command_line /usr/bin/printf "%b" "***** nagios ***** notification type: $notificationtype$ host: $hostname$ state: $hoststate$ address: $hostaddress$ info: $hostoutput$ date/time: $longdatetime$ " | /bin/mail -s "** $notificationtype$ host alert: $hostname$ is $hoststate$ **" $contactemail$ #命令具體的執(zhí)行方式。
}
#notify-service-by-email命令的定義
define command{
command_name notify-service-by-email #命令名稱,即定義了一個服務(wù)異常時發(fā)送郵件的命令
command_line /usr/bin/printf "%b" "***** nagios ***** notification type: $notificationtype$ service: $servicedesc$ host: $hostalias$ address: $hostaddress$ state: $servicestate$ date/time: $longdatetime$ additional info: $serviceoutput$ " | /bin/mail -s "** $notificationtype$ service alert: $hostalias$/$servicedesc$ is $servicestate$ **" $contactemail$
}
#check-host-alive命令的定義
define command{
command_name check-host-alive #命令名稱,用來檢測主機狀態(tài)。
command_line $user1$/check_ping -h $hostaddress$ -w 3000.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”說明后面的一對值對應(yīng)的是“warning”狀態(tài),“80%”是其臨界值。
# “-c 5000.0,100%”中“-c”說明后面的一對值對應(yīng)的是“critical”,“100%”是其臨界值。
# “-p 1”說明每次探測發(fā)送一個包。
}
define command{
command_name check_local_disk
command_line $user1$/check_disk -w $arg1$ -c $arg2$ -p $arg3$ #$arg1$是指在調(diào)用這個命令的時候,命令后面的第一個參數(shù)。
}
define command{
command_name check_local_load
command_line $user1$/check_load -w $arg1$ -c $arg2$
}
define command{
command_name check_local_procs
command_line $user1$/check_procs -w $arg1$ -c $arg2$ -s $arg3$
}
define command{
command_name check_local_users
command_line $user1$/check_users -w $arg1$ -c $arg2$
}
define command{
command_name check_local_swap
command_line $user1$/check_swap -w $arg1$ -c $arg2$
}
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$
}
define command{
command_name check_ping
command_line $user1$/check_ping -h $hostaddress$ -w $arg1$ -c $arg2$ -p 5
}
define command{
command_name check_nt
command_line $user1$/check_nt -h $hostaddress$ -p 12489 -v $arg1$ $arg2$
}
d. hosts.cfg文件
此文件默認不存在,需要手動創(chuàng)建,hosts.cfg主要用來指定被監(jiān)控的主機地址以及相關(guān)屬性信息,根據(jù)實驗?zāi)繕?biāo)配置如下:
define host{
use linux-server #引用主機linux-server的屬性信息,linux-server主機在templates.cfg文件中進行了定義。
host_name nagios-linux #主機名
alias nagios-linux #主機別名
address 192.168.1.111 #被監(jiān)控的主機地址,這個地址可以是ip,也可以是域名。
}
#定義一個主機組
define hostgroup{
hostgroup_name bsmart-servers #主機組名稱,可以隨意指定。
alias bsmart servers #主機組別名
members nagios-linux #主機組成員,其中“nagios-linux”就是上面定義的主機。
}
localhost.cfg
define host{
use linux-server ; name of host template to use
; this host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name nagios-server
alias nagios-server
address 127.0.0.1
}
define hostgroup{
hostgroup_name linux-servers ; the name of the hostgroup
alias linux servers ; long name of the group
members nagios-server ; comma separated list of hosts that belong to this group
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description ping
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description root partition
check_command check_local_disk!20%!10%!/
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description current users
check_command check_local_users!20!50
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description total processes
check_command check_local_procs!250!400!rszdt
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description current load
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description swap usage
check_command check_local_swap!20!10
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description ssh
check_command check_ssh
notifications_enabled 0
}
define service{
use local-service ; name of service template to use
host_name nagios-server
service_description http
check_command check_http
notifications_enabled 0
}
windows.cfg
define host{
use windows-server ; inherit default values from a template
host_name nagios-windows ; the name we're giving to this host
alias my windows server ; a longer name associated with the host
address 192.168.1.113 ; ip address of the host
}
define hostgroup{
hostgroup_name windows-servers ; the name of the hostgroup
alias windows servers ; long name of the group
}
define service{
use generic-service
host_name nagios-windows
service_description nsclient++ version
check_command check_nt!clientversion
}
define service{
use generic-service
host_name nagios-windows
service_description uptime
check_command check_nt!uptime
}
define service{
use generic-service
host_name nagios-windows
service_description cpu load
check_command check_nt!cpuload!-l 5,80,90
}
define service{
use generic-service
host_name nagios-windows
service_description memory usage
check_command check_nt!memuse!-w 80 -c 90
}
define service{
use generic-service
host_name nagios-windows
service_description c: drive space
check_command check_nt!useddiskspace!-l c -w 80 -c 90
}
define service{
use generic-service
host_name nagios-windows
service_description w3svc
check_command check_nt!servicestate!-d showall -l w3svc
}
define service{
use generic-service
host_name nagios-windows
service_description explorer
check_command check_nt!procstate!-d showall -l explorer.exe
}
e. services.cfg文件
此文件默認也不存在,需要手動創(chuàng)建,services.cfg文件主要用于定義監(jiān)控的服務(wù)和主機資源,例如監(jiān)控http服務(wù)、ftp服務(wù)、主機磁盤空間、主機系統(tǒng)負載等等。nagios-server 和nagios-windows 相關(guān)服務(wù)已在相應(yīng)的配置文件中定義,所以這里只需要定義nagios-linux 相關(guān)服務(wù)即可,這里只定義一個檢測是否存活的服務(wù)來驗證配置文件的正確性,其他服務(wù)的定義將在后面講到。
define service{
use local-service #引用local-service服務(wù)的屬性值,local-service在templates.cfg文件中進行了定義。
host_name nagios-linux #指定要監(jiān)控哪個主機上的服務(wù),“nagios-server”在hosts.cfg文件中進行了定義。
service_description check-host-alive #對監(jiān)控服務(wù)內(nèi)容的描述,以供維護人員參考。
check_command check-host-alive #指定檢查的命令。
}
f. contacts.cfg文件
contacts.cfg是一個定義聯(lián)系人和聯(lián)系人組的配置文件,當(dāng)監(jiān)控的主機或者服務(wù)出現(xiàn)故障,nagios會通過指定的通知方式(郵件或者短信)將信息發(fā)給這里指定的聯(lián)系人或者使用者。
define contact{
contact_name david #聯(lián)系人的名稱,這個地方不要有空格
use generic-contact #引用generic-contact的屬性信息,其中“generic-contact”在templates.cfg文件中進行定義
alias nagios admin
email david.tang@bsmart.cn
}
define contactgroup{
contactgroup_name ts #聯(lián)系人組的名稱,同樣不能空格
alias technical support #聯(lián)系人組描述
members david #聯(lián)系人組成員,其中“david”就是上面定義的聯(lián)系人,如果有多個聯(lián)系人則以逗號相隔
}
g. timeperiods.cfg文件
此文件只要用于定義監(jiān)控的時間段,下面是一個配置好的實例:
#下面是定義一個名為24x7的時間段,即監(jiān)控所有時間段
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
}
#下面是定義一個名為workhours的時間段,即工作時間段。
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
}
h. cgi.cfg文件
此文件用來控制相關(guān)cgi腳本,如果想在nagios的web監(jiān)控界面執(zhí)行cgi腳本,例如重啟nagios進程、關(guān)閉nagios通知、停止nagios主機檢測等,這時就需要配置cgi.cfg文件了。
由于nagios的web監(jiān)控界面驗證用戶為david,所以只需在cgi.cfg文件中添加此用戶的執(zhí)行權(quán)限就可以了,需要修改的配置信息如下:
default_user_name=david
authorized_for_system_information=nagiosadmin,david
authorized_for_configuration_information=nagiosadmin,david
authorized_for_system_commands=david
authorized_for_all_services=nagiosadmin,david
authorized_for_all_hosts=nagiosadmin,david
authorized_for_all_service_commands=nagiosadmin,david
authorized_for_all_host_commands=nagiosadmin,david
i. nagios.cfg文件
nagios.cfg默認的路徑為/usr/local/nagios/etc/nagios.cfg,是nagios的核心配置文件,所有的對象配置文件都必須在這個文件中進行定義才能發(fā)揮其作用,這里只需將對象配置文件在nagios.cfg文件中進行引用即可。
log_file=/usr/local/nagios/var/nagios.log # 定義nagios日志文件的路徑
cfg_file=/usr/local/nagios/etc/objects/commands.cfg # “cfg_file”變量用來引用對象配置文件,如果有更多的對象配置文件,在這里依次添加即可。
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg # 本機配置文件
cfg_file=/usr/local/nagios/etc/objects/windows.cfg # windows 主機配置文件
object_cache_file=/usr/local/nagios/var/objects.cache # 該變量用于指定一個“所有對象配置文件”的副本文件,或者叫對象緩沖文件
precached_object_file=/usr/local/nagios/var/objects.precache
resource_file=/usr/local/nagios/etc/resource.cfg # 該變量用于指定nagios資源文件的路徑,可以在nagios.cfg中定義多個資源文件。
status_file=/usr/local/nagios/var/status.dat # 該變量用于定義一個狀態(tài)文件,此文件用于保存nagios的當(dāng)前狀態(tài)、注釋和宕機信息等。
status_update_interval=10 # 該變量用于定義狀態(tài)文件(即status.dat)的更新時間間隔,單位是秒,最小更新間隔是1秒。
nagios_user=nagios # 該變量指定了nagios進程使用哪個用戶運行。
nagios_group=nagios # 該變量用于指定nagios使用哪個用戶組運行。
check_external_commands=1 # 該變量用于設(shè)置是否允許nagios在web監(jiān)控界面運行cgi命令;
# 也就是是否允許nagios在web界面下執(zhí)行重啟nagios、停止主機/服務(wù)檢查等操作;
# “1”為運行,“0”為不允許。
command_check_interval=10s # 該變量用于設(shè)置nagios對外部命令檢測的時間間隔,如果指定了一個數(shù)字加一個"s"(如10s);
# 那么外部檢測命令的間隔是這個數(shù)值以秒為單位的時間間隔;
# 如果沒有用"s",那么外部檢測命令的間隔是以這個數(shù)值的“時間單位”的時間間隔。
interval_length=60 # 該變量指定了nagios的時間單位,默認值是60秒,也就是1分鐘;
# 即在nagios配置中所有的時間單位都是分鐘。
6.4 驗證nagios 配置文件的正確性
nagios 在驗證配置文件方面做的非常到位,只需通過一個命令即可完成:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
![基于Linux下Nagios的安裝與配置說明介紹[圖] 基于Linux下Nagios的安裝與配置說明介紹[圖]](/uploads/allimg/210928/1406421K7-27.jpg)
看到上面這些信息就說明沒問題了,然后啟動nagios 服務(wù)。
7.1 啟動nagios
a. 通過初始化腳本啟動nagios
# /etc/init.d/nagios start or # service nagios start
b. 手工方式啟動nagios
# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
7.2 重啟nagios
當(dāng)修改了配置文件讓其生效時,需要重啟/重載nagios服務(wù)。
a. 通過初始化腳本來重啟nagios
# /etc/init.d/nagios reload or # /etc/init.d/nagios restart or # service nagios restart
b. 通過web監(jiān)控頁重啟nagios
可以通過web監(jiān)控頁的 "process info" -> "restart the nagios process"來重啟nagios
c. 手工方式平滑重啟
# kill -hup <nagios_pid>
7.3 停止nagios
a. 通過初始化腳本關(guān)閉nagios服務(wù)
# /etc/init.d/nagios stop or # service nagios stop
b. 通過web監(jiān)控頁停止nagios
可以通過web監(jiān)控頁的 "process info" -> "shutdown the nagios process"來停止nagios
c. 手工方式停止nagios
# kill <nagios_pid>
八、查看初步配置情況
8.1 啟動完成之后,登錄nagios web監(jiān)控頁 查看相關(guān)信息。
8.2 點擊左面的current status -> hosts 可以看到所定義的三臺主機已經(jīng)全部up了。
8.3 點擊current status -> services 查看服務(wù)監(jiān)控情況。
看到nagios-linux和nagios-server的服務(wù)狀態(tài)已經(jīng)ok了,但是nagios-windows的服務(wù)狀態(tài)為critical,status information 提示connection refused。因為nagios-windows上還未安裝插件,內(nèi)部服務(wù)還無法查看,所以出現(xiàn)這種情況。將在下面具體講解。
九、利用nrpe監(jiān)控遠程linux上的“本地信息”
上面已經(jīng)對遠程linux 主機是否存活做了監(jiān)控,而判斷遠程機器是否存活,我們可以使用ping 工具對其監(jiān)測。還有一些遠程主機服務(wù),例如ftp、ssh、http,都是對外開放的服務(wù),即使不用nagios,我們也可以試的出來,隨便找一臺機器看能不能訪問這些服務(wù)就行了。但是對于像磁盤容量,cpu負載這樣的“本地信息”,nagios只能監(jiān)測自己所在的主機,而對其他的機器則顯得有點無能為力。畢竟沒得到被控主機的適當(dāng)權(quán)限是不可能得到這些信息的。為了解決這個問題,nagios有這樣一個附加組件--“nrpe”,用它就可以完成對linux 類型主機"本地信息”的監(jiān)控。
9.1 nrpe 工作原理
check_nrpe 插件,位于監(jiān)控主機上nrpe daemon,運行在遠程的linux主機上(通常就是被監(jiān)控機)
按照上圖,整個的監(jiān)控過程如下:
當(dāng)nagios 需要監(jiān)控某個遠程linux 主機的服務(wù)或者資源情況時:
nagios 會運行check_nrpe 這個插件,告訴它要檢查什么;check_nrpe 插件會連接到遠程的nrpe daemon,所用的方式是ssl;nrpe daemon 會運行相應(yīng)的nagios 插件來執(zhí)行檢查;nrpe daemon 將檢查的結(jié)果返回給check_nrpe 插件,插件將其遞交給nagios做處理。
注意:nrpe daemon 需要nagios 插件安裝在遠程的linux主機上,否則,daemon不能做任何的監(jiān)控。
9.2 在被監(jiān)控機(nagios-linux)上
a. 增加用戶&設(shè)定密碼
# useradd nagios
# passwd nagios
b. 安裝nagios 插件
# tar zxvf nagios-plugins-1.4.16.tar.gz# cd nagios-plugins-1.4.16# ./configure --prefix=/usr/local/nagios# make && make install
這一步完成后會在/usr/local/nagios/下生成三個目錄include、libexec和share。
![基于Linux下Nagios的安裝與配置說明介紹[圖] 基于Linux下Nagios的安裝與配置說明介紹[圖]](/uploads/allimg/210928/1406424627-32.jpg)
修改目錄權(quán)限
# chown nagios.nagios /usr/local/nagios# chown -r nagios.nagios /usr/local/nagios/libexec
c. 安裝nrpe
# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz# tar zxvf nrpe-2.13.tar.gz# cd nrpe-2.13# ./configure
# make all
接下來安裝npre插件,daemon和示例配置文件。
c.1 安裝check_nrpe 這個插件
# make install-plugin
監(jiān)控機需要安裝check_nrpe 這個插件,被監(jiān)控機并不需要,我們在這里安裝它只是為了測試目的。
c.2 安裝deamon
# make install-daemon
c.3 安裝配置文件
# make install-daemon-config
現(xiàn)在再查看nagios 目錄就會發(fā)現(xiàn)有5個目錄了
按照安裝文檔的說明,是將nrpe deamon作為xinetd下的一個服務(wù)運行的。在這樣的情況下xinetd就必須要先安裝好,不過一般系統(tǒng)已經(jīng)默認安裝了。
d. 安裝xinted 腳本
# make install-xinetd
可以看到創(chuàng)建了這個文件/etc/xinetd.d/nrpe。
編輯這個腳本:
在only_from 后增加監(jiān)控主機的ip地址。
編輯/etc/services 文件,增加nrpe服務(wù)
重啟xinted 服務(wù)
# service xinetd restart
查看nrpe 是否已經(jīng)啟動
可以看到5666端口已經(jīng)在監(jiān)聽了。
e. 測試nrpe是否則正常工作
使用上面在被監(jiān)控機上安裝的check_nrpe 這個插件測試nrpe 是否工作正常。
# /usr/local/nagios/libexec/check_nrpe -h localhost
會返回當(dāng)前nrpe的版本
也就是在本地用check_nrpe連接nrpe daemon是正常的。
注:為了后面工作的順利進行,注意本地防火墻要打開5666能讓外部的監(jiān)控機訪問。
f. check_nrpe 命令用法
查看check_nrpe 命令用法
# /usr/local/nagios/libexec/check_nrpe –h
可以看到用法是:
check_nrpe –h 被監(jiān)控的主機 -c 要執(zhí)行的監(jiān)控命令
注意:-c 后面接的監(jiān)控命令必須是nrpe.cfg 文件中定義的。也就是nrpe daemon只運行nrpe.cfg中所定義的命令。
g. 查看nrpe的監(jiān)控命令
[root@nagiso-linux etc]# cat nrpe.cfg |grep -v "^#"|grep -v "^$"
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1
dont_blame_nrpe=0
debug=0
command_timeout=60
connection_timeout=300
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
[root@nagiso-linux etc]#
紅色部分是命令名,也就是check_nrpe 的-c 參數(shù)可以接的內(nèi)容,等號 “=” 后面是實際執(zhí)行的插件程序(這與commands.cfg 中定義命令的形式十分相似,只不過是寫在了一行)。也就是說check_users 就是等號后面/usr/local/nagios/libexec/check_users -w 5 -c 10 的簡稱。
我們可以很容易知道上面這5行定義的命令分別是檢測登陸用戶數(shù),cpu負載,sda1的容量,僵尸進程,總進程數(shù)。各條命令具體的含義見插件用法(執(zhí)行“插件程序名 –h”)。
由于-c 后面只能接nrpe.cfg 中定義的命令,也就是說現(xiàn)在我們只能用上面定義的這五條命令。我們可以在本機實驗一下。
9.3 在監(jiān)控主機(nagios-server)上
之前已經(jīng)將nagios運行起來了,現(xiàn)在要做的事情是:
安裝check_nrpe 插件;在commands.cfg 中創(chuàng)建check_nrpe 的命令定義,因為只有在commands.cfg 中定義過的命令才能在services.cfg 中使用;創(chuàng)建對被監(jiān)控主機的監(jiān)控項目;
9.3.1 安裝check_nrpe 插件
# tar zxvf nrpe-2.13.tar.gz # cd nrpe-2.13# ./configure# make all# make install-plugin
只運行這一步就行了,因為只需要check_nrpe插件。
在nagios-linux 上我們已經(jīng)裝好了nrpe,現(xiàn)在我們測試一下監(jiān)控機使用check_nrpe 與被監(jiān)控機運行的nrpe daemon之間的通信。
看到已經(jīng)正確返回了nrpe的版本信息,說明一切正常。
9.3.2 在commands.cfg中增加對check_nrpe的定義
# vi /usr/local/nagios/etc/objects/commands.cfg
在最后面增加如下內(nèi)容:
意義如下:
# 'check_nrpe' command definitiondefine command{ command_name check_nrpe # 定義命令名稱為check_nrpe,在services.cfg中要使用這個名稱. command_line $user1$/check_nrpe -h $hostaddress$ -c $arg1$ #這是定義實際運行的插件程序. # 這個命令行的書寫要完全按照check_nrpe這個命令的用法,不知道用法的就用check_nrpe –h查看. }
-c 后面帶的$arg1$ 參數(shù)是傳給nrpe daemon 執(zhí)行的檢測命令,之前說過了它必須是nrpe.cfg 中所定義的那5條命令中的其中一條。在services.cfg 中使用check_nrpe 的時候要用 “!” 帶上這個參數(shù)。
9.3.3 定義對nagios-linux 主機的監(jiān)控
下面就可以在services.cfg 中定義對nagios-linux 主機的監(jiān)控了。
define service{
use local-service
host_name nagios-linux
service_description current load
check_command check_nrpe!check_load
}
define service{
use local-service
host_name nagios-linux
service_description check disk sda1
check_command check_nrpe!check_load
}
define service{
use local-service
host_name nagios-linux
service_description total processes
check_command check_nrpe!check_total_procs
}
define service{
use local-service
host_name nagios-linux
service_description current users
check_command check_nrpe!check_users
}
define service{
use local-service
host_name nagios-linux
service_description check zombie procs
check_command check_nrpe!check_zombie_procs
}
還有一個任務(wù)是要監(jiān)控nagios-linux 的swap 使用情況。但是在nrpe.cfg 中默認沒有定義這個監(jiān)控功能的命令。怎么辦?手動在nrpe.cfg 中添加,也就是自定義nrpe命令。
現(xiàn)在我們要監(jiān)控swap 分區(qū),如果空閑空間小于20%則為警告狀態(tài) -> warning;如果小于10%則為嚴重狀態(tài) -> critical。我們可以查得需要使用check_swap插件,完整的命令行應(yīng)該是下面這樣。
# /usr/local/nagios/libexec/check_swap -w 20% -c 10%
在被監(jiān)控機(nagios-linux)上增加check_swap 命令的定義
# vi /usr/local/nagios/etc/nrpe.cfg
增加下面這一行
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
我們知道check_swap 現(xiàn)在就可以作為check_nrpe 的-c 的參數(shù)使用了
修改了配置文件,當(dāng)然要重啟。
如果你是以獨立的daemon運行的nrpe,那么需要手動重啟;如果你是在xinetd 下面運行的,則不需要。
由于本實驗中nrpe 是xinetd 下運行的,所以不需要重啟服務(wù)。
在監(jiān)控機(nagios-server)上增加這個check_swap 監(jiān)控項目
define service{ use local-service host_name nagios-linux service_description check swap check_command check_nrpe!check_swap }
同理,nagios-linux 上我還開啟了http 服務(wù),需要監(jiān)控一下,按照上面的做法,在被監(jiān)控機(nagios-linux)上增加check_http 命令的定義
# vi /usr/local/nagios/etc/nrpe.cfg
增加下面這一行
command[check_http]=/usr/local/nagios/libexec/check_http -i 127.0.0.1
在監(jiān)控機(nagios-server)上增加check_http 監(jiān)控項目
define service{ use local-service host_name nagios-linux service_description http check_command check_nrpe!check_http }
所有的配置文件已經(jīng)修改好了,現(xiàn)在重啟nagios。
# service nagios restart
9.3.4 查看配置情況
登錄nagios web監(jiān)控頁 查看相關(guān)信息。
可以看到,對于nagios-server 和nagios-linux 上的相關(guān)服務(wù)的監(jiān)控已經(jīng)成功了,還有nagios-windows 上的服務(wù)還沒有定義,下面講到。
十、利用nsclient++監(jiān)控遠程windows上的“本地信息”
在nagios的libexec下有check_nt這個插件,它就是用來檢查windows機器的服務(wù)的。其功能類似于check_nrpe。不過還需要搭配另外一個軟件nsclient++,它則類似于nrpe。
nsclient++的原理如下圖
可以看到nsclient與nrpe最大的區(qū)別就是:
被監(jiān)控機上安裝有nrpe,并且還有插件,最終的監(jiān)控是由這些插件來進行的。當(dāng)監(jiān)控主機將監(jiān)控請求發(fā)給nrpe后,nrpe調(diào)用插件來完成監(jiān)控。nsclient++則不同,被監(jiān)控機上只安裝nsclient,沒有任何的插件。當(dāng)監(jiān)控主機將監(jiān)控請求發(fā)給nsclient++后,nsclient直接完成監(jiān)控,所有的監(jiān)控是由nsclient完成的。
這也說明了nsclient++的一個很大的問題:不靈活、沒有可擴展性。它只能完成自己本身包含的監(jiān)控操作,不能由一些插件來擴展。好在nsclient++已經(jīng)做的不錯了,基本上可以完全滿足我們的監(jiān)控需求。
10.1 安裝nsclient++
從http://www.nsclient.org/nscp/downloads 下載nsclient++-0.2.7.zip
解壓到c盤根目錄。
打開cmd 切換到c: sclient++-0.2.7
執(zhí)行nsclient++ /install 進行安裝
執(zhí)行nsclient++ systray (注意大小寫),這一步是安裝系統(tǒng)托盤,時間稍微有點長。
看到下圖就說明nsclient服務(wù)已經(jīng)安裝上了
雙擊打開,點“登錄”標(biāo)簽,在“允許服務(wù)與桌面交互”前打勾。
編輯c: sclient++-0.2.7下的nsc.ini文件。
將 [modules]部分的所有模塊前面的注釋都去掉,除了checkwmi.dll 和 remoteconfiguration.dll 這兩個。
在[settings]部分設(shè)置'password'選項來設(shè)置密碼,作用是在nagios連接過來時要求提供密碼。這一步是可選的,我這里設(shè)置為'123456'。
將[settings]部分'allowed_hosts'選項的注釋去掉,并且加上運行nagios的監(jiān)控主機的ip。各ip之間以逗號相隔。這個地方是支持子網(wǎng)的,如果寫成192.168.1.0/24則表示該子網(wǎng)內(nèi)的所有機器都可以訪問。如果這個地方是空白則表示所有的主機都可以連接上來。
注意是[settings]部分的,因為[nsclient]部分也有這個選項。
必須保證[nsclient]的'port'選項并沒有被注釋,并且它的值是'12489',這是nsclient的默認監(jiān)聽端口。
在cmd 中執(zhí)行nsclient++ /start啟動服務(wù),注意所在目錄是c: sclient++-0.2.7
這時在桌面右下角的系統(tǒng)托盤處會出現(xiàn)一個黃色的m字樣的圖標(biāo)
查看服務(wù)
已經(jīng)正常啟動了。
注意服務(wù)默認設(shè)的是“自動”,也就是說是開機自動啟動的。
在cmd 里面執(zhí)行netstat –an 可以看到已經(jīng)開始監(jiān)聽tcp的12489端口了。
這樣外部就可以訪問了嗎?
錯!
防火墻也要打開tcp的12489端口,否則nagios 檢查此服務(wù)的時候會報錯。
這樣被監(jiān)控機的配置就搞定了,它就等待nagios 發(fā)出某個監(jiān)控請求,然后它執(zhí)行請求將監(jiān)控的結(jié)果發(fā)回到nagios監(jiān)控主機上。
之前已經(jīng)在監(jiān)控主機(nagios-server)上對windows 主機的監(jiān)控做了配置,但是commands.cfg 中默認沒有設(shè)置密碼項,所以要修改一下,增加"-s 123456",如下:
# 'check_nt' command definitiondefine command{ command_name check_nt command_line $user1$/check_nt -h $hostaddress$ -p 12489 -s 123456 -v $arg1$ $arg2$ }
現(xiàn)在打開nagios web監(jiān)控頁便可查看到相關(guān)信息了。
可以看到有錯誤:nsclient - error: pdh collection thread not running.
google 一下,是由于操作系統(tǒng)語言的問題,好像nsclient 默認支持的語言并不多,具體可以百度一下。
查看nsclient的日志c: sclient++-0.2.7nsclient.log,信息如下:
2013-02-02 22:05:30: error:.pdhcollector.cpp:98: you need to manually configure performance counters!
需要手動配置performance counters。
打開c: sclient++-0.2.7counters.defs文件,復(fù)制文件里面"english us"那部分內(nèi)容,粘貼到counters.defs 文件的最后,修改description = "chinese"。
修改完之后,在mmc中重啟nsclient 服務(wù)。
然后查看日志,內(nèi)容如下:
在正常執(zhí)行了。
打開nagios web監(jiān)控頁查看。
執(zhí)行成功,但是w3svc服務(wù)為unknown 狀態(tài)。查資料,需要開戶windows 的iis服務(wù)。
打開“控制面板”進行安裝。
安裝完畢后,再到nagios web監(jiān)控頁查看,全部監(jiān)控正常。
十一、nagios郵件報警的配置
11.1 安裝sendmail 組件
首先要確保sendmail 相關(guān)組件的完整安裝,我們可以使用如下的命令來完成sendmail 的安裝:
# yum install -y sendmail*
然后重新啟動sendmail服務(wù):
# service sendmail restart
然后發(fā)送測試郵件,驗證sendmail的可用性:
# echo "hello world" | mail david.tang@bsmart.cn
11.2 郵件報警的配置
在上面我們已經(jīng)簡單配置過了/usr/local/nagios/etc/objects/contacts.cfg 文件,nagios 會將報警郵件發(fā)送到配置文件里的e-mail 地址。
11.3 nagios 通知
problem
recovery
linux下nagios安裝配置完畢。