Skip to content

Commit

Permalink
CP-44372:
Browse files Browse the repository at this point in the history
1 Refactor code (add data binding, modify icon, modify text, etc)
2 Modify test issues.
  • Loading branch information
BengangY committed Sep 21, 2023
1 parent 2967617 commit 6ac41a2
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 82 deletions.
1 change: 1 addition & 0 deletions XenAdmin/Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ public static class StaticImages
public static Bitmap _000_NewStorage_h32bit_32 = Properties.Resources._000_NewStorage_h32bit_32;
public static Bitmap _000_NewVirtualAppliance_h32bit_16 = Properties.Resources._000_NewVirtualAppliance_h32bit_16;
public static Bitmap _000_NewVirtualAppliance_h32bit_32 = Properties.Resources._000_NewVirtualAppliance_h32bit_32;
public static Bitmap _000_NRPE_h32bit_16 = Properties.Resources._000_NRPE_h32bit_16;
public static Bitmap _000_Optimize_h32bit_16 = Properties.Resources._000_Optimize_h32bit_16;
public static Bitmap _000_Patch_h32bit_16 = Properties.Resources._000_Patch_h32bit_16;
public static Bitmap _000_Patch_h32bit_32 = Properties.Resources._000_Patch_h32bit_32;
Expand Down
Binary file added XenAdmin/Images/000_NRPE_h32bit_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions XenAdmin/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions XenAdmin/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1159,4 +1159,7 @@
<data name="rpm_package" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Images\rpm_package.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="_000_NRPE_h32bit_16" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Images\000_NRPE_h32bit_16.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
1 change: 0 additions & 1 deletion XenAdmin/SettingsPanels/NRPEEditPage.CheckGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
* SUCH DAMAGE.
*/

using System;
using System.Drawing;
using System.Windows.Forms;

Expand Down
112 changes: 59 additions & 53 deletions XenAdmin/SettingsPanels/NRPEEditPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public partial class NRPEEditPage : UserControl, IEditPage
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

private static readonly string ALLOW_HOSTS_PLACE_HOLDER = Messages.NRPE_ALLOW_HOSTS_PLACE_HOLDER;

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]))?$");
Expand All @@ -59,43 +58,44 @@ public partial class NRPEEditPage : UserControl, IEditPage
private readonly Dictionary<string, CheckGroup> CheckGroupDictByName = new Dictionary<string, CheckGroup>();
private readonly Dictionary<string, CheckGroup> CheckGroupDictByLabel = new Dictionary<string, CheckGroup>();

private readonly NRPEHostConfiguration NRPEOriginalConfig = new NRPEHostConfiguration();

private NRPEHostConfiguration NRPEOriginalConfig;
private NRPEHostConfiguration NRPECurrentConfig;
public string SubText
{
get
{
return Messages.NRPE_EDIT_PAGE_TEXT;
if (IsHost)
{
return EnableNRPECheckBox.Checked ? Messages.NRPE_ACTIVE : Messages.NRPE_INACTIVE;
}
else
{
return Messages.NRPE_BATCH_CONFIGURATION;
}
}
}

public Image Image => Images.StaticImages._000_Network_h32bit_16;
public Image Image => Images.StaticImages._000_NRPE_h32bit_16;

public NRPEEditPage(bool isHost)
{
IsHost = isHost;
InitializeComponent();
Text = Messages.NRPE_EDIT_PAGE_TEXT;
EnableNRPECheckBox.Checked = false;
UpdateOtherComponentBasedEnableNRPECheckBox(false);
Text = "NRPE";

AllowHostsTextBox.Text = ALLOW_HOSTS_PLACE_HOLDER;
AllowHostsTextBox.ForeColor = Color.FromKnownColor(KnownColor.ControlDark);
AllowHostsTextBox.GotFocus += AllowHostsTextBox_GotFocus;
AllowHostsTextBox.LostFocus += AllowHostsTextBox_LostFocus;

if (isHost)
{
PoolTipsPicture.Hide();
PoolTipsLabel.Hide();
}
InvalidParamToolTip = new ToolTip
NRPECurrentConfig = new NRPEHostConfiguration
{
IsBalloon = true,
ToolTipIcon = ToolTipIcon.Warning,
ToolTipTitle = Messages.INVALID_PARAMETER,
Tag = AllowHostsTextBox
EnableNRPE = false,
AllowHosts = NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER,
Debug = false,
SslLogging = false
};
NRPEOriginalConfig = (NRPEHostConfiguration) NRPECurrentConfig.Clone();

EnableNRPECheckBox.DataBindings.Add("Checked", NRPECurrentConfig, "enableNRPE");
AllowHostsTextBox.DataBindings.Add("Text", NRPECurrentConfig, "allowHosts");
DebugLogCheckBox.DataBindings.Add("Checked", NRPECurrentConfig, "debug");
SslDebugLogCheckBox.DataBindings.Add("Checked", NRPECurrentConfig, "sslLogging");

CheckGroupList.Add(new HostLoadCheckGroup("check_host_load", Messages.NRPE_CHECK_HOST_LOAD));
CheckGroupList.Add(new CheckGroup("check_host_cpu", Messages.NRPE_CHECK_HOST_CPU));
Expand All @@ -115,6 +115,25 @@ public NRPEEditPage(bool isHost)
CheckGroupDictByName.Add(checkGroup.Name, checkGroup);
CheckGroupDictByLabel.Add(checkGroup.NameCell.Value.ToString(), checkGroup);
}

