using System;
using System.Management;
namespace Endpoint
{
///
/// Endpoint for Windows Management Instrumentation (WMI)
///
public class WmiEndpoint : IEndpoint
{
///
/// Gets the service name
///
public string Name { get; set; }
///
/// Returns a string explaining details on the current status
///
/// string with status message
public string StatusDescription { get; protected set; }
///
/// Gets or sets the name of the machine to query with WMI.
///
/// The name of the machine.
public string MachineName { get; set; }
///
/// Gets or sets the object query string.
///
/// The object query string.
public string ObjectQueryString { get; set; }
///
/// Gets or sets the connection username.
///
/// The connection username.
public string ConnectionUsername { get; set; }
///
/// Gets or sets the connection password.
///
/// The connection password.
public string ConnectionPassword { get; set; }
///
/// Gets or sets the name of the result property - this needs to be returned by the query.
///
/// The name of the result property.
public string ResultPropertyName { get; set; }
///
/// Report error when result is this value.
///
/// A result indicating an error.
public string ErrorResult { get; set; }
///
/// Report up when result is this value.
///
/// A result indicating everything is ok.
public string UpResult { get; set; }
///
/// Report error when result is below this number
///
///
public double MinimumThreshold { get; set; }
///
/// Report error when result is above this number
///
///
public double MaximumThreshold { get; set; }
///
/// Initializes a new instance of the class.
///
public WmiEndpoint()
{
MinimumThreshold = double.MinValue;
MaximumThreshold = double.MaxValue;
}
///
/// Gets the current status result of the endpoint
///
/// Status
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());
}
///
/// Gets the status.
///
/// The result value string.
///
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;
}
}
}