private async Task WriteTrafficData(MonitoredPort port, long octets, TrafficType trafficType) { var entity = new CollectedTrafficData { TimeScanned = DateTime.Now, Octets = octets, TrafficType = trafficType, PortNumber = port.PortNumber, SnmpIpAddress = port.SnmpIpAddress }; var existing = await CollectedTrafficDatas.Query() .Where(data => data.TrafficType == trafficType) .Include(data => data.MonitoredPort) .Where(data => data.MonitoredPort.SnmpIpAddress == port.SnmpIpAddress) .Where(data => data.MonitoredPort.PortNumber == port.PortNumber) .OrderByDescending(data => data.TimeScanned) .FirstOrDefaultAsync(); if (existing == null) { entity.DiffedTimeSpan = null; entity.Octets = octets; entity.AbsoluteOctets = octets; } else { entity.DiffedTimeSpan = (entity.TimeScanned - existing.TimeScanned); entity.Octets = (octets - existing.AbsoluteOctets); entity.AbsoluteOctets = octets; } CollectedTrafficDatas.Insert(entity); }
public async Task <List <DateTime> > GetPendingReportTimes() { var times = await CollectedTrafficDatas.Query() .Select(data => new { data.TimeScanned.Hour, data.TimeScanned.Day, data.TimeScanned.Month, data.TimeScanned.Year }) .Distinct() .ToListAsync(); return(times .Select(time => new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0)) .Where(time => DateTime.Now.Subtract(time) > new TimeSpan(2, 0, 0)) .ToList()); }
public async Task WritePortReports(int year, int month, int day, int hour) { var startTime = new DateTime(year, month, day, hour, 0, 0); var endTime = startTime.Add(new TimeSpan(1, 0, 0)); var inboundHighscore = await SnmpStatisticsService .GetHighscore(int.MaxValue, startTime, endTime, TrafficType.Inbound); var outboundHighscore = await SnmpStatisticsService .GetHighscore(int.MaxValue, startTime, endTime, TrafficType.Outbound); if (inboundHighscore == null || outboundHighscore == null) { throw new Exception("high score failure :("); } var reports = new List <PortReport>(); var monitoredPorts = await MonitoredPorts.Query().ToListAsync(); foreach (var monitoredPort in monitoredPorts) { var inboundStatistics = inboundHighscore .FirstOrDefault( stats => stats.MonitoredPort.SnmpIpAddress == monitoredPort.SnmpIpAddress && stats.MonitoredPort.PortNumber == monitoredPort.PortNumber); var outboundStatistics = outboundHighscore .FirstOrDefault( stats => stats.MonitoredPort.SnmpIpAddress == monitoredPort.SnmpIpAddress && stats.MonitoredPort.PortNumber == monitoredPort.PortNumber); if (inboundStatistics == null || outboundStatistics == null) { continue; } var currentReport = new PortReport { Day = day, Hour = hour, Month = month, Year = year, InboundTraffic = inboundStatistics.InboundTraffic, OutboundTraffic = outboundStatistics.OutboundTraffic, PortNumber = monitoredPort.PortNumber, SnmpIpAddress = monitoredPort.SnmpIpAddress }; reports.Add(currentReport); } PortReports.InsertRange(reports); await Context.SaveAsync(); var itemsToDelete = await CollectedTrafficDatas.Query() .Where(data => data.TimeScanned >= startTime) .Where(data => data.TimeScanned <= endTime) .ToListAsync(); itemsToDelete.ForEach(item => CollectedTrafficDatas.Delete(item)); await Context.SaveAsync(); }
public async Task <List <PortStatistics> > GetHighscore(int countOfEntries, DateTime startTime, DateTime endTime, TrafficType type) { try { var entriesInTimespan = await CollectedTrafficDatas.Query() .Where(data => data.TrafficType == type) .Where(data => data.DiffedTimeSpan != null) .Include(data => data.MonitoredPort) .Where(data => !data.MonitoredPort.ExcludeFromStatistics) .GroupBy(data => data.MonitoredPort) .Select(data => data.Where(inner => inner.TimeScanned >= startTime)) .Select(data => data.Where(inner => inner.TimeScanned <= endTime)) .Select(data => data.OrderBy(inner => inner.TimeScanned)) .Select(data => new { Sum = data.Sum(inner => inner.Octets), Data = data }) .OrderByDescending(data => data.Sum) .Take(countOfEntries) .ToListAsync(); var desiredSampledSeconds = (endTime - startTime).TotalSeconds; var results = new List <PortStatistics>(); var monitoredPorts = await MonitoredPorts.Query() .Include(port => port.SnmpTarget) .ToListAsync(); foreach (var entry in entriesInTimespan) { var sampledSeconds = entry.Data.Sum(entity => entity.DiffedTimeSpan.Value.TotalSeconds); var correctionFactor = (float)desiredSampledSeconds / sampledSeconds; var trafficInSpan = (long)(correctionFactor * entry.Sum); var result = new PortStatistics { MonitoredPort = monitoredPorts.FirstOrDefault( port => port.SnmpIpAddress == entry.Data.FirstOrDefault().SnmpIpAddress&& port.PortNumber == entry.Data.FirstOrDefault().PortNumber), }; result.SnmpTarget = result.MonitoredPort.SnmpTarget; result.Comment = result.MonitoredPort.Comment; var bytesPerSecond = trafficInSpan / desiredSampledSeconds; var ordinals = new[] { "", "K", "M", "G", "T", "P", "E" }; var ordinal = 0; while (bytesPerSecond > 1024) { bytesPerSecond /= 1024; ordinal++; } result.EstimatedSpeed = String.Format("{0} {1}b/s", Math.Round(bytesPerSecond, 2, MidpointRounding.AwayFromZero), ordinals[ordinal]); switch (type) { case TrafficType.Inbound: result.InboundTraffic = trafficInSpan; break; case TrafficType.Outbound: result.OutboundTraffic = trafficInSpan; break; } results.Add(result); } return(results); } catch { return(new List <PortStatistics>()); } }