1919import java .io .InputStream ;
2020import java .nio .file .Files ;
2121import java .nio .file .Paths ;
22+ import java .util .ArrayList ;
2223import java .util .Arrays ;
2324import java .util .Collections ;
2425import java .util .List ;
3435public class JmxScraper {
3536 private static final Logger logger = Logger .getLogger (JmxScraper .class .getName ());
3637 private static final String CONFIG_ARG = "-config" ;
38+ private static final String TEST_ARG = "-test" ;
3739
3840 private final JmxConnectorBuilder client ;
3941 private final JmxMetricInsight service ;
@@ -52,8 +54,11 @@ public static void main(String[] args) {
5254 // set log format
5355 System .setProperty ("java.util.logging.SimpleFormatter.format" , "%1$tF %1$tT %4$s %5$s%n" );
5456
57+ List <String > effectiveArgs = new ArrayList <>(Arrays .asList (args ));
58+ boolean testMode = effectiveArgs .remove (TEST_ARG );
59+
5560 try {
56- Properties argsConfig = parseArgs ( Arrays . asList ( args ) );
61+ Properties argsConfig = parseArgsConfig ( effectiveArgs );
5762 propagateToSystemProperties (argsConfig );
5863
5964 // auto-configure and register SDK
@@ -78,16 +83,21 @@ public static void main(String[] args) {
7883 Optional .ofNullable (scraperConfig .getUsername ()).ifPresent (connectorBuilder ::withUser );
7984 Optional .ofNullable (scraperConfig .getPassword ()).ifPresent (connectorBuilder ::withPassword );
8085
81- JmxScraper jmxScraper = new JmxScraper (connectorBuilder , service , scraperConfig );
82- jmxScraper .start ();
86+ if (testMode ) {
87+ System .exit (testConnection (connectorBuilder ) ? 0 : 1 );
88+ } else {
89+ JmxScraper jmxScraper = new JmxScraper (connectorBuilder , service , scraperConfig );
90+ jmxScraper .start ();
91+ }
8392 } catch (ConfigurationException e ) {
8493 logger .log (Level .SEVERE , "invalid configuration " , e );
8594 System .exit (1 );
8695 } catch (InvalidArgumentException e ) {
8796 logger .log (Level .SEVERE , "invalid configuration provided through arguments" , e );
8897 logger .info (
89- "Usage: java -jar <path_to_jmxscraper.jar> "
90- + "-config <path_to_config.properties or - for stdin>" );
98+ "Usage: java -jar <path_to_jmxscraper.jar> [-test] [-config <conf>]" );
99+ logger .info (" -test test JMX connection with provided configuration and exit" );
100+ logger .info (" -config <conf> provide configuration, where <conf> is - for stdin, or <path_to_config.properties>" );
91101 System .exit (1 );
92102 } catch (IOException e ) {
93103 logger .log (Level .SEVERE , "Unable to connect " , e );
@@ -98,6 +108,24 @@ public static void main(String[] args) {
98108 }
99109 }
100110
111+ private static boolean testConnection (JmxConnectorBuilder connectorBuilder ) {
112+ try (JMXConnector connector = connectorBuilder .build ()) {
113+
114+ MBeanServerConnection connection = connector .getMBeanServerConnection ();
115+ Integer mbeanCount = connection .getMBeanCount ();
116+ if (mbeanCount > 0 ) {
117+ logger .log (Level .INFO , "JMX connection test OK" );
118+ return true ;
119+ } else {
120+ logger .log (Level .SEVERE , "JMX connection test ERROR" );
121+ return false ;
122+ }
123+ } catch (IOException e ) {
124+ logger .log (Level .SEVERE , "JMX connection test ERROR" , e );
125+ return false ;
126+ }
127+ }
128+
101129 // package private for testing
102130 static void propagateToSystemProperties (Properties properties ) {
103131 for (Map .Entry <Object , Object > entry : properties .entrySet ()) {
@@ -116,7 +144,7 @@ static void propagateToSystemProperties(Properties properties) {
116144 *
117145 * @param args application commandline arguments
118146 */
119- static Properties parseArgs (List <String > args ) throws InvalidArgumentException {
147+ static Properties parseArgsConfig (List <String > args ) throws InvalidArgumentException {
120148
121149 if (args .isEmpty ()) {
122150 // empty properties from stdin or external file
0 commit comments