0% found this document useful (0 votes)
11 views

NSM Lab Arguments

The document describes a Java program that calculates the Uniformly Weighted Moving Average (UWMA) of SNMP counters polled from a device. It uses SNMP to periodically fetch the values of several OIDs, calculates the UWMA over increasing window sizes, and prints the results.

Uploaded by

Shalini Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

NSM Lab Arguments

The document describes a Java program that calculates the Uniformly Weighted Moving Average (UWMA) of SNMP counters polled from a device. It uses SNMP to periodically fetch the values of several OIDs, calculates the UWMA over increasing window sizes, and prints the results.

Uploaded by

Shalini Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

feps-teach02 6 2 .1.3.6.1.2.1.2.2.1.10 .1.3.6.1.2.1.2.2.1.

16

feps-
teach02 .1.3.6.1.2.1.6.13.1.1 .1.3.6.1.2.1.6.13.1.2 .1.3.6.1.2.1.6.13.1.3 .1.3.6.1.2.1.6.13.1.4 .1.3.6.1.2.1
.6.13.1.5

1) Listen in the 2nd code : 1) I need to show for all the 6 oids and change the window size
constantly and show the output before calculating the output and after caculating the output.
2) Understand how does UWMA is calculated.

------------------------------------------------------------------------------------------------------------------------
Window Size tcpInSegs tcpOutSegs ifInOctets ifOutOctets ipInDelivers ipOutRequests

feps-teach02
2 .1.3.6.1.2.1.2.2.1.10 .1.3.6.1.2.1.2.2.1.16 .1.3.6.1.2.1.2.2.1.10 .1.3.6.1.2.1.2.2.1.16 .1.3.6.1.2.1.4.9 .1.
3.6.1.2.1.4.10

========================================================================
=
package UWMA;
import com.adventnet.snmp.snmp2.*;
import java.util.*;

