feps-teach02 6 2 .[Link].[Link].1.10 .[Link].[Link].1.
16
feps-
teach02 .[Link].[Link].1.1 .[Link].[Link].1.2 .[Link].[Link].1.3 .[Link].[Link].1.4 .[Link].2.1
.[Link]
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 .[Link].[Link].1.10 .[Link].[Link].1.16 .[Link].[Link].1.10 .[Link].[Link].1.16 .[Link].[Link] .1.
[Link].1.4.10
========================================================================
=
package UWMA;
import [Link].snmp2.*;
import [Link].*;
public class UWMA {
public static void main(String[] args) {
if ([Link] != 8) {
[Link]("Usage: java UWMA <hostname> <pollingPeriod> <OID1>
<OID2> <OID3> <OID4> <OID5> <OID6>");
[Link](0);
}
String remoteHost = args[0];
int pollingPeriod = [Link](args[1]);
List<String> oids = [Link](args).subList(2, [Link]);
SnmpAPI api = new SnmpAPI();
[Link]();
SnmpSession session = new SnmpSession(api);
[Link](remoteHost);
[Link]("teachinglabs");
try {
[Link]();
} catch (SnmpException e) {
[Link]("Error opening SNMP session: " + e);
[Link]();
[Link](1);
}
printHeaders();
for (String oid : oids) {
new Thread(() -> {
try {
processOID(session, oid, pollingPeriod);
} catch (InterruptedException e) {
[Link]("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<>();
[Link]("Monitoring OID: " + oid);
[Link]("Polling Period: " + pollingPeriod + "s");
[Link](pollingPeriod * 1000);
while (true) {
long currentValue = fetchSnmpValue(session, oid);
[Link](currentValue);
for (int windowSize = 1; windowSize <= [Link](); windowSize++) {{
[Link](windowSize, [Link]());
[Link](windowSize, calculateUWMA(new LinkedList<>(values),
pollingPeriod));}
{ printRow(windowSize, oid, [Link](windowSize),
[Link](windowSize));}
{ [Link](pollingPeriod * 1000);}
}
}
}
private static void printRow(int windowSize, String oid, long rawValue, double
uwmaValue) {
[Link]("%-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() {
[Link]("Window Size UWMA_tc Raw Data_tcpl Rawdata_
UWMA_tcpOutSe UWMA_ifln Rawdata_ UWMA_ifOut Rawdata_ifOut UWMA_ Rawdata_ipOut");
[Link](" plnSegs nSegs cpOutSe gs
Octets iflnOctets Octets Octets iplnDelivers Requests");
}
private static double getUWMAValue(String oid, double uwmaValue) {
switch (oid) {
case ".[Link].[Link]":
return uwmaValue;
case ".[Link].[Link]":
return uwmaValue;
case ".[Link].[Link].1.10":
return uwmaValue;
case ".[Link].[Link].1.16":
return uwmaValue;
case ".[Link].[Link]":
return uwmaValue;
case ".[Link].[Link]":
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();
[Link](SnmpAPI.GETNEXT_REQ_MSG);
[Link](oid);
try {
SnmpPDU response = [Link](pdu);
if (response == null) {
[Link]("Received null response, assuming no data
available.");
return 0;
}
SnmpVarBind varbind = (SnmpVarBind)
[Link]().elementAt(0);
String variableValue = [Link]().toString();
try {
return [Link](variableValue);
} catch (NumberFormatException e) {
[Link]("Non-numeric SNMP response: " + variableValue);
return 0;
}
} catch (SnmpException e) {
[Link]("Error fetching SNMP value: " + e);
return 0;
}
}
private static double calculateUWMA(Deque<Long> values, int pollingPeriod) {
if ([Link]() <= 1) return 0; // Need at least two values to calculate
differences
Iterator<Long> it = [Link]();
long current = [Link]();
double sumDifferences = 0;
int count = 0;
while ([Link]() && count < [Link]() - 1) {
long previous = [Link]();
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 [Link].snmp2.*;
import [Link].*;
public class UWMA {
private static final String[] OID_NAMES = {
"tcpInSegs", "tcpOutSegs", "ifInOctets", "ifOutOctets", "ipInDelivers",
"ipOutRequests"
};
public static void main(String[] args) {
if ([Link] != 8) {
[Link]("Usage: java UWMA <hostname> <pollingPeriod> <OID1>
<OID2> <OID3> <OID4> <OID5> <OID6>");
[Link](0);
}
String remoteHost = args[0];
int pollingPeriod = [Link](args[1]);
List<String> oids = [Link](args).subList(2, [Link]);
SnmpAPI api = new SnmpAPI();
[Link]();
SnmpSession session = new SnmpSession(api);
[Link](remoteHost);
[Link]("teachinglabs");
try {
[Link]();
} catch (SnmpException e) {
[Link]("Error opening SNMP session: " + e);
[Link]();
[Link](1);
}
printHeaders();
// Start a single thread to process OIDs sequentially
new Thread(() -> {
try {
for (int i = 0; i < [Link](); i += 2) {
processOID(session, [Link](i), [Link](i + 1),
pollingPeriod);
}
} catch (InterruptedException e) {
[Link]("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);
[Link](inCurrentValue);
[Link](outCurrentValue);
if ([Link]() > 4) { // Assuming we are keeping only the last 4
readings for window size 1 to 4
[Link]();
[Link]();
}
// Print rows for each window size
for (int windowSize = 1; windowSize <= [Link](); windowSize++)
{
printRow(windowSize, inValues, outValues, pollingPeriod);
}
[Link](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);
[Link]("%-12d %-12d %-12.2f %-12d %-12.2f %-12d %-12.2f %-12d
%-12.2f %-12d %-12.2f %-12d%n",
windowSize, [Link](), uwmaIn,
[Link](), uwmaOut,
[Link](), uwmaOut, [Link](),
uwmaIn,
[Link](), uwmaOut, [Link]());
}
private static void printHeaders() {
// Print header based on OID_NAMES
[Link]("Window Size " + [Link](" ", OID_NAMES));
}
private static long fetchSnmpValue(SnmpSession session, String oidString) {
SnmpOID oid = new SnmpOID(oidString);
SnmpPDU pdu = new SnmpPDU();
[Link](SnmpAPI.GETNEXT_REQ_MSG);
[Link](oid);
try {
SnmpPDU response = [Link](pdu);
if (response == null) {
[Link]("Received null response, assuming no data
available.");
return 0;
}
SnmpVarBind varbind = (SnmpVarBind)
[Link]().elementAt(0);
String variableValue = [Link]().toString();
try {
return [Link](variableValue);
} catch (NumberFormatException e) {
[Link]("Non-numeric SNMP response: " + variableValue);
return 0;
}
} catch (SnmpException e) {
[Link]("Error fetching SNMP value: " + e);
return 0;
}
}
private static double calculateUWMA(Deque<Long> values, int pollingPeriod) {
if ([Link]() <= 1) return 0; // Need at least two values to calculate
differences
Iterator<Long> it = [Link]();
long current = [Link]();
double sumDifferences = 0;
int count = 0;
while ([Link]() && count < [Link]() - 1) {
long previous = [Link]();
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 [Link].snmp2.*;
import [Link].*;
public class UWMA {
private static final String[] OID_NAMES = {
"tcpInSegs", "tcpOutSegs", "ifInOctets", "ifOutOctets", "ipInDelivers",
"ipOutRequests"
};
public static void main(String[] args) {
if ([Link] != 8) {
[Link]("Usage: java UWMA <hostname> <pollingPeriod> <OID1>
<OID2> <OID3> <OID4> <OID5> <OID6>");
[Link](0);
}
String remoteHost = args[0];
int pollingPeriod = [Link](args[1]);
List<String> oids = [Link](args).subList(2, [Link]);
SnmpAPI api = new SnmpAPI();
[Link]();
SnmpSession session = new SnmpSession(api);
[Link](remoteHost);
[Link]("teachinglabs");
try {
[Link]();
} catch (SnmpException e) {
[Link]("Error opening SNMP session: " + e);
[Link]();
[Link](1);
}
printHeaders();
new Thread(() -> {
try {
for (int i = 0; i < [Link](); i += 2) {
processOID(session, [Link](i), [Link](i + 1),
pollingPeriod);
}
} catch (InterruptedException e) {
[Link]("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);
[Link](inCurrentValue);
[Link](outCurrentValue);
// Print rows for each window size
for (int windowSize = 1; windowSize <= [Link](); windowSize++)
{
printRow(windowSize, inValues, outValues, pollingPeriod);
}
[Link](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);
[Link]("%-12d %-12d %-12.2f %-12d %-12.2f %-12d %-12.2f %-12d
%-12.2f %-12d %-12.2f %-12d%n",
windowSize, [Link](), uwmaIn,
[Link](), uwmaOut,
[Link](), uwmaOut, [Link](),
uwmaIn,
[Link](), uwmaOut, [Link]());
}
private static void printHeaders() {
// Print header based on OID_NAMES
[Link]("Window Size " + [Link](" ", OID_NAMES));
}
private static long fetchSnmpValue(SnmpSession session, String oidString) {
SnmpOID oid = new SnmpOID(oidString);
SnmpPDU pdu = new SnmpPDU();
[Link](SnmpAPI.GETNEXT_REQ_MSG);
[Link](oid);
try {
SnmpPDU response = [Link](pdu);
if (response == null) {
[Link]("Received null response, assuming no data
available.");
return 0;
}
SnmpVarBind varbind = (SnmpVarBind)
[Link]().elementAt(0);
String variableValue = [Link]().toString();
try {
return [Link](variableValue);
} catch (NumberFormatException e) {
[Link]("Non-numeric SNMP response: " + variableValue);
return 0;
}
} catch (SnmpException e) {
[Link]("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 ([Link]() < windowSize) return 0;
// Use a ListIterator to allow bi-directional traversal of the list
ListIterator<Long> it = new
ArrayList<>(values).listIterator([Link]());
double sum = 0;
int count = 0;
long current = [Link](); // Start with the most recent value
// Calculate sum of differences
while ([Link]() && count < windowSize - 1) {
long previous = [Link]();
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;
}
}