Skip to content

Commit

Permalink
Fixes snapshot plugin exception in case vdisk was removed from VM
Browse files Browse the repository at this point in the history
  • Loading branch information
LordHepipud committed Jun 1, 2023
1 parent d8f175b commit 5fc5fe5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
8 changes: 8 additions & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ documentation before upgrading to a new release.

Released closed milestones can be found on [GitHub](https://github.com/Icinga/icinga-powershell-hyperv/milestones?state=closed).

## 1.3.0 (2023-06-16)

[Issue and PRs](https://github.com/Icinga/icinga-powershell-hyperv/milestone/4?closed=1)

### Bugfixes

* [#67](https://github.com/Icinga/icinga-powershell-hyperv/issues/67) Fixes Hyper-V Snapshot plugin throwing an exception in case vdisks were deleted from disk or removed from the virtual machine object in Hyper-V Manager

## 1.2.0 (2022-08-30)

[Issue and PRs](https://github.com/Icinga/icinga-powershell-hyperv/milestone/3?closed=1)
Expand Down
38 changes: 22 additions & 16 deletions plugins/Invoke-IcingaCheckHyperVSnapshot.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,28 @@ function Invoke-IcingaCheckHyperVSnapshot()
)
);

$SnapshotMainCheck.AddCheck(
(
New-IcingaCheck `
-Name ([string]::Format('{0} {1} Total Snapshot Size', $virtualMachine.ElementName, $part)) `
-Value $Partition.TotalUsed `
-BaseValue $virtualMachine.CurrentUsage `
-Unit 'B' `
-MetricIndex $virtualMachine.ElementName `
-MetricName 'summary' `
-MetricTemplate 'hypervsnapshotdata'
).WarnOutOfRange(
$TotalSnapshotSizeWarning
).CritOutOfRange(
$TotalSnapshotSizeCritical
)
);
if ([string]::IsNullOrEmpty($virtualMachine.CurrentUsageError)) {
$SnapshotMainCheck.AddCheck(
(
New-IcingaCheck `
-Name ([string]::Format('{0} {1} Total Snapshot Size', $virtualMachine.ElementName, $part)) `
-Value $Partition.TotalUsed `
-BaseValue $virtualMachine.CurrentUsage `
-Unit 'B' `
-MetricIndex $virtualMachine.ElementName `
-MetricName 'summary' `
-MetricTemplate 'hypervsnapshotdata'
).WarnOutOfRange(
$TotalSnapshotSizeWarning
).CritOutOfRange(
$TotalSnapshotSizeCritical
)
);
} else {
$IcingaCheck = New-IcingaCheck -Name ([string]::Format('Snapshots: {0} for location "{1}" and virtual machine "{2}"', $virtualMachine.CurrentUsageError, $virtualMachine.Partition, $virtualMachine.ElementName)) -NoPerfData;
$IcingaCheck.SetUnknown() | Out-Null;
$SnapshotMainCheck.AddCheck($IcingaCheck);
}

$AverageSnapshotSize = [System.Math]::Truncate([System.Math]::Round($Partition.TotalUsed / $SnapshotsCount));
$CalculateTotalSnapshots = [System.Math]::Truncate([System.Math]::Round($Partition.FreeSpace / ([math]::Max($AverageSnapshotSize, 1)), 2));
Expand Down
30 changes: 20 additions & 10 deletions provider/vcomputer/Get-IcingaVirtualComputerInfo.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ function Get-IcingaVirtualComputerInfo()
'Path' = ([string]::Format('{0}{1}{2}', $snapshot.ConfigurationDataRoot, '\', $snapshot.ConfigurationFile));
};

if (Test-Path $SnapshotContent.Path -ErrorAction SilentlyContinue) {
$SnapshotSize = Get-ChildItem -Path $SnapshotContent.Path | Select-Object Length;
try {
$SnapshotSize = Get-ChildItem -Path $SnapshotContent.Path -ErrorAction Stop | Select-Object Length;
$SnapshotContent.Add('Error', $null);
} else {
$SnapshotSize = @{ 'Length' = 0};
$SnapshotContent.Add('Error', 'PermissionDenied');
} catch {
$SnapshotSize = @{ 'Length' = 0 };
$SnapshotContent.Add('Error', $_.Exception.GetType().Name);
}

$SnapshotContent.Add('Size', $SnapshotSize.Length);
Expand Down Expand Up @@ -284,6 +284,13 @@ function Get-IcingaVirtualComputerInfo()
# Sort our Snapshots based on the creation Time
$details.Snapshots.List = $details.Snapshots.List | ConvertTo-IcingaSecureSortedArray -MemberName 'CreationTime' -Descending;

# Add some base data to prevent the plugin from crashing
$details += @{
'Partition' = 'Unassigned';
'CurrentUsage' = 0;
'CurrentUsageError' = 'No virtual disk assigned to this virtual machine';
};

# We are going to loop through all available vm partitions
foreach ($vmPartition in $VmPartitionsPath) {
# We filter out the drive letter where the VMs are stored
Expand All @@ -305,12 +312,15 @@ function Get-IcingaVirtualComputerInfo()
'AutomaticAllocation' = $vmPartition.AutomaticAllocation;
'AutomaticDeallocation' = $vmPartition.AutomaticDeallocation;
'HostResource' = $vmPartition.HostResource;
'Partition' = $vmPath;
'CurrentUsage' = 'PermissionDenied';
};

if (Test-Path -Path ([string]$vmPartition.HostResource)) {
$details.CurrentUsage = (Get-Item -Path ([string]$vmPartition.HostResource) | Select-Object Length).Length;
$details.Partition = $vmPath;
$details.CurrentUsageError = '';

try {
$details.CurrentUsage = (Get-Item -Path ([string]$vmPartition.HostResource) -ErrorAction Stop | Select-Object Length).Length;
} catch {
$details.CurrentUsageError = $_.Exception.GetType().Name;
}

# If the partition doesn't exist in StorageOverCommit hashtable then add ones
Expand Down Expand Up @@ -372,7 +382,7 @@ function Get-IcingaVirtualComputerInfo()
$VComputerData.Resources.StorageOverCommit[$details.Partition].Bytes += ($details.DiskCapacity);
}

if ($details.CurrentUsage -ne 'PermissionDenied') {
if ([string]::IsNullOrEmpty($details.CurrentUsageError)) {
$CurrentVmsUsage += $details.CurrentUsage;
} else {
$AccessDeniedVms += $vcomputer.ElementName;
Expand Down

0 comments on commit 5fc5fe5

Please sign in to comment.