nagios事件處理機(jī)制-創(chuàng)新互聯(lián)

接到zz的任務(wù),實(shí)現(xiàn)自動(dòng)化處理nagios某項(xiàng)報(bào)警

腦海里有個(gè)印象,這個(gè)功能之前線下做過(guò)實(shí)驗(yàn)

一、首先必須查看下nagios的官方文檔,確認(rèn)可行,以下是筆者整理的一些自認(rèn)為有用的信息

1)
了解命令的定義方法
Writing Event Handler Commands
Event handler commands will likely be shell or perl scripts, but they can be any type of executable that can run from a
command prompt. At a minimum, the scripts should take the following macros as arguments:
For Services: $SERVICESTATE$, $SERVICESTATETYPE$, $SERVICEATTEMPT$
For Hosts: $HOSTSTATE$, $HOSTSTATETYPE$, $HOSTATTEMPT$
這段說(shuō)的是,針對(duì)于主機(jī)處理需要的一些參數(shù),跟針對(duì)于服務(wù)需要的一些參數(shù),這方面配置是在objects/commands.cfg配置的
官方文檔記錄
define command{
command_name restart-httpd
command_line /usr/local/nagios/libexec/eventhandlers/restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}

2)
了解主機(jī)配置文件的方法(筆者線上host和service合并的一個(gè)文件,一般是分開(kāi)的,主host跟service是分開(kāi)的)
官方文檔記錄:
define service{
host_name somehost
service_description HTTP
max_check_attempts 4
event_handler restart-httpd
...
}

二、一些解釋:

1)
變量解釋:
$SERVICESTATE$:服務(wù)的當(dāng)前狀態(tài)(OK、WARNING、UNKNOWN、CRITICAL)
$SERVICESTATETYPE$:服務(wù)器狀態(tài)類型,分為兩種,軟狀態(tài),硬狀態(tài)
$SERVICEATTEMPT$:軟狀態(tài)的嘗試check的次數(shù)
這些值是后面自動(dòng)恢復(fù)腳本必須要處理的三個(gè)參數(shù)

2)
HARD:硬狀態(tài)
SOFT:軟狀態(tài)
nagios在檢測(cè)服務(wù)正常的過(guò)程中,如果第一次檢測(cè)失敗,狀態(tài)成SOFT 嘗試設(shè)置的大次數(shù)后,狀態(tài)就改變成為HARD

3)
事件處理的一些參數(shù)配置
event_handler_timeout=30 超時(shí)時(shí)間
enable_event_handlers=1  開(kāi)機(jī)事件處理機(jī)制
event_handler

三、操作步驟:
1)
確認(rèn)事件處理開(kāi)關(guān)有沒(méi)有打開(kāi)
enable_event_handlers=1
0:關(guān)閉
1:打開(kāi)

2)
自恢復(fù)腳本制作
需要處理的參數(shù)太多,建議使用case,下面是官網(wǎng)的腳本例子,當(dāng)然可以寫成其他的
#!/bin/sh
#
# Event handler script for restarting the web server on the local machine
#
# Note: This script will only restart the web server if the service is
# retried 3 times (in a "soft" state) or if the web service somehow
# manages to fall into a "hard" error state.
#
# What state is the HTTP service in?
case "$1" in
OK)
# The service just came back up, so don't do anything...
;;
WARNING)
# We don't really care about warning states, since the service is probably still running...
;;
UNKNOWN)
# We don't know what might be causing an unknown error, so don't do anything...
;;
CRITICAL)
# Aha! The HTTP service appears to have a problem - perhaps we should restart the server...
# Is this a "soft" or a "hard" state?
case "$2" in
# We're in a "soft" state, meaning that Nagios is in the middle of retrying the
# check before it turns into a "hard" state and contacts get notified...
SOFT)
# What check attempt are we on? We don't want to restart the web server on the first
# check, because it may just be a fluke!
case "$3" in
# Wait until the check has been tried 3 times before restarting the web server.
# If the check fails on the 4th time (after we restart the web server), the state
# type will turn to "hard" and contacts will be notified of the problem.
# Hopefully this will restart the web server successfully, so the 4th check will
# result in a "soft" recovery. If that happens no one gets notified because we
# fixed the problem!
3)
echo -n "Restarting HTTP service (3rd soft critical state)..."
# Call the init script to restart the HTTPD server
/etc/rc.d/init.d/httpd restart
;;
esac
;;
#

3)報(bào)警產(chǎn)生之后,如何當(dāng)nagios服務(wù)器得到下面?zhèn)魃蟻?lái)的報(bào)警呢,做出自維護(hù)呢(objects/commands.cfg)
在objects/commands.cfg該文件中表明當(dāng)報(bào)警產(chǎn)生,如何去執(zhí)行遠(yuǎn)端的腳本,

4)
在主機(jī)服務(wù)的配置文件中,啟用時(shí)間處理機(jī)制
event_handler shell_name

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
檢測(cè)上面修改的文件,有沒(méi)有報(bào)錯(cuò)

殺掉個(gè)服務(wù),做下簡(jiǎn)單的測(cè)試,ok

不辜負(fù)zz的信任,解決了

搞定收工

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站制作、岳陽(yáng)樓網(wǎng)絡(luò)推廣、成都微信小程序、岳陽(yáng)樓網(wǎng)絡(luò)營(yíng)銷、岳陽(yáng)樓企業(yè)策劃、岳陽(yáng)樓品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供岳陽(yáng)樓建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:muchs.cn

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章標(biāo)題:nagios事件處理機(jī)制-創(chuàng)新互聯(lián)
文章源于:http://muchs.cn/article48/degoep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站改版、云服務(wù)器、電子商務(wù)、品牌網(wǎng)站建設(shè)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)