changeset 590:bddfc1384d32

reporter - standalone SNMP - configurable output dir
author Devel 1
date Tue, 03 Oct 2017 10:59:48 +0200
parents 0c51d5e3e624
children 87ed37e4d18f
files stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/Main.java stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/SnmpLogger.java
diffstat 2 files changed, 32 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/Main.java	Tue Oct 03 10:37:22 2017 +0200
+++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/Main.java	Tue Oct 03 10:59:48 2017 +0200
@@ -2,11 +2,11 @@
 
 import com.passus.st.utils.CliUtils;
 import static com.passus.st.utils.CliUtils.option;
+import java.io.File;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
-import static com.passus.st.utils.CliUtils.option;
 
 /**
  *
@@ -20,7 +20,8 @@
             String snmpAddr = cl.getOptionValue("s");
             String snmpCommunity = cl.hasOption("co") ? cl.getOptionValue("co") : "passus";
             int snmpPeriod = cl.hasOption("p") ? Integer.parseUnsignedInt(cl.getOptionValue("p")) : 5;
-            return new SnmpLogger(snmpAddr, snmpCommunity, snmpPeriod);
+            File directory = cl.hasOption("od") ? new File(cl.getOptionValue("od")) : null;
+            return new SnmpLogger(snmpAddr, snmpCommunity, snmpPeriod, directory);
         } else if (cl.hasOption("co") || cl.hasOption("p")) {
             // Community option without SNMP option specified
             throw new ParseException("Options <snmpCommunity> and <snmpPeriod> require to specify SNMP Address.");
@@ -41,6 +42,10 @@
                 .hasArg().argName("snmpPeriod")
                 .build()
         );
+        options.addOption(option("od", "outputDirectory").desc("Output directory")
+                .hasArg().argName("directory")
+                .build()
+        );
     }
 
     public static void main(String[] args) {
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/SnmpLogger.java	Tue Oct 03 10:37:22 2017 +0200
+++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/SnmpLogger.java	Tue Oct 03 10:59:48 2017 +0200
@@ -1,5 +1,6 @@
 package com.passus.st.reporter.snmp;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.concurrent.Executors;
@@ -22,6 +23,7 @@
 import org.snmp4j.transport.DefaultUdpTransportMapping;
 
 public class SnmpLogger implements Runnable {
+
     private OID oid_cpuLoad1m = new OID(".1.3.6.1.4.1.2021.10.1.3.1");
     private OID oid_cpuLoad5m = new OID(".1.3.6.1.4.1.2021.10.1.3.2");
     private OID oid_cpuLoad15m = new OID(".1.3.6.1.4.1.2021.10.1.3.3");
@@ -29,18 +31,18 @@
     private OID oid_ramTotal = new OID(".1.3.6.1.4.1.2021.4.5.0");
     private OID oid_swapFree = new OID(".1.3.6.1.4.1.2021.4.4.0");
     private OID oid_swapTotal = new OID(".1.3.6.1.4.1.2021.4.3.0");
-    
-    private final ScheduledExecutorService scheduler = 
-       Executors.newScheduledThreadPool(1);
-    
+
+    private final ScheduledExecutorService scheduler
+            = Executors.newScheduledThreadPool(1);
+
     private Snmp snmp = null;
     private String address = null;
     private String community = "passus";
-    private PrintWriter file = null;
+    private PrintWriter out = null;
     private boolean initialized = false;
     private StringBuilder builder;
     private ScheduledFuture<?> scheduleHandle;
-    
+
     public boolean isInitialized() {
         return initialized;
     }
@@ -48,20 +50,24 @@
     public void setInitialized(boolean initialized) {
         this.initialized = initialized;
     }
-    
+
     /**
      * Constructor
      *
      * @param add must be quasi url, (ex. udp:172.16.60.101/161)
      * @param comm name of the SNMP community
      * @param period
+     * @param directory output directory
      */
-    public SnmpLogger(final String add, final String comm, final int period) {
-        
+    public SnmpLogger(final String add, final String comm, final int period, File directory) {
         address = add;
         community = comm;
         try {
-            file = new PrintWriter("snmp.csv", "UTF-8");
+            if (!directory.exists()) {
+                directory.mkdirs();
+            }
+            File file = new File(directory, "snmp.csv");
+            out = new PrintWriter(file, "UTF-8");
             this.initialize();
             this.initialized = true;
             this.builder = new StringBuilder(200);
@@ -69,11 +75,10 @@
         } catch (IOException ex) {
             System.err.println(ex);
         }
-        
     }
 
     @Override
-    public void run(){
+    public void run() {
         try {
             builder = new StringBuilder(200);
             builder.append(System.currentTimeMillis());
@@ -95,13 +100,13 @@
             builder.append(";");
             builder.append(Integer.parseInt(swapTotal) - Integer.parseInt(swapFree));
             builder.append("\n");
-            
-            file.write(builder.toString());
-            file.flush();
+
+            out.write(builder.toString());
+            out.flush();
         } catch (IOException ex) {
             System.err.println(ex);
         }
-        
+
     }
 
     /**
@@ -114,17 +119,17 @@
         snmp = new Snmp(transport);
         transport.listen();
     }
-    
+
     public void close() {
         try {
             scheduleHandle.cancel(true);
-        
+
             snmp.close();
-            file.close();
+            out.close();
         } catch (IOException ex) {
             System.err.println(ex);
         }
-    } 
+    }
 
     /**
      * Method which takes a single OID and returns the response from the agent