From 2ad5fd8792197066e69503a741eee98c82dfc133 Mon Sep 17 00:00:00 2001 From: Bengang Yuan Date: Mon, 23 Oct 2023 17:05:03 +0800 Subject: [PATCH] Resolve NRPE code review comments from Tina. --- .../SettingsPanels/NRPEEditPage.CheckGroup.cs | 213 ++++++++++++++ .../SettingsPanels/NRPEEditPage.Designer.cs | 44 +-- XenAdmin/SettingsPanels/NRPEEditPage.cs | 42 ++- XenAdmin/XenAdmin.csproj | 8 +- XenModel/Actions/NRPE/CheckGroup.cs | 267 ------------------ XenModel/Actions/NRPE/NRPERetrieveAction.cs | 23 +- XenModel/Actions/NRPE/NRPEUpdateAction.cs | 29 +- XenModel/Messages.Designer.cs | 4 +- XenModel/Messages.resx | 4 +- XenModel/XenModel.csproj | 2 - 10 files changed, 293 insertions(+), 343 deletions(-) create mode 100644 XenAdmin/SettingsPanels/NRPEEditPage.CheckGroup.cs delete mode 100644 XenModel/Actions/NRPE/CheckGroup.cs diff --git a/XenAdmin/SettingsPanels/NRPEEditPage.CheckGroup.cs b/XenAdmin/SettingsPanels/NRPEEditPage.CheckGroup.cs new file mode 100644 index 000000000..268a008ed --- /dev/null +++ b/XenAdmin/SettingsPanels/NRPEEditPage.CheckGroup.cs @@ -0,0 +1,213 @@ +/* Copyright (c) Cloud Software Group, Inc. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +using System.Drawing; +using System.Windows.Forms; + +namespace XenAdmin.SettingsPanels +{ + public partial class NRPEEditPage + { + public class CheckGroup + { + private const decimal THRESHOLD_MINIMUM = 0.01M; + private const decimal THRESHOLD_MAXIMUM = 100M; + + public string Name { get; } + + public DataGridViewRow CheckThresholdRow { get; } + + public DataGridViewTextBoxCell NameCell { get; } + + public DataGridViewTextBoxCell WarningThresholdCell { get; } + + public DataGridViewTextBoxCell CriticalThresholdCell { get; } + + public CheckGroup(string name, string labelName) + { + Name = name; + NameCell = new DataGridViewTextBoxCell { Value = labelName }; + WarningThresholdCell = new DataGridViewTextBoxCell(); + CriticalThresholdCell = new DataGridViewTextBoxCell(); + CheckThresholdRow = new DataGridViewRow(); + CheckThresholdRow.Cells.AddRange(NameCell, WarningThresholdCell, CriticalThresholdCell); + CheckThresholdRow.DefaultCellStyle.Format = "N2"; + CheckThresholdRow.DefaultCellStyle.NullValue = 0; + WarningThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark); + CriticalThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark); + } + + public void UpdateThreshold(string warningThreshold, string criticalThreshold) + { + WarningThresholdCell.Value = warningThreshold; + CriticalThresholdCell.Value = criticalThreshold; + WarningThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlText); + CriticalThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlText); + } + + public virtual bool CheckValue() + { + WarningThresholdCell.ErrorText = ""; + CriticalThresholdCell.ErrorText = ""; + + return CheckEachValue(WarningThresholdCell) && + CheckEachValue(CriticalThresholdCell) && + CompareWarningAndCritical(); + } + + protected virtual bool CheckEachValue(DataGridViewTextBoxCell cell) + { + string thresholdStr = cell.Value.ToString().Trim(); + if (thresholdStr.Equals("")) + { + cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_NOT_BE_EMPTY); + return false; + } + + if (!decimal.TryParse(thresholdStr, out decimal threshold)) + { + cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_BE_NUMBER); + return false; + } + + if (threshold < THRESHOLD_MINIMUM || threshold > THRESHOLD_MAXIMUM) + { + cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_RANGE_ERROR, THRESHOLD_MINIMUM, + THRESHOLD_MAXIMUM); + return false; + } + + cell.ErrorText = ""; + return true; + } + + protected virtual bool CompareWarningAndCritical() + { + decimal.TryParse(WarningThresholdCell.Value.ToString().Trim(), out decimal warningDecimal); + decimal.TryParse(CriticalThresholdCell.Value.ToString().Trim(), out decimal criticalDecimal); + if (warningDecimal < criticalDecimal) + { + WarningThresholdCell.ErrorText = ""; + return true; + } + else + { + WarningThresholdCell.ErrorText = + string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL); + return false; + } + } + } + + public class FreeCheckGroup : CheckGroup + { + public FreeCheckGroup(string name, string labelName) + : base(name, labelName) + { + } + + protected override bool CompareWarningAndCritical() + { + decimal.TryParse(WarningThresholdCell.Value.ToString().Trim(), out decimal warningDecimal); + decimal.TryParse(CriticalThresholdCell.Value.ToString().Trim(), out decimal criticalDecimal); + if (warningDecimal > criticalDecimal) + { + WarningThresholdCell.ErrorText = ""; + return true; + } + else + { + WarningThresholdCell.ErrorText = + string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_BIGGER_THAN_CRITICAL); + return false; + } + } + + } + + public class HostLoadCheckGroup : CheckGroup + { + public HostLoadCheckGroup(string name, string labelName) + : base(name, labelName) + { + } + } + + public class Dom0LoadCheckGroup : CheckGroup + { + public Dom0LoadCheckGroup(string name, string labelName) + : base(name, labelName) + { + } + + protected override bool CompareWarningAndCritical() + { + string[] warningArray = WarningThresholdCell.Value.ToString().Split(','); + string[] criticalArray = CriticalThresholdCell.Value.ToString().Split(','); + for (int i = 0; i < 3; i++) + { + decimal.TryParse(warningArray[i].Trim(), out decimal warningDecimal); + decimal.TryParse(criticalArray[i].Trim(), out decimal criticalDecimal); + if (warningDecimal > criticalDecimal) + { + WarningThresholdCell.ErrorText = + string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL); + return false; + } + } + + WarningThresholdCell.ErrorText = ""; + return true; + } + + protected override bool CheckEachValue(DataGridViewTextBoxCell cell) + { + cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_BE_3_NUMBERS); + string[] loadArray = cell.Value.ToString().Split(','); + if (loadArray.Length != 3) + { + return false; + } + + foreach (string load in loadArray) + { + bool isDecimal = decimal.TryParse(load, out _); + if (!isDecimal) + { + return false; + } + } + + cell.ErrorText = ""; + return true; + } + } + } +} diff --git a/XenAdmin/SettingsPanels/NRPEEditPage.Designer.cs b/XenAdmin/SettingsPanels/NRPEEditPage.Designer.cs index a619e79ac..af65fa8f5 100644 --- a/XenAdmin/SettingsPanels/NRPEEditPage.Designer.cs +++ b/XenAdmin/SettingsPanels/NRPEEditPage.Designer.cs @@ -40,12 +40,12 @@ private void InitializeComponent() this.DebugLogCheckBox = new System.Windows.Forms.CheckBox(); this.SslDebugLogCheckBox = new System.Windows.Forms.CheckBox(); this.CheckDataGridView = new XenAdmin.Controls.DataGridViewEx.DataGridViewEx(); - this.RetrieveNRPEPanel = new System.Windows.Forms.TableLayoutPanel(); - this.RetrieveNRPELabel = new System.Windows.Forms.Label(); - this.RetrieveNRPEPicture = new System.Windows.Forms.PictureBox(); this.CheckColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.WarningThresholdColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.CriticalThresholdColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.RetrieveNRPEPanel = new System.Windows.Forms.TableLayoutPanel(); + this.RetrieveNRPELabel = new System.Windows.Forms.Label(); + this.RetrieveNRPEPicture = new System.Windows.Forms.PictureBox(); this.NRPETableLayoutPanel.SuspendLayout(); this.GeneralConfigureGroupBox.SuspendLayout(); this.GeneralConfigTableLayoutPanel.SuspendLayout(); @@ -136,24 +136,8 @@ private void InitializeComponent() this.CheckDataGridView.Name = "CheckDataGridView"; this.CheckDataGridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing; this.CheckDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect; - // - // RetrieveNRPEPanel - // - resources.ApplyResources(this.RetrieveNRPEPanel, "RetrieveNRPEPanel"); - this.RetrieveNRPEPanel.Controls.Add(this.RetrieveNRPELabel, 1, 0); - this.RetrieveNRPEPanel.Controls.Add(this.RetrieveNRPEPicture, 0, 0); - this.RetrieveNRPEPanel.Name = "RetrieveNRPEPanel"; - // - // RetrieveNRPELabel - // - resources.ApplyResources(this.RetrieveNRPELabel, "RetrieveNRPELabel"); - this.RetrieveNRPELabel.Name = "RetrieveNRPELabel"; - // - // RetrieveNRPEPicture - // - resources.ApplyResources(this.RetrieveNRPEPicture, "RetrieveNRPEPicture"); - this.RetrieveNRPEPicture.Name = "RetrieveNRPEPicture"; - this.RetrieveNRPEPicture.TabStop = false; + this.CheckDataGridView.ShowCellErrors = true; + this.CheckDataGridView.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.CheckDataGridView_EndEdit); // // CheckColumn // @@ -174,6 +158,24 @@ private void InitializeComponent() resources.ApplyResources(this.CriticalThresholdColumn, "CriticalThresholdColumn"); this.CriticalThresholdColumn.Name = "CriticalThresholdColumn"; // + // RetrieveNRPEPanel + // + resources.ApplyResources(this.RetrieveNRPEPanel, "RetrieveNRPEPanel"); + this.RetrieveNRPEPanel.Controls.Add(this.RetrieveNRPELabel, 1, 0); + this.RetrieveNRPEPanel.Controls.Add(this.RetrieveNRPEPicture, 0, 0); + this.RetrieveNRPEPanel.Name = "RetrieveNRPEPanel"; + // + // RetrieveNRPELabel + // + resources.ApplyResources(this.RetrieveNRPELabel, "RetrieveNRPELabel"); + this.RetrieveNRPELabel.Name = "RetrieveNRPELabel"; + // + // RetrieveNRPEPicture + // + resources.ApplyResources(this.RetrieveNRPEPicture, "RetrieveNRPEPicture"); + this.RetrieveNRPEPicture.Name = "RetrieveNRPEPicture"; + this.RetrieveNRPEPicture.TabStop = false; + // // NRPEEditPage // resources.ApplyResources(this, "$this"); diff --git a/XenAdmin/SettingsPanels/NRPEEditPage.cs b/XenAdmin/SettingsPanels/NRPEEditPage.cs index 9bb719e6c..758526fae 100644 --- a/XenAdmin/SettingsPanels/NRPEEditPage.cs +++ b/XenAdmin/SettingsPanels/NRPEEditPage.cs @@ -37,15 +37,11 @@ using XenAdmin.Core; using XenAdmin.Actions.NRPE; using XenAPI; -using System.Linq; -using XenAdmin.Network; namespace XenAdmin.SettingsPanels { public partial class NRPEEditPage : UserControl, IEditPage { - private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); - private static readonly Regex REGEX_IPV4 = new Regex("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)"); private static readonly Regex REGEX_IPV4_CIDR = new Regex("^([0-9]{1,3}\\.){3}[0-9]{1,3}(\\/([0-9]|[1-2][0-9]|3[0-2]))?$"); private static readonly Regex REGEX_DOMAIN = new Regex("^(((?!-))(xn--|_)?[a-z0-9-]{0,61}[a-z0-9]{1,1}\\.)*(xn--)?([a-z0-9][a-z0-9\\-]{0,60}|[a-z0-9-]{1,30}\\.[a-z]{2,})$"); @@ -119,8 +115,6 @@ public bool ValidToSave { _invalidParamToolTipText = ""; _invalidParamToolTip.ToolTipTitle = ""; - CheckDataGridView.ShowCellToolTips = false; - CheckDataGridView.ShowCellErrors = false; if (!EnableNRPECheckBox.Checked) { @@ -129,15 +123,23 @@ public bool ValidToSave bool valid = IsAllowHostsValid(); + DataGridViewTextBoxCell focusCellWhenErrorOccurs = null; foreach (CheckGroup checkGroup in _checkGroupList) { if (!checkGroup.CheckValue()) { - CheckDataGridView.ShowCellToolTips = true; - CheckDataGridView.ShowCellErrors = true; valid = false; + if (focusCellWhenErrorOccurs == null) + { + focusCellWhenErrorOccurs = checkGroup.NameCell; + } } } + if (focusCellWhenErrorOccurs != null) + { + CheckDataGridView.CurrentCell = CheckDataGridView.Rows[0].Cells[0]; + CheckDataGridView.CurrentCell = focusCellWhenErrorOccurs; + } return valid; } } @@ -190,7 +192,7 @@ public void SetXenObjects(IXenObject orig, IXenObject clone) UpdateRetrievingNRPETip(NRPEHostConfiguration.RetrieveNRPEStatus.Retrieving); DisableAllComponent(); - NRPERetrieveAction action = new NRPERetrieveAction(_clone, _nrpeOriginalConfig, _checkGroupDictByName, true); + NRPERetrieveAction action = new NRPERetrieveAction(_clone, _nrpeOriginalConfig, true); action.Completed += ActionCompleted; action.RunAsync(); } @@ -250,6 +252,12 @@ private void UpdateComponentValueBasedConfiguration() Color.FromKnownColor(KnownColor.ControlDark) : AllowHostsTextBox.ForeColor = Color.FromKnownColor(KnownColor.ControlText); DebugLogCheckBox.Checked = _nrpeOriginalConfig.Debug; SslDebugLogCheckBox.Checked = _nrpeOriginalConfig.SslLogging; + + foreach (KeyValuePair check in _nrpeOriginalConfig.CheckDict) + { + _checkGroupDictByName.TryGetValue(check.Key, out CheckGroup cg); + cg?.UpdateThreshold(check.Value.WarningThreshold, check.Value.CriticalThreshold); + } } private void UpdateComponentStatusBasedEnableNRPECheckBox() @@ -272,13 +280,14 @@ private void UpdateComponentStatusBasedEnableNRPECheckBox() checkGroup.CriticalThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark); } } + CheckDataGridView.ShowCellToolTips = EnableNRPECheckBox.Checked; + CheckDataGridView.ShowCellErrors = EnableNRPECheckBox.Checked; } private bool IsAllowHostsValid() { _invalidParamToolTip.ToolTipTitle = Messages.NRPE_ALLOW_HOSTS_ERROR_TITLE; _invalidParamToolTip.Tag = AllowHostsTextBox; - CheckDataGridView.ShowCellToolTips = true; string str = AllowHostsTextBox.Text; if (str.Trim().Length == 0 || str.Trim().Equals(NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER)) @@ -316,7 +325,6 @@ private bool IsAllowHostsValid() return false; } } - CheckDataGridView.ShowCellToolTips = false; return true; } @@ -341,14 +349,14 @@ private void UpdateCurrentNRPEConfiguration() _nrpeCurrentConfig = new NRPEHostConfiguration { EnableNRPE = EnableNRPECheckBox.Checked, - AllowHosts = AllowHostsTextBox.Text, + AllowHosts = AllowHostsTextBox.Text.Replace(" ", string.Empty), Debug = DebugLogCheckBox.Checked, SslLogging = SslDebugLogCheckBox.Checked }; foreach (KeyValuePair item in _checkGroupDictByName) { _nrpeCurrentConfig.AddNRPECheck(new NRPEHostConfiguration.Check(item.Key, - item.Value.WarningThresholdCell.Value.ToString(), item.Value.CriticalThresholdCell.Value.ToString())); + item.Value.WarningThresholdCell.Value.ToString().Trim(), item.Value.CriticalThresholdCell.Value.ToString().Trim())); } } @@ -374,5 +382,13 @@ private void AllowHostsTextBox_LostFocus(object sender, EventArgs e) AllowHostsTextBox.ForeColor = Color.FromKnownColor(KnownColor.ControlDark); } } + + private void CheckDataGridView_EndEdit(object sender, DataGridViewCellEventArgs e) + { + foreach (CheckGroup checkGroup in _checkGroupList) + { + checkGroup.CheckValue(); + } + } } } diff --git a/XenAdmin/XenAdmin.csproj b/XenAdmin/XenAdmin.csproj index 0e1b7504d..5f26b3a41 100755 --- a/XenAdmin/XenAdmin.csproj +++ b/XenAdmin/XenAdmin.csproj @@ -9,7 +9,7 @@ WinExe Properties XenAdmin - XenCenter + [XenCenter] ..\Branding\Images\AppIcon.ico v4.8 publish\ @@ -483,6 +483,10 @@ NRPEEditPage.cs + + NRPEEditPage.cs + UserControl + UserControl @@ -6808,4 +6812,4 @@ copy "$(ProjectDir)\ReportViewer\resource_report.rdlc" "$(TargetDir)" - + \ No newline at end of file diff --git a/XenModel/Actions/NRPE/CheckGroup.cs b/XenModel/Actions/NRPE/CheckGroup.cs deleted file mode 100644 index 3b23469d9..000000000 --- a/XenModel/Actions/NRPE/CheckGroup.cs +++ /dev/null @@ -1,267 +0,0 @@ -/* Copyright (c) Cloud Software Group, Inc. - * - * Redistribution and use in source and binary forms, - * with or without modification, are permitted provided - * that the following conditions are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -using System.Drawing; -using System.Windows.Forms; - -namespace XenAdmin.Actions.NRPE -{ - public class CheckGroup - { - private static readonly string DEFAULT_CHECK_WARNING_THRESHOLD = "80"; - private static readonly string DEFAULT_CHECK_CRITICAL_THRESHOLD = "90"; - - private readonly decimal THRESHOLD_MINIMUM = 0.01M; - private readonly decimal THRESHOLD_MAXIMUM = 100M; - - private string name; - private string warningThresholdDefault; - private string criticalThresholdDefault; - private bool changed; - - protected DataGridViewRow checkThresholdRow; - protected DataGridViewTextBoxCell nameCell; - protected DataGridViewTextBoxCell warningThresholdCell; - protected DataGridViewTextBoxCell criticalThresholdCell; - - public string Name { get => name; set => name = value; } - public string WarningThresholdDefault { get => warningThresholdDefault; set => warningThresholdDefault = value; } - public string CriticalThresholdDefault { get => criticalThresholdDefault; set => criticalThresholdDefault = value; } - public bool Changed { get => changed; set => changed = value; } - public DataGridViewRow CheckThresholdRow { get => checkThresholdRow; set => checkThresholdRow = value; } - public DataGridViewTextBoxCell NameCell { get => nameCell; set => nameCell = value; } - public DataGridViewTextBoxCell WarningThresholdCell { get => warningThresholdCell; set => warningThresholdCell = value; } - public DataGridViewTextBoxCell CriticalThresholdCell { get => criticalThresholdCell; set => criticalThresholdCell = value; } - - public CheckGroup(string name, string labelName, string warningThresholdDefaultValue, string criticalThresholdDefaultValue) - { - InitCheckGroup(name, labelName, warningThresholdDefaultValue, criticalThresholdDefaultValue); - } - - public CheckGroup(string name, string labelName) - { - InitCheckGroup(name, labelName, DEFAULT_CHECK_WARNING_THRESHOLD, DEFAULT_CHECK_CRITICAL_THRESHOLD); - } - - private void InitCheckGroup(string name, string labelName, string warningThresholdDefaultValue, string criticalThresholdDefaultValue) - { - Name = name; - nameCell = new DataGridViewTextBoxCell { Value = labelName }; - warningThresholdDefault = warningThresholdDefaultValue; - criticalThresholdDefault = criticalThresholdDefaultValue; - warningThresholdCell = new DataGridViewTextBoxCell { Value = warningThresholdDefaultValue }; - criticalThresholdCell = new DataGridViewTextBoxCell { Value = criticalThresholdDefaultValue }; - checkThresholdRow = new DataGridViewRow(); - checkThresholdRow.Cells.AddRange(nameCell, warningThresholdCell, criticalThresholdCell); - checkThresholdRow.DefaultCellStyle.Format = "N2"; - checkThresholdRow.DefaultCellStyle.NullValue = 0; - warningThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark); - criticalThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark); - } - - public void UpdateThreshold(string warningThreshold, string criticalThreshold) - { - warningThresholdCell.Value = warningThreshold; - criticalThresholdCell.Value = criticalThreshold; - warningThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlText); - criticalThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlText); - } - - public virtual bool CheckValue() - { - warningThresholdCell.ErrorText = ""; - criticalThresholdCell.ErrorText = ""; - - if (IsEmptyForPool()) - { - return true; - } - - if (CheckEachValue(warningThresholdCell) && - CheckEachValue(criticalThresholdCell) && - CompareWarningAndCritical() && - CheckModifyAllForPool()) - { - return true; - } - return false; - } - - private bool IsEmptyForPool() - { - return warningThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlDark)) - && criticalThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlDark)); - } - - protected virtual bool CheckEachValue(DataGridViewTextBoxCell cell) - { - string thresholdStr = cell.Value.ToString().Trim(); - if (thresholdStr.Equals("")) - { - cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_NOT_BE_EMPTY); - return false; - } - if (!decimal.TryParse(thresholdStr, out decimal threshold)) - { - cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_BE_NUMBER); - return false; - } - if (threshold < THRESHOLD_MINIMUM || threshold > THRESHOLD_MAXIMUM) - { - cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_RANGE_ERROR, THRESHOLD_MINIMUM, THRESHOLD_MAXIMUM); - return false; - } - cell.ErrorText = ""; - return true; - } - - protected virtual bool CompareWarningAndCritical() - { - decimal.TryParse(warningThresholdCell.Value.ToString().Trim(), out decimal warningDecimal); - decimal.TryParse(criticalThresholdCell.Value.ToString().Trim(), out decimal criticalDecimal); - if (warningDecimal < criticalDecimal) - { - warningThresholdCell.ErrorText = ""; - return true; - } - else - { - warningThresholdCell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL); - return false; - } - } - - private bool CheckModifyAllForPool() - { - if (warningThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlText)) - && criticalThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlDark))) - { - criticalThresholdCell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_NOT_BE_EMPTY); - return false; - } - else if (warningThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlDark)) - && criticalThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlText))) - { - warningThresholdCell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_NOT_BE_EMPTY); - return false; - } - return true; - } - } - - public class FreeCheckGroup : CheckGroup - { - private static readonly string DEFAULT_CHECK_WARNING_THRESHOLD = "20"; - private static readonly string DEFAULT_CHECK_CRITICAL_THRESHOLD = "10"; - - public FreeCheckGroup(string name, string labelName) - : base(name, labelName, DEFAULT_CHECK_WARNING_THRESHOLD, DEFAULT_CHECK_CRITICAL_THRESHOLD) - { - } - - protected override bool CompareWarningAndCritical() - { - decimal.TryParse(warningThresholdCell.Value.ToString().Trim(), out decimal warningDecimal); - decimal.TryParse(criticalThresholdCell.Value.ToString().Trim(), out decimal criticalDecimal); - if (warningDecimal > criticalDecimal) - { - warningThresholdCell.ErrorText = ""; - return true; - } - else - { - warningThresholdCell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_BIGGER_THAN_CRITICAL); - return false; - } - } - - } - - public class HostLoadCheckGroup : CheckGroup - { - private static readonly string DEFAULT_CHECK_WARNING_THRESHOLD = "3"; - private static readonly string DEFAULT_CHECK_CRITICAL_THRESHOLD = "4"; - - public HostLoadCheckGroup(string name, string labelName) - : base(name, labelName, DEFAULT_CHECK_WARNING_THRESHOLD, DEFAULT_CHECK_CRITICAL_THRESHOLD) - { - } - } - - public class Dom0LoadCheckGroup : CheckGroup - { - private static readonly string DEFAULT_CHECK_WARNING_THRESHOLD = "2.7,2.6,2.5"; - private static readonly string DEFAULT_CHECK_CRITICAL_THRESHOLD = "3.2,3.1,3"; - - public Dom0LoadCheckGroup(string name, string labelName) - : base(name, labelName, DEFAULT_CHECK_WARNING_THRESHOLD, DEFAULT_CHECK_CRITICAL_THRESHOLD) - { - } - - protected override bool CompareWarningAndCritical() - { - string[] warningArray = warningThresholdCell.Value.ToString().Split(','); - string[] criticalArray = criticalThresholdCell.Value.ToString().Split(','); - for (int i = 0; i < 3; i++) - { - decimal.TryParse(warningArray[i].Trim(), out decimal warningDecimal); - decimal.TryParse(criticalArray[i].Trim(), out decimal criticalDecimal); - if (warningDecimal > criticalDecimal) - { - warningThresholdCell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL); - return false; - } - } - warningThresholdCell.ErrorText = ""; - return true; - } - - protected override bool CheckEachValue(DataGridViewTextBoxCell cell) - { - checkThresholdRow.DataGridView.ShowCellToolTips = true; - cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_BE_3_NUMBERS); - string[] loadArray = cell.Value.ToString().Split(','); - if (loadArray.Length != 3) - { - return false; - } - foreach (string load in loadArray) - { - bool isDecimal = decimal.TryParse(load, out _); - if (!isDecimal) - { - return false; - } - } - cell.ErrorText = ""; - return true; - } - } -} diff --git a/XenModel/Actions/NRPE/NRPERetrieveAction.cs b/XenModel/Actions/NRPE/NRPERetrieveAction.cs index 77513db45..dc5ab62ed 100644 --- a/XenModel/Actions/NRPE/NRPERetrieveAction.cs +++ b/XenModel/Actions/NRPE/NRPERetrieveAction.cs @@ -29,7 +29,6 @@ */ using System; -using System.Collections.Generic; using XenAdmin.Core; using XenAPI; @@ -41,16 +40,14 @@ public class NRPERetrieveAction : AsyncAction private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private readonly NRPEHostConfiguration _nrpeCurrentConfig; - private readonly Dictionary _checkGroupDictByName; private readonly IXenObject _clone; - public NRPERetrieveAction(IXenObject host, NRPEHostConfiguration nrpeHostConfiguration, Dictionary checkGroupDictByName, bool suppressHistory) + public NRPERetrieveAction(IXenObject host, NRPEHostConfiguration nrpeHostConfiguration, bool suppressHistory) : base(host.Connection, Messages.NRPE_ACTION_RETRIEVING, Messages.NRPE_ACTION_RETRIEVING, suppressHistory) { _clone = host; _nrpeCurrentConfig = nrpeHostConfiguration; - _checkGroupDictByName = checkGroupDictByName; } protected override void Run() @@ -65,7 +62,7 @@ protected override void Run() } catch (Exception e) { - log.ErrorFormat("Execute NRPE plugin failed, failed reason: {0}", e.Message); + log.ErrorFormat("Run NRPE plugin failed, failed reason: {0}", e.Message); _nrpeCurrentConfig.Status = NRPEHostConfiguration.RetrieveNRPEStatus.Failed; } } @@ -74,12 +71,12 @@ private void InitNRPEGeneralConfiguration(IXenObject o) { string status = Host.call_plugin(o.Connection.Session, o.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, NRPEHostConfiguration.XAPI_NRPE_STATUS, null); - log.InfoFormat("Execute nrpe {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_STATUS, status); + log.InfoFormat("Run NRPE {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_STATUS, status); _nrpeCurrentConfig.EnableNRPE = status.Trim().Equals("active enabled"); string nrpeConfig = Host.call_plugin(o.Connection.Session, o.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, NRPEHostConfiguration.XAPI_NRPE_GET_CONFIG, null); - log.InfoFormat("Execute nrpe {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_GET_CONFIG, nrpeConfig); + log.InfoFormat("Run NRPE {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_GET_CONFIG, nrpeConfig); string[] nrpeConfigArray = nrpeConfig.Split('\n'); foreach (string nrpeConfigItem in nrpeConfigArray) @@ -106,21 +103,15 @@ private void InitNRPEThreshold(IXenObject o) { string nrpeThreshold = Host.call_plugin(o.Connection.Session, o.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, NRPEHostConfiguration.XAPI_NRPE_GET_THRESHOLD, null); - log.InfoFormat("Execute nrpe {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_GET_THRESHOLD, nrpeThreshold); + log.InfoFormat("Run NRPE {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_GET_THRESHOLD, nrpeThreshold); string[] nrpeThresholdArray = nrpeThreshold.Split('\n'); foreach (string nrpeThresholdItem in nrpeThresholdArray) { // Return string format for each line: check_cpu warning threshold - 50 critical threshold - 80 string[] thresholdRtnArray = nrpeThresholdItem.Split(' '); - string checkName = thresholdRtnArray[0]; - if (_checkGroupDictByName.TryGetValue(thresholdRtnArray[0], out CheckGroup thresholdCheck)) - { - string warningThreshold = thresholdRtnArray[4]; - string criticalThreshold = thresholdRtnArray[8]; - thresholdCheck.UpdateThreshold(warningThreshold, criticalThreshold); - _nrpeCurrentConfig.AddNRPECheck(new NRPEHostConfiguration.Check(checkName, warningThreshold, criticalThreshold)); - } + _nrpeCurrentConfig.AddNRPECheck(new NRPEHostConfiguration.Check(thresholdRtnArray[0], + thresholdRtnArray[4], thresholdRtnArray[8])); } } diff --git a/XenModel/Actions/NRPE/NRPEUpdateAction.cs b/XenModel/Actions/NRPE/NRPEUpdateAction.cs index 47bdf9e87..366d0a5b7 100644 --- a/XenModel/Actions/NRPE/NRPEUpdateAction.cs +++ b/XenModel/Actions/NRPE/NRPEUpdateAction.cs @@ -30,6 +30,7 @@ using System.Collections.Generic; using System.Linq; +using System.Threading; using XenAPI; @@ -60,7 +61,8 @@ protected override void Run() } else { - SetNRPEConfigureForPool(); + List hostList = ((Pool) _clone).Connection.Cache.Hosts.ToList(); + hostList.ForEach(SetNRPEConfigureForHost); } } @@ -88,10 +90,10 @@ private void SetNRPEConfigureForHost(IXenObject o) foreach (KeyValuePair kvp in _nrpeHostConfiguration.CheckDict) { NRPEHostConfiguration.Check currentCheck = kvp.Value; - _nrpeOriginalConfig.GetNRPECheck(kvp.Key, out NRPEHostConfiguration.Check OriginalCheck); - if (currentCheck != null && OriginalCheck != null - && (!currentCheck.WarningThreshold.Equals(OriginalCheck.WarningThreshold) - || !currentCheck.CriticalThreshold.Equals(OriginalCheck.CriticalThreshold))) + _nrpeOriginalConfig.GetNRPECheck(kvp.Key, out NRPEHostConfiguration.Check originalCheck); + if (currentCheck != null && originalCheck != null + && (!currentCheck.WarningThreshold.Equals(originalCheck.WarningThreshold) + || !currentCheck.CriticalThreshold.Equals(originalCheck.CriticalThreshold))) { SetNRPEThreshold(o, currentCheck.Name, currentCheck.WarningThreshold, currentCheck.CriticalThreshold); } @@ -100,22 +102,13 @@ private void SetNRPEConfigureForHost(IXenObject o) RestartNRPE(o); } - private void SetNRPEConfigureForPool() - { - List hostList = ((Pool) _clone).Connection.Cache.Hosts.ToList(); - hostList.ForEach(host => - { - SetNRPEConfigureForHost(host); - }); - } - private void SetNRPEStatus(IXenObject host, bool enableNRPE) { string nrpeUpdateStatusMethod = enableNRPE ? NRPEHostConfiguration.XAPI_NRPE_ENABLE : NRPEHostConfiguration.XAPI_NRPE_DISABLE; string result = Host.call_plugin(host.Connection.Session, host.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, nrpeUpdateStatusMethod, null); - log.InfoFormat("Execute nrpe {0}, return: {1}", nrpeUpdateStatusMethod, result); + log.InfoFormat("Run NRPE {0}, return: {1}", nrpeUpdateStatusMethod, result); } private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, bool debug, bool sslLogging) @@ -128,7 +121,7 @@ private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, boo }; string result = Host.call_plugin(host.Connection.Session, host.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, NRPEHostConfiguration.XAPI_NRPE_SET_CONFIG, configArgDict); - log.InfoFormat("Execute nrpe {0}, allowed_hosts={1}, debug={2}, ssl_logging={3}, return: {4}", + log.InfoFormat("Run NRPE {0}, allowed_hosts={1}, debug={2}, ssl_logging={3}, return: {4}", NRPEHostConfiguration.XAPI_NRPE_SET_CONFIG, _nrpeHostConfiguration.AllowHosts, _nrpeHostConfiguration.Debug, @@ -146,7 +139,7 @@ private void SetNRPEThreshold(IXenObject host, string checkName, string warningT }; string result = Host.call_plugin(host.Connection.Session, host.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, NRPEHostConfiguration.XAPI_NRPE_SET_THRESHOLD, thresholdArgDict); - log.InfoFormat("Execute nrpe {0}, check={1}, w={2}, c={3}, return: {4}", + log.InfoFormat("Run NRPE {0}, check={1}, w={2}, c={3}, return: {4}", NRPEHostConfiguration.XAPI_NRPE_SET_THRESHOLD, checkName, warningThreshold, @@ -159,7 +152,7 @@ private void RestartNRPE(IXenObject host) { string result = Host.call_plugin(host.Connection.Session, host.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, NRPEHostConfiguration.XAPI_NRPE_RESTART, null); - log.InfoFormat("Execute nrpe {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_RESTART, result); + log.InfoFormat("Run NRPE {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_RESTART, result); } } } diff --git a/XenModel/Messages.Designer.cs b/XenModel/Messages.Designer.cs index 9409d987a..d4cbd6173 100755 --- a/XenModel/Messages.Designer.cs +++ b/XenModel/Messages.Designer.cs @@ -29151,7 +29151,7 @@ public static string NRPE_ALLOW_HOSTS_PLACE_HOLDER { } /// - /// Looks up a localized string similar to Please remove duplicated address. + /// Looks up a localized string similar to Please remove duplicate addresses. /// public static string NRPE_ALLOW_HOSTS_SAME_ADDRESS { get { @@ -29286,7 +29286,7 @@ public static string NRPE_INACTIVE { } /// - /// Looks up a localized string similar to Retrieve NRPE configuration failed, please check XenCenter logs.. + /// Looks up a localized string similar to Failed to retrieve NRPE configuration, please check XenCenter logs.. /// public static string NRPE_RETRIEVE_FAILED { get { diff --git a/XenModel/Messages.resx b/XenModel/Messages.resx index 1df986623..54838ed18 100755 --- a/XenModel/Messages.resx +++ b/XenModel/Messages.resx @@ -10117,7 +10117,7 @@ When you configure an NFS storage repository, you simply provide the host name o Comma separated IP address or domain list, e.g. 192.168.1.1, test.domain.com - Please remove duplicated address + Please remove duplicate addresses NRPE batch configuration @@ -10162,7 +10162,7 @@ When you configure an NFS storage repository, you simply provide the host name o NRPE service is inactive - Retrieve NRPE configuration failed, please check XenCenter logs. + Failed to retrieve NRPE configuration, please check XenCenter logs. Retrieving NRPE configuration... diff --git a/XenModel/XenModel.csproj b/XenModel/XenModel.csproj index 117ab7806..1ff3f37a4 100755 --- a/XenModel/XenModel.csproj +++ b/XenModel/XenModel.csproj @@ -57,7 +57,6 @@ - @@ -89,7 +88,6 @@ -