2022-05-20 20:33:42 +00:00
|
|
|
from __future__ import annotations
|
2022-05-28 16:00:25 +00:00
|
|
|
from contextlib import redirect_stderr
|
2020-08-19 01:04:43 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import datetime
|
2021-08-28 22:48:30 +00:00
|
|
|
import base64
|
2022-05-20 19:05:37 +00:00
|
|
|
import typing as t
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2022-05-28 16:00:25 +00:00
|
|
|
from app.classes.shared.null_writer import NullWriter
|
2022-03-08 04:40:44 +00:00
|
|
|
from app.classes.minecraft.mc_ping import ping
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.management import HostStats
|
|
|
|
from app.classes.models.servers import HelperServers
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
2022-05-20 20:33:42 +00:00
|
|
|
|
2022-05-28 16:00:25 +00:00
|
|
|
with redirect_stderr(NullWriter()):
|
|
|
|
import psutil
|
|
|
|
|
2022-05-20 20:33:42 +00:00
|
|
|
if t.TYPE_CHECKING:
|
|
|
|
from app.classes.shared.main_controller import Controller
|
2020-08-19 01:04:43 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-05-20 19:05:37 +00:00
|
|
|
class DiskDataDict(t.TypedDict):
|
|
|
|
device: str
|
|
|
|
total_raw: int
|
|
|
|
total: str
|
|
|
|
used_raw: int
|
|
|
|
used: str
|
|
|
|
free_raw: int
|
|
|
|
free: str
|
|
|
|
percent_used: float
|
|
|
|
fs: str
|
|
|
|
mount: str
|
|
|
|
|
|
|
|
|
|
|
|
class NodeStatsDict(t.TypedDict):
|
|
|
|
boot_time: str
|
|
|
|
cpu_usage: float
|
|
|
|
cpu_count: int
|
|
|
|
cpu_cur_freq: float
|
|
|
|
cpu_max_freq: float
|
|
|
|
mem_percent: float
|
|
|
|
mem_usage_raw: int
|
|
|
|
mem_usage: str
|
|
|
|
mem_total_raw: int
|
|
|
|
mem_total: str
|
|
|
|
disk_data: t.List[DiskDataDict]
|
|
|
|
|
|
|
|
|
|
|
|
class NodeStatsReturnDict(t.TypedDict):
|
|
|
|
node_stats: NodeStatsDict
|
|
|
|
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class Stats:
|
2022-05-20 19:05:37 +00:00
|
|
|
helper: Helpers
|
|
|
|
controller: Controller
|
|
|
|
|
2022-05-28 16:32:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def try_get_boot_time():
|
|
|
|
try:
|
|
|
|
return datetime.datetime.fromtimestamp(
|
|
|
|
psutil.boot_time(), datetime.timezone.utc
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2022-05-28 16:43:37 +00:00
|
|
|
logger.debug(f"error while getting boot time due to {e}")
|
2022-05-28 16:32:54 +00:00
|
|
|
# unix epoch with no timezone data
|
|
|
|
return datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def try_get_cpu_usage():
|
|
|
|
try:
|
|
|
|
return psutil.cpu_percent(interval=0.5) / psutil.cpu_count()
|
|
|
|
except Exception as e:
|
2022-05-28 16:43:37 +00:00
|
|
|
logger.debug(f"error while getting cpu percentage due to {e}")
|
2022-05-28 16:32:54 +00:00
|
|
|
return -1
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, helper, controller):
|
|
|
|
self.helper = helper
|
2021-03-22 04:02:18 +00:00
|
|
|
self.controller = controller
|
|
|
|
|
2022-05-20 19:05:37 +00:00
|
|
|
def get_node_stats(self) -> NodeStatsReturnDict:
|
2020-12-19 13:39:31 +00:00
|
|
|
try:
|
|
|
|
cpu_freq = psutil.cpu_freq()
|
|
|
|
except NotImplementedError:
|
|
|
|
cpu_freq = psutil._common.scpufreq(current=0, min=0, max=0)
|
2022-05-20 19:05:37 +00:00
|
|
|
memory = psutil.virtual_memory()
|
2022-05-28 16:32:54 +00:00
|
|
|
try:
|
|
|
|
node_stats: NodeStatsDict = {
|
|
|
|
"boot_time": str(Stats.try_get_boot_time()),
|
|
|
|
"cpu_usage": Stats.try_get_cpu_usage(),
|
|
|
|
"cpu_count": psutil.cpu_count(),
|
|
|
|
"cpu_cur_freq": round(cpu_freq[0], 2),
|
|
|
|
"cpu_max_freq": cpu_freq[2],
|
|
|
|
"mem_percent": memory.percent,
|
|
|
|
"mem_usage_raw": memory.used,
|
|
|
|
"mem_usage": Helpers.human_readable_file_size(memory.used),
|
|
|
|
"mem_total_raw": memory.total,
|
|
|
|
"mem_total": Helpers.human_readable_file_size(memory.total),
|
|
|
|
"disk_data": Stats._try_all_disk_usage(),
|
|
|
|
}
|
|
|
|
except Exception as e:
|
2022-05-28 16:43:37 +00:00
|
|
|
logger.debug(f"error while getting host stats due to {e}")
|
2022-05-28 16:32:54 +00:00
|
|
|
node_stats: NodeStatsDict = {
|
|
|
|
"boot_time": str(
|
|
|
|
datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
|
|
|
|
),
|
|
|
|
"cpu_usage": -1,
|
|
|
|
"cpu_count": -1,
|
|
|
|
"cpu_cur_freq": -1,
|
|
|
|
"cpu_max_freq": -1,
|
|
|
|
"mem_percent": -1,
|
|
|
|
"mem_usage_raw": -1,
|
|
|
|
"mem_usage": "",
|
|
|
|
"mem_total_raw": -1,
|
|
|
|
"mem_total": "",
|
|
|
|
"disk_data": [],
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
# server_stats = self.get_servers_stats()
|
|
|
|
# data['servers'] = server_stats
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2022-05-20 19:05:37 +00:00
|
|
|
return {
|
|
|
|
"node_stats": node_stats,
|
|
|
|
}
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2022-05-28 16:32:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def _try_get_process_stats(process):
|
|
|
|
try:
|
|
|
|
return Stats._get_process_stats(process)
|
|
|
|
except Exception as e:
|
2022-05-28 16:43:37 +00:00
|
|
|
logger.debug(f"error while getting process stats due to {e}")
|
2022-05-28 16:32:54 +00:00
|
|
|
return {"cpu_usage": -1, "memory_usage": -1, "mem_percentage": -1}
|
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
@staticmethod
|
2021-09-25 19:29:28 +00:00
|
|
|
def _get_process_stats(process):
|
|
|
|
if process is None:
|
2022-03-23 02:50:12 +00:00
|
|
|
process_stats = {"cpu_usage": 0, "memory_usage": 0, "mem_percentage": 0}
|
2020-08-19 01:04:43 +00:00
|
|
|
return process_stats
|
2021-09-25 19:29:28 +00:00
|
|
|
else:
|
|
|
|
process_pid = process.pid
|
2020-08-19 01:04:43 +00:00
|
|
|
try:
|
|
|
|
p = psutil.Process(process_pid)
|
|
|
|
dummy = p.cpu_percent()
|
|
|
|
|
|
|
|
# call it first so we can be more accurate per the docs
|
|
|
|
# https://giamptest.readthedocs.io/en/latest/#psutil.Process.cpu_percent
|
|
|
|
|
|
|
|
real_cpu = round(p.cpu_percent(interval=0.5) / psutil.cpu_count(), 2)
|
|
|
|
|
|
|
|
# this is a faster way of getting data for a process
|
|
|
|
with p.oneshot():
|
|
|
|
process_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"cpu_usage": real_cpu,
|
2022-04-11 10:08:36 +00:00
|
|
|
"memory_usage": Helpers.human_readable_file_size(
|
|
|
|
p.memory_info()[0]
|
|
|
|
),
|
2022-03-23 02:50:12 +00:00
|
|
|
"mem_percentage": round(p.memory_percent(), 0),
|
2020-08-19 01:04:43 +00:00
|
|
|
}
|
|
|
|
return process_stats
|
|
|
|
|
|
|
|
except Exception as e:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Unable to get process details for pid: {process_pid} Error: {e}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-08-19 01:04:43 +00:00
|
|
|
|
|
|
|
# Dummy Data
|
|
|
|
process_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"cpu_usage": 0,
|
|
|
|
"memory_usage": 0,
|
2020-08-19 01:04:43 +00:00
|
|
|
}
|
|
|
|
return process_stats
|
|
|
|
|
2022-05-28 16:32:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def _try_all_disk_usage():
|
|
|
|
try:
|
|
|
|
return Stats._all_disk_usage()
|
|
|
|
except Exception as e:
|
2022-05-28 16:43:37 +00:00
|
|
|
logger.debug(f"error while getting disk data due to {e}")
|
2022-05-28 16:32:54 +00:00
|
|
|
return []
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# Source: https://github.com/giampaolo/psutil/blob/master/scripts/disk_usage.py
|
2020-08-19 01:04:43 +00:00
|
|
|
@staticmethod
|
2022-05-20 19:05:37 +00:00
|
|
|
def _all_disk_usage() -> t.List[DiskDataDict]:
|
2020-08-19 01:04:43 +00:00
|
|
|
disk_data = []
|
|
|
|
# print(templ % ("Device", "Total", "Used", "Free", "Use ", "Type","Mount"))
|
|
|
|
|
|
|
|
for part in psutil.disk_partitions(all=False):
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
2022-03-23 02:50:12 +00:00
|
|
|
if "cdrom" in part.opts or part.fstype == "":
|
2020-08-19 01:04:43 +00:00
|
|
|
# skip cd-rom drives with no disk in it; they may raise
|
|
|
|
# ENOENT, pop-up a Windows GUI error for a non-ready
|
|
|
|
# partition or just hang.
|
|
|
|
continue
|
|
|
|
usage = psutil.disk_usage(part.mountpoint)
|
|
|
|
disk_data.append(
|
|
|
|
{
|
2022-03-23 02:50:12 +00:00
|
|
|
"device": part.device,
|
2022-05-20 19:05:37 +00:00
|
|
|
"total_raw": usage.total,
|
2022-04-11 05:23:55 +00:00
|
|
|
"total": Helpers.human_readable_file_size(usage.total),
|
2022-05-20 19:05:37 +00:00
|
|
|
"used_raw": usage.used,
|
2022-04-11 05:23:55 +00:00
|
|
|
"used": Helpers.human_readable_file_size(usage.used),
|
2022-05-20 19:05:37 +00:00
|
|
|
"free_raw": usage.free,
|
2022-04-11 05:23:55 +00:00
|
|
|
"free": Helpers.human_readable_file_size(usage.free),
|
2022-05-20 19:05:37 +00:00
|
|
|
"percent_used": usage.percent,
|
2022-03-23 02:50:12 +00:00
|
|
|
"fs": part.fstype,
|
|
|
|
"mount": part.mountpoint,
|
2020-08-19 01:04:43 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return disk_data
|
|
|
|
|
|
|
|
@staticmethod
|
2022-03-02 02:43:36 +00:00
|
|
|
def get_world_size(server_path):
|
2020-08-19 01:04:43 +00:00
|
|
|
|
|
|
|
total_size = 0
|
2022-03-02 15:36:24 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
total_size = Helpers.get_dir_size(server_path)
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
level_total_size = Helpers.human_readable_file_size(total_size)
|
2022-03-02 03:30:53 +00:00
|
|
|
|
|
|
|
return level_total_size
|
2020-08-19 01:04:43 +00:00
|
|
|
|
2022-03-04 00:41:30 +00:00
|
|
|
def get_server_players(self, server_id):
|
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
server = HelperServers.get_server_data_by_id(server_id)
|
2022-03-04 00:41:30 +00:00
|
|
|
|
|
|
|
logger.info(f"Getting players for server {server}")
|
|
|
|
|
|
|
|
# get our settings and data dictionaries
|
|
|
|
# server_settings = server.get('server_settings', {})
|
|
|
|
# server_data = server.get('server_data_obj', {})
|
|
|
|
|
|
|
|
# TODO: search server properties file for possible override of 127.0.0.1
|
2022-03-23 02:50:12 +00:00
|
|
|
internal_ip = server["server_ip"]
|
|
|
|
server_port = server["server_port"]
|
2022-03-04 00:41:30 +00:00
|
|
|
|
2022-04-12 01:34:46 +00:00
|
|
|
logger.debug(f"Pinging {internal_ip} on port {server_port}")
|
2022-04-14 02:10:25 +00:00
|
|
|
if HelperServers.get_server_type_by_id(server_id) != "minecraft-bedrock":
|
2022-03-04 00:41:30 +00:00
|
|
|
int_mc_ping = ping(internal_ip, int(server_port))
|
|
|
|
|
|
|
|
ping_data = {}
|
|
|
|
|
|
|
|
# if we got a good ping return, let's parse it
|
|
|
|
if int_mc_ping:
|
|
|
|
ping_data = Stats.parse_server_ping(int_mc_ping)
|
2022-03-23 02:50:12 +00:00
|
|
|
return ping_data["players"]
|
2022-03-04 00:41:30 +00:00
|
|
|
return []
|
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def parse_server_ping(ping_obj: object):
|
|
|
|
online_stats = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
online_stats = json.loads(ping_obj.players)
|
|
|
|
|
|
|
|
except Exception as e:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Unable to read json from ping_obj: {e}")
|
|
|
|
|
2021-08-28 22:48:30 +00:00
|
|
|
try:
|
|
|
|
server_icon = base64.encodebytes(ping_obj.icon)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_icon = server_icon.decode("utf-8")
|
|
|
|
except Exception as e:
|
2021-08-28 22:48:30 +00:00
|
|
|
server_icon = False
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Unable to read the server icon : {e}")
|
2021-08-28 22:48:30 +00:00
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
ping_data = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"online": online_stats.get("online", 0),
|
|
|
|
"max": online_stats.get("max", 0),
|
|
|
|
"players": online_stats.get("players", 0),
|
|
|
|
"server_description": ping_obj.description,
|
|
|
|
"server_version": ping_obj.version,
|
|
|
|
"server_icon": server_icon,
|
2020-08-19 01:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ping_data
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-03-01 03:40:11 +00:00
|
|
|
@staticmethod
|
2022-04-14 02:10:25 +00:00
|
|
|
def parse_server_raknet_ping(ping_obj: object):
|
2022-03-01 03:40:11 +00:00
|
|
|
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
server_icon = base64.encodebytes(ping_obj["icon"])
|
|
|
|
except Exception as e:
|
2022-03-01 03:40:11 +00:00
|
|
|
server_icon = False
|
|
|
|
logger.info(f"Unable to read the server icon : {e}")
|
|
|
|
ping_data = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"online": ping_obj["server_player_count"],
|
|
|
|
"max": ping_obj["server_player_max"],
|
|
|
|
"players": [],
|
|
|
|
"server_description": ping_obj["server_edition"],
|
|
|
|
"server_version": ping_obj["server_version_name"],
|
|
|
|
"server_icon": server_icon,
|
2022-03-01 03:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ping_data
|
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
def record_stats(self):
|
|
|
|
stats_to_send = self.get_node_stats()
|
2022-05-20 19:05:37 +00:00
|
|
|
node_stats = stats_to_send["node_stats"]
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
HostStats.insert(
|
2022-03-23 02:50:12 +00:00
|
|
|
{
|
2022-04-14 02:10:25 +00:00
|
|
|
HostStats.boot_time: node_stats.get("boot_time", "Unknown"),
|
|
|
|
HostStats.cpu_usage: round(node_stats.get("cpu_usage", 0), 2),
|
|
|
|
HostStats.cpu_cores: node_stats.get("cpu_count", 0),
|
|
|
|
HostStats.cpu_cur_freq: node_stats.get("cpu_cur_freq", 0),
|
|
|
|
HostStats.cpu_max_freq: node_stats.get("cpu_max_freq", 0),
|
|
|
|
HostStats.mem_usage: node_stats.get("mem_usage", "0 MB"),
|
|
|
|
HostStats.mem_percent: node_stats.get("mem_percent", 0),
|
|
|
|
HostStats.mem_total: node_stats.get("mem_total", "0 MB"),
|
|
|
|
HostStats.disk_json: node_stats.get("disk_data", "{}"),
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
).execute()
|
|
|
|
|
2020-09-22 16:11:16 +00:00
|
|
|
# delete old data
|
2022-04-11 05:23:55 +00:00
|
|
|
max_age = self.helper.get_setting("history_max_age")
|
2020-08-19 01:04:43 +00:00
|
|
|
now = datetime.datetime.now()
|
2022-05-25 13:14:42 +00:00
|
|
|
minimum_to_exist = now - datetime.timedelta(days=max_age)
|
2020-08-24 17:08:17 +00:00
|
|
|
|
2022-05-25 11:49:17 +00:00
|
|
|
HostStats.delete().where(HostStats.time < minimum_to_exist).execute()
|