gpt4 book ai didi

c# - 如何以编程方式使用 C#/VB.Net 以 MB 为单位测量网站带宽(上传+下载)?

转载 作者:太空狗 更新时间:2023-10-29 18:30:23 27 4
gpt4 key购买 nike

希望大家在这里一切都好。

我正在用 C#/VB.Net 编写一个 windows 服务,旨在测量本地主机上所有网站的带宽消耗 并将他们的统计数据存储在本地/远程数据库中以供上传、下载等。

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

我对这个东西进行了一些搜索,发现了以下内容:

  1. 使用 Windows 2003 中的 SNMP mgmtapi.dll
  2. 使用自定义网络驱动程序收集统计信息。

请指导最合适、安全和有效的方法/技术或可用于测量每个不同网站的带宽消耗的一组此类技术。

也请分享这方面的任何代码

问候

史蒂夫

最佳答案

我找到了一个名为 snmpsharpnet 的程序集这对于在 .NET 上使用 SNMP 非常有帮助。

根据我在this article中写的解释输入带宽使用率可以通过给定接口(interface)计算:

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

这只是概念验证,现在您可能需要合并一个 BackgroundWorker 和一个计时器。


如果您不确定 SNMP 是否可以在服务器上连接并且您想要测试 SNMP 端口连接,您将需要 Microsoft PortQry .此工具确定端口是否正在监听。此工具还提供其他功能,具体取决于您使用的命令。

关于c# - 如何以编程方式使用 C#/VB.Net 以 MB 为单位测量网站带宽(上传+下载)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2948830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com