diff --git a/src/main/java/com/profiler/Agent.java b/src/main/java/com/profiler/Agent.java index dfc317729..9c30c810d 100644 --- a/src/main/java/com/profiler/Agent.java +++ b/src/main/java/com/profiler/Agent.java @@ -1,152 +1,145 @@ package com.profiler; +import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import java.util.Map.Entry; +import java.util.logging.Level; import java.util.logging.Logger; import com.profiler.common.dto.thrift.AgentInfo; +import com.profiler.common.util.SpanUtils; import com.profiler.context.TraceContext; import com.profiler.sender.DataSender; +import com.profiler.util.NetworkUtils; +import sun.management.resources.agent; public class Agent { - public static final String FQCN = Agent.class.getName(); + public static final String FQCN = Agent.class.getName(); - private static final Logger logger = Logger.getLogger(Agent.class.getName()); + private static final Logger logger = Logger.getLogger(Agent.class.getName()); - private volatile boolean alive = false; + private volatile boolean alive = false; - private final ServerInfo serverInfo; - private final SystemMonitor systemMonitor; + private final ServerInfo serverInfo; + private final SystemMonitor systemMonitor; - private final String agentId; - private final String applicationName; - - private Agent() { - this.serverInfo = new ServerInfo(); - this.systemMonitor = new SystemMonitor(); - - this.agentId = System.getProperty("hippo.agentId", getMachineName()); - this.applicationName = System.getProperty("hippo.applicationName", "TOMCAT"); - } + private final String agentId; + private final String applicationName; +// private boolean validate = true; - private static class SingletonHolder { - public static final Agent INSTANCE = new Agent(); - } + private Agent() { + this.serverInfo = new ServerInfo(); + this.systemMonitor = new SystemMonitor(); + String machineName = NetworkUtils.getMachineName(); + this.agentId = System.getProperty("hippo.agentId", machineName); + validateAgentId(); + this.applicationName = System.getProperty("hippo.applicationName", "TOMCAT"); + } - public static Agent getInstance() { - return SingletonHolder.INSTANCE; - } + private void validateAgentId() { + try { + byte[] bytes = agentId.getBytes("UTF-8"); + if (bytes.length > SpanUtils.AGENT_NAME_LIMIT) { + logger.warning("AgentId is too long(1~24) " + agentId); + } +// validate = false; + // TODO 이거 후처리를 어떻게 해야 될지. agent를 시작 시키지 않아야 될거 같은데. lifecycle이 이쪽저쪽에 퍼져 있어서 일관된 stop에 문제가 있음.. + } catch (UnsupportedEncodingException e) { + logger.log(Level.WARNING, "invalid agentId. Cause:" + e.getMessage(), e); + } - public boolean isAlive() { - return alive; - } + } - public void setIsAlive(boolean alive) { - this.alive = alive; - } + private static class SingletonHolder { + public static final Agent INSTANCE = new Agent(); + } - public ServerInfo getServerInfo() { - return this.serverInfo; - } + public static Agent getInstance() { + return SingletonHolder.INSTANCE; + } - public String getAgentId() { - return agentId; - } + public boolean isAlive() { + return alive; + } - public String getApplicationName() { - return applicationName; - } + public void setIsAlive(boolean alive) { + this.alive = alive; + } - private String getMachineName() { - try { - String name = null; - Enumeration enet = NetworkInterface.getNetworkInterfaces(); + public ServerInfo getServerInfo() { + return this.serverInfo; + } - while (enet.hasMoreElements() && (name == null)) { - NetworkInterface net = enet.nextElement(); + public String getAgentId() { + return agentId; + } - if (net.isLoopback()) - continue; + public String getApplicationName() { + return applicationName; + } - Enumeration eaddr = net.getInetAddresses(); - while (eaddr.hasMoreElements()) { - InetAddress inet = eaddr.nextElement(); + /** + * HIPPO 서버로 WAS정보를 전송한다. + */ + // TODO: life cycle을 체크하는 방법으로 바꿀까.. DEAD, STARTING, STARTED, STOPPING, + // STOPPED + public void sendStartupInfo() { + logger.info("Send startup information to HIPPO server."); - if (inet.getCanonicalHostName().equalsIgnoreCase(inet.getHostAddress()) == false) { - name = inet.getCanonicalHostName(); - break; - } - } - } - return name; - } catch (Exception e) { - logger.warning(e.getMessage()); - return "UNKNOWN-HOST"; - } - } - - /** - * HIPPO 서버로 WAS정보를 전송한다. - */ - // TODO: life cycle을 체크하는 방법으로 바꿀까.. DEAD, STARTING, STARTED, STOPPING, - // STOPPED - public void sendStartupInfo() { - logger.info("Send startup information to HIPPO server."); + String ip = getServerInfo().getHostip(); + String ports = ""; + for (Entry entry : getServerInfo().getConnectors().entrySet()) { + ports += " " + entry.getKey(); + } - String ip = getServerInfo().getHostip(); - String ports = ""; - for (Entry entry : getServerInfo().getConnectors().entrySet()) { - ports += " " + entry.getKey(); - } + AgentInfo agentInfo = new AgentInfo(); - AgentInfo agentInfo = new AgentInfo(); + agentInfo.setHostname(ip); + agentInfo.setPorts(ports); + agentInfo.setIsAlive(true); + agentInfo.setTimestamp(System.currentTimeMillis()); + agentInfo.setAgentId(getAgentId()); - agentInfo.setHostname(ip); - agentInfo.setPorts(ports); - agentInfo.setIsAlive(true); - agentInfo.setTimestamp(System.currentTimeMillis()); - agentInfo.setAgentId(getAgentId()); + DataSender.getInstance().addDataToSend(agentInfo); + } - DataSender.getInstance().addDataToSend(agentInfo); - } - - public void start() { - logger.info("Starting HIPPO Agent."); + public void start() { + logger.info("Starting HIPPO Agent."); // trace context 새롭게 생성. TraceContext.initialize(); - systemMonitor.start(); - } + systemMonitor.start(); + } - public void stop() { - logger.info("Stopping HIPPO Agent."); - systemMonitor.stop(); + public void stop() { + logger.info("Stopping HIPPO Agent."); + systemMonitor.stop(); - String ip = getServerInfo().getHostip(); - String ports = ""; - for (Entry entry : getServerInfo().getConnectors().entrySet()) { - ports += " " + entry.getKey(); - } + String ip = getServerInfo().getHostip(); + String ports = ""; + for (Entry entry : getServerInfo().getConnectors().entrySet()) { + ports += " " + entry.getKey(); + } - AgentInfo agentInfo = new AgentInfo(); + AgentInfo agentInfo = new AgentInfo(); - agentInfo.setHostname(ip); - agentInfo.setPorts(ports); - agentInfo.setIsAlive(false); - agentInfo.setTimestamp(System.currentTimeMillis()); - agentInfo.setAgentId(getAgentId()); + agentInfo.setHostname(ip); + agentInfo.setPorts(ports); + agentInfo.setIsAlive(false); + agentInfo.setTimestamp(System.currentTimeMillis()); + agentInfo.setAgentId(getAgentId()); - DataSender.getInstance().addDataToSend(agentInfo); - } + DataSender.getInstance().addDataToSend(agentInfo); + } - public static void startAgent() { - Agent.getInstance().start(); - } + public static void startAgent() { + Agent.getInstance().start(); + } - public static void stopAgent() throws Exception { - Agent.getInstance().stop(); - } + public static void stopAgent() throws Exception { + Agent.getInstance().stop(); + } } diff --git a/src/main/java/com/profiler/util/NetworkUtils.java b/src/main/java/com/profiler/util/NetworkUtils.java new file mode 100644 index 000000000..ee90486db --- /dev/null +++ b/src/main/java/com/profiler/util/NetworkUtils.java @@ -0,0 +1,41 @@ +package com.profiler.util; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; +import java.util.logging.Logger; + +/** + * + */ +public class NetworkUtils { + + public static String getMachineName() { + try { + String name = null; + Enumeration enet = NetworkInterface.getNetworkInterfaces(); + + while (enet.hasMoreElements() && (name == null)) { + NetworkInterface net = enet.nextElement(); + + if (net.isLoopback()) + continue; + + Enumeration eaddr = net.getInetAddresses(); + + while (eaddr.hasMoreElements()) { + InetAddress inet = eaddr.nextElement(); + + if (inet.getCanonicalHostName().equalsIgnoreCase(inet.getHostAddress()) == false) { + name = inet.getCanonicalHostName(); + break; + } + } + } + return name; + } catch (Exception e) { + Logger.getLogger(NetworkUtils.class.getClass().getName()).warning(e.getMessage()); + return "UNKNOWN-HOST"; + } + } +} diff --git a/src/test/java/com/profiler/util/HeaderUtilTest.java b/src/test/java/com/profiler/util/HeaderUtilTest.java deleted file mode 100644 index d776aa89a..000000000 --- a/src/test/java/com/profiler/util/HeaderUtilTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.profiler.util; - -import com.profiler.common.dto.Header; -import com.profiler.common.util.HeaderUtils; -import org.apache.thrift.TException; -import org.junit.Assert; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class HeaderUtilTest { - - private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); - - @Test - public void validateSignature() throws TException { - Header header = new Header(); - Assert.assertTrue(HeaderUtils.validateSignature(header.getSignature())); - - - Header error = new Header((byte) 0x11, (byte) 0x20, (short) 1); - Assert.assertTrue(!HeaderUtils.validateSignature(error.getSignature())); - - - logger.info(header.toString()); - } - -} diff --git a/src/test/java/com/profiler/util/NetworkUtilsTest.java b/src/test/java/com/profiler/util/NetworkUtilsTest.java new file mode 100644 index 000000000..8ea05aa32 --- /dev/null +++ b/src/test/java/com/profiler/util/NetworkUtilsTest.java @@ -0,0 +1,18 @@ +package com.profiler.util; + +import org.junit.Test; + +import java.util.logging.Logger; + +/** + * + */ +public class NetworkUtilsTest { + private Logger logger = Logger.getLogger(this.getClass().getName()); + + @Test + public void testGetMachineName() throws Exception { + String machineName = NetworkUtils.getMachineName(); + logger.info(machineName); + } +}