5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hyper-V レプリカ / レプリケーション 状況監視(定期メール送信)Replication Status Check

Posted at

This article proposes a way to monitor Windows Server Hyper-V replication status by sending periodic email notifications rather than its GUI console or purchasing expensive monitoring software.

Windows Server Hyper-Vのレプリカですが、DR / BCP 対策として有用な反面、監視をしていないとレプリカが上手くできておらず本当に必要な時に意味をなさないことがあります。監視ツールを購入するお金が無い際に参考にしましたタスクスケジューラーでバッチを仕掛けてレプリカの正常、異常をメール受信で検知するプログラムです。Qiitaに無いようでしたので一部ネットで見つけたプログラムの中身を編集し記事にいたします。毎朝出社前に自動実行し出社時の定期タスクとしてチェックしておりました。またホストサーバー再起動等の後にも実行していました。当時は英文の情報も見当たらず、参考資料先の資料を発見したことで本当に助かりました。

参考資料:
https://yuzo-6str-bass.blogspot.com/2015/01/

以下の例では二つのファイルを同一フォルダ(Hyper-Vホスト機)に設置し、hyper-v-replica-monitor.batを実行させることでメール送信できる事例です。

hyper-v-replica-monitor.bat

powershell -NoProfile -ExecutionPolicy ByPass .\hyper-v-replica-monitor.ps1

hyper-v-replica-monitor.ps1

# MailSender Function
Function Send-MailNotification($Subject, $Body) {
 $EmailFrom = "something@something.test" #送信元メールアドレス。ダミー可
 $EmailTo ="something@something.test" #宛先メールアドレス
 $SMTPServer = "メールサーバ名" #SMTP サーバー
 $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) #ポート番号
 $SMTPClient.EnableSsl = $false
 $SMTPClient.Send($EmailFrom,$EmailTo,$Subject,$Body)
# $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");  #メール送信認証する場合
}

# Define Hyper-V Hosts
# 監視対象にしたいHyper-V を配列で定義します
$HVHosts = @("SERVERNAME1","SERVERNAME2")

# 結果を受け取るために、ArrayList を定義します。
$ReplicationFailureList = New-Object System.Collections.ArrayList
$ReplicationSuccessList = New-Object System.Collections.ArrayList

foreach ($HVHost in $HVHosts) {
 # レプリケーションの状態を一覧で取得
    $Results = $null
    # Replication の状態を取得します。
 $Results = Get-VMReplication -ComputerName $HVHost | where {($_.Mode -eq "Primary") -and ($_.Health -ne "Normal")}
    foreach ($Result in $Results) {
        $ReplicationFailureList.Add($Result)
    }
}

foreach ($HVHost in $HVHosts) {
 # レプリケーションの状態を一覧で取得
     $Results2 = $null
    # Replication の状態を取得します。
 $Results2 = Get-VMReplication -ComputerName $HVHost | where {($_.Mode -eq "Primary") -and ($_.Health -eq "Normal")}
    foreach ($Result2 in $Results2) {
        $ReplicationSuccessList.Add($Result2)
    }
}

# レプリケーション失敗数が 1 以上の場合は統計情報のリセットとメール送信
if ($ReplicationFailureList.Count -gt 0) {
    $body = $null

    # List Header
    # そのままテキストファイルに出力するばあいは、リストを Out-File してやればよいのですが、メール本文に載せるとなるとうまくキャストされないため、明示的に String 形式で作成してやります。
    $body = "VMName`tState`tHealth`tPrimaryServer`tReplicaServer`n"
    $body+= "------`t-----`t------`t-------------`t-------------"
    foreach ($ReplicationFailureVM in $ReplicationFailureList) {

        # Reset Replication Statistics
        # 統計情報は残しておくと通知が延々と続くため、リセットします。
        #Reset-VMReplicationStatistics -ComputerName $ReplicationFailureVM.ComputerName -VMName $ReplicationFailureVM.VMName

        # レプリケーションを再開して同期させます。
        #Resume-VMReplication -ComputerName $ReplicationFailureVM.ComputerName -VMName $ReplicationFailureVM.VMName

        # レプリケーションのレコードを String  に変換してやります。
        # Changing Replication Record format from Object to String
        $vmstat = $ReplicationFailureVM.VMName + "`t" + $ReplicationFailureVM.State + "`t" + $ReplicationFailureVM.Health + "`t" + $ReplicationFailureVM.PrimaryServer + "`t" + $ReplicationFailureVM.ReplicaServer
        # Add to record
        # 見やすくするために改行コードを加えて表にします。
        $body += "`n" + $vmstat
    }
    $Subject = "HYPER-V REPLICATION ERROR / HYPER-V レプリカ異常結果"
    Send-MailNotification -Subject $Subject -Body $body
}

# レプリケーション成功数が 1 以上の場合は統計情報のリセットとメール送信
if ($ReplicationSuccessList.Count -gt 0) {
    $body = $null

    # List Header
    # そのままテキストファイルに出力するばあいは、リストを Out-File してやればよいのですが、メール本文に載せるとなるとうまくキャストされないため、明示的に String 形式で作成してやります。
    $body = "VMName`tState`tHealth`tPrimaryServer`tReplicaServer`n"
    $body+= "------`t-----`t------`t-------------`t-------------"
    foreach ($ReplicationSuccessVM in $ReplicationSuccessList) {

        # Reset Replication Statistics
        # 統計情報は残しておくと通知が延々と続くため、リセットします。
        Reset-VMReplicationStatistics -ComputerName $ReplicationSuccessVM.ComputerName -VMName $ReplicationSuccessVM.VMName

        # レプリケーションを再開して同期させます。
        #Resume-VMReplication -ComputerName $ReplicationSuccessVM.ComputerName -VMName $ReplicationSuccessVM.VMName

        # レプリケーションのレコードを String  に変換してやります。
        # Changing Replication Record format from Object to String

        $vmstat = $ReplicationSuccessVM.VMName + "`t" + $ReplicationSuccessVM.State + "`t" + $ReplicationSuccessVM.Health + "`t" + $ReplicationSuccessVM.PrimaryServer + "`t" + $ReplicationSuccessVM.ReplicaServer

        # Add to record
        # 見やすくするために改行コードを加えて表にします。
        $body += "`n" + $vmstat
    }
    $Subject = "HYPER-V REPLICATION SUCCESS / HYPER-V レプリカ正常結果"
    Send-MailNotification -Subject $Subject -Body $body
}

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?