如何:创建运行状况规则

上次修改时间: 2009年10月16日

适用范围: SharePoint Foundation 2010

在 Microsoft SharePoint Foundation 2010 中,您可以通过为继承自下面两个抽象类之一的实际子类编写代码来创建运行状况规则:SPHealthAnalysisRuleSPRepairableHealthAnalysisRule

这两个基类都要求您实现 SummaryExplanationRemedyCategoryErrorLevel 属性以及 Check() 方法。对于自定义规则,请不要将 ErrorLevel 设置为 Success 或 RuleExecutionFailure,这两个值仅供内部使用。

使用 Check 方法可检测您的规则旨在查找的问题。如果您的规则继承自 SPRepairableHealthAnalysisRule 类,则还必须实现 Repair() 方法。这是用于放置纠正 Check 方法所发现问题的代码的位置。

如果您打算安装规则以使其自动运行,则还应该重写并实现 AutomaticExecutionParameters 属性。该属性为运行该规则的计时器作业提供默认设置。

创建运行状况规则

  1. 右键单击"开始"菜单中的 Visual Studio,然后选择"以管理员身份运行",以管理员身份打开该程序。

  2. 新建一个类库项目。

    在"新建项目"对话框中,选择"Visual C#"或"Visual Basic",然后选择"Windows"并选择"类库"模板。

    为项目指定的名称将用作程序集的默认命名空间。可以在同一程序集中部署多个运行状况规则,因此您应考虑相应地命名项目:例如,命名为 CompanyNameHealthRules。

  3. 为程序集签名。

    在"解决方案资源管理器"中,右键单击项目名称,然后选择"属性"。在属性表上,单击"签名",然后选中"为程序集签名"。浏览以找到强名称密钥文件或创建一个新文件。

    必须执行该步骤,因为程序集必须安装在所有计算机上的全局程序集缓存 (GAC) 中,然后才能注册到 SharePoint 运行状况分析器 中。必须使用强名称对安装在 GAC 中的程序集进行签名。有关详细信息,请参阅如何:使用强名称为程序集签名

  4. 将类名称从 Class1 更改为描述您的规则将检测的错误的名称。

    在"解决方案资源管理器"中,右键单击文件 Class1.cs 或 Class1.vb,选择"重命名",然后键入一个新名称。当系统询问您是否要重命名对 Class1 的所有引用时,单击"是"。

  5. 添加对 Microsoft.SharePoint.dll 的引用。

    在"解决方案资源管理器"中,右键单击项目名称,然后选择"添加引用..."。在"添加引用"对话框中,浏览到 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI\Microsoft.SharePoint.dll。选择"Microsoft.SharePoint.dll",然后单击"确定"。

  6. Microsoft.SharePoint.Administration.Health 命名空间添加 using 语句(在 Visual Basic 中为 Imports)。

    using Microsoft.SharePoint.Administration.Health;
    
    Imports Microsoft.SharePoint.Administration.Health
    
  7. 修改类的声明使其从 SPHealthAnalysisRule 类或 SPRepairableHealthAnalysisRule 类继承。

    • 如果使用 Visual C#,请导航到类声明。在类名称之后,键入一个冒号,再键入您的规则应从其继承的类的名称。然后按 Shift+Alt+F10。当系统询问您是否要实现该类时,按 Enter。基类中标记为 abstract 的成员的存根将被添加到派生类中。

      此时,您的类库的代码应类似以下示例。

      using Microsoft.SharePoint.Administration.Health;
      
      namespace Samples.HealthRules
      {
          public class DiskDriveAlmostFull: SPHealthAnalysisRule
          {
              public override SPHealthCategory Category
              {
                  get { throw new System.NotImplementedException(); }
              }
      
              public override SPHealthCheckStatus Check()
              {
                  throw new System.NotImplementedException();
              }
      
              public override SPHealthCheckErrorLevel ErrorLevel
              {
                  get { throw new System.NotImplementedException(); }
              }
      
              public override string Explanation
              {
                  get { throw new System.NotImplementedException(); }
              }
      
              public override string Remedy
              {
                  get { throw new System.NotImplementedException(); }
              }
      
              public override string Summary
              {
                  get { throw new System.NotImplementedException(); }
              }
          }
      }
      
    • 如果使用 Visual Basic,请在类声明下面的第一个空白行中键入 Inherits 和您的规则应从其继承的类的名称。然后按 Enter。基类中标记为 MustOverride 的成员的存根将被添加到派生类中。

      此时,您的类库的代码应类似以下示例。

      Imports Microsoft.SharePoint.Administration.Health
      
      Public Class DiskDriveAlmostFull
          Inherits SPHealthAnalysisRule
      
      
          Public Overrides ReadOnly Property Category() As Microsoft.SharePoint.Administration.Health.SPHealthCategory
              Get
      
              End Get
          End Property
      
          Public Overrides Function Check() As Microsoft.SharePoint.Administration.Health.SPHealthCheckStatus
      
          End Function
      
          Public Overrides ReadOnly Property ErrorLevel() As Microsoft.SharePoint.Administration.Health.SPHealthCheckErrorLevel
              Get
      
              End Get
          End Property
      
          Public Overrides ReadOnly Property Explanation() As String
              Get
      
              End Get
          End Property
      
          Public Overrides ReadOnly Property Remedy() As String
              Get
      
              End Get
          End Property
      
          Public Overrides ReadOnly Property Summary() As String
              Get
      
              End Get
          End Property
      End Class
      
  8. 编写代码以实现该类的 abstract(在 Visual Basic 中为 MustOverride)成员。有关详细信息,请参阅 SPHealthAnalysisRuleSPRepairableHealthAnalysisRule 类的相关文档。

  9. 重写并实现 AutomaticExecutionParameters 属性。

    以下示例演示了如何为应每小时在服务器场中的所有服务器上运行一次的规则实现该属性。

    public override SPHealthAnalysisRuleAutomaticExecutionParameters AutomaticExecutionParameters
    {
        get
        {
            SPHealthAnalysisRuleAutomaticExecutionParameters retval =
                new SPHealthAnalysisRuleAutomaticExecutionParameters();
            retval.Schedule = SPHealthCheckSchedule.Hourly;
            retval.Scope = SPHealthCheckScope.All;
            retval.ServiceType = typeof(SPTimerService);
            return retval;
        }
    }
    
    Public Overrides ReadOnly Property AutomaticExecutionParameters() As Microsoft.SharePoint.Administration.Health.SPHealthAnalysisRuleAutomaticExecutionParameters
        Get
            Dim retval As SPHealthAnalysisRuleAutomaticExecutionParameters = _
                New SPHealthAnalysisRuleAutomaticExecutionParameters()
            retval.Schedule = SPHealthCheckSchedule.Hourly
            retval.Scope = SPHealthCheckScope.All
            retval.ServiceType = Type.GetType("Microsoft.SharePoint.Administration.SPTimerService")
            Return retval
        End Get
    End Property
    
  10. 编译和测试规则。

