If you want to write a Java program that monitors zookeeper, you can do this in two ways:(1) Send command through Telnetclient, the order of the detailed reference: Http://zookeeper.apache.org/doc/trunk/zookeeperAdmin.html#sc_zkCommands (2) through JMX, Instructions please refer to: http://zookeeper.apache.org/doc/trunk/zookeeperJMX.html
This article is a simple example of how to use the Telnetclient send Mntr command to get zookeeper monitoring data
Write a tool class for Telnet
Package Com.eric.agent.utils;import Org.apache.commons.net.telnet.telnetclient;import Java.io.IOException;import Java.io.inputstream;import java.io.printstream;/** * Telnet operator, based on Commons-net-2.2.jar * * @author Aihua.sun * @date 2015/ 4/9 * @since V1.0 */public class Telnettools {private string prompt = ">";//End identification string, >,linux in Windows is # PRIV ate char PromptChar = ' > '; End identity character private telnetclient telnet; Private InputStream in; Input stream, receive return information private printstream out; Write command to Server/** * @param termtype protocol type: VT100, VT52, VT220, VTNT, ANSI * @param prompt result End identity */Public Te Lnettools (string TermType, string prompt) {telnet = new telnetclient (TermType); Setprompt (prompt); } public Telnettools (String termtype) {telnet = new telnetclient (TermType); } public Telnettools () {telnet = new telnetclient (); /** * Login to target host * * @param IP * @param port */public void login (String IP, int port) {try {telnet.connect (IP, port); in = Telnet.getinputstream (); out = new PrintStream (Telnet.getoutputstream ()); } catch (Exception e) {throw new RuntimeException (e); }}/** * Read analysis Results * * @param pattern matches to the string when the result is returned * @return */public string Readuntil (string pattern) {StringBuffer sb = new StringBuffer (); try {char Lastchar = (char)-1; Boolean flag = pattern! = null && pattern.length () > 0; if (flag) Lastchar = Pattern.charat (Pattern.length ()-1); Char ch; int code =-1; while ((Code = In.read ())! =-1) {ch = (char) code; Sb.append (CH); Match to end identity returns the result if (flag) {if (ch = = Lastchar && sb.tostring (). EndsWith (Pattern) ) {return sb.tostring (); } } else {//If the end identity is not specified, match to the default end identifier character when the result is returned if (ch = = PromptChar) return sb.tostring (); }//Login failed when returning results if (sb.tostring (). Contains ("Login Failed")) {return Sb.tos Tring (); }}} catch (Exception e) {e.printstacktrace (); } return sb.tostring (); /** * Send Command * * @param value */public void Write (String value) {try {Out.print ln (value); Out.flush (); } catch (Exception e) {e.printstacktrace (); }}/** * Send command, return execution result * * @param command * @return */public string SendCommand (string Comman D) {try {write (command); return Readuntil (Prompt); } catch (Exception e) {e.printstacktrace (); } return null; }/** * Close connection */publicvoid distinct () {try {if (telnet! = null &&!telnet.isconnected ()) Telnet.disco Nnect (); } catch (IOException e) {e.printstacktrace (); }} public void Setprompt (String prompt) {if (prompt! = null) {this.prompt = prompt; This.promptchar = Prompt.charat (Prompt.length ()-1); } }}
Call class
Package com.tscloud.agent.flume.source.dataprovider;/** * For the source * Details of the HDFS Master Monitoring information via HTTP, refer to HTTP// Zookeeper.apache.org/doc/r3.4.6/zookeeperadmin.html#sc_zkcommands * @author Aihua.sun * @date 2015/4/6 * @since V1.0 * * Import Com.eric.agent.flume.model.zookeeperroleinfo;import Com.eric.agent.flume.source.base.iclusterserviceroledataprovider;import com.eric.agent.utils.AgentConstants; Import Com.eric.agent.utils.telnettools;import Com.eric.common.utils.stringutils;import Org.slf4j.Logger;import Org.slf4j.loggerfactory;import java.io.unsupportedencodingexception;import java.util.*;p ublic class zookeeperdataprovider{protected final Logger Logger = Loggerfactory.getlogger (GetClass ()); private static final String zk_avg_latency = "Zk_avg_latency"; private static final String zk_max_latency = "Zk_max_latency"; private static final String zk_min_latency = "Zk_min_latency"; private static final String zk_packets_received = "zk_packets_received"; private static Final String Zk_packets_sent = "Zk_packets_sent"; private static final String zk_server_state = "Zk_server_state"; private static final String Zk_znode_count = "Zk_znode_count"; private static final String zk_followers = "Zk_followers"; private static final String Zk_open_file_descriptor_count = "Zk_open_file_descriptor_count"; Public String Extractmonitordata () {//todo obtains IP and parameter Zookeeperroleinfo by calling API Monitordatapoint = new Zookeep Erroleinfo (); String IP = "192.168.40.242"; int port = 2181; Telnettools Telnet=null; try {telnet = new telnettools (); Telnet.login (IP, Port); String rs = Telnet.sendcommand ("Mntr"); map<string, string> Telnetresultmap = Parsetelnetresult (RS); Monitordatapoint.setzkavglatency (Translatestrtolong (Telnetresultmap.get (zk_avg_latency))); Monitordatapoint.setzkmaxlatency (Translatestrtolong (Telnetresultmap.get (zk_max_latency))); MonitorDatapoint.setzkminlatency (Translatestrtolong (Telnetresultmap.get (zk_min_latency))); Monitordatapoint.setzkpacketsreceived (Translatestrtolong (Telnetresultmap.get (zk_packets_received))); Monitordatapoint.setzkpacketssent (Translatestrtolong (Telnetresultmap.get (zk_packets_sent))); Monitordatapoint.setzkserverstate (Telnetresultmap.get (zk_server_state)); Monitordatapoint.setzkznodecount (Translatestrtolong (Telnetresultmap.get (Zk_znode_count))); Monitordatapoint.setzkfollowers (Translatestrtolong (Telnetresultmap.get (zk_followers))); Monitordatapoint.setzkopenfiledescriptorcount (Translatestrtolong (Telnetresultmap.get (zk_open_file_descriptor_ count)); } catch (Exception e) {e.printstacktrace (); }finally{telnet.distinct (); } return monitordatapoint.tostring (); The private Long Translatestrtolong (String value) {if (Org.apache.commons.lang.StringUtils.isAlphanumeric (value) {return long.valueof (value); } return 0L; } private Map<string, string> Parsetelnetresult (String rs) {//the output contains multiple lines with the Following format://key \ t value string[] resultarray = rs.split ("\ n"); map<string, string> resultmap = new hashmap<string, string> (); for (String Recordline:resultarray) {string[] Recordkeyvalue = recordline.split ("\ t"); Logger.debug ("########### #recordKeyValue. Size:" + recordkeyvalue.length + "Recordkeyvalue:" + arrays.tostring ( Recordkeyvalue)); if (recordkeyvalue! = null && recordkeyvalue.length = = 2) {resultmap.put (recordkeyvalue[0], record KEYVALUE[1]); }} return resultmap; } public static void Main (string[] args) {System.out.println (New Zookeeperdataprovider (). Extractmonitordata ()); }}
Get zookeeper monitoring data via telnetclient