Difference between revisions of "Restart service if used more than x cpu or memory for the last 3 queries"
From TCAdmin 2.0 Documentation
(Created page with "=== Create the scripts === * Go to the game's settings. Click on the Custom Scripts icon. Add the following scripts. '''Operating System:''' Any '''Description:''' Check re...") |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | === Create a query monitoring rule === | ||
+ | In the game's query monitoring add this rule and configure it to restart the service: | ||
+ | HighResources=True | ||
+ | |||
=== Create the scripts === | === Create the scripts === | ||
− | + | Go to the game's settings. Click on the Custom Scripts icon. Add the following scripts. Then click on Update Existing Services in the game's settings. These can also be configured as global scripts. | |
'''Operating System:''' Any | '''Operating System:''' Any | ||
Line 15: | Line 19: | ||
from System.Collections.Generic import List | from System.Collections.Generic import List | ||
from System.IO import Path, File | from System.IO import Path, File | ||
− | |||
from System import String | from System import String | ||
from TCAdmin.SDK.Misc import ObjectXml | from TCAdmin.SDK.Misc import ObjectXml | ||
− | #Script parameters | + | #Script parameters. Change values as needed. |
MaxStoredValues=3 | MaxStoredValues=3 | ||
MaxCPU=50 | MaxCPU=50 | ||
− | MaxMemoryMB= | + | MaxMemoryMB=4096 |
CPUFile=Path.Combine(ThisService.RootDirectory, "cpu.xml") | CPUFile=Path.Combine(ThisService.RootDirectory, "cpu.xml") | ||
MemoryFile=Path.Combine(ThisService.RootDirectory, "memory.xml") | MemoryFile=Path.Combine(ThisService.RootDirectory, "memory.xml") | ||
Line 31: | Line 34: | ||
else : | else : | ||
CPUValues = List[float]() | CPUValues = List[float]() | ||
− | + | ||
if File.Exists(MemoryFile) : | if File.Exists(MemoryFile) : | ||
MemoryValues = ObjectXml.XmlToObject(File.ReadAllText(MemoryFile), (List[float]()).GetType()) | MemoryValues = ObjectXml.XmlToObject(File.ReadAllText(MemoryFile), (List[float]()).GetType()) | ||
Line 46: | Line 49: | ||
while MemoryValues.Count > MaxStoredValues : | while MemoryValues.Count > MaxStoredValues : | ||
MemoryValues.RemoveAt(0) | MemoryValues.RemoveAt(0) | ||
− | + | ||
#Save values to files | #Save values to files | ||
File.WriteAllText(CPUFile, ObjectXml.ObjectToXml(CPUValues)) | File.WriteAllText(CPUFile, ObjectXml.ObjectToXml(CPUValues)) | ||
File.WriteAllText(MemoryFile, ObjectXml.ObjectToXml(MemoryValues)) | File.WriteAllText(MemoryFile, ObjectXml.ObjectToXml(MemoryValues)) | ||
− | + | ||
#Check if stored values are higher than limits | #Check if stored values are higher than limits | ||
AllCPUHigher=True | AllCPUHigher=True | ||
Line 67: | Line 70: | ||
else : | else : | ||
AllMemoryHigher=False | AllMemoryHigher=False | ||
+ | |||
+ | if AllCPUHigher | AllMemoryHigher : | ||
+ | Script.WriteToConsole(String.Format("High resources detected for {0}", ThisService.ConnectionInfo)) | ||
+ | QueryResults.Rules.Add("HighResources", "True") | ||
+ | |||
+ | |||
+ | '''Operating System:''' Any | ||
+ | '''Description:''' Delete cpu and memory files. | ||
+ | '''Script Engine:''' IronPython | ||
+ | '''Event:''' Before Started | ||
+ | '''Ignore execution errors''' Checked | ||
+ | '''Script:''' | ||
− | + | import clr | |
− | + | import System | |
− | + | from System.IO import Path, File | |
+ | CPUFile=Path.Combine(ThisService.RootDirectory, "cpu.xml") | ||
+ | MemoryFile=Path.Combine(ThisService.RootDirectory, "memory.xml") | ||
+ | |||
+ | if File.Exists(CPUFile) : | ||
+ | File.Delete(CPUFile) | ||
+ | if File.Exists(MemoryFile) : | ||
+ | File.Delete(MemoryFile) |
Latest revision as of 17:15, 18 January 2018
Create a query monitoring rule
In the game's query monitoring add this rule and configure it to restart the service:
HighResources=True
Create the scripts
Go to the game's settings. Click on the Custom Scripts icon. Add the following scripts. Then click on Update Existing Services in the game's settings. These can also be configured as global 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 String from TCAdmin.SDK.Misc import ObjectXml #Script parameters. Change values as needed. MaxStoredValues=3 MaxCPU=50 MaxMemoryMB=4096 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("High resources detected for {0}", ThisService.ConnectionInfo)) QueryResults.Rules.Add("HighResources", "True")
Operating System: Any Description: Delete cpu and memory files. Script Engine: IronPython Event: Before Started Ignore execution errors Checked Script:
import clr import System from System.IO import Path, File CPUFile=Path.Combine(ThisService.RootDirectory, "cpu.xml") MemoryFile=Path.Combine(ThisService.RootDirectory, "memory.xml") if File.Exists(CPUFile) : File.Delete(CPUFile) if File.Exists(MemoryFile) : File.Delete(MemoryFile)