有关准备新的运行状况规则供服务器场管理员安装的信息,请参阅如何:创建用于注册运行状况规则的功能

示例

以下示例是一个运行状况规则,该规则每天检查一次服务器场中的每台服务器,以查看已修复磁盘的可用空间是否不足。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;

namespace Samples.HealthRules
{
    public class DiskDriveAlmostFull: SPHealthAnalysisRule
    {
        private List<DriveInfo> m_FailingDrives = new List<DriveInfo>();

        public override string Summary
        {
            get { return "One or more disk drives are running out of free space."; }
        }

        public override string Explanation
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (DriveInfo drive in this.m_FailingDrives)
                {
                    sb.AppendFormat("{0} ({1}), ",
                        drive.VolumeLabel, drive.Name);
                }
                if (sb.Length > 2)
                    sb.Remove(sb.Length - 2, 2);

                return string.Concat(
                    "The following drives on the failing servers have less than 10GB free: ", sb);
            }
        }

        public override string Remedy
        {
            get { return "Examine the failing servers and delete old logs or free space on the drives."; }
        }
        
        public override SPHealthCategory Category
        {
            get { return SPHealthCategory.Availability; }
        }

       public override SPHealthCheckErrorLevel ErrorLevel
        {
            get { return SPHealthCheckErrorLevel.Error; }
        }

        public override SPHealthAnalysisRuleAutomaticExecutionParameters AutomaticExecutionParameters
        {
            get
            {
                SPHealthAnalysisRuleAutomaticExecutionParameters retval =
                    new SPHealthAnalysisRuleAutomaticExecutionParameters();
                retval.Schedule = SPHealthCheckSchedule.Daily;
                retval.Scope = SPHealthCheckScope.All;
                retval.ServiceType = typeof(SPTimerService);
                return retval;
            }
        }
        
        public override SPHealthCheckStatus Check()
        {
            const long bytesInGb = 1024 * 1024 * 1024;

            if (!SPFarm.Joined)
                throw new InvalidOperationException();

            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                try
                {
                    if (!(di.IsReady && di.DriveType == DriveType.Fixed))
                        continue;

                    if (di.TotalFreeSpace < 10 * bytesInGb)
                        this.m_FailingDrives.Add(di);
                }
                catch (IOException)
                {
                }
            }

            if (this.m_FailingDrives.Count == 0)
                return SPHealthCheckStatus.Passed;
            else
                return SPHealthCheckStatus.Failed;
        } 
    }
}
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Text
Imports Microsoft.SharePoint.Administration
Imports Microsoft.SharePoint.Administration.Health

