Reputation: 1452
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?
Upvotes: 1
Views: 2600
Reputation: 1452
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
snmp
folder in %windir%\system32\wbem\
Smi2smir /g ..\..\hostmib.mib > hostmib.mof
for generating MOF files from MIB filesmofcomp 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:
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.
Upvotes: 3