From 96175e17a654958b04e7bfce21f4ef2c24160f6b 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 | 23 ++++++-- XenAdmin/SettingsPanels/NRPEEditPage.cs | 52 ++++++++++++------- .../Actions/NRPE/NRPEHostConfiguration.cs | 10 ++-- XenModel/Actions/NRPE/NRPERetrieveAction.cs | 20 +++---- XenModel/Actions/NRPE/NRPEUpdateAction.cs | 18 +++---- XenModel/Messages.Designer.cs | 27 ++++++---- XenModel/Messages.resx | 11 ++-- 7 files changed, 100 insertions(+), 61 deletions(-) diff --git a/XenAdmin/Dialogs/PropertiesDialog.cs b/XenAdmin/Dialogs/PropertiesDialog.cs index 1af7923bf..4127a0def 100755 --- a/XenAdmin/Dialogs/PropertiesDialog.cs +++ b/XenAdmin/Dialogs/PropertiesDialog.cs @@ -43,6 +43,7 @@ using XenAdmin.Wizards.NewVMApplianceWizard; using XenAdmin.Wizards.GenericPages; using System.Linq; +using System.Threading; namespace XenAdmin.Dialogs { @@ -318,11 +319,27 @@ 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 && NRPEEditPage.IsRoleSatisfied(connection) + && NRPEEditPage.IsCurrentVersionSupportNRPE(connection)) { NRPEEditPage = new NRPEEditPage(); - ShowTab(NRPEEditPage); + bool nrpeAvailable = false; + using (var dialog = new ActionProgressDialog( + new DelegatedAsyncAction(connection, Messages.NRPE_PLUGIN_CHECKING, + Messages.NRPE_PLUGIN_CHECKING, Messages.NRPE_PLUGIN_CHECKED, + delegate (Session session) + { + nrpeAvailable = NRPEEditPage.IsNRPEAvailable(_xenObjectCopy); + }), + ProgressBarStyle.Continuous)) + { + dialog.ShowCancel = false; + dialog.ShowDialog(Program.MainWindow); + } + if (nrpeAvailable) + { + ShowTab(NRPEEditPage); + } } } finally diff --git a/XenAdmin/SettingsPanels/NRPEEditPage.cs b/XenAdmin/SettingsPanels/NRPEEditPage.cs index ffbde99d9..3964b68d9 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); } } @@ -363,6 +352,33 @@ private void UpdateCurrentNRPEConfiguration() } } + public static bool IsRoleSatisfied(IXenConnection connection) + { + return connection.Session.IsLocalSuperuser || connection.Session.Roles.Any(r => r.name_label == Role.MR_ROLE_POOL_ADMIN); + } + + public static bool IsCurrentVersionSupportNRPE(IXenConnection connection) + { + string version = Helpers.HostProductVersion(Helpers.GetCoordinator(connection)); + return Helpers.ProductVersionCompare(version, "8.3") > 0; + } + + public bool IsNRPEAvailable(IXenObject clone) + { + IXenObject o = clone is Pool p ? Helpers.GetCoordinator(p) : clone; + try + { + Host.call_plugin(o.Connection.Session, o.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME, + NRPEHostConfiguration.XAPI_NRPE_STATUS, null); + return true; + } + catch (Exception e) + { + log.ErrorFormat("NRPE plugin checking failed, failed reason: {0}", e.Message); + return false; + } + } + private void EnableNRPECheckBox_CheckedChanged(object sender, EventArgs e) { UpdateComponentStatusBasedEnableNRPECheckBox(); 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..7db3c58e1 100755 --- a/XenModel/Messages.Designer.cs +++ b/XenModel/Messages.Designer.cs @@ -29285,6 +29285,24 @@ public static string NRPE_INACTIVE { } } + /// + /// Looks up a localized string similar to Checking NRPE plugin status finished.. + /// + public static string NRPE_PLUGIN_CHECKED { + get { + return ResourceManager.GetString("NRPE_PLUGIN_CHECKED", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Checking NRPE plugin status.. + /// + public static string NRPE_PLUGIN_CHECKING { + get { + return ResourceManager.GetString("NRPE_PLUGIN_CHECKING", resourceCulture); + } + } + /// /// Looks up a localized string similar to Retrieve NRPE configuration failed, please check XenCenter logs.. /// @@ -29357,15 +29375,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..d4a1bcebf 100755 --- a/XenModel/Messages.resx +++ b/XenModel/Messages.resx @@ -10161,6 +10161,12 @@ When you configure an NFS storage repository, you simply provide the host name o NRPE service is inactive + + Checking NRPE plugin status finished. + + + Checking NRPE plugin status. + Retrieve NRPE configuration failed, please check XenCenter logs. @@ -10185,9 +10191,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 +15183,4 @@ Do you want to synchronize immediately? Only synchronize &visible - + \ No newline at end of file