Skip to content

Commit

Permalink
Merge pull request #3255 from xenserver/feature/nrpe
Browse files Browse the repository at this point in the history
Merge branch feature/nrpe to master
  • Loading branch information
kc284 authored Nov 10, 2023
2 parents e42f607 + eda68f6 commit f92e988
Show file tree
Hide file tree
Showing 17 changed files with 2,361 additions and 6 deletions.
7 changes: 3 additions & 4 deletions XenAdmin/Dialogs/GraphDetailsDialog.Designer.cs

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

7 changes: 7 additions & 0 deletions XenAdmin/Dialogs/PropertiesDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public partial class PropertiesDialog : VerticallyTabbedDialog
private ClusteringEditPage ClusteringEditPage;
private SrReadCachingEditPage SrReadCachingEditPage;
private PoolAdvancedEditPage _poolAdvancedEditPage;
private NRPEEditPage NRPEEditPage;
#endregion

private readonly IXenObject _xenObjectBefore, _xenObjectCopy;
Expand Down Expand Up @@ -317,6 +318,12 @@ private void Build()
dialog.ShowDialog(Program.MainWindow);
}
}
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);
}
}
finally
{
Expand Down
213 changes: 213 additions & 0 deletions XenAdmin/SettingsPanels/NRPEEditPage.CheckGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/* Copyright (c) Cloud Software Group, Inc.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

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

namespace XenAdmin.SettingsPanels
{
public partial class NRPEEditPage
{
public class CheckGroup
{
private const decimal THRESHOLD_MINIMUM = 0.01M;
private const decimal THRESHOLD_MAXIMUM = 100M;

public string Name { get; }

public DataGridViewRow CheckThresholdRow { get; }

public DataGridViewTextBoxCell NameCell { get; }

public DataGridViewTextBoxCell WarningThresholdCell { get; }

public DataGridViewTextBoxCell CriticalThresholdCell { get; }

public CheckGroup(string name, string labelName)
{
Name = name;
NameCell = new DataGridViewTextBoxCell { Value = labelName, ToolTipText = string.Format(Messages.NRPE_METRIC_TOOLTIP, name) };
WarningThresholdCell = new DataGridViewTextBoxCell { Value = "" };
CriticalThresholdCell = new DataGridViewTextBoxCell { Value = "" };
CheckThresholdRow = new DataGridViewRow();
CheckThresholdRow.Cells.AddRange(NameCell, WarningThresholdCell, CriticalThresholdCell);
CheckThresholdRow.DefaultCellStyle.Format = "N2";
CheckThresholdRow.DefaultCellStyle.NullValue = 0;
WarningThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark);
CriticalThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlDark);
}

public void UpdateThreshold(string warningThreshold, string criticalThreshold)
{
WarningThresholdCell.Value = warningThreshold;
CriticalThresholdCell.Value = criticalThreshold;
WarningThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
CriticalThresholdCell.Style.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
}

public virtual bool CheckValue()
{
WarningThresholdCell.ErrorText = "";
CriticalThresholdCell.ErrorText = "";

return CheckEachValue(WarningThresholdCell) &&
CheckEachValue(CriticalThresholdCell) &&
CompareWarningAndCritical();
}

protected virtual bool CheckEachValue(DataGridViewTextBoxCell cell)
{
string thresholdStr = cell.Value.ToString().Trim();
if (thresholdStr.Equals(""))
{
cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_NOT_BE_EMPTY);
return false;
}

if (!decimal.TryParse(thresholdStr, out decimal threshold))
{
cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_BE_NUMBER);
return false;
}

if (threshold < THRESHOLD_MINIMUM || threshold > THRESHOLD_MAXIMUM)
{
cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_RANGE_ERROR, THRESHOLD_MINIMUM,
THRESHOLD_MAXIMUM);
return false;
}

cell.ErrorText = "";
return true;
}

protected virtual bool CompareWarningAndCritical()
{
decimal.TryParse(WarningThresholdCell.Value.ToString().Trim(), out decimal warningDecimal);
decimal.TryParse(CriticalThresholdCell.Value.ToString().Trim(), out decimal criticalDecimal);
if (warningDecimal < criticalDecimal)
{
WarningThresholdCell.ErrorText = "";
return true;
}
else
{
WarningThresholdCell.ErrorText =
string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL);
return false;
}
}
}

public class FreeCheckGroup : CheckGroup
{
public FreeCheckGroup(string name, string labelName)
: base(name, labelName)
{
}

protected override bool CompareWarningAndCritical()
{
decimal.TryParse(WarningThresholdCell.Value.ToString().Trim(), out decimal warningDecimal);
decimal.TryParse(CriticalThresholdCell.Value.ToString().Trim(), out decimal criticalDecimal);
if (warningDecimal > criticalDecimal)
{
WarningThresholdCell.ErrorText = "";
return true;
}
else
{
WarningThresholdCell.ErrorText =
string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_BIGGER_THAN_CRITICAL);
return false;
}
}

}

public class HostLoadCheckGroup : CheckGroup
{
public HostLoadCheckGroup(string name, string labelName)
: base(name, labelName)
{
}
}

public class Dom0LoadCheckGroup : CheckGroup
{
public Dom0LoadCheckGroup(string name, string labelName)
: base(name, labelName)
{
}

protected override bool CompareWarningAndCritical()
{
string[] warningArray = WarningThresholdCell.Value.ToString().Split(',');
string[] criticalArray = CriticalThresholdCell.Value.ToString().Split(',');
for (int i = 0; i < 3; i++)
{
decimal.TryParse(warningArray[i].Trim(), out decimal warningDecimal);
decimal.TryParse(criticalArray[i].Trim(), out decimal criticalDecimal);
if (warningDecimal > criticalDecimal)
{
WarningThresholdCell.ErrorText =
string.Format(Messages.NRPE_THRESHOLD_WARNING_SHOULD_LESS_THAN_CRITICAL);
return false;
}
}

WarningThresholdCell.ErrorText = "";
return true;
}

protected override bool CheckEachValue(DataGridViewTextBoxCell cell)
{
cell.ErrorText = string.Format(Messages.NRPE_THRESHOLD_SHOULD_BE_3_NUMBERS);
string[] loadArray = cell.Value.ToString().Split(',');
if (loadArray.Length != 3)
{
return false;
}

foreach (string load in loadArray)
{
bool isDecimal = decimal.TryParse(load, out _);
if (!isDecimal)
{
return false;
}
}

cell.ErrorText = "";
return true;
}
}
}
}
Loading

0 comments on commit f92e988

Please sign in to comment.