Skip to content

Commit

Permalink
Check NRPE plugin before showing properties dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
BengangY committed Oct 18, 2023
1 parent 6c81585 commit 96175e1
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 61 deletions.
23 changes: 20 additions & 3 deletions XenAdmin/Dialogs/PropertiesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
using XenAdmin.Wizards.NewVMApplianceWizard;
using XenAdmin.Wizards.GenericPages;
using System.Linq;
using System.Threading;

namespace XenAdmin.Dialogs
{
Expand Down Expand Up @@ -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
Expand Down
52 changes: 34 additions & 18 deletions XenAdmin/SettingsPanels/NRPEEditPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,})$");
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -149,11 +147,7 @@ public bool HasChanged
get
{
UpdateCurrentNRPEConfiguration();
if (!_nrpeCurrentConfig.Equals(_nrpeOriginalConfig))
{
return true;
}
return false;
return !_nrpeCurrentConfig.Equals(_nrpeOriginalConfig);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 4 additions & 6 deletions XenModel/Actions/NRPE/NRPEHostConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

using System;
using System.Collections.Generic;
using XenAPI;

namespace XenAdmin.Actions.NRPE
{
Expand All @@ -54,7 +53,7 @@ public class NRPEHostConfiguration : ICloneable, IEquatable<NRPEHostConfiguratio

public static readonly string ALLOW_HOSTS_PLACE_HOLDER = Messages.NRPE_ALLOW_HOSTS_PLACE_HOLDER;

private Dictionary<string, Check> _checkDict = new Dictionary<string, Check>();
private readonly Dictionary<string, Check> _checkDict = new Dictionary<string, Check>();

public bool EnableNRPE { get; set; }

Expand All @@ -72,8 +71,7 @@ public enum RetrieveNRPEStatus
{
Retrieving,
Successful,
Failed,
Unsupport
Failed
}

public class Check
Expand Down Expand Up @@ -133,9 +131,9 @@ public object Clone()
Debug = Debug,
SslLogging = SslLogging
};
foreach (KeyValuePair<string, NRPEHostConfiguration.Check> kvp in _checkDict)
foreach (KeyValuePair<string, Check> kvp in _checkDict)
{
NRPEHostConfiguration.Check CurrentCheck = kvp.Value;
Check CurrentCheck = kvp.Value;
cloned.AddNRPECheck(new Check(CurrentCheck.Name, CurrentCheck.WarningThreshold, CurrentCheck.CriticalThreshold));
}

Expand Down
20 changes: 8 additions & 12 deletions XenModel/Actions/NRPE/NRPERetrieveAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using XenAdmin.Core;
using XenAPI;

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
}
}
}
18 changes: 9 additions & 9 deletions XenModel/Actions/NRPE/NRPEUpdateAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ private void SetNRPEConfigureForHost(IXenObject o)
// NRPE Check Threshold
foreach (KeyValuePair<string, NRPEHostConfiguration.Check> 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);
}
}

Expand All @@ -120,14 +120,14 @@ private void SetNRPEStatus(IXenObject host, bool enableNRPE)

private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, bool debug, bool sslLogging)
{
Dictionary<string, string> ConfigArgDict = new Dictionary<string, string>
Dictionary<string, string> configArgDict = new Dictionary<string, string>
{
{ "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,
Expand All @@ -138,14 +138,14 @@ private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, boo

private void SetNRPEThreshold(IXenObject host, string checkName, string warningThreshold, string criticalThreshold)
{
Dictionary<string, string> ThresholdArgDict = new Dictionary<string, string>
Dictionary<string, string> thresholdArgDict = new Dictionary<string, string>
{
{ 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,
Expand Down
27 changes: 18 additions & 9 deletions XenModel/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions XenModel/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -10161,6 +10161,12 @@ When you configure an NFS storage repository, you simply provide the host name o
<data name="NRPE_INACTIVE" xml:space="preserve">
<value>NRPE service is inactive</value>
</data>
<data name="NRPE_PLUGIN_CHECKED" xml:space="preserve">
<value>Checking NRPE plugin status finished.</value>
</data>
<data name="NRPE_PLUGIN_CHECKING" xml:space="preserve">
<value>Checking NRPE plugin status.</value>
</data>
<data name="NRPE_RETRIEVE_FAILED" xml:space="preserve">
<value>Retrieve NRPE configuration failed, please check XenCenter logs.</value>
</data>
Expand All @@ -10185,9 +10191,6 @@ When you configure an NFS storage repository, you simply provide the host name o
<data name="NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL" xml:space="preserve">
<value>Warning threshold should be less than critical threshold.</value>
</data>
<data name="NRPE_UNSUPPORT" xml:space="preserve">
<value>Unsupport NRPE, please upgrade your XenServer version.</value>
</data>
<data name="NUMBER_OF_SNAPSHOTS_TO_KEEP" xml:space="preserve">
<value>Number of snapshots to keep</value>
</data>
Expand Down Expand Up @@ -15180,4 +15183,4 @@ Do you want to synchronize immediately?</value>
<data name="YUM_REPO_SYNC_YES_VISIBLE_BUTTON" xml:space="preserve">
<value>Only synchronize &amp;visible</value>
</data>
</root>
</root>

0 comments on commit 96175e1

Please sign in to comment.