přidany propojení mezi objekty včetně zobrazovaní trafficu

NettyPing
Michal 2025-02-24 10:48:32 +01:00
parent b679150fc9
commit 9edc873fa2
5 changed files with 232 additions and 7 deletions

View File

@ -165,7 +165,9 @@ public class Database {
rs.getInt("id"), rs.getInt("id"),
rs.getString("name"), rs.getString("name"),
rs.getBoolean("locked"), rs.getBoolean("locked"),
getMapObject(rs.getInt("id"))); getMapObject(rs.getInt("id")),
getMapConnection(rs.getInt("id"))
);
list.add(m); list.add(m);
} }
rs.close(); rs.close();
@ -226,6 +228,38 @@ public class Database {
return list; return list;
} }
/**
* vráti seznam cpojeni v dane mape
* @param mapId
* @return
*/
public static ArrayList<jnet.lib.object.Connection> getMapConnection(int mapId) {
ArrayList<jnet.lib.object.Connection> list = new ArrayList<>();
String sql = "SELECT * FROM objectConnection WHERE map = " + mapId;
try {
Statement statement = getCurrentConnection().createStatement();
ResultSet r = statement.executeQuery(sql);
while (r.next()) {
int objId = r.getInt("id");
list.add(new jnet.lib.object.Connection(
objId,
r.getInt("sourceObject"),
r.getInt("destinationObject"),
r.getInt("type"),
r.getInt("map"),
r.getBoolean("readTraffic"),
r.getInt("sourceTraffic"),
r.getInt("sourceInterface")
));
}
r.close();
statement.close();
} catch (SQLException ex) {
LogFile.printErr("MySQL exception: " + ex.getMessage() + "query: " + sql);
}
return list;
}
/** /**
* vrati seznam objektu * vrati seznam objektu
* *
@ -508,13 +542,40 @@ public class Database {
return mo; return mo;
} }
/**
* prida nove propojeni mezi objekty
* @param c
* @return
*/
public static jnet.lib.object.Connection addConnection(jnet.lib.object.Connection c){
String query = "INSERT INTO objectConnection ("
+ "sourceObject,"
+ "destinationObject,"
+ "objectConnection.type,"
+ "map,"
+ "readTraffic,"
+ "sourceTraffic,"
+ "sourceInterface"
+ ") VALUES ("
+ "'" + c.getSourceObj() + "',"
+ "'" + c.getDestinationObj() + "',"
+ "'" + c.getType() + "',"
+ "'" + c.getMap() + "',"
+ "'" + (c.isReadTraffic() ? "1" : "0") + "',"
+ "'" + c.getTrafficObject() + "',"
+ "'" + c.getTrafficIface() + "'"
+ ")";
c.setId(insert(query));
return c;
}
/** /**
* nastaví stav zámku mapy * nastaví stav zámku mapy
* *
* @param state stav * @param state stav
* @param map map id * @param map map id
*/ */
static void mapLock(int state, int map) { public static void mapLock(int state, int map) {
update("UPDATE maps SET locked='" + state + "' WHERE id=" + map); update("UPDATE maps SET locked='" + state + "' WHERE id=" + map);
} }

View File

