Mercurial > stress-tester
changeset 588:c6bdc607c25b
reporter - standalone SNMP
author | Devel 1 |
---|---|
date | Tue, 03 Oct 2017 10:15:24 +0200 |
parents | 3117f4933528 |
children | 0c51d5e3e624 |
files | stress-tester-reporter/src/main/java/com/passus/st/reporter/SnmpLogger.java stress-tester-reporter/src/main/java/com/passus/st/reporter/server/ServerMain.java 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 stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/ServerMain.java |
diffstat | 5 files changed, 264 insertions(+), 220 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/SnmpLogger.java Tue Oct 03 09:47:27 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,179 +0,0 @@ -package com.passus.st.reporter; - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; - -import org.snmp4j.CommunityTarget; -import org.snmp4j.PDU; -import org.snmp4j.Snmp; -import org.snmp4j.Target; -import org.snmp4j.TransportMapping; -import org.snmp4j.event.ResponseEvent; -import org.snmp4j.mp.SnmpConstants; -import org.snmp4j.smi.Address; -import org.snmp4j.smi.GenericAddress; -import org.snmp4j.smi.OID; -import org.snmp4j.smi.OctetString; -import org.snmp4j.smi.VariableBinding; -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"); - private OID oid_ramFree = new OID(".1.3.6.1.4.1.2021.4.6.0"); - 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 Snmp snmp = null; - private String address = null; - private String community = "passus"; - private PrintWriter file = null; - private boolean initialized = false; - private StringBuilder builder; - private ScheduledFuture<?> scheduleHandle; - - public boolean isInitialized() { - return initialized; - } - - 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 - */ - public SnmpLogger(final String add, final String comm, final int period) { - - address = add; - community = comm; - try { - file = new PrintWriter("snmp.csv", "UTF-8"); - this.initialize(); - this.initialized = true; - this.builder = new StringBuilder(200); - scheduleHandle = scheduler.scheduleAtFixedRate(this, 1, period, TimeUnit.SECONDS); - } catch (IOException ex) { - System.err.println(ex); - } - - } - - @Override - public void run(){ - try { - builder = new StringBuilder(200); - builder.append(System.currentTimeMillis()); - builder.append(";"); - String cpuLoad1m = getAsString(oid_cpuLoad1m); - String cpuLoad5m = getAsString(oid_cpuLoad5m); - String cpuLoad15m = getAsString(oid_cpuLoad15m); - String ramFree = getAsString(oid_ramFree); - String swapFree = getAsString(oid_swapFree); - String ramTotal = getAsString(oid_ramTotal); - String swapTotal = getAsString(oid_swapTotal); - builder.append(cpuLoad1m); - builder.append(";"); - builder.append(cpuLoad5m); - builder.append(";"); - builder.append(cpuLoad15m); - builder.append(";"); - builder.append(Integer.parseInt(ramTotal) - Integer.parseInt(ramFree)); - builder.append(";"); - builder.append(Integer.parseInt(swapTotal) - Integer.parseInt(swapFree)); - builder.append("\n"); - - file.write(builder.toString()); - file.flush(); - } catch (IOException ex) { - System.err.println(ex); - } - - } - - /** - * Start the Snmp session. - * - * @throws IOException - */ - private void initialize() throws IOException { - TransportMapping transport = new DefaultUdpTransportMapping(); - snmp = new Snmp(transport); - transport.listen(); - } - - public void close() { - try { - scheduleHandle.cancel(true); - - snmp.close(); - file.close(); - } catch (IOException ex) { - System.err.println(ex); - } - } - - /** - * Method which takes a single OID and returns the response from the agent - * as a String. - * - * @param oid - * @return - * @throws IOException - */ - public String getAsString(OID oid) throws IOException { - ResponseEvent event = get(oid); - return event.getResponse().get(0).getVariable().toString(); - } - - /** - * This method is capable of handling multiple OIDs - * - * @param oids - * @return - * @throws IOException - */ - public ResponseEvent get(OID... oids) throws IOException { - PDU pdu = new PDU(); - for (OID oid : oids) { - pdu.add(new VariableBinding(oid)); - } - pdu.setType(PDU.GET); - ResponseEvent event = snmp.send(pdu, getTarget(), null); - if (event != null) { - return event; - } - throw new RuntimeException("GET timed out"); - } - - /** - * This method returns a Target, which contains information about where the - * data should be fetched and how. - * - * @return - */ - private Target getTarget() { - Address targetAddress = GenericAddress.parse(address); - CommunityTarget target = new CommunityTarget(); - target.setCommunity(new OctetString(community)); - target.setAddress(targetAddress); - target.setRetries(2); - target.setTimeout(1500); - target.setVersion(SnmpConstants.version2c); - return target; - } - -}
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/server/ServerMain.java Tue Oct 03 09:47:27 2017 +0200 +++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/server/ServerMain.java Tue Oct 03 10:15:24 2017 +0200 @@ -1,8 +1,10 @@ package com.passus.st.reporter.server; import com.passus.st.reporter.ReporterImpl; -import com.passus.st.reporter.SnmpLogger; +import com.passus.st.reporter.snmp.SnmpLogger; import com.passus.st.reporter.protocol.Reporter; +import static com.passus.st.reporter.snmp.Main.addSnmpOptions; +import static com.passus.st.reporter.snmp.Main.create; import com.passus.st.utils.CliUtils; import static com.passus.st.utils.CliUtils.option; import java.io.IOException; @@ -15,6 +17,7 @@ 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; /** * @@ -24,35 +27,10 @@ static final int PORT = 11111; - public static SnmpLogger create(CommandLine cl) throws ParseException { - if (cl.hasOption("s")) { - // SNMP listener is enabled - 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); - } 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."); - } - return null; - } - public static void main(String[] args) throws IOException { final Options options = new Options(); - options.addOption(option("s", "snmp").desc("Collect SNMP metrics.") - .hasArg().argName("snmpAddr") - .build() - ); - options.addOption(option("co", "snmpCommunity").desc("SNMP Community.") - .hasArg().argName("snmpCommunity") - .build() - ); - options.addOption(option("p", "snmpPeriod").desc("Period in seconds of consequent SNMP readings.") - .hasArg().argName("snmpPeriod") - .build() - ); + addSnmpOptions(options); options.addOption(option("m", "merged").desc("Write file with merged request and response data") .hasArg(false)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/Main.java Tue Oct 03 10:15:24 2017 +0200 @@ -0,0 +1,74 @@ +package com.passus.st.reporter.snmp; + +import com.passus.st.utils.CliUtils; +import static com.passus.st.utils.CliUtils.option; +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; + +/** + * + * @author mikolaj.podbielski + */ +public class Main { + + public static SnmpLogger create(CommandLine cl) throws ParseException { + if (cl.hasOption("s")) { + // SNMP listener is enabled + 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); + } 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."); + } + return null; + } + + public static void addSnmpOptions(Options options) { + options.addOption(option("s", "snmp").desc("Collect SNMP metrics.") + .hasArg().argName("snmpAddr") + .build() + ); + options.addOption(option("co", "snmpCommunity").desc("SNMP Community.") + .hasArg().argName("snmpCommunity") + .build() + ); + options.addOption(option("p", "snmpPeriod").desc("Period in seconds of consequent SNMP readings.") + .hasArg().argName("snmpPeriod") + .build() + ); + } + + public static void main(String[] args) { + final Options options = new Options(); + + addSnmpOptions(options); + + SnmpLogger snmp; + try { + CommandLine cl = new DefaultParser().parse(options, args); + if (cl.getArgs().length != 0) { + CliUtils.printHelp(options); + return; + } + snmp = create(cl); + System.out.println("SNMP starter"); + } catch (ParseException ex) { + System.out.println(ex.getMessage()); + CliUtils.printHelp(options); + return; + } + + Runnable shutdown = () -> { + snmp.close(); + System.out.println("SNMP stopped"); + }; + Runtime.getRuntime().addShutdownHook(new Thread(shutdown)); + + System.out.println("ready"); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/snmp/SnmpLogger.java Tue Oct 03 10:15:24 2017 +0200 @@ -0,0 +1,179 @@ +package com.passus.st.reporter.snmp; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import org.snmp4j.CommunityTarget; +import org.snmp4j.PDU; +import org.snmp4j.Snmp; +import org.snmp4j.Target; +import org.snmp4j.TransportMapping; +import org.snmp4j.event.ResponseEvent; +import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.smi.Address; +import org.snmp4j.smi.GenericAddress; +import org.snmp4j.smi.OID; +import org.snmp4j.smi.OctetString; +import org.snmp4j.smi.VariableBinding; +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"); + private OID oid_ramFree = new OID(".1.3.6.1.4.1.2021.4.6.0"); + 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 Snmp snmp = null; + private String address = null; + private String community = "passus"; + private PrintWriter file = null; + private boolean initialized = false; + private StringBuilder builder; + private ScheduledFuture<?> scheduleHandle; + + public boolean isInitialized() { + return initialized; + } + + 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 + */ + public SnmpLogger(final String add, final String comm, final int period) { + + address = add; + community = comm; + try { + file = new PrintWriter("snmp.csv", "UTF-8"); + this.initialize(); + this.initialized = true; + this.builder = new StringBuilder(200); + scheduleHandle = scheduler.scheduleAtFixedRate(this, 1, period, TimeUnit.SECONDS); + } catch (IOException ex) { + System.err.println(ex); + } + + } + + @Override + public void run(){ + try { + builder = new StringBuilder(200); + builder.append(System.currentTimeMillis()); + builder.append(";"); + String cpuLoad1m = getAsString(oid_cpuLoad1m); + String cpuLoad5m = getAsString(oid_cpuLoad5m); + String cpuLoad15m = getAsString(oid_cpuLoad15m); + String ramFree = getAsString(oid_ramFree); + String swapFree = getAsString(oid_swapFree); + String ramTotal = getAsString(oid_ramTotal); + String swapTotal = getAsString(oid_swapTotal); + builder.append(cpuLoad1m); + builder.append(";"); + builder.append(cpuLoad5m); + builder.append(";"); + builder.append(cpuLoad15m); + builder.append(";"); + builder.append(Integer.parseInt(ramTotal) - Integer.parseInt(ramFree)); + builder.append(";"); + builder.append(Integer.parseInt(swapTotal) - Integer.parseInt(swapFree)); + builder.append("\n"); + + file.write(builder.toString()); + file.flush(); + } catch (IOException ex) { + System.err.println(ex); + } + + } + + /** + * Start the Snmp session. + * + * @throws IOException + */ + private void initialize() throws IOException { + TransportMapping transport = new DefaultUdpTransportMapping(); + snmp = new Snmp(transport); + transport.listen(); + } + + public void close() { + try { + scheduleHandle.cancel(true); + + snmp.close(); + file.close(); + } catch (IOException ex) { + System.err.println(ex); + } + } + + /** + * Method which takes a single OID and returns the response from the agent + * as a String. + * + * @param oid + * @return + * @throws IOException + */ + public String getAsString(OID oid) throws IOException { + ResponseEvent event = get(oid); + return event.getResponse().get(0).getVariable().toString(); + } + + /** + * This method is capable of handling multiple OIDs + * + * @param oids + * @return + * @throws IOException + */ + public ResponseEvent get(OID... oids) throws IOException { + PDU pdu = new PDU(); + for (OID oid : oids) { + pdu.add(new VariableBinding(oid)); + } + pdu.setType(PDU.GET); + ResponseEvent event = snmp.send(pdu, getTarget(), null); + if (event != null) { + return event; + } + throw new RuntimeException("GET timed out"); + } + + /** + * This method returns a Target, which contains information about where the + * data should be fetched and how. + * + * @return + */ + private Target getTarget() { + Address targetAddress = GenericAddress.parse(address); + CommunityTarget target = new CommunityTarget(); + target.setCommunity(new OctetString(community)); + target.setAddress(targetAddress); + target.setRetries(2); + target.setTimeout(1500); + target.setVersion(SnmpConstants.version2c); + return target; + } + +}
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/ServerMain.java Tue Oct 03 09:47:27 2017 +0200 +++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/ServerMain.java Tue Oct 03 10:15:24 2017 +0200 @@ -1,7 +1,9 @@ package com.passus.st.reporter.trx; import com.passus.st.reporter.ReporterImpl; -import com.passus.st.reporter.SnmpLogger; +import static com.passus.st.reporter.snmp.Main.addSnmpOptions; +import static com.passus.st.reporter.snmp.Main.create; +import com.passus.st.reporter.snmp.SnmpLogger; import com.passus.st.utils.CliUtils; import static com.passus.st.utils.CliUtils.option; import java.io.IOException; @@ -10,6 +12,7 @@ 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; /** * @@ -22,18 +25,7 @@ public static void main(String[] args) throws IOException { final Options options = new Options(); - options.addOption(option("s", "snmp").desc("Collect SNMP metrics.") - .hasArg().argName("snmpAddr") - .build() - ); - options.addOption(option("co", "snmpCommunity").desc("SNMP Community.") - .hasArg().argName("snmpCommunity") - .build() - ); - options.addOption(option("p", "snmpPeriod").desc("Period in seconds of consequent SNMP readings.") - .hasArg().argName("snmpPeriod") - .build() - ); + addSnmpOptions(options); options.addOption(option("m", "merged").desc("Write file with merged request and response data") .hasArg(false) @@ -48,7 +40,7 @@ CliUtils.printHelp(options); return; } - snmp = com.passus.st.reporter.server.ServerMain.create(cl); + snmp = create(cl); merge = cl.hasOption("m"); } catch (ParseException ex) { System.out.println(ex.getMessage());