Skip to content

Commit

Permalink
Some corrections in wording, code style, C# usage, and null checks.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <[email protected]>
  • Loading branch information
kc284 authored and BengangY committed Oct 8, 2023
1 parent 596a83d commit b2f8efa
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 85 deletions.
2 changes: 1 addition & 1 deletion XenAdmin/Dialogs/PropertiesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private void Build()
}
if (isHost || isPool)
{
NRPEEditPage = new NRPEEditPage(isHost);
NRPEEditPage = new NRPEEditPage();
ShowTab(NRPEEditPage);
}
}
Expand Down
66 changes: 32 additions & 34 deletions XenAdmin/SettingsPanels/NRPEEditPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ 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,})$");

private readonly bool IsHost = true;
private bool _isHost;

private IXenObject Clone;
private readonly ToolTip InvalidParamToolTip;
Expand All @@ -62,11 +61,12 @@ public partial class NRPEEditPage : UserControl, IEditPage

private NRPEHostConfiguration NRPEOriginalConfig;
private NRPEHostConfiguration NRPECurrentConfig;

public string SubText
{
get
{
if (IsHost)
if (_isHost)
{
return EnableNRPECheckBox.Checked ? Messages.NRPE_ACTIVE : Messages.NRPE_INACTIVE;
}
Expand All @@ -79,11 +79,10 @@ public string SubText

public Image Image => Images.StaticImages._000_Module_h32bit_16;

public NRPEEditPage(bool isHost)
public NRPEEditPage()
{
IsHost = isHost;
InitializeComponent();
Text = "NRPE";
Text = Messages.NRPE;

NRPECurrentConfig = new NRPEHostConfiguration
{
Expand Down Expand Up @@ -118,7 +117,7 @@ public NRPEEditPage(bool isHost)
CheckGroupDictByLabel.Add(checkGroup.NameCell.Value.ToString(), checkGroup);
}

UpdateOtherComponentBasedEnableNRPECheckBox(false);
UpdateOtherComponentBasedEnableNRPECheckBox();

AllowHostsTextBox.ForeColor = Color.FromKnownColor(KnownColor.ControlDark);
AllowHostsTextBox.GotFocus += AllowHostsTextBox_GotFocus;
Expand Down Expand Up @@ -146,11 +145,7 @@ public bool ValidToSave
return true;
}

bool valid = true;
if (!IsAllowHostsValid())
{
valid = false;
}
bool valid = IsAllowHostsValid();

foreach (CheckGroup checkGroup in CheckGroupList)
{
Expand Down Expand Up @@ -189,7 +184,7 @@ public void ShowLocalValidationMessages()

public void HideLocalValidationMessages()
{
if (InvalidParamToolTip.Tag is Control ctrl && ctrl != null)
if (InvalidParamToolTip.Tag is Control ctrl)
{
InvalidParamToolTip.Hide(ctrl);
}
Expand All @@ -198,11 +193,12 @@ public void HideLocalValidationMessages()
public void SetXenObjects(IXenObject orig, IXenObject clone)
{
Clone = clone;
_isHost = Clone is Host;

descLabelHost.Visible = IsHost;
DescLabelPool.Visible = !IsHost;
descLabelHost.Visible = _isHost;
DescLabelPool.Visible = !_isHost;

if (IsHost)
if (_isHost)
{
InitNRPEGeneralConfiguration();
InitNRPEThreshold();
Expand Down Expand Up @@ -257,7 +253,7 @@ private void InitNRPEGeneralConfiguration()
}
NRPEOriginalConfig = (NRPEHostConfiguration) NRPECurrentConfig.Clone();

UpdateOtherComponentBasedEnableNRPECheckBox(EnableNRPECheckBox.Checked);
UpdateOtherComponentBasedEnableNRPECheckBox();
}

private void InitNRPEThreshold()
Expand Down Expand Up @@ -326,24 +322,18 @@ private string AllowHostsWithoutLocalAddress(string allowHosts)
UpdatedAllowHosts.Substring(0, UpdatedAllowHosts.Length - 1);
}

private void UpdateOtherComponentBasedEnableNRPECheckBox(bool check)
private void UpdateOtherComponentBasedEnableNRPECheckBox()
{
if (check)
if (EnableNRPECheckBox.Checked)
{
AllowHostsTextBox.Enabled = true;
AllowHostsLabel.Enabled = true;
DebugLogCheckBox.Enabled = true;
SslDebugLogCheckBox.Enabled = true;
GeneralConfigureGroupBox.Enabled = true;
CheckDataGridView.Enabled = true;
CheckDataGridView.BackgroundColor = Color.FromKnownColor(KnownColor.Window);
CheckDataGridView.DefaultCellStyle.BackColor = Color.FromKnownColor(KnownColor.Window);
}
else
{
AllowHostsTextBox.Enabled = false;
AllowHostsLabel.Enabled = false;
DebugLogCheckBox.Enabled = false;
SslDebugLogCheckBox.Enabled = false;
GeneralConfigureGroupBox.Enabled = false;
CheckDataGridView.Enabled = false;
CheckDataGridView.BackgroundColor = Color.FromKnownColor(KnownColor.Control);
CheckDataGridView.DefaultCellStyle.BackColor = Color.FromKnownColor(KnownColor.Control);
Expand All @@ -352,7 +342,7 @@ private void UpdateOtherComponentBasedEnableNRPECheckBox(bool check)

private void EnableNRPECheckBox_CheckedChanged(object sender, EventArgs e)
{
UpdateOtherComponentBasedEnableNRPECheckBox(EnableNRPECheckBox.Checked);
UpdateOtherComponentBasedEnableNRPECheckBox();
}

private void AllowHostsTextBox_GotFocus(object sender, EventArgs e)
Expand All @@ -375,8 +365,9 @@ private void AllowHostsTextBox_LostFocus(object sender, EventArgs e)

private void CheckDataGridView_BeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridViewCell currentCell = CheckDataGridView.CurrentRow.Cells[e.ColumnIndex];
if (!IsHost && currentCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlDark)))
DataGridViewCell currentCell = CheckDataGridView.CurrentRow?.Cells[e.ColumnIndex];

if (currentCell != null && !_isHost && currentCell.Style.ForeColor.Equals(Color.FromKnownColor(KnownColor.ControlDark)))
{
currentCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
currentCell.Value = "";
Expand All @@ -385,13 +376,20 @@ private void CheckDataGridView_BeginEdit(object sender, DataGridViewCellCancelEv

private void CheckDataGridView_EndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell currentCell = CheckDataGridView.CurrentRow.Cells[e.ColumnIndex];
if (!IsHost && currentCell.Value.ToString().Trim().Equals(""))
DataGridViewCell currentCell = CheckDataGridView.CurrentRow?.Cells[e.ColumnIndex];

if (currentCell != null &&!_isHost && currentCell.Value.ToString().Trim().Equals(""))
{
currentCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark);
CheckGroupDictByLabel.TryGetValue(CheckDataGridView.CurrentRow.Cells[0].Value.ToString(), out CheckGroup checkGroup);
currentCell.Value = currentCell.ColumnIndex == 1 ?
checkGroup.WarningThresholdDefault : (object)checkGroup.CriticalThresholdDefault;

if (checkGroup != null)
{
if (currentCell.ColumnIndex == WarningThresholdColumn.Index)
currentCell.Value = checkGroup.WarningThresholdDefault;
else if (currentCell.ColumnIndex == CriticalThresholdColumn.Index)
currentCell.Value = checkGroup.CriticalThresholdDefault;
}
}
}
}
Expand Down
72 changes: 34 additions & 38 deletions XenModel/Actions/NRPE/NRPEHostConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,55 +35,51 @@ namespace XenAdmin.Actions.NRPE
{
public class NRPEHostConfiguration : ICloneable
{
public static readonly string XAPI_NRPE_PLUGIN_NAME = "nrpe";
public static readonly string XAPI_NRPE_STATUS = "status";
public static readonly string XAPI_NRPE_GET_CONFIG = "get-config";
public static readonly string XAPI_NRPE_GET_THRESHOLD = "get-threshold";
public static readonly string XAPI_NRPE_ENABLE = "enable";
public static readonly string XAPI_NRPE_DISABLE = "disable";
public static readonly string XAPI_NRPE_SET_CONFIG = "set-config";
public static readonly string XAPI_NRPE_SET_THRESHOLD = "set-threshold";
public static readonly string XAPI_NRPE_RESTART = "restart";

public static readonly string DEBUG_ENABLE = "1";
public static readonly string DEBUG_DISABLE = "0";

public static readonly string SSL_LOGGING_ENABLE = "0xff";
public static readonly string SSL_LOGGING_DISABLE = "0x00";
public const string XAPI_NRPE_PLUGIN_NAME = "nrpe";
public const string XAPI_NRPE_STATUS = "status";
public const string XAPI_NRPE_GET_CONFIG = "get-config";
public const string XAPI_NRPE_GET_THRESHOLD = "get-threshold";
public const string XAPI_NRPE_ENABLE = "enable";
public const string XAPI_NRPE_DISABLE = "disable";
public const string XAPI_NRPE_SET_CONFIG = "set-config";
public const string XAPI_NRPE_SET_THRESHOLD = "set-threshold";
public const string XAPI_NRPE_RESTART = "restart";

public const string DEBUG_ENABLE = "1";
public const string DEBUG_DISABLE = "0";

public const string SSL_LOGGING_ENABLE = "0xff";
public const string SSL_LOGGING_DISABLE = "0x00";

public static readonly string ALLOW_HOSTS_PLACE_HOLDER = Messages.NRPE_ALLOW_HOSTS_PLACE_HOLDER;

private bool enableNRPE;
private string allowHosts;
private bool debug;
private bool sslLogging;
private readonly Dictionary<string, Check> checkDict = new Dictionary<string, Check>();

public bool EnableNRPE { get => enableNRPE; set => enableNRPE = value; }
public string AllowHosts { get => allowHosts; set => allowHosts = value; }
public bool Debug { get => debug; set => debug = value; }
public bool SslLogging { get => sslLogging; set => sslLogging = value; }
public Dictionary<string, Check> CheckDict { get => checkDict; }
public bool EnableNRPE { get; set; }

public string AllowHosts { get; set; }

public bool Debug { get; set; }

public bool SslLogging { get; set; }

public Dictionary<string, Check> CheckDict => checkDict;

public class Check
{
public string Name;
public string WarningThreshold;
public string CriticalThreshold;
public string Name { get; }
public string WarningThreshold { get; }
public string CriticalThreshold{ get; }

public Check(string name, string warningThreshold, string criticalThreshold)
{
this.Name = name;
this.WarningThreshold = warningThreshold;
this.CriticalThreshold = criticalThreshold;
Name = name;
WarningThreshold = warningThreshold;
CriticalThreshold = criticalThreshold;
}

}

public NRPEHostConfiguration()
{
}

public void AddNRPECheck(Check checkItem)
{
checkDict.Add(checkItem.Name, checkItem);
Expand All @@ -98,10 +94,10 @@ public object Clone()
{
NRPEHostConfiguration cloned = new NRPEHostConfiguration
{
enableNRPE = enableNRPE,
allowHosts = allowHosts,
debug = debug,
sslLogging = sslLogging
EnableNRPE = EnableNRPE,
AllowHosts = AllowHosts,
Debug = Debug,
SslLogging = SslLogging
};
return cloned;
}
Expand Down
21 changes: 15 additions & 6 deletions XenModel/Messages.Designer.cs

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

15 changes: 9 additions & 6 deletions XenModel/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -10092,20 +10092,23 @@ When you configure an NFS storage repository, you simply provide the host name o
<data name="NOW" xml:space="preserve">
<value>Now</value>
</data>
<data name="NRPE" xml:space="preserve">
<value>NRPE</value>
</data>
<data name="NRPE_ACTION_CHANGING" xml:space="preserve">
<value>Changing NRPE configuration...</value>
</data>
<data name="NRPE_ACTIVE" xml:space="preserve">
<value>NRPE service is active</value>
</data>
<data name="NRPE_ALLOW_HOSTS_EMPTY_ERROR" xml:space="preserve">
<value>Allow hosts should not be empty.</value>
<value>Monitoring servers should not be empty.</value>
</data>
<data name="NRPE_ALLOW_HOSTS_ERROR_TITLE" xml:space="preserve">
<value>Allow hosts format is not correct</value>
<value>Monitoring servers format is not correct</value>
</data>
<data name="NRPE_ALLOW_HOSTS_FORMAT_ERROR" xml:space="preserve">
<value>Allow hosts should be comma separated IP address or domain list, e.g. 192.168.1.1, test.domain.com</value>
<value>Monitoring servers should be comma separated IP address or domain list, e.g. 192.168.1.1, test.domain.com</value>
</data>
<data name="NRPE_ALLOW_HOSTS_PLACE_HOLDER" xml:space="preserve">
<value>Comma separated IP address or domain list, e.g. 192.168.1.1, test.domain.com</value>
Expand Down Expand Up @@ -10156,16 +10159,16 @@ When you configure an NFS storage repository, you simply provide the host name o
<value>Threshold value should range from {0} to {1}.</value>
</data>
<data name="NRPE_THRESHOLD_SHOULD_BE_3_NUMBERS" xml:space="preserve">
<value>Threshold value should be 3 numbers separated with comma.</value>
<value>Threshold value should consist of 3 comma separated numbers.</value>
</data>
<data name="NRPE_THRESHOLD_SHOULD_BE_NUMBER" xml:space="preserve">
<value>Threshold value should be number.</value>
<value>Threshold value should be a number.</value>
</data>
<data name="NRPE_THRESHOLD_SHOULD_NOT_BE_EMPTY" xml:space="preserve">
<value>Threshold value should not be empty.</value>
</data>
<data name="NRPE_THRESHOLD_WARNING_SHOULD_BIGGER_THAN_CRITICAL" xml:space="preserve">
<value>Warning threshold should be bigger than critical threshold.</value>
<value>Warning threshold should be greater than critical threshold.</value>
</data>
<data name="NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL" xml:space="preserve">
<value>Warning threshold should be less than critical threshold.</value>
Expand Down

0 comments on commit b2f8efa

Please sign in to comment.