简体   繁体   English

如何从c#应用程序获得远程linux机器的cpu和ram使用

[英]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. 我有一个ac#app,它通过WMI从远程Windows机器收集数据(cpu,ram,hdd用法等)。 But now I also need to monitor few linux boxes. 但是现在我还需要监控几个linux盒子。 Is there a way to get at least CPU and RAM utilization of linux machines from c# app running on windows box? 有没有办法从Windows机器上运行的c#app获得Linux机器的CPU和RAM利用率?

I managed to get metric stats from linux box. 我设法从linux框中获取指标统计数据。 So as VirtualBlackFox mentioned - the standarized way is to use snmp for this purposes. 正如VirtualBlackFox提到的那样 - 标准化的方法是将snmp用于此目的。

First step is to install snmp on linux. 第一步是在linux上安装snmp。 (I installed Ubuntu 12 on VM) (我在VM上安装了Ubuntu 12)

Here are the links which I used for installing snmp one and two . 以下是我用于安装snmp onetwo的链接。 Basically you need to install snmp daemon and configure for expose metrics and network visibility. 基本上,您需要安装snmp守护程序并进行配置以公开指标和网络可见性。

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. 我想在这一步你可以自由地使用一些snmp库从snmp设备获取数据,但我也尝试使用WMI-SNMP桥。

Step two: Setting up the WMI SNMP Environment 第二步:设置WMI SNMP环境

This is the list of steps you need to perform. 这是您需要执行的步骤列表

For me was enough to 对我来说足够了

  1. Enable snmp feature in windows features 在Windows功能中启用snmp功能
  2. Create snmp folder in %windir%\\system32\\wbem\\ %windir%\\system32\\wbem\\创建snmp文件夹
  3. Using this command Smi2smir /g ..\\..\\hostmib.mib > hostmib.mof for generating MOF files from MIB files 使用此命令Smi2smir /g ..\\..\\hostmib.mib > hostmib.mof从MIB文件生成MOF文件
  4. Adding SNMP MOF Files to the WMI Repository mofcomp hostmib.mof 将SNMP MOF文件添加到WMI存储库mofcomp hostmib.mof

After this I was able to see wmi classes and properties 在此之后,我能够看到wmi类和属性

在此输入图像描述

Code examples 代码示例

Using sharpsnmplib 使用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. 这个将返回盒子上使用的总ram。 (btw, standart port for snmp is 161) (顺便说一下,snmp的标准端口是161)

Using snmp-wmi bridge 使用snmp-wmi桥

 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: 因此,至少有两种获取snmp数据的方法:

  1. Using OID (Object Identifiers uniquely identify manged objects in a MIB hierarchy) as I shown in first example. 正如我在第一个示例中所示,使用OID(对象标识符唯一标识MIB层次结构中的manged对象)。 And here is useful link. 这里有用的链接。 Also you can find OIDs in .mof files. 您还可以在.mof文件中找到OID。
  2. Using wmi-snmp bridge. 使用wmi-snmp桥。 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. 所以我在第二个例子中使用了路径,如果它不适合你,我想你可以在WMI资源管理器中找到正确的例子。

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. 将尝试两者并看到,这更适合我的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#(托管代码)中获得* THREAD *的CPU使用率和/或RAM使用率? - How can I get CPU usage and/or RAM usage of a *THREAD* in C# (managed code)? 如何使用 .NET CORE 在 C# web 应用程序中获取当前的 CPU/RAM/磁盘使用情况? - How to get the current CPU/RAM/Disk usage in a C# web application using .NET CORE? C#查询远程计算机的资源使用情况(即CPU负载,ram使用情况) - C# Query a remote computer for resource usage (i.e. cpu load, ram usage) 使用WMI获取C#中每个进程的CPU和RAM使用情况? - Get CPU and RAM usage for each process in C# using WMI? 在 C# 中获取当前 CPU、RAM 和磁盘驱动器的使用情况 - Get current CPU, RAM and Disk drive usage in C# 如何获取已安装的 RAM 和 CPU 详细信息 c#.Net Core? - How I can get the installed RAM and CPU details c# .Net Core? 使用C#(性能计数器)的远程计算机的CPU使用率 - CPU usage of remote Machine using C#(Performance Counter) 如何从 linux 操作系统上的 asp.net 内核检索有关 RAM、CPU、磁盘使用情况的信息? - How can retrieve information about RAM,CPU, Disk usage from asp.net core on linux OS? 从 Linux c# 获取在远程 Windows 机器上运行的进程 - Get a process running on remote windows machine from Linux c# 获取CPU和RAM使用率 - Get CPU and RAM usage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM