přidany propojení mezi objekty včetně zobrazovaní trafficu
parent
01d3a02b0d
commit
3b6717a580
|
|
@ -114,6 +114,17 @@ public class Message implements Serializable {
|
|||
// S->C: server odesila vysledek testu
|
||||
public static final int SNMP_TEST = 116;
|
||||
|
||||
// C->S: klient si vyzadal SNMP seznam rozhrani objektu
|
||||
// S->C: klient odpovida seznam interfacu
|
||||
public static final int SNMP_CONNECTION_INTERFACE = 117;
|
||||
|
||||
// C->S: klient zasila na server informaci o novem spojeni
|
||||
// S->C: server rozesila nove spojeni
|
||||
public static final int CONNECTION_NEW = 118;
|
||||
|
||||
// S->C: server posila klientu trafic informaci
|
||||
public static final int TRAFFIC = 119;
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,148 @@
|
|||
package jnet.lib.object;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Connection implements Serializable {
|
||||
|
||||
public static final int TYPE_OTHER = 0;
|
||||
public static final int TYPE_GE = 1;
|
||||
public static final int TYPE_FE = 2;
|
||||
public static final int TYPE_E = 3;
|
||||
public static final int TYPE_10GE = 4;
|
||||
public static final int TYPE_100M_FIBER = 5;
|
||||
public static final int TYPE_1G_FIBER = 6;
|
||||
public static final int TYPE_10G_FIBER = 7;
|
||||
public static final int TYPE_WIRELESS = 8;
|
||||
public static final int TYPE_TUNEL = 9;
|
||||
public static final int TYPE_VLAN = 10;
|
||||
|
||||
private int id;
|
||||
private int sourceObj;
|
||||
private int destinationObj;
|
||||
private int type;
|
||||
private int map;
|
||||
private boolean readTraffic;
|
||||
private int trafficObject;
|
||||
private int trafficIface;
|
||||
private long rx;
|
||||
private long tx;
|
||||
|
||||
public Connection(int id, int sourceObj, int destinationObj, int type, int map, boolean readTraffic, int trafficObject, int trafficIface) {
|
||||
this.id = id;
|
||||
this.sourceObj = sourceObj;
|
||||
this.destinationObj = destinationObj;
|
||||
this.type = type;
|
||||
this.map = map;
|
||||
this.readTraffic = readTraffic;
|
||||
this.trafficObject = trafficObject;
|
||||
this.trafficIface = trafficIface;
|
||||
this.rx = 0;
|
||||
this.tx = 0;
|
||||
}
|
||||
|
||||
public Connection(int sourceObj, int destinationObj, int type, int map, boolean readTraffic, int trafficObject, int trafficIface) {
|
||||
this.sourceObj = sourceObj;
|
||||
this.destinationObj = destinationObj;
|
||||
this.type = type;
|
||||
this.map = map;
|
||||
this.readTraffic = readTraffic;
|
||||
this.trafficObject = trafficObject;
|
||||
this.trafficIface = trafficIface;
|
||||
this.rx = 0;
|
||||
this.tx = 0;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getSourceObj() {
|
||||
return sourceObj;
|
||||
}
|
||||
|
||||
public void setSourceObj(int sourceObj) {
|
||||
this.sourceObj = sourceObj;
|
||||
}
|
||||
|
||||
public int getDestinationObj() {
|
||||
return destinationObj;
|
||||
}
|
||||
|
||||
public void setDestinationObj(int destinationObj) {
|
||||
this.destinationObj = destinationObj;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(int map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public boolean isReadTraffic() {
|
||||
return readTraffic;
|
||||
}
|
||||
|
||||
public void setReadTraffic(boolean readTraffic) {
|
||||
this.readTraffic = readTraffic;
|
||||
}
|
||||
|
||||
public int getTrafficObject() {
|
||||
return trafficObject;
|
||||
}
|
||||
|
||||
public void setTrafficObject(int trafficObject) {
|
||||
this.trafficObject = trafficObject;
|
||||
}
|
||||
|
||||
public int getTrafficIface() {
|
||||
return trafficIface;
|
||||
}
|
||||
|
||||
public void setTrafficIface(int trafficIface) {
|
||||
this.trafficIface = trafficIface;
|
||||
}
|
||||
|
||||
public String getRx() {
|
||||
return convertBps(rx);
|
||||
}
|
||||
|
||||
public void setRx(long rx) {
|
||||
this.rx = rx;
|
||||
}
|
||||
|
||||
public String getTx() {
|
||||
return convertBps(tx);
|
||||
}
|
||||
|
||||
public void setTx(long tx) {
|
||||
this.tx = tx;
|
||||
}
|
||||
|
||||
public static String convertBps(long Bps) {
|
||||
double bitsPerSec = Bps * 8.0; // Převod na bity za sekundu jako double pro přesnost
|
||||
if (bitsPerSec >= 1_000_000_000) {
|
||||
return String.format("%.2f Gbps", bitsPerSec / 1_000_000_000.0);
|
||||
} else if (bitsPerSec >= 1_000_000) {
|
||||
return String.format("%.2f Mbps", bitsPerSec / 1_000_000.0);
|
||||
} else if (bitsPerSec >= 1_000) {
|
||||
return String.format("%.2f Kbps", bitsPerSec / 1_000.0);
|
||||
} else {
|
||||
return String.format("%.0f bps", bitsPerSec); // Zobrazení bez desetinných míst pro malé hodnoty
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -10,14 +10,16 @@ public class Map implements Serializable {
|
|||
private String name;
|
||||
private boolean lock;
|
||||
private ArrayList<MapObject> objects;
|
||||
private ArrayList<Connection> connection;
|
||||
private int status;
|
||||
|
||||
public Map(int id, String name, boolean lock, ArrayList<MapObject> objects) {
|
||||
public Map(int id, String name, boolean lock, ArrayList<MapObject> objects, ArrayList<Connection> connection) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.lock = lock;
|
||||
this.status = Status.NA;
|
||||
this.objects = objects;
|
||||
this.connection = connection;
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +61,16 @@ public class Map implements Serializable {
|
|||
this.objects = objects;
|
||||
}
|
||||
|
||||
public ArrayList<Connection> getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void setConnection(ArrayList<Connection> connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getStatus() {
|
||||
if (getObjects().isEmpty()) {
|
||||
return Status.NA;
|
||||
|
|
|
|||
|
|
@ -1,42 +1,120 @@
|
|||
package jnet.lib.snmp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.snmp4j.CommunityTarget;
|
||||
import org.snmp4j.PDU;
|
||||
import org.snmp4j.Snmp;
|
||||
import org.snmp4j.TransportMapping;
|
||||
import org.snmp4j.event.ResponseEvent;
|
||||
import org.snmp4j.smi.OID;
|
||||
import org.snmp4j.smi.OctetString;
|
||||
import org.snmp4j.smi.UdpAddress;
|
||||
import org.snmp4j.smi.Variable;
|
||||
import org.snmp4j.smi.VariableBinding;
|
||||
import org.snmp4j.event.ResponseListener;
|
||||
import org.snmp4j.smi.*;
|
||||
import org.snmp4j.transport.DefaultUdpTransportMapping;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import org.snmp4j.CommunityTarget;
|
||||
|
||||
public class SNMPTrafficMonitor {
|
||||
|
||||
private Snmp snmp;
|
||||
|
||||
private String address;
|
||||
private String port;
|
||||
private String community;
|
||||
private int version;
|
||||
private ScheduledExecutorService scheduler;
|
||||
private int interfaceIndex;
|
||||
|
||||
private long previousInOctets = 0;
|
||||
private long previousOutOctets = 0;
|
||||
private long previousTimestamp = System.currentTimeMillis();
|
||||
private final List<TrafficListener> listeners = new ArrayList<>();
|
||||
private Snmp snmp;
|
||||
private Timer timer;
|
||||
|
||||
public SNMPTrafficMonitor(String ip, String port, String community, int version) throws IOException {
|
||||
this.address = ip + "/" + port;
|
||||
private long prevInOctets = -1;
|
||||
private long prevOutOctets = -1;
|
||||
private long prevTimestamp = -1;
|
||||
|
||||
// OIDy pro SNMP dotaz na traffic
|
||||
private static final String IF_IN_OCTETS = "1.3.6.1.2.1.2.2.1.10"; // Inbound traffic
|
||||
private static final String IF_OUT_OCTETS = "1.3.6.1.2.1.2.2.1.16"; // Outbound traffic
|
||||
|
||||
public SNMPTrafficMonitor(String targetAddress, String port, String community, int version, int interfaceIndex) {
|
||||
|
||||
this.address = targetAddress;
|
||||
this.port = port;
|
||||
this.community = community;
|
||||
this.version = version;
|
||||
|
||||
this.interfaceIndex = interfaceIndex;
|
||||
}
|
||||
|
||||
public void addTrafficListener(TrafficListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void startMonitoring(long intervalMs) throws IOException {
|
||||
TransportMapping<UdpAddress> transport = new DefaultUdpTransportMapping();
|
||||
snmp = new Snmp(transport);
|
||||
transport.listen();
|
||||
|
||||
timer = new Timer(true);
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
queryTraffic();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 0, intervalMs);
|
||||
}
|
||||
|
||||
public void stopMonitoring() throws IOException {
|
||||
if (timer != null) {
|
||||
timer.cancel();
|
||||
}
|
||||
if (snmp != null) {
|
||||
snmp.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void queryTraffic() throws IOException {
|
||||
PDU pdu = new PDU();
|
||||
pdu.add(new VariableBinding(new OID(IF_IN_OCTETS + "." + interfaceIndex)));
|
||||
pdu.add(new VariableBinding(new OID(IF_OUT_OCTETS + "." + interfaceIndex)));
|
||||
pdu.setType(PDU.GET);
|
||||
|
||||
|
||||
CommunityTarget target = createTarget();
|
||||
|
||||
snmp.send(pdu, target, null, new ResponseListener() {
|
||||
@Override
|
||||
public void onResponse(ResponseEvent event) {
|
||||
PDU response = event.getResponse();
|
||||
if (response != null) {
|
||||
long inOctets = response.get(0).getVariable().toLong();
|
||||
long outOctets = response.get(1).getVariable().toLong();
|
||||
long currentTimestamp = System.currentTimeMillis();
|
||||
|
||||
if (prevInOctets != -1 && prevOutOctets != -1) {
|
||||
double elapsedSeconds = (currentTimestamp - prevTimestamp) / 1000.0;
|
||||
|
||||
long inBps = (long) ((inOctets - prevInOctets) / elapsedSeconds);
|
||||
long outBps = (long) ((outOctets - prevOutOctets) / elapsedSeconds);
|
||||
|
||||
// Zavoláme listener s hodnotami v Bps (Byte per Second)
|
||||
notifyListeners(inBps, outBps);
|
||||
}
|
||||
|
||||
prevInOctets = inOctets;
|
||||
prevOutOctets = outOctets;
|
||||
prevTimestamp = currentTimestamp;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void notifyListeners(long inBps, long outBps) {
|
||||
for (TrafficListener listener : listeners) {
|
||||
listener.onTrafficUpdate(inBps, outBps);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -45,110 +123,14 @@ public class SNMPTrafficMonitor {
|
|||
private CommunityTarget createTarget() {
|
||||
CommunityTarget target = new CommunityTarget();
|
||||
target.setCommunity(new OctetString(this.community));
|
||||
target.setAddress(new UdpAddress(this.address));
|
||||
target.setAddress(new UdpAddress(this.address+"/"+this.port));
|
||||
target.setVersion(this.version);
|
||||
target.setRetries(2);
|
||||
target.setTimeout(1500);
|
||||
return target;
|
||||
}
|
||||
|
||||
public void getTraffic(int interfaceIndex) throws Exception {
|
||||
// OID pro přijaté a odeslané bajty specifického rozhraní
|
||||
OID oidInOctets = new OID("1.3.6.1.2.1.2.2.1.10." + interfaceIndex); // ifInOctets pro daný interface
|
||||
OID oidOutOctets = new OID("1.3.6.1.2.1.2.2.1.16." + interfaceIndex); // ifOutOctets pro daný interface
|
||||
|
||||
// Vytvoření SNMP požadavku pro získání hodnot
|
||||
PDU pdu = new PDU();
|
||||
pdu.add(new VariableBinding(oidInOctets)); // Přidaní OID pro příchozí traffic
|
||||
pdu.add(new VariableBinding(oidOutOctets)); // Přidaní OID pro odchozí traffic
|
||||
pdu.setType(PDU.GET);
|
||||
|
||||
// Definování cíle SNMP zařízení
|
||||
CommunityTarget target = createTarget();
|
||||
|
||||
// Odeslání požadavku a zpracování odpovědi
|
||||
ResponseEvent response = snmp.get(pdu, target);
|
||||
|
||||
if (response != null && response.getResponse() != null) {
|
||||
PDU responsePDU = response.getResponse();
|
||||
Variable inOctets = responsePDU.getVariableBindings().get(0).getVariable();
|
||||
Variable outOctets = responsePDU.getVariableBindings().get(1).getVariable();
|
||||
|
||||
long currentInOctets = inOctets.toLong();
|
||||
long currentOutOctets = outOctets.toLong();
|
||||
long currentTimestamp = System.currentTimeMillis();
|
||||
|
||||
// Vypočítání traffiku za sekundu
|
||||
long deltaInOctets = currentInOctets - previousInOctets;
|
||||
long deltaOutOctets = currentOutOctets - previousOutOctets;
|
||||
long deltaTime = currentTimestamp - previousTimestamp;
|
||||
|
||||
// Vypočítání traffiku za sekundu
|
||||
double inTrafficPerSecond = (deltaInOctets * 1000.0) / deltaTime; // Počet bajtů za sekundu
|
||||
double outTrafficPerSecond = (deltaOutOctets * 1000.0) / deltaTime;
|
||||
|
||||
System.out.println("Traffic na rozhrani " + interfaceIndex + ":");
|
||||
System.out.println("Prijaty traffic (In Octets): " + formatBytes(currentInOctets) + " | Za sekundu: " + formatBytesPerSecond(inTrafficPerSecond));
|
||||
System.out.println("Odeslany traffic (Out Octets): " + formatBytes(currentOutOctets) + " | Za sekundu: " + formatBytesPerSecond(outTrafficPerSecond));
|
||||
|
||||
// Uložení aktuálních hodnot pro další výpočet
|
||||
previousInOctets = currentInOctets;
|
||||
previousOutOctets = currentOutOctets;
|
||||
previousTimestamp = currentTimestamp;
|
||||
} else {
|
||||
System.out.println("Chyba při získávání dat z cílového zařízení.");
|
||||
}
|
||||
public interface TrafficListener {
|
||||
void onTrafficUpdate(long inBps, long outBps);
|
||||
}
|
||||
|
||||
public void startMonitoring(int interfaceIndex) {
|
||||
// Vytvoření plánovače pro spuštění každých 5 sekund
|
||||
scheduler = Executors.newScheduledThreadPool(1);
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
getTraffic(interfaceIndex);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 0, 3, TimeUnit.SECONDS); // Start okamžitě a potom každých 5 sekund
|
||||
}
|
||||
|
||||
public void stopMonitoring() {
|
||||
if (scheduler != null) {
|
||||
scheduler.shutdown(); // Umožní ukončení plánování
|
||||
}
|
||||
}
|
||||
|
||||
// Metoda pro přepočet traffiku za sekundu na čitelnou podobu
|
||||
private String formatBytesPerSecond(double bytesPerSecond) {
|
||||
if (bytesPerSecond < 1024) {
|
||||
return String.format("%.2f B/s", bytesPerSecond);
|
||||
} else if (bytesPerSecond < 1048576) { // 1024 * 1024
|
||||
return String.format("%.2f KB/s", bytesPerSecond / 1024.0);
|
||||
} else if (bytesPerSecond < 1073741824) { // 1024 * 1024 * 1024
|
||||
return String.format("%.2f MB/s", bytesPerSecond / 1048576.0);
|
||||
} else {
|
||||
return String.format("%.2f GB/s", bytesPerSecond / 1073741824.0);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws Exception {
|
||||
if (snmp != null) {
|
||||
snmp.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Metoda pro přepočet bajtů na čitelnou podobu
|
||||
private String formatBytes(long bytes) {
|
||||
if (bytes < 1024) {
|
||||
return bytes + " B";
|
||||
} else if (bytes < 1048576) { // 1024 * 1024
|
||||
return String.format("%.2f KB", bytes / 1024.0);
|
||||
} else if (bytes < 1073741824) { // 1024 * 1024 * 1024
|
||||
return String.format("%.2f MB", bytes / 1048576.0);
|
||||
} else {
|
||||
return String.format("%.2f GB", bytes / 1073741824.0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue