From 3997ab4ac448b8f282090600249275eebecfc0e6 Mon Sep 17 00:00:00 2001 From: Woonduk Kang Date: Tue, 30 Oct 2012 07:17:22 +0000 Subject: [PATCH] =?UTF-8?q?[=EA=B0=95=EC=9A=B4=EB=8D=95]=20[LUCYSUS-1744]?= =?UTF-8?q?=20agendid=EC=9D=98=20limit=20=EB=84=98=EC=9D=84=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=20=EB=A9=94=EC=8B=9C=EC=A7=80=EB=A5=BC=20=EC=B0=8D?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD.=20=EB=84=98=EC=9D=84?= =?UTF-8?q?=20=EA=B2=BD=EC=9A=B0=EC=9D=98=20=ED=9B=84=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=EA=B0=80=20=EB=B6=80=EC=A1=B1=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-tomcat-profiler/trunk@833 84d0f5b1-2673-498c-a247-62c4ff18d310 --- src/main/java/com/profiler/Agent.java | 207 +++++++++--------- .../java/com/profiler/util/NetworkUtils.java | 41 ++++ .../com/profiler/util/HeaderUtilTest.java | 28 --- .../com/profiler/util/NetworkUtilsTest.java | 18 ++ 4 files changed, 159 insertions(+), 135 deletions(-) create mode 100644 src/main/java/com/profiler/util/NetworkUtils.java delete mode 100644 src/test/java/com/profiler/util/HeaderUtilTest.java create mode 100644 src/test/java/com/profiler/util/NetworkUtilsTest.java 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); + } +}