changeset 6:c1dffaac66fa

- Don't show multiple password dialogs for the same key if the first one was cancelled. - Add option to trim log files.
author Brad Greco <brad@bgreco.net>
date Fri, 01 Mar 2019 21:38:22 -0500
parents b6fe203af9d5
children 8486ab7d2357
files ServerMonitor/App.config ServerMonitor/Forms/InputDialog.cs ServerMonitor/Forms/ServerForm.Designer.cs ServerMonitor/Forms/ServerSummaryForm.cs ServerMonitor/Forms/SettingsForm.Designer.cs ServerMonitor/Forms/SettingsForm.cs ServerMonitor/Objects/Logger.cs ServerMonitor/Objects/Server.cs ServerMonitor/Objects/ServerMonitor.cs ServerMonitor/Properties/Settings.Designer.cs ServerMonitor/Properties/Settings.settings
diffstat 11 files changed, 115 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
             <setting name="MaxCheckId" serializeAs="String">
                 <value>0</value>
             </setting>
+            <setting name="KeepLogDays" serializeAs="String">
+                <value>30</value>
+            </setting>
         </ServerMonitorApp.Properties.Settings>
         <ServerMonitor.Properties.Settings>
             <setting name="ConfirmDeleteCheck" serializeAs="String">
--- 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()
         {
--- 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
             // 
--- 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<string> triedKeys = new List<string>();
             foreach (Server server in monitor.Servers)
             {
+                if (triedKeys.Contains(server.KeyFile))
+                    continue;
                 ServerForm.OpenPrivateKey(monitor, server, this);
+                triedKeys.Add(server.KeyFile);
             }
         }
 
--- 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
--- 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;
--- 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);
+        }
     }
 }
--- 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
--- 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<CheckResult> 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)
--- 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;
+            }
+        }
     }
 }
--- 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 @@
     <Setting Name="MaxCheckId" Type="System.Int32" Scope="User">
       <Value Profile="(Default)">0</Value>
     </Setting>
+    <Setting Name="KeepLogDays" Type="System.Int32" Scope="User">
+      <Value Profile="(Default)">30</Value>
+    </Setting>
   </Settings>
 </SettingsFile>
\ No newline at end of file