Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            var config = PingEngineConfig.FromArgs(args);

            if (config == null)
            {
                return;
            }

            config.MessageLogger = (hostInfo, pingState, status) =>
            {
                if (pingState == PingState.Started)
                {
                    Console.Write($"Pinging {hostInfo.Name}");
                    return;
                }

                Console.WriteLine($" ({hostInfo.IPAddress?.ToString() ?? "?"})\t{status}");

                try
                {
                    File.AppendAllText(config.LogFile, ($"{DateTime.Now:s}\t{hostInfo.Name} ({hostInfo.IPAddress})\t{status}\r\n"));
                }
                catch (Exception ex)
                {
                    //go on with life...
                    Console.WriteLine($"Error writing to log file: {ex.Message}");
                }
            };

            var pingEngine = new PingEngine(config);

            pingEngine.StartPinging();

            Console.ReadKey(true);

            pingEngine.StopPinging();
        }
 public PingEngine(PingEngineConfig config)
 {
     Config = config;
 }
        public static PingEngineConfig FromArgs(string[] args)
        {
            var pingSites = new RoundRobinList <HostInfo>();
            var logFile   = "";
            var interval  = 10000;
            var showHelp  = false;

            var options = new OptionSet
            {
                {
                    "l|logfile=", "the path of the logfile", l =>
                    {
                        if (!string.IsNullOrWhiteSpace(l))
                        {
                            logFile = l;
                        }
                    }
                },
                {
                    "s|server=",
                    "a server to use in ping list (can be used more than once, if none are supplied it uses a default list).", s =>
                    {
                        if (!string.IsNullOrWhiteSpace(s))
                        {
                            pingSites.Add(new HostInfo(s));
                        }
                    }
                },
                {
                    "i|interval=", $"delay (in seconds) between ping attempts (default is {interval / 1000} seconds)", i =>
                    {
                        if (!string.IsNullOrWhiteSpace(i))
                        {
                            interval = int.Parse(i) * 1000;
                        }
                    }
                },
                { "?|h|help", "show this message and exit", h => showHelp = h != null },
            };

            try
            {
                // parse the command line
                var extra = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("UptimeTracker: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try 'UptimeTracker --help' for more information.");
                return(null);
            }

            if (showHelp)
            {
                Console.WriteLine("Usage: UptimeTracker.exe [OPTIONS]+");
                Console.WriteLine("Periodically pings hosts in a round-robin fashion to track network connectivity.");
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return(null);
            }

            if (string.IsNullOrWhiteSpace(logFile))
            {
                logFile = Path.Combine(Path.GetTempPath(), defaultLogFileName);
            }

            if (!pingSites.Any())
            {
                pingSites.AddRange(defaultPingSites);
            }

            var config = new PingEngineConfig()
            {
                PingSites = pingSites,
                Interval  = interval,
                LogFile   = logFile
            };

            return(config);
        }