|
| 1 | +function Get-IcingaProviderDataValuesHyperV() |
| 2 | +{ |
| 3 | + param ( |
| 4 | + [switch]$IncludeDetails = $FALSE |
| 5 | + ); |
| 6 | + |
| 7 | + $HyperVData = New-IcingaProviderObject -Name 'Hyper-V'; |
| 8 | + |
| 9 | + # Check if the Hyper-V is installed. If not, we will simply return an empty object |
| 10 | + if ($null -eq (Get-Service -Name 'vmms' -ErrorAction SilentlyContinue)) { |
| 11 | + $HyperVData.FeatureInstalled = $FALSE; |
| 12 | + |
| 13 | + return $HyperVData; |
| 14 | + } |
| 15 | + |
| 16 | + $HyperVData.Metrics | Add-Member -MemberType NoteProperty -Name 'ClusterData' -Value (New-Object PSCustomObject); |
| 17 | + $HyperVData.Metrics | Add-Member -MemberType NoteProperty -Name 'BlackoutTimes' -Value (New-Object PSCustomObject); |
| 18 | + $HyperVData.Metrics.BlackoutTimes | Add-Member -MemberType NoteProperty -Name 'Information' -Value (New-Object PSCustomObject); |
| 19 | + $HyperVData.Metrics.BlackoutTimes | Add-Member -MemberType NoteProperty -Name 'Warning' -Value (New-Object PSCustomObject); |
| 20 | + $HyperVData.Metrics.ClusterData | Add-Member -MemberType NoteProperty -Name 'NodeCount' -Value 1; # We always have at least 1 node |
| 21 | + $HyperVData.Metrics.ClusterData | Add-Member -MemberType NoteProperty -Name 'VMList' -Value (New-Object PSCustomObject); |
| 22 | + $HyperVData.Metrics.ClusterData.VMList | Add-Member -MemberType NoteProperty -Name 'Duplicates' -Value (New-Object PSCustomObject); |
| 23 | + $HyperVData.Metrics.ClusterData.VMList | Add-Member -MemberType NoteProperty -Name 'VMs' -Value (New-Object PSCustomObject); |
| 24 | + |
| 25 | + try { |
| 26 | + if (Test-IcingaFunction 'Get-ClusterNode') { |
| 27 | + $ClusterInformation = Get-ClusterNode -Cluster '.' -ErrorAction Stop; |
| 28 | + |
| 29 | + $HyperVData.Metrics.ClusterData.NodeCount = $ClusterInformation.Count; |
| 30 | + } |
| 31 | + |
| 32 | + [array]$VMRessources = @(); |
| 33 | + |
| 34 | + if (Test-IcingaFunction 'Get-ClusterResource') { |
| 35 | + [array]$VMRessources = Get-ClusterResource -Cluster '.' -ErrorAction Stop | Where-Object ResourceType -EQ 'Virtual Machine'; |
| 36 | + } else { |
| 37 | + [array]$VMRessources = Get-VM -ErrorAction Stop; |
| 38 | + } |
| 39 | + |
| 40 | + if ($null -ne $VMRessources -And $VMRessources.Count -ne 0) { |
| 41 | + foreach ($VMRessource in $VMRessources) { |
| 42 | + if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.ClusterData.VMList.VMs -Name $VMRessource.Name) -eq $FALSE) { |
| 43 | + $HyperVData.Metrics.ClusterData.VMList.VMs | Add-Member -MemberType NoteProperty -Name $VMRessource.Name -Value 1; |
| 44 | + } else { |
| 45 | + $HyperVData.Metrics.ClusterData.VMList.VMs.($VMRessource.Name) += 1; |
| 46 | + |
| 47 | + if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.ClusterData.VMList.Duplicates -Name $VMRessource.Name) -eq $FALSE) { |
| 48 | + $HyperVData.Metrics.ClusterData.VMList.Duplicates | Add-Member -MemberType NoteProperty -Name $VMRessource.Name -Value 0; |
| 49 | + } |
| 50 | + $HyperVData.Metrics.ClusterData.VMList.Duplicates.($VMRessource.Name) = $HyperVData.Metrics.ClusterData.VMList.VMs.($VMRessource.Name); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + # Blackout Times |
| 56 | + # => Info |
| 57 | + [array]$InformationBlackoutTimes = Get-WinEvent -FilterHashtable @{ 'LogName'='Microsoft-Windows-Hyper-V-VMMS-Admin'; 'Id' = '20415'; } -MaxEvents 300 -ErrorAction SilentlyContinue; |
| 58 | + |
| 59 | + if ($null -ne $InformationBlackoutTimes -Or $InformationBlackoutTimes.Count -ne 0) { |
| 60 | + foreach ($event in $InformationBlackoutTimes) { |
| 61 | + $XMLEventData = ([xml]$event.ToXml()).Event; |
| 62 | + |
| 63 | + if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.BlackoutTimes.Information -Name $XMLEventData.UserData.VmlEventLog.Parameter0) -eq $FALSE) { |
| 64 | + $EventObject = New-Object PSCustomObject; |
| 65 | + $EventObject | Add-Member -MemberType NoteProperty -Name 'Timestamp' -Value $event.TimeCreated; |
| 66 | + $EventObject | Add-Member -MemberType NoteProperty -Name 'BlackoutTime' -Value $XMLEventData.UserData.VmlEventLog.Parameter2; |
| 67 | + |
| 68 | + $HyperVData.Metrics.BlackoutTimes.Information | Add-Member -MemberType NoteProperty -Name $XMLEventData.UserData.VmlEventLog.Parameter0 -Value $EventObject; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + # Blackout Times |
| 74 | + # => Warning |
| 75 | + [array]$WarningBlackoutTimes = Get-WinEvent -FilterHashtable @{ 'LogName'='Microsoft-Windows-Hyper-V-VMMS-Admin'; 'Id' = '20417'; } -MaxEvents 300 -ErrorAction SilentlyContinue; |
| 76 | + |
| 77 | + if ($null -ne $WarningBlackoutTimes -Or $WarningBlackoutTimes.Count -ne 0) { |
| 78 | + foreach ($event in $WarningBlackoutTimes) { |
| 79 | + $XMLEventData = ([xml]$event.ToXml()).Event; |
| 80 | + |
| 81 | + if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.BlackoutTimes.Warning -Name $XMLEventData.UserData.VmlEventLog.Parameter0) -eq $FALSE) { |
| 82 | + $EventObject = New-Object PSCustomObject; |
| 83 | + $EventObject | Add-Member -MemberType NoteProperty -Name 'Timestamp' -Value $event.TimeCreated; |
| 84 | + $EventObject | Add-Member -MemberType NoteProperty -Name 'BlackoutTime' -Value $XMLEventData.UserData.VmlEventLog.Parameter2; |
| 85 | + |
| 86 | + [bool]$IsAcknowledged = $FALSE; |
| 87 | + |
| 88 | + foreach ($InfoBlackoutTime in $HyperVData.Metrics.BlackoutTimes.Information.PSObject.Properties.Name) { |
| 89 | + if ($InfoBlackoutTime -eq $XMLEventData.UserData.VmlEventLog.Parameter0) { |
| 90 | + if($HyperVData.Metrics.BlackoutTimes.Information.$InfoBlackoutTime.Timestamp -gt $event.TimeCreated) { |
| 91 | + $IsAcknowledged = $TRUE; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ($IsAcknowledged) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + $HyperVData.Metrics.BlackoutTimes.Warning | Add-Member -MemberType NoteProperty -Name $XMLEventData.UserData.VmlEventLog.Parameter0 -Value $EventObject; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } catch { |
| 106 | + Exit-IcingaThrowException -ExceptionType 'Custom' -CustomMessage 'Hyper-V Error' -ExceptionThrown $_.Exception.Message -Force; |
| 107 | + } |
| 108 | + |
| 109 | + return $HyperVData; |
| 110 | +} |
0 commit comments