2010-06-01 13 views
8

希望大家都很好。如何以編程方式使用C#/ VB.Net以MB爲單位測量網站帶寬(上傳+下載)?

我在C#/ VB.Net,旨在本地主機測量帶寬消耗所有網站和存儲他們的統計數據上載寫窗口服務,下載等本地/遠程數據庫。

目標平臺只包括的Windows Server 2003,2003 R2,2008年和2008 R2。

我搜索過這件事了一下,發現以下幾點:

  1. 使用SNMP MGMTAPI.DLL這是在Windows 2003
  2. 發現使用自定義的網絡驅動收集統計信息。

上最合適的,安全的有效的方法/技術請指導或設定的可用於測量對於每個不同的網站上的帶寬消耗這樣的技術。

請在這方面也分享任何代碼

問候

史蒂夫

回答

9

我發現了一個大會呼籲snmpsharpnet至極是非常有幫助的。NET的頂部,SNMP玩。

根據我在this article寫的說明輸入帶寬的使用率可以通過用於agiven接口來計算:

「以」 在帶寬%使用率=(((的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; 
     } 
    } 
    } 
} 

這只是概念驗證,現在您可能需要更新BackgroundWorker和計時器。

我希望它有幫助。

問候

JP

+0

你的代碼看起來很有希望!我會嘗試一下併發布結果。 :) – 2011-03-18 08:51:32

+2

我幾個月前用Perl做了同樣的事情,它的工作原理就像一個魅力,但是睡眠時間需要更高,否則你總會得到零值。如果你想要更精確的值,你可以選擇WMI或更好的NFDump。既然你正在使用Windows Server客戶端,我一定會去WMI。 – raz3r 2012-03-23 10:25:03

+0

作品像一個魅力:)謝謝 – 2012-09-13 12:40:57

相關問題