serverwatcher 1.2__tar.gz → 2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: serverwatcher
3
- Version: 1.2
3
+ Version: 2.1
4
4
  Summary: A HungerLib-powered Minecraft server automation engine.
5
5
  Author: iFamished
6
6
  License: MIT
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "serverwatcher"
10
- version = "1.2"
10
+ version = "2.1"
11
11
  description = "A HungerLib-powered Minecraft server automation engine."
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.10"
@@ -1,12 +1,114 @@
1
+ import os
1
2
  import time
3
+ import yaml
2
4
  from zoneinfo import ZoneInfo
3
- from hungerlib.addons import (
4
- Snapshot,
5
- snapSchedule,
6
- waitForOnline,
7
- validateAll,
8
- runCountdownEvents
9
- )
5
+
6
+ from hungerlib import Panel, HungerLogger
7
+ from hungerlib.addons import clearTerminal, Snapshot, snapSchedule, waitForOnline, validateAll, runCountdownEvents
8
+ from hungerlib.servers import MinecraftServer, GenericServer
9
+
10
+ from serverwatcher import WatcherConfig, WatcherMessages
11
+
12
+
13
+ DEFAULT_CONFIG = {
14
+ "panel": {
15
+ "name": "My Panel",
16
+ "url": "https://example.com",
17
+ "api_key": "CHANGE_ME"
18
+ },
19
+
20
+ "origin": {
21
+ "server_id": "CHANGE_ME"
22
+ },
23
+
24
+ "server": {
25
+ "name": "My SMP",
26
+ "server_id": "CHANGE_ME",
27
+ "domain": "example.com",
28
+ "port": 25565,
29
+ "rcon_port": 25575,
30
+ "rcon_password": "password",
31
+ "tps_command": "ticks"
32
+ },
33
+
34
+ "watcher": {
35
+ "restart_soon_schedule_id": 0,
36
+ "origin_disable_schedule_id": 0,
37
+
38
+ "thresholds": {
39
+ "ram": 6,
40
+ "cpu": 150,
41
+ "uptime_hours": 12,
42
+ "tps": 19.5
43
+ },
44
+
45
+ "weights": {
46
+ "restart_soon": 3,
47
+ "ram": 1,
48
+ "cpu": 1,
49
+ "uptime": 1,
50
+ "tps": 1,
51
+ "low_uptime": 5,
52
+ "per_player": 1
53
+ },
54
+
55
+ "gaps": {
56
+ "low": 120,
57
+ "high": 60
58
+ },
59
+
60
+ "restart": {
61
+ "wait_seconds": 45,
62
+ "online_timeout": 120,
63
+ "online_interval": 2
64
+ }
65
+ },
66
+
67
+ "messages": {
68
+ "prefix": "<gray>[Watcher]</gray>",
69
+
70
+ "log_start": "Evaluating server health...",
71
+ "log_validation_fail": "Validation failed!",
72
+ "log_no_restart": "No restart needed.",
73
+ "log_immediate_restart": "Restarting immediately...",
74
+ "log_scheduled": "Scheduling restart...",
75
+ "log_gap_low": "Gap is low ({gap}), using low-gap schedule.",
76
+ "log_gap_high": "Gap is high ({gap}), using high-gap schedule.",
77
+
78
+ "reason_restart_soon": "Restart soon schedule is active.",
79
+ "reason_ram": "RAM {ram}GB >= threshold {threshold}GB",
80
+ "reason_cpu": "CPU {cpu}% >= threshold {threshold}%",
81
+ "reason_uptime": "Uptime {uptime} >= {threshold} hours",
82
+ "reason_tps": "TPS {tps} <= threshold {threshold}",
83
+ "reason_low_uptime": "Uptime too low ({uptime})",
84
+ "reason_players": "There {verb} {count} {plural} online.",
85
+
86
+ "broadcast_restart_at": "<yellow>Server restart scheduled at {time}",
87
+
88
+ "broadcast_minute": {
89
+ 10: "<red>Restart in 10 minutes!",
90
+ 5: "<red>Restart in 5 minutes!",
91
+ 1: "<red>Restart in 1 minute!"
92
+ },
93
+
94
+ "broadcast_second": {
95
+ 10: "<red>Restart in 10 seconds!",
96
+ 5: "<red>Restart in 5 seconds!",
97
+ 1: "<red>Restarting now!"
98
+ }
99
+ }
100
+ }
101
+
102
+
103
+ def load_or_create_config(path="config.yaml"):
104
+ if not os.path.exists(path):
105
+ with open(path, "w") as f:
106
+ yaml.dump(DEFAULT_CONFIG, f, sort_keys=False)
107
+ print("Generated default config.yaml — please edit it.")
108
+ time.sleep(2)
109
+ with open(path, "r") as f:
110
+ return yaml.safe_load(f)
111
+
10
112
 
11
113
  class ServerWatcher:
12
114
  def __init__(self, server, origin, panel, logger, config, messages):
@@ -39,6 +141,7 @@ class ServerWatcher:
39
141
 
40
142
  if alive:
41
143
  self.log.info("Server is back online!")
144
+ self.server.sendBroadcast(f'{self.msg.prefix}<green>Restart successful!')
42
145
  self.origin.enableSchedule(self.cfg.origin_disable_schedule_id)
43
146
  else:
44
147
  self.log.error("Server failed to restart!")
@@ -161,3 +264,74 @@ class ServerWatcher:
161
264
  self.schedule_restart(self.cfg.high_gap_minutes)
162
265
 
163
266
  self.restart_and_wait()
267
+
268
+
269
+
270
+ # -------------------------
271
+ # MAIN
272
+ # -------------------------
273
+
274
+ cfg = load_or_create_config()
275
+
276
+ panel = Panel(**cfg["panel"])
277
+
278
+ origin = GenericServer(
279
+ name="Origin",
280
+ panel=panel,
281
+ server_id=cfg["origin"]["server_id"]
282
+ )
283
+
284
+ srv = cfg["server"]
285
+ server = MinecraftServer(
286
+ name=srv["name"],
287
+ panel=panel,
288
+ server_id=srv["server_id"],
289
+ server_domain=srv["domain"],
290
+ server_port=srv["port"],
291
+ rcon_port=srv["rcon_port"],
292
+ rcon_password=srv["rcon_password"],
293
+ tpsCommand=srv["tps_command"]
294
+ )
295
+
296
+ log = HungerLogger(
297
+ name=f"ServerWatcher-{srv['name']}",
298
+ server=server,
299
+ log_path="/home/container/logs/",
300
+ console_backspaces=8
301
+ )
302
+
303
+ w = cfg["watcher"]
304
+ config = WatcherConfig(
305
+ restart_soon_schedule_id=w["restart_soon_schedule_id"],
306
+ origin_disable_schedule_id=w["origin_disable_schedule_id"],
307
+
308
+ ram_threshold=w["thresholds"]["ram"],
309
+ cpu_threshold=w["thresholds"]["cpu"],
310
+ uptime_hours_threshold=w["thresholds"]["uptime_hours"],
311
+ tps_threshold=w["thresholds"]["tps"],
312
+
313
+ weight_restart_soon=w["weights"]["restart_soon"],
314
+ weight_ram=w["weights"]["ram"],
315
+ weight_cpu=w["weights"]["cpu"],
316
+ weight_uptime=w["weights"]["uptime"],
317
+ weight_tps=w["weights"]["tps"],
318
+ weight_low_uptime=w["weights"]["low_uptime"],
319
+ weight_per_player=w["weights"]["per_player"],
320
+
321
+ low_gap_minutes=w["gaps"]["low"],
322
+ high_gap_minutes=w["gaps"]["high"],
323
+
324
+ restart_wait_seconds=w["restart"]["wait_seconds"],
325
+ restart_online_timeout=w["restart"]["online_timeout"],
326
+ restart_online_interval=w["restart"]["online_interval"],
327
+ )
328
+
329
+ messages = WatcherMessages(**cfg["messages"])
330
+
331
+ clearTerminal()
332
+
333
+ Watcher = ServerWatcher(server, origin, panel, log, config, messages)
334
+
335
+ while True:
336
+ Watcher.evaluate()
337
+ time.sleep(60)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: serverwatcher
3
- Version: 1.2
3
+ Version: 2.1
4
4
  Summary: A HungerLib-powered Minecraft server automation engine.
5
5
  Author: iFamished
6
6
  License: MIT
File without changes
File without changes
File without changes