diff --git a/README.md b/README.md index 44b9ac5a..e064d95e 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ username | The username to be used in remote JMX password authentication. password | The password to be used in remote JMX password authentication. jmxUrl | A full JMX URL to connect to. Should not be specified if hostPort is. ssl | Whether JMX connection should be done over SSL. To configure certificates you have to set following system properties:
`-Djavax.net.ssl.keyStore=/home/user/.keystore`
`-Djavax.net.ssl.keyStorePassword=changeit`
`-Djavax.net.ssl.trustStore=/home/user/.truststore`
`-Djavax.net.ssl.trustStorePassword=changeit` +scrapeAllAttributes | Option to scrape everything from JMX, otherwise only Attribute instances will be scraped, default: false lowercaseOutputName | Lowercase the output metric name. Applies to default format and `name`. Defaults to false. lowercaseOutputLabelNames | Lowercase the output metric label names. Applies to default format and `labels`. Defaults to false. whitelistObjectNames | A list of [ObjectNames](http://docs.oracle.com/javase/6/docs/api/javax/management/ObjectName.html) to query. Defaults to all mBeans. diff --git a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java index fb799dc8..423e4b0e 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxCollector.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxCollector.java @@ -65,6 +65,7 @@ private static class Config { String username = ""; String password = ""; boolean ssl = false; + boolean scrapeAllAttributes = false; boolean lowercaseOutputName; boolean lowercaseOutputLabelNames; List whitelistObjectNames = new ArrayList(); @@ -183,6 +184,10 @@ private Config loadConfig(Map yamlConfig) throws MalformedObject cfg.ssl = (Boolean)yamlConfig.get("ssl"); } + if (yamlConfig.containsKey("scrapeAllAttributes")) { + cfg.scrapeAllAttributes = (Boolean)yamlConfig.get("scrapeAllAttributes"); + } + if (yamlConfig.containsKey("lowercaseOutputName")) { cfg.lowercaseOutputName = (Boolean)yamlConfig.get("lowercaseOutputName"); } @@ -587,7 +592,7 @@ public List collect() { MatchedRulesCache.StalenessTracker stalenessTracker = new MatchedRulesCache.StalenessTracker(); Receiver receiver = new Receiver(config, stalenessTracker); - JmxScraper scraper = new JmxScraper(config.jmxUrl, config.username, config.password, config.ssl, + JmxScraper scraper = new JmxScraper(config.jmxUrl, config.username, config.password, config.ssl, config.scrapeAllAttributes config.whitelistObjectNames, config.blacklistObjectNames, receiver, jmxMBeanPropertyCache); long start = System.nanoTime(); double error = 0; diff --git a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java index da8b34a6..db8b8e87 100644 --- a/collector/src/main/java/io/prometheus/jmx/JmxScraper.java +++ b/collector/src/main/java/io/prometheus/jmx/JmxScraper.java @@ -52,11 +52,12 @@ void recordBean( private final String username; private final String password; private final boolean ssl; + private final boolean scrapeAllAttributes; private final List whitelistObjectNames, blacklistObjectNames; private final JmxMBeanPropertyCache jmxMBeanPropertyCache; private final OptionalValueExtractor optionalValueExtractor = new OptionalValueExtractor(); - public JmxScraper(String jmxUrl, String username, String password, boolean ssl, + public JmxScraper(String jmxUrl, String username, String password, boolean ssl, boolean scrapeAllAttributes, List whitelistObjectNames, List blacklistObjectNames, MBeanReceiver receiver, JmxMBeanPropertyCache jmxMBeanPropertyCache) { this.jmxUrl = jmxUrl; @@ -64,6 +65,7 @@ public JmxScraper(String jmxUrl, String username, String password, boolean ssl, this.username = username; this.password = password; this.ssl = ssl; + this.scrapeAllAttributes = scrapeAllAttributes; this.whitelistObjectNames = whitelistObjectNames; this.blacklistObjectNames = blacklistObjectNames; this.jmxMBeanPropertyCache = jmxMBeanPropertyCache; @@ -160,21 +162,26 @@ private void scrapeBean(MBeanServerConnection beanConn, ObjectName mbeanName) { processAttributesOneByOne(beanConn, mbeanName, name2AttrInfo); return; } - for (Object attributeObj : attributes.asList()) { - if (Attribute.class.isInstance(attributeObj)) { - Attribute attribute = (Attribute)(attributeObj); - MBeanAttributeInfo attr = name2AttrInfo.get(attribute.getName()); - logScrape(mbeanName, attr, "process"); - processBeanValue( - mbeanName.getDomain(), - jmxMBeanPropertyCache.getKeyPropertyList(mbeanName), - new LinkedList(), - attr.getName(), - attr.getType(), - attr.getDescription(), - attribute.getValue() - ); + + List attrList = attributes.asList(); + for (int i = 0; i < attrList.size(); i++) { + Object value = attrList.get(i); + boolean isMxAttribute = value instanceof Attribute; + if (isMxAttribute) { + value = ((Attribute) (value)).getValue(); + } + if (!isMxAttribute && !scrapeAllAttributes) { + return; } + MBeanAttributeInfo attr = attrInfos[i]; + logScrape(mbeanName, attr, "process"); + processBeanValue(mbeanName.getDomain(), + jmxMBeanPropertyCache.getKeyPropertyList(mbeanName), + new LinkedList(), + attr.getName(), + attr.getType(), + attr.getDescription(), + value); } } @@ -358,15 +365,15 @@ public static void main(String[] args) throws Exception { List objectNames = new LinkedList(); objectNames.add(null); if (args.length >= 3){ - new JmxScraper(args[0], args[1], args[2], (args.length >3 && "ssl".equalsIgnoreCase(args[3])), objectNames, new LinkedList(), + new JmxScraper(args[0], args[1], args[2], (args.length >3 && "ssl".equalsIgnoreCase(args[3])), false, objectNames, new LinkedList(), new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); } else if (args.length > 0){ - new JmxScraper(args[0], "", "", false, objectNames, new LinkedList(), + new JmxScraper(args[0], "", "", false, false, objectNames, new LinkedList(), new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); } else { - new JmxScraper("", "", "", false, objectNames, new LinkedList(), + new JmxScraper("", "", "", false, false, objectNames, new LinkedList(), new StdoutWriter(), new JmxMBeanPropertyCache()).doScrape(); } }