shell腳本實(shí)現(xiàn)Hbase服務(wù)的監(jiān)控報(bào)警和自動(dòng)拉起問(wèn)題
需求說(shuō)明
期初是我們的物理機(jī)上安裝了Hbase,由于物理機(jī)硬件配置還可以,1T內(nèi)存,64核。
只有4臺(tái)機(jī)器,我們裝完Hbase后,發(fā)現(xiàn)應(yīng)用請(qǐng)求比較多,導(dǎo)致RegionServer經(jīng)常掛掉。
但是機(jī)器本身資源使用率并不高,因此我們希望在一個(gè)節(jié)點(diǎn)上啟用多個(gè)RegionServer服務(wù)。
如果一個(gè)節(jié)點(diǎn)啟動(dòng)2個(gè)RegionServe服務(wù),那么通過(guò)服務(wù)名稱(chēng)方式就無(wú)法監(jiān)控每個(gè)服務(wù),所以改用了端口監(jiān)控的方式。
當(dāng)服務(wù)出現(xiàn)異常掛掉后,可以自動(dòng)報(bào)警,并自動(dòng)拉起該服務(wù)。
1. 通過(guò)服務(wù)名監(jiān)控
monitor_regionserver.sh
#!/bin/sh # 必須配置,引入環(huán)境變量;不然使用crond 定時(shí)執(zhí)行腳本無(wú)法啟動(dòng)Java應(yīng)用 source /etc/profile #當(dāng)前時(shí)間 now=`date +"%Y-%m-%d %H:%M:%S"` file_name="/opt/local/listen/monitor.log" #重啟腳本的日志,保證可寫(xiě)入,保險(xiǎn)一點(diǎn)執(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(),獲取進(jìn)程數(shù) number=$? #獲取上一函數(shù)返回值 if [ $number -eq 0 ];then /opt/local/hbase/bin/hbase-daemon.sh start regionserver sleep 5 proc_id echo "${now} 應(yīng)用服務(wù):HRegionServer不存在,正在重啟服務(wù),進(jìn)程號(hào) -> ${pid}" >> $file_name #把重啟的進(jìn)程號(hào)、時(shí)間 寫(xiě)入日志 /opt/local/listen/weixin.sh "生產(chǎn)服務(wù)器:${hostname} HRegionServer已停止,正在重啟服務(wù),PID -> ${pid}" else proc_id echo "${now} 應(yīng)用服務(wù):HRegionServer 正常,進(jìn)程號(hào)-> ${pid}" >> $file_name fi
2. 通過(guò)端口監(jiān)控
端口監(jiān)控有2個(gè)腳本,一個(gè)是監(jiān)控腳本listen_port.sh,一個(gè)用來(lái)執(zhí)行的腳本monitor_port.sh。
monitor_port.sh可以直接用命令代替。
腳本listen_port.sh,用來(lái)監(jiān)聽(tīng)指定端口的RegionServer,運(yùn)行時(shí)需要指定端口號(hào)。
#!/bin/sh source /etc/profile #指定腳本路徑 script_path=/opt/local/listen/ if [ $# != 2 ];then echo '請(qǐng)輸入端口和Hbase的路徑' exit 1; fi port=$1 hbase_home=$2 echo '正在監(jiān)聽(tīng)端口號(hào):' $port #當(dāng)前時(shí)間 now=`date +"%Y-%m-%d %H:%M:%S"` file_name=${script_path}"monitor.log" #重啟腳本的日志,保證可寫(xiě)入,保險(xiǎn)一點(diǎn)執(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(),獲取進(jìn)程數(shù) number=$? #獲取上一函數(shù)返回值 if [ $number -eq 0 ];then $hbase_home/bin/hbase-daemon.sh start regionserver sleep 5 proc_id echo "${now} 應(yīng)用服務(wù):HRegionServer不存在,正在重啟服務(wù),端口:${port} 進(jìn)程號(hào):${pid}" >> $file_name #把重啟的進(jìn)程號(hào)、時(shí)間 寫(xiě)入日志 ${script_path}/weixin.sh "測(cè)試服務(wù)器:${hostname}:${port} HRegionServer已停止,正在重啟服務(wù),PID -> ${pid}" else proc_id echo "${now} HRegionServer 正常,端口:${port} 進(jìn)程號(hào):${pid}" >> $file_name fi
腳本monitor_port.sh,用來(lái)執(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è)微信消息通知腳本
微信報(bào)警腳本weixin.sh,將下面的xxxxxxxxx換成自己的key就好。
#!/bin/bash content=${@:1} content=${content//\ /} content=${content//\"/} date=$(date +%Y-%m-%d) time=$(date "+%H:%M:%S") content=" **Hbase** >告警時(shí)間:$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.定時(shí)調(diào)度
使用crontab每間隔1分鐘執(zhí)行一次。
# 監(jiān)控服務(wù)名的 */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. 報(bào)警信息
報(bào)警信息樣式可以自己在weixin.sh中定義,支持markdown寫(xiě)法。
到此這篇關(guān)于shell腳本實(shí)現(xiàn)Hbase服務(wù)的監(jiān)控報(bào)警和自動(dòng)拉起的文章就介紹到這了,更多相關(guān)shell腳本Hbase服務(wù)監(jiān)控報(bào)警內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解在dotnet core實(shí)現(xiàn)類(lèi)似crontab的定時(shí)任務(wù)
這篇文章主要介紹了在dotnet core實(shí)現(xiàn)類(lèi)似crontab的定時(shí)任務(wù),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07shell中使用expect命令進(jìn)行遠(yuǎn)程執(zhí)行命令腳本
今天小編就為大家分享一篇關(guān)于shell中使用expect命令進(jìn)行遠(yuǎn)程執(zhí)行命令腳本,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03如何短時(shí)間內(nèi)學(xué)好一門(mén)語(yǔ)言 shell腳本語(yǔ)言為例
這篇文章主要以shell腳本語(yǔ)言為例,為大家介紹了如何短時(shí)間內(nèi)學(xué)好一門(mén)語(yǔ)言,感興趣的小伙伴們可以參考一下2016-09-09linux下安裝oracle后使用命令行啟動(dòng)的方法 linux啟動(dòng)oracle
這篇文章主要介紹了在linux下安裝oracle后使用命令啟動(dòng)oracle的方法,大家參考使用吧2014-01-01Linux里awk中split函數(shù)的用法小結(jié)
The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep2013-03-03shell linux中如何用shell寫(xiě)一個(gè)占用CPU的腳本
本文主要介紹了shell linux中如何用shell寫(xiě)一個(gè)占用CPU的腳本,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09