-
Modulo HyperV
Hola,
Cuando empece a utilizar pandora hace unos meses hice unos modulos para monitorizar maquinas virtuales en sevidores Hyper-V, ahora los he refundido en uno solo, y antes de subirlos a la libreria quisiera exponer la estrategia y diseño seguidos por si a alguien se le ocurre algo.
He intentado documentar en el modulo la informacion necesaria, pero en cualquier caso esta es:
En la version 3 de HyperV ha aparecido la replicacion de maquinas, por ello determinamos en primer lugar en que version de HyperV estamos, ya que tambien se utiliza un ‘Namespace’ de WMI diferente, segun la version del sistema operativo.
Si estamos en HyperV 1 o 2 (Win2008,Win2008R2) simplemente veremos el estado de una maquina.
Si estamos en HyperV 3 (Win2012) y una maquina esta replicandose lo que queremos saber es si su estado de replica es normal, para ello nos basta con saber su estado de replica en el host donde se ejecuta y no hace falta ver el estado en el host donde se esta replicando, en el modulo esta comentado y se puede activar si se desea. Si la maquina no tiene replica, pues monitorizamos su estado.He intentado poner los valores de cadena para estado Warning/Critical pero al parecer no esta soportado, se tendra que hacer a mano.
Esta probado en 2 Host con Windows Server 2012 Standard, con maquinas replicandose, y otro con HyperV 2008 R2.
Os pego el codigo.
Un saludo.‘ MonitorHyperV
‘ Check Hyper-V version and virtual machines status.
‘ Controla la version de Hyper-V y el estado de las maquinas virtuales
‘ Josep A. Ferrando ([email protected]) 2013Option Explicit
On Error Resume Next‘ Variables
Dim strComputer, objWMIService, objItem, colItems
Dim i, HyperV
Dim Language
Dim strEnabled, StrDisabled, strOther
Dim strNormal, strWarning, strCritical
Dim strDescPre1, strDescPos1, strDescPre2, strDescPos2‘Start of module / Inicio del modulo
Language = “I”
Initialize
Monitor‘End of module / Fin del modulo
‘Subprograms & Functions / Subprogramas y Funciones
Sub Initialize
‘ Initialize configuration values / variables
‘ Inicializar valores / variables de configuracionstrComputer = “.” ‘Local Computer / Ordenador local
Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootCIMV2”)
Set colItemS = objWMIService.ExecQuery(“SELECT Caption, Version FROM Win32_OperatingSystem”)‘Obtain Operating System Version / Obtener Version Sistema Operativo
‘Windows 2008Â Â – HyperV Server 2008Â Â – 6.0.xxxx = HyperV 1.0
‘Windows 2008 R2 – HyperV Server 2008 R2 – 6.1.xxxx = HyperV 2.0
‘Windows 2012Â Â – HyperV Server 2012Â Â – 6.2.xxxx = HyperV 3.0For Each objItem in colItems
Select Case Left(objItem.Version , 3)
Case “6.2”
HyperV = 3
case Else
HyperV = 2
End Select
‘Debug
‘Wscript.Echo “———————————–”
‘Wscript.Echo “Op. System  : ” & objItem.Caption
‘Wscript.Echo “OS Version  : ” & objItem.Version
‘Wscript.Echo “HyperV Version: ” & HyperV
‘Wscript.Echo “———————————–”
NextSelect Case Language
Case “I”
strEnabled = “Enabled”
strDisabled = “Disabled”
strOther  = “Other”
strNormal = “Normal”
strWarning = “Warning”
strCritical = “Critical”
strDescPre1 = “HyperV machine ”
strDescPos1 = ” status”
strDescPre2 = “HyperV machine ”
strDescPos2 = ” replication health”
Case “E”
strEnabled = “Encendida”
strDisabled = “Apagada”
strOther  = “Otro”
strNormal = “Normal”
strWarning = “Advertencia”
strCritical = “CrÃtico”
strDescPre1 = “Estado máquina HyperV ”
strDescPos1 = “”
strDescPre2 = “Estado replicación máquina HyperV ”
strDescPos2 = “”
Case “V”
strEnabled = “Encesa”
strDisabled = “Apagada”
strOther  = “Altre”
strNormal = “Normal”
strWarning = “Advertència”
strCritical = “CrÃtic”
strDescPre1 = “Estat mà quina HyperV ”
strDescPos1 = “”
strDescPre2 = “Estat replicació mà quina HyperV ”
strDescPos2 = “”
End SelectEnd Sub
Sub Monitor
Select Case HyperV
Case 2
‘ HyperV 1.0/2.0 has no replication , then monitor the state of machines
‘ HyperV 1.0/2.0 no tiene replicacion, entonces monitorizamos el estado de las maquinas
‘ EnabledState values / Valores EnabledState – http://msdn.microsoft.com/en-us/library/hh850116(v=vs.85)
‘ 0 Unknown
‘ 1 Other
‘ 2 Enabled
‘ 3 Disabled
‘ 4 Shutting Down
‘ 5 Not Applicable
‘ 6 Enabled but Offline
‘ 7 In test
‘ 8 Deferred
‘ 9 Quiesce
’10 StartingSet objWMIService = GetObject(“winmgmts:\” & strComputer & “rootvirtualization”)
Set colItems = objWMIService.ExecQuery( “SELECT * FROM Msvm_ComputerSystem”,,48)For Each objItem in colItems
‘Wscript.Echo “- DEBUG ———————————-”
‘Wscript.Echo “HyperV : ” & HyperV & objItem.ElementName
Wscript.Stdout.WriteLine “” ”
Wscript.Stdout.WriteLine ” ”
Wscript.Stdout.WriteLine ” ”
Wscript.Stdout.WriteLine ” ”
Select Case objItem.EnabledState
Case 2 : Wscript.Stdout.WriteLine ”  ”
Case 3 : Wscript.Stdout.WriteLine ”  ”
Case Else
Wscript.Stdout.WriteLine ”  ”
End Select
Wscript.Stdout.WriteLine “
NextCase 3
‘ HyperV 3.0 has replication, then monitor replication status of machines
‘ that are in these host (Primary) and replicated in other host (Recovery)
‘ HyperV 3.0 tiene replicacion, entonces monitorizamos el estado de la replicacion de las maquinas
‘ que estan en este host (Primary) y replicandose en otro host (Recovery)
‘ If replication is not enabled, then monitor the state of machines
‘ Si no tienen la replicacion habilitada monitorizamos su estado
‘ ReplicationMode values / Valores ReplicationMode – http://msdn.microsoft.com/en-us/library/hh850116(v=vs.85)
‘ 0 None
‘ 1 Primary
‘ 2 Recovery
‘ 3 Test replication
‘ ReplicationHealth
‘ 0 Not applicable
‘ 1 Ok
‘ 2 Warning
‘ 3 CriticalSet objWMIService = GetObject(“winmgmts:\” & strComputer & “rootvirtualizationv2”)
Set colItems = objWMIService.ExecQuery( “SELECT * FROM Msvm_ComputerSystem”,,48)
For Each objItem in colItems
‘Wscript.Echo “- DEBUG ———————————-”
‘Wscript.Echo “HyperV : ” & HyperV & ” RMode : ” & objItem.ReplicationMode & ” ” & objItem.ElementName
Select Case objItem.ReplicationMode
Case 1 ‘ Primary
Wscript.StdOut.WriteLine “” ”
Wscript.StdOut.WriteLine ” ”
Wscript.StdOut.WriteLine ” ”
Wscript.Stdout.WriteLine ” ”
Select Case objItem.ReplicationHealth
Case 2 : Wscript.Stdout.WriteLine ”  ”
Case 3 : Wscript.Stdout.WriteLine ”  ”
Case Else
Wscript.Stdout.WriteLine ”  ”
End Select
‘Wscript.Stdout.WriteLine ” ”
‘Wscript.Stdout.WriteLine ” ”
Wscript.StdOut.WriteLine “
Case 2 ‘ Secondary Monitor on Host with Primary
‘Wscript.StdOut.WriteLine “” ”
‘Wscript.StdOut.WriteLine ” ”
‘Wscript.StdOut.WriteLine ” ”
‘Wscript.Stdout.WriteLine ” ”
‘Select Case objItem.ReplicationHealth
‘ Case 2 : Wscript.Stdout.WriteLine ”  ”
‘ Case 3 : Wscript.Stdout.WriteLine ”  ”
‘ Case Else
‘ Wscript.Stdout.WriteLine ”  ”
‘End Select
‘Wscript.Stdout.WriteLine ” ”
‘Wscript.Stdout.WriteLine ” ”
‘Wscript.StdOut.WriteLine “
Case 0 ‘ None
Wscript.Stdout.WriteLine “” ”
Wscript.Stdout.WriteLine ” ”
Wscript.Stdout.WriteLine ” ”
Wscript.Stdout.WriteLine ” ”
Select Case objItem.EnabledState
Case 2 : Wscript.Stdout.WriteLine ”  ”
Case 3 : Wscript.Stdout.WriteLine ”  ”
Case Else
Wscript.Stdout.WriteLine ”  ”
End Select
Wscript.Stdout.WriteLine “
Case 3 ‘ Testing
Case Else
‘ Monitor Host
Wscript.Stdout.WriteLine “” ”
Wscript.Stdout.WriteLine ” ”
Wscript.Stdout.WriteLine ” ”
Wscript.Stdout.WriteLine ” ”
Select Case objItem.EnabledState
Case 2 : Wscript.Stdout.WriteLine ”  ”
Case 3 : Wscript.Stdout.WriteLine ”  ”
Case Else
Wscript.Stdout.WriteLine ”  ”
End Select
Wscript.Stdout.WriteLine “
End Select
NextCase Else
End Select
End Sub