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;
}
}