代码之家  ›  专栏  ›  技术社区  ›  Steve Johnson

如何使用c/vb.net以编程方式测量网站带宽(上载+下载)(MB)?

  •  8
  • Steve Johnson  · 技术社区  · 16 年前

    希望大家都没事。

    我正在写一篇文章 视窗服务 在里面 C.V/VB.NET 目的在于 测量带宽消耗 对于上的所有网站 本地服务器 并将统计数据存储在本地/远程数据库中,以便上传、下载等。

    目标平台仅包括 Windows Server 2003、2003 R2、2008和2008 R2。

    我对这件事做了一些调查,发现了以下几点:

    1. 使用 SNMP管理API.dll 在Windows 2003中找到
    2. 使用A 自定义网络驱动程序 收集统计数据。

    请以最合适的为指导, 保护 以及有效的方法/技术或此类技术的集合,可用于测量每个不同网站的带宽消耗。

    请同时分享 代码 在这方面。

    当做

    史蒂夫

    1 回复  |  直到 15 年前
        1
  •  9
  •   Community Mohan Dere    9 年前

    我发现一个叫做 snmpsharpnet wich对于在.net上使用snmp非常有用。

    根据我写的解释 this article 输入带使用率可由以下公式计算:

    “in”波段,使用率为%=((ifinoctets(t2)-ifinoctets(t1))*8)*100)/(ifspeed*(t2-t1))

    这是一个示例代码。

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using SnmpSharpNet;
    
    namespace Exemple2
    {
      class Program
      {
        static void Main(string[] args)
        {
          /* Get an SNMP Object
           */
          SimpleSnmp snmpVerb = new SimpleSnmp("192.168.1.121", 161, "public");
          if (!snmpVerb.Valid)
          {
            Console.WriteLine("Seems that IP or comunauty is not cool");
            return;
          }
    
    
          /* Sample of simple Get usage on ifSpeed on interface 10
           * TODO : for sure you have to detect the right interface
           */
          Oid oidifSpeed = new Oid(".1.3.6.1.2.1.2.2.1.5.10");
    
          /* Getting ifSpeed
           */
          Dictionary<Oid, AsnType> snmpDataS = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifSpeed.ToString() });
          if (snmpDataS != null)
            Console.WriteLine("Interface speed \"{0}\" : {1}", oidifSpeed.ToString(), snmpDataS[oidifSpeed].ToString());
          else
            Console.WriteLine("Not Glop!");
    
          /* Sample of simple Get usage on ifInOctets on interface 10
           * TODO : for sure you have to detect the right interface
           */
          Oid oidifInOctets = new Oid(".1.3.6.1.2.1.2.2.1.10.10");
          Dictionary<Oid, AsnType> snmpData1;
    
          /* Getting it for the first time
           */
          snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
          if (snmpData1 != null)
            Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
          else
            Console.WriteLine("Not Glop!");
    
          int missed = 0;
          while (true)
          {
            if (missed == 0)
            {
              /* When you detect a non refesh data, keep the last one
               */
              snmpData1 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
              if (snmpData1 != null)
                Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData1[oidifInOctets].ToString());
              else
                Console.WriteLine("Not Glop!");
            }
    
            /* Some Wait (less aproximative)
             */
            int duration = 5;
            System.Threading.Thread.Sleep(duration*1000); // duration seconds
    
            /* Getting it for the Second time
             */
            Dictionary<Oid, AsnType> snmpData2 = snmpVerb.Get(SnmpVersion.Ver2, new string[] { oidifInOctets.ToString() });
            if (snmpData2 != null)
              Console.WriteLine("Number of In octets \"{0}\" : {1}", oidifInOctets.ToString(), snmpData2[oidifInOctets].ToString());
            else
              Console.WriteLine("Not Glop!");
    
            Counter32 I1 = new Counter32();
            I1.Set(snmpData1[oidifInOctets]);
            Counter32 I2 = new Counter32();
            I2.Set(snmpData2[oidifInOctets]);
            Counter32 speed = new Counter32();
            speed.Set(snmpDataS[oidifSpeed]);
    
            if (I2.Value == I1.Value)
            {
              missed += 1;
              continue;
            }
            decimal bandWithUsage = (((decimal)(I2.Value - I1.Value) * 8) * 100) / (speed * duration * (1+missed));
            Console.WriteLine("BandWith usage : {0}%", bandWithUsage);
            missed = 0;
          }
        }
      }
    }
    

    这只是一个概念的证明,现在你可能需要一个背景工作者和一个计时器。

    希望能有所帮助。

    最好的问候

    日本药典