I am building a Health & Monitoring page to show various servers CPU Usage and Memory Usage. Please advise how to get Total RAM so that I can show Memory Usage in %. I see lot of examples for local system but didn't find any working solution for remote machines.
CPU Usage:
var cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total", server.Name);
Available Memory:
var rama = new PerformanceCounter("Memory", "Available MBytes", String.Empty, server.Name);
a = getCurrentCpuUsage();
Thread.Sleep(1000);
a = getCurrentCpuUsage();
b = getAvailableRAM();
Thread.Sleep(1000);
b = getAvailableRAM();
public string getCurrentCpuUsage()
{
return cpu.NextValue() + "%";
}
public string getAvailableRAM()
{
return rama.NextValue() + "MB";
}
Tried to do something like below.
//System.Management.ManagementScope ms = new System.Management.ManagementScope(@"\\" + server.Name + @"\root\cimv2");
//System.Management.SelectQuery sq = new System.Management.SelectQuery("SELECT * FROM Win32_OperatingSystem");
//var wmiObject = new ManagementObjectSearcher(ms,sq);
//var memoryValues = wmiObject.Get().Cast<ManagementObject>().Select(mo => new
//{
// FreePhysicalMemory = Double.Parse(mo["FreePhysicalMemory"].ToString()),
// TotalVisibleMemorySize = Double.Parse(mo["TotalVisibleMemorySize"].ToString())
//}).FirstOrDefault();
It didn't work though I impersonate user as ADMIN user on those servers. I am getting An exception of type 'System.UnauthorizedAccessException' occurred in System.Management.dll but was not handled in user code
Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
FYI.. Please note I can get all Server's Services data using the same ADMIN user access.
var services = ServiceController.GetServices(server.Name);