简体   繁体   English

当CPU使用率超过限制一段时间时发出警报

[英]Alert when CPU usage stays over a limit for a period of time

I have a Windows 2003 Server and i need to code a service which would email me when the cpu usage stays over 90% for more than 5 minutes. 我有一台Windows 2003 Server,我需要编写一项服务,当cpu的使用率超过90%超过5分钟时,它将向我发送电子邮件。 How could i do that? 我该怎么办?

First you would have the read the processor load like: 首先,您将读取处理器负载,例如:

How to get the CPU Usage in C#? 如何在C#中获取CPU使用率?

Second, you would have to use a timer which checks for processor time in some interval. 其次,您必须使用一个计时器,该计时器在一定间隔内检查处理器时间。 The standard Timer from .Net is properly precise enough when we talk 5 min. 当我们通话5分钟时,.Net的标准Timer足够精确。 The interval could be something like a few seconds. 时间间隔可能约为几秒钟。 On Tick, you measure the CPU and if it is LOWER than 90% then reset some start-DateTime and if it is OVER 90% then check if the start-DateTime is over 5 min old. 在Tick上,测量CPU,如果它低于90%,则重置一些start-DateTime,如果它超过90%,则检查start-DateTime是否超过5分钟。

And last you would need to send a mail like this: 最后,您需要发送这样的邮件:

Sending E-mail using C# 使用C#发送电子邮件

This way uses a SMTP server which you can install local or otherwise you properly have a SMTP for one of your e-mail etc. You can also send a mail without with more overhead. 这种方式使用的是SMTP服务器,您可以在本地安装它,也可以为其中的一封电子邮件等安装适当的SMTP。还可以发送邮件而不会增加开销。 Search Stackoverflow for that :) 搜索Stackoverflow :)

您的Windows服务将需要以类似于这种方式的方式来调用WMI,并确定是否通知您。

Read the appropriate performance counter at a suitable interval. 以适当的时间间隔读取适当的性能计数器。 If the collected data meets your criterion then perform the action you want. 如果收集的数据符合您的条件,请执行所需的操作。

Many things like this can already been done with tools like performance monitor's alerts (including launching an application). 使用性能监视器的警报(包括启动应用程序)之类的工具已经可以完成许多类似的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM