![](/img/trans.png)
[英]How can I get CPU usage and/or RAM usage of a *THREAD* in C# (managed code)?
[英]How can I get cpu and ram usage of remote linux machine from c# application
我有一個ac#app,它通過WMI從遠程Windows機器收集數據(cpu,ram,hdd用法等)。 但是現在我還需要監控幾個linux盒子。 有沒有辦法從Windows機器上運行的c#app獲得Linux機器的CPU和RAM利用率?
我設法從linux框中獲取指標統計數據。 正如VirtualBlackFox
提到的那樣 - 標准化的方法是將snmp用於此目的。
第一步是在linux上安裝snmp。 (我在VM上安裝了Ubuntu 12)
以下是我用於安裝snmp one和two的鏈接。 基本上,您需要安裝snmp守護程序並進行配置以公開指標和網絡可見性。
我想在這一步你可以自由地使用一些snmp庫從snmp設備獲取數據,但我也嘗試使用WMI-SNMP
橋。
第二步:設置WMI SNMP環境
這是您需要執行的步驟列表 。
對我來說足夠了
%windir%\\system32\\wbem\\
創建snmp
文件夾 Smi2smir /g ..\\..\\hostmib.mib > hostmib.mof
從MIB文件生成MOF文件 mofcomp hostmib.mof
在此之后,我能夠看到wmi類和屬性
代碼示例
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);
這個將返回盒子上使用的總ram。 (順便說一下,snmp的標准端口是161)
使用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();
因此,至少有兩種獲取snmp數據的方法:
所以這非常適合它。 不知道哪種方法更好或更快。 將嘗試兩者並看到,這更適合我的目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.