2010-06-01 2 views
8

모두가 잘 지내기를 바랍니다.C#/VB.Net을 사용하여 프로그래밍 방식으로 웹 사이트 대역폭 (업로드 + 다운로드)을 MB 단위로 측정하는 방법은 무엇입니까?

내가 업로드 자신의 통계를 로컬 호스트의 모든 웹 사이트에 대한 대역폭 소비을 측정하고 저장 목표로 의 C#/VB.NetWindows 서비스를 쓰고, 원격/로컬 데이터베이스에 등을 다운로드 할 수 있습니다.

대상 플랫폼에는 Windows Server 2003, 2003 R2, 2008 및 2008 R2 만 포함됩니다.

나는이 일에 조금 검색 한 다음 발견 :

  1. SNMP 통계를 수집하기 위해 사용자 정의 네트워크 드라이버를 사용하여 Windows 2003
  2. 에서 발견된다를 mgmtapi.dll 사용을 .

가장 적합한, 보안하고 효과적인 방법/기술에 안내 또는 각기 다른 웹 사이트에 대한 대역폭 소비를 측정하는 데 사용 될 수있는 기술의 설정하십시오.

이와 관련하여 코드도 공유하십시오.

감사

스티브

답변

9

나는 어셈블리가 snmpsharpnet 느릅 나무는 .NET의 상단에 SNMP와 함께 연주하는 것은 매우 도움이된다라는 발견.

입력 대역폭 사용량의 비율이 agiven 인터페이스에 의해 계산 될 수 I는 this article 쓴 설명에 따르면

%의 대역폭 사용량 "에서"= (((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

이 코드가 유망 보인다! 나는 그것을 밖으로 시도하고 결과를 게시 할 것입니다. :) –

+2

몇 달 전 Perl과 같은 일을했지만 매력처럼 작동하지만 수면 시간이 더 길어야합니다. 그렇지 않으면 항상 0 값이됩니다. 보다 정확한 값을 원할 경우 WMI 또는 NFDump를 사용할 수 있습니다. Windows Server 클라이언트를 사용하고 있으므로 WMI를 사용할 수 있습니다. – raz3r

+0

매력처럼 작동합니다 :) 감사합니다. –