|
@ -19,17 +19,27 @@ namespace ISPChk |
|
|
optionsBuilder.UseSqlite("Data Source=ispchk.db"); |
|
|
optionsBuilder.UseSqlite("Data Source=ispchk.db"); |
|
|
_context = new ISPChkContext(optionsBuilder.Options); |
|
|
_context = new ISPChkContext(optionsBuilder.Options); |
|
|
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated."); |
|
|
System.Diagnostics.Debug.WriteLine("NetworkTest instantiated."); |
|
|
|
|
|
_context.Database.EnsureCreated(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async void LoadHostsFromDatabase() |
|
|
|
|
|
{ |
|
|
|
|
|
List<Host> hosts = await _context.Hosts.Include(host => host.PingItems).ToListAsync(); |
|
|
|
|
|
foreach (Host host in hosts) |
|
|
|
|
|
{ |
|
|
|
|
|
AddHost(host); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void AddHost(Host host) |
|
|
public void AddHost(Host host) |
|
|
{ |
|
|
{ |
|
|
System.Diagnostics.Debug.WriteLine("Host added!"); |
|
|
System.Diagnostics.Debug.WriteLine("Host added!"); |
|
|
var t = Task.Run(() => { |
|
|
var t = Task.Run(async () => { |
|
|
|
|
|
|
|
|
Ping pingSender = new Ping(); |
|
|
Ping pingSender = new Ping(); |
|
|
|
|
|
|
|
|
// Create a buffer of 32 bytes of data to be transmitted.
|
|
|
// Create a buffer of 32 bytes of data to be transmitted.
|
|
|
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
|
|
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
|
|
byte[] buffer = Encoding.ASCII.GetBytes(data); |
|
|
byte[] buffer = Encoding.ASCII.GetBytes(data); |
|
|
|
|
|
|
|
|
// Wait 10 seconds for a reply.
|
|
|
// Wait 10 seconds for a reply.
|
|
@ -44,15 +54,31 @@ namespace ISPChk |
|
|
System.Diagnostics.Debug.WriteLine("Pinging " + host.Name); |
|
|
System.Diagnostics.Debug.WriteLine("Pinging " + host.Name); |
|
|
|
|
|
|
|
|
while (true) |
|
|
while (true) |
|
|
|
|
|
{ |
|
|
|
|
|
long min = timeout+1; |
|
|
|
|
|
long max = 0; |
|
|
|
|
|
long avg = 0; |
|
|
|
|
|
int successes = 0; |
|
|
|
|
|
for(var i=0;i<5;++i) |
|
|
{ |
|
|
{ |
|
|
// Send the request.
|
|
|
// Send the request.
|
|
|
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); |
|
|
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); |
|
|
|
|
|
|
|
|
if (reply.Status == IPStatus.Success) |
|
|
if (reply.Status == IPStatus.Success) |
|
|
{ |
|
|
{ |
|
|
System.Diagnostics.Debug.WriteLine("Reply from "+reply.Address.ToString()+ |
|
|
successes++; |
|
|
": bytes="+reply.Buffer.Length+ |
|
|
System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() + |
|
|
" time="+ reply.RoundtripTime+"ms"); |
|
|
": bytes=" + reply.Buffer.Length + |
|
|
|
|
|
" time=" + reply.RoundtripTime + "ms"); |
|
|
|
|
|
avg += reply.RoundtripTime; |
|
|
|
|
|
if(reply.RoundtripTime < min) |
|
|
|
|
|
{ |
|
|
|
|
|
min = reply.RoundtripTime; |
|
|
|
|
|
} |
|
|
|
|
|
if(reply.RoundtripTime > max) |
|
|
|
|
|
{ |
|
|
|
|
|
max = reply.RoundtripTime; |
|
|
|
|
|
} |
|
|
//System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl);
|
|
|
//System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl);
|
|
|
//System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
|
|
|
//System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
|
|
|
} |
|
|
} |
|
@ -60,7 +86,16 @@ namespace ISPChk |
|
|
{ |
|
|
{ |
|
|
System.Diagnostics.Debug.WriteLine(reply.Status); |
|
|
System.Diagnostics.Debug.WriteLine(reply.Status); |
|
|
} |
|
|
} |
|
|
Thread.Sleep(1000); |
|
|
Thread.Sleep(500); |
|
|
|
|
|
} |
|
|
|
|
|
avg = avg / successes; |
|
|
|
|
|
System.Diagnostics.Debug.WriteLine("min:" + min + " max:" + max + " avg:" + avg); |
|
|
|
|
|
PingItem pi = new PingItem { Date = DateTime.UtcNow, Min = min, Max = max, Avg = avg }; |
|
|
|
|
|
if(host.PingItems == null) |
|
|
|
|
|
host = _context.Hosts.Include(host => host.PingItems).Where(h => h.HostId == host.HostId).Single(); |
|
|
|
|
|
host.PingItems.Add(pi); |
|
|
|
|
|
_context.SaveChanges(); |
|
|
|
|
|
Thread.Sleep(5000); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|