You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/07/31 14:47:46 UTC
svn commit: r1508833 - in
/sling/branches/SLING-2987-healthcheck-redesign/hc-core: ./
src/main/java/org/apache/sling/hc/api/
src/main/java/org/apache/sling/hc/impl/
src/main/java/org/apache/sling/hc/util/
src/test/java/org/apache/sling/hc/api/ src/test...
Author: bdelacretaz
Date: Wed Jul 31 12:47:45 2013
New Revision: 1508833
URL: http://svn.apache.org/r1508833
Log:
SLING-2987 - JmxAttributeHealthCheck added
Added:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java (with props)
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java
- copied, changed from r1508827, sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/NumericValueChecker.java
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java (with props)
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java
- copied, changed from r1508827, sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/NumericValueCheckerTest.java
Removed:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/NumericValueChecker.java
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/NumericValueCheckerTest.java
Modified:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Constants.java
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Result.java
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/ResultLog.java
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultLogTest.java
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultTest.java
Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml?rev=1508833&r1=1508832&r2=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml Wed Jul 31 12:47:45 2013
@@ -67,6 +67,16 @@
<scope>provided</scope>
</dependency>
<dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.2.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.2</version>
@@ -84,5 +94,11 @@
<version>4.8.1</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.9.5</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Constants.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Constants.java?rev=1508833&r1=1508832&r2=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Constants.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Constants.java Wed Jul 31 12:47:45 2013
@@ -23,4 +23,7 @@ public interface Constants {
/** info property name: description of a health check */
String HC_DESCRIPTION = "hc.description";
+
+ /** info property name: the tags of a health check */
+ String HC_TAGS = "hc.tags";
}
Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Result.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Result.java?rev=1508833&r1=1508832&r2=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Result.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/Result.java Wed Jul 31 12:47:45 2013
@@ -39,7 +39,7 @@ public class Result {
}
public boolean isOk() {
- return log.getMaxLevel().ordinal() < ResultLog.MIN_LEVEL_TO_REPORT.ordinal();
+ return log.getMaxLevel() != null && log.getMaxLevel().ordinal() < ResultLog.MIN_LEVEL_TO_REPORT.ordinal();
}
public ResultLog.Level getStatus() {
Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/ResultLog.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/ResultLog.java?rev=1508833&r1=1508832&r2=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/ResultLog.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/ResultLog.java Wed Jul 31 12:47:45 2013
@@ -32,7 +32,7 @@ import org.slf4j.helpers.MessageFormatte
public class ResultLog implements Logger {
private final Logger wrappedLogger;
- private Level maxLevel = Level.DEBUG;
+ private Level maxLevel = null;
private final List<Entry> entries;
/** Log messages at or above this level change the {@link Result's status} */
@@ -85,7 +85,11 @@ public class ResultLog implements Logger
}
private void storeMessage(ResultLog.Level level, String message) {
- maxLevel = level.ordinal() > maxLevel.ordinal() ? level : maxLevel;
+ if(maxLevel == null) {
+ maxLevel = level;
+ } else {
+ maxLevel = level.ordinal() > maxLevel.ordinal() ? level : maxLevel;
+ }
entries.add(new ResultLog.Entry(level, message));
}
Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java?rev=1508833&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java Wed Jul 31 12:47:45 2013
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.impl;
+
+import java.lang.management.ManagementFactory;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.hc.api.Constants;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.apache.sling.hc.util.SimpleConstraintChecker;
+import org.osgi.service.component.ComponentContext;
+
+/** {@link HealthCheck} that checks a single JMX attribute */
+@Component(configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true)
+@Service
+public class JmxAttributeHealthCheck implements HealthCheck {
+
+ private final Map<String, String> info = new HashMap<String, String>();
+ private String mbeanName;
+ private String attributeName;
+ private String constraint;
+
+ @Property
+ public static final String PROP_OBJECT_NAME = "mbean.name";
+
+ @Property
+ public static final String PROP_ATTRIBUTE_NAME = "attribute.name";
+
+ @Property
+ public static final String PROP_CONSTRAINT = "attribute.value.constraint";
+
+ @Property(cardinality=50)
+ public static final String PROP_TAGS = Constants.HC_TAGS;
+
+ @Activate
+ public void activate(ComponentContext ctx) {
+ mbeanName = PropertiesUtil.toString(ctx.getProperties().get(PROP_OBJECT_NAME), "");
+ attributeName = PropertiesUtil.toString(ctx.getProperties().get(PROP_ATTRIBUTE_NAME), "");
+ constraint = PropertiesUtil.toString(ctx.getProperties().get(PROP_CONSTRAINT), "");
+
+ info.put(PROP_OBJECT_NAME, mbeanName);
+ info.put(PROP_ATTRIBUTE_NAME, attributeName);
+ info.put(PROP_CONSTRAINT, constraint);
+ }
+
+ @Override
+ public Result execute(ResultLog log) {
+ final Result result = new Result(this, log);
+ log.debug("Checking {} / {} with constraint {}", new Object[] { mbeanName, attributeName, constraint });
+ try {
+ final MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
+ final ObjectName objectName = new ObjectName(mbeanName);
+ if(jmxServer.queryNames(objectName, null).size() == 0) {
+ log.error("MBean not found: {}", objectName);
+ }
+ final Object value = jmxServer.getAttribute(objectName, attributeName);
+ log.debug("{} {} returns {}", new Object[] { mbeanName, attributeName, value });
+ new SimpleConstraintChecker().check(value, constraint, log);
+ } catch(Exception e) {
+ log.warn(e.toString(), e);
+ }
+ return result;
+ }
+
+ @Override
+ public Map<String, String> getInfo() {
+ return info;
+ }
+}
Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Copied: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java (from r1508827, sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/NumericValueChecker.java)
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java?p2=sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java&p1=sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/NumericValueChecker.java&r1=1508827&r2=1508833&rev=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/NumericValueChecker.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java Wed Jul 31 12:47:45 2013
@@ -21,28 +21,28 @@ import org.slf4j.Logger;
/** Simple check of numeric values against expressions
* like < N, > N, between two values etc.
- * See {@link NumericValueCheckerTest} for examples.
+ * See {@link SimpleConstraintCheckerTest} for examples.
*/
-public class NumericValueChecker {
+public class SimpleConstraintChecker {
/** Check value against expression and report to logger */
- public void check(Object inputValue, String expression, Logger logger) {
+ public void check(Object inputValue, String constraint, Logger logger) {
final String stringValue = inputValue == null ? "" : inputValue.toString();
- if(expression == null || expression.trim().length() == 0) {
+ if(constraint == null || constraint.trim().length() == 0) {
// No expression, result will be based on a.getValue() logging only
return;
}
- final String [] parts = expression.split(" ");
+ final String [] parts = constraint.split(" ");
boolean matches = false;
try {
- if(expression.startsWith(">") && parts.length == 2) {
+ if(constraint.startsWith(">") && parts.length == 2) {
final int value = Integer.valueOf(stringValue).intValue();
matches = value > Integer.valueOf(parts[1]);
- } else if(expression.startsWith("<") && parts.length == 2) {
+ } else if(constraint.startsWith("<") && parts.length == 2) {
final int value = Integer.valueOf(stringValue).intValue();
matches = value < Integer.valueOf(parts[1]);
@@ -53,14 +53,16 @@ public class NumericValueChecker {
matches = value > lowerBound && value < upperBound;
} else {
- matches = expression.equals(stringValue);
+ matches = constraint.equals(stringValue);
}
} catch(NumberFormatException nfe) {
- logger.warn("Invalid numeric value [{}] while evaluating {}", inputValue, expression);
+ logger.warn("Invalid numeric value [{}] while evaluating {}", inputValue, constraint);
}
- if(!matches) {
- logger.warn("Value [{}] does not match expression [{}]", stringValue, expression);
+ if(matches) {
+ logger.debug("Value [{}] matches constraint [{}]", stringValue, constraint);
+ } else {
+ logger.warn("Value [{}] does not match constraint [{}]", stringValue, constraint);
}
}
}
\ No newline at end of file
Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultLogTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultLogTest.java?rev=1508833&r1=1508832&r2=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultLogTest.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultLogTest.java Wed Jul 31 12:47:45 2013
@@ -33,7 +33,7 @@ public class ResultLogTest {
@Test
public void testMaxLevel() {
- assertEquals(ResultLog.Level.DEBUG, resultLog.getMaxLevel());
+ assertEquals(null, resultLog.getMaxLevel());
resultLog.debug("something");
assertEquals(ResultLog.Level.DEBUG, resultLog.getMaxLevel());
resultLog.info("something");
Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultTest.java?rev=1508833&r1=1508832&r2=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultTest.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/api/ResultTest.java Wed Jul 31 12:47:45 2013
@@ -35,8 +35,8 @@ public class ResultTest {
}
@Test
- public void testInitiallyOk() {
- assertTrue(result.isOk());
+ public void testInitiallyNotOk() {
+ assertFalse(result.isOk());
}
@Test
Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java?rev=1508833&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java Wed Jul 31 12:47:45 2013
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.LoggerFactory;
+
+public class JmxAttributeHealthCheckTest {
+
+ private JmxAttributeHealthCheck hc;
+ private ResultLog log;
+ private Dictionary<String, String> props;
+ private ComponentContext ctx;
+
+ @Before
+ public void setup() {
+ final JmxAttributeHealthCheck c = new JmxAttributeHealthCheck();
+ hc = c;
+ log = new ResultLog(LoggerFactory.getLogger(getClass()));
+
+ ctx = Mockito.mock(ComponentContext.class);
+ props = new Hashtable<String, String>();
+ props.put(JmxAttributeHealthCheck.PROP_OBJECT_NAME, "java.lang:type=ClassLoading");
+ props.put(JmxAttributeHealthCheck.PROP_ATTRIBUTE_NAME, "LoadedClassCount");
+ Mockito.when(ctx.getProperties()).thenReturn(props);
+ }
+
+ @Test
+ public void testJmxAttributeMatch() {
+ props.put(JmxAttributeHealthCheck.PROP_CONSTRAINT, "> 10");
+ hc.activate(ctx);
+ final Result r = hc.execute(log);
+ assertTrue(r.isOk());
+ assertFalse(r.getLogEntries().isEmpty());
+ }
+
+ @Test
+ public void testJmxAttributeNoMatch() {
+ props.put(JmxAttributeHealthCheck.PROP_CONSTRAINT, "< 10");
+ hc.activate(ctx);
+ final Result r = hc.execute(log);
+ assertFalse(r.isOk());
+ assertFalse(r.getLogEntries().isEmpty());
+ }
+}
Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Copied: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java (from r1508827, sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/NumericValueCheckerTest.java)
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java?p2=sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java&p1=sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/NumericValueCheckerTest.java&r1=1508827&r2=1508833&rev=1508833&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/NumericValueCheckerTest.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java Wed Jul 31 12:47:45 2013
@@ -22,16 +22,16 @@ import static org.junit.Assert.assertTru
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.api.ResultLog;
-import org.apache.sling.hc.util.NumericValueChecker;
+import org.apache.sling.hc.util.SimpleConstraintChecker;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;
-public class NumericValueCheckerTest {
+public class SimpleConstraintCheckerTest {
private ResultLog resultLog;
private Result result;
- private final NumericValueChecker checker = new NumericValueChecker();
+ private final SimpleConstraintChecker checker = new SimpleConstraintChecker();
@Before
public void setup() {