21a22 > import java.util.Arrays; 22a24,26 > import java.util.Hashtable; > import java.util.Map; > import java.util.Set; 25d28 < import javax.management.MBeanInfo; 33,34c36,38 < import org.json.*; < --- > import org.json.JSONArray; > import org.json.JSONException; > import org.json.JSONObject; 38,39c42 < class JMXItemChecker extends ItemChecker < { --- > class JMXItemChecker extends ItemChecker { 49,50c52 < public JMXItemChecker(JSONObject request) throws ZabbixException < { --- > public JMXItemChecker(JSONObject request) throws ZabbixException { 53,54c55 < try < { --- > try { 65c66 < if (null != username && null == password || null == username && null != password) --- > if (null != username && null == password || null == username && null != password) { 67,69c68,69 < } < catch (Exception e) < { --- > } > } catch (Exception e) { 75,76c75 < public JSONArray getValues() throws ZabbixException < { --- > public JSONArray getValues() throws ZabbixException { 79,80c78 < try < { --- > try { 83,84c81 < if (null != username && null != password) < { --- > if (null != username && null != password) { 86c83 < env.put(JMXConnector.CREDENTIALS, new String[] {username, password}); --- > env.put(JMXConnector.CREDENTIALS, new String[] { username, password }); 92c89 < for (String key : keys) --- > for (String key : keys) { 94,96c91,92 < } < catch (Exception e) < { --- > } > } catch (Exception e) { 98,101c94,101 < } < finally < { < try { if (null != jmxc) jmxc.close(); } catch (java.io.IOException exception) { } --- > } finally { > try { > if (null != jmxc) { > jmxc.close(); > } > } catch (java.io.IOException exception) { > // Do nothing > } 111,112c111 < protected String getStringValue(String key) throws Exception < { --- > protected String getStringValue(String key) throws Exception { 115,117c114,115 < if (item.getKeyId().equals("jmx")) < { < if (2 != item.getArgumentCount()) --- > if (item.getKeyId().equals("jmx")) { > if (2 != item.getArgumentCount()) { 119c117,118 < --- > } > 125,127c124,129 < // Attribute name and composite data field names are separated by dots. On the other hand the < // name may contain a dot too. In this case user needs to escape it with a backslash. Also the < // backslash symbols in the name must be escaped. So a real separator is unescaped dot and --- > // Attribute name and composite data field names are separated by > // dots. On the other hand the > // name may contain a dot too. In this case user needs to escape it > // with a backslash. Also the > // backslash symbols in the name must be escaped. So a real > // separator is unescaped dot and 132,133c134 < if (-1 != sep) < { --- > if (-1 != sep) { 138,139c139 < } < else --- > } else { 140a141 > } 148,153c149,161 < return getPrimitiveAttributeValue(mbsc.getAttribute(objectName, realAttributeName), fieldNames); < } < else if (item.getKeyId().equals("jmx.discovery")) < { < if (0 != item.getArgumentCount()) < throw new ZabbixException("required key format: jmx.discovery"); --- > if (item.getArgument(1).contains("*")) { > logger.trace("WILDCARD:" + item.getArgument(1)); > // Assume the object name is a regex > Set matchingNames = mbsc.queryNames(objectName, null); > if (matchingNames != null && matchingNames.size() > 0) { > objectName = matchingNames.iterator().next(); > logger.trace("FOUND:" + objectName.toString() + "FOR " + item.getArgument(1)); > } else { > logger.trace("EMPTY:" + item.getArgument(1)); > } > } else { > logger.trace("NO WILDCARD:" + item.getArgument(1)); > } 155c163,164 < JSONArray counters = new JSONArray(); --- > return getPrimitiveAttributeValue(mbsc.getAttribute(objectName, realAttributeName), fieldNames); > } else if (item.getKeyId().equals("jmx.discovery")) { 157,169c166,168 < for (ObjectName name : mbsc.queryNames(null, null)) < { < logger.trace("discovered object '{}'", name); < < for (MBeanAttributeInfo attrInfo : mbsc.getMBeanInfo(name).getAttributes()) < { < logger.trace("discovered attribute '{}'", attrInfo.getName()); < < if (!attrInfo.isReadable()) < { < logger.trace("attribute not readable, skipping"); < continue; < } --- > if (item.getArgumentCount() > 1) { > throw new ZabbixException("required key format: jmx.discovery or jmx.discovery[]"); > } 171,180c170,193 < try < { < logger.trace("looking for attributes of primitive types"); < String descr = (attrInfo.getName().equals(attrInfo.getDescription()) ? null : attrInfo.getDescription()); < findPrimitiveAttributes(counters, name, descr, attrInfo.getName(), mbsc.getAttribute(name, attrInfo.getName())); < } < catch (Exception e) < { < Object[] logInfo = {name, attrInfo.getName(), e}; < logger.trace("processing '{},{}' failed", logInfo); --- > JSONArray counters = new JSONArray(); > if (item.getArgumentCount() == 0) { > for (ObjectName name : mbsc.queryNames(null, null)) { > logger.trace("discovered object '{}'", name); > > for (MBeanAttributeInfo attrInfo : mbsc.getMBeanInfo(name).getAttributes()) { > logger.trace("discovered attribute '{}'", attrInfo.getName()); > > > if (!attrInfo.isReadable()) { > logger.trace("attribute not readable, skipping"); > continue; > } > > try { > logger.trace("looking for attributes of primitive types"); > String descr = (attrInfo.getName().equals(attrInfo.getDescription()) ? null > : attrInfo.getDescription()); > findPrimitiveAttributes(counters, name, descr, attrInfo.getName(), > mbsc.getAttribute(name, attrInfo.getName())); > } catch (Exception e) { > Object[] logInfo = { name, attrInfo.getName(), e }; > logger.trace("processing '{},{}' failed", logInfo); > } 182a196,201 > } else { > String objNameWildcard = item.getArgument(1); > logger.trace("performing wildcard lookup against '{}'", objNameWildcard); > Set objectNames = mbsc.queryNames(new ObjectName(objNameWildcard), null); > logger.trace("wildcard output: {}", Arrays.toString(objectNames.toArray())); > buildDiscoveryOutput(counters, objectNames); 188,189c207,210 < } < else --- > > > > } else { 190a212,213 > } > 193,194c216,218 < private String getPrimitiveAttributeValue(Object dataObject, String fieldNames) throws ZabbixException < { --- > > > private String getPrimitiveAttributeValue(Object dataObject, String fieldNames) throws ZabbixException { 197c221 < if (null == dataObject) --- > if (null == dataObject) { 198a223 > } 200,202c225,228 < if (fieldNames.equals("")) < { < if (isPrimitiveAttributeType(dataObject.getClass())) --- > if (fieldNames.equals("")) { > if (isPrimitiveAttributeType(dataObject.getClass())) { > return dataObject.toString(); > } else if (dataObject instanceof Number) { 204c230 < else --- > } else { 205a232 > } 208,209c235 < if (dataObject instanceof CompositeData) < { --- > if (dataObject instanceof CompositeData || dataObject instanceof Map) { 212,213d237 < CompositeData comp = (CompositeData)dataObject; < 219,220c243 < if (-1 != sep) < { --- > if (-1 != sep) { 223,224c246 < } < else --- > } else { 225a248 > } 230,232c253,262 < return getPrimitiveAttributeValue(comp.get(dataObjectName), newFieldNames); < } < else --- > Object data = null; > > if (dataObject instanceof CompositeData) { > data = ((CompositeData) dataObject).get(dataObjectName); > } else { > data = ((Map) dataObject).get(dataObjectName); > } > > return getPrimitiveAttributeValue(data, newFieldNames); > } else { 233a264 > } 236,237c267,285 < private void findPrimitiveAttributes(JSONArray counters, ObjectName name, String descr, String attrPath, Object attribute) throws JSONException < { --- > private void buildDiscoveryOutput(JSONArray counters, Set objectNames) throws JSONException { > for (ObjectName objName : objectNames) { > // Add the full JMX Object Name as a macro > // in the return string > JSONObject taskObj = new JSONObject(); > logger.trace("building discovery output for object '{}'", objName.getCanonicalName()); > taskObj.put("{#JMXOBJ}", objName.getCanonicalName()); > > // Add each property of the Object Name as returned macros > Hashtable props = objName.getKeyPropertyList(); > for (Map.Entry propEntry : props.entrySet()) { > taskObj.put(String.format("{#%s}", propEntry.getKey().toUpperCase()), propEntry.getValue()); > } > counters.put(taskObj); > } > } > > private void findPrimitiveAttributes(JSONArray counters, ObjectName name, String descr, String attrPath, > Object attribute) throws JSONException { 240,241c288 < if (isPrimitiveAttributeType(attribute.getClass())) < { --- > if (isPrimitiveAttributeType(attribute.getClass())) { 253,255c300 < } < else if (attribute instanceof CompositeData) < { --- > } else if (attribute instanceof CompositeData) { 258c303 < CompositeData comp = (CompositeData)attribute; --- > CompositeData comp = (CompositeData) attribute; 262,264c307 < } < else if (attribute instanceof TabularDataSupport || attribute.getClass().isArray()) < { --- > } else if (attribute instanceof TabularDataSupport || attribute.getClass().isArray()) { 266,267c309 < } < else --- > } else { 268a311 > } 271,275c314,317 < private boolean isPrimitiveAttributeType(Class clazz) < { < Class[] clazzez = {Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, < Float.class, Double.class, String.class, java.math.BigDecimal.class, java.math.BigInteger.class, < java.util.Date.class, javax.management.ObjectName.class}; --- > private boolean isPrimitiveAttributeType(Class clazz) { > Class[] clazzez = { Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, > Float.class, Double.class, String.class, java.math.BigDecimal.class, java.math.BigInteger.class, > java.util.Date.class, javax.management.ObjectName.class };