需求说明
期初是我们的物理机上安装了Hbase,由于物理机硬件配置还可以,1T内存,64核。
只有4台机器,我们装完Hbase后,发现应用请求比较多,导致RegionServer经常挂掉。
但是机器本身资源使用率并不高,因此我们希望在一个节点上启用多个RegionServer服务。
如果一个节点启动2个RegionServe服务,那么通过服务名称方式就无法监控每个服务,所以改用了端口监控的方式。
当服务出现异常挂掉后,可以自动报警,并自动拉起该服务。
1. 通过服务名监控
monitor_regionserver.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#!/bin/sh # 必须配置,引入环境变量;不然使用crond 定时执行脚本无法启动Java应用 source /etc/profile #当前时间 now=` date + "%Y-%m-%d %H:%M:%S" ` file_name= "/opt/local/listen/monitor.log" #重启脚本的日志,保证可写入,保险一点执行 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 #执行proc_num(),获取进程数 number=$? #获取上一函数返回值 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 "生产服务器:${hostname} HRegionServer已停止,正在重启服务,PID -> ${pid}" else proc_id echo "${now} 应用服务:HRegionServer 正常,进程号-> ${pid}" >> $file_name fi |
2. 通过端口监控
端口监控有2个脚本,一个是监控脚本listen_port.sh,一个用来执行的脚本monitor_port.sh。
monitor_port.sh可以直接用命令代替。
脚本listen_port.sh,用来监听指定端口的RegionServer,运行时需要指定端口号。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#!/bin/sh source /etc/profile #指定脚本路径 script_path= /opt/local/listen/ if [ $ # != 2 ];then echo '请输入端口和Hbase的路径' exit 1; fi port=$1 hbase_home=$2 echo '正在监听端口号:' $port #当前时间 now=` date + "%Y-%m-%d %H:%M:%S" ` file_name=${script_path} "monitor.log" #重启脚本的日志,保证可写入,保险一点执行 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 #执行proc_num(),获取进程数 number=$? #获取上一函数返回值 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,用来执行listen_port.sh脚本。
1
2
3
4
5
|
#!/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. 企业微信消息通知脚本
微信报警脚本weixin.sh,将下面的xxxxxxxxx换成自己的key就好。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/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.定时调度
使用crontab每间隔1分钟执行一次。
1
2
3
4
5
|
# 监控服务名的 * /1 * * * * sh /opt/local/listen/monitor_regionserver .sh > /dev/null 2>&1 # 监控端口的 * /1 * * * * sh /opt/local/listen/monitor_port .sh > /dev/null 2>&1 |
5. 报警信息
报警信息样式可以自己在weixin.sh中定义,支持markdown写法。
到此这篇关于shell脚本实现Hbase服务的监控报警和自动拉起的文章就介绍到这了,更多相关shell脚本Hbase服务监控报警内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/30go/p/16873424.html