UpdateOtherComponentBasedEnableNRPECheckBox(false);

AllowHostsTextBox.ForeColor = Color.FromKnownColor(KnownColor.ControlDark);
AllowHostsTextBox.GotFocus += AllowHostsTextBox_GotFocus;
AllowHostsTextBox.LostFocus += AllowHostsTextBox_LostFocus;

if (isHost)
{
PoolTipsPicture.Hide();
PoolTipsLabel.Hide();
}
InvalidParamToolTip = new ToolTip
{
IsBalloon = true,
ToolTipIcon = ToolTipIcon.Warning,
ToolTipTitle = Messages.INVALID_PARAMETER,
Tag = AllowHostsTextBox
};
}

public bool ValidToSave
Expand Down Expand Up @@ -191,36 +210,26 @@ public void SetXenObjects(IXenObject orig, IXenObject clone)

public AsyncAction SaveSettings()
{
NRPEHostConfiguration NRPEHostConfiguration = new NRPEHostConfiguration
{
EnableNRPE = EnableNRPECheckBox.Checked,
AllowHosts = AllowHostsTextBox.Text,
Debug = DebugLogCheckBox.Checked ? NRPEHostConfiguration.DEBUG_ENABLE : NRPEHostConfiguration.DEBUG_DISABLE,
SslLogging = SslDebugLogCheckBox.Checked ? NRPEHostConfiguration.SSL_LOGGING_ENABLE : NRPEHostConfiguration.SSL_LOGGING_DISABLE
};

foreach (KeyValuePair<string, CheckGroup> item in CheckGroupDictByName)
{
if (item.Value.WarningThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlText))
&& item.Value.CriticalThresholdCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlText)))
{
NRPEHostConfiguration.AddNRPECheck(new NRPEHostConfiguration.Check(item.Key,
NRPECurrentConfig.AddNRPECheck(new NRPEHostConfiguration.Check(item.Key,
item.Value.WarningThresholdCell.Value.ToString(), item.Value.CriticalThresholdCell.Value.ToString()));
}
}
return new NRPEUpdateAction(Clone, NRPEHostConfiguration, NRPEOriginalConfig, false);
return new NRPEUpdateAction(Clone, NRPECurrentConfig, NRPEOriginalConfig, false);
}

