Monitor process (s), shutdown if CPU usage threshold is exceeded
I'm new to WMI
and System.Diagnostics
etc. I need to write something (service?) That monitors multiple processes for CPU% usage. When I am in Task Manager, the CPU column is the one I want (i.e. Percentage).
I need to be able to run this on a remote machine and test it for CPU usage every second or so. When the usage is over 30%, I need the offending service to restart automatically.
I've read all (read: most) of the related questions, and I think the PerformanceCounter is probably the one I need to use, but I'm not sure if I need one for each process, and how to find the process (it should be name-based, not PID-based).
Can anyone please advise.
<B> Summary
- I do not want to iterate over all processes on the machine (if this can be avoided)
- I need CPU usage% of process
- I need to restart the process if the usage is over 30%.
thanks
a source to share
I finally solved my problem by writing a wrapper around PerformanceCounter and Process. I have a class called ProcessMonitor that has PerformanceCounter and Process as properties. I created another class called ProcessMonitorList that does a bit more than the List contains. Then my main application Commandline has a time (true) that queries for all processes with the provided name and spills out their CPU usage every second. If the usage exceeds the threshold, the process is killed and, if necessary, an email is sent to the configured email address.
a source to share
You may consider the sources of one of my old free software projects: SETI @Home Service . He implemented a Windows service that pauses its client process (SETI @Home client) if the CPU usage has exceeded the certian value. He even added a couple of other sex twists, like pausing any other processes (ex: your favorite game), restarting reboots, built-in web server, etc.
The main drawback would be that the sources are in Ada, but most people find Ada easier to read, that most other languages they are not familiar with.
IIRC, the main point of the code is that there is an area of the Windows registry that you can look at that contains information about system performance. Basically, everything that is available inside perfmon can be read from the system registry using the program.
a source to share