private async Task PollNetworkUtilizationAsync() { var utilizationTable = _canQueryAdapterUtilization ? "Win32_PerfRawData_Tcpip_NetworkAdapter" : "Win32_PerfRawData_Tcpip_NetworkInterface"; var query = $@" SELECT Name, Timestamp_Sys100NS, BytesReceivedPersec, BytesSentPersec, PacketsReceivedPersec, PacketsSentPersec FROM {utilizationTable}"; var queryTime = DateTime.UtcNow.ToEpochTime(); var combinedUtil = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = 0, OutAvgBps = 0 }; using (var q = Wmi.Query(Endpoint, query)) { foreach (var data in await q.GetDynamicResultAsync().ConfigureAwait(false)) { var perfData = new PerfRawData(this, data); var name = perfData.Identifier; var iface = Interfaces.Find(i => name == GetCounterName(i.Name)); if (iface == null) { continue; } iface.InBps = (float)perfData.GetCalculatedValue("BytesReceivedPersec", 10000000); iface.OutBps = (float)perfData.GetCalculatedValue("BytesSentPersec", 10000000); iface.InPps = (float)perfData.GetCalculatedValue("PacketsReceivedPersec", 10000000); iface.OutPps = (float)perfData.GetCalculatedValue("PacketsSentPersec", 10000000); var util = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = iface.InBps, OutAvgBps = iface.OutBps }; var netData = NetHistory.GetOrAdd(iface.Name, k => new List <Interface.InterfaceUtilization>(1024)); UpdateHistoryStorage(netData, util); if (PrimaryInterfaces.Contains(iface)) { combinedUtil.InAvgBps += util.InAvgBps; combinedUtil.OutAvgBps += util.OutAvgBps; } } } UpdateHistoryStorage(CombinedNetHistory, combinedUtil); }
public override Task <List <Interface.InterfaceUtilization> > GetUtilization(Interface @interface, DateTime?start, DateTime?end, int?pointCount = null) { var node = _wmiNodes.FirstOrDefault(x => x.Id == @interface.NodeId); if (node == null) { return(Task.FromResult(new List <Interface.InterfaceUtilization>())); } var history = node.GetInterfaceUtilizationHistory(@interface); Interface.InterfaceUtilization startVal = null; if (start.HasValue) { startVal = new Interface.InterfaceUtilization { DateTime = start.Value }; } Interface.InterfaceUtilization endVal = null; if (end.HasValue) { endVal = new Interface.InterfaceUtilization { DateTime = end.Value }; } return(FilterHistory(history, startVal, endVal, new InterfaceUtilComparer())); }
private async Task PollNetworkUtilization() { const string query = @" SELECT Name, BytesReceivedPersec, BytesSentPersec, PacketsReceivedPersec, PacketsSentPersec FROM Win32_PerfFormattedData_Tcpip_NetworkInterface"; var queryTime = DateTime.UtcNow.ToEpochTime(); var combinedUtil = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = 0, OutAvgBps = 0 }; using (var q = Wmi.Query(Name, query)) { foreach (var data in await q.GetDynamicResult()) { if (data == null) { continue; } var iface = Interfaces.FirstOrDefault(i => data.Name == GetCounterName(i.Name)); if (iface == null) { continue; } iface.InBps = data.BytesReceivedPersec; iface.OutBps = data.BytesSentPersec; iface.InPps = data.PacketsReceivedPersec; iface.OutPps = data.PacketsSentPersec; var util = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = iface.InBps, OutAvgBps = iface.OutBps }; var netData = NetHistory.GetOrAdd(iface.Name, k => new List <Interface.InterfaceUtilization>(1024)); UpdateHistoryStorage(netData, util); if (PrimaryInterfaces.Contains(iface)) { combinedUtil.InAvgBps += util.InAvgBps; combinedUtil.OutAvgBps += util.OutAvgBps; } } } UpdateHistoryStorage(CombinedNetHistory, combinedUtil); }
public void AddNetworkUtilization(Interface iface, Interface.InterfaceUtilization item) { if (iface == null) { return; } var ownIface = Interfaces.FirstOrDefault(x => x == iface); if (ownIface == null) { return; } List <Interface.InterfaceUtilization> data; if (!_netUtilization.ContainsKey(ownIface.Name)) { data = new List <Interface.InterfaceUtilization>(1024); _netUtilization[ownIface.Name] = data; } else { data = _netUtilization[ownIface.Name]; } if (!item.InAvgBps.HasValue) { item.InAvgBps = item.InMaxBps; } if (!item.OutAvgBps.HasValue) { item.OutAvgBps = item.OutMaxBps; } if (iface.Speed.HasValue && iface.Speed.Value > 0) { item.MaxLoad = (short)(100 * (item.InMaxBps + item.OutMaxBps) / iface.Speed); item.AvgLoad = (short)(100 * (item.InAvgBps + item.OutAvgBps) / iface.Speed); } UpdateHistoryStorage(data, item, x => x.DateTime); }
private async Task PollNetworkUtilization() { const string query = @" SELECT Name, BytesReceivedPersec, BytesSentPersec, PacketsReceivedPersec, PacketsSentPersec FROM Win32_PerfFormattedData_Tcpip_NetworkInterface"; var queryTime = DateTime.UtcNow.ToEpochTime(); var combinedUtil = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = 0, OutAvgBps = 0 }; using (var q = Wmi.Query(Name, query)) { foreach (var data in await q.GetDynamicResult()) { if (data == null) continue; var iface = Interfaces.FirstOrDefault(i => data.Name == GetCounterName(i.Name)); if (iface == null) continue; iface.InBps = data.BytesReceivedPersec; iface.OutBps = data.BytesSentPersec; iface.InPps = data.PacketsReceivedPersec; iface.OutPps = data.PacketsSentPersec; var util = new Interface.InterfaceUtilization { DateEpoch = queryTime, InAvgBps = iface.InBps, OutAvgBps = iface.OutBps }; var netData = NetHistory.GetOrAdd(iface.Name, k => new List<Interface.InterfaceUtilization>(1024)); UpdateHistoryStorage(netData, util); if (PrimaryInterfaces.Contains(iface)) { combinedUtil.InAvgBps += util.InAvgBps; combinedUtil.OutAvgBps += util.OutAvgBps; } } } UpdateHistoryStorage(CombinedNetHistory, combinedUtil); }