@ -4,8 +4,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jnet.lib.BuilddDate; import jnet.lib.BuilddDate;
import jnet.lib.LogFile; import jnet.lib.LogFile;
import jnet.lib.LogWindow; import jnet.lib.LogWindow;
@ -18,9 +16,9 @@ import jnet.lib.object.OnlineClients;
import jnet.lib.object.SnmpProbe; import jnet.lib.object.SnmpProbe;
import jnet.lib.object.SnmpProfile; import jnet.lib.object.SnmpProfile;
import jnet.lib.object.User; import jnet.lib.object.User;
import jnet.lib.snmp.SNMPTrafficMonitor;
import jnet.server.network.NettyServer; import jnet.server.network.NettyServer;
import jnet.server.probe.NettyPing; import jnet.server.probe.NettyPing;
import jnet.server.probe.TrafficProbe;
public class Server { public class Server {
@ -41,6 +39,7 @@ public class Server {
public static void main(String[] args) { public static void main(String[] args) {
// // Zde můžete zadat index rozhraní, které chcete monitorovat (např. 1 pro eth0) // // Zde můžete zadat index rozhraní, které chcete monitorovat (např. 1 pro eth0)
// int interfaceIndex = 1; // int interfaceIndex = 1;
// //
@ -53,7 +52,7 @@ public class Server {
// Thread.sleep(60000); // Monitoring běží 60 sekund // Thread.sleep(60000); // Monitoring běží 60 sekund
// monitor.stopMonitoring(); // monitor.stopMonitoring();
// monitor.stop(); // monitor.stop();
// } catch (Exception ex) { // } catch (Exception ex) {
// Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); // Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
// } // }
@ -147,6 +146,9 @@ public class Server {
NettyPing np = new NettyPing(); NettyPing np = new NettyPing();
np.start(); np.start();
TrafficProbe t = new TrafficProbe();
t.start();
} }
public static void restartServer() { public static void restartServer() {

View File

@ -14,6 +14,7 @@ import java.util.function.Consumer;
import jnet.lib.LogFile; import jnet.lib.LogFile;
import jnet.lib.Message; import jnet.lib.Message;
import jnet.lib.PasswordHashing; import jnet.lib.PasswordHashing;
import jnet.lib.object.Connection;
import jnet.lib.object.Event; import jnet.lib.object.Event;
import jnet.lib.object.Map; import jnet.lib.object.Map;
import jnet.lib.object.MapObject; import jnet.lib.object.MapObject;
@ -59,6 +60,8 @@ public class ServerMessageParser {
handlers.put(Message.SNMP_OBJECT_INFO, this::handleSnmpObjectInfo); handlers.put(Message.SNMP_OBJECT_INFO, this::handleSnmpObjectInfo);
handlers.put(Message.SNMP_OBJECT_INTERFACE, this::handleSnmpObjectInterface); handlers.put(Message.SNMP_OBJECT_INTERFACE, this::handleSnmpObjectInterface);
handlers.put(Message.SNMP_TEST, this::handleSnmpTest); handlers.put(Message.SNMP_TEST, this::handleSnmpTest);
handlers.put(Message.SNMP_CONNECTION_INTERFACE, this::handleSnmpConnectionInterface);
handlers.put(Message.CONNECTION_NEW, this::handlerConnectionNew);
} }
public void parse(Message msg) { public void parse(Message msg) {
@ -423,6 +426,41 @@ public class ServerMessageParser {
t1.start(); t1.start();
} }
private void handleSnmpConnectionInterface(Message msg){
Object[] msg_object = (Object[]) msg.getMsg();
String frameName = (String) msg_object[0];
MapObject obj = (MapObject) msg_object[1];
Thread t1 = new Thread() {
@Override
public void run() {
try {
for (SnmpProfile snmpProfile : Server.snmpProfile) {
if (snmpProfile.getId() == obj.getSnmpProfile()) {
SNMPInterfaceScanner snmp = new SNMPInterfaceScanner(obj.getIp(), snmpProfile.getPort(), snmpProfile.getCommunityRead(), snmpProfile.getVersion());
List<Interface> list = snmp.scanInterfaces();
Object[] o = {frameName, list};
send(Message.SNMP_CONNECTION_INTERFACE, o);
break;
}
}
} catch (Exception ex) {
LogFile.printErr("SNMP error: " + ex.getMessage());
}
}
};
t1.start();
}
private void handlerConnectionNew(Message msg) {
Connection c = Database.addConnection((Connection) msg.getMsg());
for (Map map : Server.maps) {
if (map.getId() == c.getMap()) {
map.getConnection().add(c);
}
}
sendAll(Message.CONNECTION_NEW, c);
}
/** /**
* *
* *

View File

@ -71,7 +71,7 @@ public class NettyPing extends Thread {
// Přidání nových hostů k monitorování // Přidání nových hostů k monitorování
for (MapObject obj : currentHosts) { for (MapObject obj : currentHosts) {
if (activeHosts.add(obj)) { // Přidá pouze pokud tam ještě není if (activeHosts.add(obj)) { // Přidá pouze pokud tam ještě není
LogFile.printInfo("Pridan host: " + obj.getIp()); LogFile.printDebug("Ping probe add: " + obj.getIp());
} }
} }

View File

@ -0,0 +1,124 @@
package jnet.server.probe;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import jnet.lib.LogFile;
import jnet.lib.Message;
import jnet.lib.object.Connection;
import jnet.lib.object.MapObject;
import jnet.lib.object.SnmpProfile;
import jnet.lib.snmp.SNMPTrafficMonitor;
import jnet.lib.snmp.SNMPTrafficMonitor.TrafficListener;
import jnet.server.Server;
import jnet.server.ServerMessageParser;
public class TrafficProbe extends Thread {
private static final int TIME = 3; // kontrola každé 3 sekundy
private final Map<Integer, TProbe> activeProbes = new HashMap<>();
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
@Override
public void run() {
LogFile.printInfo("Traffic probe start");
scheduler.scheduleAtFixedRate(this::updateProbe, 0, TIME, TimeUnit.SECONDS);
}
private void updateProbe() {
Set<Integer> currentProbeIds = new HashSet<>();
// Projde všechny connections a zjistí, které je potřeba monitorovat
for (jnet.lib.object.Map map : Server.maps) {
for (Connection connection : map.getConnection()) {
if (connection.isReadTraffic()) {
currentProbeIds.add(connection.getId());
if (!activeProbes.containsKey(connection.getId())) {
addProbe(connection);
}
}
}
}
// Odebrání neaktuálních probe
activeProbes.keySet().removeIf(id -> !currentProbeIds.contains(id));
}
private void addProbe(Connection c) {
try {
String host = "";
String port = "161";
String community = "public";
int version = 1;
for (jnet.lib.object.Map map : Server.maps) {
for (MapObject object : map.getObjects()) {
if (object.getId() == c.getSourceObj()) {
host = object.getIp();
SnmpProfile profile = Server.snmpProfile.stream()
.filter(p -> p.getId() == object.getSnmpProfile())
.findFirst()
.orElse(null);
if (profile != null) {
port = profile.getPort();
community = profile.getCommunityRead();
version = profile.getVersion();
}
break;
}
}
}
SNMPTrafficMonitor monitor = new SNMPTrafficMonitor(host, port, community, version, c.getTrafficIface());
monitor.addTrafficListener(new TrafficListener() {
@Override
public void onTrafficUpdate(long inBps, long outBps) {
Object[] o = {c.getId(), inBps, outBps};
ServerMessageParser.sendAll(Message.TRAFFIC, o);
}
});
activeProbes.put(c.getId(), new TProbe(c, monitor));
monitor.startMonitoring(2000);
LogFile.printInfo("Traffic probe add: " + c.getId());
} catch (IOException ex) {
LogFile.printErr("Traffic probe error: " + ex.getMessage());
}
}
public void stopProbe() {
scheduler.shutdown();
activeProbes.values().forEach(probe -> {
try {
probe.getMonitor().stopMonitoring();
} catch (IOException e) {
LogFile.printErr("Error stopping monitor for probe: " + probe.getConnection().getId() + " - " + e.getMessage());
}
});
activeProbes.clear();
LogFile.printInfo("Traffic probe stopped");
}
private static class TProbe {
private final Connection connection;
private final SNMPTrafficMonitor monitor;
public TProbe(Connection connection, SNMPTrafficMonitor monitor) {
this.connection = connection;
this.monitor = monitor;
}
public Connection getConnection() {
return connection;
}
public SNMPTrafficMonitor getMonitor() {
return monitor;
}
}
}