I have a c# app which collects data(cpu, ram, hdd usages etc) from remote windows machines via WMI. But now I also need to monitor few linux boxes. Is there a way to get at least CPU and RAM utilization of linux machines from c# app running on windows box?
-
1The standardized way would be to install an SNMP server and use some C# lib like sharpsnmplib.codeplex.com to access it. But I never used it myself. – Julien Roncaglia Nov 13 '13 at 12:52
I managed to get metric stats from linux box. So as VirtualBlackFox
mentioned - the standarized way is to use snmp for this purposes.
First step is to install snmp on linux. (I installed Ubuntu 12 on VM)
Here are the links which I used for installing snmp one and two. Basically you need to install snmp daemon and configure for expose metrics and network visibility.
I think at this step you are free to use some snmp library to get data from snmp device, but I also tried to use WMI-SNMP
bridge.
Step two: Setting up the WMI SNMP Environment
This is the list of steps you need to perform.
For me was enough to
- Enable snmp feature in windows features
- Create
snmp
folder in%windir%\system32\wbem\
- Using this command
Smi2smir /g ..\..\hostmib.mib > hostmib.mof
for generating MOF files from MIB files - Adding SNMP MOF Files to the WMI Repository
mofcomp hostmib.mof
After this I was able to see wmi classes and properties
Code examples
Using sharpsnmplib
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
var result = Messenger.Get(
VersionCode.V1,
new IPEndPoint(IPAddress.Parse("172.10.206.108"), 161),
new OctetString("public"),
new List<Variable> { new Variable(new ObjectIdentifier("1.3.6.1.4.1.2021.4.6.0")) },
60000);
This one will return total ram used on the box. (btw, standart port for snmp is 161)
Using snmp-wmi bridge
string snmpClass = "SNMP_RFC1213_MIB_system";
string path = string.Format("\\\\.\\root\\snmp\\localhost:{0}=@", snmpClass);
var contextParams = new ManagementNamedValueCollection
{
{"AgentAddress", "172.10.206.108"}, // ip address of snmp device
{"AgentReadCommunity", "public"}
};
var options = new ObjectGetOptions(contextParams);
var objSys = new ManagementObject(new ManagementPath(path), options);
Console.WriteLine(objSys.Properties["sysDescr"].Value);
Console.ReadLine();
So there are at least two ways to get snmp data:
- Using OID (Object Identifiers uniquely identify manged objects in a MIB hierarchy) as I shown in first example. And here is useful link. Also you can find OIDs in .mof files.
- Using wmi-snmp bridge. So I used path like in second example, if it is not working for you, I think you can find correct one in WMI Explorer for example.
So this is pretty much for it. Don't know which approach is better or faster. Will try both and see, which is more suitable for my purposes.