简体   繁体   中英

How can I get cpu and ram usage of remote linux machine from c# application

I have ac# 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?

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

  1. Enable snmp feature in windows features
  2. Create snmp folder in %windir%\\system32\\wbem\\
  3. Using this command Smi2smir /g ..\\..\\hostmib.mib > hostmib.mof for generating MOF files from MIB files
  4. 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:

  1. 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.
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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