C# 系统管理资源监控统计信息,如I/O、CPU、内存使用。。。etc';
我正在尝试对本地数据库管理系统进行一些基准测试。 我需要拿出一个GUI来显示如下信息C# 系统管理资源监控统计信息,如I/O、CPU、内存使用。。。etc';,c#,asp.net,ajax,winapi,wmi-query,C#,Asp.net,Ajax,Winapi,Wmi Query,我正在尝试对本地数据库管理系统进行一些基准测试。 我需要拿出一个GUI来显示如下信息 每个核心的cpu使用率 内存使用 硬盘输入/输出 等等 如果不使用Vb.net,这将是,但如果需要,我可以使用p/invoke或本机代码 以下代码是:据我所知 Set objCimv2 = GetObject("winmgmts:root\cimv2") Set objRefresher = CreateObject("WbemScripting.SWbemRefresher") ' Add items
- 每个核心的cpu使用率
- 内存使用
- 硬盘输入/输出
- 等等
Set objCimv2 = GetObject("winmgmts:root\cimv2")
Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
' Add items to the SWbemRefresher
' Without the SWbemRefreshableItem.ObjectSet call,
' the script will fail
Set objMemory = objRefresher.AddEnum _
(objCimv2, _
"Win32_PerfFormattedData_PerfOS_Memory").ObjectSet
Set objDiskQueue = objRefresher.AddEnum _
(objCimv2, _
"Win32_PerfFormattedData_PerfDisk_LogicalDisk").ObjectSet
Set objQueueLength = objRefresher.AddEnum _
(objCimv2, _
"Win32_PerfFormattedData_PerfNet_ServerWorkQueues").ObjectSet
' Initial refresh needed to get baseline values
objRefresher.Refresh
intTotalHealth = 0
' Do three refreshes to get data
For i = 1 to 3
WScript.Echo "Refresh " & i
For each intAvailableBytes in objMemory
WScript.Echo "Available megabytes of memory: " _
& intAvailableBytes.AvailableMBytes
If intAvailableBytes.AvailableMBytes < 4 Then
intTotalHealth = intTotalHealth + 1
End If
Next
For each intDiskQueue in objDiskQueue
WScript.Echo "Current disk queue length " & "Name: " _
& intDiskQueue.Name & ":" _
& intDiskQueue.CurrentDiskQueueLength
If intDiskQueue.CurrentDiskQueueLength > 2 Then
intTotalHealth = intTotalHealth + 1
End If
Next
For each intServerQueueLength in objQueueLength
WScript.Echo "Server work queue length: " _
& intServerQueueLength.QueueLength
If intServerQueueLength.QueueLength > 4 Then
intTotalHealth = intTotalHealth + 1
End If
Wscript.Echo " "
Next
If intTotalHealth > 0 Then
Wscript.Echo "Unhealthy."
Else
Wscript.Echo "Healthy."
End If
intTotalHealth = 0
Wscript.Sleep 5000
' Refresh data for all objects in the collection
objRefresher.Refresh
Next
Set objCimv2=GetObject(“winmgmts:root\cimv2”)
设置objRefresher=CreateObject(“WbemScripting.SWbemRefresher”)
'将项目添加到SWbemRefresher
'没有SWbemRefreshableItem.ObjectSet调用,
'脚本将失败
设置objMemory=objRefresher.AddEnum_
(objCimv2
“Win32_performatteddata_PerfOS_Memory”).ObjectSet
设置objDiskQueue=objRefresher.AddEnum_
(objCimv2_
“Win32\u PerfFormattedData\u PerfDisk\u LogicalDisk”).ObjectSet
设置objQueueLength=objRefresher.AddEnum_
(objCimv2_
“Win32_PerfFormattedData_PerfNet_ServerWorkQueues”).ObjectSet
'获取基线值需要初始刷新
objRefresher。刷新
intTotalHealth=0
'执行三次刷新以获取数据
对于i=1到3
Echo“刷新”&i
对于objMemory中的每个intAvailableBytes
Echo“可用内存兆字节:”_
&intAvailableBytes.AvailableMB
如果intAvailableBytes.AvailableBytes小于4,则
intTotalHealth=intTotalHealth+1
如果结束
下一个
对于objDiskQueue中的每个intDiskQueue
Echo“当前磁盘队列长度”和“名称:”_
&intDiskQueue.Name&“:”_
&intDiskQueue.CurrentDiskQueueLength
如果intDiskQueue.CurrentDiskQueueLength>2,则
intTotalHealth=intTotalHealth+1
如果结束
下一个
对于objQueueLength中的每个intServerQueueLength
Echo“服务器工作队列长度:”_
&intServerQueueLength.QueueLength
如果intServerQueueLength.QueueLength>4,则
intTotalHealth=intTotalHealth+1
如果结束
Wscript.Echo“”
下一个
如果intTotalHealth>0,则
Echo“不健康”
其他的
Echo“健康”
如果结束
intTotalHealth=0
Wscript.Sleep 5000
'刷新集合中所有对象的数据
objRefresher。刷新
下一个
我正在寻找一个很好的API来访问实时硬件(当前版本是Windows7x64)信息和统计数据,使用C#,asp.NETWebForms,Framework4.0-4.02。
现在我所知道的只是如何操作和访问“进程”,而不是按照本文的要求进行绑定。公共类MyPerfOamnCounter
public class MyPerfoamnceCounter
{
public string pc()
{
Dictionary<string, List<PerformanceCounter>> counters =
new Dictionary<string, List<PerformanceCounter>>();
List<PerformanceCounter> cpuList = new List<PerformanceCounter>();
List<PerformanceCounter> procList = new List<PerformanceCounter>();
List<PerformanceCounter> memList = new List<PerformanceCounter>();
PerformanceCounterCategory perfCat = new PerformanceCounterCategory();
foreach (Process process in Process.GetProcesses())
{
PerformanceCounter procProcesTimeCounter = new PerformanceCounter(
"Process",
"% Processor Time",
process.ProcessName);
var proc = procProcesTimeCounter; procList.Add(proc);
procProcesTimeCounter.NextValue();
PerformanceCounter procCPUTimeCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
process.ProcessName);
//procCPUTimeCounter.CategoryName= "Processor";
perfCat.CategoryName = procCPUTimeCounter.CategoryName;
var ex = PerformanceCounterCategory.Exists("Processor");
if (ex)
{
cpuList.Add(procCPUTimeCounter);
//if(procCPUTimeCounter.InstanceName
//if(procCPUTimeCounter.InstanceName == process.ProcessName)
procCPUTimeCounter.NextValue();
}
PerformanceCounter procMemTimeCounter = new PerformanceCounter(
"Memory", "Available MBytes",
process.ProcessName);
ex = perfCat.InstanceExists(process.ProcessName);
if (ex)
{
var mem = procMemTimeCounter; memList.Add(mem);
procMemTimeCounter.NextValue();
}
/*
var oktoplot = Convert.ToUInt32(v) != 0;
if (oktoplot)
counters.Add(procProcesTimeCounter);
*/
}
counters.Add("CPUs", cpuList);
counters.Add("PROCs", procList);
counters.Add("MEMs", memList);
System.Threading.Thread.Sleep(2250); // 1 second wait ("Memory", "Available MBytes")
StringBuilder SbRw = new StringBuilder();
SbRw.Append("<table id = 'tblMainCounters'>");
List<string> couterNames = new List<string>();
foreach (string cName in counters.Keys)
{
couterNames.Add(cName);
}
foreach (string cNameStr in couterNames)
{
SbRw.Append("\r\n\t<tr>\r\n\t\t<td>\r\n\t\t\t<table id='tbl" + cNameStr + "'>\r\n\t\t\t\t");
for (int i = 0; i < counters[cNameStr].Count; i++)
{
//string ToPRint = counters[cNameStr].ElementAt(i).NextValue().ToString();
//bool OkToprint = string.IsNullOrEmpty(ToPRint) == false && ToPRint != (0.0).ToString() && ToPRint != (0).ToString();
//if (OkToprint)
SbRw.Append(string.Format("\r\n\t\t\t\t\t<tr id='{0}List{1}'>\r\n\t\t\t\t\t\t<td>Category : </td><td>{2}</td><td> Process : </td><td>{3}</td><td> CounterName : </td><td>{4}</td><td> CPU Usage : </td><td><b> {5}%</b></td>\r\n\t\t\t\t\t</tr>",
cNameStr,
i.ToString(),
counters[cNameStr].ElementAt(i).CategoryName,
counters[cNameStr].ElementAt(i).InstanceName,
counters[cNameStr].ElementAt(i).CounterName,
counters[cNameStr].ElementAt(i).NextValue()));
}
SbRw.Append("</table><!-- Closing table_" + cNameStr + " -->\r\n\t\t\t</td>\r\n\t\t</tr>");
}
SbRw.Append("</table><!-- Closing tableMainCounters --><br /><br />");
return(SbRw.ToString());
}
{
公共字符串pc()
{
字典计数器=
新字典();
List cpuList=新列表();
List procList=新列表();
List memList=新列表();
PerformanceCounterCategory perfCat=新的PerformanceCounterCategory();
foreach(Process.getprocesss()中的进程)
{
PerformanceCounter PROCESTIMECounter=新的PerformanceCounter(
“过程”,
“%Processor Time”,
process.ProcessName);
var proc=procestimecounter;procList.Add(proc);
ProcEstimeCounter.NextValue();
PerformanceCounter procCPUTimeCounter=新的PerformanceCounter(
“处理器”,
“%Processor Time”,
process.ProcessName);
//procCPUTimeCounter.CategoryName=“处理器”;
perfCat.CategoryName=procCPUTimeCounter.CategoryName;
var ex=PerformanceCounterCategory.Exists(“处理器”);
如果(ex)
{
cpuList.Add(procCPUTimeCounter);
//if(procCPUTimeCounter.InstanceName
//if(procCPUTimeCounter.InstanceName==process.ProcessName)
procCPUTimeCounter.NextValue();
}
PerformanceCounter procMemTimeCounter=新的PerformanceCounter(
“内存”,“可用MB”,
process.ProcessName);
ex=perfCat.InstanceExists(process.ProcessName);
如果(ex)
{
var mem=procMemTimeCounter;memList.Add(mem);
procMemTimeCounter.NextValue();
}
/*
var-oktoplot=Convert.ToUInt32(v)!=0;
如果(oktoplot)
计数器。添加(procestimecounter);
*/
}
添加(“CPU”,cpuList);
计数器。添加(“PROCs”,procList);
计数器。添加(“MEMs”,memList);
System.Threading.Thread.Sleep(2250);//1秒等待(“内存”,“可用MB”)
StringBuilder SbRw=新的StringBuilder();
SbRw.追加(“”);
List couterNames=新列表();
foreach(counters.Keys中的字符串cName)
{
couterNames.Add(cName);
}
foreach(couterNames中的字符串cNameStr)
{
SbRw.Append(“\r\n\t\r\n\t\t\r\n\t\t\r\n\t\t\t\t”);
对于(int i=0;i<计数器[cNameStr]。计数;i++)
{
//字符串ToPRint=counters[cNameStr].ElementAt(i).NextValue().ToString();
//bool-OkToprint=string.IsNullOrEmpty(ToPRint)==false&&ToPRint!=(0.0).ToString()和&ToPRint!=(0.ToString();
//如果(OkToprint)
SbRw.Append(string.Format(“\r\n\t\t\t\t\t\t\t\t类别:{2}进程:{3}计数器名:{4}CPU使用率:{5}%\r\n\t\t\t\t\t”,
cNameStr,
i、 ToString(),
计数器[cNameStr].ElementAt(i).CategoryName,
计数器[cNameStr].ElementAt(i).InstanceName,
计数器[cNameStr].ElementAt(i).计数器名称,
计数器[cNameStr].ElementAt(i).NextValue();
}
SbRw.Append(“\r\n\t\t\t\r\n\t\t”);
}
SbRw.追加(“
”);
返回(SbRw.ToString());
}
jqueryajax部分,用于在不刷新的情况下更新页面
$('document').ready(function () {
// alert("JeuryIsOnline");
$('#DivRobCounterRes').fadeTo(4, 0);
$('.Span_imgMemeBase').click(
function () {
var Spars = [];
Spars.push("egnition");
for (i = 1; i < 11; i++)
jQuerySendCfrm(Spars);
});
}); // EndJquery Ready Code
function jQuerySendCfrm(resluts) {
var SentClientinfo = []
SentClientinfo.push({ key: "SentClientinfo", value: resluts });
var CurrpageURL = "default.aspx/";
var WebmethodName = "StartTest";
var StageIdentifyer = "stage1";
var Post_TargetUrl = CurrpageURL + WebmethodName;
jQueryAajaxNoPostBack(Post_TargetUrl, SentClientinfo, StageIdentifyer);
}
function jQueryAajaxNoPostBack(targetUrl, passedSentPars, StageIdentifyer) {
////////// alert(JSON.stringify({ SentPars: passedSentPars }));
$.ajax({
type: 'POST',
url: targetUrl,
data: JSON.stringify({ SentPars: passedSentPars }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (StageIdentifyer == "stage1") {
var htmlret = response.d;
getCallBackToGenerateSeYaMsg(htmlret);
}
// else if (StageIdentifyer == "stage2") {
// var html4TableHeaders = response.d;
// getCallBackToGenerateInnerHtmlDiv(html4TableHeaders);
// //alert(response.d)
// }
},
error: function (response) {
//////// alert(response.status + ' ' + response.statusText);
}
});
}
function getCallBackToGenerateSeYaMsg(html) {
$('#DivRobCounterRes').fadeTo(400, 1);
$('#DivRobCounterRes').append(html);
}
$('document').ready(函数(){
//警惕(“JeuryIsOnline”);
$('divrobaccountres')。法德托(4