public class UWMA {

public static void main(String[] args) {


if (args.length != 8) {
System.out.println("Usage: java UWMA <hostname> <pollingPeriod> <OID1>
<OID2> <OID3> <OID4> <OID5> <OID6>");
System.exit(0);
}

String remoteHost = args[0];


int pollingPeriod = Integer.parseInt(args[1]);

List<String> oids = Arrays.asList(args).subList(2, args.length);

SnmpAPI api = new SnmpAPI();


api.start();
SnmpSession session = new SnmpSession(api);
session.setPeername(remoteHost);
session.setCommunity("teachinglabs");

try {
session.open();
} catch (SnmpException e) {
System.err.println("Error opening SNMP session: " + e);
api.close();
System.exit(1);
}

printHeaders();

for (String oid : oids) {


new Thread(() -> {
try {
processOID(session, oid, pollingPeriod);
} catch (InterruptedException e) {
System.err.println("Thread interrupted: " + e);
}
}).start();
}
}

private static void processOID(SnmpSession session, String oid, int


pollingPeriod) throws InterruptedException {
Deque<Long> values = new LinkedList<>();
Map<Integer, Long> rawValues = new HashMap<>();
Map<Integer, Double> uwmaValues = new HashMap<>();

System.out.println("Monitoring OID: " + oid);


System.out.println("Polling Period: " + pollingPeriod + "s");

Thread.sleep(pollingPeriod * 1000);

while (true) {
long currentValue = fetchSnmpValue(session, oid);
values.add(currentValue);

for (int windowSize = 1; windowSize <= values.size(); windowSize++) {{


rawValues.put(windowSize, values.getLast());
uwmaValues.put(windowSize, calculateUWMA(new LinkedList<>(values),
pollingPeriod));}

{ printRow(windowSize, oid, rawValues.get(windowSize),


uwmaValues.get(windowSize));}

{ Thread.sleep(pollingPeriod * 1000);}
}
}
}
private static void printRow(int windowSize, String oid, long rawValue, double
uwmaValue) {
System.out.printf("%-12d %-12.2f %-12d %-12d %-12.2f %-12.2f %-12d %-12.2f
%-12d %-12.2f %-12d%n",
windowSize, getUWMAValue(oid, uwmaValue), rawValue,
rawValue, getUWMAValue(oid, uwmaValue),
getUWMAValue(oid, uwmaValue), rawValue,
getUWMAValue(oid, uwmaValue), rawValue,
getUWMAValue(oid, uwmaValue), rawValue);
}

private static void printHeaders() {


System.out.println("Window Size UWMA_tc Raw Data_tcpl Rawdata_
UWMA_tcpOutSe UWMA_ifln Rawdata_ UWMA_ifOut Rawdata_ifOut UWMA_ Rawdata_ipOut");
System.out.println(" plnSegs nSegs cpOutSe gs
Octets iflnOctets Octets Octets iplnDelivers Requests");
}

private static double getUWMAValue(String oid, double uwmaValue) {


switch (oid) {
case ".1.3.6.1.2.1.6.10":
return uwmaValue;
case ".1.3.6.1.2.1.6.11":
return uwmaValue;
case ".1.3.6.1.2.1.2.2.1.10":
return uwmaValue;
case ".1.3.6.1.2.1.2.2.1.16":
return uwmaValue;
case ".1.3.6.1.2.1.4.9":
return uwmaValue;
case ".1.3.6.1.2.1.4.10":
return 0.0; // ipOutRequests is not a part of the UWMA calculation
default:
return 0.0;
}
}
private static long fetchSnmpValue(SnmpSession session, String oidString) {
SnmpOID oid = new SnmpOID(oidString);
SnmpPDU pdu = new SnmpPDU();
pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);
pdu.addNull(oid);

try {
SnmpPDU response = session.syncSend(pdu);
if (response == null) {
System.err.println("Received null response, assuming no data
available.");
return 0;
}

SnmpVarBind varbind = (SnmpVarBind)


response.getVariableBindings().elementAt(0);
String variableValue = varbind.getVariable().toString();
try {
return Long.parseLong(variableValue);
} catch (NumberFormatException e) {
System.err.println("Non-numeric SNMP response: " + variableValue);
return 0;
}
} catch (SnmpException e) {
System.err.println("Error fetching SNMP value: " + e);
return 0;
}
}
private static double calculateUWMA(Deque<Long> values, int pollingPeriod) {
if (values.size() <= 1) return 0; // Need at least two values to calculate
differences

Iterator<Long> it = values.descendingIterator();
long current = it.next();
double sumDifferences = 0;
int count = 0;

while (it.hasNext() && count < values.size() - 1) {


long previous = it.next();
sumDifferences += (current - previous);
current = previous;
count++;
}

// Divide the sum of differences by the number of differences and then by


the polling period
return sumDifferences / (count * pollingPeriod);
}
}

package UWMA;

import com.adventnet.snmp.snmp2.*;
import java.util.*;

public class UWMA {

private static final String[] OID_NAMES = {


"tcpInSegs", "tcpOutSegs", "ifInOctets", "ifOutOctets", "ipInDelivers",
"ipOutRequests"
};

public static void main(String[] args) {


if (args.length != 8) {
System.out.println("Usage: java UWMA <hostname> <pollingPeriod> <OID1>
<OID2> <OID3> <OID4> <OID5> <OID6>");
System.exit(0);
}

String remoteHost = args[0];


int pollingPeriod = Integer.parseInt(args[1]);
List<String> oids = Arrays.asList(args).subList(2, args.length);

SnmpAPI api = new SnmpAPI();


api.start();

SnmpSession session = new SnmpSession(api);


session.setPeername(remoteHost);
session.setCommunity("teachinglabs");

try {
session.open();
} catch (SnmpException e) {
System.err.println("Error opening SNMP session: " + e);
api.close();
System.exit(1);
}

printHeaders();

// Start a single thread to process OIDs sequentially


new Thread(() -> {
try {
for (int i = 0; i < oids.size(); i += 2) {
processOID(session, oids.get(i), oids.get(i + 1),
pollingPeriod);
}
} catch (InterruptedException e) {
System.err.println("Thread interrupted: " + e);
}
}).start();
}

private static void processOID(SnmpSession session, String oidIn, String


oidOut, int pollingPeriod) throws InterruptedException {
Deque<Long> inValues = new LinkedList<>();
Deque<Long> outValues = new LinkedList<>();

while (true) {
long inCurrentValue = fetchSnmpValue(session, oidIn);
long outCurrentValue = fetchSnmpValue(session, oidOut);
inValues.add(inCurrentValue);
outValues.add(outCurrentValue);

if (inValues.size() > 4) { // Assuming we are keeping only the last 4


readings for window size 1 to 4
inValues.poll();
outValues.poll();
}

// Print rows for each window size


for (int windowSize = 1; windowSize <= inValues.size(); windowSize++)
{
printRow(windowSize, inValues, outValues, pollingPeriod);
}

Thread.sleep(pollingPeriod * 1000);
}
}

private static void printRow(int windowSize, Deque<Long> inValues, Deque<Long>


outValues, int pollingPeriod) {
// Calculate the UWMA for both in and out
double uwmaIn = calculateUWMA(inValues, windowSize);
double uwmaOut = calculateUWMA(outValues, windowSize);

System.out.printf("%-12d %-12d %-12.2f %-12d %-12.2f %-12d %-12.2f %-12d


%-12.2f %-12d %-12.2f %-12d%n",
windowSize, inValues.getLast(), uwmaIn,
inValues.getLast(), uwmaOut,
outValues.getLast(), uwmaOut, outValues.getLast(),
uwmaIn,
inValues.getLast(), uwmaOut, outValues.getLast());
}
private static void printHeaders() {
// Print header based on OID_NAMES
System.out.println("Window Size " + String.join(" ", OID_NAMES));
}

private static long fetchSnmpValue(SnmpSession session, String oidString) {


SnmpOID oid = new SnmpOID(oidString);
SnmpPDU pdu = new SnmpPDU();
pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);
pdu.addNull(oid);

try {
SnmpPDU response = session.syncSend(pdu);
if (response == null) {
System.err.println("Received null response, assuming no data
available.");
return 0;
}

SnmpVarBind varbind = (SnmpVarBind)


response.getVariableBindings().elementAt(0);
String variableValue = varbind.getVariable().toString();
try {
return Long.parseLong(variableValue);
} catch (NumberFormatException e) {
System.err.println("Non-numeric SNMP response: " + variableValue);
return 0;
}
} catch (SnmpException e) {
System.err.println("Error fetching SNMP value: " + e);
return 0;
}
}

private static double calculateUWMA(Deque<Long> values, int pollingPeriod) {


if (values.size() <= 1) return 0; // Need at least two values to calculate
differences

Iterator<Long> it = values.descendingIterator();
long current = it.next();
double sumDifferences = 0;
int count = 0;

while (it.hasNext() && count < values.size() - 1) {


long previous = it.next();
sumDifferences += (current - previous);
current = previous;
count++;
}

// Divide the sum of differences by the number of differences and then by


the polling period
return sumDifferences / (count * pollingPeriod);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++

package UWMA;

import com.adventnet.snmp.snmp2.*;
import java.util.*;

public class UWMA {

private static final String[] OID_NAMES = {


"tcpInSegs", "tcpOutSegs", "ifInOctets", "ifOutOctets", "ipInDelivers",
"ipOutRequests"
};

public static void main(String[] args) {


if (args.length != 8) {
System.out.println("Usage: java UWMA <hostname> <pollingPeriod> <OID1>
<OID2> <OID3> <OID4> <OID5> <OID6>");
System.exit(0);
}

String remoteHost = args[0];


int pollingPeriod = Integer.parseInt(args[1]);
List<String> oids = Arrays.asList(args).subList(2, args.length);

SnmpAPI api = new SnmpAPI();


api.start();

SnmpSession session = new SnmpSession(api);


session.setPeername(remoteHost);
session.setCommunity("teachinglabs");

try {
session.open();
} catch (SnmpException e) {
System.err.println("Error opening SNMP session: " + e);
api.close();
System.exit(1);
}

printHeaders();

new Thread(() -> {


try {
for (int i = 0; i < oids.size(); i += 2) {
processOID(session, oids.get(i), oids.get(i + 1),
pollingPeriod);
}
} catch (InterruptedException e) {
System.err.println("Thread interrupted: " + e);
}
}).start();
}

private static void processOID(SnmpSession session, String oidIn, String


oidOut, int pollingPeriod) throws InterruptedException {
Deque<Long> inValues = new LinkedList<>();
Deque<Long> outValues = new LinkedList<>();

while (true) {
long inCurrentValue = fetchSnmpValue(session, oidIn);
long outCurrentValue = fetchSnmpValue(session, oidOut);
inValues.add(inCurrentValue);
outValues.add(outCurrentValue);

// Print rows for each window size


for (int windowSize = 1; windowSize <= inValues.size(); windowSize++)
{
printRow(windowSize, inValues, outValues, pollingPeriod);
}

Thread.sleep(pollingPeriod * 1000);
}
}

private static void printRow(int windowSize, Deque<Long> inValues, Deque<Long>


outValues, int pollingPeriod) {
// Calculate the UWMA for both in and out
double uwmaIn = calculateUWMA(inValues, windowSize, pollingPeriod);
double uwmaOut = calculateUWMA(outValues, windowSize, pollingPeriod);

System.out.printf("%-12d %-12d %-12.2f %-12d %-12.2f %-12d %-12.2f %-12d


%-12.2f %-12d %-12.2f %-12d%n",
windowSize, inValues.getLast(), uwmaIn,
inValues.getLast(), uwmaOut,
outValues.getLast(), uwmaOut, outValues.getLast(),
uwmaIn,
inValues.getLast(), uwmaOut, outValues.getLast());
}

private static void printHeaders() {


// Print header based on OID_NAMES
System.out.println("Window Size " + String.join(" ", OID_NAMES));
}

private static long fetchSnmpValue(SnmpSession session, String oidString) {


SnmpOID oid = new SnmpOID(oidString);
SnmpPDU pdu = new SnmpPDU();
pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);
pdu.addNull(oid);

try {
SnmpPDU response = session.syncSend(pdu);
if (response == null) {
System.err.println("Received null response, assuming no data
available.");
return 0;
}

SnmpVarBind varbind = (SnmpVarBind)


response.getVariableBindings().elementAt(0);
String variableValue = varbind.getVariable().toString();
try {
return Long.parseLong(variableValue);
} catch (NumberFormatException e) {
System.err.println("Non-numeric SNMP response: " + variableValue);
return 0;
}
} catch (SnmpException e) {
System.err.println("Error fetching SNMP value: " + e);
return 0;
}
}

private static double calculateUWMA(Deque<Long> values, int windowSize, int


pollingPeriod) {
// Ensure enough data is available for the specified window
if (values.size() < windowSize) return 0;

// Use a ListIterator to allow bi-directional traversal of the list


ListIterator<Long> it = new
ArrayList<>(values).listIterator(values.size());

double sum = 0;
int count = 0;
long current = it.previous(); // Start with the most recent value

// Calculate sum of differences


while (it.hasPrevious() && count < windowSize - 1) {
long previous = it.previous();
sum += (current - previous);
current = previous;
count++;
}

// Average the sum of differences and divide by the polling period


// Check if count is zero to avoid division by zero
return count == 0 ? 0 : (sum / count) / pollingPeriod;
}
}

You might also like