/** * {@inheritDoc} */ @Override public void handleNotification(Notification notification, Object handback) { if (notification instanceof MBeanServerNotification) { MBeanServerNotification serverNotification = (MBeanServerNotification) notification; ObjectName mBeanName = serverNotification.getMBeanName(); if (MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(serverNotification.getType())) { // if we have registration pick up the attributes registerMBeans(MBeanServerHolder.this, mBeanName); } else if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(serverNotification.getType())) { // get maps from holder // if we have un-registration remove from maps String mBeanNameString = mBeanName.toString(); for (Iterator<JmxAttributeDescriptor> it = activeAttributes.keySet().iterator(); it.hasNext();) { JmxAttributeDescriptor descriptor = it.next(); if (Objects.equal(descriptor.getmBeanObjectName(), mBeanNameString)) { it.remove(); } } nameStringToObjectName.remove(mBeanNameString); } } }
ObjectName objectName = nameStringToObjectName.get(descriptor.getmBeanObjectName()); Object collectedValue = mBeanServer.getAttribute(objectName, descriptor.getAttributeName()); } catch (Exception e) { iterator.remove(); log.warn("JMX::Exception. An exception has been thrown during attempt to fetch the attribute " + descriptor.getAttributeName() + " from the MBean " + descriptor.getmBeanObjectName() + ". Attribute removed from the actively read list.", e);
@Test public void notMatchingWrongObjectName() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("something not valid"); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); }
@Test public void notMatchingAssignmentObjectNameNull() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(assignment.getObjectName()).thenReturn(null); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); }
@Test public void matchingAllAttributes() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(descriptor.getAttributeName()).thenReturn("attribute"); when(assignment.getObjectName()).thenReturn(objectName); when(assignment.getAttributes()).thenReturn(Collections.<String> emptySet()); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(true); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(true)); }
@Test public void notMatchingObjectName() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(assignment.getObjectName()).thenReturn(objectName); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(false); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); }
@Test public void matchingSpecificAttribute() { String attributeName = "attribute"; when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(descriptor.getAttributeName()).thenReturn(attributeName); when(assignment.getObjectName()).thenReturn(objectName); when(assignment.getAttributes()).thenReturn(Collections.singleton(attributeName)); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(true); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(true)); }
@Test public void notMatchingAttribute() { when(descriptor.ismBeanAttributeIsReadable()).thenReturn(true); when(descriptor.getmBeanObjectName()).thenReturn("java.lang:type=Memory"); when(descriptor.getAttributeName()).thenReturn("attribute"); when(assignment.getAttributes()).thenReturn(Collections.singleton("not matching")); when(assignment.getObjectName()).thenReturn(objectName); when(objectName.apply(Matchers.<ObjectName> any())).thenReturn(true); boolean matches = filter.matches(assignment, descriptor); assertThat(matches, is(false)); } }
/** * Tests if the given {@link JmxAttributeDescriptor} matches the jmx bean sensor assignment. * * @param assignment * {@link JmxBeanSensorAssignment} * @param attributeDescriptor * {@link JmxAttributeDescriptor} to check * @return Returns <code>true</code> if the data in the descriptor fits the monitoring * definition in the assignment, <code>false</code> otherwise */ public boolean matches(JmxBeanSensorAssignment assignment, JmxAttributeDescriptor attributeDescriptor) { // not matching if the attribute is not readable if (!attributeDescriptor.ismBeanAttributeIsReadable()) { return false; } if (!matchesObjectName(assignment, attributeDescriptor.getmBeanObjectName())) { return false; } if (!matchesAttribute(assignment, attributeDescriptor.getAttributeName())) { return false; } return true; }
@Test public void add() { long platformId = 7L; String attributeName = "attributeName"; String objectName = "objectname"; long attributeId = 13L; String attributeType = "type"; String attributeDecs = "desc"; boolean isIs = false; boolean readable = true; boolean writable = false; when(filter.matches(assignment, descriptor)).thenReturn(true); when(agentConfig.getPlatformId()).thenReturn(platformId); when(descriptor.getAttributeName()).thenReturn(attributeName); when(descriptor.getmBeanAttributeDescription()).thenReturn(attributeDecs); when(descriptor.getmBeanObjectName()).thenReturn(objectName); when(descriptor.getmBeanAttributeId()).thenReturn(attributeId); when(descriptor.getmBeanAttributeType()).thenReturn(attributeType); when(descriptor.ismBeanAttributeIsIs()).thenReturn(isIs); when(descriptor.ismBeanAttributeIsReadable()).thenReturn(readable); when(descriptor.ismBeanAttributeIsWritable()).thenReturn(writable); boolean added = applier.addMonitoringPoint(agentConfig, descriptor); assertThat(added, is(true)); verify(registrationService).registerJmxSensorDefinitionDataIdent(platformId, objectName, attributeName, attributeDecs, attributeType, isIs, readable, writable); }
assertThat(sensorConfig.getmBeanObjectName(), is(equalTo(testObjectName))); assertThat(sensorConfig.getAttributeName(), is(equalTo(testAttributeName))); assertThat(sensorConfig.getmBeanAttributeDescription(), is(equalTo(testAttrDescription)));
assertThat(sensorConfig.getmBeanObjectName(), is(equalTo(testObjectName))); assertThat(sensorConfig.getAttributeName(), is(equalTo(testAttributeName))); assertThat(sensorConfig.getmBeanAttributeDescription(), is(equalTo(testAttrDescription)));
if (matches(jmxAttributeDescriptor)) { String objectName = jmxAttributeDescriptor.getmBeanObjectName(); String attributeName = jmxAttributeDescriptor.getAttributeName(); String description = jmxAttributeDescriptor.getmBeanAttributeDescription();