I need to develop an agent windows service which will always monitor resources in the current installed machine only. The monitored resources might be processor health, SNMP traps, SQL Server health.
I have figured out that this can be achieved programmatically in .NET using WMI or Perfmon.
I'm not sure which one use. I'm looking for the pros and cons and in which situation we need to go for each option.
Generally if a counter is available in Perform I'd start there but you will land up using both Perform and WMI at a minimum. As an example:
If I want to monitor the % total memory used on a machine I'd use the Perfmon counter "Memory,Available Kbytes". I would also grab the total memory on the machine using WMI along the following lines:
new ManagementObjectSearcher("root\\CIMV2", "select * from win32_computersystem")
and then storing the totalphysicalmemory property into a variable called totalKBytes.
Now whenever I want to calculate the % memory used I do a calculation :
(1.0f - (counter.NextValue() / totalKBytes)) * 100.0f
The conclusion is don't get bogged down trying to decide between Perform or WMI. You going to need them both at a minimum. Once you really get into monitoring you may also find yourself reading from the registry, running custom scripts, reading the file system etc.