From e756d897825453d0ebe0ba93f1d675b8884068bf Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Thu, 26 Nov 2020 00:11:21 -0500 Subject: [PATCH] allow the feedback button to run collect-logs.ps1 via the monitor service --- DesktopEdge/MainMenu.xaml.cs | 36 +++++++++---------- DesktopEdge/MainWindow.xaml.cs | 19 +++++++--- DesktopEdge/ZitiDesktopEdge.log.config | 2 +- ZitiDesktopEdge.Client/Server/IPCServer.cs | 28 +++++++++++---- .../ServiceClient/MonitorClient.cs | 6 ++++ ZitiUpdateService/UpdateService.cs | 28 ++++++++++++--- ZitiUpdateService/ziti-monitor-log.config | 2 +- 7 files changed, 86 insertions(+), 35 deletions(-) diff --git a/DesktopEdge/MainMenu.xaml.cs b/DesktopEdge/MainMenu.xaml.cs index 90f0a1f5..8707e192 100644 --- a/DesktopEdge/MainMenu.xaml.cs +++ b/DesktopEdge/MainMenu.xaml.cs @@ -18,6 +18,8 @@ using NLog.Config; using NLog.Targets; using ZitiDesktopEdge.Server; +using System.Threading.Tasks; +using ZitiDesktopEdge.DataStructures; namespace ZitiDesktopEdge { @@ -223,7 +225,8 @@ namespace ZitiDesktopEdge private void ShowTerms(object sender, MouseButtonEventArgs e) { Process.Start(new ProcessStartInfo("https://netfoundry.io/terms") { UseShellExecute = true }); } - private void ShowFeedback(object sender, MouseButtonEventArgs e) { + + async private void ShowFeedback(object sender, MouseButtonEventArgs e) { DataClient client = (DataClient)Application.Current.Properties["ServiceClient"]; var mailMessage = new MailMessage(); mailMessage.From = new MailAddress("ziti-support@netfoundry.io"); @@ -232,18 +235,11 @@ namespace ZitiDesktopEdge mailMessage.Body = ""; string timestamp = DateTime.Now.ToFileTime().ToString(); - string serviceLogTempFile = Path.Combine(Path.GetTempPath(), timestamp+"-Ziti-Service.log"); - using (StreamWriter sw = new StreamWriter(serviceLogTempFile)) { - sw.WriteLine(client.GetLogs()); - } - - string uiLogTempFile = Path.Combine(Path.GetTempPath(), timestamp+"-Ziti-Application.log"); - using (StreamWriter sw = new StreamWriter(uiLogTempFile)) { - sw.WriteLine(UILog.GetLogs()); - } - - mailMessage.Attachments.Add(new Attachment(serviceLogTempFile)); - mailMessage.Attachments.Add(new Attachment(uiLogTempFile)); + var monitorClient = (MonitorClient)Application.Current.Properties["MonitorClient"]; + ServiceStatusEvent resp = await monitorClient.CaptureLogsAsync(); + string pathToLogs = resp.Message; + logger.Info("Log files found at : {0}", resp.Message); + mailMessage.Attachments.Add(new Attachment(pathToLogs)); string emlFile = Path.Combine(Path.GetTempPath(), timestamp+"-ziti.eml"); @@ -260,12 +256,14 @@ namespace ZitiDesktopEdge closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null); } - Process.Start(emlFile); - - - - //string body = HttpUtility.UrlEncode("\n\nService Logs\n\n" + client.GetLogs());// + "\n\nApplication Logs\n\n" + UILog.GetLogs()); - //Process.Start(new ProcessStartInfo("mailto:ziti-support@netfoundry.io?subject=Ziti%20Support&body="+body) { UseShellExecute = true }); + var p = Process.Start(emlFile); + Task.Run(async () => { + logger.Info("Waiting to send email..."); + p.WaitForExit(); + logger.Info("Email sent or cancelled... removing file: {0}", emlFile); + File.Delete(emlFile); + logger.Info("file removed: {0}", emlFile); + }); } private void ShowSupport(object sender, MouseButtonEventArgs e) { Process.Start(new ProcessStartInfo("https://openziti.discourse.group/") { UseShellExecute = true }); diff --git a/DesktopEdge/MainWindow.xaml.cs b/DesktopEdge/MainWindow.xaml.cs index 187ec66e..16eae515 100644 --- a/DesktopEdge/MainWindow.xaml.cs +++ b/DesktopEdge/MainWindow.xaml.cs @@ -187,12 +187,13 @@ namespace ZitiDesktopEdge { serviceClient.OnMetricsEvent += ServiceClient_OnMetricsEvent; serviceClient.OnServiceEvent += ServiceClient_OnServiceEvent; serviceClient.OnTunnelStatusEvent += ServiceClient_OnTunnelStatusEvent; + Application.Current.Properties.Add("ServiceClient", serviceClient); monitorClient = new MonitorClient(); monitorClient.OnClientConnected += MonitorClient_OnClientConnected; monitorClient.OnServiceStatusEvent += MonitorClient_OnServiceStatusEvent; + Application.Current.Properties.Add("MonitorClient", monitorClient); - Application.Current.Properties.Add("ServiceClient", serviceClient); Application.Current.Properties.Add("Identities", new List()); MainMenu.OnAttachmentChange += AttachmentChanged; MainMenu.OnLogLevelChanged += LogLevelChanged; @@ -567,11 +568,9 @@ namespace ZitiDesktopEdge { } private void Id_OnStatusChanged(bool attached) { - bool isActive = false; for (int i = 0; i < IdList.Children.Count; i++) { IdentityItem item = IdList.Children[i] as IdentityItem; if (item.ToggleSwitch.Enabled) { - isActive = true; break; } } @@ -817,5 +816,17 @@ namespace ZitiDesktopEdge { private void IdList_LayoutUpdated(object sender, EventArgs e) { Placement(); } - } + + async private void CollectLogFileClick(object sender, RoutedEventArgs e) { + await CollectLogFiles(); + } + async private Task CollectLogFiles() { + ServiceStatusEvent resp = await monitorClient.CaptureLogsAsync(); + logger.Info("response: {0}", resp.Message); + } + + private void Button_Click(object sender, RoutedEventArgs e) { + + } + } } \ No newline at end of file diff --git a/DesktopEdge/ZitiDesktopEdge.log.config b/DesktopEdge/ZitiDesktopEdge.log.config index d5cd0d34..8b7d14da 100644 --- a/DesktopEdge/ZitiDesktopEdge.log.config +++ b/DesktopEdge/ZitiDesktopEdge.log.config @@ -5,7 +5,7 @@ (new JsonTextReader(new StringReader(msg))); + ActionEvent ae = serializer.Deserialize(new JsonTextReader(new StringReader(json))); Logger.Info("Op: {0}", ae.Op); switch (ae.Op.ToLower()) { case "stop": @@ -195,15 +198,28 @@ namespace ZitiDesktopEdge.Server { rr.Status = ServiceActions.ServiceStatus(); r = rr; break; + case "capturelogs": + try { + string results = CaptureLogs(); + r.Message = results; + } catch(Exception ex) { + string err = string.Format("UNKNOWN ERROR : {0}", ex.Message); + Logger.Error(ex, err); + r.Code = -5; + r.Message = "FAILURE"; + r.Error = err; + } + break; default: - msg = string.Format("UNKNOWN ACTION received: {0}", ae.Op); - Logger.Error(msg); + r.Message = "FAILURE"; r.Code = -3; - r.Error = msg; + r.Error = string.Format("UNKNOWN ACTION received: {0}", ae.Op); + Logger.Error(r.Message); break; } } catch (Exception e) { Logger.Error(e, "Unexpected erorr in processMessage!"); + r.Message = "FAILURE"; r.Code = -2; r.Error = e.Message + ":" + e?.InnerException?.Message; } diff --git a/ZitiDesktopEdge.Client/ServiceClient/MonitorClient.cs b/ZitiDesktopEdge.Client/ServiceClient/MonitorClient.cs index 75cd4a7c..60bc7f98 100644 --- a/ZitiDesktopEdge.Client/ServiceClient/MonitorClient.cs +++ b/ZitiDesktopEdge.Client/ServiceClient/MonitorClient.cs @@ -91,5 +91,11 @@ namespace ZitiDesktopEdge.ServiceClient { await sendAsync(action); return await readAsync(ipcReader, "Status"); } + async public Task CaptureLogsAsync() { + ActionEvent action = new ActionEvent() { Action = "Normal", Op = "captureLogs" }; + //string result = await SendServiceFunctionAsync(action); + await sendAsync(action); + return await readAsync(ipcReader, "CaptureLogsAsync"); + } } } \ No newline at end of file diff --git a/ZitiUpdateService/UpdateService.cs b/ZitiUpdateService/UpdateService.cs index f41c09a4..0bb97c73 100644 --- a/ZitiUpdateService/UpdateService.cs +++ b/ZitiUpdateService/UpdateService.cs @@ -27,6 +27,7 @@ namespace ZitiUpdateService { private bool inUpdateCheck = false; private string _versionType = "latest"; + private string exeLocation = null; private DataClient svc = new DataClient(); private bool running = false; @@ -36,22 +37,42 @@ namespace ZitiUpdateService { Version assemblyVersion = null; ServiceController controller; - ZitiDesktopEdge.Server.IPCServer svr = new ZitiDesktopEdge.Server.IPCServer(); + IPCServer svr = new IPCServer(); Task ipcServer = null; Task eventServer = null; IUpdateCheck check = null; public UpdateService() { InitializeComponent(); + exeLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); Logger.Info("Initializing"); svc.OnClientConnected += Svc_OnClientConnected; svc.OnTunnelStatusEvent += Svc_OnTunnelStatusEvent; svc.OnClientDisconnected += Svc_OnClientDisconnected; svc.OnShutdownEvent += Svc_OnShutdownEvent; + + svr.CaptureLogs = GetLogs; } - public void Debug() { + private string GetLogs() { + Logger.Info("Request to collect logs received"); + var ps = System.Management.Automation.PowerShell.Create(); + string script = Path.Combine(exeLocation, "collect-logs.ps1"); + ps.Commands.AddScript(script); + var results = ps.Invoke(); + Logger.Info("Collected logs."); + for(int i=0; i