Condividi tramite


How to Automate the Setup of URL Monitoring

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

You can automate the URL monitoring setup to use one or more agent computers to monitor a Web site or Web-based application. The URL monitoring information is created by using a template and then added to a Management Pack. You specify the URL that you want to monitor, one or more fully qualified domain names of the computers that will monitor the URL, the display name of the monitoring type that is created in a Management Pack, and the display name of the Management Pack you want to add the type to.

After you set up URL monitoring, you must also create a state view and an alert view for the newly created type in the Management Pack. This allows you to view the alerts that are generated from monitoring the URL and view the health state of the URL.

The following example sets up URL monitoring for an agent computer.

/// <summary> 
/// URL monitoring example.
/// </summary>
using System;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Xml;
using System.Globalization;
using System.Collections.Generic;

namespace SDKSamples
{
    class Program
    {
        private static ManagementGroup mg = null;

        static void Main(string[] args)
        {
            // The URL to monitor.
            string url = "https://www.microsoft.com";

            // One or more fully qualified domain names for the watcher nodes, 
            // which must have the Operations Manager agent installed.
            // Replace the placeholder name below with a real computer name.
            List<string> watcherNodes = new List<string>();
            watcherNodes.Add("computerName.domain.com");

            // The display name of the type that will be created.  This will appear in the 
            // authoring space and when you create a state view or alert view.
            string displayName = "TestURLMonitoring";

            // The display name of the Management Pack that the URL monitoring logic 
            // will be added into.
            string targetMP = "Sample Management Pack";

            mg = new ManagementGroup("localhost");

            string configDocXML = CreateConfigurationXmlDoc(url, watcherNodes, displayName);
            ManagementPackTemplate template = GetUrlMonitoringTemplate();
            ManagementPack mp = GetManagementPack(targetMP);

            string folderId = string.Format("WebAppFolder{0}", Guid.NewGuid().ToString("N"));
            mp.ProcessMonitoringTemplate(template, configDocXML, folderId, displayName, "folder description goes here");

        }

        private static MonitoringTemplate GetUrlMonitoringTemplate()
        {
            string criteria = "Name = 'Microsoft.SystemCenter.WebApplication.SingleUrl.Template'";
            MonitoringTemplateCriteria templateCriteria = new MonitoringTemplateCriteria(criteria);
            MonitoringTemplate template = null;

            template = mg.GetMonitoringTemplates(templateCriteria)[0];

            return template;
        }

        /// <summary>
        /// Gets the specified ManagementPack.
        /// </summary>
        private static ManagementPack GetManagementPack(string displayName)
        {
            string criteria = string.Format("DisplayName = '{0}'", displayName);
            ManagementPackCriteria mpCriteria = new ManagementPackCriteria(criteria);
            ManagementPack mp = null;

            try
            {
                mp = mg.GetManagementPacks(mpCriteria)[0];
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new InvalidOperationException(
                     "Could not find the specified Management Pack: " + criteria);
            }
            return mp;
        }


        private static void AddChildElement(XmlElement parentElement,
            string newElementName, string value)
        {
            XmlDocument document = parentElement.OwnerDocument;
            XmlElement newElement = document.CreateElement(newElementName);

            newElement.InnerText = value;
            parentElement.AppendChild(newElement);
        }

        private static string CreateConfigurationXmlDoc(
            string url,
            List<string> watcherNodes,
            string displayName
            )
        {
            XmlDocument configDoc = new XmlDocument();
            XmlElement rootNode = configDoc.CreateElement("Configuration");
            string typeId;
            string watcherNodesList;
            string uniqueKey;

            configDoc.AppendChild(rootNode);

            typeId = string.Format(CultureInfo.InvariantCulture,
                                   "WebApplication_{0}",
                                   Guid.NewGuid().ToString("N"));
            AddChildElement(rootNode, "TypeId", typeId);
            AddChildElement(rootNode, "Name", displayName);
            AddChildElement(rootNode, "Description", "");
            AddChildElement(rootNode, "LocaleId", "ENU");
            AddChildElement(rootNode, "Verb", "GET");
            AddChildElement(rootNode, "URL", url);
            AddChildElement(rootNode, "Version", "HTTP/1.1");
            AddChildElement(rootNode, "PollIntervalInSeconds", "120");
            AddWatcherNodeIds(rootNode, watcherNodes);

            watcherNodesList = CreateWatcherComputerList(watcherNodes);

            AddChildElement(rootNode, "WatcherComputersList", watcherNodesList);

            uniqueKey = Guid.NewGuid().ToString();

            AddChildElement(rootNode, "UniquenessKey", uniqueKey);
            AddChildElement(rootNode, "Proxy", "");
            AddChildElement(rootNode, "ProxyUserName", "");
            AddChildElement(rootNode, "ProxyPassword", "");
            AddChildElement(rootNode, "ProxyAuthenticationScheme", "None");
            AddChildElement(rootNode, "CredentialUserName", "");
            AddChildElement(rootNode, "CredentialPassword", "");
            AddChildElement(rootNode, "AuthenticationScheme", "None");

            return (configDoc.InnerXml);
        }

