# HG changeset patch # User Brad Greco # Date 1551494302 18000 # Node ID c1dffaac66fac547ea1f71d514ebf2dafbc6857b # Parent b6fe203af9d5409d1d61a40bb73b2d7aa91e419c - Don't show multiple password dialogs for the same key if the first one was cancelled. - Add option to trim log files. diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/App.config --- a/ServerMonitor/App.config Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/App.config Fri Mar 01 21:38:22 2019 -0500 @@ -26,6 +26,9 @@ 0 + + 30 + diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Forms/InputDialog.cs --- a/ServerMonitor/Forms/InputDialog.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Forms/InputDialog.cs Fri Mar 01 21:38:22 2019 -0500 @@ -16,7 +16,7 @@ public Icon MessageIcon { get; set; } - public string Input { get; private set; } + public string Input { get; private set; } = ""; public InputDialog() { diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Forms/ServerForm.Designer.cs --- a/ServerMonitor/Forms/ServerForm.Designer.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Forms/ServerForm.Designer.cs Fri Mar 01 21:38:22 2019 -0500 @@ -184,7 +184,7 @@ this.UsernameTextBox.Location = new System.Drawing.Point(228, 56); this.UsernameTextBox.Name = "UsernameTextBox"; this.UsernameTextBox.Size = new System.Drawing.Size(187, 20); - this.UsernameTextBox.TabIndex = 12; + this.UsernameTextBox.TabIndex = 8; // // KeyTextBox // diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Forms/ServerSummaryForm.cs --- a/ServerMonitor/Forms/ServerSummaryForm.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Forms/ServerSummaryForm.cs Fri Mar 01 21:38:22 2019 -0500 @@ -112,9 +112,13 @@ private void CollectPrivateKeyPasswords() { + List triedKeys = new List(); foreach (Server server in monitor.Servers) { + if (triedKeys.Contains(server.KeyFile)) + continue; ServerForm.OpenPrivateKey(monitor, server, this); + triedKeys.Add(server.KeyFile); } } diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Forms/SettingsForm.Designer.cs --- a/ServerMonitor/Forms/SettingsForm.Designer.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Forms/SettingsForm.Designer.cs Fri Mar 01 21:38:22 2019 -0500 @@ -41,10 +41,14 @@ this.label1 = new System.Windows.Forms.Label(); this.OkButton = new System.Windows.Forms.Button(); this.CancelSettingsButton = new System.Windows.Forms.Button(); + this.KeepLogDaysInput = new System.Windows.Forms.NumericUpDown(); + this.DeleteLabel = new System.Windows.Forms.Label(); + this.DaysLabel = new System.Windows.Forms.Label(); this.SeverityOptionsGroupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.InformationIcon)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.WarningIcon)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.ErrorIcon)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.KeepLogDaysInput)).BeginInit(); this.SuspendLayout(); // // SeverityOptionsGroupBox @@ -60,7 +64,7 @@ this.SeverityOptionsGroupBox.Controls.Add(this.WarningIcon); this.SeverityOptionsGroupBox.Controls.Add(this.ErrorLabel); this.SeverityOptionsGroupBox.Controls.Add(this.ErrorIcon); - this.SeverityOptionsGroupBox.Location = new System.Drawing.Point(12, 57); + this.SeverityOptionsGroupBox.Location = new System.Drawing.Point(12, 61); this.SeverityOptionsGroupBox.Name = "SeverityOptionsGroupBox"; this.SeverityOptionsGroupBox.Size = new System.Drawing.Size(337, 104); this.SeverityOptionsGroupBox.TabIndex = 0; @@ -164,7 +168,7 @@ // OkButton // this.OkButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.OkButton.Location = new System.Drawing.Point(193, 170); + this.OkButton.Location = new System.Drawing.Point(193, 176); this.OkButton.Name = "OkButton"; this.OkButton.Size = new System.Drawing.Size(75, 23); this.OkButton.TabIndex = 2; @@ -175,7 +179,8 @@ // CancelSettingsButton // this.CancelSettingsButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.CancelSettingsButton.Location = new System.Drawing.Point(274, 170); + this.CancelSettingsButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.CancelSettingsButton.Location = new System.Drawing.Point(274, 176); this.CancelSettingsButton.Name = "CancelSettingsButton"; this.CancelSettingsButton.Size = new System.Drawing.Size(75, 23); this.CancelSettingsButton.TabIndex = 3; @@ -183,16 +188,52 @@ this.CancelSettingsButton.UseVisualStyleBackColor = true; this.CancelSettingsButton.Click += new System.EventHandler(this.CancelSettingsButton_Click); // + // KeepLogDaysInput + // + this.KeepLogDaysInput.Location = new System.Drawing.Point(130, 31); + this.KeepLogDaysInput.Maximum = new decimal(new int[] { + 9999, + 0, + 0, + 0}); + this.KeepLogDaysInput.Name = "KeepLogDaysInput"; + this.KeepLogDaysInput.Size = new System.Drawing.Size(50, 20); + this.KeepLogDaysInput.TabIndex = 4; + // + // DeleteLabel + // + this.DeleteLabel.AutoSize = true; + this.DeleteLabel.Location = new System.Drawing.Point(15, 33); + this.DeleteLabel.Name = "DeleteLabel"; + this.DeleteLabel.Size = new System.Drawing.Size(113, 13); + this.DeleteLabel.TabIndex = 5; + this.DeleteLabel.Text = "Delete log entries after"; + // + // DaysLabel + // + this.DaysLabel.AutoSize = true; + this.DaysLabel.Location = new System.Drawing.Point(182, 33); + this.DaysLabel.Name = "DaysLabel"; + this.DaysLabel.Size = new System.Drawing.Size(29, 13); + this.DaysLabel.TabIndex = 6; + this.DaysLabel.Text = "days"; + // // SettingsForm // this.AcceptButton = this.OkButton; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(361, 205); + this.CancelButton = this.CancelSettingsButton; + this.ClientSize = new System.Drawing.Size(361, 211); + this.Controls.Add(this.DaysLabel); + this.Controls.Add(this.DeleteLabel); + this.Controls.Add(this.KeepLogDaysInput); this.Controls.Add(this.CancelSettingsButton); this.Controls.Add(this.OkButton); this.Controls.Add(this.label1); this.Controls.Add(this.SeverityOptionsGroupBox); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.MaximizeBox = false; this.Name = "SettingsForm"; this.Text = "Options"; this.Load += new System.EventHandler(this.SettingsForm_Load); @@ -201,6 +242,7 @@ ((System.ComponentModel.ISupportInitialize)(this.InformationIcon)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.WarningIcon)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.ErrorIcon)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.KeepLogDaysInput)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -221,5 +263,8 @@ private System.Windows.Forms.ComboBox InformationComboBox; private System.Windows.Forms.Button OkButton; private System.Windows.Forms.Button CancelSettingsButton; + private System.Windows.Forms.NumericUpDown KeepLogDaysInput; + private System.Windows.Forms.Label DeleteLabel; + private System.Windows.Forms.Label DaysLabel; } } \ No newline at end of file diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Forms/SettingsForm.cs --- a/ServerMonitor/Forms/SettingsForm.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Forms/SettingsForm.cs Fri Mar 01 21:38:22 2019 -0500 @@ -26,6 +26,7 @@ comboBox.DataSource = Enum.GetValues(typeof(FailAction)); comboBox.Format += FailActionComboBox_Format; } + KeepLogDaysInput.Value = Settings.Default.KeepLogDays; ErrorComboBox.SelectedItem = Settings.Default.ErrorAction; WarningComboBox.SelectedItem = Settings.Default.WarningAction; InformationComboBox.SelectedItem = Settings.Default.InformationAction; @@ -38,6 +39,7 @@ private void OkButton_Click(object sender, EventArgs e) { + Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value; Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem; Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem; Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem; diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Objects/Logger.cs --- a/ServerMonitor/Objects/Logger.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Objects/Logger.cs Fri Mar 01 21:38:22 2019 -0500 @@ -1,4 +1,5 @@ -using System; +using ServerMonitorApp.Properties; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -59,5 +60,33 @@ results.Reverse(); return results; } + + public async void TrimLog() + { + DateTime maxAge = DateTime.Now.AddDays(-1 * Settings.Default.KeepLogDays); + string tempFile = logFile + ".tmp"; + using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) + using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None)) + using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8)) + { + while (true) + { + try + { + string line = reader.ReadLine(); + if (line == null) + break; + if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge) + { + await writer.WriteLineAsync(line); + } + } + catch (Exception) { } + } + } + File.Delete(logFile); + File.Move(tempFile, logFile); + } } } diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Objects/Server.cs --- a/ServerMonitor/Objects/Server.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Objects/Server.cs Fri Mar 01 21:38:22 2019 -0500 @@ -200,6 +200,11 @@ _sshClient?.Dispose(); _sshClient = null; } + + public override string ToString() + { + return Name.IsNullOrEmpty() ? Host : Name; + } } public enum KeyStatus diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Objects/ServerMonitor.cs --- a/ServerMonitor/Objects/ServerMonitor.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Objects/ServerMonitor.cs Fri Mar 01 21:38:22 2019 -0500 @@ -72,7 +72,7 @@ // that doesn't work when using the XML serializer for some reason. foreach (Server server in Servers) { - if (server.LoginType == LoginType.PrivateKey) + if (server.LoginType == LoginType.PrivateKey && server.PrivateKeyFile == null) OpenPrivateKey(server.KeyFile); foreach (Check check in server.Checks) { @@ -97,6 +97,7 @@ reader?.Close(); } NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; + logger.TrimLog(); Run(); } @@ -242,6 +243,8 @@ private void CancelCheck(Check check) { + if (tasks == null) + return; Task task = tasks.FirstOrDefault(kvp => kvp.Value == check.Id).Key; if (task != null) tasks.Remove(task); @@ -286,7 +289,7 @@ return KeyStatus.Open; try { - key = new PrivateKeyFile(path, password); + key = new PrivateKeyFile(path, password); keyStatus = KeyStatus.Open; } catch (Exception e) when (e is SshPassPhraseNullOrEmptyException || e is InvalidOperationException) diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Properties/Settings.Designer.cs --- a/ServerMonitor/Properties/Settings.Designer.cs Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Properties/Settings.Designer.cs Fri Mar 01 21:38:22 2019 -0500 @@ -82,5 +82,17 @@ this["MaxCheckId"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("30")] + public int KeepLogDays { + get { + return ((int)(this["KeepLogDays"])); + } + set { + this["KeepLogDays"] = value; + } + } } } diff -r b6fe203af9d5 -r c1dffaac66fa ServerMonitor/Properties/Settings.settings --- a/ServerMonitor/Properties/Settings.settings Thu Feb 28 21:19:32 2019 -0500 +++ b/ServerMonitor/Properties/Settings.settings Fri Mar 01 21:38:22 2019 -0500 @@ -17,5 +17,8 @@ 0 + + 30 + \ No newline at end of file