监控进程,如果 CPU 使用率超过阈值则关闭
我是 WMI
和 System.Diagnostics
等新手。 我需要编写一些东西(服务?)来监视多个进程的 CPU 使用率%。 当我在任务管理器中时,CPU 列就是我想要的列(即百分比)。
我需要能够在远程计算机上运行它,并让它检查 CPU 使用情况 每隔一秒左右。当使用率超过 30% 时,我需要自动重新启动有问题的服务。
我已经阅读了所有(阅读:大多数)相关问题,我认为 PerformanceCounter 可能是我需要使用的那个,但我不确定每个进程是否需要其中之一,以及如何找到进程(它必须基于名称,而不是基于 PID)。
任何人都可以请建议。
摘要
- 我不想循环遍历机器上的所有进程(如果可以避免的话)
- 我需要进程的 CPU 使用率 %
- 如果使用率超过 30%,我需要重新启动进程。
谢谢
I am new to WMI
and System.Diagnostics
etc.
I need to write something (a service?) that monitors several processes for CPU Usage %.
When I am in Task Manager, the CPU column is the one that I want (i.e. the percentage).
I need to be able to run this on a remote machine, and have it check the CPU Usage
every second or so. When the usage is over 30%, I need for the offending service to be restarted automatically.
I have read through all(read:most) of the Related questions, and I think the PerformanceCounter is probably the one I need to use, but I am unsure as to whether or not I need one of these for each process, and how to locate the process, (it has to be name based, not PID based).
Can anyone please advise.
Summary
- I don't want to loop through all processes on the machine (if I can avoid it)
- I need the CPU Usage % of the process
- I need to restart a process if the usage is over 30%.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以考虑查看我的一个旧自由软件项目的源代码: SETI@Home 服务。它实现了一项 Windows 服务,如果 CPU 使用率超过某个特定值,该服务会暂停其客户端进程(SETI@Home 客户端)。它甚至还添加了一些其他性感的曲折,例如在某些其他进程启动时暂停(例如:您最喜欢的游戏)、崩溃重新启动、内置网络服务器等。
主要缺点是源代码位于 Ada 中,但大多数人发现 Ada 比他们不熟悉的大多数其他语言更容易阅读。
IIRC,代码的基本要点是,您可以查看 Windows 注册表的一个区域,其中包含系统性能信息。基本上,程序可以从系统注册表中读取 perfmon 中可用的任何内容。
You might consider looking at the sources for one of my old Free Software projects: The SETI@Home Service. It implemented a Windows service that pauses its client process (the SETI@Home client) if CPU usage got over a certian value. It even added a couple other sexy twists, like pausing any time certian other processes start up (eg: your favorite game), crash restarts, built-in webserver, etc.
The main drawback is going to be that the sources are in Ada, but most people find Ada easier to read that most other languages they aren't familiar with.
IIRC, the basic gist of the code is that there's an area of the Windows registry you can look in that contains system performance information. Basically anything available inside perfmon can be read from the system registry by a program.
我最终通过编写 PerformanceCounter 和 Process 的包装器解决了自己的问题。
我有一个名为 ProcessMonitor 的类,它具有 PerformanceCounter 和 Process 作为属性。我创建了另一个名为 ProcessMonitorList 的类,它的作用比包含 List 多一点。然后我的主要命令行应用程序有一个 while(true) 查询具有所提供名称的所有进程,并每秒吐出它们的 CPU 使用情况。如果使用量超过阈值,则进程将被终止,并且可以选择将电子邮件发送到配置的电子邮件地址。
I finally solved my own problem by writing a wrapper around PerformanceCounter and Process.
I have a class called a ProcessMonitor which has a PerformanceCounter and a Process as properties. I created another class called a ProcessMonitorList which does a little more than contains List. Then my primary Commandline app has a while(true) which queries for all processes with the provided name and spits out at every second, their CPU usage. If the usage exceeds the threshold, the process is killed and optionally an email is sent to a configured email address.