服务器性能监控之性能计数器

服务器性能监控之性能计数器文章来源:http://blog.csdn.net/lhfzd2004/article/details/1722379上一篇文章《服务器性能监控之WMI》介绍了通过远程com获取服务器性能(当然也可用于本地),那么这篇主要说说windows系统自带的性能监视功能-performancecouonter.打开管理工具性能,我们可以立即看到服务器的CPU,进程运行时间,磁盘容量

大家好,欢迎来到IT知识分享网。

文章来源:http://blog.csdn.net/lhfzd2004/article/details/1722379

上一篇文章《服务器性能监控之WMI》介绍了通过远程com获取服务器性能(当然也可用于本地),那么这篇主要说说windows系统自带的性能监视功能—–>performancecouonter.

打开管理工具–>性能,我们可以立即看到服务器的CPU,进程运行时间,磁盘容量等性能参数走势图。然而不仅仅是这几项,我们可以通过添加技术器来查看其他的性能指标:

服务器性能监控之性能计数器

如果你说,这么看太麻烦了,OK,我们通过C#将这些值取出来,用于实现自身的性能监视:

1.添加引用:

 

服务器性能监控之性能计数器
using
 System.Diagnostics;

 

2.创建并实例化PerformanceCounter

 

服务器性能监控之性能计数器
public
 
static
 System.Diagnostics.PerformanceCounter pc
=
 
new
 System.Diagnostics.PerformanceCounter();
服务器性能监控之性能计数器

public
 
static
 System.Diagnostics.PerformanceCounter pcm
=
 
new
 System.Diagnostics.PerformanceCounter();
服务器性能监控之性能计数器

public
 
static
 System.Diagnostics.PerformanceCounter pcb
=
 
new
 System.Diagnostics.PerformanceCounter();
服务器性能监控之性能计数器

public
 
static
 System.Diagnostics.PerformanceCounter pcc
=
 
new
 System.Diagnostics.PerformanceCounter();
服务器性能监控之性能计数器

//
我们用四个对象做不同的操作,注意:是static的,不然每次取出的数据都是初始值,如cpu利用率就是0

 

3.构造函数

 

服务器性能监控之性能计数器
static
 CapabilityScout()
服务器性能监控之性能计数器服务器性能监控之性能计数器


{

服务器性能监控之性能计数器pc.CategoryName 
= Processor;
服务器性能监控之性能计数器pc.CounterName 
= % Processor Time;
服务器性能监控之性能计数器pc.InstanceName 
= _Total;
服务器性能监控之性能计数器pc.MachineName 
= .;
服务器性能监控之性能计数器pcm.CategoryName 
= Memory;
服务器性能监控之性能计数器pcm.CounterName 
= % Committed Bytes In Use;
服务器性能监控之性能计数器pcm.MachineName 
= .;
服务器性能监控之性能计数器pcb.CategoryName 
= Windows Media Unicast Service;
服务器性能监控之性能计数器pcb.CounterName 
= Allocated Bandwidth;
服务器性能监控之性能计数器pcb.MachineName 
= .;
服务器性能监控之性能计数器pcc.CategoryName 
= Windows Media Unicast Service;
服务器性能监控之性能计数器pcc.CounterName 
= Connected Clients;
服务器性能监控之性能计数器pcc.MachineName 
= .;
服务器性能监控之性能计数器}

 

4.获取计数器值

 

服务器性能监控之性能计数器

服务器性能监控之性能计数器服务器性能监控之性能计数器        

获取CPU利用率
#region 获取CPU利用率
服务器性能监控之性能计数器        
public static string getCpuUsage()
服务器性能监控之性能计数器服务器性能监控之性能计数器        
{

服务器性能监控之性能计数器            
string used = pc.NextValue().ToString();
服务器性能监控之性能计数器            
return used;
服务器性能监控之性能计数器        }

服务器性能监控之性能计数器        
#endregion


服务器性能监控之性能计数器服务器性能监控之性能计数器        

获取内存使用率
#region 获取内存使用率
服务器性能监控之性能计数器        
public static string getMemory()
服务器性能监控之性能计数器服务器性能监控之性能计数器        
{

服务器性能监控之性能计数器            
float used = pcm.NextValue();
服务器性能监控之性能计数器            
return used.ToString();
服务器性能监控之性能计数器        }

服务器性能监控之性能计数器        
#endregion


服务器性能监控之性能计数器服务器性能监控之性能计数器        

获取WMS连接数
#region 获取WMS连接数
服务器性能监控之性能计数器        
public static string getConnectedCount()
服务器性能监控之性能计数器服务器性能监控之性能计数器        
{

服务器性能监控之性能计数器            
string count = pcc.NextValue().ToString();
服务器性能监控之性能计数器            
return count;
服务器性能监控之性能计数器        }

服务器性能监控之性能计数器        
#endregion


服务器性能监控之性能计数器服务器性能监控之性能计数器        

获取网络流量
#region 获取网络流量
服务器性能监控之性能计数器        
public static string getServerBandWidth()
服务器性能监控之性能计数器服务器性能监控之性能计数器        
{

服务器性能监控之性能计数器            
string bandwidth = pcb.NextValue().ToString();
服务器性能监控之性能计数器            
return bandwidth;
服务器性能监控之性能计数器        }

服务器性能监控之性能计数器        
#endregion

 

当然,这里只是其中及少的部分,不过通过使用同样的方式,我们可以获取更多的性能以及进程运行的情况,但是要说明的一点是,所获取的数据必定是windows服务所提供的,当然我们也可以自己写一些windows服务,添加到系统performancecounter中来,对.net来说也是非常方便的。

怎么样,和WMI比起来,是不是又方便了一些呢,呵呵~~

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22471.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信