Skip to content

Commit 73fba17

Browse files
authored
Merge pull request #226 from Icinga:fix/add_type_classnames_and_added_check
Fix: Class names for Add-Type and check if class was already added Fixes class names of `Add-Type` for `Get-IcingaDiskAttributes` and `Get-IcingaUNCPathSize`. Also adds checks if the class was already added inside the context
2 parents e4b1451 + 2a951ed commit 73fba17

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1414
## Bugfixes
1515

1616
* [#224](https://github.com/Icinga/icinga-powershell-plugins/pull/224) Fixes `Invoke-IcingaCheckUsedPartitionSpace` label names, which might have caused conflicts with Graphite/InfluxDB, because of changed metric unit
17+
* [#226](https://github.com/Icinga/icinga-powershell-plugins/pull/226) Fixes class names of `Add-Type` for `Get-IcingaDiskAttributes` and `Get-IcingaUNCPathSize` and adds checks if the class was already added inside the session
1718

1819
### Enhancements
1920

provider/disks/Get-IcingaDiskAttributes.psm1

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,55 +34,57 @@ function Get-IcingaDiskAttributes()
3434
$DISK_ATTRIBUTE_OFFLINE = 0x0000000000000001;
3535
$DISK_ATTRIBUTE_READ_ONLY = 0x0000000000000002;
3636

37-
Add-Type -TypeDefinition @"
38-
using System;
39-
using System.IO;
40-
using System.Diagnostics;
41-
using System.Runtime.InteropServices;
37+
if ((Test-IcingaAddTypeExist -Type 'IcingaDiskAttributes') -eq $FALSE) {
38+
Add-Type -TypeDefinition @"
39+
using System;
40+
using System.IO;
41+
using System.Diagnostics;
42+
using System.Runtime.InteropServices;
4243
43-
public static class kernel32 {
44-
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
45-
public static extern IntPtr CreateFile(
46-
[MarshalAs(UnmanagedType.LPTStr)] string filename,
47-
[MarshalAs(UnmanagedType.U4)] FileAccess access,
48-
[MarshalAs(UnmanagedType.U4)] FileShare share,
49-
IntPtr securityAttributes,
50-
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
51-
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
52-
IntPtr templateFile
53-
);
44+
public static class IcingaDiskAttributes {
45+
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
46+
public static extern IntPtr CreateFile(
47+
[MarshalAs(UnmanagedType.LPTStr)] string filename,
48+
[MarshalAs(UnmanagedType.U4)] FileAccess access,
49+
[MarshalAs(UnmanagedType.U4)] FileShare share,
50+
IntPtr securityAttributes,
51+
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
52+
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
53+
IntPtr templateFile
54+
);
5455
55-
public struct Icinga_Disk_Data {
56-
[MarshalAs(UnmanagedType.U4)]public UInt32 Version;
57-
[MarshalAs(UnmanagedType.U4)]public UInt32 Reserved1;
58-
[MarshalAs(UnmanagedType.U8)]public UInt64 Attributes;
59-
}
56+
public struct Icinga_Disk_Data {
57+
[MarshalAs(UnmanagedType.U4)]public UInt32 Version;
58+
[MarshalAs(UnmanagedType.U4)]public UInt32 Reserved1;
59+
[MarshalAs(UnmanagedType.U8)]public UInt64 Attributes;
60+
}
6061
61-
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
62-
public static extern bool DeviceIoControl(
63-
IntPtr hDevice,
64-
uint dwIoControlCode,
65-
IntPtr lpInBuffer,
66-
uint nInBufferSize,
67-
out Icinga_Disk_Data lpOutBuffer,
68-
uint nOutBufferSize,
69-
out uint lpBytesReturned,
70-
IntPtr lpOverlapped
71-
);
62+
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
63+
public static extern bool DeviceIoControl(
64+
IntPtr hDevice,
65+
uint dwIoControlCode,
66+
IntPtr lpInBuffer,
67+
uint nInBufferSize,
68+
out Icinga_Disk_Data lpOutBuffer,
69+
uint nOutBufferSize,
70+
out uint lpBytesReturned,
71+
IntPtr lpOverlapped
72+
);
7273
73-
[DllImport("kernel32.dll", SetLastError=true)]
74-
public static extern bool CloseHandle(IntPtr hObject);
75-
}
74+
[DllImport("kernel32.dll", SetLastError=true)]
75+
public static extern bool CloseHandle(IntPtr hObject);
76+
}
7677
"@
78+
}
7779

7880
[bool]$DiskOffline = $FALSE;
7981
[bool]$DiskReadOnly = $FALSE;
80-
$KernelHandle = [kernel32]::CreateFile($PhysicalDisk, 0, [System.IO.FileShare]::ReadWrite, [System.IntPtr]::Zero, [System.IO.FileMode]::Open, 0, [System.IntPtr]::Zero);
82+
$KernelHandle = [IcingaDiskAttributes]::CreateFile($PhysicalDisk, 0, [System.IO.FileShare]::ReadWrite, [System.IntPtr]::Zero, [System.IO.FileMode]::Open, 0, [System.IntPtr]::Zero);
8183

8284
if ($KernelHandle) {
83-
$DiskData = New-Object -TypeName Kernel32+Icinga_Disk_Data;
85+
$DiskData = New-Object -TypeName IcingaDiskAttributes+Icinga_Disk_Data;
8486
$Value = New-Object -TypeName UInt32;
85-
$Result = [kernel32]::DeviceIoControl($KernelHandle, $IOCTL_DISK_GET_DISK_ATTRIBUTES, [System.IntPtr]::Zero, 0, [ref]$DiskData, [System.Runtime.InteropServices.Marshal]::SizeOf($DiskData), [ref]$Value, [System.IntPtr]::Zero);
87+
$Result = [IcingaDiskAttributes]::DeviceIoControl($KernelHandle, $IOCTL_DISK_GET_DISK_ATTRIBUTES, [System.IntPtr]::Zero, 0, [ref]$DiskData, [System.Runtime.InteropServices.Marshal]::SizeOf($DiskData), [ref]$Value, [System.IntPtr]::Zero);
8688
if ($Result) {
8789
if (($DiskData.attributes -band $DISK_ATTRIBUTE_OFFLINE) -eq $DISK_ATTRIBUTE_OFFLINE) {
8890
$DiskOffline = $TRUE;
@@ -91,7 +93,7 @@ function Get-IcingaDiskAttributes()
9193
$DiskReadOnly = $TRUE;
9294
}
9395
}
94-
$Result = [kernel32]::CloseHandle($KernelHandle);
96+
$Result = [IcingaDiskAttributes]::CloseHandle($KernelHandle);
9597
}
9698

9799
return @{

provider/disks/Get-IcingaUNCPathSize.psm1

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@ function Get-IcingaUNCPathSize()
1212
-Force;
1313
}
1414

15-
# Register our kernel32.dll Windows API function call
16-
Add-Type -TypeDefinition @"
17-
using System;
18-
using System.Runtime.InteropServices;
19-
20-
public static class kernel32 {
21-
[DllImport("kernel32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
22-
23-
public static extern int GetDiskFreeSpaceEx(
24-
IntPtr lpDirectoryName, // UNC Path for share
25-
out long lpFreeBytesAvailable, // Free Bytes available on path
26-
out long lpTotalNumberOfBytes, // Bytes available on target disk / path
27-
out long lpTotalNumberOfFreeBytes // Total available space on target disk / path
28-
);
29-
}
15+
if ((Test-IcingaAddTypeExist -Type 'IcingaUNCPath') -eq $FALSE) {
16+
# Register our kernel32.dll Windows API function call
17+
Add-Type -TypeDefinition @"
18+
using System;
19+
using System.Runtime.InteropServices;
20+
21+
public static class IcingaUNCPath {
22+
[DllImport("kernel32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
23+
24+
public static extern int GetDiskFreeSpaceEx(
25+
IntPtr lpDirectoryName, // UNC Path for share
26+
out long lpFreeBytesAvailable, // Free Bytes available on path
27+
out long lpTotalNumberOfBytes, // Bytes available on target disk / path
28+
out long lpTotalNumberOfFreeBytes // Total available space on target disk / path
29+
);
30+
}
3031
"@
32+
}
3133

3234
# Setup variables as object which we can use to reference data into
3335
$ShareFree = New-Object -TypeName long;
@@ -38,7 +40,7 @@ Add-Type -TypeDefinition @"
3840
[System.IntPtr]$ptrPath = [System.Runtime.InteropServices.Marshal]::StringToHGlobalAuto($Path);
3941

4042
# Call our function we registered within the Add-Type definition
41-
[kernel32]::GetDiskFreeSpaceEx($ptrPath, [ref]$ShareFree, [ref]$ShareSize, [ref]$TotalFree) | Out-Null;
43+
[IcingaUNCPath]::GetDiskFreeSpaceEx($ptrPath, [ref]$ShareFree, [ref]$ShareSize, [ref]$TotalFree) | Out-Null;
4244
$ShareFreePercent = 0;
4345

4446
if ($ShareSize -ne 0) {

0 commit comments

Comments
 (0)