Public Class DiskDriveAlmostFull
    Inherits SPHealthAnalysisRule

    Private m_FailingDrives As List(Of DriveInfo) = New List(Of DriveInfo)

    Public Overrides ReadOnly Property Summary() As String
        Get
            Return "One or more disk drives are running out of free space."
        End Get
    End Property

    Public Overrides ReadOnly Property Explanation() As String
        Get
            Dim sb As StringBuilder = New StringBuilder()
            Dim drive As DriveInfo
            For Each drive In Me.m_FailingDrives
                sb.AppendFormat("{0} ({1}), ", _
                    drive.VolumeLabel, drive.Name)
            Next
            If sb.Length > 2 Then
                sb.Remove(sb.Length - 2, 2)
            End If

            Return String.Concat( _
            "The following drives on the failing servers have less than 10GB free: ", sb)

        End Get
    End Property

    Public Overrides ReadOnly Property Remedy() As String
        Get
            Return "Examine the failing servers and delete old logs or free space on the drives."
        End Get
    End Property

    Public Overrides ReadOnly Property Category() As Microsoft.SharePoint.Administration.Health.SPHealthCategory
        Get
            Return SPHealthCategory.Availability
        End Get
    End Property

    Public Overrides ReadOnly Property ErrorLevel() As Microsoft.SharePoint.Administration.Health.SPHealthCheckErrorLevel
        Get
            Return SPHealthCheckErrorLevel.Error
        End Get
    End Property

    Public Overrides ReadOnly Property AutomaticExecutionParameters() As Microsoft.SharePoint.Administration.Health.SPHealthAnalysisRuleAutomaticExecutionParameters
        Get
            Dim retval As SPHealthAnalysisRuleAutomaticExecutionParameters = _
                New SPHealthAnalysisRuleAutomaticExecutionParameters()
            retval.Schedule = SPHealthCheckSchedule.Daily
            retval.Scope = SPHealthCheckScope.All
            retval.ServiceType = Type.GetType("Microsoft.SharePoint.Administration.SPTimerService")
            Return retval
        End Get
    End Property

    Public Overrides Function Check() As Microsoft.SharePoint.Administration.Health.SPHealthCheckStatus

        Dim bytesInGb As Long = 1024 * 1024 * 1024

        If Not SPFarm.Joined Then
            Throw New InvalidOperationException()
        End If

        Dim di As DriveInfo
        For Each di In DriveInfo.GetDrives()
            Try
                If Not (di.IsReady And di.DriveType = DriveType.Fixed) Then
                    Continue For
                End If

                If di.TotalFreeSpace < 10 * bytesInGb Then
                    Me.m_FailingDrives.Add(di)
                End If
            Catch ex As IOException

            End Try
        Next

        If Me.m_FailingDrives.Count = 0 Then
            Return SPHealthCheckStatus.Passed
        Else
            Return SPHealthCheckStatus.Failed
        End If
    End Function

End Class

请参阅

任务

如何:在开发过程中测试运行状况规则

如何:创建用于注册运行状况规则的功能

如何:使用解决方案包部署运行状况规则