grep ^User /usr/local/apache2/conf/httpd.conf
DirectoryIndex index.php
#setting for nagios
ScriptAlias /nagios/cgi-bin /opt/nagios/sbin
<Directory "/opt/nagios/sbin">
Options ExecCGI AllowOverride None Order allow,deny Allow from all AuthName "Nagios Access" AuthType Basic AuthUserFile /opt/nagios/etc/htpasswd
//用於此目錄訪問身份驗證的文件
Require valid-user </Directory>
Alias /nagios /opt/nagios/share
<Directory "/opt/nagios/share">
Options None AllowOverride None Order allow,deny Allow from all AuthName "Nagios Access" AuthType Basic AuthUserFile /opt/nagios/etc/htpasswd
//用於此目錄訪問身份驗證的文件
Require valid-user </Directory>
|
/opt/apache/bin/htpasswd -mbc /opt/nagios/etc/htpasswd test 123456
[url]http://10.0.2.216/nagios[/url] 會彈出對話框要求輸入用戶名密碼,輸入test,密碼12345,就可以進入nagios的主頁面了。但是可以發現什麼也點不開,因爲nagios還沒啓動呢!下面的工作就是修改配置文件,增加要監控的主機和服務。
聯繫人
|
contact
|
出了問題像誰報告?一般當然是系統管理員了
|
監控時間段
|
timeperiod
|
7X24小時不間斷還是週一至週五,或是自定義的其他時間段
|
被監控主機
|
host
|
所需要監控的服務器,當然可以是監控機自己
|
監控命令
|
command
|
nagios發出的哪個指令來執行某個監控,這也是自己定義的
|
被監控的服務
|
service
|
例如主機是否存活,80端口是否開,磁盤使用情況或者自定義的服務等
|
cfg_file=/usr/local/nagios/etc /objects /contactgroups.cfg //聯繫組配置文件路徑 cfg_file=/usr/local/nagios/etc /objects /contacts.cfg //聯繫人配置文件路徑
cfg_file=/usr/local/nagios/etc /objects /hostgroups.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 //監視時段配置文件路徑
|
將check_external_commands=0改爲check_external_commands=1
authorized_for_system_information=nagiosadmin,test authorized_for_configuration_information=nagiosadmin,test
authorized_for_system_commands=test //多個用戶之間用逗號隔開
authorized_for_all_services=nagiosadmin,test
authorized_for_all_hosts=nagiosadmin,test
authorized_for_all_service_commands=nagiosadmin,test
authorized_for_all_host_commands=nagiosadmin,test
|
那麼上述用戶名打那裏來的呢?是執行命令 /opt/apache/bin/htpasswd -mbc /opt/nagios/etc/htpasswd test 所生成的,這個要注意,不能隨便加沒有存在的驗證用戶,爲了安全起見,不要添加過多的驗證用戶。
define timeperiod{ timeperiod_name 24x7 //時間段的名稱,這個地方不要有空格
alias 24 Hours A Day,7Days 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 contact{
contact_name test //聯繫人的名稱,這個地方不要有空格
alias sys admin
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-by-email
host_notification_commands host-notify-by-email
email [email protected]
pager 1338757xxxx
address1 [email protected]
address2 555-555-5555
}
|
service_notification_period 24x7
服務出了狀況通知的時間段,這個時間段就是上面在timeperiods.cfg中定義的.
|
host_notification_period 24x7
主機出了狀況通知的時間段, 這個時間段就是上面在timeperiods.cfg中定義的
|
service_notification_options w,u,c,r
當服務出現w—報警(warning),u—未知(unkown),c—嚴重(critical),或者r—從異常情況恢復正常,在這四種情況下通知聯繫人.
|
host_notification_options d,u,r
當主機出現d—當機(down),u—返回不可達(unreachable),r—從異常情況恢復正常,在這3種情況下通知聯繫人
|
service_notification_commands notify-by-email
服務出問題通知採用的命令notify-by-email,這個命令是在commands.cfg中定義的,作用是給聯繫人發郵件.至於commands.cfg之後將專門介紹
|
host_notification_commands host-notify-by-email
同上,主機出問題時採用的也是發郵件的方式通知聯繫人
|
email [email protected]
很明顯,聯繫的人email地址
|
pager 1338757xxxx
聯繫人的手機,如果支持短信的通知的話,這個就很有用了.
|
alias是聯繫人別名,address是地址 意義不大.
|
define contactgroup{
contactgroup_name sagroup //聯繫人組的名稱,同樣不能空格
alias System Administrators //別名 members test //組的成員,來自於上面定義的contacts.cfg,如果有多個聯繫人則以逗號相隔
} |
define host{
host_name nagios-server //被監控主機的名稱,最好別帶空格
alias nagios server //別名 address 10.0.2.216 //被監控主機的IP地址,我現在暫時先填本機的IP check_command check-host-alive //監控的命令check-host-alive,這個命令來自commands.cfg,用來監控主機是否存活 max_check_attempts 5 //檢查失敗後重試的次數 check_period 24x7 //檢查的時間段24x7,同樣來自於我們之前在timeperiods.cfg中定義的 contact_groups sagroup
//聯繫人組,上面在contactgroups.cfg中定義的sagroup notification_interval 10
//提醒的間隔,每隔10秒提醒一次 notification_period 24x7
//提醒的週期, 24x7,同樣來自於我們之前在timeperiods.cfg中定義的 notification_options d,u,r
//指定什麼情況下提醒,具體含義見之前contacts.cfg部分的介紹
} |
define hostgroup{
hostgroup_name sa-servers //主機組名稱 alias sa Servers //別名 members nagios-server //組的成員主機,多個主機以逗號相隔,必須是上面hosts.cfg中定義的
} |
#service definition define service{
host_name nagios-server
//被監控的主機,hosts.cfg中定義的
service_description check-host-alive
//這個監控項目的描述(也可以說是這個項目的名稱),可以空格,我們這裏定義的是監控這個主機是不是存活
check_command check-host-alive
//所用的命令,是commands.cfg中定義的
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
//監控的時間段,是timeperiods.cfg中定義的
notification_interval 10
notification_period 24x7
//通知的時間段, ,是timeperiods.cfg中定義的 notification_options w,u,c,r
//在監控的結果是wucr時通知聯繫人,具體含義看前文.
contact_groups sagroup
//聯繫人組,是contactgroups.cfg中定義的
}
|
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check |
n 登陸[url]http://10.0.2.216 /nagios/[/url]就可以查看上面配置的監控內容了:
要實現資源和服務兩個方面的監控,需要配置服務器端(nagios server)及被監控端(NRPE-nagios remote plugin executor).爲了能順利地、有條理的部署nagios 監控平臺,一個好的工作風格是在nagios server 自身實現服務和資源的監控,然後再在其他監控端部署nrpe,從nagios server 端用check_nrpe 測試通過後,再在nagios server 配置文件中逐一增加監控項目。當然如果只是監控服務而不監控主機資源,則被監控端不做任何nrpe 的安裝。爲了方便初學者更容易上手和成功,我們就從簡單的步驟開始。
1)增加用戶
useradd nagios
passwd nagios
2)安裝nagios 插件
tar zvxf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/opt/nagios
make
make install
3)修改目錄權限
chown -R nagios:nagios /opt/nagios
4)安裝nrpe
tar zxvf nrpe-2.8.tar.gz
cd nrpe-2.8
./configure –prefix=/opt/nagios
make all
(1) 安裝check_nrpe 這個插件
make install-plugin
(2) 安裝deamon
make install-daemon
(3) 安裝配置文件
make install-daemon-config
(4) 編輯nrpe配置文件
vim /opt/nagios/etc/nrpe.cfg(更改如下)
allowed_hosts=127.0.0.1,192.168.10.8
默認爲allowed_hosts=127.0.0.1
5) 啓動nrpe
/opt/nagios/bin/nrpe -c /opt/nagios/etc/nrpe.cfg -d
6) 查看NRPE 是否已經啓動
netstat -nltp |grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:*
LISTEN 5163/nrpe
7) 測試NRPE 是否則正常工作
之前安裝了check_nrpe 這個插件用於測試,現在就是用的時候.執行
/opt/nagios/libexec/check_nrpe -H localhost
NRPE v2.8
看到已經正確返回了NRPE 的版本信息,說明一切正常
之前已經將nagios 運行起來了,現在要做的事情是: 安裝check_nrpe 插件
在commands.cfg 中創建check_nrpe 的命令定義,因爲只有commands.cfg
中定義過的命令才能在services.cfg 中使用創建對被監控主機的監控項目
1)安裝check_nrpe 插件
# tar -zxvf nrpe-2.8.1.tar.gz
# cd nrpe-2.8.1
# ./configure
# make all
# make install-plugin
只運行這一步就行了,因爲只需要check_nrpe 插件
2)在apache 剛裝好了nrpe,現在我們測試一下監控機使用
check_nrpe 與被監控機運行的nrpe daemon 之間的通信.
# /opt/nagios/libexec/check_nrpe -H 10.0.2.234
NRPE v2.8
看到已經正確返回了NRPE 的版本信息,說明一切正常.
3)在commands.cfg 中增加對check_nrpe 的定義
# vim /opt/nagios/etc/objects/commands.cfg
在最後面增加如下內容
####################################################################
# 'check_nrpe ' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
意義如下
command_name check_nrpe
定義命令名稱爲check_nrpe,在services.cfg 中要使用這個名稱.
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
這是定義實際運行的插件程序.這個命令行的書寫要完全按照check_nrpe 這個命令的用法.不知道用法的就用check_nrpe –h 查看-c 後面帶的$ARG1$參數是傳給nrpe daemon 執行的檢測命令,之前說過了它必須是被監控機nrpe.cfg中所定義的那5 條命令中的其中一條.在services.cfg 中使用check_nrpe 的時候要用!帶上這個參數
查看被控機NRPE的監控命令
# The following examples use hardcoded command arguments... command[check_users]=/opt/nagios/libexec/check_users -w 5 -c 10 command[check_load]=/opt/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 command[check_hda1]=/opt/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1 command[check_zombie_procs]=/opt/nagios/libexec/check_procs -w 5 -c 10 -s Z command[check_total_procs]=/opt/nagios/libexec/check_procs -w 150 -c 200 |
###########################################
舉例:
下面就可以在services.cfg 中定義對apache 主機磁盤容量的監控
define service{
host_name apache
被監控的主機名,這裏注意必須是linux 且運行着nrpe,而且必須是hosts.cfg 中定義的
service_description check-disk
監控項目的名稱
check_command check_nrpe!check_disk
監控命令是check_nrpe,是在commands.cfg 中定義的,帶的參數是check_disk,是
在nrpe.cfg 中定義的
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
像這樣將其餘幾個監控項目加進來.