changeset 1056:c0ada9453f53

NetflowMetric
author Devel 2
date Wed, 15 Apr 2020 11:56:03 +0200
parents 2f945e8445a0
children b3d44dd719f1
files stress-tester/src/main/java/com/passus/st/client/AbstractFlowHandler.java stress-tester/src/main/java/com/passus/st/client/FlowHandler.java stress-tester/src/main/java/com/passus/st/client/netflow/NetflowFlowHandler.java stress-tester/src/main/java/com/passus/st/client/netflow/NetflowMetric.java
diffstat 4 files changed, 79 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/AbstractFlowHandler.java	Wed Apr 15 11:46:21 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/AbstractFlowHandler.java	Wed Apr 15 11:56:03 2020 +0200
@@ -41,9 +41,5 @@
             }
         }
     }
-
-    @Override
-    public void init(FlowContext flowContext) {
-
-    }
+    
 }
--- a/stress-tester/src/main/java/com/passus/st/client/FlowHandler.java	Wed Apr 15 11:46:21 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/FlowHandler.java	Wed Apr 15 11:56:03 2020 +0200
@@ -14,7 +14,7 @@
 
     int getProtocolId();
 
-    void init(FlowContext flowContext);
+    default void init(FlowContext flowContext) {};
 
     FlowHandlerDataDecoder getResponseDecoder(FlowContext flowContext);
 
--- a/stress-tester/src/main/java/com/passus/st/client/netflow/NetflowFlowHandler.java	Wed Apr 15 11:46:21 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/netflow/NetflowFlowHandler.java	Wed Apr 15 11:56:03 2020 +0200
@@ -3,22 +3,20 @@
 import com.passus.commons.Assert;
 import com.passus.commons.time.TimeAware;
 import com.passus.commons.time.TimeGenerator;
+import com.passus.net.netflow.Netflow;
+import com.passus.st.client.AbstractFlowHandler;
 import com.passus.st.client.FlowContext;
-import com.passus.st.client.FlowHandler;
 import com.passus.st.client.FlowHandlerDataDecoder;
 import com.passus.st.client.FlowHandlerDataEncoder;
-import com.passus.st.metric.MetricsContainer;
 
 import static com.passus.st.Protocols.NETFLOW;
 
-public class NetflowFlowHandler implements FlowHandler, TimeAware {
+public class NetflowFlowHandler extends AbstractFlowHandler<NetflowMetric> implements TimeAware {
 
     private final NetflowFlowHandlerDataEncoder encoder;
 
     TimeGenerator timeGenerator = TimeGenerator.getDefaultGenerator();
 
-    boolean collectMetrics = false;
-
     public NetflowFlowHandler() {
         this.encoder = new NetflowFlowHandlerDataEncoder();
     }
@@ -29,13 +27,8 @@
     }
 
     @Override
-    public boolean isCollectMetrics() {
-        return collectMetrics;
-    }
-
-    @Override
-    public void setCollectMetrics(boolean collectMetrics) {
-        this.collectMetrics = collectMetrics;
+    protected NetflowMetric createMetric() {
+        return new NetflowMetric();
     }
 
     @Override
@@ -49,11 +42,6 @@
     }
 
     @Override
-    public void writeMetrics(MetricsContainer container) {
-
-    }
-
-    @Override
     public TimeGenerator getTimeGenerator() {
         return timeGenerator;
     }
@@ -69,5 +57,13 @@
         flowContext.setBidirectional(false);
     }
 
-
+    @Override
+    public void onRequestSent(Object request, FlowContext flowContext) {
+        if (collectMetrics) {
+            Netflow netflow = (Netflow) request;
+            synchronized (metric) {
+                metric.addNetflow(netflow);
+            }
+        }
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/netflow/NetflowMetric.java	Wed Apr 15 11:56:03 2020 +0200
@@ -0,0 +1,63 @@
+package com.passus.st.client.netflow;
+
+import com.passus.commons.metric.Metric;
+import com.passus.net.netflow.*;
+import com.passus.st.client.GenericMetric;
+
+public class NetflowMetric extends GenericMetric {
+
+    public static final String DEFAULT_NAME = "Netflow";
+
+    private int v5flows;
+
+    private int v9flows;
+
+    private int v10flows;
+
+    public NetflowMetric() {
+        this(DEFAULT_NAME);
+    }
+
+    public NetflowMetric(String name) {
+        super(name);
+        attrs.put("v5flows", v5flows);
+        attrs.put("v5flows", v5flows);
+        attrs.put("v10flows", v10flows);
+    }
+
+    public void addNetflow(Netflow netflow) {
+        try {
+            switch (netflow.getVersion()) {
+                case NetflowUtils.VERSION_5:
+                    Netflow5 netflow5 = (Netflow5) netflow;
+                    v5flows += netflow5.getPdus().length;
+                    break;
+                case NetflowUtils.VERSION_9:
+                    Netflow9 netflow9 = (Netflow9) netflow;
+                    v9flows += netflow9.getFlowSets().size();
+                    break;
+                case NetflowUtils.VERSION_10:
+                    Netflow10 netflow10 = (Netflow10) netflow;
+                    v9flows += netflow10.getFlowSets().size();
+                    break;
+            }
+        } catch (Exception ignore) {
+
+        }
+    }
+
+    @Override
+    public void update(Metric metric) {
+        NetflowMetric netflowMetric = (NetflowMetric) metric;
+        netflowMetric.v5flows += v5flows;
+        netflowMetric.v9flows += v9flows;
+        netflowMetric.v10flows += v10flows;
+    }
+
+    @Override
+    public void reset() {
+        v5flows = 0;
+        v9flows = 0;
+        v10flows = 0;
+    }
+}