Obtain the CPU and memory status of a remote computer

Source: Internet
Author: User

Msdn: win32_processor, cim_operatingsystem, and win32_logicalmemoryconfiguration are mainly the attributes that can be viewed in the first two msdn

Protected string [] getservercpuandmem (string IP, string username, string password)
{
Connectionoptions conoptions = new connectionoptions ();
Conoptions. Username = username;
Conoptions. Password = password;

String Path = "\\\\" + IP + "\\ Root \ cimv2 ";
Managementscope MS = new managementscope (PATH );
Ms. Options = conoptions;
Ms. Connect ();

String cpupercent = ""; // CPU serial number \ 192.0.0.184 \ Root \ cimv2: win32_processor

Managementclass cimobject = new managementclass (MS, new managementpath (path + ": win32_processor"), null );
Managementobjectcollection MOC = cimobject. getinstances ();
Foreach (managementobject Mo in MoC)
{
Cpupercent = Mo. properties ["loadpercentage"]. value. tostring ();
}
Cpupercent = cpupercent + "% ";

Objectquery OQ = new objectquery ("select * From cim_operatingsystem ");
Managementobjectsearcher searcher = new managementobjectsearcher (MS, OQ );
Uint64 totalvisiblememorysize = 0;
Uint64 freephysicalmemory = 0;
Foreach (managementobject queryobj in searcher. Get ())
{
Totalvisiblememorysize = (uint64) queryobj ["totalvisiblememorysize"];
Freephysicalmemory = (uint64) queryobj ["freephysicalmemory"];
}
Decimal di = convert. todecimal (totalvisiblememorysize-freephysicalmemory)/totalvisiblememorysize;

String mempercent = "";
Try
{
Mempercent = convert. tostring (di * 100). Split ('.') [0];
}
Catch
{
Mempercent = convert. tostring (di * 100 );
}
Mempercent = mempercent + "% ";

String [] result = new string [2];
Result [0] = cpupercent;
Result [1] = mempercent;


Return result;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.