Hbase集群监控

Hbase集群监控方式,通过集群监控平台url可以获取json信息

推荐json识别网站:http://www.bejson.com/jsonviewernew/

集群监控URL:http://10.2.4.41:16010/jmx?description=true 端口与ip根据实际情况更改,一般部署后集群可正常访问其url

属性文件

#hbase监控url配置
hbase.url=http://10.2.4.41:16010/jmx?description=true

监控timer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import org.hornetq.utils.json.JSONArray;
import org.hornetq.utils.json.JSONException;
import org.hornetq.utils.json.JSONObject;

/**
 * HbaseMonitor schedule
 */
public class HbaseMonitor {
    private static String hbase_url = "";
    private static Timer timer = new Timer();
    static {
        InputStream is = HbaseMonitor.class.getResourceAsStream("/monconfig.properties");
        Properties props = new Properties();
        try {
            props.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //hbase url配置
        hbase_url = props.getProperty("hbase.url");
        if(null == hbase_url || "".equals(hbase_url)){
            System.out.println("hbase.url is not config at monconfig.properties");
        }else{
            try {
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println(start());
                    }
                }, 5000, 5000);
            } catch (Exception e) {

            }
        }
    }
    
    public static void main(String[] args){}
    
    /**
     * HbaseMonitor 入口
     */
    public static boolean start() {
        try {

            String hbaseinfojson = getReturnData(hbase_url);
            if ("error".equals(hbaseinfojson)) {
                System.out.println("hbase-master down");
                return false;
            } else {
                return jsonTomap(hbaseinfojson);

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 访问url 获取json
     */
    public static String getReturnData(String urlString)
        throws UnsupportedEncodingException {
        String res = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url
                .openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            InputStream is = conn.getInputStream();

            BufferedReader in = new java.io.BufferedReader(
                new InputStreamReader(is, "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line;
            }
            in.close();

        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
            return "error";
        }
        return res;
    }

    public static boolean jsonTomap(String json) {
        String jsonstr = json;
        try {
            JSONObject jsonObject = new JSONObject(jsonstr);
            JSONArray name = (JSONArray) jsonObject.get("beans");
            String aliveString = "";
            for(int i = 0 ;i<name.length();i++){
                if("Hadoop:service=HBase,name=Master,sub=Server".equals(jsonString(name.get(i).toString(),"name"))){
                    aliveString = name.get(i).toString();  
                }
            }
            String RServeralive = jsonString(aliveString,"tag.liveRegionServers");
            String RServerdead = jsonString(aliveString,"tag.deadRegionServers");
            String MServer = jsonString(aliveString, "tag.isActiveMaster");
            
            RServeralive = jsonString(RServeralive, "value");
            RServerdead = jsonString(RServerdead, "value");
            MServer = jsonString(MServer, "value");
            
            System.out.println("MServer:"+MServer+" RegionServersAlive:"+RServeralive + " RServerdead:"+RServerdead);
            
            /**
             * true  master存活  值为 1  
             * false master死亡  值为0
             * 情景描述
             * 当master为0时 主节点死亡
             * 当rServerAliveInfo为空时,regionserver没有存活节点
             * 当rServerDeadInfo不为空时,regionserver存在死亡节点
             */
            if("false".equals(MServer)
               || "".equals(RServeralive)
               || !("".equals(RServerdead))){
               return false;
            }else{
               return true;
            }
 
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String jsonString(String json, String tag) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            return jsonObject.getString(tag);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值