當(dāng)前位置:首頁(yè) > IT技術(shù) > 移動(dòng)平臺(tái) > 正文

企業(yè)級(jí)監(jiān)控Nagios實(shí)踐(下)
2021-09-24 09:13:09

七,服務(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ù)顯示。

QQ截圖20170820114101.png-103.1kB

如果同學(xué)們打開(kāi)出現(xiàn)如下錯(cuò)誤:

QQ截圖20170820111137.png-58.8kB

如果出現(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ì)圖如下圖所示:

QQ截圖20170820122224.png-98.7kB

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ù)圖。

QQ截圖20170820134626.png-42.9kB

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
	
}

配置完成后的效果圖如下:

QQ截圖20170820135700.png-78.6kB

也可以快速設(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é)果如下圖所示:

QQ截圖20170820140636.png-105.5kB

此時(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攔截名單取出,然后添加白名單

QQ截圖20170820160305.png-40kB

特別警示!
同學(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)系圖

QQ截圖20170820155456.png-22.5kB

九,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. 1 nagios nagios  20287 Aug 14 10:11 check_mailq
-rwxr-xr-x. 1 nagios nagios  93142 Aug 14 10:11 check_mrtg
-rwxr-xr-x. 1 nagios nagios  92487 Aug 14 10:11 check_mrtgtraf
-rwxr-xr-x. 1 nagios nagios 105606 Aug 14 10:11 check_nagios
lrwxrwxrwx. 1 root   root        9 Aug 14 10:11 check_nntp -> check_tcp
lrwxrwxrwx. 1 root   root        9 Aug 14 10:11 check_nntps -> check_tcp
-rwxrwxr-x. 1 nagios nagios  76744 Aug 14 10:32 check_nrpe
-rwxr-xr-x. 1 nagios nagios 127679 Aug 14 10:11 check_nt
-rwxr-xr-x. 1 nagios nagios 130078 Aug 14 10:11 check_ntp
-rwxr-xr-x. 1 nagios nagios 119167 Aug 14 10:11 check_ntp_peer
-rwxr-xr-x. 1 nagios nagios 117728 Aug 14 10:11 check_ntp_time
-rwxr-xr-x. 1 nagios nagios 159372 Aug 14 10:11 check_nwstat
-rwxr-xr-x. 1 nagios nagios   8324 Aug 14 10:11 check_oracle
-rwxr-xr-x. 1 nagios nagios 108934 Aug 14 10:11 check_overcr
-rwxr-xr-x. 1 nagios nagios 132691 Aug 14 10:11 check_ping
-rwxr-xr-x  1 nagios nagios   6184 Aug 19 23:04 check_pnp_rrds.pl
lrwxrwxrwx. 1 root   root        9 Aug 14 10:11 check_pop -> check_tcp
-rwxr-xr-x. 1 nagios nagios 396833 Aug 14 10:11 check_procs
-rwxr-xr-x. 1 nagios nagios 106492 Aug 14 10:11 check_real
-rwxr-xr-x. 1 nagios nagios   9584 Aug 14 10:11 check_rpc
-rwxr-xr-x. 1 nagios nagios   1412 Aug 14 10:11 check_sensors
lrwxrwxrwx. 1 root   root        9 Aug 14 10:11 check_simap -> check_tcp
-rwxr-xr-x. 1 nagios nagios 446511 Aug 14 10:11 check_smtp
lrwxrwxrwx. 1 root   root        9 Aug 14 10:11 check_spop -> check_tcp
-rwxr-xr-x. 1 nagios nagios 103000 Aug 14 10:11 check_ssh
lrwxrwxrwx. 1 root   root        9 Aug 14 10:11 check_ssmtp -> check_tcp
-rwxr-xr-x. 1 nagios nagios 108233 Aug 14 10:11 check_swap
-rwxr-xr-x. 1 nagios nagios 160386 Aug 14 10:11 check_tcp
-rwxr-xr-x. 1 nagios nagios 105022 Aug 14 10:11 check_time
lrwxrwxrwx. 1 root   root        9 Aug 14 10:11 check_udp -> check_tcp
-rwxr-xr-x. 1 nagios nagios 117534 Aug 14 10:11 check_ups
-rwxr-xr-x. 1 nagios nagios  83434 Aug 14 10:11 check_users
-rwxr-xr-x. 1 nagios nagios   2939 Aug 14 10:11 check_wave
-rwxr-xr-x. 1 nagios nagios 109723 Aug 14 10:11 negate
-rwxr-xr-x  1 nagios nagios  31813 Aug 19 23:04 process_perfdata.pl
-rwxr-xr-x. 1 nagios nagios 103242 Aug 14 10:11 urlize
-rwxr-xr-x. 1 nagios nagios   1904 Aug 14 10:11 utils.pm
-rwxr-xr-x. 1 nagios nagios   2728 Aug 14 10:11 utils.sh

提示:
默認(rèn)安裝后大概有60個(gè)左右的插件,數(shù)量比較多,這里只介紹幾個(gè)常見(jiàn)的。
以上結(jié)果內(nèi)容都是Nagios插件,現(xiàn)在大家應(yīng)該對(duì)Nagios插件有一個(gè)基本的了解了。其實(shí),Nagios軟件本身僅僅是一個(gè)監(jiān)控的平臺(tái),如果要監(jiān)控具體的主機(jī)及服務(wù)的狀態(tài)和數(shù)據(jù)信息,還必須配置或調(diào)用插件或程序文件才能完成任務(wù),因此,如果沒(méi)有Nagios插件,Nagios就是一個(gè)空殼,啥都做不了。

9.1.2 為什么要開(kāi)發(fā)Nagios插件

  • 既然已經(jīng)安裝了Nagios的插件軟件包,為什么還要開(kāi)發(fā)Nagios插件呢?
  • 首先想說(shuō)明的是,在生產(chǎn)場(chǎng)景中常用的大部分服務(wù)都是不需要編寫(xiě)插件就可以完成監(jiān)控的,check_http,check_tcp,check_nrpe等這些自帶的插件已經(jīng)很強(qiáng)大了。但是,仍然有部分我們想要監(jiān)控的服務(wù),是Nagios未自帶插件的,如:監(jiān)控LVS RS的lo網(wǎng)卡的VIP,監(jiān)控NFS狀態(tài),又或是監(jiān)控iostat,mem,sar系統(tǒng)指標(biāo)及相關(guān)APP應(yīng)用(MQ隊(duì)列)等。這個(gè)時(shí)候我們有兩個(gè)選擇,一個(gè)是去網(wǎng)上搜索,看看有沒(méi)有別人寫(xiě)過(guò)的腳本,拿來(lái)使用或修改后使用;另外就是自己開(kāi)發(fā)編寫(xiě)腳本。這里建議大家學(xué)會(huì)手工編寫(xiě)插件,如果開(kāi)始不會(huì)寫(xiě),可以把網(wǎng)上別人分享的插件拿來(lái)改,改著改著就會(huì)寫(xiě)了。
  • 如果要開(kāi)發(fā)插件,最好掌握一門開(kāi)發(fā)語(yǔ)言,例如:Shell,Python。

9.2 編寫(xiě)Nagios插件的規(guī)則

9.2.1 編寫(xiě)Nagios插件說(shuō)明

  • Nagios插件是Nagios提供的一種通過(guò)可擴(kuò)展的方式部署的程序組件,該插件可通過(guò)Shell,Java,C/C++,PHP等多種語(yǔ)言開(kāi)發(fā),運(yùn)維或者系統(tǒng)架構(gòu)人員只要通過(guò)修改Nagios配置文件和相應(yīng)參數(shù),就能很方便的將該插件集成到Nagios中,實(shí)現(xiàn)對(duì)目標(biāo)系統(tǒng)的監(jiān)控。
  • Nagios服務(wù)為1插件程序提供了兩個(gè)返回值接口和插件交互:一個(gè)是插件執(zhí)行后的退出狀態(tài)碼,另一個(gè)是插件執(zhí)行過(guò)程中在控制臺(tái)打印的1第一行數(shù)據(jù)。退出狀態(tài)碼可以被Nagios主程序作為判斷被監(jiān)控系統(tǒng)服務(wù)狀態(tài)的依據(jù),控制臺(tái)打印的第一行數(shù)據(jù)可以被Nagios主程序作為被監(jiān)控系統(tǒng)服務(wù)狀態(tài)的補(bǔ)充說(shuō)明,會(huì)顯示在Web管理頁(yè)面,如下圖所示:

QQ截圖20170820165857.png-4.5kB

為了管理Nagios插件,Nagios每查詢一個(gè)服務(wù)的狀態(tài)時(shí),就會(huì)產(chǎn)生一個(gè)子進(jìn)程,并使用來(lái)自該命令的輸出和退出代碼來(lái)確定其具體的狀態(tài)。Nagios主程序可識(shí)別的插件的退出狀態(tài)碼和說(shuō)明如下:

  • OK:退出代碼,0表示服務(wù)工作正常。
  • WARNING:退出代碼,1表示服務(wù)處于警告狀態(tài)
  • CRITICAL:退出代碼,2表示服務(wù)處于緊急,嚴(yán)重狀態(tài)。
  • UNKNOWN:退出代碼,3表示服務(wù)處于未知狀態(tài)。

注意:
此處數(shù)字代碼的含義曾經(jīng)有公司面試時(shí)考過(guò)。

最后一種狀態(tài)通常表示該插件無(wú)法確定服務(wù)的狀態(tài)。例如,可能出現(xiàn)了網(wǎng)絡(luò)或內(nèi)部錯(cuò)誤。相關(guān)狀態(tài)可以從如下文件中看到:

[root@Nagios objects]# head -7 /usr/local/nagios/libexec/utils.sh 
#! /bin/sh

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

#提示:結(jié)尾處比列舉的還多個(gè)狀態(tài),但不常用

9.2.2 Nagios插件開(kāi)發(fā)原理

Nagios插件程序中需要調(diào)用監(jiān)控服務(wù)規(guī)定的操作序列,并根據(jù)預(yù)先定義的規(guī)則,對(duì)返回結(jié)果進(jìn)行分析,判斷服務(wù)的當(dāng)前狀態(tài),然后以指定的狀態(tài)碼退出程序,同時(shí)將對(duì)該狀態(tài)的說(shuō)明不換行輸出到控制臺(tái)。

9.2.3 Nagios插件開(kāi)發(fā)語(yǔ)言

Nagios的插件開(kāi)發(fā)不限制任何開(kāi)發(fā)語(yǔ)言,只要該插件能被Nagios調(diào)用,并獲取到相應(yīng)業(yè)務(wù)數(shù)據(jù)就OK,如能在命令行執(zhí)行輸出結(jié)果也可以,常用的插件語(yǔ)言有Shell,Perl,Python,PHP, C/C++。

9.3 使用Shell開(kāi)發(fā)Nagios插件

9.3.1 編寫(xiě)檢查WebURL地址的插件

以下腳本只是針對(duì)訪問(wèn)客戶端192.168.0.223的IP的

[root@Nagios libexec]# cat check_url.sh 
#!/bin/bash
# anthor:Mr.chen by 2017-8-20

wget -T 10 --spider 192.168.0.223 >/dev/null 2>&1   #用wget檢查192.168.0.223是不是可以訪問(wèn),-T超時(shí)時(shí)間 --spider不下載網(wǎng)頁(yè)

if [ $? -eq 0 ];then                    #判斷上述wget命令返回值,0成功非0失敗
	echo "URL 192.168.0.223 OK"
	exit 0
else
	echo "URL 192.168.0.223 CRITICAL"
	exit 2
fi

下面利用傳參把腳本改進(jìn)為通用的WebURL插件

[root@Nagios libexec]# cat check_url.sh 
#!/bin/bash
# anthor:Mr.chen by 2017-8-20

PROGNAME=`basename $0`      #取腳本名
PROGPATH=`dirname $0`       #取腳本路徑

usage(){            #打印幫助

	echo "Usage: /bin/sh ${PROGPATH}/${PROGNAME} url" 
	exit 1

}

[ $# -ne 1 ] && usage           #參數(shù)個(gè)數(shù)不是1,打印幫助

wget -T 10 --spider $1 >/dev/null 2>&1       #URL地址改成傳參   

if [ $? -eq 0 ];then
	echo "URL $1 OK"
	exit 0
else
	echo "URL $1 CRITICAL"
	exit 2
fi

以下是監(jiān)控WebURL的插件腳本專業(yè)型寫(xiě)法

[root@Nagios libexec]# cat check_url.sh 
#!/bin/bash
# anthor:Mr.chen by 2017-8-20

PROGNAME=`basename $0`
PROGPATH=`dirname $0`

usage(){

	echo "Usage: /bin/sh ${PROGPATH}/${PROGNAME} url" 
	exit 1

}

[ $# -ne 1 ] && usage

. $PROGPATH/utils.sh

if wget -T 20 --spider $1 >/dev/null 2>&1;then
	echo "URL $1 OK"
	exit $STATE_OK
else
	echo "URL $1 NO"
	exit $STATE_CRITICAL
fi

最后手工測(cè)試以下改進(jìn)的WebURL插件腳本

[root@Nagios libexec]# sh /usr/local/nagios/libexec/check_url.sh www.yunjisuan.com
URL www.yunjisuan.com OK
[root@Nagios libexec]# echo $?
0
[root@Nagios libexec]# sh /usr/local/nagios/libexec/check_url.sh bbs.yunjisuan.com
URL bbs.yunjisuan.com OK
[root@Nagios libexec]# echo $?
0
[root@Nagios libexec]# sh /usr/local/nagios/libexec/check_url.sh blog.yunjisuan.com
URL blog.yunjisuan.com NO
[root@Nagios libexec]# echo $?
2

9.3.2 WebURL插件腳本的部署過(guò)程(主動(dòng)監(jiān)控方式)

Nagios主動(dòng)模式監(jiān)控和Nagios客戶端的nrpe進(jìn)程沒(méi)有關(guān)系。
主動(dòng)模式的所有操作完全在Nagios主服務(wù)器上進(jìn)行。部署步驟如下:

(1)開(kāi)發(fā)check_url.sh,放到/usr/local/nagios/libexec中,授權(quán)為可執(zhí)行

root@Nagios libexec]# cd /usr/local/nagios/libexec/
[root@Nagios libexec]# chmod +x check_url.sh 
[root@Nagios libexec]# ll check_url.sh
-rwxr-xr-x 1 root root 337 Aug 20 06:38 check_url.sh
[root@Nagios libexec]# cat check_url.sh 
#!/bin/bash
# anthor:Mr.chen by 2017-8-20

PROGNAME=`basename $0`
PROGPATH=`dirname $0`

usage(){

	echo "Usage: /bin/sh ${PROGPATH}/${PROGNAME} url" 
	exit 1

}

[ $# -ne 1 ] && usage

. $PROGPATH/utils.sh

if wget -T 20 --spider $1 >/dev/null 2>&1;then
	echo "URL $1 OK"
	exit $STATE_OK
else
	echo "URL $1 NO"
	exit $STATE_CRITICAL
fi

(2)在commands.cfg中建立check_url命令:

[root@Nagios objects]# cd /usr/local/nagios/etc/objects/
[root@Nagios objects]# tail -7 commands.cfg 
# 'check_url' command definition by Mr.chen
define command {

	command_name check_url
	command_line $USER1$/check_url.sh 192.168.0.223    #加載腳本并傳參數(shù)

}
#提示:$USER1$是Nagios默認(rèn)變量,為/usr/local/nagios/libexec

(3)在services.cfg里添加監(jiān)控上述URL地址的服務(wù)

可以將服務(wù)直接添加進(jìn)services里也可以,寫(xiě)一個(gè)子服務(wù)的配置文件,寫(xiě)在/usr/local/nagios/etc/objects/services目錄里

#創(chuàng)建需要監(jiān)控的子服務(wù)配置文件
[root@Nagios objects]# pwd
/usr/local/nagios/etc/objects
[root@Nagios objects]# cd services
[root@Nagios services]# pwd
/usr/local/nagios/etc/objects/services
[root@Nagios services]# vim check_url.cfg
[root@Nagios services]# cat check_url.cfg 
define service {

	use generic-service
	host_name web01
	service_description 	http_zhudong_url
	check_command		check_url

}

由于/usr/local/nagios/etc/objects/services/*已經(jīng)被nagios.cfg主配置文件引用,因此無(wú)需在include進(jìn)service.cfg配置文件。

[root@Nagios etc]# cat nagios.cfg | grep "/usr/local/nagios/etc/objects/services"
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_dir=/usr/local/nagios/etc/objects/services

各個(gè)配置文件與Nagios.cfg主配置文件的關(guān)系如下圖所示:

QQ截圖20170820190632.png-30.3kB

(4)重新加載Nagios,查看結(jié)果

[root@Nagios etc]# /etc/init.d/nagios reload     
Running configuration check...done.
Reloading nagios configuration...done

(5)查看Nagios服務(wù)頁(yè)面監(jiān)控結(jié)果,如下圖所示:

QQ截圖20170820190940.png-109.9kB

備注:
Web01服務(wù)器需要能夠提供http協(xié)議的web訪問(wèn)。
等待刷新....

QQ截圖20170820192403.png-96.9kB

9.3.3 利用被動(dòng)模式的nrpe方式監(jiān)控/etc/passwd文件是否變化

Nagios被動(dòng)模式下的所有插件都需要部署在被監(jiān)控的Nagios客戶端。部署步驟如下。

1)在Nagios客戶端web01上取/etc/passwd的文件指紋,即md5值。

[root@web01 ~]# md5sum /etc/passwd >/opt/ps.md5
[root@web01 ~]# cat /opt/ps.md5
3660c548ce618df6c066f0db6bedd2af  /etc/passwd       #記住這個(gè)校驗(yàn)碼

2)在Nagios客戶端web01上開(kāi)發(fā)插件腳本,并測(cè)試

#請(qǐng)注意這是在web01客戶端的操作
[root@web01 ~]# cd /usr/local/nagios/libexec/
[root@web01 libexec]# vim check_passwd
[root@web01 libexec]# vim check_passwd
[root@web01 libexec]# cat check_passwd 
#!/bin/bash
# author:Mr.chen by 2017-8-20

OriMd5="3660c548ce618df6c066f0db6bedd2af"       #之前記錄的校驗(yàn)碼
CurrMd5=`md5sum /etc/passwd | cut -c 1-32`      #每次都重新生成校驗(yàn)碼
if [ "$OriMd5" == "$CurrMd5" ];then
	echo "/etc/passwd:OK"
	exit 0
else
	echo "/etc/passwd:FAILED"
	exit 2
fi
[root@web01 libexec]# sh check_passwd
/etc/passwd:OK
[root@web01 libexec]# chmod +x check_passwd 

#提示:還可以用md5sum -c /opt/ps.md5的方法比較

3)在Nagios客戶端web01上編輯nrpe.cfg,插入如下的內(nèi)容后保存

root@web01 libexec]# cd /usr/local/nagios/etc/
[root@web01 etc]# vim nrpe.cfg 
[root@web01 etc]# tail -1 nrpe.cfg      #在文件末尾加入如下內(nèi)容
command[check_passwd]=/usr/local/nagios/libexec/check_passwd

4)在Nagios客戶端web01上重啟nrpe,并檢查是否重啟成功(check_nrpe檢驗(yàn))

[root@web01 etc]# ps -ef | grep nrpe | grep -v grep
nagios     1027      1  0 Aug18 ?        00:00:05 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@web01 etc]# pkill nrpe
[root@web01 etc]# ps -ef | grep nrpe | grep -v grep
[root@web01 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@web01 etc]# ps -ef | grep nrpe | grep -v grep
nagios     4362      1  0 06:33 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

5)在Nagios服務(wù)器端nagios-server上進(jìn)入service目錄,創(chuàng)建配置文件check_passwd_web01.cfg

#請(qǐng)注意這里是Nagios服務(wù)器端的操作
[root@Nagios ~]# cd /usr/local/nagios/etc/objects/services
[root@Nagios services]# vim check_passwd_web01.cfg
[root@Nagios services]# cat check_passwd_web01.cfg
define service {

	use generic-service
	service_description check_passwd
	check_command check_nrpe!check_passwd
#這里的check_passwd就是Nagios客戶端nrpe.cfg里command[check_passwd]=/usr/local/nagios/libexec/check_passwd配置的中括號(hào)命令名check_passwd
}

6)在Nagios服務(wù)器端檢查語(yǔ)法

[root@Nagios services]# /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.

7)在Nagios服務(wù)器端加載Nagios配置,然后打開(kāi)Nagios頁(yè)面查看

[root@Nagios services]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done

QQ截圖20170820195452.png-102.1kB

等待刷新....

QQ截圖20170820195654.png-101.3kB

十,本節(jié)重點(diǎn)回顧

  1. Nagios監(jiān)控系統(tǒng)家族成員功能介紹
  2. Nagios監(jiān)控系統(tǒng)完整框架圖解說(shuō)明
  3. Nagios服務(wù)器端核心配置文件之間的關(guān)系原理
  4. Nagios服務(wù)器端及客戶端安裝,配置細(xì)節(jié)。
  5. Nagios利用check_nrpe插件進(jìn)行監(jiān)控的原理
  6. Nagios圖形監(jiān)控顯示和數(shù)據(jù)管理
  7. Nagios報(bào)警方式選擇及報(bào)警實(shí)施細(xì)節(jié)
  8. Nagios自定義插件開(kāi)發(fā)原理及開(kāi)發(fā)實(shí)踐
  9. Nagios插件主動(dòng)和被動(dòng)方式工作原理及實(shí)施部署細(xì)節(jié)。

本文摘自 :https://www.cnblogs.com/

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >