245 lines
6.7 KiB
C#
245 lines
6.7 KiB
C#
using System;
|
|
using System.Management;
|
|
|
|
namespace Endpoint
|
|
{
|
|
/// <summary>
|
|
/// Endpoint for Windows Management Instrumentation (WMI)
|
|
/// </summary>
|
|
public class WmiEndpoint : IEndpoint
|
|
{
|
|
/// <summary>
|
|
/// Gets the service name
|
|
/// </summary>
|
|
public string Name { get; set; }
|
|
|
|
/// <summary>
|
|
/// Returns a string explaining details on the current status
|
|
/// </summary>
|
|
/// <returns>string with status message</returns>
|
|
public string StatusDescription { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the name of the machine to query with WMI.
|
|
/// </summary>
|
|
/// <value>The name of the machine.</value>
|
|
public string MachineName { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the object query string.
|
|
/// </summary>
|
|
/// <value>The object query string.</value>
|
|
public string ObjectQueryString { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the connection username.
|
|
/// </summary>
|
|
/// <value>The connection username.</value>
|
|
public string ConnectionUsername { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the connection password.
|
|
/// </summary>
|
|
/// <value>The connection password.</value>
|
|
public string ConnectionPassword { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the name of the result property - this needs to be returned by the query.
|
|
/// </summary>
|
|
/// <value>The name of the result property.</value>
|
|
public string ResultPropertyName { get; set; }
|
|
|
|
/// <summary>
|
|
/// Report error when result is this value.
|
|
/// </summary>
|
|
/// <value>A result indicating an error.</value>
|
|
public string ErrorResult { get; set; }
|
|
|
|
/// <summary>
|
|
/// Report up when result is this value.
|
|
/// </summary>
|
|
/// <value>A result indicating everything is ok.</value>
|
|
public string UpResult { get; set; }
|
|
|
|
/// <summary>
|
|
/// Report error when result is below this number
|
|
/// </summary>
|
|
/// <value></value>
|
|
public double MinimumThreshold { get; set; }
|
|
|
|
/// <summary>
|
|
/// Report error when result is above this number
|
|
/// </summary>
|
|
/// <value></value>
|
|
public double MaximumThreshold { get; set; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="WmiEndpoint"/> class.
|
|
/// </summary>
|
|
public WmiEndpoint()
|
|
{
|
|
MinimumThreshold = double.MinValue;
|
|
MaximumThreshold = double.MaxValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current status result of the endpoint
|
|
/// </summary>
|
|
/// <returns>Status</returns>
|
|
public Status GetStatus()
|
|
{
|
|
var connectionOptions = new ConnectionOptions();
|
|
if (!string.IsNullOrEmpty(ConnectionUsername) && !string.IsNullOrEmpty(ConnectionPassword))
|
|
{
|
|
connectionOptions.Username = ConnectionUsername;
|
|
connectionOptions.Password = ConnectionPassword;
|
|
}
|
|
|
|
ManagementScope managementScope;
|
|
try
|
|
{
|
|
managementScope = new ManagementScope(MachineName, connectionOptions);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
StatusDescription = string.Format("Management Scope for MachineName \"{1}\" Failed : \"{0}\"", e.Message, MachineName);
|
|
return Status.Error;
|
|
}
|
|
|
|
ObjectQuery objectQuery;
|
|
try
|
|
{
|
|
objectQuery = new ObjectQuery(ObjectQueryString);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
StatusDescription = string.Format("ObjectQuery initialization \"{1}\" Failed : \"{0}\"", e.Message, ObjectQueryString);
|
|
return Status.Error;
|
|
}
|
|
|
|
ManagementObjectSearcher results;
|
|
try
|
|
{
|
|
//Execute the query
|
|
results = new ManagementObjectSearcher(managementScope, objectQuery);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
StatusDescription = string.Format("Building Query failed : \"{0}\"", e.Message);
|
|
return Status.Error;
|
|
}
|
|
|
|
ManagementObjectCollection managementObjectCollection;
|
|
try
|
|
{
|
|
//Get the results
|
|
managementObjectCollection = results.Get();
|
|
|
|
if (managementObjectCollection.Count == 0)
|
|
{
|
|
StatusDescription = string.Format("Query returned 0 results : \"{0}\"", ObjectQueryString);
|
|
return Status.Error;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
StatusDescription = "Error retrieving results. Exception: " + ex.Message;
|
|
return Status.Unreachable;
|
|
}
|
|
|
|
ManagementObject firstResult = null;
|
|
|
|
//take only the first result if there are more than one
|
|
foreach (ManagementObject result in managementObjectCollection)
|
|
{
|
|
firstResult = result;
|
|
break;
|
|
}
|
|
|
|
if (firstResult == null)
|
|
{
|
|
StatusDescription = "Problem accessing first result object";
|
|
return Status.Error;
|
|
}
|
|
|
|
object resultValue;
|
|
try
|
|
{
|
|
resultValue = firstResult[ResultPropertyName];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
StatusDescription = string.Format("Error retrieving result property \"{0}\". Exception: {1}",
|
|
ResultPropertyName, ex.Message);
|
|
return Status.Error;
|
|
}
|
|
|
|
if (resultValue == null)
|
|
{
|
|
StatusDescription = string.Format("Result value was null for property \"{0}\".", ResultPropertyName);
|
|
return Status.Error;
|
|
}
|
|
|
|
return getStatus(resultValue.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the status.
|
|
/// </summary>
|
|
/// <param name="resultValueString">The result value string.</param>
|
|
/// <returns></returns>
|
|
private Status getStatus(string resultValueString)
|
|
{
|
|
// Up Value
|
|
if (!string.IsNullOrEmpty(UpResult) && UpResult != resultValueString)
|
|
{
|
|
StatusDescription =
|
|
string.Format("Result for property \"{0}\" was not expected value. Expected \"{1}\" but was \"{2}\"",
|
|
ResultPropertyName, UpResult ?? "(NULL)", resultValueString);
|
|
return Status.Error;
|
|
}
|
|
|
|
// Error Value
|
|
if (!string.IsNullOrEmpty(ErrorResult) && ErrorResult == resultValueString)
|
|
{
|
|
StatusDescription =
|
|
string.Format("Result for property \"{0}\" was error value - \"{1}\"",
|
|
ResultPropertyName, resultValueString);
|
|
return Status.Error;
|
|
}
|
|
|
|
if (MinimumThreshold != double.MinValue || MaximumThreshold != double.MaxValue)
|
|
{
|
|
double resultValueDouble;
|
|
if (Double.TryParse(resultValueString, out resultValueDouble))
|
|
{
|
|
if (resultValueDouble < MinimumThreshold)
|
|
{
|
|
StatusDescription =
|
|
string.Format("Result for property \"{0}\" was less then threshold - {1} < {2}",
|
|
ResultPropertyName, resultValueDouble, MinimumThreshold);
|
|
return Status.Error;
|
|
}
|
|
if (resultValueDouble > MaximumThreshold)
|
|
{
|
|
StatusDescription =
|
|
string.Format("Result for property \"{0}\" was more then threshold - {1} > {2}",
|
|
ResultPropertyName, resultValueDouble, MaximumThreshold);
|
|
return Status.Error;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
StatusDescription =
|
|
string.Format("Result for property \"{0}\" was not a number - \"{1}\"",
|
|
ResultPropertyName, resultValueString);
|
|
return Status.Error;
|
|
}
|
|
}
|
|
|
|
StatusDescription = resultValueString;
|
|
return Status.Up;
|
|
}
|
|
}
|
|
}
|