I'm trying to build a load balancer for a program that is running in 2 different servers.
So far my load balancer, only checks the CPU usage of each server using an instance of PerformanceCounter in each server program.
I would also like to check the bandwidth usage of each server, how can I check that?
(it is probably done also using the PerformanceCounter but I'm unfamiliar with its usage)
Thanks to moonlight I've found this http://www.keyvan.ms/how-to-calculate-network-utilization-in-net
public double getNetworkUtilization(string networkCard){
const int numberOfIterations = 10;
PerformanceCounter bandwidthCounter = new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard);
float bandwidth = bandwidthCounter.NextValue();//valor fixo 10Mb/100Mn/
PerformanceCounter dataSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);
PerformanceCounter dataReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkCard);
float sendSum = 0;
float receiveSum = 0;
for (int index = 0; index < numberOfIterations; index++)
{
sendSum += dataSentCounter.NextValue();
receiveSum += dataReceivedCounter.NextValue();
}
float dataSent = sendSum;
float dataReceived = receiveSum;
double utilization = (8 * (dataSent + dataReceived)) / (bandwidth * numberOfIterations) * 100;
return utilization;
}
to find the available network cards this code helped me:
public void printNetworkCards()
{
PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
String[] instancename = category.GetInstanceNames();
foreach (string name in instancename)
{
Console.WriteLine(name);
}
}