Nagios的基本思想就是nagios server 獲取被監(jiān)控節(jié)點(diǎn)(monitored server)的相關(guān)信息,通過CGI(Web GUI)或者郵件等方式展示給管理員。
信息的獲取主要分為主動拿,被動得到,其中主動拿分直接去被監(jiān)控節(jié)點(diǎn)取或者中間數(shù)據(jù)庫節(jié)點(diǎn)取。
如果是個(gè)個(gè)被監(jiān)控節(jié)點(diǎn)直接去取,機(jī)器一多,nagios所在的系統(tǒng)CPU可能會扛不住,所以我們系統(tǒng)在測試環(huán)境中中使用了被動獲取NSCA的方式,以后可能會轉(zhuǎn)到NDOUtils方式,通過數(shù)據(jù)庫實(shí)現(xiàn)監(jiān)控端和被監(jiān)控端的分離。
NSCA具體工作過程:
1. 數(shù)據(jù)生成: 被監(jiān)控端程序定時(shí)收集數(shù)據(jù)并發(fā)送到Nagios服務(wù)端,Nagios服務(wù)端的NSCA進(jìn)程收到數(shù)據(jù),數(shù)據(jù)放到指定文件(nagios.cfg和nsca.cfg中指定)。
2. 數(shù)據(jù)處理: Nagios實(shí)時(shí)處理指定文件,將數(shù)據(jù)存到queue,同時(shí)Nagios會定期檢查queue,對數(shù)據(jù)做最終處理,例如:發(fā)送alert郵件,記錄日志等。
監(jiān)控系統(tǒng)的最終模式:Nagios + NSCA + Pnp4nagios(bulknpcd)。
NSCA負(fù)責(zé)收集數(shù)據(jù),Nagios把收集的數(shù)據(jù)按照Pnp4nagios要求的格式寫到spool文件,每隔15分鐘把文件移動到npcd處理的spool文件夾,npcd調(diào)用process_perfdata.pl處理spool文件夾中的文件,最后存到rrd里,在Nagios監(jiān)控頁面加入pnp4nagios的web訪問url,就可以查看圖形化監(jiān)控?cái)?shù)據(jù)了。
接下來說說我們Nagios的安裝,
Nagios Server端安裝
一臺liunx server(CentOS),安裝以下安裝包 的的對應(yīng)版本
1. 基本nagios以及Web GUI安裝包
httpd-*.tar.gz,php-*.tar.gz, nagios-*.tar.gz, nagios-plugins-*.tar.gz
安裝步驟
2. 圖形化性能數(shù)據(jù)安裝包
pnp4nagios-*.tar.gz
安裝步驟
3. Linux 系統(tǒng)監(jiān)控插件
主動方式:nrpe-*.tar.gz,被動方式:nsca-*.tar.gz
nsca安裝步驟
Note: inetd或者xinetd沒有使用在我們的系統(tǒng)中
Nagios monitored Clients
對于NSCA模式,我們使用了Python腳本+Cron Job(crontab)的方式來定時(shí)按格式發(fā)送數(shù)據(jù)到Nagios Server,所以被監(jiān)控設(shè)備上面只要安裝了Python就可以了。
現(xiàn)在說說在我們系統(tǒng)中如何是用NSCA方式來獲取我們需要的被監(jiān)控節(jié)點(diǎn)的資源使用情況信息。
Nagios Server端的基本配置:
1. nagios web gui 訪問配置,系統(tǒng)中使用的是nagios core中推薦的配置方式,通過apache httpd訪問nagios web,安裝nagios的時(shí)候會自動尋找系統(tǒng)中安裝的httpd,把nagios的httpd文件放到httpd的conf.d目錄下,這樣,只要生成一次nagios web訪問的密碼就可以通過http://localhost/nagios/訪問nagios web.
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
2. 在Nagios中定義要監(jiān)控的機(jī)器和要監(jiān)控的服務(wù)
2. 1 在/usr/local/nagios/etc/objects/中定義被監(jiān)聽節(jié)點(diǎn)和需要被監(jiān)聽的服務(wù),例如:
/usr/local/nagios/etc/objects/system_1.cfg
## define hosts
# system_1 oam_1
define host{
use linux-server,host-pnp
host_name system_1_oam_1
address ip>
contact_groups system_1_admins,systems_admins
}
......
# system_1 traffic_1
define host{
use linux-server,host-pnp
host_name system_1_traffic_1
address ip>
contact_groups system_1_admins,systems_admins
}
......
## define services
# system_1 oam_1/p>
p>define service{
use passive_service,srv-pnp
service_description CPU_Status
host_name system_1_oam_1
contact_groups system_1_admins,systems_admins
}
define service{
use passive_service,srv-pnp
service_description Memory_Status
host_name system_1_oam_1
contact_groups system_1_admins,systems_admins
}
....../p>
p># system_1 traffic_1
define service{
use passive_service,srv-pnp
service_description CPU_Status
host_name system_1_traffic_1
contact_groups system_1_admins,systems_admins
}
Note:
a. passive_service是NSCA需要的配置
b. host-pnp和srv-pnp是pnp4nagios要用的配置
2.2 把定義的配置文件添加到/usr/local/nagios/etc/nagios.cfg中
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg/p>
p>cfg_file=/usr/local/nagios/etc/objects/system_1.cfg
cfg_file=/usr/local/nagios/etc/objects/system_2.cfg
......
nagios_user=nagios
nagios_group=nagios
lock_file=/usr/local/nagios/var/nagios.lock
status_update_interval=10
3. 對不同的系統(tǒng)添加系統(tǒng)監(jiān)控管理員
3.1
/usr/local/nagios/etc/objects/contactgroups.cfg
define contactgroup{
contactgroup_name system_1_admins
alias system_1_admins
members oam-1,traffic-1
}
......
3.2
/usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name oam-1 ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias oam-1 ; Full name of user
service_notification_options c
host_notification_options d
email a href="mailto:nobody@localhost.com">nobody@localhost.com/a> ; ***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
define contact{
contact_name traffic-1 ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias traffic-1 ; Full name of user
email a href="mailto:nobody@localhost.com">nobody@localhost.com/a> ; ***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
......
4. NSCA配置
4.1 NSCA端:如果沒有端口沖突之類的問題,/usr/local/nagios/etc/nsca.cfg可以作為默認(rèn)配置直接使用
Note: 當(dāng)Nagios端有問題的時(shí)候,nsca本身可以保存收到的數(shù)據(jù),要小心數(shù)據(jù)量大小的問題,監(jiān)控機(jī)器多的時(shí)候,Nagios掛的時(shí)間長了點(diǎn),文件就很容易變的很大
4.2 Nagios端:
4.2.1 修改/usr/local/nagios/etc/objects/templates.cfg,增加被動檢測模版
define service{
use generic-service
name passive_service
active_checks_enabled 0
passive_checks_enabled 1 # We want only passive checking
flap_detection_enabled 0
register 0 # This is a template, not a real service
is_volatile 0
check_period 24x7
max_check_attempts 1
normal_check_interval 5
retry_check_interval 1
check_freshness 0
contact_groups system_admins
check_command check_dummy!0
notification_interval 120
notification_period 24x7
notification_options w,u,c,r
stalking_options w,c,u
}
4. 2.2 修改/usr/local/nagios/etc/commands.cfg,增加命令定義如何處理NSCA發(fā)送來的數(shù)據(jù)
# 'check_nsca' command definition
define command{
command_name check_dummy
command_line $USER1$check_dummy$ARG1$
}
Note: check_dummy很簡單,處理Nagios中OK,WARNING,CRITICAL,UNKNOWN
nagios:/usr/local/nagios/etc # /usr/local/nagios/libexec/check_dummy 0
OK
nagios:/usr/local/nagios/etc # /usr/local/nagios/libexec/check_dummy 1
WARNING
nagios:/usr/local/nagios/etc # /usr/local/nagios/libexec/check_dummy 2
CRITICAL
nagios:/usr/local/nagios/etc # /usr/local/nagios/libexec/check_dummy 3
UNKNOWN
4.2.3 檢查/usr/local/nagios/etc/nagios.cfg已經(jīng)可以處理外部NSCA發(fā)送來的數(shù)據(jù),默認(rèn)是沒有問題的
accept_passive_service_checks=1
accept_passive_host_checks=1
check_external_commands=1
command_check_interval=-1
command_file=/usr/local/nagios/var/rw/nagios.cmd
log_passive_checks=1
5. 查看Nagios GUI中新加的host和service已經(jīng)存在
6. 添加pnp4nagios bulk+npcd 模式監(jiān)聽系統(tǒng)
6.1 修改/usr/local/nagios/etc/nagios.cfg
#處理performance數(shù)據(jù)
process_performance_data=1
#host_perfdata_command=process-host-perfdata
#service_perfdata_command=process-service-perfdata
#performance數(shù)據(jù)存儲文件
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
#performance數(shù)據(jù)存儲格式
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$\tSERVICEOUTPUT::$SERVICEOUTPUT$
#performance文件存儲方式
host_perfdata_file_mode=a
service_perfdata_file_mode=a
#performance文件處理時(shí)間間隔
host_perfdata_file_processing_interval=15
service_perfdata_file_processing_interval=15
#performance文件處理命令
host_perfdata_file_processing_command=process-host-perfdata-fileservice_perfdata_file_processing_command=process-service-perfdata-file
6.2 修改/usr/local/nagios/etc/objects/commands.cfg,增加performance文件處理命令
define command{
command_name process-service-perfdata-file
command_line /bin/mv /usr/local/pnp4nagios/var/service-perfdata /usr/local/pnp4nagios/var/spool/service-perfdata.$TIMET$
}
define command{
command_name process-host-perfdata-file
command_line /bin/mv /usr/local/pnp4nagios/var/host-perfdata /usr/local/pnp4nagios/var/spool/host-perfdata.$TIMET$
}
6.3 修改nagios.cfg,設(shè)置在nagios web gui中顯示pnp4nagios圖標(biāo)的方式
第一種方法在templates.cfg里面加入如下,只能在新窗口打開圖像數(shù)據(jù)
define host {
name host-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$srv=_HOST_
register 0
}/p>
p>define service {
name srv-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$srv=$SERVICEDESC$
register 0
}
第二種方法可以在鼠標(biāo)移到pnp4nagios圖標(biāo)的時(shí)候就可以開到實(shí)時(shí)圖標(biāo),不過效果不是很好,按照說明加入如下:
拷貝status-header.ssi到/usr/local/nagios/share/ssi/目錄下面,同時(shí)修改templates.cfg,加入如下內(nèi)容:
define host {
name host-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$srv=_HOST_' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$srv=_HOST_
register 0
}
define service {
name srv-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$srv=$SERVICEDESC$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$srv=$SERVICEDESC$
register 0
}
Note1: 自動出現(xiàn)的小窗口清晰度不好,我喜歡第一種方式
Note2: 這里沒有涉及email的配置,記得通知設(shè)計(jì)的時(shí)候小心消息泛濫被投訴。。。