測試環境:
監控主機:nagios+nagios插件+nrpe+網站平台 192.168.1.210
被監控機:nagios插件+nrpe 192.168.1.211
1、首先在監控主機上安裝nrpe,nagios隻能監控一些外部的資訊,例如:ftp端口有沒有開放,ssh端口有沒有開放,ping值如何之類的,如果想監控linux主機一些本地的資訊如:硬碟使用情況,機器負載等,必須是監控主機通過nagios調用被監控機的nrpe,被監控機的nrpe搜集資訊,然後再返來給監控主機的nagios,這樣的一個過程
監控主機安裝nrpe:
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
在被監控機上安裝nagios插件和nrpe
tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
安裝nrpe的方法與監控主機的nrpe安裝方法一樣
檢查目錄及檔案:
ll /usr/local/nagios/libexec
看看裡面是不是有一大堆check的什麼東西,如果有就對了
2、配置nagios監控主機的配置檔案
nagios的全部配置檔案在:/usr/local/nagios/etc目錄下面
首先cgi.cfg
refresh_rate=30 #nagios首頁的重新整理時間,我設定成30秒自動重新整理
use_authentication=0 #關閉認證功能,當nagios配置好後,建議開回來
這個檔案暫時隻修改了這兩項
然後輪到nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/commands.cfg #nagios可調用的監控指令
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg #聯系人配置
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg #監控時間配置
cfg_file=/usr/local/nagios/etc/objects/templates.cfg #模闆配置
cfg_dir=/usr/local/nagios/etc/services #新添加,把需要添加的主機檔案放進去,就不必在這裡一行行添加
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg #新添加,主機組配置
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #本地資訊監疊
3、現在可以開始添加被監控機
我們上面定義了所有的主機檔案都放在services目錄下,那麼我們在此目錄下建立主機:
vi 192.168.1.211.cfg
内容如下:
define host{
use linux-server
host_name 192.168.1.211
alias 192.168.1.211
address 192.168.1.211
}
define service{
use generic-service
service_description check_ping
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
service_description check_ftp
check_command check_ftp!21
service_description check_ssh
check_command check_ssh
service_description check_http
check_command check_http
上面監控了ping值,ftp服務,ssh服務還有http服務,我拿一個例子來說明
例如這一段,首先define host定義了這台被監控主機,它所用的模闆是linux-server這個,那這個模闆又是在哪裡定義的呢,就是在剛才nagios裡不是有一行模闆配置資訊嗎,就是那個檔案,我打開templates.cfg 檔案并找到linux-server模闆,這模闆的資訊是這樣的:
define host{
name linux-server ; The name of this host template
use generic-host ; This template inherits other values from the generic-host template
check_period 24x7 ; By default, Linux hosts are checked round the clock
check_interval 5 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-host-alive ; Default command to check Linux hosts
notification_period workhours ; Linux admins hate 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 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
這裡的資訊後面都有說明,把他們拿上谷歌翻譯一下便是,
其中check_period 這個是定義監控的時間
check_interval這個是每多少時間執行一次指令
max_check_attempts這個是多少次異常就會報警
這幾個便是常用的配置
回到剛才的例子,然後define service就是定義要監控的東西
host_name 就是被監控機的IP
service_description 指令的描述,會在nagios主面上顯示
check_command 要執行的指令
max_check_attempts 5 異常多少次會報警
normal_check_interval 1 每1分鐘執行一次指令
check_command 這個是根據什麼來填寫的呢,其實就是根據command.cfg這份配置來填寫,打開這份檔案找一個ping的指令來看看
define command{
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
這份是定義ping指令的配置,很簡單,一個名稱,然後就是要執行的東西
-w就是警告的值
-c就是嚴重的值
要看看指令怎麼使用很簡單
/usr/local/nagios/libexec/check_ping -H 192.168.1.211 -w 100,20% -c 200,40%
這樣執行下去,可看見返來的資訊:
PING OK - Packet loss = 0%, RTA = 0.54 ms|rta=0.543000ms;100.000000;200.000000;0.000000 pl=0%;20;40;0
那麼很明顯
執行指令是這樣的
/usr/local/nagios/libexec/check_ping -H 192.168.1.211 -w 100,20% -c 200,40%
而寫在配置檔案上是這樣的:
check_ping!100.0,20%!200.0,50%
check_ping後面是要跟參數的,在寫配置檔案的時候參數是要用“!”這個符号來隔開,要注意順序
明白這個之後,面後都差不多了
現在我們重新開機nagios來看看效果,養成習慣重新開機前先檢查配置檔案有沒有錯誤
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果出現如下資訊就是正常:
Total Warnings: 0
Total Errors: 0
現在可以安心重新開機服務了
service nagios restart
然後在nagios首頁上會看見我們配置的東西出來了:
<a href="http://img1.51cto.com/attachment/201105/224931616.jpg" target="_blank"></a>
我的被監控機上沒有安裝http服務,是以報警了
(未完……太累了睡覺去,明天再寫)
回來繼續配置!
4、利用nrpe監控linux主機的本地資訊
首先是解決nagios怎麼去調用nrpe指令,之前我們配置時有說過nagios調用的指令是從command.cfg檔案中定義過的,那麼要使用nrpe也須要在該檔案中定義
vi command.cfg
在檔案中添加以下配置:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
在添加監控資訊之前我們先測試一下監控主機與被監控機的Nrpe的連通性
首先在被監控機上配置允許的監控主機:
vi nrpe.cfg
allowed_hosts=127.0.0.1,192.168.1.210 #添加上我的監控主機的IP
然後在被監控機上啟動nrpe服務:
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
檢視端口是否正常運作:
[root@localhost ~]# netstat -tunlp | grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 4304/nrpe
我們在監控主機上運作一下測試指令看看結果:
/usr/local/nagios/libexec/check_nrpe -H 192.168.1.211
運作這條指令會傳回被監控機的nrpe版本資訊
NRPE v2.8.1
這樣就說明兩邊的通信沒問題!
監控主機的nagios是通過調用被監控機的nrpe來擷取資訊,那麼我們先在被監控機的nrpe配置上添加要搜尋的資訊,也就是一些指令,然後nagios就通過nrpe調用這些指令來擷取資訊
這些指令是在nrpe.cfg檔案中定義:
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_sda3]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda3
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
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
在檔案最後我看見有這樣一些指令,check_XXX 這個名字是可以自己定義,隻要不重複就可以,後面就是你定義的這條指令是調用哪個東西來擷取資訊,後面跟的這些指令路徑不難發現全是在libexec檔案裡,我們之前說nagios是調用command.cfg定義的指令,而command.cfg指令也是在libexec裡,那麼整個流程我們可以看成是這樣:
監控外部資訊是這樣一個過程:
nagios——command.cfg——libexec
監控一些要登陸了機器才能檢視的本地資訊側是這樣一個過程:
nagios——command.cfg(check_nrpe)——nrpe.cfg——libexec
在nrpe.cfg檔案中定義的幾條預設的配置可以直接使用,我們在使用前先測試一下,看看需不需對指令的參數進行一些調整,以符合我們實際情況:
在監控主機上運作:
/usr/local/nagios/libexec/check_nrpe -H 192.168.1.211 -c check_users
這條指令會出現這樣的資訊:
USERS OK - 3 users currently logged in |users=3;5;10;0
如果需要調整參數須在被監控機上做調整,然後我們把這些預設已有的指令添加在我們的被監控機上
在監控主機上編輯我們剛才建立的192.168.1.211.cfg檔案,添加資訊:
define service{
service_description check_users
check_command check_nrpe!check_users
service_description check_load
check_command check_nrpe!check_load
service_description check_sda3
check_command check_nrpe!check_sda3
service_description check_zombie_procs
check_command check_nrpe!check_zombie_procs
service_description check_total_procs
check_command check_nrpe!check_total_procs
service_description check_swap
check_command check_nrpe!check_swap
我還需要監控sda1這個分區?我要怎麼辦?
那麼我們先在被監控機的nrpe.cfg上添加:
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
修改完記得重新開機nrpe服務!重新開機方法不多說了Kill掉程序,再重新運作就是!
然後在監控主機的192.168.1.211.cfg檔案中再添加:
service_description check_sda1
check_command check_nrpe!check_sda1
重新開機nagios:
service nagios restart
打開nagios監控首頁看看:
<a href="http://img1.51cto.com/attachment/201105/115332403.jpg" target="_blank"></a>
上面就是我們剛才添加的資訊,如果還想添加更多的監控,就按照上面的步驟操作就可以,關鍵是要明白libexec裡面的指令怎麼使用,這個就要發揮谷歌的作用了。
本文轉自:http://lihuipeng.blog.51cto.com/3064864/570698
本文轉自奔跑在路上部落格51CTO部落格,原文連結http://blog.51cto.com/qiangsh/1565185如需轉載請自行聯系原作者
qianghong000