        private static void AddWatcherNodeIds(
            XmlElement rootNode,
            List<string> watcherNodes
            )
        {
            XmlElement includeListElement;
            includeListElement = rootNode.OwnerDocument.CreateElement("IncludeList");

            rootNode.AppendChild(includeListElement);

            if (watcherNodes.Count == 0)
            {
                throw new InvalidOperationException("No watcher nodes defined.");
            }

            foreach (string watcherNode in watcherNodes)
            {
                MonitoringObject computerMonitoringObject;
                computerMonitoringObject = GetComputerMonitoringObject(watcherNode);

                if (computerMonitoringObject == null)
                {
                    Console.WriteLine("watcher nodes not found.");
                }
                else
                {
                    AddChildElement(includeListElement,
                                    "MonitoringObjectId",
                                    computerMonitoringObject.Id.ToString());
                }
            }
        }

        private static string CreateWatcherComputerList(
            List<string> watcherNodes
            )
        {
            string watcherNodesList = string.Empty;
            if (watcherNodes.Count == 0)
            {
                throw new InvalidOperationException("No watcher nodes defined.");
            }
            else
            {
                for (int i = 0; i < watcherNodes.Count; i++)
                {
                    watcherNodesList += watcherNodes[i];

                    if (i < (watcherNodes.Count - 2))
                    {
                        watcherNodesList += " | ";
                    }
                }
                watcherNodesList = string.Format("({0})", watcherNodesList);
            }
            return (watcherNodesList);
        }

        private static MonitoringObject GetComputerMonitoringObject(
            string computerFQDN
            )
        {
            MonitoringObjectCriteria monitoringObjectCriteria;
            MonitoringClass windowsComputerClass;
            MonitoringObject monitoringObject;

            windowsComputerClass = mg.GetMonitoringClass(SystemMonitoringClass.WindowsComputer);

            monitoringObjectCriteria = new MonitoringObjectCriteria(string.Format("PrincipalName = '{0}'", computerFQDN),
                                                                    windowsComputerClass);
            try
            {
                monitoringObject = mg.GetMonitoringObjects(monitoringObjectCriteria)[0];
                return (monitoringObject);
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new InvalidOperationException("Could not find the specified monitoring object: " + computerFQDN);
            }
        }
    }
}
' URL monitoring example.
Imports System
Imports System.Collections.ObjectModel
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Monitoring
Imports System.Xml
Imports System.Globalization
Imports System.Collections.Generic


