Skip to content

Commit

Permalink
Adds new Hyper-V data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
LordHepipud committed Jul 24, 2023
1 parent fbd286f commit 8f7f7cd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#640](https://github.com/Icinga/icinga-powershell-framework/issues/640) Adds support to set the flag `-NoSSLValidation` for Cmdlets `icinga` and `Install-Icinga`, to ignore errors on self-signed certificates within the environment
* [#643](https://github.com/Icinga/icinga-powershell-framework/pull/643) Adds support for `-RebuildCache` flag on `icinga` cmd to rebuild component cache as well
* [#644](https://github.com/Icinga/icinga-powershell-framework/pull/644) Adds progress bar output to repository interaction (sync, update, new) instead of plain text output
* [#649](https://github.com/Icinga/icinga-powershell-framework/pull/649) Adds new basic data provider base for Hyper-V information

## 1.10.1 (2022-12-20)

Expand Down
110 changes: 110 additions & 0 deletions lib/provider/assets/hyperv/Get-IcingaProviderDataValuesHyperV.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
function Get-IcingaProviderDataValuesHyperV()
{
param (
[switch]$IncludeDetails = $FALSE
);

$HyperVData = New-IcingaProviderObject -Name 'Hyper-V';

# Check if the Hyper-V is installed. If not, we will simply return an empty object
if ($null -eq (Get-Service -Name 'vmms' -ErrorAction SilentlyContinue)) {
$HyperVData.FeatureInstalled = $FALSE;

return $HyperVData;
}

$HyperVData.Metrics | Add-Member -MemberType NoteProperty -Name 'ClusterData' -Value (New-Object PSCustomObject);
$HyperVData.Metrics | Add-Member -MemberType NoteProperty -Name 'BlackoutTimes' -Value (New-Object PSCustomObject);
$HyperVData.Metrics.BlackoutTimes | Add-Member -MemberType NoteProperty -Name 'Information' -Value (New-Object PSCustomObject);
$HyperVData.Metrics.BlackoutTimes | Add-Member -MemberType NoteProperty -Name 'Warning' -Value (New-Object PSCustomObject);
$HyperVData.Metrics.ClusterData | Add-Member -MemberType NoteProperty -Name 'NodeCount' -Value 1; # We always have at least 1 node
$HyperVData.Metrics.ClusterData | Add-Member -MemberType NoteProperty -Name 'VMList' -Value (New-Object PSCustomObject);
$HyperVData.Metrics.ClusterData.VMList | Add-Member -MemberType NoteProperty -Name 'Duplicates' -Value (New-Object PSCustomObject);
$HyperVData.Metrics.ClusterData.VMList | Add-Member -MemberType NoteProperty -Name 'VMs' -Value (New-Object PSCustomObject);

try {
if (Test-IcingaFunction 'Get-ClusterNode') {
$ClusterInformation = Get-ClusterNode -Cluster '.' -ErrorAction Stop;

$HyperVData.Metrics.ClusterData.NodeCount = $ClusterInformation.Count;
}

[array]$VMRessources = @();

if (Test-IcingaFunction 'Get-ClusterResource') {
[array]$VMRessources = Get-ClusterResource -Cluster '.' -ErrorAction Stop | Where-Object ResourceType -EQ 'Virtual Machine';
} else {
[array]$VMRessources = Get-VM -ErrorAction Stop;
}

if ($null -ne $VMRessources -And $VMRessources.Count -ne 0) {
foreach ($VMRessource in $VMRessources) {
if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.ClusterData.VMList.VMs -Name $VMRessource.Name) -eq $FALSE) {
$HyperVData.Metrics.ClusterData.VMList.VMs | Add-Member -MemberType NoteProperty -Name $VMRessource.Name -Value 1;
} else {
$HyperVData.Metrics.ClusterData.VMList.VMs.($VMRessource.Name) += 1;

if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.ClusterData.VMList.Duplicates -Name $VMRessource.Name) -eq $FALSE) {
$HyperVData.Metrics.ClusterData.VMList.Duplicates | Add-Member -MemberType NoteProperty -Name $VMRessource.Name -Value 0;
}
$HyperVData.Metrics.ClusterData.VMList.Duplicates.($VMRessource.Name) = $HyperVData.Metrics.ClusterData.VMList.VMs.($VMRessource.Name);
}
}
}

# Blackout Times
# => Info
[array]$InformationBlackoutTimes = Get-WinEvent -FilterHashtable @{ 'LogName'='Microsoft-Windows-Hyper-V-VMMS-Admin'; 'Id' = '20415'; } -MaxEvents 300 -ErrorAction SilentlyContinue;

if ($null -ne $InformationBlackoutTimes -Or $InformationBlackoutTimes.Count -ne 0) {
foreach ($event in $InformationBlackoutTimes) {
$XMLEventData = ([xml]$event.ToXml()).Event;

if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.BlackoutTimes.Information -Name $XMLEventData.UserData.VmlEventLog.Parameter0) -eq $FALSE) {
$EventObject = New-Object PSCustomObject;
$EventObject | Add-Member -MemberType NoteProperty -Name 'Timestamp' -Value $event.TimeCreated;
$EventObject | Add-Member -MemberType NoteProperty -Name 'BlackoutTime' -Value $XMLEventData.UserData.VmlEventLog.Parameter2;

$HyperVData.Metrics.BlackoutTimes.Information | Add-Member -MemberType NoteProperty -Name $XMLEventData.UserData.VmlEventLog.Parameter0 -Value $EventObject;
}
}
}

