pep.py/helpers/systemHelper.py

107 lines
3.0 KiB
Python
Raw Normal View History

2016-05-18 17:12:46 +00:00
from objects import glob
from constants import serverPackets
2016-09-04 10:01:10 +00:00
from helpers import consoleHelper
2016-04-19 17:40:59 +00:00
import psutil
import os
import sys
import threading
import signal
from helpers import logHelper as log
2016-09-04 10:01:10 +00:00
from constants import bcolors
import time
import math
2016-04-19 17:40:59 +00:00
2016-09-04 10:01:10 +00:00
def dispose():
"""
Perform some clean up. Called on shutdown.
:return:
"""
print("> Disposing server... ")
glob.fileBuffers.flushAll()
consoleHelper.printColored("Goodbye!", bcolors.GREEN)
2016-04-19 17:40:59 +00:00
def runningUnderUnix():
"""
Get if the server is running under UNIX or NT
return --- True if running under UNIX, otherwise False
"""
return True if os.name == "posix" else False
def scheduleShutdown(sendRestartTime, restart, message = "", delay=20):
2016-04-19 17:40:59 +00:00
"""
Schedule a server shutdown/restart
sendRestartTime -- time (seconds) to wait before sending server restart packets to every client
restart -- if True, server will restart. if False, server will shudown
message -- if set, send that message to every client to warn about the shutdown/restart
"""
# Console output
log.info("Pep.py will {} in {} seconds!".format("restart" if restart else "shutdown", sendRestartTime+delay))
log.info("Sending server restart packets in {} seconds...".format(sendRestartTime))
2016-04-19 17:40:59 +00:00
# Send notification if set
if message != "":
glob.tokens.enqueueAll(serverPackets.notification(message))
# Schedule server restart packet
threading.Timer(sendRestartTime, glob.tokens.enqueueAll, [serverPackets.banchoRestart(delay*2*1000)]).start()
2016-04-19 17:40:59 +00:00
glob.restarting = True
# Restart/shutdown
if restart:
action = restartServer
else:
action = shutdownServer
# Schedule actual server shutdown/restart some seconds after server restart packet, so everyone gets it
threading.Timer(sendRestartTime+delay, action).start()
2016-04-19 17:40:59 +00:00
def restartServer():
"""Restart pep.py script"""
log.info("Restarting pep.py...")
2016-09-04 10:01:10 +00:00
dispose()
2016-04-19 17:40:59 +00:00
os.execv(sys.executable, [sys.executable] + sys.argv)
def shutdownServer():
"""Shutdown pep.py"""
log.info("Shutting down pep.py...")
2016-09-04 10:01:10 +00:00
dispose()
2016-04-19 17:40:59 +00:00
sig = signal.SIGKILL if runningUnderUnix() else signal.CTRL_C_EVENT
os.kill(os.getpid(), sig)
def getSystemInfo():
"""
Get a dictionary with some system/server info
return -- ["unix", "connectedUsers", "webServer", "cpuUsage", "totalMemory", "usedMemory", "loadAverage"]
"""
2016-09-02 15:45:10 +00:00
data = {"unix": runningUnderUnix(), "connectedUsers": len(glob.tokens.tokens), "matches": len(glob.matches.matches)}
2016-04-19 17:40:59 +00:00
# General stats
delta = time.time()-glob.startTime
days = math.floor(delta/86400)
delta -= days*86400
hours = math.floor(delta/3600)
delta -= hours*3600
minutes = math.floor(delta/60)
delta -= minutes*60
seconds = math.floor(delta)
data["uptime"] = "{}d {}h {}m {}s".format(days, hours, minutes, seconds)
2016-04-19 17:40:59 +00:00
data["cpuUsage"] = psutil.cpu_percent()
memory = psutil.virtual_memory()
data["totalMemory"] = "{0:.2f}".format(memory.total/1074000000)
data["usedMemory"] = "{0:.2f}".format(memory.active/1074000000)
2016-04-19 17:40:59 +00:00
# Unix only stats
2016-09-02 15:45:10 +00:00
if data["unix"]:
2016-04-19 17:40:59 +00:00
data["loadAverage"] = os.getloadavg()
else:
data["loadAverage"] = (0,0,0)
return data