private void InitNRPEGeneralConfiguration()
{
string status = Host.call_plugin(Clone.Connection.Session, Clone.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME,
string status = Host.call_plugin(Clone.Connection.Session, Clone.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME,
NRPEHostConfiguration.XAPI_NRPE_STATUS, null);
log.InfoFormat("Execute nrpe {0}, return: {1}", NRPEHostConfiguration.XAPI_NRPE_STATUS, status);
EnableNRPECheckBox.Checked = status.Trim().Equals("active enabled");
NRPEOriginalConfig.EnableNRPE = EnableNRPECheckBox.Checked;
UpdateOtherComponentBasedEnableNRPECheckBox(EnableNRPECheckBox.Checked);
NRPECurrentConfig.EnableNRPE = status.Trim().Equals("active enabled");

string nrpeConfig = Host.call_plugin(Clone.Connection.Session, Clone.opaque_ref, NRPEHostConfiguration.XAPI_NRPE_PLUGIN_NAME,
string nrpeConfig = Host.call_plugin(Clone.Connection.Session, Clone.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);

Expand All @@ -230,27 +239,24 @@ private void InitNRPEGeneralConfiguration()
if (nrpeConfigItem.Trim().StartsWith("allowed_hosts:"))
{
string allowHosts = nrpeConfigItem.Replace("allowed_hosts:", "").Trim();
if (!allowHosts.Equals(""))
{
AllowHostsTextBox.Text = AllowHostsWithoutLocalAddress(allowHosts);
AllowHostsTextBox.ForeColor = AllowHostsTextBox.Text.Equals(ALLOW_HOSTS_PLACE_HOLDER) ?
Color.FromKnownColor(KnownColor.ControlDark) : Color.FromKnownColor(KnownColor.ControlText);
NRPEOriginalConfig.AllowHosts = AllowHostsTextBox.Text;
}
NRPECurrentConfig.AllowHosts = AllowHostsWithoutLocalAddress(allowHosts);
AllowHostsTextBox.ForeColor = AllowHostsTextBox.Text.Equals(NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER) ?
Color.FromKnownColor(KnownColor.ControlDark) : Color.FromKnownColor(KnownColor.ControlText);
}
else if (nrpeConfigItem.Trim().StartsWith("debug:"))
{
string enableDebug = nrpeConfigItem.Replace("debug:", "").Trim();
DebugLogCheckBox.Checked = enableDebug.Equals(NRPEHostConfiguration.DEBUG_ENABLE);
NRPEOriginalConfig.Debug = DebugLogCheckBox.Checked ? NRPEHostConfiguration.DEBUG_ENABLE : NRPEHostConfiguration.DEBUG_DISABLE;
NRPECurrentConfig.Debug = enableDebug.Equals(NRPEHostConfiguration.DEBUG_ENABLE);
}
else if (nrpeConfigItem.Trim().StartsWith("ssl_logging:"))
{
string enableSslLogging = nrpeConfigItem.Replace("ssl_logging:", "").Trim();
SslDebugLogCheckBox.Checked = enableSslLogging.Equals(NRPEHostConfiguration.SSL_LOGGING_ENABLE);
NRPEOriginalConfig.SslLogging = SslDebugLogCheckBox.Checked ? NRPEHostConfiguration.SSL_LOGGING_ENABLE : NRPEHostConfiguration.SSL_LOGGING_DISABLE;
NRPECurrentConfig.SslLogging = enableSslLogging.Equals(NRPEHostConfiguration.SSL_LOGGING_ENABLE);
}
}
NRPEOriginalConfig = (NRPEHostConfiguration) NRPECurrentConfig.Clone();

UpdateOtherComponentBasedEnableNRPECheckBox(EnableNRPECheckBox.Checked);
}

private void InitNRPEThreshold()
Expand Down Expand Up @@ -282,7 +288,7 @@ private bool IsAllowHostsValid()
CheckDataGridView.ShowCellToolTips = true;

string str = AllowHostsTextBox.Text;
if (str.Trim().Length == 0 || str.Trim().Equals(ALLOW_HOSTS_PLACE_HOLDER))
if (str.Trim().Length == 0 || str.Trim().Equals(NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER))
{
InvalidParamToolTipText = Messages.NRPE_ALLOW_HOSTS_EMPTY_ERROR;
return false;
Expand Down Expand Up @@ -315,7 +321,7 @@ private string AllowHostsWithoutLocalAddress(string allowHosts)
UpdatedAllowHosts += allowHost + ",";
}
}
return UpdatedAllowHosts.Length == 0 ? ALLOW_HOSTS_PLACE_HOLDER :
return UpdatedAllowHosts.Length == 0 ? NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER :
UpdatedAllowHosts.Substring(0, UpdatedAllowHosts.Length - 1);
}

Expand Down Expand Up @@ -350,7 +356,7 @@ private void EnableNRPECheckBox_CheckedChanged(object sender, EventArgs e)

private void AllowHostsTextBox_GotFocus(object sender, EventArgs e)
{
if (ALLOW_HOSTS_PLACE_HOLDER.Equals(AllowHostsTextBox.Text))
if (AllowHostsTextBox.Text.Equals(NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER))
{
AllowHostsTextBox.Text = "";
AllowHostsTextBox.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
Expand All @@ -361,7 +367,7 @@ private void AllowHostsTextBox_LostFocus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(AllowHostsTextBox.Text))
{
AllowHostsTextBox.Text = ALLOW_HOSTS_PLACE_HOLDER;
AllowHostsTextBox.Text = NRPEHostConfiguration.ALLOW_HOSTS_PLACE_HOLDER;
AllowHostsTextBox.ForeColor = Color.FromKnownColor(KnownColor.ControlDark);
}
}
Expand Down
17 changes: 1 addition & 16 deletions XenAdmin/SettingsPanels/NRPEEditPage.resx
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
<value>5</value>
</data>
<data name="DescLabel.Text" xml:space="preserve">
<value>NRPE (Nagios Remote Plugin Excutor) allow you to execute Nagios plugins on a remote host. You can modify NRPE General Configuration and NRPE Check Threshold in this page.</value>
<value>NRPE (Nagios Remote Plugin Excutor) allow you to execute Nagios plugins on a remote host. You can modify NRPE general configuration and NRPE Check threshold in this page.</value>
</data>
<data name="&gt;&gt;DescLabel.Name" xml:space="preserve">
<value>DescLabel</value>
Expand Down Expand Up @@ -576,21 +576,6 @@
<data name="NRPETableLayoutPanel.LayoutSettings" type="System.Windows.Forms.TableLayoutSettings, System.Windows.Forms">
<value>&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;TableLayoutSettings&gt;&lt;Controls&gt;&lt;Control Name="PoolTipsTableLayoutPanel" Row="4" RowSpan="1" Column="0" ColumnSpan="1" /&gt;&lt;Control Name="DescLabel" Row="0" RowSpan="1" Column="0" ColumnSpan="1" /&gt;&lt;Control Name="GeneralConfigureGroupBox" Row="1" RowSpan="1" Column="0" ColumnSpan="1" /&gt;&lt;Control Name="CheckDataGridView" Row="2" RowSpan="1" Column="0" ColumnSpan="1" /&gt;&lt;/Controls&gt;&lt;Columns Styles="Percent,100" /&gt;&lt;Rows Styles="AutoSize,0,AutoSize,0,AutoSize,0,AutoSize,0,AutoSize,0" /&gt;&lt;/TableLayoutSettings&gt;</value>
</data>
<data name="EnableNRPEFlowLayoutPanel.Location" type="System.Drawing.Point, System.Drawing">
<value>0, 0</value>
</data>
<data name="EnableNRPEFlowLayoutPanel.Size" type="System.Drawing.Size, System.Drawing">
<value>200, 100</value>
</data>
<data name="EnableNRPEFlowLayoutPanel.TabIndex" type="System.Int32, mscorlib">
<value>0</value>
</data>
<data name="&gt;&gt;EnableNRPEFlowLayoutPanel.Name" xml:space="preserve">
<value>EnableNRPEFlowLayoutPanel</value>
</data>
<data name="&gt;&gt;EnableNRPEFlowLayoutPanel.Type" xml:space="preserve">
<value>System.Windows.Forms.FlowLayoutPanel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
Expand Down
1 change: 1 addition & 0 deletions XenAdmin/XenAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4442,6 +4442,7 @@
<None Include="Images\000_MigrateSuspendedVM_h32bit_16.png" />
<Content Include="Images\000_MigrateVM_h32bit_16.png" />
<Content Include="Images\000_MigrateVM_h32bit_32.png" />
<None Include="Images\000_NRPE_h32bit_16.png" />
<Content Include="Images\000_Patch_h32bit_32.png" />
<None Include="Images\000_ScheduledVMsnapshotDiskMemory_h32bit_16.png" />
<None Include="Images\000_ScheduledVMSnapshotDiskMemory_h32bit_32.png" />
Expand Down
26 changes: 19 additions & 7 deletions XenModel/Actions/Host/NRPEUpdateAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ protected override void Run()
{
SetNRPEConfigureForHost();
}
else
{
SetNRPEConfigureForPool();
else
{
SetNRPEConfigureForPool();
}
}

Expand Down Expand Up @@ -96,6 +96,8 @@ private void SetNRPEConfigureForHost()
SetNRPEThreshold(Clone, CurrentCheck.Name, CurrentCheck.WarningThreshold, CurrentCheck.CriticalThreshold);
}
}

RestartNRPE(Clone);
}

private void SetNRPEConfigureForPool()
Expand Down Expand Up @@ -128,6 +130,8 @@ private void SetNRPEConfigureForPool()
SetNRPEThreshold(host, CurrentCheck.Name, CurrentCheck.WarningThreshold, CurrentCheck.CriticalThreshold);
}
}
RestartNRPE(host);
});
}

Expand All @@ -140,13 +144,13 @@ private void SetNRPEStatus(IXenObject host, bool enableNRPE)
log.InfoFormat("Execute nrpe {0}, return: {1}", nrpeUpdateStatusMethod, result);
}

private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, string debug, string sslLogging)
private void SetNRPEGeneralConfiguration(IXenObject host, string allowHosts, bool debug, bool sslLogging)
{
Dictionary<string, string> ConfigArgDict = new Dictionary<string, string>
{
{ "allowed_hosts", "127.0.0.1,::1," + allowHosts },
{ "debug", debug },
{ "ssl_logging", sslLogging }
{ "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);
Expand Down Expand Up @@ -175,5 +179,13 @@ private void SetNRPEThreshold(IXenObject host, string checkName, string warningT
criticalThreshold,
result);
}

// After modified NRPE configuration, we need to restart NRPE to take effect
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);
}
}
}
Loading

0 comments on commit 6ac41a2

Please sign in to comment.