pax_global_header00006660000000000000000000000064123051305260014507gustar00rootroot0000000000000052 comment=417ef73d4dea2c11a7f022df0995af68997a0dcf jmxetric-1.0.6/000077500000000000000000000000001230513052600133405ustar00rootroot00000000000000jmxetric-1.0.6/.classpath000066400000000000000000000032331230513052600153240ustar00rootroot00000000000000 jmxetric-1.0.6/.gitignore000066400000000000000000000000221230513052600153220ustar00rootroot00000000000000target /.settings jmxetric-1.0.6/.project000066400000000000000000000013051230513052600150060ustar00rootroot00000000000000 jmxetric org.eclipse.jdt.core.javabuilder org.maven.ide.eclipse.maven2Builder org.eclipse.m2e.core.maven2Builder org.eclipse.m2e.core.maven2Nature org.maven.ide.eclipse.maven2Nature org.eclipse.jdt.core.javanature jmxetric-1.0.6/.travis.yml000066400000000000000000000000601230513052600154450ustar00rootroot00000000000000language: java jdk: - oraclejdk7 - openjdk6 jmxetric-1.0.6/CHANGES000066400000000000000000000024521230513052600143360ustar00rootroot00000000000000Changes in version 1.0.0 * Tweak dependencies to include gmetric4j (not yet in maven central) * Update docs to refer to new home on github * rename package from jmxetric -> info.ganglia.jmxetric Changes in version 0.0.5 #23 Publishing JMXetric jar file to a public Maven repo Changes in version 0.0.4 #10 Attributes with composite values are not published correctly #11 If an attribute value is null, an exception is thrown as MBeanSampler attempts to publish it. Changes in version 0.0.3 Please note that the config file format has changed to support mbeans with a dot in the name. See the README or example file. * Built using Maven * #2 Compiled to target JVM 1.5 for legacy support * #3 NPE if element has no "pname" attribute specified * #4 Attributes with embedded "."s improperly parsed * #5 Uninitialised mbeans cause exception on startup * #7 Incorrect javaagent option sample in README * #6 Slope is not defined in the config file so cannot use ganglia/rrd counter attributes * Suppress exception (keep warning) in the event the mbean is slow to initialise on startup, also add initial delay parameter to config file Changes in version 0.0.2 * Added support for the ganglia 3.1.* wire format. * Added support for UDP unicast. Thanks to Sam Tunnicliffe and Malcolm Landon for the patch. jmxetric-1.0.6/COPYING000066400000000000000000000020501230513052600143700ustar00rootroot00000000000000Copyright (c) 2008-2011 Jasper Humphrey Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. jmxetric-1.0.6/README000066400000000000000000000146121230513052600142240ustar00rootroot00000000000000Name jmxetric - jvm instrumentation to ganglia Version jmxetric 1.0.4 The latest version of this software and document will always be found at https://github.com/ganglia/jmxetric Synopsis JMXetric is a 100% java, configurable JVM agent that periodically polls MBean attributes and reports their values to Ganglia. Project goals are that JMXetric should be * configurable (xml) * lightweight (small memory and cpu footprint) * standalone (not depend on third party libraries) The gmetric protocol implementation uses classes generated by the LGPL remotetea project (http://remotetea.sf.net). Installation Unzip the archive Add the following to your JVM java -javaagent:/jmxetric.jar=host="",port="",config="",process="" usual.java.main.class Demo / Quickstart 1) Ensure you have a gmond running on localhost:8649 2) Download jmxetric-1.0.4.jar, gmetric4j-1.0.3.jar and oncrpc-1.0.7.jar all into the same directory. 3) cd into the directory 2) In bash do: $ export config="host=localhost,port=8649,wireformat31x=true,config=etc/jmxetric.xml" $ java -Djava.util.logging.config.file=etc/logging.properties \ -javaagent:jmxetric-1.0.4.jar=$config jmxetric.JMXetricAgent Configuration The configuration of JMXetric is loaded from an xml file in the working directory of the shell, or specified as an argument to the JVM agent ("config"). The JVM agent arguments can also be used to specify the following: host, port The multicast address that is used to publish metrics to the ganglia gmond process config The full path to the config file (jmxetric.xml) mode The UDP addressing mode, either multicast or unicast (default multicast) wireformat31x True if the ganglia v3.1.x wire format should be used (default false) spoof An IP:hostname pair that will be used to spoof the metric host information. (default: no spoofing, i.e., local hostname reported by OS) process A name that is prefixed to the metric name before publication (so that metrics from different JVMs on the same host can be determined) XML Configuation File JMXetric schedules a number of "samples", that queries a list of "mbeans", that have "attributes". Element/Attribute Description mbean/name The name of the mbean to query mbean/pname The metric name for this mbean. This should always be used as ganglia/rrdtool misbehaves if the filename is "unusual". attribute/nam The name of the attribute. This can have two levels for the case of a composite key see below example for attribute "HeapMemoryUsage", key "used" attribute/type The type used for the metric in ganglia attribute/units The units used for the metric in ganglia attribute/pname The metric name for this attribute An example file: ]> Support/Queries I'm sure there are bugs and I'm sure there are some mbeans that can't be sampled currently. If you find some, then let me know jasper521 at gmail If you are using this at all, then I'd love to know! Copyright Copyright (C) 2008-2011 Jasper Humphrey Copyright (C) 2011-2013 Daniel Pocock http://danielpocock.com Authors Jasper Humphrey jasper521 at gmail Developer jmxetric-1.0.6/etc/000077500000000000000000000000001230513052600141135ustar00rootroot00000000000000jmxetric-1.0.6/etc/jmxetric.xml000066400000000000000000000057071230513052600164730ustar00rootroot00000000000000 ]> jmxetric-1.0.6/etc/logging.properties000066400000000000000000000011111230513052600176510ustar00rootroot00000000000000############################################################ # Default Logging Configuration File # # You can use a different file by specifying a filename # with the java.util.logging.config.file system property. # For example java -Djava.util.logging.config.file=myfile ############################################################ handlers= java.util.logging.ConsoleHandler .level=INFO java.util.logging.ConsoleHandler.level = ALL java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter info.ganglia.jmxetric.level=ALL info.ganglia.gmetric4j.level=ALL jmxetric-1.0.6/pom.xml000066400000000000000000000122051230513052600146550ustar00rootroot00000000000000 4.0.0 info.ganglia.jmxetric jmxetric JMXetric jar 1.0.6 JVM instrumentation to Ganglia http://github.com/ganglia/jmxetric The MIT License http://www.opensource.org/licenses/mit-license.php repo scm:git:git@github.com:ganglia/jmxetric.git scm:git:git@github.com:ganglia/jmxetric.git scm:git:git@github.com:ganglia/jmxetric.git humphrej Jasper Humphrey jasper521@googlemail.com pocock Daniel Pocock daniel@pocock.com.au 1.0.4 1.0.7 info.ganglia.gmetric4j gmetric4j ${gmetric4j.version} jar info.ganglia.gmetric4j gmetric4j ${gmetric4j.version} test-jar test junit junit 4.1 jar test org.acplt oncrpc ${oncrpc.version} org.sonatype.oss oss-parent 7 src/main/resources true org.apache.maven.plugins maven-compiler-plugin 2.5.1 1.5 1.5 org.apache.maven.plugins maven-jar-plugin 2.4 target/classes/META-INF/MANIFEST.MF org.apache.maven.plugins maven-source-plugin attach-sources jar org.apache.maven.plugins maven-javadoc-plugin attach-javadocs jar org.apache.maven.plugins maven-assembly-plugin 2.3 src/main/assembly/bin.xml org.codehaus.mojo findbugs-maven-plugin 2.5.2 org.apache.maven.plugins maven-javadoc-plugin release-sign-artifacts performRelease true org.apache.maven.plugins maven-gpg-plugin 1.4 sign-artifacts verify sign jmxetric-1.0.6/src/000077500000000000000000000000001230513052600141275ustar00rootroot00000000000000jmxetric-1.0.6/src/main/000077500000000000000000000000001230513052600150535ustar00rootroot00000000000000jmxetric-1.0.6/src/main/assembly/000077500000000000000000000000001230513052600166725ustar00rootroot00000000000000jmxetric-1.0.6/src/main/assembly/bin.xml000066400000000000000000000012151230513052600201630ustar00rootroot00000000000000 bin zip true false etc/* README* LICENSE* NOTICE* COPYING* CHANGES* jmxetric-1.0.6/src/main/java/000077500000000000000000000000001230513052600157745ustar00rootroot00000000000000jmxetric-1.0.6/src/main/java/info/000077500000000000000000000000001230513052600167275ustar00rootroot00000000000000jmxetric-1.0.6/src/main/java/info/ganglia/000077500000000000000000000000001230513052600203315ustar00rootroot00000000000000jmxetric-1.0.6/src/main/java/info/ganglia/jmxetric/000077500000000000000000000000001230513052600221565ustar00rootroot00000000000000jmxetric-1.0.6/src/main/java/info/ganglia/jmxetric/JMXetricAgent.java000066400000000000000000000037321230513052600254720ustar00rootroot00000000000000package info.ganglia.jmxetric; import info.ganglia.gmetric4j.GMonitor; import java.lang.instrument.Instrumentation; // import java.util.logging.Logger; /** * JMXetricAgent is a JVM agent that will sample MBean attributes on a periodic basis, * publishing the value of those attributes to the Ganglia gmond process. *
* Use:
* java -javaagent:path/jmxetric.jar=args yourmainclass *
* Example:
* java -javaagent:/opt/jmxetric_0_1/jmxetric.jar=host="localhost",port="8649",config=/opt/jmxetric_0_1/jmxetric.xml yourmainclass *
* Arguments can be:
* * * * * *
ArgumentDefaultDescription
hostHost address for ganglia
portPort for ganglia
configjmxetric.xmlConfig file path
*/ public class JMXetricAgent extends GMonitor { // private static Logger log = // Logger.getLogger(JMXetricAgent.class.getName()); /** * A log running, trivial main method for test purposes * premain method * @param args Not used */ public static void main(String[] args) throws Exception { while( true ) { Thread.sleep(1000*60*5); System.out.println("Test wakeup"); } } /** * The JVM agent entry point * @param agentArgs * @param inst */ public static void premain(String agentArgs, Instrumentation inst) { System.out.println(STARTUP_NOTICE) ; JMXetricAgent a = null ; try { a = new JMXetricAgent(); XMLConfigurationService.configure(a, agentArgs); a.start(); } catch ( Exception ex ) { // log.severe("Exception starting JMXetricAgent"); ex.printStackTrace(); } } private static final String STARTUP_NOTICE="JMXetricAgent instrumented JVM, see https://github.com/ganglia/jmxetric"; } jmxetric-1.0.6/src/main/java/info/ganglia/jmxetric/MBeanSampler.java000066400000000000000000000210571230513052600253340ustar00rootroot00000000000000package info.ganglia.jmxetric; import info.ganglia.gmetric4j.GSampler; import info.ganglia.gmetric4j.Publisher; import info.ganglia.gmetric4j.gmetric.GMetricSlope; import info.ganglia.gmetric4j.gmetric.GMetricType; import java.lang.management.ManagementFactory; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; // import java.util.logging.Level; // import java.util.logging.Logger; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.management.openmbean.CompositeData; /** * A class that samples MBeans and publishes attributes to Ganglia. This * classes' run method will be called periodically to sample the mbeans. */ public class MBeanSampler extends GSampler { //private static Logger log = // Logger.getLogger(JMXetricAgent.class.getName()); /* * The internal data structure is a hashmap of key=mbean name */ private Map mbeanMap = new HashMap(); private MBeanServer mbs = null; /** * Creates an MBeanSampler * @param delay the sample interval in seconds * @param process the process name that is appended to metrics */ public MBeanSampler(int initialDelay, int delay, String process ) { super(initialDelay, delay, process); } /** * Adds a mbean name/attribute pair to be sampled * @param mbean the name of the mbean * @param attribute the name of the attribute * @param composite the name of the composite * @param publishName the name to publish this attribute on * @throws java.lang.Exception */ public void addMBeanAttribute(String mbean, String attribute, String composite, GMetricType type, String units, GMetricSlope slope, String publishName ) throws Exception { MBeanHolder mbeanHolder = mbeanMap.get(mbean); if (mbeanHolder == null) { mbeanHolder = new MBeanHolder(mbean); mbeanMap.put(mbean, mbeanHolder); } mbeanHolder.addAttribute(attribute, composite, type, slope, units, publishName); } /** * Adds a mbean name/attribute pair to be sampled * @param mbean the name of the mbean * @param attribute the name of the attribute * @param publishName the name to publish this attribute on * @throws java.lang.Exception */ public void addMBeanAttribute(String mbean, String attribute, GMetricType type, String units, GMetricSlope slope, String publishName ) throws Exception { addMBeanAttribute( mbean, attribute, null, type, units, slope, publishName ); } /** * Called by the JMXAgent periodically to sample the mbeans */ public void run() { try { for (String mbean : mbeanMap.keySet()) { MBeanHolder h = mbeanMap.get(mbean); h.publish(); } } catch ( Exception ex ) { // Robust exception to prevent thread death //log.warning("Exception thrown sampling Mbeans"); //log.throwing( this.getClass().getName(), "Exception thrown sampling Mbeans:", ex) ; } } /** * Data structure used to sample one attribute */ private class MBeanAttribute { private String attributeName; private String key; private String canonicalName; private String units ; private GMetricType type ; private GMetricSlope slope ; private String publishName ; public MBeanAttribute(String attributeName, String compositeKey, GMetricType type, String units, GMetricSlope slope, String publishName ) { this.key = compositeKey ; this.canonicalName = attributeName + "." + compositeKey ; this.attributeName = attributeName; this.units = units ; this.type = type ; this.slope = slope ; this.publishName = publishName ; } public MBeanAttribute(String attributeName, GMetricType type, GMetricSlope slope, String units, String publishName ) { this(attributeName, null, type, units, slope, publishName); } public void publish(ObjectName objectName) { try { String value = null; if(mbs == null) { mbs = ManagementFactory.getPlatformMBeanServer(); } Object o = mbs.getAttribute(objectName, attributeName); if (o instanceof CompositeData) { CompositeData cd = (CompositeData) o; if (key != null) { Object val = cd.get(key); // log.fine("Sampling " + objectName + // " attribute " + canonicalName + ":" + val); value = val.toString(); } } else { if (null != o){ value = o.toString(); // log.fine("Sampling " + objectName + // " attribute " + canonicalName + ":" + o); }else{ // log.fine("Not sampling " + objectName + // " attribute " + canonicalName + // " as value is null"); } } if (null != value){ Publisher gm = getPublisher(); // log.finer("Announcing metric " + this.toString() + " value=" + value ); gm.publish(process, publishName, value, getType(), getSlope(), getUnits()); } } catch ( javax.management.InstanceNotFoundException ex ) { // log.warning("Exception when getting " + objectName + " " + canonicalName); } catch (Exception ex) { // log.log(Level.WARNING, // "Exception when getting " + objectName + " " + canonicalName, // ex); } } public String getAttributeName() { return attributeName; } public String getCanonicalName() { return canonicalName; } public String getUnits() { return units; } public GMetricType getType() { return type; } public GMetricSlope getSlope() { return slope; } public String getKey() { return key; } @Override public String toString() { StringBuilder buf = new StringBuilder() ; buf.append("attributeName=").append(attributeName); buf.append(" canonicalName=").append(canonicalName); buf.append(" units=").append(units); buf.append(" type=").append(type); buf.append(" slope=").append(slope); buf.append(" publishName=").append(publishName); return buf.toString(); } @Override public boolean equals(Object obj) { if (obj == null) return false; if (obj == this) return true; if (this.getClass() != obj.getClass()) return false; MBeanAttribute attribute = (MBeanAttribute)obj ; return canonicalName.equals(attribute.getCanonicalName()); // return attributeName.equals(attribute.getAttributeName()); } @Override public int hashCode() { int hash = 7; hash = 79 * hash + (this.canonicalName != null ? this.canonicalName.hashCode() : 0); // hash = 79 * hash + (this.attributeName != null ? this.attributeName.hashCode() : 0); return hash; } } /** * Data structure to hold and query mbean */ private class MBeanHolder { private ObjectName objectName; private Set attributes = new HashSet(); public MBeanHolder(String name) throws Exception { objectName = new ObjectName(name); } public void addAttribute(String attributeName, String compositeName, GMetricType type, GMetricSlope slope, String units, String publishName ) { attributes.add(new MBeanAttribute(attributeName, compositeName, type, units, slope, publishName)); } public void publish() { for (MBeanAttribute attr : attributes) { try { attr.publish(objectName); } catch (Exception ex) { ex.printStackTrace(); } } } } } jmxetric-1.0.6/src/main/java/info/ganglia/jmxetric/Publisher.java000066400000000000000000000005741230513052600247640ustar00rootroot00000000000000package info.ganglia.jmxetric; import info.ganglia.gmetric4j.gmetric.GMetricSlope; import info.ganglia.gmetric4j.gmetric.GMetricType; import info.ganglia.gmetric4j.gmetric.GangliaException; public interface Publisher { void publish( String processName, String attributeName, String value, GMetricType type, GMetricSlope slope, String units ) throws GangliaException; } jmxetric-1.0.6/src/main/java/info/ganglia/jmxetric/XMLConfigurationService.java000066400000000000000000000311751230513052600275410ustar00rootroot00000000000000package info.ganglia.jmxetric; import info.ganglia.gmetric4j.gmetric.GMetric; import info.ganglia.gmetric4j.gmetric.GMetricSlope; import info.ganglia.gmetric4j.gmetric.GMetricType; import info.ganglia.gmetric4j.gmetric.GMetric.UDPAddressingMode; //import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * Configures the JMXetricAgent based on the XML config file */ public class XMLConfigurationService { // private static Logger log = // Logger.getLogger(JMXetricAgent.class.getName()); private final static XPath xpath = XPathFactory.newInstance().newXPath(); private static final String DEFAULT_CONFIG="jmxetric.xml"; private static final String DEFAULT_MODE="multicast"; /** * Configures the JMXetricAgent based on the supplied agent args * @param agent the agent to configure * @param agentArgs the agent arg list * @throws java.lang.Exception */ public static void configure(JMXetricAgent agent, String agentArgs) throws Exception { String host = null ; String port = null ; String mode = null ; String config = DEFAULT_CONFIG ; String wireformat = null ; String processName = null ; String spoof = null; if ( agentArgs != null ) { String[] args = agentArgs.split(","); host = getTagValue( "host", args, null ); port = getTagValue( "port", args, null ); config = getTagValue( "config", args, DEFAULT_CONFIG ); mode = getTagValue( "mode", args, DEFAULT_MODE ); wireformat = getTagValue( "wireformat31x", args, "false"); processName = getTagValue( "process", args, null ); spoof = getTagValue( "spoof", args, null ); } // log.config("Command line argument found: host=" + host ); // log.config("Command line args: port=" + port ); // log.config("Command line args: config=" + config ); // log.config("Command line args: mode=" + mode ); // log.config("Command line args: wireformat31x=" + wireformat ); // log.config("Command line args: process=" + processName ); // log.config("Command line args: spoof=" + spoof ); InputSource inputSource = new InputSource(config); configureGangliaFromXML( agent, inputSource, host, port, mode, wireformat, spoof ); configureJMXetricFromXML( agent, inputSource, config, processName ); } private final static Pattern argPattern = Pattern.compile( "(\\S+?)\\=(\\S*)" ); // Default multicast TTL = 5 (same site) private static final int DEFAULT_TTL = 5; /** * Parses the string array, input, looking for a pattern tag=value * @param tag the tag to search for * @param input the array list * @param defaultValue the default value if tag is not found * @return tha value */ private static String getTagValue(String tag, String[] input, String defaultValue) { for( String arg: input) { Matcher matcher = argPattern.matcher(arg); // Get tagname and contents of tag if ( matcher.find() ) { String tagname = matcher.group(1); if ( tag.equals( tagname )) { return matcher.group(2); } } } return defaultValue ; } private static String selectParameterFromNode( Node ganglia, String attributeName, String defaultValue ) { return selectParameterFromNode( (String)null, ganglia, attributeName, defaultValue); } private static String selectParameterFromNode( String cmdLine, Node ganglia, String attributeName, String defaultValue ) { String ret = defaultValue ; if ( cmdLine != null ) ret = cmdLine ; else if ( ganglia != null ) { Node node = ganglia.getAttributes().getNamedItem(attributeName); if ( node != null ) { String value = node.getNodeValue(); if ( value != null ) ret = value ; } } return ret ; } /** * Creates a GMetric attribute on the JMXetricAgent from the XML config * @param agent the agent to configure * @param inputSource the input xml * @param cmdLineHost the host found on the agent arg list * @param cmdLinePort the port found on the agent arg list * @param cmdLineMode the mode found on the agent arg list * @param v31x true if the ganglia v31x wire format should be used * @param cmdLineSpoof the spoof value found on the agent arg list * @throws java.lang.Exception */ private static void configureGangliaFromXML(JMXetricAgent agent, InputSource inputSource, String cmdLineHost, String cmdLinePort, String cmdLineMode, String cmdLinev31x, String cmdLineSpoof) throws Exception { // Gets the config for ganglia // Note that the ganglia config needs to be found before the samplers // are created. // String gangliaExpr = "/jmxetric-config/ganglia"; Node g = (Node) xpath.evaluate(gangliaExpr, inputSource, XPathConstants.NODE); String hostname = selectParameterFromNode( cmdLineHost, g, "hostname", "localhost"); String port = selectParameterFromNode( cmdLinePort, g, "port", "8649"); int iport = Integer.parseInt(port); String mode = selectParameterFromNode( cmdLineMode, g, "mode","multicast"); UDPAddressingMode addressingMode = UDPAddressingMode.MULTICAST; if ( mode.toLowerCase().equals("unicast")) { addressingMode = UDPAddressingMode.UNICAST; } String stringv31x = selectParameterFromNode( cmdLinev31x, g, "wireformat31x", "false"); boolean v31x = Boolean.parseBoolean(stringv31x) ; String spoof = selectParameterFromNode( cmdLineSpoof, g, "spoof", null); StringBuilder buf = new StringBuilder() ; buf.append("GMetric host=").append( hostname ); buf.append(" port=").append( port ); buf.append(" mode=").append( mode ); buf.append(" v31x=").append( v31x ); buf.append(" spoof=").append( spoof ); // log.fine(buf.toString()); GMetric gmetric = new GMetric(hostname, iport, addressingMode, DEFAULT_TTL, v31x, null, spoof ); agent.setGmetric(gmetric); } /** * Creates managed attributed on the JMXetricAgent from the XML config * @param agent the agent to configure * @param inputSource the input xml * @param file the config file from trom the agent arg list * @throws java.lang.Exception */ private static void configureJMXetricFromXML(JMXetricAgent agent, InputSource inputSource, String file, String processName) throws Exception { // if the processName supplied from the agent arg list is null, see if // it's in the xml if ( processName == null ) { Node jvm = (Node) xpath.evaluate("/jmxetric-config/jvm", inputSource, XPathConstants.NODE); if ( jvm != null ) processName = jvm.getAttributes().getNamedItem("process").getNodeValue(); } // Gets the config for the samplers // String expression = "/jmxetric-config/sample"; NodeList samples = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); // for every sample for (int i = 0; i < samples.getLength(); i++) { Node sample = samples.item(i); //log.finer("Sample is " + sample); /* * String port = selectParameterFromNode( cmdLinePort, g, "port", "8649"); int iport = Integer.parseInt(port); */ String delayString = selectParameterFromNode( sample, "delay", "60"); int delay = Integer.parseInt(delayString); String initialDelayString = selectParameterFromNode( sample, "initialdelay", "0"); int initialDelay = Integer.parseInt(initialDelayString); MBeanSampler mbSampler = new MBeanSampler(initialDelay, delay, processName); NodeList mbeans = (NodeList) xpath.evaluate("mbean", sample, XPathConstants.NODESET); // for every mbean for (int j = 0; j < mbeans.getLength(); j++) { Node mbean = mbeans.item(j); String mbeanName = selectParameterFromNode( mbean, "name", null ); String mbeanPublishName = selectParameterFromNode( mbean, "pname", "NULL" ); // log.finer("Mbean is " + mbeanName); NodeList attrs = (NodeList) xpath.evaluate("attribute", mbean, XPathConstants.NODESET); //for every attribute for (int k = 0; k < attrs.getLength(); k++) { Node attr = attrs.item(k); String attrName = selectParameterFromNode(attr, "name","NULL"); String type = selectParameterFromNode(attr, "type", ""); String units = selectParameterFromNode(attr, "units", "" ); String pname = selectParameterFromNode(attr, "pname", ""); String slope = selectParameterFromNode(attr, "slope", "" ); if ( "".equals(type)) { //assume that there is a composite attribute to follow NodeList composites = (NodeList) xpath.evaluate("composite", attr, XPathConstants.NODESET); //for every composite for (int l = 0; l < composites.getLength(); l++) { Node composite = composites.item(l); String compositeName = selectParameterFromNode(composite, "name", "NULL"); String compositeType = selectParameterFromNode(composite, "type", ""); String compositeUnits = selectParameterFromNode(composite, "units", ""); String compositePname = selectParameterFromNode(composite, "pname", ""); String compositeSlope = selectParameterFromNode(composite, "slope", ""); String metricName = buildMetricName( processName, mbeanName, mbeanPublishName, compositeName, compositePname ); //log.finer("Attr is " + compositeName); mbSampler.addMBeanAttribute(mbeanName, attrName, compositeName, GMetricType.valueOf(compositeType.toUpperCase()), compositeUnits, GMetricSlope.valueOf(compositeSlope.toUpperCase()), metricName); } } else { // It's a non composite attribute //log.finer("Attr is " + attrName); String metricName = buildMetricName( processName, mbeanName, mbeanPublishName, attrName, pname ); mbSampler.addMBeanAttribute(mbeanName, attrName, null, GMetricType.valueOf(type.toUpperCase()), units, GMetricSlope.valueOf(slope.toUpperCase()), metricName); } } } agent.addSampler(mbSampler); } } /** * Builds the metric name in ganglia * @param process the process name, or null if not used * @param mbeanName the mbean name * @param mbeanPublishName the mbean publish name, or null if not used * @param attribute the mbean attribute name * @param attrPublishName the mbean attribute publish name * @return the metric name */ private static String buildMetricName( String process, String mbeanName, String mbeanPublishName, String attribute, String attrPublishName ) { StringBuilder buf = new StringBuilder() ; if ( process != null ) { buf.append( process ); buf.append("_"); } if ( mbeanPublishName != null ) { buf.append( mbeanPublishName); } else { buf.append( mbeanName ); } buf.append("_"); if ( ! "".equals( attrPublishName ) ) { buf.append( attrPublishName ); } else { buf.append( attribute ); } return buf.toString() ; } } jmxetric-1.0.6/src/main/java/info/ganglia/jmxetric/package.html000066400000000000000000000006071230513052600244420ustar00rootroot00000000000000 Provides a JVM agent that polls MBeans periodically and publishes the results to Ganglia

Package Specification

Related Documentation

For a description of Ganglia, please see: jmxetric-1.0.6/src/main/resources/000077500000000000000000000000001230513052600170655ustar00rootroot00000000000000jmxetric-1.0.6/src/main/resources/META-INF/000077500000000000000000000000001230513052600202255ustar00rootroot00000000000000jmxetric-1.0.6/src/main/resources/META-INF/MANIFEST.MF000066400000000000000000000002721230513052600216600ustar00rootroot00000000000000Manifest-Version: 1.0 Premain-Class: info.ganglia.jmxetric.JMXetricAgent Boot-Class-Path: oncrpc-${oncrpc.version}.jar gmetric4j-${gmetric4j.version}.jar Can-Redefine-Classes: false jmxetric-1.0.6/src/test/000077500000000000000000000000001230513052600151065ustar00rootroot00000000000000jmxetric-1.0.6/src/test/java/000077500000000000000000000000001230513052600160275ustar00rootroot00000000000000jmxetric-1.0.6/src/test/java/info/000077500000000000000000000000001230513052600167625ustar00rootroot00000000000000jmxetric-1.0.6/src/test/java/info/ganglia/000077500000000000000000000000001230513052600203645ustar00rootroot00000000000000jmxetric-1.0.6/src/test/java/info/ganglia/jmxetric/000077500000000000000000000000001230513052600222115ustar00rootroot00000000000000jmxetric-1.0.6/src/test/java/info/ganglia/jmxetric/Example.java000066400000000000000000000034661230513052600244600ustar00rootroot00000000000000package info.ganglia.jmxetric; import java.lang.management.ManagementFactory; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import javax.management.MBeanServer; import javax.management.ObjectName; /** * MXBean implementation used for testing * @author humphrej * */ public class Example implements TestExampleMXBean { private AtomicInteger counter = new AtomicInteger(0); private ScheduledExecutorService executor = null ; public Example() { executor = Executors.newSingleThreadScheduledExecutor( new ThreadFactory() { public Thread newThread(Runnable r) { Thread t = new Thread( r ); t.setName("ExampleMBean"); return t ; } }); } public void start() { executor.scheduleAtFixedRate( new Runnable() { public void run() { counter.incrementAndGet(); } }, 0, 1, TimeUnit.SECONDS); } public void stop() { executor.shutdown(); } public double getDouble() { return DOUBLE_VALUE ; } public float getFloat() { return FLOAT_VALUE; } public int getInt() { return INT_VALUE; } public long getLong() { return LONG_VALUE; } public String getString() { return STRING_VALUE; } public int getCounter() { return counter.get(); } public ExampleComposite getComposite() { return new ExampleComposite(ExampleComposite.DATE_VALUE, ExampleComposite.INT_VALUE, ExampleComposite.STRING_VALUE); } public static void register() { try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("jmxetric:type=TestExample"); Example mbean = new Example(); mbs.registerMBean(mbean, name); } catch (Exception ex ){ ex.printStackTrace(); } } } jmxetric-1.0.6/src/test/java/info/ganglia/jmxetric/ExampleComposite.java000066400000000000000000000015761230513052600263430ustar00rootroot00000000000000package info.ganglia.jmxetric; import java.beans.ConstructorProperties; import java.util.Date; /** * Class used to test composite attribute of an MXBean * @author humphrej * */ public class ExampleComposite { public static final Date DATE_VALUE=new Date(1000); public static final int INT_VALUE=29111974; public static final String STRING_VALUE="WIBBLE"; private final Date date; private final int integer; private final String name; @ConstructorProperties({"date", "integer", "name"}) public ExampleComposite(Date date, int integer, String name) { this.date = date; this.integer = integer; this.name = name; } public Date getDate() { return date; } public int getInteger() { return integer; } public String getName() { return name; } } jmxetric-1.0.6/src/test/java/info/ganglia/jmxetric/JMXetricAgentIT.java000066400000000000000000000025741230513052600257650ustar00rootroot00000000000000package info.ganglia.jmxetric; import static org.junit.Assert.*; import info.ganglia.gmetric4j.gmetric.GMetricResult; import info.ganglia.jmxetric.JMXetricAgent; import info.ganglia.jmxetric.XMLConfigurationService; import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import javax.management.ObjectName; import org.junit.Before; import org.junit.Test; /** * */ public class JMXetricAgentIT { @Before public void setUp() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("jmxetric:type=TestExample"); Example mbean = new Example(); mbs.registerMBean(mbean, name); } @Test public void testRun() { JMXetricAgent a = null ; try { a = new JMXetricAgent(); XMLConfigurationService.configure(a, "host=localhost,port=8649,wireformat31x=true,config=src/test/resources/jmxetric_test.xml"); a.start(); Thread.sleep(5000); GMetricResult.GMetricDetail floatResult = GMetricResult.getGMetric("ProcessName_TestExample_Float"); assertEquals( Float.toString( Example.FLOAT_VALUE) , floatResult.value); assertEquals( "both", floatResult.slope); // TODO Add asserts } catch ( Exception ex ) { ex.printStackTrace(); fail("Exception thrown"); } } } jmxetric-1.0.6/src/test/java/info/ganglia/jmxetric/MBeanSamplerTest.java000066400000000000000000000100451230513052600262220ustar00rootroot00000000000000package info.ganglia.jmxetric; import static org.junit.Assert.*; import info.ganglia.gmetric4j.Publisher; import info.ganglia.gmetric4j.gmetric.GMetricSlope; import info.ganglia.gmetric4j.gmetric.GMetricType; import info.ganglia.gmetric4j.gmetric.GangliaException; import info.ganglia.jmxetric.MBeanSampler; import java.lang.management.ManagementFactory; import java.util.HashMap; import java.util.Map; import javax.management.MBeanServer; import javax.management.ObjectName; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * */ public class MBeanSamplerTest { public static String BEAN_NAME="jmxetric:type=Test.Example"; private class MyPublisher implements Publisher { Map results = new HashMap(); public void publish(String processName, String attributeName, String value, GMetricType type, GMetricSlope slope, String units) throws GangliaException { results.put(attributeName, value); } public String getResult( String attributeName ) { return results.get(attributeName); } } @Before public void setUp() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName(BEAN_NAME); Example mbean = new Example(); mbs.registerMBean(mbean, name); } @After public void tearDown() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName(BEAN_NAME); mbs.unregisterMBean(name); } /** * Test of attribute sample, type Long */ @Test public void sampleLong() throws Exception { MBeanSampler sampler = new MBeanSampler(0, 30000, "TEST") ; MyPublisher publisher = new MyPublisher() ; sampler.setPublisher(publisher); sampler.addMBeanAttribute(BEAN_NAME, "Long", GMetricType.INT32, "bytes", GMetricSlope.BOTH, "Longer"); sampler.run() ; String value = publisher.getResult("Longer"); assertEquals( Example.LONG_VALUE, Long.valueOf(value )); } /** * Test of attribute sample, type String */ @Test public void sampleComposite() throws Exception { MBeanSampler sampler = new MBeanSampler(0, 30000, "TEST") ; MyPublisher publisher = new MyPublisher() ; sampler.setPublisher(publisher); String compNamePublishName = "compName"; sampler.addMBeanAttribute(BEAN_NAME, "Composite", "name", GMetricType.STRING, "bytes", GMetricSlope.BOTH, compNamePublishName); String compIntPublishName = "compInt"; sampler.addMBeanAttribute(BEAN_NAME, "Composite", "integer", GMetricType.STRING, "bytes", GMetricSlope.BOTH, compIntPublishName); String compDatePublishName = "compDate"; sampler.addMBeanAttribute(BEAN_NAME, "Composite", "date", GMetricType.STRING, "bytes", GMetricSlope.BOTH, compDatePublishName); sampler.run(); assertEquals( ExampleComposite.STRING_VALUE, publisher.getResult(compNamePublishName) ); assertEquals( "" + ExampleComposite.INT_VALUE, publisher.getResult(compIntPublishName) ); assertEquals( "" + ExampleComposite.DATE_VALUE, publisher.getResult(compDatePublishName) ); } /** * Test of attribute sample, type int, slope positive */ @Test public void sampleCounter() throws Exception { MBeanSampler sampler = new MBeanSampler(0, 1, "TEST") ; MyPublisher publisher = new MyPublisher() ; sampler.setPublisher(publisher); sampler.addMBeanAttribute(BEAN_NAME, "Counter", GMetricType.INT32, "units", GMetricSlope.POSITIVE, "counter"); sampler.run() ; String value = publisher.getResult("counter"); assertTrue( Integer.valueOf(value) >= 0); } } jmxetric-1.0.6/src/test/java/info/ganglia/jmxetric/TestExampleMXBean.java000066400000000000000000000007221230513052600263430ustar00rootroot00000000000000package info.ganglia.jmxetric; /** * MXBean definition used for testing * @author humphrej * */ public interface TestExampleMXBean { final int INT_VALUE=1234 ; final long LONG_VALUE=43210L ; final double DOUBLE_VALUE=123.45D ; final float FLOAT_VALUE=543.21F ; final String STRING_VALUE="STRING" ; int getInt() ; long getLong() ; String getString() ; double getDouble() ; float getFloat() ; ExampleComposite getComposite(); int getCounter() ; } jmxetric-1.0.6/src/test/resources/000077500000000000000000000000001230513052600171205ustar00rootroot00000000000000jmxetric-1.0.6/src/test/resources/jmxetric_test.xml000066400000000000000000000062041230513052600225300ustar00rootroot00000000000000 ]>