标签:
C#实现对远程服务器的内存和CPU监控小记
1、 主要使用到的组件有System.Management.dll
2、 主要类为 :ManagementScope
连接远程服务器示例代码:
1 private const string PROPERTY_CAPACITY = "Capacity"; 2 private const string PROPERTY_AVAILABLE_BYTES = "AvailableBytes"; 3 private const string PROPERTY_PROCESS_ID = "ProcessId"; 4 private const string PROPERTY_PROCESS_NAME = "Name"; 5 private const string PROPERTY_WORKING_SET_SIZE = "WorkingSetSize"; 6 7 8 ConnectionOptions Conn = new ConnectionOptions(); 9 //设定用于WMI连接操作的用户名 10 Conn.Username = "用户名"; 11 //设定用户的口令 12 Conn.Password = "密码"; 13 ManagementScope Ms = new ManagementScope("\\\\ip\\root\\cimv2", Conn); 14 Ms.Connect();
具体查询举例:
1 var query1 = new SelectQuery("SELECT * FROM Win32_PhysicalMemory"); 2 var query2 = new SelectQuery("SELECT * FROM Win32_PerfRawData_PerfOS_Memory"); 3 var query3 = new SelectQuery("SELECT * FROM Win32_Processor"); 4 var query4 = new SelectQuery("SELECT * FROM Win32_Process"); 5 6 var searcher1 = new ManagementObjectSearcher(Ms, query1); 7 var searcher2 = new ManagementObjectSearcher(Ms, query2); 8 var searcher3 = new ManagementObjectSearcher(Ms, query3); 9 var searcher4 = new ManagementObjectSearcher(Ms, query4);
查询内存和CPU:
1 var capacity = 0.0; 2 var free = 0.0; 3 var cpu = 0.0; 4 // 2048 5 foreach (var o in searcher1.Get()) 6capacity += (double)Convert.ToUInt64(o[PROPERTY_CAPACITY]) / (double)(1024 * 1024); 7 foreach (var o in searcher2.Get()) 8 free += (double)Convert.ToUInt64(o[PROPERTY_AVAILABLE_BYTES]) / (double)(1024 * 1024); 9 foreach (var o in searcher3.Get()) 10 { 11 if (o["LoadPercentage"] != null) 12 { 13 cpu = long.Parse(o["LoadPercentage"].ToString()); 14 } 15 else { 16 cpu = 0; 17 } 18 }
举例远程重启某个进程(部分示例代码):
1 ConnectionOptions Conn = new ConnectionOptions(); 2 Conn.Username = userName; //设定用户的口令 3 Conn.Password = pwd; 4 Ms = new ManagementScope("\\\\" + ip + "\\root\\cimv2", Conn); 5 Ms.Connect(); 6 ManagementClass processClass = new ManagementClass(Ms, new ManagementPath("Win32_Process"), null); 7 //获得用来提供参数的对象 8 ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); 9 //设定命令行参数 10 inParams["CommandLine"] = path;// @"需要启动的进程路径"; 11 //执行程序 12 ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
Other、可以用程序操作批处理文件(.bat)来查询进程或者是关闭进程:
例如查询所有进程 :tasklist /S ip /U 用户名 /P 密码
关闭某个进程:
taskkill /S ip /U用户名/P 密码 /IM 需要关闭的进程名
部分示例代码:
1 public delegate void DelReadStdOutput(string result); 2 public delegate void DelReadErrOutput(string result); 3 public event DelReadStdOutput ReadStdOutput; 4 public event DelReadErrOutput ReadErrOutput; 5 /// <summary> 6 /// 注册事件 7 /// </summary> 8 private void Init() 9 { 10 ReadStdOutput += new DelReadStdOutput(ReadStdOutputAction); 11 ReadErrOutput += new DelReadErrOutput(ReadErrOutputAction); 12 13 } 14 //----------------------部分实现代码================== 15 using (p = new Process()) 16 { 17 18 //pause -- 停止 19 p.StartInfo.FileName = fileName; 20 21 p.StartInfo.UseShellExecute = false; 22 p.StartInfo.CreateNoWindow = true; 23 24 p.StartInfo.RedirectStandardError = true; 25 p.StartInfo.RedirectStandardInput = true; 26 p.StartInfo.RedirectStandardOutput = true; 27 p.EnableRaisingEvents = true; 28 p.Exited += new EventHandler(p_Exited); 29 p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); 30 p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 31 32 p.Start(); 33 //读取输出: 34 p.BeginErrorReadLine(); 35 p.BeginOutputReadLine(); 36 p.WaitForExit(); 37 } 38 39 40 void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 41 { 42 this.lab_tips.Text = "开始读取数据...读取时间可能有点长,请耐心等待!"; 43 if (e.Data != null) 44 { 45 this.Invoke(ReadStdOutput, new object[] { e.Data }); 46 } 47 } 48 // 读取进程信息: 49 public void ReadStdOutputAction(string s) 50 { 51 52 if (num > 1) 53 { 54 budiler.AppendLine(s); 55 } 56 this.textBox1.Text = budiler.ToString(); 57 num++; 58 }
暂时就到这儿了。
标签:
原文地址:http://www.cnblogs.com/luo-super/p/5641680.html