From a64449e074d53f143b5f11cdf729cec1c1a1b3ad Mon Sep 17 00:00:00 2001 From: Alexander Stoll Date: Mon, 22 Jul 2019 07:32:52 +0200 Subject: [PATCH] Added Process.psm1, Added Services.psm1; Fixed previous concept errors --- lib/provider/cpu.psm1 | 143 ++++++++++++++++++--------------- lib/provider/disks.psm1 | 42 ++++++---- lib/provider/enums.psm1 | 4 + lib/provider/memory.psm1 | 160 ++++++++++++++++++------------------- lib/provider/process.psm1 | 124 ++++++++++++++++++++++++++++ lib/provider/services.psm1 | 61 ++++++++++++++ 6 files changed, 374 insertions(+), 160 deletions(-) create mode 100644 lib/provider/process.psm1 create mode 100644 lib/provider/services.psm1 diff --git a/lib/provider/cpu.psm1 b/lib/provider/cpu.psm1 index bdef95a..986a9f9 100644 --- a/lib/provider/cpu.psm1 +++ b/lib/provider/cpu.psm1 @@ -30,75 +30,74 @@ function Get-IcingaCPUs() $CPUInformation = Get-CimInstance Win32_Processor; [hashtable]$CPUData = @{}; - foreach ($id in $CPUInformation.DeviceID) { - $id=$id.trim('CPU'); + foreach ($cpu in $CPUInformation) { $CPUData.Add( - $id, @{ + $cpu.DeviceID.trim('CPU'), @{ 'metadata' = @{ - 'Name' = $CPUInformation.Name; - 'DeviceID' = $CPUInformation.DeviceID; - 'ProcessorID' = $CPUInformation.ProcessorId; - 'UniqueID' = $CPUInformation.UniqueId; - 'Description' = $CPUInformation.Description; - 'OtherFamilyDescription' = $CPUInformation.OtherFamilyDescription; - 'Caption' = $CPUInformation.Caption; - 'Version' = $CPUInformation.Version; - 'SerialNumber' = $CPUInformation.SerialNumber; - 'Manufacturer' = $CPUInformation.Manufacturer; - 'NumberOfCores' = $CPUInformation.NumberOfCores; - 'PartNumber' = $CPUInformation.PartNumber; - 'Status' = $CPUInformation.Status; - 'CPUStatus' = $CPUInformation.CpuStatus; - 'Revision' = $CPUInformation.Revision; - 'NumberOfLogicalProcessors' = $CPUInformation.NumberOfLogicalProcessors; - 'Level'= $CPUInformation.Level; - 'AddressWidth' = $CPUInformation.AddressWidth; - 'Stepping' = $CPUInformation.Stepping; - 'SocketDesignation' = $CPUInformation.SocketDesignation; + 'Name' = $cpu.Name; + 'DeviceID' = $cpu.DeviceID; + 'ProcessorID' = $cpu.ProcessorId; + 'UniqueID' = $cpu.UniqueId; + 'Description' = $cpu.Description; + 'OtherFamilyDescription' = $cpu.OtherFamilyDescription; + 'Caption' = $cpu.Caption; + 'Version' = $cpu.Version; + 'SerialNumber' = $cpu.SerialNumber; + 'Manufacturer' = $cpu.Manufacturer; + 'NumberOfCores' = $cpu.NumberOfCores; + 'PartNumber' = $cpu.PartNumber; + 'Status' = $cpu.Status; + 'CPUStatus' = $cpu.CpuStatus; + 'Revision' = $cpu.Revision; + 'NumberOfLogicalProcessors' = $cpu.NumberOfLogicalProcessors; + 'Level'= $cpu.Level; + 'AddressWidth' = $cpu.AddressWidth; + 'Stepping' = $cpu.Stepping; + 'SocketDesignation' = $cpu.SocketDesignation; 'Family' = @{ - 'raw' = $CPUInformation.Family; - 'value' = $ProviderEnums.CPUFamily[[int]$CPUInformation.Family]; + 'raw' = $cpu.Family; + 'value' = $ProviderEnums.CPUFamily[[int]$cpu.Family]; }; 'Architecture' = @{ - 'raw' = $CPUInformation.Architecture; - 'value' = $ProviderEnums.CPUArchitecture[[int]$CPUInformation.Architecture]; + 'raw' = $cpu.Architecture; + 'value' = $ProviderEnums.CPUArchitecture[[int]$cpu.Architecture]; }; 'ProcessorType' = @{ - 'raw' = $CPUInformation.ProcessorType; - 'value' = $ProviderEnums.CPUProcessorType[[int]$CPUInformation.ProcessorType]; + 'raw' = $cpu.ProcessorType; + 'value' = $ProviderEnums.CPUProcessorType[[int]$cpu.ProcessorType]; }; 'StatusInfo' = @{ - 'raw' = $CPUInformation.StatusInfo; - 'value' = $ProviderEnums.CPUStatusInfo[[int]$CPUInformation.StatusInfo]; + 'raw' = $cpu.StatusInfo; + 'value' = $ProviderEnums.CPUStatusInfo[[int]$cpu.StatusInfo]; }; 'Availability' = @{ - 'raw' = $CPUInformation.Availability; - 'value' = $ProviderEnums.CPUAvailability[[int]$CPUInformation.Availability]; + 'raw' = $cpu.Availability; + 'value' = $ProviderEnums.CPUAvailability[[int]$cpu.Availability]; }; 'PowerManagementCapabilities' = @{ - 'raw' = $CPUInformation.PowerManagementCapabilities; - 'value' = $ProviderEnums.CPUPowerManagementCapabilities[[int]$CPUInformation.PowerManagementCapabilities]; + 'raw' = $cpu.PowerManagementCapabilities; + 'value' = $ProviderEnums.CPUPowerManagementCapabilities[[int]$cpu.PowerManagementCapabilities]; } }; 'errors' = @{ - 'LastErrorCode' = $CPUInformation.LastErrorCode; - 'ErrorCleared' = $CPUInformation.ErrorCleared; - 'ErrorDescription' = $CPUInformation.ErrorDescription; + 'LastErrorCode' = $cpu.LastErrorCode; + 'ErrorCleared' = $cpu.ErrorCleared; + 'ErrorDescription' = $cpu.ErrorDescription; 'ConfigManagerErrorCode' = @{ - 'raw' = [int]$CPUInformation.ConfigManagerErrorCode; - 'value' = $ProviderEnums.CPUConfigManagerErrorCode.([int]$CPUInformation.ConfigManagerErrorCode); + 'raw' = [int]$cpu.ConfigManagerErrorCode; + 'value' = $ProviderEnums.CPUConfigManagerErrorCode.([int]$cpu.ConfigManagerErrorCode); } }; 'specs' = @{ - 'LoadPercentage' = $CPUInformation.LoadPercentage; - 'CurrentVoltage' = $CPUInformation.CurrentVoltage; - 'ThreadCount' = $CPUInformation.ThreadCount; - 'L3CacheSize' = $CPUInformation.L3CacheSize; - 'L2CacheSpeed' = $CPUInformation.L2CacheSpeed; - 'L2CacheSize' = $CPUInformation.L2CacheSize; - 'VoltageCaps' = $CPUInformation.VoltageCaps; - 'CurrentClockSpeed' = $CPUInformation.CurrentClockSpeed; + 'LoadPercentage' = $cpu.LoadPercentage; + 'CurrentVoltage' = $cpu.CurrentVoltage; + 'ThreadCount' = $cpu.ThreadCount; + 'L3CacheSize' = $cpu.L3CacheSize; + 'L2CacheSpeed' = $cpu.L2CacheSpeed; + 'L2CacheSize' = $cpu.L2CacheSize; + 'VoltageCaps' = $cpu.VoltageCaps; + 'CurrentClockSpeed' = $cpu.CurrentClockSpeed; } } ); @@ -117,8 +116,8 @@ function Get-IcingaCPUInformation() $CPUInformation = Get-CimInstance Win32_Processor; [hashtable]$CPUData = @{}; - foreach ($id in $CPUInformation.DeviceID) { - $CPUData.Add($id.trim('CPU'), $CPUInformation.$Parameter); + foreach ($cpu in $CPUInformation) { + $CPUData.Add($cpu.DeviceID.trim('CPU'), $cpu.$Parameter); } return $CPUData; @@ -137,12 +136,11 @@ function Get-IcingaCPUInformationWithEnums() [hashtable]$CPUData = @{}; - foreach ($id in $CPUInformation.DeviceID) { - $id=$id.trim('CPU'); + foreach ($cpu in $CPUInformation) { $CPUData.Add( - $id, @{ - 'raw' = $CPUInformation.$Parameter; - 'value' = $ProviderEnums."$Prefix$Parameter"[[int]$CPUInformation.$Parameter] + $cpu.DeviceID.trim('CPU'), @{ + 'raw' = $cpu.$Parameter; + 'value' = $ProviderEnums."$Prefix$Parameter"[[int]$cpu.$Parameter] } ); } @@ -154,17 +152,16 @@ function Get-IcingaCPUErrors() $CPUInformation = Get-CimInstance Win32_Processor; [hashtable]$CPUData = @{}; - foreach ($id in $CPUInformation.DeviceID) { - $id=$id.trim('CPU'); + foreach ($cpu in $CPUInformation) { $CPUData.Add( - $id, @{ + $cpu.trim('CPU'), @{ 'errors' = @{ - 'LastErrorCode' = $CPUInformation.LastErrorCode; - 'ErrorCleared' = $CPUInformation.ErrorCleared; - 'ErrorDescription' = $CPUInformation.ErrorDescription; + 'LastErrorCode' = $cpu.LastErrorCode; + 'ErrorCleared' = $cpu.ErrorCleared; + 'ErrorDescription' = $cpu.ErrorDescription; 'ConfigManagerErrorCode' = @{ - 'raw' = [int]$CPUInformation.ConfigManagerErrorCode; - 'value' = $ProviderEnums.CPUConfigManagerErrorCode.([int]$CPUInformation.ConfigManagerErrorCode); + 'raw' = [int]$cpu.ConfigManagerErrorCode; + 'value' = $ProviderEnums.CPUConfigManagerErrorCode.([int]$cpu.ConfigManagerErrorCode); } } } @@ -283,4 +280,24 @@ function Get-IcingaCPUNumberOfLogicalProcessors() $CPUNumberOfLogicalProcessors = Get-IcingaCPUInformation -Parameter NumberOfLogicalProcessors; return @{'value' = $CPUNumberOfLogicalProcessors; 'name' = 'NumberOfLogicalProcessors'}; +} + +function Get-IcingaCPUCount() +{ + <# Collects the most important CPU informations, + e.g. name, version, manufacturer#> + $CPUInformation = Get-CimInstance Win32_Processor; + + foreach ($cpu in $CPUInformation) { + $NumberOfCoresValue += $cpu.NumberOfCores; + $NumberOfLogicalProcessorsValue += $cpu.NumberOfLogicalProcessors; + $ThreadCountValue += $cpu.ThreadCount; + } + + If (($NumberOfCoresValue -ge $NumberOfLogicalProcessorsValue) -and ($NumberOfCoresValue -ge $ThreadCountValue)) { + return $NumberOfCoresValue; + } elseif ($NumberOfLogicalProcessorsValue -ge $ThreadCountValue) { + return $NumberOfLogicalProcessorsValue; + } + return $ThreadCountValue; } \ No newline at end of file diff --git a/lib/provider/disks.psm1 b/lib/provider/disks.psm1 index d689a33..eed30ec 100644 --- a/lib/provider/disks.psm1 +++ b/lib/provider/disks.psm1 @@ -106,15 +106,17 @@ function Get-IcingaDiskInformation() $DiskInformation = Get-CimInstance Win32_DiskDrive; [hashtable]$DiskData = @{}; - foreach ($id in $DiskInformation.DeviceID) { - $id = $id.trimstart(".\PHYSICALDRVE"); - $DiskData.Add($id.trim(), $DiskInformation.$Parameter); + foreach ($disk in $DiskInformation) { + $DiskData.Add($disk.DeviceID.trimstart(".\PHYSICALDRVE"), $disk.$Parameter); } return $DiskData; } function Get-IcingaDiskPartitions() { + param( + $Disk + ); <# Fetches all the most important informations regarding partitions e.g. physical disk; partition, size , also collects partition information for Get-IcingaDisks #> @@ -137,7 +139,15 @@ function Get-IcingaDiskPartitions() $diskPartition = $diskPartition.trim("Partition #"); $diskDisk = $diskDisk.trim("Disk #"); + + If ([string]::IsNullOrEmpty($Disk) -eq $FALSE) { + If ([int]$Disk -ne [int]$diskDisk) { + continue; + } + } + $diskPartitionSize = Get-Partition -DriveLetter $driveLetter; + $PartitionDiskByDriveLetter.Add( $driveLetter, @{ @@ -182,8 +192,8 @@ function Get-IcingaDiskCapabilities $DiskInformation = Get-CimInstance Win32_DiskDrive; [hashtable]$DiskCapabilities = @{}; - foreach ($id in $DiskInformation.Capabilities) { - $DiskCapabilities.Add([int]$id, $ProviderEnums.DiskCapabilities.([int]$id)); + foreach ($capabilities in $DiskInformation.Capabilities) { + $DiskCapabilities.Add([int]$capabilities, $ProviderEnums.DiskCapabilities.([int]$capabilities)); } return @{'value' = $DiskCapabilities; 'name' = 'Capabilities'}; @@ -231,23 +241,21 @@ function Get-IcingaDisks { e.g. size, model, sectors, cylinders Is dependent on Get-IcingaDiskPartitions#> $DiskInformation = Get-CimInstance Win32_DiskDrive; - $diskPartitionInformation = Get-IcingaDiskPartitions; [hashtable]$DiskData = @{}; - foreach ($id in $DiskInformation.DeviceID) { - [int]$id = $id.trimstart(".\PHYSICALDRVE"); - + foreach ($disk in $DiskInformation) { + $diskID = $disk.DeviceID.trimstart(".\PHYSICALDRVE"); $DiskData.Add( - $id, @{ + $diskID, @{ 'metadata' = @{ - 'Size' = $DiskInformation.Size; - 'Model' = $DiskInformation.Model; - 'Name' = $DiskInformation.Name.trim('.\'); - 'Manufacturer' = $DiskInformation.Manufacturer; - 'Cylinder' = $DiskInformation.TotalCylinders; - 'Sectors' = $DiskInformation.TotalSectors + 'Size' = $disk.Size; + 'Model' = $disk.Model; + 'Name' = $disk.Name.trim('.\'); + 'Manufacturer' = $disk.Manufacturer; + 'Cylinder' = $disk.TotalCylinders; + 'Sectors' = $disk.TotalSectors }; - 'partitions' = $diskPartitionInformation + 'partitions' = (Get-IcingaDiskPartitions -Disk $diskID); } ); } diff --git a/lib/provider/enums.psm1 b/lib/provider/enums.psm1 index 6903ec0..52faa73 100644 --- a/lib/provider/enums.psm1 +++ b/lib/provider/enums.psm1 @@ -297,6 +297,10 @@ 3='Enabled'; } +<################################################################################################## +################# /lib/provider/memory.psm1 ####################################################### +##################################################################################################> + [hashtable]$MemoryFormFactor = @{ 0='Unknown'; 1= 'Other'; diff --git a/lib/provider/memory.psm1 b/lib/provider/memory.psm1 index da6edd0..6e17fc0 100644 --- a/lib/provider/memory.psm1 +++ b/lib/provider/memory.psm1 @@ -10,46 +10,46 @@ function Show-IcingaMemoryData () [hashtable]$MEMData = @{}; - foreach($id in $MEMInformation) { + foreach($memory in $MEMInformation) { $MEMData.Add( - $id.tag.trim("Physical Memory"), @{ - 'Caption' = $id.Name; - 'Description' = $id.Description; - 'Name' = $id.Name; - 'InstallDate' = $id.InstallDate; - 'Status' = $id.Status - 'CreationClassName'= $id.CreationClassName - 'Manufacturer'= $id.Manufacturer - 'Model'= $id.Model - 'OtherIdentifyingInfo'= $id.OtherIdentifyingInfo - 'PartNumber'= $id.PartNumber - 'PoweredOn'= $id.PoweredOn - 'SerialNumber'= $id.SerialNumber - 'SKU'= $id.SKU - 'Tag'= $id.Tag - 'Version'= $id.Version - 'HotSwappable'= $id.HotSwappable - 'Removable'= $id.Removable - 'Replaceable'= $id.Replaceable - 'FormFactor'= $id.FormFactor - 'BankLabel'= $id.BankLabel - 'Capacity'= $id.Capacity - 'DataWidth'= $id.DataWidth - 'InterleavePosition'= $id.InterleavePosition - 'MemoryType'= $id.MemoryType - 'PositionInRow'= $id.PositionInRow - 'Speed'= $id.Speed - 'TotalWidth'= $id.TotalWidth - 'Attributes'= $id.Attributes - 'ConfiguredClockSpeed'= $id.ConfiguredClockSpeed - 'ConfiguredVoltage'= $id.ConfiguredVoltage - 'DeviceLocator'= $id.DeviceLocator - 'InterleaveDataDepth'= $id.InterleaveDataDepth - 'MaxVoltage'= $id.MaxVoltage - 'MinVoltage'= $id.MinVoltage - 'SMBIOSMemoryType'= $id.SMBIOSMemoryType - 'TypeDetail'= $id.TypeDetail - 'PSComputerName'= $id.PSComputerName + $memory.tag.trim("Physical Memory"), @{ + 'Caption' = $memory.Name; + 'Description' = $memory.Description; + 'Name' = $memory.Name; + 'InstallDate' = $memory.InstallDate; + 'Status' = $memory.Status + 'CreationClassName'= $memory.CreationClassName + 'Manufacturer'= $memory.Manufacturer + 'Model'= $memory.Model + 'OtherIdentifyingInfo'= $memory.OtherIdentifyingInfo + 'PartNumber'= $memory.PartNumber + 'PoweredOn'= $memory.PoweredOn + 'SerialNumber'= $memory.SerialNumber + 'SKU'= $memory.SKU + 'Tag'= $memory.Tag + 'Version'= $memory.Version + 'HotSwappable'= $memory.HotSwappable + 'Removable'= $memory.Removable + 'Replaceable'= $memory.Replaceable + 'FormFactor'= $memory.FormFactor + 'BankLabel'= $memory.BankLabel + 'Capacity'= $memory.Capacity + 'DataWidth'= $memory.DataWidth + 'InterleavePosition'= $memory.InterleavePosition + 'MemoryType'= $memory.MemoryType + 'PositionInRow'= $memory.PositionInRow + 'Speed'= $memory.Speed + 'TotalWidth'= $memory.TotalWidth + 'Attributes'= $memory.Attributes + 'ConfiguredClockSpeed'= $memory.ConfiguredClockSpeed + 'ConfiguredVoltage'= $memory.ConfiguredVoltage + 'DeviceLocator'= $memory.DeviceLocator + 'InterleaveDataDepth'= $memory.InterleaveDataDepth + 'MaxVoltage'= $memory.MaxVoltage + 'MinVoltage'= $memory.MinVoltage + 'SMBIOSMemoryType'= $memory.SMBIOSMemoryType + 'TypeDetail'= $memory.TypeDetail + 'PSComputerName'= $memory.PSComputerName } ); } @@ -66,57 +66,57 @@ function Get-IcingaMemory () [hashtable]$MEMData = @{}; - foreach($id in $MEMInformation) { + foreach($memory in $MEMInformation) { $MEMData.Add( - $id.tag.trim("Physical Memory"), @{ + $memory.tag.trim("Physical Memory"), @{ 'metadata' = @{ - 'Caption' = $id.Name; - 'Description'= $id.Description; - 'Manufacturer'= $id.Manufacturer; - 'Model'= $id.Model; - 'OtherIdentifyingInfo'= $id.OtherIdentifyingInfo; - 'PartNumber'= $id.PartNumber; - 'SerialNumber'= $id.SerialNumber; - 'Tag'= $id.Tag; - 'SMBIOSMemoryType'= $id.SMBIOSMemoryType; - 'DeviceLocator' = $id.DeviceLocator; - 'PositionInRow' = $id.PositionInRow; - 'Version' = $id.Version; - 'PoweredOn' = $id.PoweredOn; - 'Status' = $id.Status; - 'InstallDate' = $id.InstallDate; - 'BankLabel' = $id.BankLabel; - 'InterleaveDataDepth' = $id.InterleaveDataDepth; - 'Attributes' = $id.Attributes; - 'Replaceable' = $id.Replaceable; - 'Removable' = $id.Removable; - 'HotSwappable' = $id.HotSwappable; + 'Caption' = $memory.Name; + 'Description'= $memory.Description; + 'Manufacturer'= $memory.Manufacturer; + 'Model'= $memory.Model; + 'OtherIdentifyingInfo'= $memory.OtherIdentifyingInfo; + 'PartNumber'= $memory.PartNumber; + 'SerialNumber'= $memory.SerialNumber; + 'Tag'= $memory.Tag; + 'SMBIOSMemoryType'= $memory.SMBIOSMemoryType; + 'DeviceLocator' = $memory.DeviceLocator; + 'PositionInRow' = $memory.PositionInRow; + 'Version' = $memory.Version; + 'PoweredOn' = $memory.PoweredOn; + 'Status' = $memory.Status; + 'InstallDate' = $memory.InstallDate; + 'BankLabel' = $memory.BankLabel; + 'InterleaveDataDepth' = $memory.InterleaveDataDepth; + 'Attributes' = $memory.Attributes; + 'Replaceable' = $memory.Replaceable; + 'Removable' = $memory.Removable; + 'HotSwappable' = $memory.HotSwappable; 'FormFactor' = @{ - 'raw' = $id.FormFactor; - 'value' = $ProviderEnums.MemoryFormFactor[[int]$id.FormFactor]; + 'raw' = $memory.FormFactor; + 'value' = $ProviderEnums.MemoryFormFactor[[int]$memory.FormFactor]; }; 'InterleavePosition' = @{ - 'raw' = $id.InterleavePosition; - 'value' = $ProviderEnums.MemoryInterleavePosition[[int]$id.InterleavePosition]; + 'raw' = $memory.InterleavePosition; + 'value' = $ProviderEnums.MemoryInterleavePosition[[int]$memory.InterleavePosition]; }; 'MemoryType' = @{ - 'raw' = $id.MemoryType; - 'value' = $ProviderEnums.MemoryMemoryType[[int]$id.MemoryType]; + 'raw' = $memory.MemoryType; + 'value' = $ProviderEnums.MemoryMemoryType[[int]$memory.MemoryType]; }; 'TypeDetail' = @{ - 'raw' = $id.TypeDetail; - 'value' = $ProviderEnums.MemoryTypeDetail[[int]$id.TypeDetail]; + 'raw' = $memory.TypeDetail; + 'value' = $ProviderEnums.MemoryTypeDetail[[int]$memory.TypeDetail]; }; }; 'specs' = @{ - 'MaxVoltage' = $id.MaxVoltage; - 'MinVoltage' = $id.MinVoltage; - 'ConfiguredVoltage' = $id.ConfiguredVoltage; - 'ConfiguredClockSpeed' = $id.ConfiguredClockSpeed; - 'TotalWidth' = $id.TotalWidth; - 'DataWidth' = $id.DataWidth; - 'Speed' = $id.Speed; - 'Capacity' = $id.Capacity; + 'MaxVoltage' = $memory.MaxVoltage; + 'MinVoltage' = $memory.MinVoltage; + 'ConfiguredVoltage' = $memory.ConfiguredVoltage; + 'ConfiguredClockSpeed' = $memory.ConfiguredClockSpeed; + 'TotalWidth' = $memory.TotalWidth; + 'DataWidth' = $memory.DataWidth; + 'Speed' = $memory.Speed; + 'Capacity' = $memory.Capacity; } } ); @@ -133,8 +133,8 @@ function Get-IcingaMemoryInformation() $MEMInformation = Get-CimInstance Win32_PhysicalMemory; [hashtable]$MEMData = @{}; - foreach ($id in $MEMInformation) { - $MEMData.Add($id.tag.trim("Physical Memory"), $id.$Parameter); + foreach ($memory in $MEMInformation) { + $MEMData.Add($memory.tag.trim("Physical Memory"), $memory.$Parameter); } return $MEMData; diff --git a/lib/provider/process.psm1 b/lib/provider/process.psm1 new file mode 100644 index 0000000..26e639f --- /dev/null +++ b/lib/provider/process.psm1 @@ -0,0 +1,124 @@ +Import-Module $IncludeDir\provider\cpu; + +function Add-IcingaProcessPerfData() +{ + param($ProcessList, $ProcessKey, $Process); + + if ($ProcessList.ContainsKey($ProcessKey) -eq $FALSE) { + $ProcessList.Add($ProcessKey, $Process.$ProcessKey); + } else { + $ProcessList[$ProcessKey] += $Process.$ProcessKey; + } +} + +function Get-IcingaProcessData { + + $ProcessInformation = Get-WmiObject Win32_Process; + $ProcessPerfDataList = Get-WmiObject Win32_PerfFormattedData_PerfProc_Process; + $ProcessUniqueList = Get-WmiObject Win32_Process | Select-Object name -unique; + $CPUCoreCount = Get-IcingaCPUCount; + + + [hashtable]$ProcessData = @{}; + [hashtable]$ProcessList = @{}; + [hashtable]$ProcessNamesUnique = @{}; + [hashtable]$ProcessIDsByName = @{}; + #$NumberOfCPUThreads = $Icinga2.System.NumberOfCPUThreads; + + foreach ($process in $ProcessInformation) { + [string]$processName = $process.Name.Replace('.exe', ''); + + if ($ProcessList.ContainsKey($processName) -eq $FALSE) { + $ProcessList.Add($processName, @{ + 'ProcessList' = @{}; + 'PerformanceData' = @{} + }); + } + + $ProcessList[$processName]['ProcessList'].Add( + [string]$process.ProcessID, @{ + 'Name' = $process.Name; + 'ProcessId' = $process.ProcessId; + 'Priority' = $process.Priority; + 'PageFileUsage' = $process.PageFileUsage; + 'ThreadCount' = $process.ThreadCount; + 'KernelModeTime' = $process.KernelModeTime; + 'UserModeTime' = $process.UserModeTime; + 'WorkingSetSize' = $process.WorkingSetSize; + 'CommandLine' = $process.CommandLine; + } + ); + + Add-IcingaProcessPerfData -ProcessList $ProcessList[$processName]['PerformanceData'] -ProcessKey 'ThreadCount' -Process $process; + Add-IcingaProcessPerfData -ProcessList $ProcessList[$processName]['PerformanceData'] -ProcessKey 'PageFileUsage' -Process $process; + Add-IcingaProcessPerfData -ProcessList $ProcessList[$processName]['PerformanceData'] -ProcessKey 'KernelModeTime' -Process $process; + Add-IcingaProcessPerfData -ProcessList $ProcessList[$processName]['PerformanceData'] -ProcessKey 'UserModeTime' -Process $process; + Add-IcingaProcessPerfData -ProcessList $ProcessList[$processName]['PerformanceData'] -ProcessKey 'WorkingSetSize' -Process $process; + } + + foreach ($process in $ProcessPerfDataList) { + if ($process.Name -eq '_Total' -Or $process.Name -eq 'Idle') { + continue; + } + + [string]$processName = $process.Name.Split('#')[0]; + [string]$ProcessId = $process.IDProcess; + + if ($ProcessList.ContainsKey($processName) -eq $FALSE) { + Write-Host 'Unknown Process Name: ' $processName; + continue; + } + + if ($ProcessList[$processName]['ProcessList'].ContainsKey($ProcessId) -eq $FALSE) { + Write-Host 'Unknown Process ID: ' $ProcessId; + continue; + } + + $ProcessList[$processName]['ProcessList'][$ProcessId].Add( + 'WorkingSetPrivate', $process.WorkingSetPrivate + ); + $ProcessList[$processName]['ProcessList'][$ProcessId].Add( + 'PercentProcessorTime', ($process.PercentProcessorTime / $CPUCoreCount) + ); + + Add-IcingaProcessPerfData -ProcessList $ProcessList[$processName]['PerformanceData'] -ProcessKey 'WorkingSetPrivate' -Process $process; + if ($ProcessList[$processName]['PerformanceData'].ContainsKey('PercentProcessorTime') -eq $FALSE) { + $ProcessList[$processName]['PerformanceData'].Add('PercentProcessorTime', ($process.PercentProcessorTime / $CPUCoreCount)); + } else { + $ProcessList[$processName]['PerformanceData']['PercentProcessorTime'] += ($process.PercentProcessorTime / $CPUCoreCount); + } + } + + $ProcessData.Add('Process Count', $ProcessInformation.Count); + $ProcessData.add('Processes', $ProcessList); + + return $ProcessData; + # Code Stolli below + + foreach ($NameID in $ProcessUniqueList.Name) { + $ProcessIDsBasedOnName = (Get-WmiObject Win32_Process -Filter name="'${NameID}'").ProcessID; + $ProcessIDsByName.Add($NameID,$ProcessIDsBasedOnName); + } + + foreach ($id in $ProcessUniqueList) { + $nameid = $id.name; + $ProcessNamesUnique.Add( + $id.Name.trim(".exe"), @{ + 'processlist' = @{ + $ProcessIDsByName.Item("$nameid") = "metadata"; + }; + 'perfdata' = @{ + 'lawl' = 'lol'; + 'lel' = 'lel'; + 'lol' = 'eyooo'; + } + } + ); + } + + + $ProcessData.Add('Process Count', $ProcessInformation.Count); + $ProcessData.add('Processes', $ProcessNamesUnique); + + return $ProcessData; +} \ No newline at end of file diff --git a/lib/provider/services.psm1 b/lib/provider/services.psm1 new file mode 100644 index 0000000..54724bc --- /dev/null +++ b/lib/provider/services.psm1 @@ -0,0 +1,61 @@ +function Get-IcingaServices() +{ + param ( + [array]$Service + ) + + $ServiceInformation = Get-Service -Name $Service; + + [hashtable]$ServiceData = @{}; + + foreach ($service in $ServiceInformation) { + + [array]$DependentServices = $null; + [array]$DependingServices = $null; + + #Dependent / Child + foreach ($dependency in $service.DependentServices) { + if ($null -eq $DependentServices) { $DependentServices = @(); } + $DependentServices += $dependency.Name; + } + + #Depends / Parent + foreach ($dependency in $service.ServicesDependedOn) { + if ($null -eq $DependingServices) { $DependingServices = @(); } + $DependingServices += $dependency.Name; + } + + $ServiceData.Add( + $service.Name, @{ + 'metadata' = @{ + 'DisplayName' = $service.DisplayName; + 'ServiceName' = $service.ServiceName; + 'Site' = $service.Site; + 'Container' = $service.Container; + 'ServiceHandle' = $service.ServiceHandle; + 'Dependent' = $DependentServices; + 'Depends' = $DependingServices; + }; + 'configuration' = @{ + 'CanPauseAndContinue' = $service.CanPauseAndContinue; + 'CanShutdown' = $service.CanShutdown; + 'CanStop' = $service.CanStop; + 'Status' = @{ + 'raw' = [int]$service.Status; + 'value' = $service.Status; + }; + 'ServiceType' = @{ + 'raw' = [int]$service.ServiceType; + 'value' = $service.ServiceType; + }; + 'ServiceHandle' = $service.ServiceHandle; + 'StartType' = @{ + 'raw' = [int]$service.StartType; + 'value' = $service.StartType; + }; + } + } + ); + } + return $ServiceData; +} \ No newline at end of file