次の方法で共有


正常性ルールを作成する

最終更新日: 2009年10月16日

適用対象: SharePoint Foundation 2010

Microsoft SharePoint Foundation 2010 では、2 つの抽象クラス SPHealthAnalysisRule または SPRepairableHealthAnalysisRule のどちらかから継承する具象サブクラスのコードを記述することで、正常性ルールを作成します。

両方の基本クラスで、SummaryExplanationRemedyCategory、および ErrorLevel の各プロパティのほか、Check() メソッドを実装する必要があります。カスタム ルールについては、ErrorLevel を Success または RuleExecutionFailure に設定しないでください。これは内部の使用のみに限ります。

Check メソッドを使用して、ルールの検索対象になっている問題を検出します。ルールが SPRepairableHealthAnalysisRule クラスから継承している場合は、Repair() メソッドを実装する必要もあります。ここには、Check メソッドによって検出された問題を修正するコードを配置します。

ルールをインストールして自動的に実行できるようにするには、AutomaticExecutionParameters プロパティをオーバーライドして実装する必要もあります。このプロパティは、ルールを実行するタイマー ジョブの既定の設定を提供します。

正常性ルールを作成するには

  1. [スタート] メニューで Visual Studio を右クリックし [管理者として実行] を選択して、Visual Studio を管理者として実行します。

  2. 新しいクラス ライブラリ プロジェクトを作成します。

    [新しいプロジェクト] ダイアログで、[Visual C#] または [Visual Basic] を選択し、[Windows] を選択して、[クラス ライブラリ] テンプレートを選択します。

    プロジェクトに付けた名前がアセンブリの既定の名前空間として使用されます。複数の正常性ルールを同じアセンブリに展開できます。したがって、それに応じてプロジェクトに名前を付けることを考慮する必要があります。たとえば、CompanyNameHealthRules という名前を付けます。

  3. アセンブリに署名します。

    [ソリューション エクスプローラー] で、プロジェクト名を右クリックし、[プロパティ] をクリックします。プロパティ シートで、[署名] をクリックし、[アセンブリの署名] をオンにします。厳密な名前のキー ファイルに移動するか、新しいファイルを作成します。

    この手順が必要なのは、アセンブリは、SharePoint Health Analyzer に登録する前にすべてのマシンのグローバル アセンブリ キャッシュ (GAC) にインストールされていなければならないからです。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] を選択し、[OK] をクリックします。

  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) メンバーを実装します。詳細については、SPHealthAnalysisRule クラスおよび SPRepairableHealthAnalysisRule クラスのドキュメントを参照してください。

  9. AutomaticExecutionParameters プロパティをオーバーライドして実装します。

    次の例は、ファーム内のすべてのサーバーで 1 時間ごとに実行されるルールのこのプロパティの実装を示しています。

    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. ルールをコンパイルしてテストします。

ファーム管理者によるインストールの新しい正常性ルールの準備については、「[方法] 正常性ルールを登録するフィーチャーを作成する」を参照してください。

次の例は、ファームのすべてのサーバーを 1 日 1 回チェックして、修正されたディスクの空き容量が不足していないかどうかを確認する正常性ルールを示しています。

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

関連項目

タスク

[方法] 開発中に正常性ルールをテストする

[方法] 正常性ルールを登録するフィーチャーを作成する

[方法] ソリューション パッケージで正常性ルールを展開する