Difference between revisions of "Restart service if used more than x cpu or memory for the last 3 queries"

(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...")
 
Line 1: Line 1:
 
=== Create the scripts ===
 
=== Create the scripts ===
* Go to the game's settings. Click on the Custom Scripts icon. Add the following 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.
  
 
  '''Operating System:''' Any
 
  '''Operating System:''' Any
Line 22: Line 22:
 
  MaxStoredValues=3
 
  MaxStoredValues=3
 
  MaxCPU=50
 
  MaxCPU=50
  MaxMemoryMB=100
+
  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 31:
 
  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 50: Line 50:
 
  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 67:
 
  else :
 
  else :
 
   AllMemoryHigher=False
 
   AllMemoryHigher=False
 
+
 
  if AllCPUHigher | AllMemoryHigher :
 
  if AllCPUHigher | AllMemoryHigher :
 
   Script.WriteToConsole(String.Format("Restarting {0}...", ThisService.ConnectionInfo))
 
   Script.WriteToConsole(String.Format("Restarting {0}...", ThisService.ConnectionInfo))
 
   ThisService.Restart()
 
   ThisService.Restart()
 +
 +
 +
'''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)

Revision as of 15:18, 21 November 2016

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.
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=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("Restarting {0}...", ThisService.ConnectionInfo))
  ThisService.Restart()


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)
Retrieved from "https://help.tcadmin.com/index.php?title=Restart_service_if_used_more_than_x_cpu_or_memory_for_the_last_3_queries&oldid=1328"