rdpeake
rdpeake
HHASS.Agent
Created by rdpeake on 10/14/2024 in #💬・get-help
Get process stats into HA
I am trying to work out how to get process statistics into HA. At the moment i use hass agent to send cpu usage stats and i often see that running at 100% and have been trying to work out how to get historical data per process to work out the cause... I have worked out that i can ues the following powershell module to get json of the statistis i am after, but due to character restriction this isn't a viable way to get the data into HA (though i could do a top 10 process). If i were to use the top processes option, is there a better way to format this data so it is somewhat useable in HA to analyse?
$RAM= Get-WMIObject Win32_PhysicalMemory | Measure -Property capacity -Sum | %{$_.sum/1Mb}
$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors

function Get-Process-Statistics {
Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | select-object -Property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}}, @{"Name" = "Memory(%)"; Expression = {([math]::Round(($_.WorkingSetPrivate/1Mb)/$RAM*100,2))}}, @{Name="Disk(MB)"; Expression = {[Math]::Round(($_.IODataOperationsPersec / 1mb),2)}}, @{"Name"="Network"; Expression = { $_.IOReadBytesPersec }} | Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} | Sort-Object -Property CPU -Descending | ConvertTo-Json
#| Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)", "Memory(%)", "Disk(MB)", "Network"
}
Export-ModuleMember -Function Get-Process-Statistics
$RAM= Get-WMIObject Win32_PhysicalMemory | Measure -Property capacity -Sum | %{$_.sum/1Mb}
$cores = (Get-WmiObject Win32_Processor).NumberOfLogicalProcessors

function Get-Process-Statistics {
Get-WmiObject Win32_PerfFormattedData_PerfProc_Process | select-object -Property Name, @{Name = "CPU"; Expression = {($_.PercentProcessorTime)}}, @{Name = "PID"; Expression = {$_.IDProcess}}, @{"Name" = "Memory(MB)"; Expression = {[int]($_.WorkingSetPrivate/1mb)}}, @{"Name" = "Memory(%)"; Expression = {([math]::Round(($_.WorkingSetPrivate/1Mb)/$RAM*100,2))}}, @{Name="Disk(MB)"; Expression = {[Math]::Round(($_.IODataOperationsPersec / 1mb),2)}}, @{"Name"="Network"; Expression = { $_.IOReadBytesPersec }} | Where-Object {$_.Name -notmatch "^(idle|_total|system)$"} | Sort-Object -Property CPU -Descending | ConvertTo-Json
#| Format-Table -Autosize -Property Name, CPU, PID, "Memory(MB)", "Memory(%)", "Disk(MB)", "Network"
}
Export-ModuleMember -Function Get-Process-Statistics
1 replies