private static async Task Main(string[] args) { Trace.Listeners.Add(new TextWriterTraceListener(Console.Error)); var con = await ConnectionFactory.OpenConnectionAsync(TikConnectionType.ApiSsl_v2, "192.168.0.1", "read", ""); DefaultCollectorRegistry.Instance.RegisterOnDemandCollectors(new MikrotikCollector(con)); var server = new MetricServer(1234).Start(); Console.WriteLine($"Started at {DateTime.Now}"); var quitEvent = new ManualResetEvent(false); Console.CancelKeyPress += (sender, eventArgs) => { Console.WriteLine("Stopping..."); quitEvent.Set(); eventArgs.Cancel = true; }; quitEvent.WaitOne(); server.Stop(); }
static void Main(string[] args) { try { bool showHelp = false; OptionSet optionSet = new OptionSet { { "c|config=", "path to the yml configuration", c => configurationFile = Path.GetFullPath(c) }, { "v", "enable verbose output", v => { if (v != null) { Log.Main.Level = Log.LogLevel.Debug1; } } }, { "vv", "enable more verbose output", v => { if (v != null) { Log.Main.Level = Log.LogLevel.Debug2; } } }, { "h|help", "show this help", h => showHelp = h != null } }; optionSet.Parse(args); Console.WriteLine($"MikrotikExporter.Net {Assembly.GetExecutingAssembly().GetName().Version}"); if (showHelp) { optionSet.WriteOptionDescriptions(Console.Out); return; } if (string.IsNullOrWhiteSpace(configurationFile)) { Log.Main.Error("configuration file missing. use --help for more information."); return; } // inital configuration load if (!ConfigurationManager.Load(Log.Main.CreateContext("configuration load initial"))) { return; } if (!string.IsNullOrEmpty(Configuration.Global.ReloadUrl)) { backgroundTasks.Add(ReloadServer.Start(cts.Token)); } if (!string.IsNullOrEmpty(Configuration.Global.DiscoverUrl)) { backgroundTasks.Add(DiscoverServer.Start(cts.Token)); } // start the cleanup for stale connections ConnectionManager.InitCleanup(cts.Token); ConfigurationManager.InitReload(cts.Token); metricServer = MetricServer.Start(); var tcsRun = new TaskCompletionSource <object>(); Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { Log.Main.Info("stopping..."); tcsRun.SetResult(null); e.Cancel = true; }; tcsRun.Task.ContinueWith((action) => { cts.Cancel(); Log.Main.Info("stop metric server"); backgroundTasks.Add(metricServer.StopAsync()); return(Task.WhenAll(backgroundTasks)); }, TaskScheduler.Current).Wait(); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { Log.Main.Error($"unexpected error: {ex}"); } }