You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2015/07/02 17:19:25 UTC

[1/3] incubator-brooklyn git commit: Adds sensor path to performance counter output

Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master b0243f005 -> 5d60b2c81


Adds sensor path to performance counter output


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/280baa7a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/280baa7a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/280baa7a

Branch: refs/heads/master
Commit: 280baa7adac01e41b6c01cd4bb44adc0df9991bb
Parents: 0cd634d
Author: Martin Harris <gi...@nakomis.com>
Authored: Mon Jun 29 13:53:23 2015 +0100
Committer: Martin Harris <gi...@nakomis.com>
Committed: Mon Jun 29 13:53:23 2015 +0100

----------------------------------------------------------------------
 .../windows/WindowsPerformanceCounterFeed.java  | 54 +++++++++++++++-----
 1 file changed, 42 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/280baa7a/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java b/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
index a64d2e6..8b6b007 100644
--- a/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
+++ b/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
@@ -97,6 +97,7 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed {
     protected static final Pattern lineWithPerfData = Pattern.compile("^\"[\\d:/\\-. ]+\",\".*\"$", Pattern.MULTILINE);
     private static final Joiner JOINER_ON_SPACE = Joiner.on(' ');
     private static final Joiner JOINER_ON_COMMA = Joiner.on(',');
+    private static final int OUTPUT_COLUMN_WIDTH = 100;
 
     @SuppressWarnings("serial")
     public static final ConfigKey<Collection<WindowsPerformanceCounterPollConfig<?>>> POLLS = ConfigKeys.newConfigKey(
@@ -188,19 +189,28 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed {
         }
         
         Iterable<String> allParams = ImmutableList.<String>builder()
-                .add("Get-Counter")
+                .add("(Get-Counter")
                 .add("-Counter")
                 .add(JOINER_ON_COMMA.join(Iterables.transform(performanceCounterNames, QuoteStringFunction.INSTANCE)))
                 .add("-SampleInterval")
                 .add("2") // TODO: extract SampleInterval as a config key
+                .add(").CounterSamples")
+                .add("|")
+                .add("Format-Table")
+                .add(String.format("@{Expression={$_.Path};width=%d},@{Expression={$_.CookedValue};width=%<d}", OUTPUT_COLUMN_WIDTH))
+                .add("-HideTableHeaders")
+                .add("|")
+                .add("Out-String")
+                .add("-Width")
+                .add(String.valueOf(OUTPUT_COLUMN_WIDTH * 2))
                 .build();
-        String command = String.format("(%s).CounterSamples.CookedValue", JOINER_ON_SPACE.join(allParams));
+        String command = JOINER_ON_SPACE.join(allParams);
         log.debug("Windows performance counter poll command for {} will be: {}", entity, command);
 
-        GetPerformanceCountersJob<WinRmToolResponse> job = new GetPerformanceCountersJob<WinRmToolResponse>(getEntity(), command);
+        GetPerformanceCountersJob<WinRmToolResponse> job = new GetPerformanceCountersJob(getEntity(), command);
         getPoller().scheduleAtFixedRate(
-                new CallInEntityExecutionContext<WinRmToolResponse>(entity, job),
-                new SendPerfCountersToSensors(getEntity(), getConfig(POLLS)), 
+                new CallInEntityExecutionContext(entity, job),
+                new SendPerfCountersToSensors(getEntity(), polls),
                 minPeriod);
     }
 
@@ -254,6 +264,7 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed {
         private final EntityLocal entity;
         private final List<WindowsPerformanceCounterPollConfig<?>> polls;
         private final Set<AttributeSensor<?>> failedAttributes = Sets.newLinkedHashSet();
+        private static final Pattern MACHINE_NAME_LOOKBACK_PATTERN = Pattern.compile(String.format("(?<=\\\\\\\\.{0,%d})\\\\.*", OUTPUT_COLUMN_WIDTH));
         
         public SendPerfCountersToSensors(EntityLocal entity, Collection<WindowsPerformanceCounterPollConfig<?>> polls) {
             this.entity = entity;
@@ -262,7 +273,7 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed {
 
         @Override
         public boolean checkSuccess(WinRmToolResponse val) {
-            // TODO not just using statucCode; also looking at absence of stderr.
+            // TODO not just using statusCode; also looking at absence of stderr.
             // Status code is (empirically) unreliable: it returns 0 sometimes even when failed 
             // (but never returns non-zero on success).
             if (val.getStatusCode() != 0) return false;
@@ -275,20 +286,30 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed {
 
         @Override
         public void onSuccess(WinRmToolResponse val) {
-            String[] values = val.getStdOut().split("\r\n");
-            for (int i = 0; i < polls.size(); i++) {
-                WindowsPerformanceCounterPollConfig<?> config = polls.get(i);
+            for (String pollResponse : val.getStdOut().split("\r\n")) {
+                if (Strings.isNullOrEmpty(pollResponse)) {
+                    continue;
+                }
+                String path = pollResponse.substring(0, OUTPUT_COLUMN_WIDTH - 1);
+                // The performance counter output prepends the sensor name with "\\<machinename>" so we need to remove it
+                Matcher machineNameLookbackMatcher = MACHINE_NAME_LOOKBACK_PATTERN.matcher(path);
+                if (!machineNameLookbackMatcher.find()) {
+                    continue;
+                }
+                String name = machineNameLookbackMatcher.group(0).trim();
+                String rawValue = pollResponse.substring(OUTPUT_COLUMN_WIDTH).replaceAll("^\\s+", "");
+                WindowsPerformanceCounterPollConfig<?> config = getPollConfig(name);
                 Class<?> clazz = config.getSensor().getType();
                 AttributeSensor<Object> attribute = (AttributeSensor<Object>) Sensors.newSensor(clazz, config.getSensor().getName(), config.getDescription());
                 try {
-                    Object value = TypeCoercions.coerce(values[i], TypeToken.of(clazz));
+                    Object value = TypeCoercions.coerce(rawValue, TypeToken.of(clazz));
                     entity.setAttribute(attribute, value);
                 } catch (Exception e) {
                     Exceptions.propagateIfFatal(e);
                     if (failedAttributes.add(attribute)) {
-                        log.warn("Failed to coerce value '{}' to {} for {} -> {}", new Object[] {values[i], clazz, entity, attribute});
+                        log.warn("Failed to coerce value '{}' to {} for {} -> {}", new Object[] {rawValue, clazz, entity, attribute});
                     } else {
-                        if (log.isTraceEnabled()) log.trace("Failed (repeatedly) to coerce value '{}' to {} for {} -> {}", new Object[] {values[i], clazz, entity, attribute});
+                        if (log.isTraceEnabled()) log.trace("Failed (repeatedly) to coerce value '{}' to {} for {} -> {}", new Object[] {rawValue, clazz, entity, attribute});
                     }
                 }
             }
@@ -323,6 +344,15 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed {
         public String toString() {
             return super.toString()+"["+getDescription()+"]";
         }
+
+        private WindowsPerformanceCounterPollConfig<?> getPollConfig(String sensorName) {
+            for (WindowsPerformanceCounterPollConfig<?> poll : polls) {
+                if (poll.getPerformanceCounterName().equalsIgnoreCase(sensorName)) {
+                    return poll;
+                }
+            }
+            throw new IllegalStateException(String.format("%s not found in configured polls: %s", sensorName, polls));
+        }
     }
 
     static class PerfCounterValueIterator implements Iterator<String> {


[2/3] incubator-brooklyn git commit: Adds test for WindowsPerformanceCounterFeed.SendPerfCountersToSensors.onSuccess

Posted by al...@apache.org.
Adds test for WindowsPerformanceCounterFeed.SendPerfCountersToSensors.onSuccess


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/a85fb3c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/a85fb3c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/a85fb3c9

Branch: refs/heads/master
Commit: a85fb3c9af4090ad8ec8729e870712f29d5b03ee
Parents: 280baa7
Author: Martin Harris <gi...@nakomis.com>
Authored: Mon Jun 29 16:48:13 2015 +0100
Committer: Martin Harris <gi...@nakomis.com>
Committed: Mon Jun 29 16:48:13 2015 +0100

----------------------------------------------------------------------
 .../windows/WindowsPerformanceCounterFeed.java  |  4 +-
 .../WindowsPerformanceCounterFeedTest.java      | 83 +++++++++++++++++++-
 2 files changed, 83 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a85fb3c9/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java b/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
index 8b6b007..5278ff9 100644
--- a/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
+++ b/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java
@@ -54,6 +54,7 @@ import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.flags.TypeCoercions;
 import brooklyn.util.time.Duration;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Strings;
@@ -260,7 +261,8 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed {
         }
     }
 
-    private static class SendPerfCountersToSensors implements PollHandler<WinRmToolResponse> {
+    @VisibleForTesting
+    static class SendPerfCountersToSensors implements PollHandler<WinRmToolResponse> {
         private final EntityLocal entity;
         private final List<WindowsPerformanceCounterPollConfig<?>> polls;
         private final Set<AttributeSensor<?>> failedAttributes = Sets.newLinkedHashSet();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a85fb3c9/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java b/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java
index 4b2ed91..f4ba43b 100644
--- a/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java
+++ b/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java
@@ -18,15 +18,54 @@
  */
 package brooklyn.event.feed.windows;
 
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Collection;
+import java.util.Iterator;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import java.util.Iterator;
+import brooklyn.entity.BrooklynAppUnitTestSupport;
+import brooklyn.entity.basic.EntityLocal;
+import brooklyn.entity.proxying.EntitySpec;
+import brooklyn.event.AttributeSensor;
+import brooklyn.event.basic.Sensors;
+import brooklyn.location.Location;
+import brooklyn.location.basic.LocalhostMachineProvisioningLocation;
+import brooklyn.test.EntityTestUtils;
+import brooklyn.test.entity.TestEntity;
+import brooklyn.util.text.Strings;
+import io.cloudsoft.winrm4j.winrm.WinRmToolResponse;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 
-import static org.testng.Assert.*;
+public class WindowsPerformanceCounterFeedTest extends BrooklynAppUnitTestSupport {
 
-public class WindowsPerformanceCounterFeedTest {
+    private Location loc;
+    private EntityLocal entity;
+
+    @BeforeMethod(alwaysRun=true)
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        loc = new LocalhostMachineProvisioningLocation();
+        entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        app.start(ImmutableList.of(loc));
+    }
+
+    @AfterMethod(alwaysRun=true)
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
 
     private static final Logger log = LoggerFactory.getLogger(WindowsPerformanceCounterFeedTest.class);
 
@@ -50,4 +89,42 @@ public class WindowsPerformanceCounterFeedTest {
         assertFalse(iterator.hasNext());
     }
 
+    @Test
+    public void testSendPerfCountersToSensors() {
+        AttributeSensor<String> stringSensor = Sensors.newStringSensor("foo.bar");
+        AttributeSensor<Integer> integerSensor = Sensors.newIntegerSensor("bar.baz");
+        AttributeSensor<Double> doubleSensor = Sensors.newDoubleSensor("baz.quux");
+
+        Collection<WindowsPerformanceCounterPollConfig<?>> polls = ImmutableSet.<WindowsPerformanceCounterPollConfig<?>>of(
+                new WindowsPerformanceCounterPollConfig(stringSensor).performanceCounterName("\\processor information(_total)\\% processor time"),
+                new WindowsPerformanceCounterPollConfig(integerSensor).performanceCounterName("\\integer.sensor"),
+                new WindowsPerformanceCounterPollConfig(doubleSensor).performanceCounterName("\\double\\sensor\\with\\multiple\\sub\\paths")
+        );
+
+        WindowsPerformanceCounterFeed.SendPerfCountersToSensors sendPerfCountersToSensors = new WindowsPerformanceCounterFeed.SendPerfCountersToSensors(entity, polls);
+
+        assertNull(entity.getAttribute(stringSensor));
+
+        StringBuilder responseBuilder = new StringBuilder();
+        // NOTE: This builds the response in a different order to which they are passed to the SendPerfCountersToSensors constructor
+        // this tests that the values are applied correctly even if the (possibly non-deterministic) order in which
+        // they are returned by the Get-Counter scriptlet is different
+        addMockResponse(responseBuilder, "\\\\machine.name\\double\\sensor\\with\\multiple\\sub\\paths", "3.1415926");
+        addMockResponse(responseBuilder, "\\\\win-lge7uj2blau\\processor information(_total)\\% processor time", "99.9");
+        addMockResponse(responseBuilder, "\\\\machine.name\\integer.sensor", "15");
+
+        sendPerfCountersToSensors.onSuccess(new WinRmToolResponse(responseBuilder.toString(), "", 0));
+
+        EntityTestUtils.assertAttributeEquals(entity, stringSensor, "99.9");
+        EntityTestUtils.assertAttributeEquals(entity, integerSensor, 15);
+        EntityTestUtils.assertAttributeEquals(entity, doubleSensor, 3.1415926);
+    }
+
+    private void addMockResponse(StringBuilder responseBuilder, String path, String value) {
+        responseBuilder.append(path);
+        responseBuilder.append(Strings.repeat(" ", 200 - (path.length() + value.length())));
+        responseBuilder.append(value);
+        responseBuilder.append("\r\n");
+    }
+
 }


[3/3] incubator-brooklyn git commit: This closes #726

Posted by al...@apache.org.
This closes #726


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/5d60b2c8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/5d60b2c8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/5d60b2c8

Branch: refs/heads/master
Commit: 5d60b2c810a615de8e15a84bb091410390cd9fd3
Parents: b0243f0 a85fb3c
Author: Aled Sage <al...@gmail.com>
Authored: Thu Jul 2 16:19:03 2015 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Jul 2 16:19:03 2015 +0100

----------------------------------------------------------------------
 .../windows/WindowsPerformanceCounterFeed.java  | 58 +++++++++++---
 .../WindowsPerformanceCounterFeedTest.java      | 83 +++++++++++++++++++-
 2 files changed, 125 insertions(+), 16 deletions(-)
----------------------------------------------------------------------