Skip to content

Commit

Permalink
Merge pull request #34 from Icinga/feature/cim_wmi_wrapper
Browse files Browse the repository at this point in the history
Replace CIM/WMI calls for wrapper function for proper error/permission handling
  • Loading branch information
LordHepipud authored Aug 21, 2020
2 parents 6c5aed8 + 377bb4b commit beb2b00
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 42 deletions.
3 changes: 3 additions & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ This release adds the following new plugins:
* [Invoke-IcingaCheckTimeSync](https://icinga.com/docs/windows/latest/plugins/doc/plugins/18-Invoke-IcingaChecTimeSync/): Allows to compare local machine time with a time server to check for possible time missmatch
* [Invoke-IcingaCheckScheduledTask](https://icinga.com/docs/windows/latest/plugins/doc/plugins/19-Invoke-IcingaCheckScheduledTask/): Allows to check the current state for a list of provided scheduled tasks
* [Invoke-IcingaCheckDiskHealth](https://icinga.com/docs/windows/latest/plugins/doc/plugins/20-Invoke-IcingaCheckDiskHealth/): Allows to check for several disk Performance Counters and the disk health in general
### Notes

* [#34](https://github.com/Icinga/icinga-powershell-plugins/issues/34) Replaces plugin CIM/WMI calls for new Framework wrapper function `Get-IcingaWindowsInformation` to properly handle config/permission errors

### Enhancements

Expand Down
24 changes: 12 additions & 12 deletions provider/bios/Icinga_ProviderBios.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function Get-IcingaBios()
{
<# Collects the most important BIOS informations,
e.g. name, version, manufacturer#>
$BIOSInformation = Get-CimInstance Win32_BIOS;
$BIOSInformation = Get-IcingaWindowsInformation Win32_BIOS;
[hashtable]$BIOSCharacteristics = @{};
[hashtable]$BIOSData = @{};

Expand Down Expand Up @@ -35,7 +35,7 @@ function Get-IcingaBiosCharacteristics()
{
param([switch]$Sorted);

$bios = Get-CimInstance WIN32_BIOS;
$bios = Get-IcingaWindowsInformation WIN32_BIOS;
[hashtable]$BIOSCharacteristics = @{};

foreach ($id in $bios.BiosCharacteristics) {
Expand All @@ -54,7 +54,7 @@ function Get-IcingaBiosCharacteristics()
{
param([switch]$Sorted);

$bios = Get-CimInstance WIN32_BIOS;
$bios = Get-IcingaWindowsInformation WIN32_BIOS;
[hashtable]$BIOSCharacteristics = @{};

foreach ($id in $bios.BiosCharacteristics) {
Expand All @@ -71,55 +71,55 @@ function Get-IcingaBiosCharacteristics()
}
function Get-IcingaBiosSerialNumber()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.SerialNumber; 'name' = 'SerialNumber'};
}

function Get-IcingaBiosVersion()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.Version; 'name' = 'Version'};
}

function Get-IcingaBiosManufacturer()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.Manufacturer; 'name' = 'Manufacturer'};
}

# Primary Bios might be more relevant in dual bios context
function Get-IcingaBiosPrimaryBios()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.PrimaryBIOS; 'name' = 'PrimaryBIOS'};
}

function Get-IcingaBiosName()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.Name; 'name' = 'Name'};
}

function Get-IcingaBiosStatus()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.Status; 'name' = 'Status'};
}

function Get-IcingaBiosCaption()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.Caption; 'name' = 'Caption'};
}

function Get-IcingaBiosSMBIOSBIOSVersion()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.SMBIOSBIOSVersion; 'name' = 'SMBIOSBIOSVersion'};
}

