七,服務(wù)器端Nagios圖形監(jiān)控顯示和管理
前面搭建的Nagios服務(wù)雖然能顯示信息,能報(bào)警。但是在企業(yè)工作中還會(huì)需要一個(gè)歷史趨勢(shì)圖,跟蹤每一個(gè)業(yè)務(wù)的長(zhǎng)期趨勢(shì),并且能以圖形的方式展示,例如:根據(jù)磁盤的剩余趨勢(shì),確定是否需要提前購(gòu)買磁盤。
7.1 服務(wù)器端安裝PNP生成圖形監(jiān)控曲線
PNP是一款配合Nagios出圖的軟件,官方站點(diǎn)為:http://www.pnp4nagios.org
7.1.1 PNP出圖基礎(chǔ)依賴軟件安裝
[root@Nagios 6]# yum -y install cairo pango zlib zlib-devel freetype freetype-devel gd gd-devel
[root@Nagios 6]# rpm -qa cairo pango zlib zlib-devel freetype freetype-devel gd gd-devel
freetype-2.3.11-17.el6.x86_64
zlib-1.2.3-29.el6.x86_64
zlib-devel-1.2.3-29.el6.x86_64
freetype-devel-2.3.11-17.el6.x86_64
gd-2.0.35-11.el6.x86_64
gd-devel-2.0.35-11.el6.x86_64
cairo-1.8.8-6.el6_6.x86_64
pango-1.28.1-11.el6.x86_64
#然后安裝rrdtool依賴的libart_lgpl相關(guān)軟件包,這個(gè)軟件包要優(yōu)先于rrdtool安裝
[root@Nagios 6]# yum -y install libart_lgpl libart_lgpl-devel
[root@Nagios 6]# rpm -qa libart_lgpl libart_lgpl-devel
libart_lgpl-2.3.20-5.1.el6.x86_64
libart_lgpl-devel-2.3.20-5.1.el6.x86_64
#PNP工具最終是通過(guò)rrdtool實(shí)現(xiàn)的畫(huà)圖,因此需要提前安裝rrdtool
[root@Nagios 6]# yum -y install rrdtool rrdtool-devel
[root@Nagios 6]# rpm -qa rrdtool rrdtool-devel
rrdtool-1.3.8-10.el6.x86_64
rrdtool-devel-1.3.8-10.el6.x86_64
[root@Nagios 6]# which rrdtool
/usr/bin/rrdtool
7.1.2 安裝出圖web界面展示軟件PNP
此處選擇0.4.14的PNP版本,如果選擇高版本在出圖方面可能會(huì)有坑,正常情況下,選04版本已經(jīng)足夠了,因此,如果沒(méi)有特殊需求,建議最好完全按照書(shū)本測(cè)試步驟,在弄清楚之后再變通版本。
PNP軟件無(wú)法yum安裝,可通過(guò)編譯的方式進(jìn)行安裝,操作過(guò)程如下:
[root@Nagios ~]# yum -y install perl-Time-HiRes
[root@Nagios ~]# cd nagios/
[root@Nagios nagios]# ll pnp-0.4.14.tar.gz
-rw-r--r--. 1 root root 455593 Aug 12 12:22 pnp-0.4.14.tar.gz
[root@Nagios nagios]# tar xf pnp-0.4.14.tar.gz -C /usr/src/
[root@Nagios nagios]# cd /usr/src/pnp-0.4.14/
[root@Nagios pnp-0.4.14]# ./configure
> --with-rrdtool
> --with-perfdata-dir=/usr/local/nagios/share/perfdata/
[root@Nagios pnp-0.4.14]# make all
[root@Nagios pnp-0.4.14]# make install
[root@Nagios pnp-0.4.14]# make install-config
[root@Nagios pnp-0.4.14]# make install-init
[root@Nagios pnp-0.4.14]# ll /usr/local/nagios/libexec/ | grep process
-rwxr-xr-x 1 nagios nagios 31813 Aug 19 23:04 process_perfdata.pl
如果configure后出現(xiàn)如下警告信息,請(qǐng)忽略:
#################
# WARNING:The RRDs Perl Modules are not found on your System
# Using RRDs will speedup things in larg
##################
PNP提供了一個(gè)獲取數(shù)據(jù)出圖的Perl腳本,可以用如下命令查到:
[root@Nagios pnp-0.4.14]# ll /usr/local/nagios/libexec/ | grep process
-rwxr-xr-x 1 nagios nagios 31813 Aug 19 23:04 process_perfdata.pl
7.1.3 Nagios出圖相關(guān)配置
1)執(zhí)行編輯命令vi,需要改nagios.cfg主配置文件833行,將如下參數(shù)對(duì)應(yīng)的值從0改為1,表示記錄數(shù)據(jù)。
[root@Nagios nagios]# sed -n '833p' /usr/local/nagios/etc/nagios.cfg
process_performance_data=0 #默認(rèn)0,改為1
#然后繼續(xù)向下大概在845,846行的位置,找到如下兩項(xiàng),取消參數(shù)開(kāi)頭的注釋。
[root@Nagios nagios]# sed -n '845,846p' /usr/local/nagios/etc/nagios.cfg
#host_perfdata_command=process-host-perfdata #取消注釋
#service_perfdata_command=process-service-perfdata #取消注釋
2)執(zhí)行編輯命令vi,需要修改commands.cfg配置文件,定義出圖獲取數(shù)據(jù)的命令。
[root@Nagios nagios]# sed -n '227,238p' /usr/local/nagios/etc/objects/commands.cfg
# 'process-host-perfdata' command definition
define command{
command_name process-host-perfdata
command_line /usr/bin/printf "%b" "$LASTHOSTCHECK$ $HOSTNAME$ $HOSTSTATE$ $HOSTATTEMPT$ $HOSTSTATETYPE$ $HOSTEXECUTIONTIME$ $HOSTOUTPUT$ $HOSTPERFDATA$
" >>/usr/local/nagios/var/host-perfdata.out
}
# 'process-service-perfdata' command definition
define command{
command_name process-service-perfdata
command_line /usr/bin/printf "%b" "$LASTSERVICECHECK$ $HOSTNAME$ $SERVICEDESC$ $SERVICESTATE$ $SERVICEATTEMPT$ $SERVICESTATETYPE$ $SERVICEEXECUTIONTIME$ $SERVICELATENCY$ $SERVICEOUTPUT$ $SERVICEPERFDATA$
" >>/usr/local/nagios/var/service-perfdata.out
}
現(xiàn)在刪除上述的默認(rèn)配置,然后將其修改為如下的配置內(nèi)容:
[root@Nagios nagios]# sed -n '227,238p' /usr/local/nagios/etc/objects/commands.cfg
# 'process-host-perfdata' command definition
define command{
command_name process-host-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
# 'process-service-perfdata' command definition
define command{
command_name process-service-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
3)執(zhí)行檢查語(yǔ)法命令
[root@Nagios nagios]# /etc/init.d/nagios checkconfig
#..以上省略若干...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
OK
4)執(zhí)行命令使Nagios配置文件生效。
[root@Nagios nagios]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
5)此時(shí)打開(kāi)瀏覽器訪問(wèn)“http://192.168.0.200/nagios/pnp/”,應(yīng)該會(huì)出現(xiàn)如下圖所示的圖形界面,但是沒(méi)有業(yè)務(wù)數(shù)據(jù)顯示。
如果同學(xué)們打開(kāi)出現(xiàn)如下錯(cuò)誤:
如果出現(xiàn)上圖中的錯(cuò)誤,先別著急,可能過(guò)一會(huì)兒重新訪問(wèn)上述地址就會(huì)恢復(fù)正常。
如果過(guò)了很長(zhǎng)時(shí)間重新訪問(wèn)上述地址還不正常,可以執(zhí)行如下命令看看,然后再訪問(wèn)試試:
yum -y install php-gd gd gd-devel
7.2 配置主機(jī)及服務(wù)獲取狀態(tài)數(shù)據(jù)出圖
7.1結(jié)尾的圖形是沒(méi)有具體的業(yè)務(wù)數(shù)據(jù)圖形趨勢(shì)的,因?yàn)槟菚r(shí)還沒(méi)有為Nagios的各個(gè)主機(jī)和具體要監(jiān)控的服務(wù)配置獲取數(shù)據(jù)信息,下面是讓各個(gè)主機(jī)或服務(wù)獲取數(shù)據(jù)的配置。
7.2.1 設(shè)置讓被監(jiān)控的主機(jī)記錄數(shù)據(jù)
如果要讓所有的主機(jī)獲取數(shù)據(jù)并出趨勢(shì)圖,則需編輯Nagios的主機(jī)hosts.cfg文件,不過(guò),只要在每一個(gè)被監(jiān)控主機(jī)的配置下面增加同一個(gè)參數(shù)項(xiàng)“process_perf_data 1”即可。操作步驟如下:
[root@Nagios nagios]# cd /usr/local/nagios/etc/objects/
[root@Nagios objects]# cat hosts.cfg
# Define a host for the local machine
define host{
use linux-server
host_name web01
alias web01
address 192.168.0.223
process_perf_data 1 #為web01增加1此行,表示記錄web01主機(jī)狀態(tài)數(shù)據(jù)
}
define host{
use linux-server
host_name web02
alias web02
address 192.168.0.224
process_perf_data 1 #為Web02增加此行,表示記錄web02主機(jī)狀態(tài)數(shù)據(jù)
}
define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members web01,web02
7.2.2 設(shè)置讓被監(jiān)控主機(jī)對(duì)應(yīng)的服務(wù)記錄數(shù)據(jù)
如果需要所有的主機(jī)對(duì)應(yīng)的服務(wù)獲取數(shù)據(jù)并出趨勢(shì)圖,則要編輯Nagios的服務(wù)配置文件services.cfg,當(dāng)然,也只需要在每一個(gè)對(duì)應(yīng)服務(wù)下面增加同一個(gè)參數(shù)項(xiàng)即可,即“process_perf_data 1”,配置步驟如下:
[root@Nagios objects]# cat /usr/local/nagios/etc/objects/services.cfg
define service {
use generic-service
host_name web01,web02
service_description Disk Partition
check_command check_nrpe!check_disk
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01,web02
service_description Swap Useage
check_command check_nrpe!check_swap
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01,web02
service_description MEM Useage
check_command check_nrpe!check_mem
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01,web02
service_description Current Load
check_command check_nrpe!check_load
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01,web02
service_description Disk lostat
check_command check_nrpe!check_iostat!5!11
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01,web02
service_description PING
check_command check_ping!100.0,20%!500.0,60%
process_perf_data 1 #為每個(gè)service添加此行
}
#url examples http://www.yunjisuan.com
define service {
use generic-service
host_name web01
service_description www_url
check_command check_weburl! -H www.yunjisuan.com
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01
service_description www_url
check_command check_http
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01
ervice_description www_static_url
check_command check_weburl! -H www.yunjisuan.com -u /static/test.html
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01
service_description www_yunjisuan_url
check_command check_weburl! -H www.yunjisuan.com -u "/article/index.phpm=article&a=list&id=670"
process_perf_data 1 #為每個(gè)service添加此行
}
#tcp examples
define service {
use generic-service
host_name web01
service_description ssh_22
check_command check_tcp! 22
process_perf_data 1 #為每個(gè)service添加此行
}
define service {
use generic-service
host_name web01
service_description http_80
check_command check_tcp! 80
process_perf_data 1 #為每個(gè)service添加此行
}
由于每個(gè)主機(jī)對(duì)應(yīng)的服務(wù)內(nèi)容太多了,因此可以采取在所有服務(wù)對(duì)應(yīng)的統(tǒng)一模板里添加配置參數(shù)的方式,這樣可使所有的服務(wù)都可以生效。這里每個(gè)服務(wù)使用的模板就是由服務(wù)里的“use generic-service”這個(gè)選項(xiàng)確定的,查看與模板文件里服務(wù)模板generic-service名對(duì)應(yīng)的服務(wù)參數(shù):
[root@Nagios objects]# sed -n '154,177p' /usr/local/nagios/etc/objects/templates.cfg | awk -F ";" '{print $1}'
name generic-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 10
retry_check_interval 2
contact_groups admins
notification_options w,u,c,r
notification_interval 60
notification_period 24x7
register 0
}
提示:
為了看的清晰,這里去掉了所有注釋,服務(wù)的模板里默認(rèn)已經(jīng)配置了“process_perf_data 1”,即凡是使用templates.cfg模板文件里名字為generic-service的模板,均作為服務(wù)的模板,這樣就相當(dāng)于所有服務(wù)都執(zhí)行g(shù)eneric-service模板里的配置了。
配置完畢重啟Nagios服務(wù):
[root@Nagios objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
到此,如果等一段時(shí)間,然后查看PNP URL就可以發(fā)現(xiàn)生成了圖形數(shù)據(jù),有些數(shù)據(jù)需要壓力測(cè)試或者真實(shí)環(huán)境才能看到,例如主機(jī)負(fù)載等。趨勢(shì)圖如下圖所示:
7.3 整合PNP URL超鏈接到Nagios Web界面
在整合PNP URL超鏈接到Nagios Web界面后,會(huì)在所有的主機(jī)或主機(jī)對(duì)應(yīng)服務(wù)的前面,出現(xiàn)一個(gè)閃電樣的超鏈接1圖標(biāo),單擊超鏈接,就可以查看到對(duì)應(yīng)的主機(jī)或服務(wù)實(shí)際的監(jiān)控狀態(tài)趨勢(shì)圖。
7.3.1 給被監(jiān)控的所有主機(jī)添加超鏈接圖標(biāo)
默認(rèn)情況PNP的URL為http://192.168.0.200/nagios/pnp/index.php和Nagios不在一個(gè)界面里,所以查看主機(jī)或服務(wù)對(duì)應(yīng)的趨勢(shì)圖很費(fèi)勁。那么如何完善呢?
我們可以直接在host.cfg里在希望出圖的主機(jī)里配置如下一行參數(shù):
action_url /nagios/pnp/index.php?host=$HOSTNAME$ #實(shí)際上就是給URL傳個(gè)主機(jī)參數(shù)
然后編輯host.cfg,增加上述配置。配置結(jié)果如下:
[root@Nagios objects]# cat /usr/local/nagios/etc/objects/hosts.cfg
# Define a host for the local machine
define host{
use linux-server
host_name web01
alias web01
address 192.168.0.223
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$ #添加超鏈接圖標(biāo)
}
define host{
use linux-server
host_name web02
alias web02
address 192.168.0.224
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$ #添加超鏈接圖標(biāo)
}
define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members web01,web02
}
接著,檢查語(yǔ)法重新加載Nagios
[root@Nagios objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
如果配置過(guò)程都正確,打開(kāi)瀏覽器訪問(wèn)Nagios界面,最終可以看到如下圖所示的圖形。圖中,右邊方框里標(biāo)記的白色方格里,中間帶波浪線的就是超鏈接圖標(biāo)。單擊進(jìn)去即可看到一個(gè)主機(jī)所有的服務(wù)圖。
7.3.2 給被監(jiān)控主機(jī)指定的服務(wù)添加超鏈接圖標(biāo)
和上述主機(jī)添加超鏈接圖標(biāo)的配置幾乎一樣,執(zhí)行“vi /usr/local/nagios/etc/objects/services.cfg”,添加如下內(nèi)容:
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
#實(shí)際上就是給URL傳了一個(gè)主機(jī)的參數(shù)和一個(gè)主機(jī)對(duì)應(yīng)服務(wù)的參數(shù)
給具體服務(wù)增加超鏈接配置方法是,直接在define service {}大括號(hào)中增加參數(shù)即可,具體配置的內(nèi)容如下“action_url參數(shù)部分”:
[root@Nagios objects]# cat /usr/local/nagios/etc/objects/services.cfg
define service {
use generic-service
host_name web01,web02
service_description Disk Partition
check_command check_nrpe!check_disk
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
#給具體服務(wù)增加超鏈接配置
}
define service {
use generic-service
host_name web01,web02
service_description Swap Useage
check_command check_nrpe!check_swap
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
#給具體服務(wù)增加超鏈接配置
}
define service {
use generic-service
host_name web01,web02
service_description MEM Useage
check_command check_nrpe!check_mem
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
#給具體服務(wù)增加超鏈接配置
}
define service {
use generic-service
host_name web01,web02
service_description Current Load
check_command check_nrpe!check_load
process_perf_data 1
}
define service {
use generic-service
host_name web01,web02
service_description Disk lostat
check_command check_nrpe!check_iostat!5!11
process_perf_data 1
}
define service {
use generic-service
host_name web01,web02
service_description PING
check_command check_ping!100.0,20%!500.0,60%
process_perf_data 1
}
#url examples http://www.yunjisuan.com
define service {
use generic-service
host_name web01
service_description www_url
check_command check_weburl! -H www.yunjisuan.com
process_perf_data 1
}
define service {
use generic-service
host_name web01
service_description www_url
check_command check_http
process_perf_data 1
}
define service {
use generic-service
host_name web01
service_description www_static_url
check_command check_weburl! -H www.yunjisuan.com -u /static/test.html
process_perf_data 1
}
define service {
use generic-service
host_name web01
service_description www_yunjisuan_url
check_command check_weburl! -H www.yunjisuan.com -u "/article/index.phpm=article&a=list&id=670"
process_perf_data 1
}
#tcp examples
define service {
use generic-service
host_name web01
service_description ssh_22
check_command check_tcp! 22
process_perf_data 1
}
define service {
use generic-service
host_name web01
service_description http_80
check_command check_tcp! 80
process_perf_data 1
}
配置完成后的效果圖如下:
也可以快速設(shè)置讓全部的服務(wù)出圖,找到templates.cfg模板文件,找到默認(rèn)的服務(wù)名generic-service,在這個(gè)服務(wù)名大括號(hào)的內(nèi)部結(jié)尾增加“action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$ ”一行即可。
[root@Nagios objects]# sed -n '153,178p' /usr/local/nagios/etc/objects/templates.cfg | awk -F ";" '{print $1}'
define service{
name generic-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 10
retry_check_interval 2
contact_groups admins
notification_options w,u,c,r
notification_interval 60
notification_period 24x7
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
#在最后添加此行
}
這樣所有主機(jī)的所有服務(wù)都將增加出圖的超鏈接圖標(biāo)了。
現(xiàn)在,人要檢查語(yǔ)法并重新加載Nagios
[root@Nagios objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
全部主機(jī)和服務(wù)的監(jiān)控圖最終結(jié)果如下圖所示:
此時(shí),單擊任意一個(gè)超鏈接圖標(biāo),就可以查看對(duì)應(yīng)的主機(jī)或服務(wù)的業(yè)務(wù)趨勢(shì)圖了,到此,Nagios的主機(jī)和服務(wù)出圖的配置就完成了,是不是很簡(jiǎn)單?
7.3.3 Nagios出圖獲取的數(shù)據(jù)存放路徑
想真正繪制出業(yè)務(wù)的趨勢(shì)圖全靠下面命令生成的數(shù)據(jù)。這些歷史數(shù)據(jù)要備份好。
[root@Nagios objects]# ll /usr/local/nagios/share/perfdata/
total 8
drwxr-xr-x 2 nagios nagios 4096 Aug 20 02:10 web01
drwxr-xr-x 2 nagios nagios 4096 Aug 20 02:03 web02
[root@Nagios objects]# tree /usr/local/nagios/share/perfdata/
/usr/local/nagios/share/perfdata/
|-- web01
| |-- Current_Load.rrd
| |-- Current_Load.xml
| |-- Disk_Partition.rrd
| |-- Disk_Partition.xml
| |-- Disk_lostat.rrd
| |-- Disk_lostat.xml
| |-- MEM_Useage.rrd
| |-- MEM_Useage.xml
| |-- PING.rrd
| |-- PING.xml
| |-- Swap_Useage.rrd
| |-- Swap_Useage.xml
| |-- http_80.rrd
| |-- http_80.xml
| |-- ssh_22.rrd
| |-- ssh_22.xml
| |-- www_static_url.rrd
| |-- www_static_url.xml
| |-- www_url.rrd
| |-- www_url.xml
| |-- www_yunjisuan_url.rrd
| `-- www_yunjisuan_url.xml
`-- web02
|-- PING.rrd
`-- PING.xml
2 directories, 24 files
八,實(shí)現(xiàn)將Nagios故障報(bào)警給管理員
要將Nagios故障報(bào)警給管理員時(shí),常用的方式包括郵件報(bào)警和手機(jī)報(bào)警,下面分別介紹
8.1 郵件報(bào)警
- 普通郵件報(bào)警就是在故障發(fā)生或恢復(fù)時(shí),將報(bào)警信息發(fā)到系統(tǒng)管理員或相關(guān)維護(hù)人員的信箱中,一般來(lái)說(shuō)最好使用公司內(nèi)部信箱作為報(bào)警信箱。同學(xué)們回家學(xué)習(xí)測(cè)試時(shí)如果用QQ,126等信箱可能會(huì)有收不到郵件的情況或者被當(dāng)作垃圾郵件了。
- 一般白天上班時(shí),郵件報(bào)警還算比較及時(shí),但是如果人不在計(jì)算機(jī)旁,郵件報(bào)警就不行了,因此,郵件報(bào)警只適合不是特別重要的業(yè)務(wù),或者作為發(fā)送大量報(bào)警信息中的一個(gè)輔助方式,如硬盤,內(nèi)存,及日志相關(guān)等不需要及時(shí)解決的服務(wù)報(bào)警。故而,在生產(chǎn)環(huán)境中,郵件報(bào)警一般會(huì)結(jié)合其他報(bào)警方式一起使用。
- 那么,下面就來(lái)看一下郵件報(bào)警的基本配置方法。
首先,添加監(jiān)控報(bào)警的接收Email地址
[root@Nagios objects]# sed -n '35p' /usr/local/nagios/etc/objects/contacts.cfg | awk -F ";" '{print $1}'
email 215379068@qq.com #將本行內(nèi)容改成你的QQ郵箱
打開(kāi)postfix服務(wù)
[root@Nagios objects]# /etc/init.d/postfix start
Starting postfix: [ OK ]
[root@Nagios objects]# echo "/etc/init.d/postfix start" >> /etc/rc.local
[root@Nagios objects]# tail -3 /etc/rc.local
touch /var/lock/subsys/local
/etc/init.d/nagios start
/etc/init.d/postfix start
用命令測(cè)試發(fā)郵件:
[root@Nagios objects]# echo "this is test email" | mail -s "yunjisuan" 215379068@qq.com
#將郵件從QQ攔截名單取出,然后添加白名單
特別警示!
同學(xué)們?cè)诩彝鍺agios一定要用自己的QQ玩,誰(shuí)給我發(fā),我和誰(shuí)急-_-!
8.2 關(guān)于郵件相關(guān)的配置文件的大概解釋(喜歡的同學(xué)自己百度含義)
templates.cfg系統(tǒng)定義模板
#模板:generic-service
[root@Nagios objects]# sed -n '153,178p' /usr/local/nagios/etc/objects/templates.cfg | awk -F ";" '{print $1}'
define service{
name generic-service
active_checks_enabled 1
passive_checks_enabled 1
parallelize_check 1
obsess_over_service 1
check_freshness 0
notifications_enabled 1
event_handler_enabled 1
flap_detection_enabled 1
failure_prediction_enabled 1
process_perf_data 1
retain_status_information 1
retain_nonstatus_information 1
is_volatile 0
check_period 24x7 #告訴Nagios檢查服務(wù)的時(shí)間段
max_check_attempts 3 #對(duì)Nagios服務(wù)的最大檢查次數(shù)
normal_check_interval 10 #兩次檢查的時(shí)間間隔
retry_check_interval 2 #重新檢查時(shí)間間隔
contact_groups admins #指定聯(lián)系人主
notification_options w,u,c,r #定義何種異常可以被通知(email),w即warn表示警告狀態(tài),r即recover,表示恢復(fù)狀態(tài)
notification_interval 60 #服務(wù)出現(xiàn)異常,故障一直沒(méi)解決,Nagios再次對(duì)聯(lián)系人發(fā)出通知的時(shí)間間隔
notification_period 24x7 #指定email的時(shí)間段
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
}
#模板:generic-contact
[root@Nagios objects]# sed -n '28,37p' /usr/local/nagios/etc/objects/templates.cfg | awk -F ";" '{print $1}'
define contact{
name generic-contact #聯(lián)系人名稱
service_notification_period 24x7 #服務(wù)異常,發(fā)送通知時(shí)間段
host_notification_period 24x7 #主機(jī)異常,發(fā)送通知時(shí)間段
service_notification_options w,u,c,r,f,s #何種異常進(jìn)行通知
host_notification_options d,u,r,f,s #何種異常進(jìn)行通知
service_notification_commands notify-service-by-email #定義服務(wù)異常發(fā)送郵件命令,commands.cfg文件里定義
host_notification_commands notify-host-by-email #定義主機(jī)異常發(fā)送郵件命令,commands.cfg文件里定義
register 0
}
commands.cfg命令定義模板
#定義發(fā)送郵件命令
[root@Nagios objects]# sed -n '27,37p' commands.cfg
# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email #主機(jī)異常發(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$
}
# 'notify-service-by-email' command definition
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$
}
contacts.cfg聯(lián)系人定義模板
[root@Nagios objects]# cat contacts.cfg | egrep -v "#|^$" | awk -F ";" '{print $1}'
define contact{
contact_name nagiosadmin #定義成員
use generic-contact
alias Nagios Admin #成員別名
email 215379068@qq.com #成員郵箱
}
define contactgroup{
contactgroup_name admins #聯(lián)系人組名
alias Nagios Administrators #別名
members nagiosadmin #組員名單定義
}
8.3 Nagios各個(gè)配置文件的相互間關(guān)系圖
九,Nagios插件開(kāi)發(fā)
9.1 概述
9.1.1 什么是Nagios插件
前文在部署Nagios服務(wù)時(shí)已經(jīng)安裝了nagios-plugins-1.4.16.tar.gz,這個(gè)軟件包就是Nagios的插件安裝包,安裝后,執(zhí)行l(wèi)s -l /usr/local/nagios/libexec可以看到如下插件內(nèi)容:
[root@Nagios objects]# ls -l /usr/local/nagios/libexec/
total 5288
lrwxrwxrwx 1 root root 27 Aug 18 08:29 check_111 -> /service/scripts/check_test
-rwxr-xr-x. 1 nagios nagios 376524 Aug 14 10:11 check_apt
-rwxr-xr-x. 1 nagios nagios 2245 Aug 14 10:11 check_breeze
-rwxr-xr-x. 1 nagios nagios 128296 Aug 14 10:11 check_by_ssh
lrwxrwxrwx. 1 root root 9 Aug 14 10:11 check_clamd -> check_tcp
-rwxr-xr-x. 1 nagios nagios 85694 Aug 14 10:11 check_cluster
-r-sr-xr-x. 1 root nagios 123603 Aug 14 10:11 check_dhcp
-rwxr-xr-x. 1 nagios nagios 417895 Aug 14 10:11 check_disk
-rwxr-xr-x. 1 nagios nagios 9148 Aug 14 10:11 check_disk_smb
-rwxr-xr-x. 1 nagios nagios 80689 Aug 14 10:11 check_dummy
-rwxr-xr-x. 1 nagios nagios 3056 Aug 14 10:11 check_file_age
-rwxr-xr-x. 1 nagios nagios 6318 Aug 14 10:11 check_flexlm
lrwxrwxrwx. 1 root root 9 Aug 14 10:11 check_ftp -> check_tcp
-rwxr-xr-x. 1 nagios nagios 520614 Aug 14 10:11 check_http
-r-sr-xr-x. 1 root nagios 133689 Aug 14 10:11 check_icmp
-rwxr-xr-x. 1 nagios nagios 93416 Aug 14 10:11 check_ide_smart
-rwxr-xr-x. 1 nagios nagios 15137 Aug 14 10:11 check_ifoperstatus
-rwxr-xr-x. 1 nagios nagios 12601 Aug 14 10:11 check_ifstatus
lrwxrwxrwx. 1 root root 9 Aug 14 10:11 check_imap -> check_tcp
-rwxr-xr-x. 1 nagios nagios 6890 Aug 14 10:11 check_ircd
lrwxrwxrwx. 1 root root 9 Aug 14 10:11 check_jabber -> check_tcp
-rwxr-xr-x. 1 nagios nagios 106573 Aug 14 10:11 check_load
-rwxr-xr-x. 1 nagios nagios 6020 Aug 14 10:11 check_log
-rwxr-xr-x.