I need to write an app to account for bandwidth that comes from a sensor, it gives details on data flow in a table captured as below:
[ElasticsearchType(Name = "trafficSnapshot")]
public class TrafficSnapshot
{
// use epoch_second @ https://mixmax.com/blog/30x-faster-elasticsearch-queries
[Date(Format = "epoch_second")]
public long TimeStamp { get; set; }
[Nested]
public Sample[] Samples { get; set; }
}
[ElasticsearchType(Name = "sample")]
public class Sample
{
public ulong Bytes { get; set; }
public ulong Packets { get; set; }
public string Source { get; set; }
public string Destination { get; set; }
}
There will be potentially a lot of log entries especially at high traffic flows every second, I believe we can contain the growth by sharding/indexing by mm/dd/yyyy
(and discard unneeded days by deleting old indexes) - however when i create an index with a date string i get the error Invalid NEST response built from a unsuccessful low level call on PUT: /15%2F12%2F2017
. How should i define the index if i want to split in to dates?
If i log the data in this format, is it then possible for me to perform a summation per IP address for the total data send and total data received (over a date range which can be defined), or am i better off storing/indexing my data with a different structure before i progress further?
My full code is below and first stab tonight, pointers appreciated (or if i am going off track and may be better using logstash or similar please do let me know).
public static class DateTimeEpochHelpers
{
public static DateTime FromUnixTime(this long unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
public static long ToUnixTime(this DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalSeconds);
}
}
public static class ElasticClientTrafficSnapshotHelpers
{
public static void IndexSnapshot(this ElasticClient elasticClient, DateTime sampleTakenOn, Sample[] samples)
{
var timestamp = sampleTakenOn.ToUniversalTime();
var unixTime = timestamp.ToUnixTime();
var dateString = timestamp.Date.ToShortDateString();
// create the index if it doesn't exist
if (!elasticClient.IndexExists(dateString).Exists)
{
elasticClient.CreateIndex(dateString);
}
var response = elasticClient.Index(
new TrafficSnapshot
{
TimeStamp = unixTime,
Samples = samples
},
p => p
.Index(dateString)
.Id(unixTime)
);
}
}
class Program
{
static void Main(string[] args)
{
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
var elasticClient = new ElasticClient(settings);
var timestamp = DateTime.UtcNow;
var samples = new[]
{
new Sample() {Bytes = 100, Packets = 1, Source = "193.100.100.5", Destination = "8.8.8.8"},
new Sample() {Bytes = 1022, Packets = 1, Source = "8.8.8.8", Destination = "193.100.100.5"},
new Sample() {Bytes = 66, Packets = 1, Source = "193.100.100.1", Destination = "91.100.100.1"},
new Sample() {Bytes = 554, Packets = 1, Source = "193.100.100.10", Destination = "91.100.100.2"},
new Sample() {Bytes = 89, Packets = 1, Source = "9.9.9.9", Destination = "193.100.100.20"},
};
elasticClient.IndexSnapshot(timestamp, samples);
}
}