cmfr 发表于 2015-8-12 10:56:20

iis 网站流量实时监控

  好多网站管理员通常在维护好多iis站点的时候(成百上千),经常会遇到iis 占用带宽或系统资源较多的情况,而导致整个iis响应迟缓。
  这种情况我们非常想知道是iis中哪些站点占用资源这么大,从而找出问题的根源。
  许多站长一般都会在这时候打开iis 日志记录功能,来分析iis日志记录,在汪洋大海中搜寻我们需要的信息,而在这个过程中有80%的人
  徒劳无获。
  为了解决这个问题:在这里把最近写的一个iis网站全面信息实时监控程序和大家分享,希望给需要者帮助。
  图

  
  程序是用.net 开发的,下面讲讲具体的实现。
  
  程序实现:
  想必大家都有听说过wmi这个东西,你猜对了我们程序就是读取wmi性能技术器参数,实现的iis站点监控的。
  
  WMI说明参见http://baike.baidu.com/view/442461.htm网上一大堆,自己看看就明白了。
  其实我们通过WMI 可以干很多事情,例如像获得本地和远程计算机硬件信息,当前进程信息,远程重启,等功能。
  是不是听起来挺有意思吧,呵呵,其实好多系统管理软件都是通过WMI来实现的---优化大师等等。
  
  新接触WMI感觉它是很难一样,其实不尽然。WMI其实就是一个数据库,存放着计算机所有的信息,它还提供了
  一种类似于sql 的数据查询语言,叫WQL 我们可以通过这个语言查找数据。
  附个WQL查询器,大家可用来查询一下信息试试,其实关键是参数的熟悉。http://files.iyunv.com/zhuxiangyu/WQL%e5%88%86%e6%9e%90%e5%99%a8.rar
  
  言归正传,我们继续实现我们的程序,其实有了刚才的基础我们就很简单了,我们就是通过WQL去查询性能计数器的属性值了。
  看代码:
  
  //页面绑定方法
  private void bind()
{
       int i = 0;
       try
       {
  //下面就是.net 操作WMI的代码了
  //Win32_PerfFormattedData_W3SVC_WebService就是我们说的性能计数器实体,我们用WQL执行查询就行
            ManagementObjectSearcher query = new ManagementObjectSearcher(
  @"\root\cimv2","SELECT Name,TotalBytesReceived,TotalBytesSent,TotalBytesTransfered," +
                "TotalFilesSent,TotalFilesReceived,TotalFilesTransferred,TotalGetRequests,TotalPostRequests,TotalMethodRequests,TotalNotFoundErrors," +
                "CurrentConnections FROM Win32_PerfFormattedData_W3SVC_WebService");
  
                ManagementObjectCollection queryCollection = query.Get(); //Get获取集合并赋给 Collention
  foreach (ManagementObject mo in queryCollection) //偏历集合
                {
                  i++;
                  DataRow webInfo = dataTable.NewRow();
                  webInfo["ID"] = i;
                  webInfo["Name"] = mo.Properties["Name"].Value.ToString();
  double receivedBytes = Convert.ToDouble(mo.Properties["TotalBytesReceived"].Value);
                  receivedBytes = receivedBytes / 1024;
                  webInfo["TotalBytesReceived"] = Convert.ToDouble(receivedBytes.ToString("0.0"));
  double sendBytes = Convert.ToDouble(mo.Properties["TotalBytesSent"].Value);
                  sendBytes = sendBytes / 1024;
                  webInfo["TotalBytesSent"] = Convert.ToDouble(sendBytes.ToString("0.0"));
  double sendReciveSUM = Convert.ToDouble(mo.Properties["TotalBytesTransfered"].Value);
                  sendReciveSUM = sendReciveSUM / 1024;
                  webInfo["TotalBytesTransfered"] = Convert.ToDouble(sendReciveSUM.ToString("0.0"));
  webInfo["TotalFilesSent"] = Convert.ToInt64(mo.Properties["TotalFilesSent"].Value);
                  webInfo["TotalFilesReceived"] = Convert.ToInt64(mo.Properties["TotalFilesReceived"].Value);
                  webInfo["TotalFilesTransferred"] = Convert.ToInt64(mo.Properties["TotalFilesTransferred"].Value);
  webInfo["TotalGetRequests"] = Convert.ToInt64(mo.Properties["TotalGetRequests"].Value);
                  webInfo["TotalPostRequests"] = Convert.ToInt64(mo.Properties["TotalPostRequests"].Value);
                  webInfo["TotalMethodRequests"] = Convert.ToInt64(mo.Properties["TotalMethodRequests"].Value);
  webInfo["TotalNotFoundErrors"] = Convert.ToInt64(mo.Properties["TotalNotFoundErrors"].Value);
                  webInfo["CurrentConnections"] = Convert.ToInt64(mo.Properties["CurrentConnections"].Value);
                  dataTable.Rows.Add(webInfo);
  }
                this.dataGridView1.DataSource = dataTable;
                Refresh();
            }
            catch (System.Exception ex)
            {
               MessageBox.Show(error);
            }
      }
  具体程序地址:http://files.iyunv.com/zhuxiangyu/%e7%bd%91%e7%ab%99%e6%b5%81%e9%87%8f%e7%9b%91%e6%8e%a7.rar
  大家有兴趣可以玩玩。
  
  
页: [1]
查看完整版本: iis 网站流量实时监控