function Get-IcingaBiosSoftwareElementID()
{
$bios = Get-CimInstance Win32_BIOS;
$bios = Get-IcingaWindowsInformation Win32_BIOS;
return @{'value' = $bios.SoftwareElementID; 'name' = 'SoftwareElementID'};
}
2 changes: 1 addition & 1 deletion provider/bios/Show-IcingaBiosData.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Show-IcingaBiosData()
{
$BIOSInformation = Get-CimInstance Win32_BIOS;
$BIOSInformation = Get-IcingaWindowsInformation Win32_BIOS;
[hashtable]$BIOSData = @{};

foreach ($bios_properties in $BIOSInformation) {
Expand Down
8 changes: 4 additions & 4 deletions provider/cpu/Icinga_ProviderCpu.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function Get-IcingaCPUs()
{
<# Collects the most important CPU informations,
e.g. name, version, manufacturer#>
$CPUInformation = Get-CimInstance Win32_Processor;
$CPUInformation = Get-IcingaWindowsInformation Win32_Processor;
[hashtable]$CPUData = @{};

foreach ($cpu in $CPUInformation) {
Expand Down Expand Up @@ -89,7 +89,7 @@ function Get-IcingaCPUInformation()
param(
[string]$Parameter
);
$CPUInformation = Get-CimInstance Win32_Processor;
$CPUInformation = Get-IcingaWindowsInformation Win32_Processor;
[hashtable]$CPUData = @{};

foreach ($cpu in $CPUInformation) {
Expand All @@ -107,7 +107,7 @@ function Get-IcingaCPUInformationWithEnums()
[string]$Parameter
);

$CPUInformation = Get-CimInstance Win32_Processor;
$CPUInformation = Get-IcingaWindowsInformation Win32_Processor;
$Prefix = "CPU";

[hashtable]$CPUData = @{};
Expand All @@ -125,7 +125,7 @@ function Get-IcingaCPUInformationWithEnums()

function Get-IcingaCPUErrors()
{
$CPUInformation = Get-CimInstance Win32_Processor;
$CPUInformation = Get-IcingaWindowsInformation Win32_Processor;
[hashtable]$CPUData = @{};

foreach ($cpu in $CPUInformation) {
Expand Down
2 changes: 1 addition & 1 deletion provider/cpu/Show-IcingaCPUData.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function Show-IcingaCPUData()
{

$CPUInformation = Get-CimInstance Win32_Processor;
$CPUInformation = Get-IcingaWindowsInformation Win32_Processor;
[hashtable]$PhysicalCPUData = @{};

foreach ($cpu_properties in $CPUInformation) {
Expand Down
10 changes: 5 additions & 5 deletions provider/disks/Icinga_ProviderDisks.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function Get-IcingaDiskInformation()
# The value to fetch from Win32_DiskDrive
[string]$Parameter
);
$DiskInformation = Get-CimInstance Win32_DiskDrive;
$DiskInformation = Get-IcingaWindowsInformation Win32_DiskDrive;
[hashtable]$DiskData = @{};

foreach ($disk in $DiskInformation) {
Expand Down Expand Up @@ -42,7 +42,7 @@ function Get-IcingaDiskPartitions()
<# Fetches all the most important informations regarding partitions
e.g. physical disk; partition, size
, also collects partition information for Get-IcingaDisks #>
$LogicalDiskInfo = Get-WmiObject Win32_LogicalDiskToPartition;
$LogicalDiskInfo = Get-IcingaWindowsInformation Win32_LogicalDiskToPartition -ForceWMI;
[hashtable]$PartitionDiskByDriveLetter = @{};

foreach ($item in $LogicalDiskInfo) {
Expand Down Expand Up @@ -70,7 +70,7 @@ function Get-IcingaDiskPartitions()

$DiskArray = New-IcingaPerformanceCounterStructure -CounterCategory 'LogicalDisk' -PerformanceCounterHash (New-IcingaPerformanceCounterArray @('\LogicalDisk(*)\% free space'));

$diskPartitionSize = (Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='${DriveLetter}:'");
$diskPartitionSize = (Get-IcingaWindowsInformation Win32_LogicalDisk -Filter "DeviceID='${DriveLetter}:'" -ForceWMI);

$PartitionDiskByDriveLetter.Add(
$driveLetter,
Expand Down Expand Up @@ -136,7 +136,7 @@ function Join-IcingaPhysicalDiskDataPerfCounter()

function Get-IcingaDiskCapabilities
{
$DiskInformation = Get-CimInstance Win32_DiskDrive;
$DiskInformation = Get-IcingaWindowsInformation Win32_DiskDrive;
[hashtable]$DiskCapabilities = @{};

foreach ($capabilities in $DiskInformation.Capabilities) {
Expand Down Expand Up @@ -187,7 +187,7 @@ function Get-IcingaDisks {
<# Collects all the most important Disk-Informations,
e.g. size, model, sectors, cylinders
Is dependent on Get-IcingaDiskPartitions#>
$DiskInformation = Get-CimInstance Win32_DiskDrive;
$DiskInformation = Get-IcingaWindowsInformation Win32_DiskDrive;
[hashtable]$DiskData = @{};

foreach ($disk in $DiskInformation) {
Expand Down
14 changes: 7 additions & 7 deletions provider/disks/Show-IcingaDiskData.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Show-IcingaDiskData {

$DisksInformations = Get-CimInstance Win32_DiskDrive;
$DisksInformations = Get-IcingaWindowsInformation Win32_DiskDrive;

[hashtable]$PhysicalDiskData = @{};

Expand All @@ -13,9 +13,9 @@ function Show-IcingaDiskData {
$disk_datails.Add('DriveReference', @());
$PhysicalDiskData.Add($disk_datails.DeviceID, $disk_datails);
}

$DiskPartitionInfo = Get-WmiObject Win32_DiskDriveToDiskPartition;

$DiskPartitionInfo = Get-IcingaWindowsInformation Win32_DiskDriveToDiskPartition -ForceWMI;
[hashtable]$MapDiskPartitionToLogicalDisk = @{};

foreach ($item in $DiskPartitionInfo) {
Expand All @@ -33,9 +33,9 @@ function Show-IcingaDiskData {

$MapDiskPartitionToLogicalDisk.Add($diskPartition, $physicalDrive);
}

$LogicalDiskInfo = Get-WmiObject Win32_LogicalDiskToPartition;

$LogicalDiskInfo = Get-IcingaWindowsInformation Win32_LogicalDiskToPartition -ForceWMI;
foreach ($item in $LogicalDiskInfo) {
[string]$driveLetter = $item.Dependent.SubString(
$item.Dependent.LastIndexOf('=') + 1,
Expand Down
2 changes: 1 addition & 1 deletion provider/memory/Get-IcingaMemoryPerformanceCounter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function Get-IcingaMemoryPerformanceCounter()
}

$MemoryData.Add('Memory Available Bytes', [decimal]($Initial.'\Memory\Available Bytes'.value));
$MemoryData.Add('Memory Total Bytes', (Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory);
$MemoryData.Add('Memory Total Bytes', (Get-IcingaWindowsInformation Win32_ComputerSystem).TotalPhysicalMemory);
$MemoryData.Add('Memory Used Bytes', $MemoryData.'Memory Total Bytes' - $MemoryData.'Memory Available Bytes');
$MemoryData.Add('Memory Used %', 100 - ($MemoryData.'Memory Available Bytes' / $MemoryData.'Memory Total Bytes' * 100));
$MemoryData.Add('PageFile %', $Initial.'\Paging File(_Total)\% usage'.value);
Expand Down
2 changes: 1 addition & 1 deletion provider/memory/Get-IcingaMemoryUsage.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Get-IcingaMemoryUsage()
{
$MEMUsageInformations = Get-CimInstance Win32_OperatingSystem;
$MEMUsageInformations = Get-IcingaWindowsInformation Win32_OperatingSystem;

[hashtable]$MEMUsageData = @{
'FreePhysicalMemory' = $MEMUsageInformations.FreePhysicalMemory;
Expand Down
4 changes: 2 additions & 2 deletions provider/memory/Icinga_ProviderMemory.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function Get-IcingaMemory()
{
<# Collects the most important Memory informations,
e.g. name, version, manufacturer#>
$MEMInformation = Get-CimInstance Win32_PhysicalMemory;
$MEMInformation = Get-IcingaWindowsInformation Win32_PhysicalMemory;

[hashtable]$MEMData = @{};

Expand Down Expand Up @@ -82,7 +82,7 @@ function Get-IcingaMemoryInformation()
param(
[string]$Parameter
);
$MEMInformation = Get-CimInstance Win32_PhysicalMemory;
$MEMInformation = Get-IcingaWindowsInformation Win32_PhysicalMemory;
[hashtable]$MEMData = @{};

foreach ($memory in $MEMInformation) {
Expand Down
2 changes: 1 addition & 1 deletion provider/memory/Show-IcingaMemoryData.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Show-IcingaMemoryData ()
{
$MEMInformation = Get-CimInstance Win32_PhysicalMemory;
$MEMInformation = Get-IcingaWindowsInformation Win32_PhysicalMemory;

[hashtable]$MEMData = @{};

Expand Down
4 changes: 2 additions & 2 deletions provider/process/Icinga_ProviderProcess.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function Get-IcingaProcessData {
[array]$Process
);

$ProcessInformation = Get-WmiObject Win32_Process;
$ProcessPerfDataList = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process;
$ProcessInformation = Get-IcingaWindowsInformation Win32_Process -ForceWMI;
$ProcessPerfDataList = Get-IcingaWindowsInformation Win32_PerfFormattedData_PerfProc_Process -ForceWMI;
$CPUCoreCount = Get-IcingaCPUCount;


Expand Down
4 changes: 2 additions & 2 deletions provider/services/Icinga_ProviderServices.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function Get-IcingaServices()
$ServiceWmiInfo = $null;

if ($Service.Count -eq 0) {
$ServiceWmiInfo = Get-WmiObject Win32_Service;
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service;
} else {
$ServiceWmiInfo = Get-WmiObject -Class Win32_Service | Where-Object { $Service -Contains $_.Name } | Select-Object StartName, Name;
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service | Where-Object { $Service -Contains $_.Name } | Select-Object StartName, Name;
}

if ($null -eq $ServiceInformation) {
Expand Down
2 changes: 1 addition & 1 deletion provider/users/Get-IcingaLoggedOnUsers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Get-IcingaLoggedOnUsers()
[int]$UserCount = 0;
$UserList.Add('users', @{ });

$Users = Get-CIMInstance Win32_LoggedOnUser | Select-Object Antecedent, Dependent;
$Users = Get-IcingaWindowsInformation Win32_LoggedOnUser | Select-Object Antecedent, Dependent;

foreach ($user in $Users) {
[string]$username = $user.Antecedent.Name;
Expand Down
2 changes: 1 addition & 1 deletion provider/windows/Icinga_ProviderWindows.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Import-IcingaLib provider\enums;

function Get-IcingaWindows()
{
$WindowsInformations = Get-CimInstance Win32_OperatingSystem;
$WindowsInformations = Get-IcingaWindowsInformation Win32_OperatingSystem;

$OSProductSuite = @();
$ProviderEnums.WindowsOSProductSuite.Keys | Where-Object {
Expand Down
2 changes: 1 addition & 1 deletion provider/windows/Show-IcingaWindowsData.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Show-IcingaWindowsData()
{
$WindowsInformations = Get-CimInstance Win32_OperatingSystem;
$WindowsInformations = Get-IcingaWindowsInformation Win32_OperatingSystem;

$windows_datails = @{};
foreach($cpu_core in $WindowsInformations.CimInstanceProperties) {
Expand Down

0 comments on commit beb2b00

Please sign in to comment.