Merge pull request #3639 from Machine-Garden-MUD/cmdserverload

Use consistent locale settings for system utilities
This commit is contained in:
Griatch 2024-10-21 20:09:44 +02:00 committed by GitHub
commit 5140f5b8d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,6 +13,7 @@ import traceback
import django
import evennia
import subprocess
import twisted
from django.conf import settings
from evennia.accounts.models import AccountDB
@ -862,16 +863,26 @@ class CmdServerLoad(COMMAND_DEFAULT_CLASS):
if not _RESOURCE:
import resource as _RESOURCE
env = os.environ.copy()
env["LC_NUMERIC"] = "C" # use default locale instead of system locale
loadavg = os.getloadavg()[0]
rmem = (
float(os.popen("ps -p %d -o %s | tail -1" % (pid, "rss")).read()) / 1000.0
) # resident memory
vmem = (
float(os.popen("ps -p %d -o %s | tail -1" % (pid, "vsz")).read()) / 1000.0
) # virtual memory
pmem = float(
os.popen("ps -p %d -o %s | tail -1" % (pid, "%mem")).read()
) # % of resident memory to total
# Helper function to run the ps command with a modified environment
def run_ps_command(command):
result = subprocess.run(
command, shell=True, env=env, stdout=subprocess.PIPE, text=True
)
return result.stdout.strip()
# Resident memory
rmem = float(run_ps_command(f"ps -p {pid} -o rss | tail -1")) / 1000.0
# Virtual memory
vmem = float(run_ps_command(f"ps -p {pid} -o vsz | tail -1")) / 1000.0
# Percentage of resident memory to total
pmem = float(run_ps_command(f"ps -p {pid} -o %mem | tail -1"))
rusage = _RESOURCE.getrusage(_RESOURCE.RUSAGE_SELF)
if "mem" in self.switches: