Skip to content

Instantly share code, notes, and snippets.

@marceloinacio
Created November 10, 2015 00:35
Show Gist options
  • Select an option

  • Save marceloinacio/8256128eeb0cfe49724d to your computer and use it in GitHub Desktop.

Select an option

Save marceloinacio/8256128eeb0cfe49724d to your computer and use it in GitHub Desktop.
Thread to monitoring the Server
private void Process()
{
while (!finalizeService)
{
PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Processor";
PC.CounterName = "% Processor Time";
PC.InstanceName = "_Total";
PC.ReadOnly = true;
var value = PC.NextValue();
Thread.Sleep(1000);
value = PC.NextValue();
PC.Close();
PC.Dispose();
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
var ramValue = ramCounter.NextValue();
if (ramValue <= MinRAMAvailable)
{
SendAlertMessage(AlertType.RAM_ALERT, value,Convert.ToInt64(ramValue));
}
if (value >= MaxCPUUsage)
{
totalHits = totalHits + 1;
if (totalHits == Period)
{
SendAlertMessage(AlertType.PROCESS_ALERT, value, Convert.ToInt64(ramValue));
totalHits = 0;
}
}
else
{
totalHits = 0;
}
}
eventLog.WriteEntry(ServiceName + " stoped.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment