Skip to content
Snippets Groups Projects
Commit 5d64f8e5 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

validate wmi checks are working...

parent e714c470
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ namespace ATxCommon
/// <summary>
/// Get the available physical memory in MB.
/// </summary>
/// <returns>The available physical memory in MB or -1 in case of an error.</returns>
/// <returns>Available physical memory in MB or -1 in case of an error.</returns>
public static long GetFreeMemory() {
try {
var searcher =
......@@ -26,7 +26,7 @@ namespace ATxCommon
}
}
catch (Exception ex) {
Log.Warn("Error in GetFreeMemory: {0}", ex.Message);
Log.Trace("Error in GetFreeMemory: {0}", ex.Message);
}
return -1;
......@@ -39,22 +39,94 @@ namespace ATxCommon
public static int GetCpuUsage() {
// TODO: fix bug #36
try {
Log.Trace("Querying WMI for CPU load...");
var searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
foreach (var mo in searcher.Get()) {
Int32 usageInt32 = -1;
/*
var managementObjects = searcher.Get();
Log.Trace("WMI query returned {0} objects.", managementObjects.Count);
foreach (var mo in managementObjects) {
var obj = (ManagementObject)mo;
var usage = obj["PercentProcessorTime"];
var name = obj["Name"];
if (name.ToString().Equals("_Total"))
return Convert.ToInt32(usage);
usageInt32 = Convert.ToInt32(usage);
Log.Trace("CPU usage {1}: {0}", usageInt32, name);
if (name.ToString().Equals("_Total")) {
usageInt32 = Convert.ToInt32(usage);
Log.Trace("CPU usage: {0}", usageInt32);
}
}
return usageInt32;
*/
var cpuTimes = searcher.Get()
.Cast<ManagementObject>()
.Select(mo => new {
Name = mo["Name"],
Usage = mo["PercentProcessorTime"]
}
)
.ToList();
if (cpuTimes.Count == 0)
return -1;
Log.Trace("WMI query returned {0} objects.", cpuTimes.Count);
// The '_Total' value represents the average usage across all cores,
// and is the best representation of overall CPU usage
var query = cpuTimes.Where(x => x.Name.ToString() == "_Total").Select(x => x.Usage);
var cpuUsage = query.SingleOrDefault();
usageInt32 = Convert.ToInt32(cpuUsage);
Log.Trace("CPU usage: {0}", usageInt32);
return usageInt32;
// */
}
catch (Exception ex) {
Log.Warn("Error in GetCpuUsage: {0}", ex.Message);
Log.Trace("Error in GetCpuUsage: {0}", ex.Message);
}
Log.Trace("Failed querying CPU usage!");
return -1;
}
/// <summary>
/// Validate the WMI query methods work correctly and create a summary or warning message.
/// </summary>
/// <returns>A summary with current readings from WMI or a warning message.</returns>
public static string WmiSummary() {
var failed = new List<string>();
var load = GetCpuUsage();
var free = GetFreeMemory();
var summary = $"CPU load: {load}\n" +
$"Free system memory: {Conv.MegabytesToString(free)}\n";
Log.Warn(summary);
if (load == -1) {
failed.Add("CPU load");
}
if (free == -1) {
failed.Add("free RAM");
}
if (failed.Count > 0) {
summary = "*******************************************************\n" +
"WARNING: Checking system parameters via WMI failed for:\n";
foreach (var property in failed) {
summary += $" - {property}\n";
}
summary += "\n" +
"-------------------------------------------------------\n" +
"Limits configured for these properties will be IGNORED!\n" +
"-------------------------------------------------------\n\n";
Log.Error(summary);
}
return summary;
}
/// <summary>
/// Get the free space of a drive in bytes.
/// </summary>
......
......@@ -338,7 +338,8 @@ namespace ATxService
msg += "\n------ Current system parameters ------\n" +
"Hostname: " + Environment.MachineName + "\n" +
"Free system memory: " + SystemChecks.GetFreeMemory() + " MB" + "\n";
SystemChecks.WmiSummary();
foreach (var driveToCheck in _config.SpaceMonitoring) {
msg += "Free space on drive '" + driveToCheck.DriveName + "': " +
Conv.BytesToString(SystemChecks.GetFreeDriveSpace(driveToCheck.DriveName)) + "\n";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment