Compare commits

...

4 Commits

Author SHA1 Message Date
Michal ca6161b505 drobne opravy 2025-02-13 18:29:11 +01:00
Michal 9bbbcb7e9f pridaní metody pro restart serveru 2025-02-13 18:28:55 +01:00
Michal 00bf11e673 2025-02-13 18:28:35 +01:00
Michal 839231901f uprava ping probe 2025-02-13 18:28:03 +01:00
9 changed files with 138 additions and 58 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.log

View File

@ -6,8 +6,9 @@ mysql_password=Michal2407
mysql_database=jnet
server_port=1225
debug=true
ping_attempt=2
ping_timeout=2000
ping_attempt=4
ping_timeout=2
ping_repeat=3
instability_attempt=2
instability_limit=1000
instability_limit=1

7
log
View File

@ -1,7 +0,0 @@
03.02.25 16:34:13 I ---------------------------------------------
03.02.25 16:34:13 I jNet Server
03.02.25 16:34:13 I Version: 1 (Mon Feb 03 16:33:44 CET 2025)
03.02.25 16:34:13 I ---------------------------------------------
03.02.25 16:34:13 I
03.02.25 16:34:13 I Loading config ...
03.02.25 16:34:13 E fail. Soubor konfigurace nenalezen config.properties (Systém nemůže nalézt uvedený soubor)

84
log.log
View File

@ -1,24 +1,60 @@
10.02.25 20:29:05 I ---------------------------------------------
10.02.25 20:29:05 I jNet Server
10.02.25 20:29:05 I Version: 1 (Sat Feb 08 19:21:37 CET 2025)
10.02.25 20:29:05 I ---------------------------------------------
10.02.25 20:29:05 I
10.02.25 20:29:05 I Loading config ...
10.02.25 20:29:05 I successfully
10.02.25 20:29:05 D Debug mode on
10.02.25 20:29:05 I Connecting to a selected database ...
10.02.25 20:29:06 I successfully
10.02.25 20:29:06 I Loading user list ...
10.02.25 20:29:06 I load 2 user(s)
10.02.25 20:29:06 I Loading map list ...
10.02.25 20:29:06 I load 6 map(s)
10.02.25 20:29:06 I Loading object list ...
10.02.25 20:29:06 I load 7 object(s)
10.02.25 20:29:06 I Loading object type ...
10.02.25 20:29:06 I load 12 type(s)
10.02.25 20:29:06 I Loading SNMP profile ...
10.02.25 20:29:06 I load 2 profile(s)
10.02.25 20:29:06 I Loading events ...
10.02.25 20:29:06 I load 0 event(s)
10.02.25 20:29:06 I Ping probe start
10.02.25 20:29:06 I Server is ready and listening on port 1225
13.02.25 18:7:16 I ---------------------------------------------
13.02.25 18:7:16 I jNet Server
13.02.25 18:7:16 I Version: 1 (Thu Feb 13 09:04:35 CET 2025)
13.02.25 18:7:16 I ---------------------------------------------
13.02.25 18:7:16 I
13.02.25 18:7:16 I Loading config ...
13.02.25 18:7:16 I successfully
13.02.25 18:7:16 D Debug mode on
13.02.25 18:7:16 I Connecting to a selected database ...
13.02.25 18:7:16 I successfully
13.02.25 18:7:16 I Loading user list ...
13.02.25 18:7:16 I load 2 user(s)
13.02.25 18:7:16 I Loading map list ...
13.02.25 18:7:16 I load 6 map(s)
13.02.25 18:7:16 I Loading object list ...
13.02.25 18:7:17 I load 8 object(s)
13.02.25 18:7:17 I Loading object type ...
13.02.25 18:7:17 I load 12 type(s)
13.02.25 18:7:17 I Loading SNMP profile ...
13.02.25 18:7:17 I load 2 profile(s)
13.02.25 18:7:17 I Loading events ...
13.02.25 18:7:17 I load 30 event(s)
13.02.25 18:7:17 I Ping probe start
13.02.25 18:7:17 I Server is ready and listening on port 1225
13.02.25 18:7:49 D -> Message type: 1
13.02.25 18:7:49 I Authorization request send (kubernetes.docker.internal)
13.02.25 18:7:49 D <- Message type: 3
13.02.25 18:7:49 D -> Message type: 4
13.02.25 18:7:49 I User: michal logged in (kubernetes.docker.internal)
13.02.25 18:7:49 D <- Message type: 5
13.02.25 18:7:49 D <- Message type: 9
13.02.25 18:7:49 D <- For all. Message type: 7
13.02.25 18:7:49 D <- Message type: 13
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 11
13.02.25 18:7:49 D <- Message type: 11
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 14
13.02.25 18:7:49 D <- Message type: 110
13.02.25 18:7:49 D <- Message type: 107
13.02.25 18:7:49 D <- Message type: 107
13.02.25 18:8:31 D -> Message type: 105
13.02.25 18:8:31 D <- For all. Message type: 105
13.02.25 18:8:31 D <- For all. Message type: 108

View File

@ -635,7 +635,6 @@ public class Database {
try {
String select_query = "SELECT * FROM events WHERE object = '" + objectId + "' && type = '" + type + "' ORDER BY id DESC LIMIT 1";
System.err.println(select_query);
PreparedStatement stmt = getCurrentConnection().prepareStatement(select_query);
ResultSet rs = stmt.executeQuery();
// Process the result

View File

@ -1,5 +1,7 @@
package jnet.server;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jnet.lib.BuilddDate;
@ -45,7 +47,6 @@ public class Server {
}
LogWindow.addRow("tset");
LogFile.clear();
LogFile.setDebug(false);
@ -143,5 +144,30 @@ public class Server {
np.start();
}
public static void restartServer() {
new Thread(() -> {
try {
Thread.sleep(5000);
try {
String java = System.getProperty("java.home") + "/bin/java";
File jarFile = new File(Server.class
.getProtectionDomain().getCodeSource().getLocation().getPath());
List<String> command = new ArrayList<>();
command.add(java);
command.add("-jar");
command.add(jarFile.getAbsolutePath());
new ProcessBuilder(command).start();
System.exit(0);
} catch (IOException ex) {
LogFile.printErr("Restart failed! " + ex.getMessage());
}
} catch (InterruptedException e) {
LogFile.printErr("Restart failed! " + e.getMessage());
}
}).start();
}
}

View File

@ -4,6 +4,7 @@ import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;
import java.util.ListIterator;
import jnet.lib.LogFile;
@ -79,6 +80,7 @@ public class ServerMessageParser {
sendAll(new Message(Message.ONLINE_CLIENTS, Server.onlineClients));
// odeslat nastaveni serveru
ServerConfig sc = new ServerConfig(
Server.config.getInt("ping_repeat"),
Server.config.getInt("ping_attempt"),
Server.config.getInt("ping_timeout"),
Server.config.getInt("instability_attempt"),
@ -109,6 +111,16 @@ public class ServerMessageParser {
LogFile.printInfo("Authorization failed (ser: " + username + " )");
}
break;
case Message.RESTART:
LogFile.printInfo("Prijat pozadavek na restart serveru");
Server.restartServer();
break;
case Message.UPTIME:
long uptime = ManagementFactory.getRuntimeMXBean().getUptime();
send(Message.UPTIME, uptime);
break;
case Message.NEW_OBJECT_TYPE:
ObjectType not = (ObjectType) msg.getMsg();
@ -136,7 +148,7 @@ public class ServerMessageParser {
case Message.ADD_OBJECT:
MapObject mo = Database.addMapObject((MapObject) msg.getMsg());
// pridat do seznamu
iteratorMapObject.add(mo);
Server.mapObject.add(mo);
// odesle všem novy objekt
sendAll(new Message(Message.ADD_OBJECT, mo));
// prida event a odesle ho
@ -273,6 +285,7 @@ public class ServerMessageParser {
case Message.SERVER_CONFIG:
ServerConfig sc = (ServerConfig) msg.getMsg();
// ulozeni
Server.config.setInt("ping_repeat", sc.getPingRepeat());
Server.config.setInt("ping_attempt", sc.getPingAttempt());
Server.config.setInt("ping_timeout", sc.getPingTimeout());
Server.config.setInt("instability_attempt", sc.getInstabilityAttempt());

View File

@ -11,39 +11,36 @@ import jnet.lib.Status;
import jnet.lib.object.MapObject;
import jnet.server.Server;
public class NettyPing extends Thread {
private static final HashedWheelTimer TIMER = new HashedWheelTimer(); // Netty časovač
public NettyPing() {
}
@Override
public void start() {
public void run() {
LogFile.printInfo("Ping probe start");
for (MapObject mapObject : Server.mapObject) {
schedulePing(mapObject);
}
}
private static void schedulePing(MapObject object) {
TIMER.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) {
pingHost(object);
timeout.timer().newTimeout(this, 3, TimeUnit.SECONDS); // Opakuje každých 5 sekund
timeout.timer().newTimeout(this, Server.config.getInt("ping_repeat"), TimeUnit.SECONDS); // Opakuje každých x sekund
}
}, 0, TimeUnit.SECONDS);
}
private static void pingHost(MapObject object) {
try {
String command = System.getProperty("os.name").startsWith("Windows")
? "ping -n 1 " + object.getIp()
: "ping -c 1 " + object.getIp();
? "ping -n 1 -w " + String.valueOf((Server.config.getInt("ping_timeout")*1000)) + " " + object.getIp() // Timeout 2000 ms (Windows)
: "ping -c 1 -W " + String.valueOf(Server.config.getInt("ping_timeout")) + " " + object.getIp(); // Timeout 2 sec (Linux)
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
@ -60,16 +57,18 @@ public class NettyPing extends Thread {
}
//System.out.println("Ping na " + object.getIp() + (success ? " uspesny! RTT: " + timeMs + " ms" : " selhal!"));
if (object.getStatus() != Status.OK) {
PingResult.stateOnline(object);
if (success) {
if (object.getStatus() != Status.OK) {
PingResult.stateOnline(object);
}
} else {
PingResult.stateOffline(object);
}
process.waitFor();
} catch (Exception e) {
System.err.println("Chyba pri pingu na " + object.getIp());
PingResult.stateOffline(object);
//e.printStackTrace();
LogFile.printErr("Chyba pri pingu na " + object.getIp() + " " + e.getMessage());
}
}
@ -82,6 +81,5 @@ public class NettyPing extends Thread {
}
return -1;
}
}

View File

@ -10,14 +10,26 @@ import jnet.server.ServerMessageParser;
public class PingResult {
private static final int POKUS = 2;
private static final int POKUS = Server.config.getInt("ping_attempt");
public static void stateOnline(MapObject obj) {
// oveří jestli neni zarizeni vypnuto
if (obj.isActive()) {
if (obj.isActive() && obj.getStatus() != Status.OK) {
// vynuluje ping pokusy
obj.setPingAttemp(0);
// pokud byl předchozí stav offline ukončí event
if (obj.getStatus() == Status.OFFLINE) {
// aktualizace eventu
Event event = Database.endEvent(obj.getId(), Event.TYPE_PING);
for (int i = 0; i < Server.events.size(); i++) {
if (Server.events.get(i).getId() == event.getId()) {
Server.events.set(i, event);
}
}
// odeslani aktualizovaného eventu
ServerMessageParser.sendAll(Message.EVENT_UPDATE, event);
}
// nastavi status objektu
obj.setStatus(Status.OK);
// odesle informaci o novem stavu klientum
@ -28,7 +40,8 @@ public class PingResult {
public static void stateOffline(MapObject obj) {
// oveří jestli neni zarizeni vypnuto
if (obj.isActive()) {
if (obj.getPingAttemp() >= POKUS) {
if (obj.getPingAttemp() >= POKUS && obj.getStatus() != Status.OFFLINE) {
System.err.println("objekt: " + obj.getName() + " pokus: " + obj.getPingAttemp());
// nastavi status objektu
obj.setStatus(Status.OFFLINE);
// odesle informaci o novem stavu klientum