我的小工具开源一下-PingTest
czxs2000
8年前
<p>先交代下背景,最近我们项目组的网络真是太渣了,时常remote不了另外一个地方的机器,过个几分钟就断开连接,太烦躁了,严重影响工作心情。。。于是想着做个工具记录下每天的断开remote连接的时刻,然后将这份记录给IT人员,让他们去分析分析到底哪里出问题了。到了第二个版本,我就将这个记录汇总成了一个Report,包含了与远程机器的累计失联次数,累计失联时间,最长失联时间,最长在线时间。将失联和重新建立连接的时间点打印在控制台窗口和log文件中。效果还不错。</p> <h2>1.项目结构:</h2> <p style="text-align:center"><img src="https://simg.open-open.com/show/bda5b452cf4e44a24ad341c22d1e222b.png"></p> <h2>2.结果展示:</h2> <p>Consel窗口</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/70ccd978378fea04be8b63953a1787b0.png"></p> <p>Log文件:</p> <p><img src="https://simg.open-open.com/show/87cc803421f22b04ef4d3e556818bb31.png"></p> <h2>3.原理</h2> <h2>1.检测的原理</h2> <p>1.1 用两个后台线程不断的去ping另外一台机器的IP地址,其中一个线程检测是否可以连接,如果可以连接,则记录时间点及状态,另外一个线程检测是否可以连接,如果不能连接,则retry ,如果连续retry 3次都不能ping通,则表示该机器失联,记录这次失联的时间点。</p> <p>ping的代码:</p> <pre> <code class="language-cs">public static bool Ping(string ip) { options.DontFragment = true; byte[] buffer = Encoding.ASCII.GetBytes(data); reply = p.Send(ip, timeout, buffer, options); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { return true; } else { return false; } }</code></pre> <p>1.2 两个线程去检测机器连接状态:</p> <pre> <code class="language-cs">public static void TestTargetIP1Connecting() { while (true) { if (targetIP1lastStatus != NetworkStatus.Online) { pingTarget1Count++; if (PingUtil.IsConnected(targetIP1)) { timePoints1.Add(new TimePoint(DateTime.Now, NetworkStatus.Online)); targetIP1lastStatus = NetworkStatus.Online; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(FormatConnectingConsoleLog(targetIP1, pingTarget1Count)); Console.ForegroundColor = ConsoleColor.White; log1.Info(FormatConnectingFileLog(targetIP1, pingTarget1Count)); } } Thread.Sleep(sleepTime); } } public static void TestTargetIP1Disconnected() { while (true) { if (targetIP1lastStatus != NetworkStatus.Offline && !PingUtil.IsConnected(targetIP1)) { timePoints1.Add(new TimePoint(DateTime.Now, NetworkStatus.Offline)); pingTarget1Count = 0; targetIP1GroupCount++; targetIP1lastStatus = NetworkStatus.Offline; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(FormatDisconnectedConsoleLog(targetIP1, targetIP1GroupCount)); Console.ForegroundColor = ConsoleColor.White; log1.Info(FormatDisconnectedFileLog(targetIP1, targetIP1GroupCount)); } Thread.Sleep(sleepTime); } }</code></pre> <p>1.3 retry的机制</p> <pre> <code class="language-cs">public static bool IsConnected(string ipAddress) { bool isConnected = true; try { RetryScope.BeginRetry(retryInterval); do { try { try { if (!PingUtil.Ping(ipAddress)) { throw new Exception(string.Format("{0} Disconnected", ipAddress), new Exception("Disconnected")); } } finally { } break; } catch (Exception ex) { if (!RetryScope.HandleException(ex)) { throw; } } } while (RetryScope.Continue); } catch (Exception cex) { isConnected = false; } finally { RetryScope.EndRetry(retryInterval); } return isConnected; }</code></pre> <h2>2.对所有时间点进行分析</h2> <p style="text-align:center"><img src="https://simg.open-open.com/show/b2b76ff25be4d96fe5f6e9f1878c7ddc.png"></p> <p>2.1 统计分析的核心代码。发现用Linq超级爽,一下子就统计分析出来了。</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/894f63446e2e70baeced08ae4b814c4a.png"></p> <p>2.2 单元测试:</p> <p>因为每次统计分析都需要手动连网或断网来测试,所以写了这部分的单元测试。</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/557baa81cda6024b96502912fa7e4399.png"></p> <h2>3.生成Report</h2> <p>一个打印到Console窗口的类:ConsoleReport.cs</p> <p>一个记录到Log4Net的日志文件中的类LogFileReport.cs</p> <p>这两个类继承接口IExport.cs。</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/0a863cfb26ab81c979fc392a53731791.png"></p> <p style="text-align:center"><img src="https://simg.open-open.com/show/e6daa9cf848b8d514884ffa26d1319fe.png"></p> <h2>4.Log4Net的使用</h2> <p style="text-align:center"><img src="https://simg.open-open.com/show/59bb4d1165d3ae0279e8007ca341a700.png"></p> <p>Log4Net的配置文件</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/af1c7d10af4cd426e6226492413ae278.png"></p> <p>记录日志:</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/c99b6e97ff905a1cac67b3f9ce2616a2.png"></p> <h2>5.支持配置IP地址</h2> <p style="text-align:center"><img src="https://simg.open-open.com/show/6e395a3bfde336331e92d832d13d399b.png"></p> <p> </p> <p> </p>