MOM Management Server Class Library Queries
Basic Queries
When using the MOM Management Server Class Library (MCL), simple queries for alerts, computers, events, performance data, and tasks are performed using the following classes:
Methods on these five classes support the most commonly used queries. For example, the AlertQuery class allows you to query by owner, resolution state, associated rule ID, and other important properties of the Alert class:
- AlertQuery.GetAlertsByOwner
- AlertQuery.GetAlertsByResolutionStateId
- AlertQuery.GetAlertsByResolutionStateIdAndComputer
- AlertQuery.GetAlertsByRuleId
See the AlertQuery Methods topic for a complete list of the supported methods. Many of the query-related methods include multiple overloads.
In situations where the built-in query methods of these classes are not suitable, you must construct your own advanced query for alert, event, computer, and performance data objects. Note that advanced queries are not available for the Task class.
Advanced Queries
Advanced queries use Boolean expression trees to define the object properties and values used to filter the search results. The queries are based on the MCL classes and do not require any knowledge of SQL or the underlying MOM database schema.
The following illustration shows a simple Boolean expression tree, which includes an operator (1), a left operand (2) and a right operand (3):
The following illustration shows the expression tree for a query that retrieves all MOM events with a Source property value equal to "MSSQLSERVER":
The elements of the query would be constructed using the following classes and enumerations:
Expression | EventQueryExpression |
Left expression | EventColumnNameExpression.ColumnName.Source enumeration |
Operator | QueryExpression.ExpressionOperatorType.Equals enumeration |
Right expression | ValueExpression("MSSQLSERVER") class |
Classes and Enumerations for Advanced Queries
A similar set of enumerations and classes exist for defining the alert, event, computer, and performance data queries. The four types of query expressions are a defined using the following elements:
All queries use one of the QueryExpression.ExpressionOperatorType enumerations as the expression object's ExpressionOperator property value. All queries use the ValueExpression class for the expression object's RightExpression property value.
Advanced Query Examples
The following code sample demonstrates how the example event query expression would be implemented using the MCL classes and C#:
/// <summary>
/// Demonstrates how to use the MCL query classes to retrieve
/// MOM Event objects based on their property values.
/// </summary>
/// <param name="source">The event source to use in the query.</param>
public static void GetEventsBySource(string source)
{
Administration mom;
EventsCollection events;
EventQuery eventQuery;
EventQueryExpression eventExpression;
EventColumnNameExpression eventSourceExpression;
ValueExpression sourceValue;
try
{
mom = Administration.GetAdministrationObject();
eventQuery = mom.GetEventQueryObject();
eventExpression = new EventQueryExpression();
// set the expression's operator
eventExpression.ExpressionOperator = QueryExpression.ExpressionOperatorType.Equals;
// set left side of the Boolean expression using the named property of the event
eventSourceExpression = new EventColumnNameExpression(EventColumnNameExpression.ColumnName.Source);
eventExpression.LeftExpression = eventSourceExpression;
// set right side of the Boolean expression with the property value
sourceValue = new ValueExpression(source);
eventExpression.RightExpression = sourceValue;
// run the query
events = eventQuery.GetEvents(eventExpression);
Trace.WriteLine("Found " + events.TotalCount.ToString() + " matching objects.");
foreach(Event momEvent in events)
{
Trace.WriteLine("\nFound a matching event");
Trace.WriteLine(" Source: " + momEvent.Source);
Trace.WriteLine(" Computer: " + momEvent.ComputerDomain + "\\" + momEvent.ComputerName);
Trace.WriteLine(" Message: " + momEvent.Message);
}
}
catch (Exception e)
{
Trace.WriteLine("Event query failed: " + e.Message);
}
}