get windows service used memory using wmi
I'm trying to retrieve the amount of memory used by a windows service, for example, IIS. I'm us开发者_开发技巧ing python and windows wmi extensions. I didn't find any method on WIN32_Service class that would give me the information I need. What's the best approach to get this information?
Information concerning process memory is exposed through WMI using the Win32_Process
class. In particular, you'd probably be interested in the WorkingSetSize
and VirtualSize
properties. Since you are starting from a service, you will want to obtain the ProcessId
property from your service in order to select the appropriate Win32_Process instance.
Monitoring the total memory consumption of IIS can be a bit complex as IIS spins up multiple processes and services depending on how it's configured (see the hierarchy of managed entities described here). But, let's assume for this example that we're only interested in the memory consumed by W3SVC, which implements the actual HTTP protocol. Assuming we are using Tim Golden's fantastic WMI library, we would need to do something like this:
import wmi
c = wmi.WMI()
for www_srv in c.Win32_Service(Name = 'W3SVC'):
for process in c.Win32_Process(ProcessId = www_srv.ProcessId):
print 'Memory used by W3SVC', process.WorkingSetSize
精彩评论