|
16 | 16 |
|
17 | 17 | package com.navercorp.pinpoint.tools;
|
18 | 18 |
|
19 |
| -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig; |
20 |
| -import com.navercorp.pinpoint.bootstrap.config.ProfilerConfigLoader; |
21 |
| -import com.navercorp.pinpoint.bootstrap.config.Profiles; |
22 |
| -import com.navercorp.pinpoint.common.util.PropertyUtils; |
23 |
| -import com.navercorp.pinpoint.thrift.io.HeaderTBaseSerializer; |
24 |
| -import com.navercorp.pinpoint.thrift.io.HeaderTBaseSerializerFactory; |
25 |
| -import com.navercorp.pinpoint.thrift.io.NetworkAvailabilityCheckPacket; |
26 | 19 | import com.navercorp.pinpoint.tools.network.NetworkChecker;
|
27 | 20 | import com.navercorp.pinpoint.tools.network.TCPChecker;
|
28 |
| -import com.navercorp.pinpoint.tools.network.UDPChecker; |
29 | 21 | import com.navercorp.pinpoint.tools.network.grpc.GrpcTransportConfig;
|
30 |
| -import com.navercorp.pinpoint.tools.network.thrift.ThriftTransportConfig; |
31 |
| -import org.apache.thrift.TException; |
| 22 | +import com.navercorp.pinpoint.tools.utils.Logger; |
32 | 23 |
|
33 |
| -import java.io.File; |
34 | 24 | import java.io.IOException;
|
| 25 | +import java.io.InputStream; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
35 | 28 | import java.nio.file.Paths;
|
36 |
| -import java.util.Arrays; |
37 | 29 | import java.util.Properties;
|
38 | 30 |
|
39 | 31 | /**
|
40 | 32 | * @author netspider
|
41 | 33 | */
|
42 | 34 | public class NetworkAvailabilityChecker {
|
43 | 35 |
|
44 |
| - private static final String SEPARATOR = File.separator; |
| 36 | + public static final String ACTIVE_PROFILE_KEY = "pinpoint.profiler.profiles.active"; |
| 37 | + public static final String CONFIG_FILE_NAME = "pinpoint-root.config"; |
| 38 | + public static final String PROFILE_CONFIG_FILE_NAME = "pinpoint.config"; |
| 39 | + |
| 40 | + private static final Logger logger = new Logger(); |
45 | 41 |
|
46 | 42 | public static void main(String[] args) {
|
47 | 43 | if (args.length != 1) {
|
48 |
| - System.out.println("usage : " + NetworkAvailabilityChecker.class.getSimpleName() + " AGENT_CONFIG_FILE"); |
| 44 | + logger.info("usage : " + NetworkAvailabilityChecker.class.getSimpleName() + " AGENT_CONFIG_FILE"); |
49 | 45 | return;
|
50 | 46 | }
|
51 | 47 |
|
52 | 48 | String configPath = args[0];
|
| 49 | + Path filePath = Paths.get(configPath); |
53 | 50 |
|
54 | 51 | final Properties defaultProperties = new Properties();
|
55 |
| - loadFileProperties(defaultProperties, configPath); |
| 52 | + loadFileProperties(defaultProperties, filePath); |
56 | 53 |
|
57 |
| - File file = new File(configPath); |
58 |
| - String path = file.getAbsoluteFile().getParent(); |
| 54 | + Path path = filePath.toAbsolutePath().getParent(); |
59 | 55 |
|
60 |
| - if (configPath.contains(Profiles.CONFIG_FILE_NAME)) { |
| 56 | + if (configPath.contains(CONFIG_FILE_NAME)) { |
61 | 57 | // 2. load profile
|
62 | 58 | final String activeProfile = getActiveProfile(defaultProperties);
|
63 |
| - System.out.println("Active profile : " + activeProfile); |
| 59 | + logger.info("Active profile : " + activeProfile); |
64 | 60 |
|
65 | 61 | if (activeProfile == null) {
|
66 |
| - System.out.println("Could not find activeProfile : null"); |
| 62 | + logger.info("Could not find activeProfile : null"); |
67 | 63 | return;
|
68 | 64 | }
|
69 | 65 |
|
70 |
| - final File activeProfileConfigFile = new File(path, "profiles" + SEPARATOR + activeProfile + SEPARATOR + Profiles.PROFILE_CONFIG_FILE_NAME); |
71 |
| - loadFileProperties(defaultProperties, activeProfileConfigFile.getAbsolutePath()); |
72 |
| - } |
73 |
| - |
74 |
| - final ProfilerConfig profilerConfig = ProfilerConfigLoader.load(defaultProperties); |
75 |
| - if (profilerConfig.getTransportModule().toString().equals("GRPC")) { |
76 |
| - |
77 |
| - System.out.println("Transport Module set to GRPC"); |
78 |
| - |
79 |
| - GrpcTransportConfig grpcTransportConfig = new GrpcTransportConfig(); |
80 |
| - grpcTransportConfig.read(defaultProperties); |
| 66 | + final Path activeProfileConfigFile = Paths.get(path.toString(), "profiles", activeProfile, PROFILE_CONFIG_FILE_NAME); |
81 | 67 |
|
82 |
| - try { |
83 |
| - checkGRPCBase(grpcTransportConfig); |
84 |
| - } catch (Exception e) { |
85 |
| - e.printStackTrace(); |
86 |
| - } |
| 68 | + loadFileProperties(defaultProperties, activeProfileConfigFile.toAbsolutePath()); |
| 69 | + } |
87 | 70 |
|
88 |
| - try { |
89 |
| - checkGRPCMeta(grpcTransportConfig); |
90 |
| - } catch (Exception e) { |
91 |
| - e.printStackTrace(); |
92 |
| - } |
| 71 | + logger.info("Transport Module set to GRPC"); |
93 | 72 |
|
94 |
| - try { |
95 |
| - checkGRPCStat(grpcTransportConfig); |
96 |
| - } catch (Exception e) { |
97 |
| - e.printStackTrace(); |
98 |
| - } |
| 73 | + GrpcTransportConfig grpcTransportConfig = new GrpcTransportConfig(defaultProperties); |
99 | 74 |
|
100 |
| - try { |
101 |
| - checkGRPCSpan(grpcTransportConfig); |
102 |
| - } catch (Exception e) { |
103 |
| - e.printStackTrace(); |
104 |
| - } |
105 |
| - |
106 |
| - } else { |
| 75 | + try { |
| 76 | + checkGRPCBase(grpcTransportConfig); |
| 77 | + } catch (Exception e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
107 | 80 |
|
108 |
| - System.out.println("Transport Module set to THRIFT"); |
109 |
| - ThriftTransportConfig thriftTransportConfig = new ThriftTransportConfig(); |
110 |
| - thriftTransportConfig.read(defaultProperties); |
111 |
| - try { |
112 |
| - checkUDPStat(thriftTransportConfig); |
113 |
| - } catch (Exception e) { |
114 |
| - e.printStackTrace(); |
115 |
| - } |
| 81 | + try { |
| 82 | + checkGRPCMeta(grpcTransportConfig); |
| 83 | + } catch (Exception e) { |
| 84 | + e.printStackTrace(); |
| 85 | + } |
116 | 86 |
|
117 |
| - try { |
118 |
| - checkUDPSpan(thriftTransportConfig); |
119 |
| - } catch (Exception e) { |
120 |
| - e.printStackTrace(); |
121 |
| - } |
| 87 | + try { |
| 88 | + checkGRPCStat(grpcTransportConfig); |
| 89 | + } catch (Exception e) { |
| 90 | + e.printStackTrace(); |
| 91 | + } |
122 | 92 |
|
123 |
| - try { |
124 |
| - checkTCP(thriftTransportConfig); |
125 |
| - } catch (Exception e) { |
126 |
| - e.printStackTrace(); |
127 |
| - } |
| 93 | + try { |
| 94 | + checkGRPCSpan(grpcTransportConfig); |
| 95 | + } catch (Exception e) { |
| 96 | + e.printStackTrace(); |
128 | 97 | }
|
| 98 | + |
129 | 99 | }
|
130 | 100 |
|
131 | 101 | private static String getActiveProfile(Properties defaultProperties) {
|
132 |
| - return defaultProperties.getProperty(Profiles.ACTIVE_PROFILE_KEY, "release"); |
| 102 | + return defaultProperties.getProperty(ACTIVE_PROFILE_KEY, "release"); |
133 | 103 | }
|
134 | 104 |
|
135 |
| - private static void loadFileProperties(Properties properties, String filePath) { |
| 105 | + private static void loadFileProperties(Properties properties, Path filePath) { |
136 | 106 | try {
|
137 |
| - PropertyUtils.loadProperty(properties, Paths.get(filePath)); |
| 107 | + InputStream inputStream = Files.newInputStream(filePath); |
| 108 | + properties.load(inputStream); |
138 | 109 | } catch (IOException e) {
|
139 | 110 | throw new IllegalStateException(String.format("%s load fail Caused by:%s", filePath, e.getMessage()), e);
|
140 | 111 | }
|
@@ -172,38 +143,4 @@ private static void checkGRPCSpan(GrpcTransportConfig grpcTransportConfig) throw
|
172 | 143 | checker.check();
|
173 | 144 | }
|
174 | 145 |
|
175 |
| - private static void checkUDPStat(ThriftTransportConfig profilerConfig) throws Exception { |
176 |
| - String ip = profilerConfig.getCollectorStatServerIp(); |
177 |
| - int port = profilerConfig.getCollectorStatServerPort(); |
178 |
| - |
179 |
| - NetworkChecker checker = new UDPChecker("UDP-STAT", ip, port); |
180 |
| - checker.check(getNetworkCheckPayload(), getNetworkCheckResponsePayload()); |
181 |
| - } |
182 |
| - |
183 |
| - |
184 |
| - private static void checkUDPSpan(ThriftTransportConfig profilerConfig) throws Exception { |
185 |
| - String ip = profilerConfig.getCollectorSpanServerIp(); |
186 |
| - int port = profilerConfig.getCollectorSpanServerPort(); |
187 |
| - |
188 |
| - NetworkChecker checker = new UDPChecker("UDP-SPAN", ip, port); |
189 |
| - checker.check(getNetworkCheckPayload(), getNetworkCheckResponsePayload()); |
190 |
| - } |
191 |
| - |
192 |
| - private static void checkTCP(ThriftTransportConfig profilerConfig) throws Exception { |
193 |
| - String ip = profilerConfig.getCollectorTcpServerIp(); |
194 |
| - int port = profilerConfig.getCollectorTcpServerPort(); |
195 |
| - |
196 |
| - NetworkChecker checker = new TCPChecker("TCP", ip, port); |
197 |
| - checker.check(); |
198 |
| - } |
199 |
| - |
200 |
| - private static byte[] getNetworkCheckPayload() throws TException { |
201 |
| - HeaderTBaseSerializer serializer = new HeaderTBaseSerializerFactory(65535).createSerializer(); |
202 |
| - return serializer.serialize(new NetworkAvailabilityCheckPacket()); |
203 |
| - } |
204 |
| - |
205 |
| - private static byte[] getNetworkCheckResponsePayload() { |
206 |
| - return Arrays.copyOf(NetworkAvailabilityCheckPacket.DATA_OK, NetworkAvailabilityCheckPacket.DATA_OK.length); |
207 |
| - } |
208 |
| - |
209 | 146 | }
|
0 commit comments