# Blackout Times
# => Warning
[array]$WarningBlackoutTimes = Get-WinEvent -FilterHashtable @{ 'LogName'='Microsoft-Windows-Hyper-V-VMMS-Admin'; 'Id' = '20417'; } -MaxEvents 300 -ErrorAction SilentlyContinue;

if ($null -ne $WarningBlackoutTimes -Or $WarningBlackoutTimes.Count -ne 0) {
foreach ($event in $WarningBlackoutTimes) {
$XMLEventData = ([xml]$event.ToXml()).Event;

if ((Test-PSCustomObjectMember -PSObject $HyperVData.Metrics.BlackoutTimes.Warning -Name $XMLEventData.UserData.VmlEventLog.Parameter0) -eq $FALSE) {
$EventObject = New-Object PSCustomObject;
$EventObject | Add-Member -MemberType NoteProperty -Name 'Timestamp' -Value $event.TimeCreated;
$EventObject | Add-Member -MemberType NoteProperty -Name 'BlackoutTime' -Value $XMLEventData.UserData.VmlEventLog.Parameter2;

[bool]$IsAcknowledged = $FALSE;

foreach ($InfoBlackoutTime in $HyperVData.Metrics.BlackoutTimes.Information.PSObject.Properties.Name) {
if ($InfoBlackoutTime -eq $XMLEventData.UserData.VmlEventLog.Parameter0) {
if($HyperVData.Metrics.BlackoutTimes.Information.$InfoBlackoutTime.Timestamp -gt $event.TimeCreated) {
$IsAcknowledged = $TRUE;
break;
}
}
}

if ($IsAcknowledged) {
continue;
}

$HyperVData.Metrics.BlackoutTimes.Warning | Add-Member -MemberType NoteProperty -Name $XMLEventData.UserData.VmlEventLog.Parameter0 -Value $EventObject;
}
}
}
} catch {
Exit-IcingaThrowException -ExceptionType 'Custom' -CustomMessage 'Hyper-V Error' -ExceptionThrown $_.Exception.Message -Force;
}

return $HyperVData;
}
15 changes: 8 additions & 7 deletions lib/provider/core/New-IcingaProviderObject.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ function New-IcingaProviderObject()
);

$ProviderObject = New-Object PSCustomObject;
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Name;
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (New-Object PSCustomObject);
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'Metrics' -Value (New-Object PSCustomObject);
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'MetricsOverTime' -Value (New-Object PSCustomObject);
$ProviderObject.MetricsOverTime | Add-Member -MemberType NoteProperty -Name 'MetricContainer' -Value (New-Object PSCustomObject);
$ProviderObject.MetricsOverTime | Add-Member -MemberType NoteProperty -Name 'Cache' -Value (New-Object PSCustomObject);
$ProviderObject.MetricsOverTime | Add-Member -MemberType NoteProperty -Name 'Compiled' -Value (New-Object PSCustomObject);
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Name;
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'FeatureInstalled' -Value $TRUE;
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'Metadata' -Value (New-Object PSCustomObject);
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'Metrics' -Value (New-Object PSCustomObject);
$ProviderObject | Add-Member -MemberType NoteProperty -Name 'MetricsOverTime' -Value (New-Object PSCustomObject);
$ProviderObject.MetricsOverTime | Add-Member -MemberType NoteProperty -Name 'MetricContainer' -Value (New-Object PSCustomObject);
$ProviderObject.MetricsOverTime | Add-Member -MemberType NoteProperty -Name 'Cache' -Value (New-Object PSCustomObject);
$ProviderObject.MetricsOverTime | Add-Member -MemberType NoteProperty -Name 'Compiled' -Value (New-Object PSCustomObject);

return $ProviderObject;
}

0 comments on commit 8f7f7cd

Please sign in to comment.