1、背景html
上一篇經過Java自帶的JConsole來獲取zookeeper狀態。主要有幾個不方便的地方,zk集羣通常會部署3或者5臺,在多個JConsole窗口中切換比較麻煩,各個zk服務及歷史數據之間,不能直觀比較。通常會作一個WEB管理頁面來展現集羣狀態,設置報警閥值來作報警。java
2、JVM平臺提供Mbeansapache
在Java5.0以上版本,有一組API可讓Java應用程序和容許的工具監視和管理Java虛擬機(JVM)和虛擬機所在的本機操做系統。該組API在 java.lang.management包。能夠經過這些API能夠監控local端JVM,同時也能夠監控遠端JVM。
經過靜態工廠方法獲取MXBean實例,從本地訪問正在運行的虛擬機的MXBean接口。這些Bean咱們從ManagementFactory類中定義的靜態方法獲取;如ManagementFactory.getOperatingSystemMXBean();其中不足就是隻能獲取本地的JVM狀態。沒法獲取遠程的虛擬機數據。bash
ClassLoadingMXBean Java虛擬機的類加載系統
CompilationMXBean Java虛擬機的編譯系統
MemoryMXBean Java虛擬機的內存系統
RuntimeMXBean Java虛擬機的運行時系統
OperatingSystemMXBean Java虛擬機在其上運行的操做系統
GarbageCollectorMXBean Java虛擬機中的垃圾回收器
MemoryManagerMXBean Java虛擬機中的內存管理器
MemoryPoolMXBean Java虛擬機中的內存池
3、Zookeeper提供出來的Mbeansapp
構造MXBean代理實例,經過代理將方法調用轉發到給定的MBeanServe。JConsole可以監控的項目,經過API都能獲取到。工具
具體代碼以下:ui
import java.io.IOException;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.zookeeper.server.ConnectionMXBean;
import org.apache.zookeeper.server.DataTreeMXBean;
import org.apache.zookeeper.server.ZooKeeperServerMXBean;
public class ZkJMXTest {
static JMXConnector connector;
/**
* @param args
* @throws IOException
* @throws MalformedObjectNameException
* @throws InstanceNotFoundException
* @throws ReflectionException
* @throws IntrospectionException
*/
public static void main(String[] args) throws IOException, MalformedObjectNameException,
InstanceNotFoundException, IntrospectionException, ReflectionException {
OperatingSystemMXBean osbean = ManagementFactory.getOperatingSystemMXBean();
System.out.println("體系結構:" + osbean.getArch());//操做系統體系結構
System.out.println("處理器核數:" + osbean.getAvailableProcessors());///核數
System.out.println("名字:" + osbean.getName());//名字
System.out.println(osbean.getVersion());//操做系統版本
ThreadMXBean threadBean=ManagementFactory.getThreadMXBean();
System.out.println("活動線程:" + threadBean.getThreadCount());//總線程數
ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
System.out.println("===========");
// 經過 MBeanServer間接地訪問 MXBean 接口
MBeanServerConnection mbsc = createMBeanServer("192.168.1.100", "9991", "controlRole", "123456");
// 操做系統
ObjectName os = new ObjectName("java.lang:type=OperatingSystem");
System.out.println("體系結構:" + getAttribute(mbsc, os, "Arch"));//體系結構
System.out.println("處理器核數:" + getAttribute(mbsc, os, "AvailableProcessors"));//核數
System.out.println("總物理內存:" + getAttribute(mbsc, os, "TotalPhysicalMemorySize"));//總物理內存
System.out.println("空閒物理內存:" + getAttribute(mbsc, os, "FreePhysicalMemorySize"));//空閒物理內存
System.out.println("總交換空間:" + getAttribute(mbsc, os, "TotalSwapSpaceSize"));//總交換空間
System.out.println("空閒交換空間:" + getAttribute(mbsc, os, "FreeSwapSpaceSize"));//空閒交換空間
System.out.println("操做系統:" + getAttribute(mbsc, os, "Name")+ getAttribute(mbsc, os, "Version"));//操做系統
System.out.println("提交的虛擬內存:" + getAttribute(mbsc, os, "CommittedVirtualMemorySize"));//提交的虛擬內存
System.out.println("系統cpu使用率:" + getAttribute(mbsc, os, "SystemCpuLoad"));//系統cpu使用率
System.out.println("進程cpu使用率:" + getAttribute(mbsc, os, "ProcessCpuLoad"));//進程cpu使用率
System.out.println("============");//
// 線程
ObjectName Threading = new ObjectName("java.lang:type=Threading");
System.out.println("活動線程:" + getAttribute(mbsc, Threading, "ThreadCount"));// 活動線程
System.out.println("守護程序線程:" + getAttribute(mbsc, Threading, "DaemonThreadCount"));// 守護程序線程
System.out.println("峯值:" + getAttribute(mbsc, Threading, "PeakThreadCount"));// 峯值
System.out.println("啓動的線程總數:" + getAttribute(mbsc, Threading, "TotalStartedThreadCount"));// 啓動的線程總數
ThreadMXBean threadBean2 = ManagementFactory.newPlatformMXBeanProxy
(mbsc, ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean.class);
System.out.println("活動線程:" + threadBean2.getThreadCount());// 活動線程
ThreadMXBean threadBean3 = ManagementFactory.getThreadMXBean();
System.out.println("本地活動線程:" + threadBean3.getThreadCount());// 本地活動線程
System.out.println("============");//
ObjectName Compilation = new ObjectName("java.lang:type=Compilation");
System.out.println("總編譯時間 毫秒:" + getAttribute(mbsc, Compilation, "TotalCompilationTime"));// 總編譯時間 毫秒
System.out.println("============");//
ObjectName ClassLoading = new ObjectName("java.lang:type=ClassLoading");
System.out.println("已加載類總數:" + getAttribute(mbsc, ClassLoading, "TotalLoadedClassCount"));// 已加載類總數
System.out.println("已加裝當前類:" + getAttribute(mbsc, ClassLoading, "LoadedClassCount"));// 已加裝當前類
System.out.println("已卸載類總數:" + getAttribute(mbsc, ClassLoading, "UnloadedClassCount"));// 已卸載類總數
System.out.println("==========================================================");//
// http://zookeeper.apache.org/doc/r3.4.6/zookeeperJMX.html
// org.apache.ZooKeeperService:name0=ReplicatedServer_id1,name1=replica.1,name2=Follower
ObjectName replica = new ObjectName("org.apache.ZooKeeperService:name0=ReplicatedServer_id1,name1=replica.1");
System.out.println("replica.1運行狀態:" + getAttribute(mbsc, replica, "State"));// 運行狀態
mbsc = createMBeanServer("192.168.1.100", "9992", "controlRole", "123456");
System.out.println("==============節點樹對象===========");
ObjectName dataTreePattern = new ObjectName("org.apache.ZooKeeperService:name0=ReplicatedServer_id?,name1=replica.?,name2=*,name3=InMemoryDataTree");
Set<ObjectName> dataTreeSets = mbsc.queryNames(dataTreePattern, null);
Iterator<ObjectName> dataTreeIterator = dataTreeSets.iterator();
// 只有一個
while (dataTreeIterator.hasNext()) {
ObjectName dataTreeObjectName = dataTreeIterator.next();
DataTreeMXBean dataTree = JMX.newMBeanProxy(mbsc, dataTreeObjectName, DataTreeMXBean.class);
System.out.println("節點總數:" + dataTree.getNodeCount());// 節點總數
System.out.println("Watch總數:" + dataTree.getWatchCount());// Watch總數
System.out.println("臨時節點總數:" + dataTree.countEphemerals());// Watch總數
System.out.println("節點名及字符總數:" + dataTree.approximateDataSize());// 節點全路徑和值的總字符數
Map<String, String> dataTreeMap = dataTreeObjectName.getKeyPropertyList();
String replicaId = dataTreeMap.get("name1").replace("replica.", "");
String role = dataTreeMap.get("name2");// Follower,Leader,Observer,Standalone
String canonicalName = dataTreeObjectName.getCanonicalName();
int roleEndIndex = canonicalName.indexOf(",name3");
ObjectName roleObjectName = new ObjectName(canonicalName.substring(0, roleEndIndex));
System.out.println("==============zk服務狀態===========");
ZooKeeperServerMXBean ZooKeeperServer = JMX.newMBeanProxy(mbsc, roleObjectName, ZooKeeperServerMXBean.class);
System.out.println(role + " 的IP和端口:" + ZooKeeperServer.getClientPort());// IP和端口
System.out.println(role + " 活着的鏈接數:" + ZooKeeperServer.getNumAliveConnections());// 鏈接數
System.out.println(role + " 未完成請求數:" + ZooKeeperServer.getOutstandingRequests());// 未完成的請求數
System.out.println(role + " 接收的包:" + ZooKeeperServer.getPacketsReceived());// 收到的包
System.out.println(role + " 發送的包:" + ZooKeeperServer.getPacketsSent());// 發送的包
System.out.println(role + " 平均延遲(毫秒):" + ZooKeeperServer.getAvgRequestLatency());
System.out.println(role + " 最大延遲(毫秒):" + ZooKeeperServer.getMaxRequestLatency());
System.out.println(role + " 每一個客戶端IP容許的最大鏈接數:" + ZooKeeperServer.getMaxClientCnxnsPerHost());
System.out.println(role + " 最大Session超時(毫秒):" + ZooKeeperServer.getMaxSessionTimeout());
System.out.println(role + " 心跳時間(毫秒):" + ZooKeeperServer.getTickTime());
System.out.println(role + " 版本:" + ZooKeeperServer.getVersion());// 版本
// 三個重置操做
// ZooKeeperServer.resetLatency(); //重置min/avg/max latency statistics
// ZooKeeperServer.resetMaxLatency(); //重置最大延遲統計
// ZooKeeperServer.resetStatistics(); // 重置包和延遲全部統計
System.out.println("==============全部客戶端的鏈接信息===========");
ObjectName connectionPattern = new ObjectName("org.apache.ZooKeeperService:name0=ReplicatedServer_id?,name1=replica.?,name2=*,name3=Connections,*");
Set<ObjectName> connectionSets = mbsc.queryNames(connectionPattern, null);
List<ObjectName> connectionList = new ArrayList<ObjectName>(connectionSets.size());
connectionList.addAll(connectionSets);
Collections.sort(connectionList);
for (ObjectName connectionON : connectionList) {
System.out.println("=========================");
ConnectionMXBean connectionBean = JMX.newMBeanProxy(mbsc, connectionON, ConnectionMXBean.class);
System.out.println(" IP+Port:" + connectionBean.getSourceIP());//
System.out.println(" SessionId:" + connectionBean.getSessionId());//
System.out.println(" PacketsReceived:" + connectionBean.getPacketsReceived());// 收到的包
System.out.println(" PacketsSent:" + connectionBean.getPacketsSent());// 發送的包
System.out.println(" MinLatency:" + connectionBean.getMinLatency());//
System.out.println(" AvgLatency:" + connectionBean.getAvgLatency());//
System.out.println(" MaxLatency:" + connectionBean.getMaxLatency());//
System.out.println(" StartedTime:" + connectionBean.getStartedTime());//
System.out.println(" EphemeralNodes:" + connectionBean.getEphemeralNodes().length);//
System.out.println(" EphemeralNodes:" + Arrays.asList(connectionBean.getEphemeralNodes()));//
System.out.println(" OutstandingRequests:" + connectionBean.getOutstandingRequests());//
//connectionBean.resetCounters();
//connectionBean.terminateConnection();
//connectionBean.terminateSession();
}
}
// close connection
if (connector != null) {
connector.close();
}
}
/**
* 創建鏈接
* @param ip
* @param jmxport
* @return
*/
public static MBeanServerConnection createMBeanServer(String ip,
String jmxport, String userName, String password) {
try {
String jmxURL = "service:jmx:rmi:///jndi/rmi://" + ip + ":"
+ jmxport + "/jmxrmi";
// jmxurl
JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
Map<String, String[]> map = new HashMap<String, String[]>();
String[] credentials = new String[] { userName, password };
map.put("jmx.remote.credentials", credentials);
connector = JMXConnectorFactory.connect(serviceURL, map);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
return mbsc;
} catch (IOException ioe) {
ioe.printStackTrace();
System.err.println(ip + ":" + jmxport + " 鏈接創建失敗");
}
return null;
}
/**
* 使用MBeanServer獲取對象名爲[objName]的MBean的[objAttr]屬性值
* <p>
* 靜態代碼: return MBeanServer.getAttribute(ObjectName name, String attribute)
* @param mbeanServer
* - MBeanServer實例
* @param objName
* - MBean的對象名
* @param objAttr
* - MBean的某個屬性名
* @return 屬性值
*/
private static String getAttribute(MBeanServerConnection mbeanServer,
ObjectName objName, String objAttr) {
if (mbeanServer == null || objName == null || objAttr == null)
throw new IllegalArgumentException();
try {
return String.valueOf(mbeanServer.getAttribute(objName, objAttr));
} catch (Exception e) {
return null;
}
}
}複製代碼