jrun monitor |
Ketan Jetty
enthusiasm for technology
|
||
tags
functions
scopes
|
|||
|
jrun monitor The following code is written in CSharp to monitor the underlying jrun. This code will restart coldfusion 8 whenever the CPU and memory usage exceeds 90% of the alloted resources for more than 5 minutes. Here is a screencast of "how to deploy this code as a windows service packaged as an installer."
Coldfusion JRun monitoring and auto-restart code in CSharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Diagnostics.PerformanceData;
using System.Management;
using System.IO;
using System.Threading;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static string service = "JRUN";
static string serviceName = "ColdFusion 8 Application Server";
static bool jrunRunning = false;
static bool jrunRestart = false;
static int samplingInterval = 6000; // 6 seconds
static Queue qCPU = new Queue(30);
static Queue qMEM = new Queue(30);
static float cpuAvg = 0.0F;
static float memAvg = 0.0F;
static void Main(string[] args)
{
monitor(service, serviceName);
Console.Read();
}
static void monitor(string service, string serviceName)
{
// infinite loop
while (true)
{
jrunRunning = false;
// get the process as process list array
Process[] processList = Process.GetProcessesByName(service);
// if the process is avaialbe, then the process is running
if (processList != null && processList.Length == 1)
{
jrunRunning = true;
}
else
{
// if the process has not been restarted then restart it
if (!jrunRestart)
{
jrunRestart = true;
}
}
// restart the process only if is not running and is not already started
if (!jrunRunning && jrunRestart)
{
jrunRestart = false;
qCPU.Clear();
qMEM.Clear();
cpuAvg = 0;
memAvg = 0;
try
{
// process is not running, STOP and START the process, and give a gap of 5 seconds between the processes
processStop(serviceName);
Thread.Sleep(5000);
processStart(serviceName);
}
catch (Exception ex)
{
// if any exception, restart the server again
jrunRestart = true;
}
}
if (jrunRunning)
{
foreach (Process proc in processList)
{
// process specific CPU and Memory counters
PerformanceCounter cpu = new PerformanceCounter("Process", "% Processor Time", proc.ProcessName);
PerformanceCounter mem = new PerformanceCounter("Process", "Private Bytes", proc.ProcessName);
cpu.NextValue();
mem.NextValue();
System.Threading.Thread.Sleep(1000);
float fcpu = cpu.NextValue();
// sometimes, the cpu will be over 100% (Example 171.34%], if so set the value to a max of 100%
// to ensure a bad cpu data not to mess up the averages
if (fcpu > 100)
{
fcpu = 100;
}
float fmem = mem.NextValue() / 1024 / 1024;
Console.Write(proc.ProcessName + ": " + fcpu + "%" + " | " + fmem + "Mb | ");
// queue is used to store the latest 30 samples (only) for average calculation
// push to the queue (FIFO)
qCPU.Enqueue(fcpu);
qMEM.Enqueue(fmem);
if (qCPU.Count > 30)
{
// pop of the queue (FIFO)
qCPU.Dequeue();
qMEM.Dequeue();
}
// calculate the cpu average
cpuAvg = 0;
foreach (object obj in qCPU)
{
cpuAvg += (float)obj;
//Console.WriteLine("qCPU: " + obj);
}
cpuAvg = cpuAvg / qCPU.Count;
Console.Write("cpuAvg: " + cpuAvg + " | ");
// calculate the memeory average
memAvg = 0;
foreach (object obj in qMEM)
{
memAvg += (float)obj;
//Console.WriteLine("qMEM: " + obj);
}
memAvg = memAvg / qMEM.Count;
Console.Write("memAvg: " + memAvg + " | ");
Console.WriteLine("");
}
// if the cpu average exceeds 90% and memory average exceeds 1GB (hard coded here)
// restart Jrun instance
if (cpuAvg > 90 && memAvg > 1024)
{
//Console.WriteLine("cpuAvg: " + cpuAvg + " | memAvg: " + memAvg);
qCPU.Clear();
qMEM.Clear();
// process is not running, STOP and START the process
processStop(serviceName);
Thread.Sleep(5000);
processStart(serviceName);
}
}
Thread.Sleep(samplingInterval - 1000);
}
}
static void processStop(string serviceName)
{
// stop the process
Console.WriteLine("stopping - " + serviceName);
string cmd = "net stop \"" + serviceName + "\"";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + cmd);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
p.WaitForExit(30000); //wait for 30 sec
int exitCode = p.ExitCode;
Console.WriteLine("exitCode: " + exitCode);
Console.WriteLine("stopped - " + serviceName);
p.Close();
}
static void processStart(string serviceName)
{
// start the process
Console.WriteLine("starting - " + serviceName);
string cmd = "net start \"" + serviceName + "\"";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + cmd);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
p.WaitForExit(30000); //wait for 30 sec
int exitCode = p.ExitCode;
Console.WriteLine("exitCode: " + exitCode);
Console.WriteLine("started - " + serviceName);
p.Close();
}
static void TESTJRunRestart2()
{
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("C:\\ColdFusion8\\runtime\\bin\\jrun.exe");
//ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("C:\\ColdFusion8\\runtime\\bin\\jrunsvc.exe","-start");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Minimized;
myProcessStartInfo.Arguments = "-start";
//ProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
Console.WriteLine("started 2");
myProcess.Close();
}
}
}
|
|
||
| Ketan Jetty @ 2010. All Rights Reserved. | |||