-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathController.java
More file actions
executable file
·467 lines (400 loc) · 17.8 KB
/
Controller.java
File metadata and controls
executable file
·467 lines (400 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*MAIN CLASS
* Creates all Objects, gives print commands and manages the operations.
*/
import java.io.Console;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import com.adventnet.snmp.snmp2.SnmpException;
import com.adventnet.snmp.snmp2.SnmpPDU;
import com.adventnet.snmp.snmp2.SnmpSession;
public class Controller {
@SuppressWarnings("unchecked")
public static void main (String args[]){
//SNMP Port
int port = 161;
//Parameters for connection provided by console
String firstHost=null;
String username = null;
String password=null;
try {
firstHost = args[0];
username = args[1];
password = args[2];
} catch (Exception e1) {
System.out.println("Wrong Input!");
System.out.println("Input as follows: IP username password");
System.exit(0);
}
//Intitialize the classes
Printer printer = new Printer();
ConnectionManager conmanager= new ConnectionManager(port);
SnmpOperator operator = new SnmpOperator();
TableAgent tagent=new TableAgent(operator);
SearchAgent sagent = new SearchAgent(tagent,operator);
RouterLoadsAgent rloadagent = new RouterLoadsAgent(sagent,conmanager,username,password);
VariableBinsAgent binsAgent=new VariableBinsAgent();
//The console
Console console=System.console();
//The variables we need
SnmpSession session = null;
SnmpPDU pdu=null;
String engineIDString=null;
String currentIP=null; //the current IP which the connection is established to
String ip=""; //used for comparisons of IPs
//Lists
List <String> visitedList=new ArrayList <String>();//all visited Routers (all IPS of these routers)
List <String> stillToVisitList=new ArrayList <String>(); //Routers that have to be visited
List <String> noSNMPList=new ArrayList <String>();//for none SNMP devices
List <String> routerList=new ArrayList <String>();//router IPS, one IP per router
List <BigDecimal> routerLoadsList=new ArrayList <BigDecimal>();//Collects the results for the loads from all routers
List <Integer> intNrList=new ArrayList <Integer>();//Numbers of the Interfaces on a defined router
//Error calculation
List <BigDecimal> []binArray;
List <BigDecimal> centroidsList=new ArrayList <BigDecimal>();
//sets (no duplicates)
Set<String> allNeighSet = new LinkedHashSet<String>(); //contains all neighbours in the network so all nodes and IPS in the nework
// Connection
Object conDetails[]=null;
String hostName=null;
//Measurement options
String histArray[][]=null;
String nrOfMeasurements=null;
String measureFrequency=null;
String sampleDistance=null;
String nrOfBins="5";//standard implemention
long sampleFrequency=new Long("0");
long startMeasurementTime=new Long("0");//for checking the time between measurements
long stopMeasurementTime=new Long("0");//measuremnet took longer then distance start next one immediately
//Octet comparison variables
BigDecimal lowestOctet;
BigDecimal biggestOctet;
//Graph parameter
BigDecimal binWidth=new BigDecimal("0");
BigDecimal [] xAxesArray=null;//values of x axes
int[] counterArray=null;//counter for the xaxes
int biggestCounter=0;//coints matches for the bins
//Initialize the ipList with an IP to start
stillToVisitList.add(firstHost);
//Inititalize visitCounter counter to jump non SNMP devices
int visitCounter=0;
//Print Header
printer.printSeperator("DISCOVERY STARTED");
//-------------------------------------Network Discovery-----------------------------
// As long as there are addresses in the stilltoVisitList go on
while(stillToVisitList.size()!=0){
//try{
boolean established=false;
//as long as there is no established connection try it
while(established==false){
//go through the List till a working IP is found
if(stillToVisitList.size()!=1){
visitCounter++;
}
try {
currentIP=stillToVisitList.get(visitCounter);
conDetails = conmanager.establishConnection(currentIP,username,password );
session = (SnmpSession) conDetails[0];
pdu=(SnmpPDU) conDetails[1];
//EngineID
engineIDString=(String)conDetails[2];
//If no Exception occured till now then it worked so established
established=true;
} catch (SnmpException e) {
//Its not accessable therefore delete from List
noSNMPList.add(currentIP);
stillToVisitList.remove(currentIP);
//Check whever end is reached
if(stillToVisitList.size()==0){
break;
}
//if a field is deleted go back one with the counter but only if the counter is not 0 (-1 Exception)
if(visitCounter!=0){
visitCounter--;
}
}//end catch
}//connection established
//only do if List is not 0 yet
if(stillToVisitList.size()!=0){
//Add ip to Router List which shows the IPS of accessable routers
routerList.add(currentIP);
//Get details
hostName=sagent.getHostName(pdu, session);
//reset visit counter
visitCounter=0;
//PRINT
//Device
printer.printTitel(hostName, currentIP,engineIDString);
//IPS
printer.printHead("LOCAL IPS");
printer.printDescr("Int Nr.", "Inte Desc.", "IP");
printer.print2DArray(sagent.getIPs(pdu, session));
//No IP Int
printer.printHead("NO IP INT");
printer.printDescr("Int Nr.", "Inte Desc.","");
printer.print2DArray(sagent.getNoIPInt(pdu, session,intNrList));
//Neighbours
printer.printHead("NEIGHBOURS");
printer.printDescr("Int Nr.", "Inte Desc.", "IP");
//Search for neighbours
sagent.searchNeighbours(pdu, session);
//Print the Neighbours Array
printer.print2DArray(sagent.getNeighbours(pdu, session));
//End of Printing
//Get the neighbour and IP list
allNeighSet.addAll(sagent.getNeighList());
//all the local IPS to the visited list to avoid duplicate visit
visitedList.addAll(sagent.getIPList());
//Clear the still to visitlist
stillToVisitList.clear();
//Fill still to visit list new
//As the allNeighList is a set we have to use an iteraotr to traverse it.
for(Iterator<String> iterator=allNeighSet.iterator();iterator.hasNext();ip=iterator.next()){
if(!visitedList.contains(ip)&& !stillToVisitList.contains(ip)&& !noSNMPList.contains(ip)){
stillToVisitList.add(ip);
}
}
}//end if
//Close
if(conmanager.terminateConnection(session)==true){
}else{
System.out.println("Session can not be terminated");
}
}//end for, end of router search
//Print information
printer.printSeperator("DISCOVERY FINISHED");
//sort list
Collections.sort(routerList);
Collections.sort(noSNMPList);
//Print Network informations
System.out.println("SNMP devices in Network: "+ Arrays.toString(routerList.toArray()));
System.out.println("Non SNMP devices in Network: "+ Arrays.toString(noSNMPList.toArray()));
//-------------------------------------------End of Discovery------------------------------------------------//
/* APPLICATION PART 2 */
/*********************************/
/**** Router Load Computation ****/
/*********************************/
//Print information
printer.printSeperator("PRINT HISTOGRAMMS");
//initialize to avoid null pointer exception
measureFrequency="3";
sampleDistance="0";
//go on till exit
while(!measureFrequency.equals("exit")){
try{
//USER INPUT ....
printer.printHead("USER INPUT");
System.out.println("");
System.out.println("Define the distance between measurements in sec (distance between Histogram creations) or type 'exit'");
measureFrequency=console.readLine();
//to avoid execution if exit is typed
if(!measureFrequency.equals("exit")){
//More INPUT
System.out.println("Define the distance between samples in seconds (0=as fast as possible).");
sampleDistance=console.readLine();
System.out.println("Define the number of bars (bins)");
nrOfBins=console.readLine();
System.out.println("Define the number of histogramms to print (repetitions of measurement)");
nrOfMeasurements=console.readLine();
//sampleDistance in seconds therefore convert to ms
sampleFrequency=new Long(sampleDistance+"000");
//How often should be measured go for it if number bigger 1 sleep for defined measureFrequency
for(int l=0; l<Integer.parseInt(nrOfMeasurements);l++){
//Print seperator
printer.printSeperator("MEASUREMENT");
//as its a loop clear
routerLoadsList.clear();
centroidsList.clear();
//for comparison to work
biggestCounter=0;
//Nr of Bins is defined therefore - reset afetr first
binArray=new ArrayList[Integer.parseInt(nrOfBins)];
for(int i=0;i<binArray.length;i++){
binArray[i]=new ArrayList <BigDecimal>();
}
//+++++++++++++++++++++Get values from RouterLoadAgent and start timer ++++++++++++++++++++
startMeasurementTime=System.currentTimeMillis();
routerLoadsList.addAll(rloadagent.getRouterLoads(routerList,intNrList,sampleFrequency));
stopMeasurementTime=System.currentTimeMillis();
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Calculating the bars and creating a 2DArray
//find largest and smallest value
//initialize with a value to avoid 0 lowest value
lowestOctet=routerLoadsList.get(0);
biggestOctet=routerLoadsList.get(0);
//biggest
for(int i=0;i<routerLoadsList.size();i++ ){
//if smaller then it returns -1
if(biggestOctet.compareTo(routerLoadsList.get(i))==-1){
biggestOctet=routerLoadsList.get(i);
}
//the lowest value
if(lowestOctet.compareTo(routerLoadsList.get(i))==1){
lowestOctet=routerLoadsList.get(i);
}
}//end for
//First lets sort the fields into the ranges (number of bins)
binWidth=biggestOctet.subtract(lowestOctet).divide(new BigDecimal(nrOfBins),0,RoundingMode.HALF_UP);
//Create the arrays needed
xAxesArray=new BigDecimal[Integer.parseInt(nrOfBins)];
counterArray=new int[Integer.parseInt(nrOfBins)];
//centroitsArray=new BigDecimal[Integer.parseInt(nrOfBins)];
//Fill the xarray (xaxes)
xAxesArray[0]=lowestOctet;
for(int i=1;i<xAxesArray.length;i++){
xAxesArray[i]=xAxesArray[i-1].add(binWidth);
}
//Count the matches in the different categories
for(int i=0;i<routerLoadsList.size();i++){
for(int j=0;j<xAxesArray.length;j++){
if(routerLoadsList.get(i).compareTo(xAxesArray[xAxesArray.length-1])>=0){
counterArray[xAxesArray.length-1]++;
binArray[xAxesArray.length-1].add(routerLoadsList.get(i));//put them in buckets
break;
}else{
if(routerLoadsList.get(i).compareTo(xAxesArray[j])>=0&&routerLoadsList.get(i).compareTo(xAxesArray[j+1])<0){
counterArray[j]++;
binArray[j].add(routerLoadsList.get(i));//sum up all in a bin
break;
}
}
}//end for j
}//end for i
//calculate the centroits for error calculation
for (int i=0;i<Integer.parseInt(nrOfBins)-1;i++){
//centroitsArray[i]=xAxesArray[i].add(xAxesArray[i]).divide(new BigDecimal("2"),3,RoundingMode.HALF_UP);
centroidsList.add(xAxesArray[i].add(xAxesArray[i+1]).divide(new BigDecimal("2"),0,RoundingMode.HALF_UP).abs());
}
//find the biggest Number of matches for dimensioning the 2D array
for(int i=0;i<counterArray.length;i++){
if(counterArray[i]>biggestCounter){
biggestCounter=counterArray[i];
}
}
//Create the 2D Array
// fill the array with ""
histArray=new String [biggestCounter][counterArray.length];
for (int i =0; i < histArray[0].length; i++) {
for (int j = 0; j < histArray.length; j++) {
histArray[j][i]=" ";
}//end for j
}//end for i
//set the counters i=column j=row .length =column ;[0].length = rows
//go through the array horizontal
for(int i=0;i<histArray[0].length;i++){
//fill vertical
for(int j=histArray.length-counterArray[i];j<histArray.length;j++){
histArray[j][i]="+";
}
}
//PRINT it and some Information !!!
System.out.println( new java.util.Date() );
System.out.println("Router in Network :"+ Arrays.toString(routerList.toArray()));
System.out.println("Router Loads (bit/s):"+(Arrays.toString(routerLoadsList.toArray())));
//sort for better comparison in graph
//sort the route rLoads List
Collections.sort(routerLoadsList);
System.out.println("Sorted RLoad (bit/s):"+(Arrays.toString(routerLoadsList.toArray())));
System.out.println("Sample Distance: "+sampleDistance+"s");
System.out.println("Highest Value : "+biggestOctet+" bit/s "+"Lowest Value: "+lowestOctet+" bit/s");
printer.printHead("Common Hist.");
System.out.println("Quantization Error: "+binsAgent.calculateEstimationError(centroidsList, binArray));
BigDecimal errorCommonApproach = new BigDecimal ("0");
errorCommonApproach = binsAgent.calculateEstimationError(centroidsList, binArray);
System.out.println("Center Values (bits/s): "+Arrays.toString(centroidsList.toArray()));
System.out.println("Width of Bins: "+binWidth);
printer.printHistogram(histArray, xAxesArray, biggestOctet);
//variable bins
getVariableBins(routerLoadsList,binsAgent,Integer.parseInt(nrOfBins),printer,biggestOctet,errorCommonApproach);
//Sleep for defined time to implement time distance between measurements. Not for the last one
if(Integer.parseInt(nrOfMeasurements)>1&&Integer.parseInt(nrOfMeasurements)-1!=l){
try {
//check whether the measurement took longer than the distance between measurements. if yes go immediately
if(stopMeasurementTime-startMeasurementTime<new Long(measureFrequency+"000")){
Thread.currentThread();
Thread.sleep(new Long(measureFrequency+"000")-(stopMeasurementTime-startMeasurementTime));
}else{
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}//end for l
}//end if
}catch(Exception e){
System.out.println("Error occured, please try again");
e.printStackTrace();
}
}//end while (sampleDistance exit)
//Programme finished
System.exit(0);
}//end main
@SuppressWarnings("unchecked")
static void getVariableBins(List routerLoadsList, VariableBinsAgent binsAgent, int binNumber,Printer printer, BigDecimal biggestOctet, BigDecimal errorCommonApproach){
printer.printHead("Variable Hist.");
//For painting later
BigDecimal[] binWidthArray=null;
BigDecimal[] minBinValArray=null;
//for painting
int[] counterArray=null;//counter for the xaxes
int biggestCounter=0;//coints matches for the bins
//2d Array
String histArray[][]=null;
List <List<BigDecimal>> finalBinList = new ArrayList <List<BigDecimal>>();;
//++++++++++++++++++++++++++++++++++++++++Variable BINS+++++++++++++++++++++++++++++++++++++++++++++++++
//***************************************optional ****************************************************
finalBinList=binsAgent.getVariableBins(routerLoadsList, binNumber);
BigDecimal errorVariableApproach = new BigDecimal ("0");
errorVariableApproach = binsAgent.result;
while (errorCommonApproach.compareTo(errorVariableApproach) == -1){
System.out.println("Random initial centroids were improperly selected, recompute centroids");
//finalBinList.clear();
finalBinList=binsAgent.getVariableBins(routerLoadsList, binNumber);
errorVariableApproach = binsAgent.result;
//binsAgent.getVariableBins(routerLoadsList, binNumber);
}
//*****************************************************************************************
//create array
binWidthArray=new BigDecimal[finalBinList.size()];
minBinValArray=new BigDecimal[finalBinList.size()];
//Fill the counter array to send to printer later
counterArray=new int[finalBinList.size()];
for(int i=0;i<finalBinList.size();i++){
counterArray[i]=finalBinList.get(i).size();
}
//------------------------2D Array creation----------------------
//find the biggest Number of matches for dimensioning the 2D array
for(int i=0;i<counterArray.length;i++){
if(counterArray[i]>biggestCounter){
biggestCounter=counterArray[i];
}
}
//Create the 2D Array
// fill the array with ""
histArray=new String [biggestCounter][counterArray.length];
for (int i =0; i < histArray[0].length; i++) {
for (int j = 0; j < histArray.length; j++) {
histArray[j][i]=" ";
}//end for j
}//end for i
//set the counters i=column j=row .length =column ;[0].length = rows
//go through the array horizontal
for(int i=0;i<histArray[0].length;i++){
//fill vertical
for(int j=histArray.length-counterArray[i];j<histArray.length;j++){
histArray[j][i]="+";
}
}
//-------------------------------------------------------------------------
//fill the binwidth array
if (errorCommonApproach.compareTo(errorVariableApproach) == 1){
for (int i = 0; i < finalBinList.size(); i++){
minBinValArray[i] = Collections.min(finalBinList.get(i));
binWidthArray[i] = (Collections.max(finalBinList.get(i)).subtract(Collections.min(finalBinList.get(i))));
}
System.out.println("Width of the Bins (bits): "+Arrays.toString(binWidthArray));
printer.printHistogram(histArray, minBinValArray, biggestOctet);
}
//System.out.println("minBin: "+Arrays.toString(minBinValArray));
}
}