1

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);
redsam
  • 145
  • 1
  • 18
  • @mybirthname: Nope. That is for local computer. I see lot of examples for local system but didn't find any working solution for remote machines. – redsam Oct 11 '16 at 16:40
  • With WMI you can query a remote machine, which is what you're asking for. – Lucero Oct 11 '16 at 16:47
  • @Lucero: I am editing question. Can't add code here. It didn't work though I impersonate user as ADMIN user on those servers. – redsam Oct 11 '16 at 16:52
  • It seems to work for others though: http://stackoverflow.com/questions/5566965/problem-reading-amount-of-memory-on-remote-computer – Lucero Oct 11 '16 at 16:55
  • FYI.. I tried all those solutions. Nothing worked for me. – redsam Oct 11 '16 at 16:59
  • "not working" is very unspecific, and probably no one will be able to tell you how to fix that without specific error information. – Lucero Oct 11 '16 at 17:01
  • @Lucero. Edited question with more details. Please take a look. – redsam Oct 11 '16 at 17:09
  • Querings services is different, as this is not done through DCOM like WMI AFAIR. WMI Troubleshooting -> https://msdn.microsoft.com/en-us/library/aa394603(v=vs.85).aspx – Lucero Oct 11 '16 at 17:18
  • I don't know much about COM/DCOM. As I added more details now and while I read and troubleshoot can you please remove "Duplicate" tag so that others can look into it as well and share their experiences. I have been struggling with this more than a week. – redsam Oct 11 '16 at 17:31
  • I will deploy in servers and check how it is behaving. May be my local development machine issues. – redsam Oct 11 '16 at 17:58
  • 1
    Just want to share the update and It may help someone. My code is working fine in Dev/QA servers. It seems to be an issue with my local system access/permissions restrictions. – redsam Oct 20 '16 at 16:53

0 Answers0