Recently I wanted to add better monitoring of the Primary and Extended Hyper-V replication of our environment. Since our primary monitoring tool is PRTG I went looking for a sensor for this. There is no built in sensor, however I did find this custom PowerShell script on the PRTG forums; Monitoring Hyper-V Replication | Paessler Knowledgebase. This script pulls the current state of the Hyper-V primary replication. I was able to also differentiate between Primary and Extended replication by adding an IF statement to the script.
if ($Name.ReplicationRelationshipType -like "Simple") --or-- if ($Name.ReplicationRelationshipType -like "Extended")
This provided the basic monitoring that I needed, but it has a couple of issues. Hyper-V doesn’t consider the replication in error until 20% of the cycles for monitored time period. I wanted to get an earlier warning and also see the trends. Digging around some more, I found the Measure-VMReplication command which gives the number of successful and missed replication cycles. This allows me to retrieve exactly the data I needed. One wrinkle is that instead of just getting the “Simple” or “Extended” replication from the target, I found I have to go to the replication source for the correct counts. To do this I needed to be able to provide a list of hosts in the parameter.
First retrieve the parameter, start the XML, and split the parameter into the list of servers
#Takes list of servers in quotes delimited by comma as a parameter Param( [string]$Hypervservers ) $ErrorActionPreference = "SilentlyContinue" #start writing in XML Write-Host "<prtg>" #Split the parameter into list of servers $ServerList = $Hypervservers.Split(",")
The script then loops through each of the servers to retrieve the statistics for each VM. One problem that I had is that the remote PowerShell session would not retrieve the statistics form a different host. I solved this by opening a new session for each host. To have the script retrieve Extended instead of Primary replication, change the ReplicationRelationshipType to “Extended”.
foreach ($Hypervserver in $ServerList) { #I had issues getting the remote server data unless I opened the remote session #to the specific server I was getting the data about. #Open a new remote PS session $Session = New-PSSession -computername $Hypervserver Import-PSSession $Session -Module Hyper-V -warningaction "SilentlyContinue" #Get the replication data $VMReplication = Measure-VMReplication -computername $Hypervserver -ReplicationRelationshipType Simple | select VMname,SuccessfulReplicationCount,MissedReplicationCount #Write the data for each VM as a PRTG channel foreach ($Name in $VMReplication) { #I couldn't get PRTG to display the percent with only 2 decimal places, so I rounded here $AvgSuccess = [math]::Round(($Name.SuccessfulReplicationCount / ($Name.SuccessfulReplicationCount + $Name.MissedReplicationCount))*100,0) $VM = $Name.VMName #Write the Channel Write-Host "<result>" "<channel>$VM</channel>" "<Unit>Percent</Unit>" "<Float>1</Float>" "<DecimalMode>2</DecimalMode>" "<value>$AvgSuccess</value>" "<LimitMinWarning>90</LimitMinWarning>" "<LimitMinError>80</LimitMinError>" "<LimitMode>1</LimitMode>" "</result>" } #Close the remote PS session so it can be opened to the next host Remove-PSSession $Session } #Close the XML Write-Host "</prtg>" #Exit code 0 so PRTG knows the script ran successfully Exit 0
After setting up the Advanced Custom Sensor I can now track on a per-VM basis the replication results. Download the full script here: www.xbyte.com/replicationstatus.zip
Image may be NSFW.
Clik here to view.
Clik here to view.
