简体   繁体   English

创建用于监视的服务

[英]Creating a service for monitoring

As an exercise in both writing Windows Services and communicating with them, I've decided I want to try and write a service to monitor hardware performance on my machine, record it and report on it daily. 作为编写Windows服务并与之通信的一项练习,我决定要尝试编写一项服务来监视计算机的硬件性能,对其进行记录并每天进行报告。 I want to be able to query the service remotely and from researching this a little, I think I could embed a ServiceHost in there. 我希望能够远程查询服务,并且通过对此进行一点研究,我认为我可以在其中嵌入ServiceHost。

How does a service actually run? 服务实际上如何运行? The examples I've found have all been, OnStart..OnStop, ServiceHost.Run( ) and that's it. 我找到的示例都是OnStart..OnStop,ServiceHost.Run(),仅此而已。 Does a process run indefinitely, do I have to write a loop somewhere that constantly checks things? 进程是否会无限期地运行,是否需要在某个地方不断编写检查内容的循环?

Also, is it realistic to monitor windows machines using WMI through a service to report on disk space, IO and memory usage? 此外,通过服务监视使用WMI的Windows计算机以报告磁盘空间,IO和内存使用情况是否现实? From what I've read they're not the fastest and as I see it, my service would have a polling interval and check certain statistics every couple of minutes or so. 据我了解,它们并不是最快的,而且据我所知,我的服务将有一个轮询间隔,并每两分钟左右检查一次某些统计信息。 Would this adversely affect a machine? 这会对机器产生不利影响吗?

I like to implement services where the Start/Stop/Shutdown starts and stops a timer, where the interval is set in the config. 我喜欢在启动/停止/关机启动和停止计时器(在配置中设置间隔)的地方实现服务。

When the the timer event fires, I usually stop the timer, process the tasks and re-start the timer (the start/stop shut-down sets a flag which is checked before restarting) This caters for long running tasks (ie tasks that may run over the interval time if its set really short) 当计时器事件触发时,我通常会停止计时器,处理任务并重新启动计时器(启动/停止关闭操作会设置一个在重新启动之前已检查的标志),这可以满足长时间运行的任务(即可能如果间隔时间过短,则在间隔时间内运行)

If you are checking every couple of minutesthe speed of WMI is probably irrelevant, but in any case, investigate various ways of reporting on those criteria and look for the most efficient ways. 如果每隔几分钟检查一次WMI的速度可能无关紧要,但是无论如何,请研究根据这些标准进行报告的各种方法,并寻找最有效的方法。

Another great thing to do is to adjust the Program.cs so the application can take parameters and install/uninstall itself and to run the tasks once by just running the exe (ie not as a service) This helps debugging the service. 另一个很棒的事情是调整Program.cs,以便应用程序可以获取参数并自行安装/卸载,并且仅通过运行exe即可运行一次任务(即,不作为服务)。这有助于调试服务。 see: Installing Windows Service programmatically 请参阅:以编程方式安装Windows Service

Have a look at this for an example (and explanation) of how to write a service very similar to what you're doing (ie repeatedly invoking some functionality). 请看一下示例(和说明),以了解如何编写与您正在做的事情非常相似的服务(即反复调用某些功能)。

To answer your question, you are responsible for doing something to make sure your code runs periodically. 为了回答您的问题,您有责任做一些事情来确保您的代码定期运行。 You can do this by registering a callback on a timer, or starting a thread that runs in a loop until the OnStop method is called. 您可以通过在计时器上注册回调,或启动循环运行直到调用OnStop方法的线程来执行此OnStop

As for monitoring the machine through WMI -- the volume of statistics you're collecting at an interval of every few minutes shouldn't cause a problem. 至于通过WMI监视计算机-您每隔几分钟就收集一次的统计信息应该不会造成问题。 I've seen cases where a lot of data was collected, relatively often, without significant impact. 我看到过这样的情况:相对频繁地收集了很多数据而没有重大影响。

Windows services receive start and stop commands (control requests) via the Service Control Manager (SCM) service. Windows服务通过Service Control Manager (SCM)服务接收启动和停止命令(控制请求)。 This "super service" starts early in the Windows startup sequence, and is responsible for bringing all the other services up. 此“超级服务”在Windows启动序列的早期启动,并负责启动所有其他服务。

The service functions provide an interface for the following tasks performed by the SCM: 服务功能为SCM执行的以下任务提供接口:

  • Maintaining the database of installed services. 维护已安装服务的数据库。
  • Starting services and driver services either upon system startup or upon demand. 在系统启动时或根据需要启动服务和驱动程序服务。
  • Enumerating installed services and driver services. 枚举已安装的服务和驱动程序服务。
  • Maintaining status information for running services and driver services. 维护运行服务和驱动程序服务的状态信息。
  • Transmitting control requests to running services. 将控制请求传输到正在运行的服务。
  • Locking and unlocking the service database. 锁定和解锁服务数据库。

See Starting Services on Demand for pointers to available SCM functions: 请参阅按需启动服务以获取指向可用SCM功能的指针:

The user can start a service with the Services control panel utility. 用户可以使用“服务”控制面板实用程序启动服务。 The user can specify arguments for the service in the Start parameters field. 用户可以在“启动参数”字段中指定服务的参数。 A service control program can start a service and specify its arguments with the StartService function. 服务控制程序可以启动服务,并使用StartService函数指定其参数。

Regarding the load generated by monitoring functions, you'll have to play with the polling interval and decide what overhead is acceptable. 关于监视功能所产生的负载,您必须考虑轮询间隔并确定可接受的开销。 Usually, intervals of 2 to 5 minutes present no problem. 通常,间隔2到5分钟没有问题。

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

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