2010-06-01 14 views
8

Espero que todos estén bien aquí.Cómo medir el ancho de banda de un sitio web (Cargar + Descargar) en MB usando C#/VB.Net programáticamente?

Estoy escribiendo un servicio ventanas en C#/VB.Net que tiene como objetivo medir el consumo de ancho de banda para todos los sitios web en localhost y almacenar sus estadísticas para la carga, descarga, etc en la base de datos local/remoto.

Las plataformas de destino incluyen solo Windows Server 2003, 2003 R2, 2008 y 2008 R2.

He buscado un poco en esta cosa y se encontró lo siguiente:

  1. Usando SNMP Mgmtapi.dll que se encuentra en Windows 2003
  2. El uso de un controlador de red encargo para recopilar estadísticas .

favor guía en el, seguro y eficaz metodología/técnica más apropiada o conjunto de tales técnicas que pueden utilizarse para medir el consumo de ancho de banda para cada sitio web diferente.

También comparta cualquier código en este sentido.

Saludos

Steve

Respuesta

9

he encontrado un montaje llamado snmpsharpnet cual es muy útil para jugar con SNMP en la parte superior de .NET.

Según la explicación escribí en this article la tasa de uso de ancho de banda de entrada se puede calcular para la interfaz agiven:

"En" el uso de ancho de banda en% = (((ifInOctets (T2) -ifInOctets (t1)) * 8) * 100)/(ifSpeed ​​* (t2-t1))

Aquí hay un código de muestra.

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; 
     } 
    } 
    } 
} 

esto es sólo au prueba de concepto, ahora es probable que necesite un incoparate BackgroundWorker y un temporizador.

Espero que ayude.

Saludos

JP

+0

su código parece prometedor !. Lo probaré y publicaré los resultados. :) –

+2

Hice lo mismo con Perl hace unos meses y funciona como un encanto, sin embargo, el tiempo de sueño debe ser mayor, de lo contrario, siempre obtendrá cero valores. Si desea valores más precisos, puede optar por WMI o, mejor aún, NFDump. Como trabaja con clientes de Windows Server, definitivamente iré por WMI. – raz3r

+0

Funciona como un encanto :) Gracias –

Cuestiones relacionadas