通过TelnetClient获取Zookeeper监控数据(2)

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.*;

public 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 通过调用API获得IP以及参数
        ZookeeperRoleInfo monitorDataPoint = new ZookeeperRoleInfo();
        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();
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/991f8042ac5151dde4afc038b26f21a2.html