public static void PingTests() { foreach (string host in Config.LocalConfig.LocalPingDevices.Select(x => x.Hostname)) { Net.PingResult result = Net.Ping.PingHost(host, 5); if (result.NumDropped != 0) { Console.WriteLine($"{result.NumDropped} Dropped Pings Detected!"); //todo, throw error, email alert } Console.WriteLine($"Host {result.PinggedAddress} Avg Response {result.AvgResponse}ms"); } }
public static PingResult PingHost(string AddressToPing, int NumPings) { //Ensure the address is not empty, if it is, return null if (string.IsNullOrEmpty(AddressToPing)) { //todo logging, email alert return(null); } if (NumPings == 0) { NumPings = 1; } //Create a list for holding all the ping replies we get List <PingReply> replies = new List <PingReply>(); for (int x = 0; x < NumPings; x++) { //send the pings with 250ms sleeps until NumPings is sent replies.Add(DoPing(AddressToPing)); System.Threading.Thread.Sleep(100); } //Determine how many dropped pings we have, we dont really care why they were dropped int dropped = 0; foreach (PingReply reply in replies) { if (reply.Status != IPStatus.Success) { dropped++; } } //Create a new ping result, add all the values from the ping test that was just done PingResult result = new PingResult { PinggedAddress = AddressToPing, NumPings = NumPings, AvgResponse = Enumerable.Average(replies.Select(x => x.RoundtripTime).ToArray()), NumDropped = dropped }; //return the result return(result); }