Create the scripts
- Go to the game's settings. Click on the Custom Scripts icon. Add the following scripts.
Operating System: Any
Description: Check resource usage.
Script Engine: IronPython
Event: Query Monitoring
Ignore execution errors Checked
Script:
import clr
import System
clr.AddReference("TCAdmin.SDK")
from System.Collections.Generic import List
from System.IO import Path, File
from System import Exception
from System import String
from TCAdmin.SDK.Misc import ObjectXml
#Script parameters
MaxStoredValues=3
MaxCPU=50
MaxMemoryMB=100
CPUFile=Path.Combine(ThisService.RootDirectory, "cpu.xml")
MemoryFile=Path.Combine(ThisService.RootDirectory, "memory.xml")
#Read CPU and memory values stored in files
if File.Exists(CPUFile) :
CPUValues = ObjectXml.XmlToObject(File.ReadAllText(CPUFile), (List[float]()).GetType())
else :
CPUValues = List[float]()
if File.Exists(MemoryFile) :
MemoryValues = ObjectXml.XmlToObject(File.ReadAllText(MemoryFile), (List[float]()).GetType())
else :
MemoryValues = List[float]()
#Add current CPU and memory values
CPUValues.Add(ThisService.Status.CpuLastSecond)
MemoryValues.Add(ThisService.Status.MemoryLastSecond/1024/1024)
#Limit stored values to specified length
while CPUValues.Count > MaxStoredValues :
CPUValues.RemoveAt(0)
while MemoryValues.Count > MaxStoredValues :
MemoryValues.RemoveAt(0)
#Save values to files
File.WriteAllText(CPUFile, ObjectXml.ObjectToXml(CPUValues))
File.WriteAllText(MemoryFile, ObjectXml.ObjectToXml(MemoryValues))
#Check if stored values are higher than limits
AllCPUHigher=True
if CPUValues.Count == MaxStoredValues :
for i in range(0, MaxStoredValues - 1):
if CPUValues.Item[i] < MaxCPU :
AllCPUHigher=False
else :
AllCPUHigher=False
AllMemoryHigher=True
if MemoryValues.Count == MaxStoredValues :
for i in range(0, MaxStoredValues - 1):
if MemoryValues.Item[i] < MaxMemoryMB :
AllMemoryHigher=False
else :
AllMemoryHigher=False
if AllCPUHigher | AllMemoryHigher :
Script.WriteToConsole(String.Format("Restarting {0}...", ThisService.ConnectionInfo))
ThisService.Restart()