Namespace SDKSamples
    Class Program
        Private Shared mg As ManagementGroup = Nothing

        Public Overloads Shared Function Main(ByVal args() As String) As Integer
            ' The URL to monitor.
            Dim url As String = "https://www.microsoft.com"

            ' One or more fully qualified domain names for the watcher nodes, 
            ' which must have the Operations Manager agent installed.
            ' Replace the placeholder name below with a real computer name.
            Dim watcherNodes As List(Of String) = New List(Of String)
            watcherNodes.Add("computerName.domain.com")

            ' The display name of the type that will be created.  This will appear in the 
            ' authoring space and when you create a state view or alert view.
            Dim displayName As String = "TestURLMonitoring"

            ' The display name of the Management Pack that the URL monitoring logic 
            ' will be added into.
            Dim targetMP As String = "Sample Management Pack"

            mg = New ManagementGroup("localhost")

            Dim configDocXML As String = CreateConfigurationXmlDoc(url, watcherNodes, displayName)
            Dim template As ManagementPackTemplate = GetUrlMonitoringTemplate()
            Dim mp As ManagementPack = GetManagementPack(targetMP)

            Dim folderId As String = String.Format("WebAppFolder{0}", System.Guid.NewGuid().ToString("N"))

            mp.ProcessMonitoringTemplate(template, configDocXML, folderId, displayName, "folder description goes here")
        End Function 'Main


        Private Shared Function GetUrlMonitoringTemplate() As MonitoringTemplate
            Dim criteria As String = "Name = 'Microsoft.SystemCenter.WebApplication.SingleUrl.Template'"
            Dim templateCriteria As New MonitoringTemplateCriteria(criteria)
            Dim template As MonitoringTemplate = Nothing

            template = mg.GetMonitoringTemplates(templateCriteria)(0)

            Return template
        End Function 'GetUrlMonitoringTemplate


        '/ <summary>
        '/ Gets the specified ManagementPack.
        '/ </summary>
        Private Shared Function GetManagementPack(ByVal displayName As String) As ManagementPack
            Dim criteria As String = String.Format("DisplayName = '{0}'", displayName)
            Dim mpCriteria As New ManagementPackCriteria(criteria)
            Dim mp As ManagementPack = Nothing

            Try
                mp = mg.GetManagementPacks(mpCriteria)(0)
            Catch e As ArgumentOutOfRangeException

                Throw New InvalidOperationException("Could not find the specified Management Pack: " + criteria)
            End Try
            Return mp
        End Function 'GetManagementPack



        Private Shared Sub AddChildElement(ByVal parentElement As XmlElement, ByVal newElementName As String, ByVal value As String)
            Dim document As XmlDocument = parentElement.OwnerDocument
            Dim newElement As XmlElement = document.CreateElement(newElementName)

            newElement.InnerText = value
            parentElement.AppendChild(newElement)
        End Sub 'AddChildElement


        Private Shared Function CreateConfigurationXmlDoc(ByVal url As String, ByVal watcherNodes As List(Of String), _
            ByVal displayName As String)

            Dim configDoc As New XmlDocument()
            Dim rootNode As XmlElement = configDoc.CreateElement("Configuration")
            Dim typeId As String
            Dim watcherNodesList As String
            Dim uniqueKey As String

            configDoc.AppendChild(rootNode)

            typeId = String.Format(CultureInfo.InvariantCulture, "WebApplication_{0}", Guid.NewGuid().ToString("N"))
            AddChildElement(rootNode, "TypeId", typeId)
            AddChildElement(rootNode, "Name", displayName)
            AddChildElement(rootNode, "Description", "")
            AddChildElement(rootNode, "LocaleId", "ENU")
            AddChildElement(rootNode, "Verb", "GET")
            AddChildElement(rootNode, "URL", url)
            AddChildElement(rootNode, "Version", "HTTP/1.1")
            AddChildElement(rootNode, "PollIntervalInSeconds", "120")
            AddWatcherNodeIds(rootNode, watcherNodes)

            watcherNodesList = CreateWatcherComputerList(watcherNodes)

            AddChildElement(rootNode, "WatcherComputersList", watcherNodesList)

            uniqueKey = Guid.NewGuid().ToString()

            AddChildElement(rootNode, "UniquenessKey", uniqueKey)
            AddChildElement(rootNode, "Proxy", "")
            AddChildElement(rootNode, "ProxyUserName", "")
            AddChildElement(rootNode, "ProxyPassword", "")
            AddChildElement(rootNode, "ProxyAuthenticationScheme", "None")
            AddChildElement(rootNode, "CredentialUserName", "")
            AddChildElement(rootNode, "CredentialPassword", "")
            AddChildElement(rootNode, "AuthenticationScheme", "None")

            Return configDoc.InnerXml
        End Function

        Private Shared Sub AddWatcherNodeIds(ByVal rootNode As XmlElement, _
            ByVal watcherNodes As List(Of String))

            Dim includeListElement As XmlElement
            includeListElement = rootNode.OwnerDocument.CreateElement("IncludeList")

            rootNode.AppendChild(includeListElement)

            If watcherNodes.Count = 0 Then
                Throw New InvalidOperationException("No watcher nodes defined.")
            End If

            Dim watcherNode As String
            For Each watcherNode In watcherNodes
                Dim computerMonitoringObject As MonitoringObject
                computerMonitoringObject = GetComputerMonitoringObject(watcherNode)

                If computerMonitoringObject Is Nothing Then
                    Console.WriteLine("watcher nodes not found.")
                Else
                    AddChildElement(includeListElement, "MonitoringObjectId", computerMonitoringObject.Id.ToString())
                End If
            Next watcherNode
        End Sub

        Private Shared Function CreateWatcherComputerList(ByVal watcherNodes As List(Of String)) As String

            Dim watcherNodesList As String = String.Empty
            If watcherNodes.Count = 0 Then
                Throw New InvalidOperationException("No watcher nodes defined.")
            Else
                Dim i As Integer
                For i = 0 To watcherNodes.Count - 1
                    watcherNodesList += watcherNodes(i)

                    If i < watcherNodes.Count - 2 Then
                        watcherNodesList += " | "
                    End If
                Next i
                watcherNodesList = String.Format("({0})", watcherNodesList)
            End If
            Return watcherNodesList
        End Function


        Private Shared Function GetComputerMonitoringObject(ByVal computerFQDN As String) As MonitoringObject
            Dim monitoringObjectCriteria As MonitoringObjectCriteria
            Dim windowsComputerClass As MonitoringClass
            Dim monitoringObject As MonitoringObject

            windowsComputerClass = mg.GetMonitoringClass(SystemMonitoringClass.WindowsComputer)

            monitoringObjectCriteria = New MonitoringObjectCriteria(String.Format("PrincipalName = '{0}'", computerFQDN), windowsComputerClass)
            Try
                monitoringObject = mg.GetMonitoringObjects(monitoringObjectCriteria)(0)
                Return monitoringObject
            Catch e As ArgumentOutOfRangeException

                Throw New InvalidOperationException("Could not find the specified monitoring object: " + computerFQDN)

            End Try

        End Function

    End Class 'Program
End Namespace 'SDKSamples