[英]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 one和two的链接。 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 对我来说足够了
snmp
folder in %windir%\\system32\\wbem\\
在%windir%\\system32\\wbem\\
创建snmp
文件夹 Smi2smir /g ..\\..\\hostmib.mib > hostmib.mof
for generating MOF files from MIB files 使用此命令Smi2smir /g ..\\..\\hostmib.mib > hostmib.mof
从MIB文件生成MOF文件 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数据的方法:
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.