如何使用C#/ VB.Net编程测量以MB为网站带宽(上传+下载)? [英] How to measure a Website Bandwidth (Upload+Download) in MB using C#/VB.Net programmatically?
问题描述
希望大家在这里很好。
我在写的 Windows服务 C#/ VB.Net ,旨在测量带宽消耗有关本地主机所有网站并保存上传他们的统计资料,下载等本地/远程数据库。
I am writing a windows service in C#/VB.Net that aims at measuring bandwidth consumption for all WebSites on localhost and store their statistics for upload , download etc on local/remote database.
目标平台只包括的Windows Server 2003,2003 R2,2008年和2008 R2。
我已经搜索这个东西了一下,发现以下内容:
I have searched a bit on this thing and found the following:
- 使用 SNMP MGMTAPI.DLL 这是在Windows 2003中找到
- 使用自定义网络驱动程序以收集统计信息。
- Using SNMP mgmtapi.dll which is found in Windows 2003
- Using a custom Network Driver to collect statistics.
请指导最合适的,安全并有效的方法/技术或设置可用于测量各种不同的网站的带宽消耗这样的技术。
Please guide on the most appropriate ,secure and effective methodology/technique or set of such techniques which can be used to measure the bandwidth consumption for each different website.
也请分享 code 在这方面。
问候
史蒂夫
推荐答案
我发现所谓的 snmpsharpnet 至极有很大的帮助一起玩的组件SNMP基于.NET的顶部。
I found An assembly called snmpsharpnet wich is very helpful to play with SNMP on the top of .NET.
据我在<一写的说明href=\"http://stackoverflow.com/questions/5253396/how-do-i-collect-bandwidth-utilization-data-on-remote-devices-switches-servers/5340942#5340942\">this文章输入带宽的使用的速率可以通过对agiven接口计算:
According to the explanation I wrote in this article the rate of input bandwith usage can be computed by for agiven interface:
以在%带宽使用量=(((的ifInOctets(T2)-ifInOctets(T1))* 8)* 100)/(ifSpeed *(T2-T1))
"In" Bandwith usage in % = (((ifInOctets(t2)-ifInOctets(t1)) * 8)*100) / (ifSpeed * (t2-t1))
下面是一个简单的code。
Here is a sample code.
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;
}
}
}
}
这是概念的金只是证明,现在你可能需要incoparate一个BackgroundWorker和一个计时器。
This is just au proof of concept, now you probably need to incoparate a BackgroundWorker and a timer.
我希望它能帮助。
最好的问候
JP
这篇关于如何使用C#/ VB.Net编程测量以MB为网站带宽(上传+下载)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!