예제 #1
0
        private static void LogPingResult(PingResult dnsResult, PingResult tcpResult)
        {
            //Submit metrics to AppInsights for outcome (success=1 / failure=0) and response time for Dns lookup and Tcp port ping
            var telemetry = new TelemetryClient();

            telemetry.TrackMetric("NetDnsHealth_" + dnsResult.EndPoint.Replace(":", "_"), dnsResult.IsSuccess);
            telemetry.TrackMetric("NetDnsTime_" + dnsResult.EndPoint.Replace(":", "_"), dnsResult.Value);
            telemetry.TrackMetric("NetTcpHealth_" + tcpResult.EndPoint.Replace(":", " "), tcpResult.IsSuccess);
            telemetry.TrackMetric("NetTcpTime_" + tcpResult.EndPoint.Replace(":", "_"), tcpResult.Value);
        }
예제 #2
0
        private void RunPingTarget(string DestinationHost, int port)
        {
            Stopwatch timeKeeper               = new Stopwatch();
            IPAddress lastKnownAddress         = null;
            IPAddress resolvedAddress          = null;
            bool      isDestinationAnIpAddress = false;

            DateTime TimeStamp   = DateTime.UtcNow;
            int      dnsResult   = 0;
            long     dnsDuration = 0;
            int      tcpResult   = 0;
            long     tcpDuration = 0;

            isDestinationAnIpAddress = IPAddress.TryParse(DestinationHost, out lastKnownAddress);

            // DNS lookup
            if (isDestinationAnIpAddress)
            {
                dnsResult   = 1;
                dnsDuration = 0;
            }
            else
            {
                TimeStamp = DateTime.UtcNow;
                timeKeeper.Start();
                resolvedAddress = null;

                try
                {
                    foreach (IPAddress Address in Dns.GetHostEntry(DestinationHost).AddressList)
                    {
                        // Check if it is v4
                        if (Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            resolvedAddress = Address;
                        }
                    }

                    if (resolvedAddress != null)
                    {
                        lastKnownAddress = resolvedAddress;
                    }
                    dnsResult = 1;
                }
                catch
                {
                    dnsResult   = 0;
                    dnsDuration = 0;
                }
                timeKeeper.Stop();
                dnsDuration = timeKeeper.ElapsedMilliseconds;
                timeKeeper.Reset();
            }

            // TCP Lookup
            timeKeeper.Start();
            try
            {
                using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    // Connect to configured TCP port
                    var asyncResult = s.BeginConnect(new IPEndPoint(lastKnownAddress, port), (result) => { return; }, null);
                    try
                    {
                        asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5));
                        if (!s.Connected)
                        {
                            tcpResult   = 0;
                            tcpDuration = 0;
                        }
                        else
                        {
                            tcpResult = 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogException(ex);
                        tcpResult   = 0;
                        tcpDuration = 0;
                    }
                    finally
                    {
                        if (s.Connected)
                        {
                            s.Disconnect(true);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                LogException(exception);
                tcpResult   = 0;
                tcpDuration = 0;
            }

            timeKeeper.Stop();
            tcpDuration = timeKeeper.ElapsedMilliseconds;
            timeKeeper.Reset();

            /* Populate DNSLookup Result */
            PingResult pDnsResult = new PingResult();

            pDnsResult.EndPoint  = DestinationHost + "_" + port.ToString();
            pDnsResult.IsSuccess = dnsResult;
            pDnsResult.Value     = dnsDuration;

            /* Populate TCPLookup Result */
            PingResult pTcpResult = new PingResult();

            pTcpResult.EndPoint  = DestinationHost + "_" + port.ToString();
            pTcpResult.IsSuccess = tcpResult;
            pTcpResult.Value     = tcpDuration;

            /* Push data to Application Insights */
            LogPingResult(pDnsResult, pTcpResult);
        }
        private void RunPingTarget(string DestinationHost, int port)
        {
            Stopwatch timeKeeper = new Stopwatch();
            IPAddress lastKnownAddress = null;
            IPAddress resolvedAddress = null;
            bool isDestinationAnIpAddress = false;

            DateTime TimeStamp = DateTime.UtcNow;
            int dnsResult = 0;
            long dnsDuration = 0;
            int tcpResult = 0;
            long tcpDuration = 0;

            isDestinationAnIpAddress = IPAddress.TryParse(DestinationHost, out lastKnownAddress);

            // DNS lookup
            if (isDestinationAnIpAddress)
            {
                dnsResult = 1;
                dnsDuration = 0;
            }
            else
            {
                TimeStamp = DateTime.UtcNow;
                timeKeeper.Start();
                resolvedAddress = null;

                try
                {
                    foreach (IPAddress Address in Dns.GetHostEntry(DestinationHost).AddressList)
                    {
                        // Check if it is v4
                        if (Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            resolvedAddress = Address;
                        }
                    }

                    if (resolvedAddress != null)
                    {
                        lastKnownAddress = resolvedAddress;
                    }
                    dnsResult = 1;
                }
                catch
                {
                    dnsResult = 0;
                    dnsDuration = 0;
                }
                timeKeeper.Stop();
                dnsDuration = timeKeeper.ElapsedMilliseconds;
                timeKeeper.Reset();
            }

            // TCP Lookup
            timeKeeper.Start();
            try
            {
                using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    // Connect to configured TCP port
                    var asyncResult = s.BeginConnect(new IPEndPoint(lastKnownAddress, port), (result) => { return; }, null);
                    try
                    {
                        asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5));
                        if (!s.Connected)
                        {
                            tcpResult = 0;
                            tcpDuration = 0;
                        }
                        else
                        {
                            tcpResult = 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogException(ex);
                        tcpResult = 0;
                        tcpDuration = 0;
                    }
                    finally
                    {
                        if (s.Connected)
                        {
                            s.Disconnect(true);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                LogException(exception);
                tcpResult = 0;
                tcpDuration = 0;
            }

            timeKeeper.Stop();
            tcpDuration = timeKeeper.ElapsedMilliseconds;
            timeKeeper.Reset();

            /* Populate DNSLookup Result */
            PingResult pDnsResult = new PingResult();
            pDnsResult.EndPoint = DestinationHost + "_" + port.ToString();
            pDnsResult.IsSuccess = dnsResult;
            pDnsResult.Value = dnsDuration;

            /* Populate TCPLookup Result */
            PingResult pTcpResult = new PingResult();
            pTcpResult.EndPoint = DestinationHost + "_" + port.ToString();
            pTcpResult.IsSuccess = tcpResult;
            pTcpResult.Value = tcpDuration;

            /* Push data to Application Insights */
            LogPingResult(pDnsResult, pTcpResult);
        }
 private static void LogPingResult(PingResult dnsResult, PingResult tcpResult)
 {
     //Submit metrics to AppInsights for outcome (success=1 / failure=0) and response time for Dns lookup and Tcp port ping
     var telemetry = new TelemetryClient();
     telemetry.TrackMetric("NetDnsHealth_" + dnsResult.EndPoint.Replace(":", "_") ,dnsResult.IsSuccess);
     telemetry.TrackMetric("NetDnsTime_"   + dnsResult.EndPoint.Replace(":", "_") ,dnsResult.Value);
     telemetry.TrackMetric("NetTcpHealth_" + tcpResult.EndPoint.Replace(":", " ") ,tcpResult.IsSuccess);
     telemetry.TrackMetric("NetTcpTime_"   + tcpResult.EndPoint.Replace(":", "_") ,tcpResult.Value);
 }