shell腳本實現(xiàn)Hbase服務的監(jiān)控報警和自動拉起問題
需求說明
期初是我們的物理機上安裝了Hbase,由于物理機硬件配置還可以,1T內(nèi)存,64核。
只有4臺機器,我們裝完Hbase后,發(fā)現(xiàn)應用請求比較多,導致RegionServer經(jīng)常掛掉。
但是機器本身資源使用率并不高,因此我們希望在一個節(jié)點上啟用多個RegionServer服務。
如果一個節(jié)點啟動2個RegionServe服務,那么通過服務名稱方式就無法監(jiān)控每個服務,所以改用了端口監(jiān)控的方式。
當服務出現(xiàn)異常掛掉后,可以自動報警,并自動拉起該服務。
1. 通過服務名監(jiān)控
monitor_regionserver.sh
#!/bin/sh
# 必須配置,引入環(huán)境變量;不然使用crond 定時執(zhí)行腳本無法啟動Java應用
source /etc/profile
#當前時間
now=`date +"%Y-%m-%d %H:%M:%S"`
file_name="/opt/local/listen/monitor.log" #重啟腳本的日志,保證可寫入,保險一點執(zhí)行 chmod 777 data.log
pid=0
hostname=`hostname`
proc_num()
{
num=`ps -ef | grep 'HRegionServer' | grep -v grep | wc -l`
return $num
}
proc_id()
{
pid=`ps -ef | grep 'HRegionServer' | grep -v grep | awk '{print $2}'`
}
proc_num #執(zhí)行proc_num(),獲取進程數(shù)
number=$? #獲取上一函數(shù)返回值
if [ $number -eq 0 ];then
/opt/local/hbase/bin/hbase-daemon.sh start regionserver
sleep 5
proc_id
echo "${now} 應用服務:HRegionServer不存在,正在重啟服務,進程號 -> ${pid}" >> $file_name #把重啟的進程號、時間 寫入日志
/opt/local/listen/weixin.sh "生產(chǎn)服務器:${hostname} HRegionServer已停止,正在重啟服務,PID -> ${pid}"
else
proc_id
echo "${now} 應用服務:HRegionServer 正常,進程號-> ${pid}" >> $file_name
fi
2. 通過端口監(jiān)控
端口監(jiān)控有2個腳本,一個是監(jiān)控腳本listen_port.sh,一個用來執(zhí)行的腳本monitor_port.sh。
monitor_port.sh可以直接用命令代替。
腳本listen_port.sh,用來監(jiān)聽指定端口的RegionServer,運行時需要指定端口號。
#!/bin/sh
source /etc/profile
#指定腳本路徑
script_path=/opt/local/listen/
if [ $# != 2 ];then
echo '請輸入端口和Hbase的路徑'
exit 1;
fi
port=$1
hbase_home=$2
echo '正在監(jiān)聽端口號:' $port
#當前時間
now=`date +"%Y-%m-%d %H:%M:%S"`
file_name=${script_path}"monitor.log" #重啟腳本的日志,保證可寫入,保險一點執(zhí)行 chmod 777 data.log
pid=0
hostname=`hostname`
proc_num()
{
num=`netstat -nltp | grep ${port} |awk '{print $4}'|grep -v grep|wc -l`
return $num
}
proc_id()
{
pid=`netstat -nltp | grep ${port} |awk '{print $7}'|cut -d/ -f1`
}
proc_num #執(zhí)行proc_num(),獲取進程數(shù)
number=$? #獲取上一函數(shù)返回值
if [ $number -eq 0 ];then
$hbase_home/bin/hbase-daemon.sh start regionserver
sleep 5
proc_id
echo "${now} 應用服務:HRegionServer不存在,正在重啟服務,端口:${port} 進程號:${pid}" >> $file_name #把重啟的進程號、時間 寫入日志
${script_path}/weixin.sh "測試服務器:${hostname}:${port} HRegionServer已停止,正在重啟服務,PID -> ${pid}"
else
proc_id
echo "${now} HRegionServer 正常,端口:${port} 進程號:${pid}" >> $file_name
fi
腳本monitor_port.sh,用來執(zhí)行l(wèi)isten_port.sh腳本。
#!/bin/sh source /etc/profile /opt/local/listen/listen_port.sh 16020 /opt/local/hbase/ sleep 1 /opt/local/listen/listen_port.sh 16120 /opt/local/hbase2/
3. 企業(yè)微信消息通知腳本
微信報警腳本weixin.sh,將下面的xxxxxxxxx換成自己的key就好。
#!/bin/bash
content=${@:1}
content=${content//\ /}
content=${content//\"/}
date=$(date +%Y-%m-%d)
time=$(date "+%H:%M:%S")
content="
**Hbase**
>告警時間:$date.$time
>告警詳情:$content
"
webHookUrl="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxxxxxx"
content='{"msgtype": "markdown","markdown": {"content": "'$content'","mentioned_list":"@all"},}'
echo "content : $content"
curl --data-ascii "$content" $webHookUrl
echo "over!"4.定時調(diào)度
使用crontab每間隔1分鐘執(zhí)行一次。
# 監(jiān)控服務名的 */1 * * * * sh /opt/local/listen/monitor_regionserver.sh >/dev/null 2>&1 # 監(jiān)控端口的 */1 * * * * sh /opt/local/listen/monitor_port.sh >/dev/null 2>&1
5. 報警信息
報警信息樣式可以自己在weixin.sh中定義,支持markdown寫法。

到此這篇關于shell腳本實現(xiàn)Hbase服務的監(jiān)控報警和自動拉起的文章就介紹到這了,更多相關shell腳本Hbase服務監(jiān)控報警內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
虛擬機中使用linux系啟用文件共享之后的文件存在的位置方法
下面小編就為大家?guī)硪黄摂M機中使用linux系啟用文件共享之后的文件存在的位置方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
通過shell腳本循環(huán)進入目錄執(zhí)行命令的方法
今天小編就為大家分享一篇通過shell腳本循環(huán)進入目錄執(zhí)行命令的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

