Freigeben über


How to Automate the Setup of URL Monitoring

Applies To: System Center 2012 - Operations Manager

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

You can automate the URL monitoring setup to use one or more agent computers to monitor a website or web-based application. The URL monitoring information is created by using a template, and then it is added to a management pack. You specify the URL that you want to monitor, one or more fully qualified domain names (FQDNs) 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 enables you to view the alerts that are generated from monitoring the URL, and it enables you to 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.Xml;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
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.ProcessTemplate(template, configDocXML, folderId, displayName, "folder description goes here");

        }

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

            template = mg.Templates.GetTemplates(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.ManagementPacks.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
            )
        {
            
            ManagementPackClass windowsComputerClass = mg.EntityTypes.GetClass(SystemMonitoringClass.WindowsComputer);      
            MonitoringObjectGenericCriteria monitoringObjectCriteria = new MonitoringObjectGenericCriteria(string.Format("PrincipalName = '{0}'", computerFQDN));

            try
            {
                IObjectReader<MonitoringObject> reader =  mg.EntityObjects.GetObjectReader<MonitoringObject>(monitoringObjectCriteria, windowsComputerClass, ObjectQueryOptions.Default);
                List<MonitoringObject> monitoringObjects = new List<MonitoringObject>();
                monitoringObjects.AddRange (reader);

                return (monitoringObjects[0]);
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new InvalidOperationException("Could not find the specified monitoring object: " + computerFQDN);
            }
        }
    }
}

See Also

Other Resources

Automating Operations Manager Administration