Skip to content

Commit 544f4d5

Browse files
authored
[MONIT-32096] Better Local Hostname detection (#817)
1 parent b197643 commit 544f4d5

4 files changed

Lines changed: 57 additions & 29 deletions

File tree

proxy/src/main/java/com/wavefront/agent/AbstractAgent.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private void postProcessConfig() {
202202
// setLevel(org.apache.log4j.Level.WARN);
203203
// Logger.getLogger("org.apache.http.impl.execchain.RetryExec").setLevel(Level.WARNING);
204204

205-
if (StringUtils.isBlank(proxyConfig.getHostname().trim())) {
205+
if (StringUtils.isBlank(proxyConfig.getHostname())) {
206206
throw new IllegalArgumentException(
207207
"hostname cannot be blank! Please correct your configuration settings.");
208208
}
@@ -223,24 +223,14 @@ private void postProcessConfig() {
223223
@VisibleForTesting
224224
void parseArguments(String[] args) {
225225
// read build information and print version.
226-
String versionStr =
227-
"Wavefront Proxy version "
228-
+ getBuildVersion()
229-
+ " (pkg:"
230-
+ getPackage()
231-
+ ")"
232-
+ ", runtime: "
233-
+ getJavaVersion();
234226
try {
235227
if (!proxyConfig.parseArguments(args, this.getClass().getCanonicalName())) {
236228
System.exit(0);
237229
}
238230
} catch (ParameterException e) {
239-
logger.info(versionStr);
240231
logger.severe("Parameter exception: " + e.getMessage());
241232
System.exit(1);
242233
}
243-
logger.info(versionStr);
244234
logger.info(
245235
"Arguments: "
246236
+ IntStream.range(0, args.length)

proxy/src/main/java/com/wavefront/agent/ProxyConfig.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,13 @@ public class ProxyConfig extends Configuration {
795795
@Parameter(
796796
names = {"--hostname"},
797797
description = "Hostname for the proxy. Defaults to FQDN of machine.")
798-
String hostname = getLocalHostName();
798+
String hostname = null;
799799

800800
/** This property holds the proxy name. Default proxyname to FQDN of machine. */
801801
@Parameter(
802802
names = {"--proxyname"},
803803
description = "Name for the proxy. Defaults to hostname.")
804-
String proxyname = getLocalHostName();
804+
String proxyname = null;
805805

806806
@Parameter(
807807
names = {"--idFile"},
@@ -2287,15 +2287,21 @@ public void verifyAndInit() {
22872287
token = ObjectUtils.firstNonNull(config.getRawProperty("token", token), "undefined").trim();
22882288
server = config.getString("server", server);
22892289

2290-
String FQDN = getLocalHostName();
2291-
hostname = config.getString("hostname", hostname);
2292-
proxyname = config.getString("proxyname", proxyname);
2293-
if (!hostname.equals(FQDN)) {
2290+
String _hostname = config.getString("hostname", hostname);
2291+
if (!Strings.isNullOrEmpty(_hostname)) {
22942292
logger.warning(
22952293
"Deprecated field hostname specified in config setting. Please use "
22962294
+ "proxyname config field to set proxy name.");
2297-
if (proxyname.equals(FQDN)) proxyname = hostname;
2295+
hostname = _hostname;
2296+
} else {
2297+
hostname = getLocalHostName();
2298+
}
2299+
2300+
proxyname = config.getString("proxyname", proxyname);
2301+
if (Strings.isNullOrEmpty(proxyname)) {
2302+
proxyname = hostname;
22982303
}
2304+
22992305
logger.info("Using proxyname:'" + proxyname + "' hostname:'" + hostname + "'");
23002306

23012307
idFile = config.getString("idFile", idFile);
@@ -2803,7 +2809,6 @@ limited system resources (4 CPU cores or less, heap size less than 4GB) to preve
28032809
* @throws ParameterException if configuration parsing failed
28042810
*/
28052811
public boolean parseArguments(String[] args, String programName) throws ParameterException {
2806-
String versionStr = "Wavefront Proxy version " + getBuildVersion();
28072812
JCommander jCommander =
28082813
JCommander.newBuilder()
28092814
.programName(programName)
@@ -2812,11 +2817,9 @@ public boolean parseArguments(String[] args, String programName) throws Paramete
28122817
.build();
28132818
jCommander.parse(args);
28142819
if (this.isVersion()) {
2815-
System.out.println(versionStr);
28162820
return false;
28172821
}
28182822
if (this.isHelp()) {
2819-
System.out.println(versionStr);
28202823
jCommander.usage();
28212824
return false;
28222825
}

proxy/src/main/java/com/wavefront/agent/PushAgent.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import static com.wavefront.agent.data.EntityProperties.NO_RATE_LIMIT;
77
import static com.wavefront.agent.handlers.ReportableEntityHandlerFactoryImpl.VALID_HISTOGRAMS_LOGGER;
88
import static com.wavefront.agent.handlers.ReportableEntityHandlerFactoryImpl.VALID_POINTS_LOGGER;
9-
import static com.wavefront.common.Utils.csvToList;
10-
import static com.wavefront.common.Utils.lazySupplier;
9+
import static com.wavefront.common.Utils.*;
1110

1211
import com.google.common.annotations.VisibleForTesting;
1312
import com.google.common.base.Preconditions;
@@ -209,6 +208,15 @@ public class PushAgent extends AbstractAgent {
209208

210209
public static void main(String[] args) {
211210
// Start the ssh daemon
211+
String versionStr =
212+
"Wavefront Proxy version "
213+
+ getBuildVersion()
214+
+ " (pkg:"
215+
+ getPackage()
216+
+ ")"
217+
+ ", runtime: "
218+
+ getJavaVersion();
219+
logger.info(versionStr);
212220
new PushAgent().start(args);
213221
}
214222

proxy/src/main/java/com/wavefront/common/Utils.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import com.google.common.base.Splitter;
77
import com.google.common.collect.ImmutableList;
8+
import java.io.BufferedReader;
9+
import java.io.IOException;
10+
import java.io.InputStreamReader;
811
import java.net.Inet4Address;
912
import java.net.InetAddress;
1013
import java.net.NetworkInterface;
1114
import java.net.SocketException;
12-
import java.util.Collections;
13-
import java.util.Enumeration;
14-
import java.util.List;
15-
import java.util.MissingResourceException;
16-
import java.util.ResourceBundle;
15+
import java.util.*;
1716
import java.util.function.Supplier;
17+
import java.util.logging.Logger;
1818
import javax.annotation.Nonnull;
1919
import javax.annotation.Nullable;
2020
import javax.ws.rs.core.Response;
@@ -31,6 +31,8 @@ public abstract class Utils {
3131
private static final ResourceBundle buildProps = ResourceBundle.getBundle("build");
3232
private static final List<Integer> UUID_SEGMENTS = ImmutableList.of(8, 4, 4, 4, 12);
3333

34+
private static final Logger log = Logger.getLogger(Utils.class.getCanonicalName());
35+
3436
/**
3537
* A lazy initialization wrapper for {@code Supplier}
3638
*
@@ -155,6 +157,27 @@ public static String getJavaVersion() {
155157
* @return name for localhost
156158
*/
157159
public static String getLocalHostName() {
160+
for (String env : Arrays.asList("COMPUTERNAME", "HOSTNAME", "PROXY_HOSTNAME")) {
161+
String hostname = System.getenv(env);
162+
if (StringUtils.isNotBlank(hostname)) {
163+
log.info("Hostname: '" + hostname + "' (detected using '" + env + "' env variable)");
164+
return hostname;
165+
}
166+
}
167+
168+
try {
169+
String hostname =
170+
new BufferedReader(
171+
new InputStreamReader(Runtime.getRuntime().exec("hostname").getInputStream()))
172+
.readLine();
173+
if (StringUtils.isNotBlank(hostname)) {
174+
log.info("Hostname: '" + hostname + "' (detected using 'hostname' command)");
175+
return hostname;
176+
}
177+
} catch (IOException e) {
178+
log.fine("Error running 'hostname' command. " + e.getMessage());
179+
}
180+
158181
InetAddress localAddress = null;
159182
try {
160183
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
@@ -184,8 +207,12 @@ public static String getLocalHostName() {
184207
// ignore
185208
}
186209
if (localAddress != null) {
187-
return localAddress.getCanonicalHostName();
210+
String hostname = localAddress.getCanonicalHostName();
211+
log.info("Hostname: '" + hostname + "' (detected using network interfaces)");
212+
return hostname;
188213
}
214+
215+
log.info("Hostname not detected, using 'localhost')");
189216
return "localhost";
190217
}
191218

0 commit comments

Comments
 (0)