Home

Posts Tagged ‘ASPX’

.NET: Monitoring Windows processes from ASPX webpage

Saturday, January 3rd, 2009

I was recently working on some ScribbleLive features that require asynchronous processes to be running in the background on Windows. Since I want to make sure they keep running, I wanted Pingdom to be able to monitor (and alert) on them.

I was happy to find that in .NET, a web application can access the processes running on the host server. I whipped up this little bit of C# (which will be on an ASPX page) to check for the processes, and report if they are “UP” or “DOWN”. Pingdom will then check for the word “DOWN” on a request to the page and let me know if it finds one.

Here are the using statements that you have to have for this code to run:

using System.Diagnostics;
using System.Collections.ObjectModel;

Then here’s the bulk of it:

Collection<string> RunningProcesses = new Collection<string>();
RunningProcesses.Add( "Program1" );
RunningProcesses.Add( "Program2" );

Response.ContentType = "text/plain";

foreach ( Process proc in Process.GetProcesses() )
{
    if ( RunningProcesses.Contains( proc.ProcessName ) )
    {
        Response.Write( proc.ProcessName + ": UP" + System.Environment.NewLine );
        RunningProcesses.Remove( proc.ProcessName );
    }
}

foreach ( string s in RunningProcesses )
{
    Response.Write( s + ": DOWN" + System.Environment.NewLine );
}

When I have a bit more time, I’m going to add a call to Process.Start(…) to restart and processes that have gone down. You know, at some point this code will probably make me unnecessary :)