-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_fivem.py
101 lines (83 loc) · 3.2 KB
/
monitor_fivem.py
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
import requests
import subprocess
import datetime
import re
import os
import time
os.system('color 0A')
# Configuration
FIVEM_SERVER_EXECUTABLE_PATH = r""
DISCORD_WEBHOOK_URL = ''
sent_errors = set()
def strip_ansi_escape_codes(text):
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)
def extract_error_details(clean_message):
pattern = r'\[\s*(.*?)\]\s*(.*)'
match = re.search(pattern, clean_message)
if match:
return match.group(1), match.group(2)
parts = clean_message.split(":", 1)
if len(parts) > 1:
return parts[0], parts[1]
return "Unknown Script", clean_message
def send_to_discord(message):
global sent_errors
clean_message = strip_ansi_escape_codes(message)
if clean_message in sent_errors:
return
if len(clean_message) > 1024:
clean_message = clean_message[:1021] + "..."
script_name, error_detail = extract_error_details(clean_message)
uptime = datetime.datetime.now() - datetime.datetime.fromtimestamp(os.path.getctime(FIVEM_SERVER_EXECUTABLE_PATH))
embed_color = 0xff0000 if "critical" in error_detail.lower() else 0xffa500 if "warning" in error_detail.lower() else 0xffff00
data = {
"embeds": [{
"title": "FiveM Server Error",
"description": "An error was detected in the FiveM server console.",
"color": embed_color,
"fields": [
{"name": "Script Name", "value": f"**{script_name}**", "inline": False},
{"name": "Error Detail", "value": f"```{error_detail.strip()}```", "inline": False},
{"name": "Server Uptime", "value": f"**{uptime}**", "inline": True}
],
"footer": {"text": "FiveM Error Monitor"},
"timestamp": datetime.datetime.utcnow().isoformat()
}]
}
response = requests.post(DISCORD_WEBHOOK_URL, json=data)
if response.status_code != 204:
print(f"Failed to send message to Discord. Status code: {response.status_code}")
print("Response content:", response.text)
else:
sent_errors.add(clean_message)
def is_fivem_server_running():
try:
result = subprocess.run(['tasklist'], stdout=subprocess.PIPE, text=True)
return FIVEM_SERVER_EXECUTABLE_PATH in result.stdout
except Exception as e:
print(f"Error checking server status: {e}")
return False
def monitor_fivem_server():
while True:
try:
process = subprocess.Popen(
[FIVEM_SERVER_EXECUTABLE_PATH],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=os.path.dirname(FIVEM_SERVER_EXECUTABLE_PATH),
universal_newlines=True,
encoding='utf-8',
errors='replace'
)
for line in iter(process.stdout.readline, ''):
print(line, end='')
if "error" in line.lower():
send_to_discord(line)
process.wait()
except Exception as e:
print(f"Error starting or monitoring the server: {e}")
finally:
time.sleep(10)
if __name__ == "__main__":
monitor_fivem_server()