diff --git a/pom.xml b/pom.xml index 324bcc088..35f64757f 100644 --- a/pom.xml +++ b/pom.xml @@ -164,6 +164,12 @@ git@github.com:Datadog/jmxfetch.git + + + src/main/resources + true + + org.apache.maven.plugins diff --git a/src/main/java/org/datadog/jmxfetch/App.java b/src/main/java/org/datadog/jmxfetch/App.java index 9f657a348..84aaf8faa 100644 --- a/src/main/java/org/datadog/jmxfetch/App.java +++ b/src/main/java/org/datadog/jmxfetch/App.java @@ -37,6 +37,7 @@ import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.Properties; import java.util.Map.Entry; import java.util.concurrent.CancellationException; import java.util.concurrent.ConcurrentHashMap; @@ -118,8 +119,14 @@ public static void main(String[] args) { System.exit(1); } + // Display the version and quit + if (config.isVersion() || AppConfig.ACTION_HELP.equals(config.getAction())) { + JCommander.getConsole().println("JMX Fetch "+getVersion()); + System.exit(0); + } + // Display the help and quit - if (config.isHelp() || config.getAction().equals(AppConfig.ACTION_HELP)) { + if (config.isHelp() || AppConfig.ACTION_HELP.equals(config.getAction())) { commander.usage(); System.exit(0); } @@ -127,6 +134,18 @@ public static void main(String[] args) { System.exit(run(config)); } + public static String getVersion() { + try { + final Properties properties = new Properties(); + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + properties.load(classLoader.getResourceAsStream("project.properties")); + return properties.getProperty("version"); + } catch (IOException e) { + e.printStackTrace(); + return "?.?.?"; + } + } + /** * Main entry point of JMXFetch that returns integer on exit instead of calling {@code * System#exit}. @@ -164,7 +183,7 @@ public static int run(AppConfig config) { // Set up the shutdown hook to properly close resources attachShutdownHook(); - LOGGER.info("JMX Fetch has started"); + LOGGER.info("JMX Fetch "+getVersion()+" has started"); // set up the config status config.updateStatus(); diff --git a/src/main/java/org/datadog/jmxfetch/AppConfig.java b/src/main/java/org/datadog/jmxfetch/AppConfig.java index 13d156125..eba222818 100644 --- a/src/main/java/org/datadog/jmxfetch/AppConfig.java +++ b/src/main/java/org/datadog/jmxfetch/AppConfig.java @@ -30,6 +30,7 @@ public class AppConfig { public static final String ACTION_LIST_NOT_MATCHING = "list_not_matching_attributes"; public static final String ACTION_LIST_LIMITED = "list_limited_attributes"; public static final String ACTION_HELP = "help"; + public static final String ACTION_VERSION = "version"; public static final HashSet ACTIONS = new HashSet( Arrays.asList( @@ -40,6 +41,7 @@ public class AppConfig { ACTION_LIST_NOT_MATCHING, ACTION_LIST_LIMITED, ACTION_HELP, + ACTION_VERSION, ACTION_LIST_JVMS)); private static final String AD_WIN_PIPE_PATH = "\\\\.\\pipe\\"; @@ -57,6 +59,12 @@ public class AppConfig { help = true) private boolean help; + @Parameter( + names = {"--version", "-v"}, + description = "Display the version number and exit", + help = true) + private boolean version; + @Parameter( names = {"--log_level", "-L"}, description = "Level of verbosity", @@ -217,6 +225,9 @@ public String getStatusLocation() { } public String getAction() { + if (this.action == null || this.action.isEmpty()) { + return null; + } return this.action.get(0); } @@ -228,6 +239,10 @@ public boolean isHelp() { return help; } + public boolean isVersion() { + return version; + } + public Status getStatus() { return status; } diff --git a/src/main/resources/project.properties b/src/main/resources/project.properties new file mode 100644 index 000000000..defbd4820 --- /dev/null +++ b/src/main/resources/project.properties @@ -0,0 +1 @@ +version=${project.version}