From c06defe53e0ffbdbc0ac5e95d66b80e44500feea Mon Sep 17 00:00:00 2001 From: Bengang Yuan Date: Wed, 18 Oct 2023 01:01:39 +0800 Subject: [PATCH] Check NRPE plugin before showing properties dialog. --- XenAdmin/Dialogs/PropertiesDialog.cs | 4 +-- XenAdmin/SettingsPanels/NRPEEditPage.cs | 25 ++++++------------- .../Actions/NRPE/NRPEHostConfiguration.cs | 10 +++----- XenModel/Actions/NRPE/NRPERetrieveAction.cs | 20 ++++++--------- XenModel/Actions/NRPE/NRPEUpdateAction.cs | 18 ++++++------- XenModel/Messages.Designer.cs | 9 ------- XenModel/Messages.resx | 5 +--- XenModel/Utils/Helpers.Versions.cs | 6 +++++ 8 files changed, 37 insertions(+), 60 deletions(-) diff --git a/XenAdmin/Dialogs/PropertiesDialog.cs b/XenAdmin/Dialogs/PropertiesDialog.cs index 1af7923bf..11a29d89d 100755 --- a/XenAdmin/Dialogs/PropertiesDialog.cs +++ b/XenAdmin/Dialogs/PropertiesDialog.cs @@ -318,8 +318,8 @@ private void Build() dialog.ShowDialog(Program.MainWindow); } } - if (isPoolOrStandalone && - (connection.Session.IsLocalSuperuser || connection.Session.Roles.Any(r => r.name_label == Role.MR_ROLE_POOL_ADMIN))) + if (isPoolOrStandalone && Helpers.XapiEqualOrGreater_23_27_0(connection) + && (connection.Session.IsLocalSuperuser || connection.Session.Roles.Any(r => r.name_label == Role.MR_ROLE_POOL_ADMIN))) { NRPEEditPage = new NRPEEditPage(); ShowTab(NRPEEditPage); diff --git a/XenAdmin/SettingsPanels/NRPEEditPage.cs b/XenAdmin/SettingsPanels/NRPEEditPage.cs index ffbde99d9..9bb719e6c 100644 --- a/XenAdmin/SettingsPanels/NRPEEditPage.cs +++ b/XenAdmin/SettingsPanels/NRPEEditPage.cs @@ -37,11 +37,15 @@ 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,})$"); @@ -107,13 +111,7 @@ public NRPEEditPage() }; } - public string SubText - { - get - { - return Messages.NRPE_EDIT_PAGE_TEXT; - } - } + public string SubText => Messages.NRPE_EDIT_PAGE_TEXT; public bool ValidToSave { @@ -149,11 +147,7 @@ public bool HasChanged get { UpdateCurrentNRPEConfiguration(); - if (!_nrpeCurrentConfig.Equals(_nrpeOriginalConfig)) - { - return true; - } - return false; + return !_nrpeCurrentConfig.Equals(_nrpeOriginalConfig); } } @@ -236,13 +230,8 @@ private void UpdateRetrievingNRPETip(NRPEHostConfiguration.RetrieveNRPEStatus st RetrieveNRPEPicture.Image = Images.StaticImages._000_error_h32bit_16; RetrieveNRPEPicture.Visible = true; break; - case NRPEHostConfiguration.RetrieveNRPEStatus.Unsupport: - RetrieveNRPELabel.Text = Messages.NRPE_UNSUPPORT; - RetrieveNRPEPicture.Image = Images.StaticImages._000_error_h32bit_16; - RetrieveNRPEPicture.Visible = true; - break; default: - break; + throw new ArgumentOutOfRangeException(nameof(status), status, null); } } diff --git a/XenModel/Actions/NRPE/NRPEHostConfiguration.cs b/XenModel/Actions/NRPE/NRPEHostConfiguration.cs index 8b3e7cf9e..31154b329 100644 --- a/XenModel/Actions/NRPE/NRPEHostConfiguration.cs +++ b/XenModel/Actions/NRPE/NRPEHostConfiguration.cs @@ -30,7 +30,6 @@ using System; using System.Collections.Generic; -using XenAPI; namespace XenAdmin.Actions.NRPE { @@ -54,7 +53,7 @@ public class NRPEHostConfiguration : ICloneable, IEquatable _checkDict = new Dictionary(); + private readonly Dictionary _checkDict = new Dictionary(); public bool EnableNRPE { get; set; } @@ -72,8 +71,7 @@ public enum RetrieveNRPEStatus { Retrieving, Successful, - Failed, - Unsupport + Failed } public class Check @@ -133,9 +131,9 @@ public object Clone() Debug = Debug, SslLogging = SslLogging }; - foreach (KeyValuePair kvp in _checkDict) + foreach (KeyValuePair kvp in _checkDict) { - NRPEHostConfiguration.Check CurrentCheck = kvp.Value; + Check CurrentCheck = kvp.Value; cloned.AddNRPECheck(new Check(CurrentCheck.Name, CurrentCheck.WarningThreshold, CurrentCheck.CriticalThreshold)); } diff --git a/XenModel/Actions/NRPE/NRPERetrieveAction.cs b/XenModel/Actions/NRPE/NRPERetrieveAction.cs index 495b3bc9e..77513db45 100644 --- a/XenModel/Actions/NRPE/NRPERetrieveAction.cs +++ b/XenModel/Actions/NRPE/NRPERetrieveAction.cs @@ -30,9 +30,6 @@ using System; using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Threading; using XenAdmin.Core; using XenAPI; @@ -69,8 +66,7 @@ protected override void Run() catch (Exception e) { log.ErrorFormat("Execute NRPE plugin failed, failed reason: {0}", e.Message); - _nrpeCurrentConfig.Status = e.Message.Contains("UNKNOWN_XENAPI_PLUGIN_FUNCTION") || e.Message.Contains("The requested plug-in could not be found") ? - NRPEHostConfiguration.RetrieveNRPEStatus.Unsupport : NRPEHostConfiguration.RetrieveNRPEStatus.Failed; + _nrpeCurrentConfig.Status = NRPEHostConfiguration.RetrieveNRPEStatus.Failed; } } @@ -128,20 +124,20 @@ private void InitNRPEThreshold(IXenObject o) } } - private string AllowHostsWithoutLocalAddress(string allowHosts) + private static string AllowHostsWithoutLocalAddress(string allowHosts) { - string UpdatedAllowHosts = ""; - string[] AllowHostArray = allowHosts.Split(','); - foreach (string allowHost in AllowHostArray) + string updatedAllowHosts = ""; + string[] allowHostArray = allowHosts.Split(','); + foreach (string allowHost in allowHostArray) { if (!allowHost.Trim().Equals("127.0.0.1") && !allowHost.Trim().Equals("::1")) { - UpdatedAllowHosts += allowHost + ","; + updatedAllowHosts += allowHost + ","; } } - return UpdatedAllowHosts.Length == 0 ? NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER : - UpdatedAllowHosts.Substring(0, UpdatedAllowHosts.Length - 1); + return updatedAllowHosts.Length == 0 ? NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER : + updatedAllowHosts.Substring(0, updatedAllowHosts.Length - 1); } } } diff --git a/XenModel/Actions/NRPE/NRPEUpdateAction.cs b/XenModel/Actions/NRPE/NRPEUpdateAction.cs index cb9060f18..47bdf9e87 100644 --- a/XenModel/Actions/NRPE/NRPEUpdateAction.cs +++ b/XenModel/Actions/NRPE/NRPEUpdateAction.cs @@ -87,13 +87,13 @@ private void SetNRPEConfigureForHost(IXenObject o) // NRPE Check Threshold foreach (KeyValuePair kvp in _nrpeHostConfiguration.CheckDict) { - NRPEHostConfiguration.Check CurrentCheck = kvp.Value; + 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))) + if (currentCheck != null && OriginalCheck != null + && (!currentCheck.WarningThreshold.Equals(OriginalCheck.WarningThreshold) + || !currentCheck.CriticalThreshold.Equals(OriginalCheck.CriticalThreshold))) { - SetNRPEThreshold(o, CurrentCheck.Name, CurrentCheck.WarningThreshold, CurrentCheck.CriticalThreshold); + SetNRPEThreshold(o, currentCheck.Name, currentCheck.WarningThreshold, currentCheck.CriticalThreshold); } } @@ -120,14 +120,14 @@ private void SetNRPEStatus(IXenObject host, bool enableNRPE) private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, bool debug, bool sslLogging) { - Dictionary ConfigArgDict = new Dictionary + Dictionary configArgDict = new Dictionary { { "allowed_hosts", "127.0.0.1,::1," + (allowHosts.Equals(NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER) ? "" : allowHosts) }, { "debug", debug ? NRPEHostConfiguration.DEBUG_ENABLE : NRPEHostConfiguration.DEBUG_DISABLE }, { "ssl_logging", sslLogging ? NRPEHostConfiguration.SSL_LOGGING_ENABLE : NRPEHostConfiguration.SSL_LOGGING_DISABLE} }; string result = Host.call_plugin(host.Connection.Session, host.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, - NRPEHostConfiguration.XAPI_NRPE_SET_CONFIG, ConfigArgDict); + NRPEHostConfiguration.XAPI_NRPE_SET_CONFIG, configArgDict); log.InfoFormat("Execute nrpe {0}, allowed_hosts={1}, debug={2}, ssl_logging={3}, return: {4}", NRPEHostConfiguration.XAPI_NRPE_SET_CONFIG, _nrpeHostConfiguration.AllowHosts, @@ -138,14 +138,14 @@ private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, boo private void SetNRPEThreshold(IXenObject host, string checkName, string warningThreshold, string criticalThreshold) { - Dictionary ThresholdArgDict = new Dictionary + Dictionary thresholdArgDict = new Dictionary { { checkName, null }, { "w", warningThreshold }, { "c", criticalThreshold } }; string result = Host.call_plugin(host.Connection.Session, host.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, - NRPEHostConfiguration.XAPI_NRPE_SET_THRESHOLD, ThresholdArgDict); + NRPEHostConfiguration.XAPI_NRPE_SET_THRESHOLD, thresholdArgDict); log.InfoFormat("Execute nrpe {0}, check={1}, w={2}, c={3}, return: {4}", NRPEHostConfiguration.XAPI_NRPE_SET_THRESHOLD, checkName, diff --git a/XenModel/Messages.Designer.cs b/XenModel/Messages.Designer.cs index 92325ebca..9409d987a 100755 --- a/XenModel/Messages.Designer.cs +++ b/XenModel/Messages.Designer.cs @@ -29357,15 +29357,6 @@ public static string NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL { } } - /// - /// Looks up a localized string similar to Unsupport NRPE, please upgrade your XenServer version.. - /// - public static string NRPE_UNSUPPORT { - get { - return ResourceManager.GetString("NRPE_UNSUPPORT", resourceCulture); - } - } - /// /// Looks up a localized string similar to Number of snapshots to keep. /// diff --git a/XenModel/Messages.resx b/XenModel/Messages.resx index 6e390e156..1df986623 100755 --- a/XenModel/Messages.resx +++ b/XenModel/Messages.resx @@ -10185,9 +10185,6 @@ When you configure an NFS storage repository, you simply provide the host name o Warning threshold should be less than critical threshold. - - Unsupport NRPE, please upgrade your XenServer version. - Number of snapshots to keep @@ -15180,4 +15177,4 @@ Do you want to synchronize immediately? Only synchronize &visible - + \ No newline at end of file diff --git a/XenModel/Utils/Helpers.Versions.cs b/XenModel/Utils/Helpers.Versions.cs index eccfe9652..a1740bede 100644 --- a/XenModel/Utils/Helpers.Versions.cs +++ b/XenModel/Utils/Helpers.Versions.cs @@ -528,6 +528,12 @@ public static bool XapiEqualOrGreater_23_18_0(IXenConnection conn) return coordinator == null || ProductVersionCompare(coordinator.GetXapiVersion(), "23.18.0") >= 0; } + public static bool XapiEqualOrGreater_23_27_0(IXenConnection conn) + { + var coordinator = GetCoordinator(conn); + return coordinator == null || ProductVersionCompare(coordinator.GetXapiVersion(), "23.27.0") >= 0; + } + #endregion } }