我正在尝试编写一个脚本,该脚本使用OpenHardwareMonitor获取CPU温度,如果它在给定的持续时间内平均超过某个阈值,则会发出警报。
获取当前温度是完美的,但我正在努力将它们平均,因为在一些系统上可能有一个或多个CPU,所以我不能只记录每分钟的温度,并除以多少分钟已经过去,下面是我得到的最好的(这不工作),请一些仁慈的灵魂帮助我看看我哪里错了?
$tempThreshold = 80 # Will alert if average temperature is above this
$minutes = 3 # Duration in minutes to test for
start-process "$env:SystemRoot\TEMP\OpenHardwareMonitor\openhardwaremonitor.exe"
# Loop each minute
for ($i=1; $i -le $minutes; $i++) {
$arrSensors=@{}
if ((Get-WmiObject -Namespace "Root\OpenHardwareMonitor" -Query "SELECT * FROM Sensor WHERE Sensortype='Temperature'" | ? {$_.Name -match 'CPU Package'}).value) {
Get-WmiObject -Namespace "Root\OpenHardwareMonitor" -Query "SELECT * FROM Sensor WHERE Sensortype='Temperature'" | ? {$_.Name -Match "CPU Package"} | % {$arrSensors[$_.Name]=$_.Value}
} else {
Get-WmiObject -Namespace "Root\OpenHardwareMonitor" -Query "SELECT * FROM Sensor WHERE Sensortype='Temperature'" | ? {$_.Identifier -match 'cpu/'} | % {$arrSensors[$_.Name]=$_.Value}
}
$temps = foreach ($sensor in $arrSensors.getEnumerator()) {
Start-Sleep -Seconds 60
$temp = $temp+$sensor.value
}
}
# Alert if average above threshold
if ($temps/$minutes -gt $tempThreshold) {
Write-Host "Alert function here"
}发布于 2021-03-28 13:51:38
我也在寻找在windows上监控cpu温度的方法。我的后端是grafana和influxdb。使用欧姆和wmi是非常占用cpu的,我找到了https://github.com/nickbabcock/OhmGraphite#influxdb-configuration。此应用程序基于ohm,但将数据直接发送到influxdb (不需要昂贵的wmi调用)。也许这也是你感兴趣的。使用grafana,您可以对该值进行平均并在其中设置阈值。
但是-也许这对你来说不是一个选择,所以这里的脚本使用欧姆应用程序来获得平均值。这个脚本的缺点是,这个脚本会运行2分钟--所以你不会每分钟收到一次警报(除非你用一分钟的appart运行它两次(这可以使用计划的任务很容易地设置)。
$tempThreshold = 80 # Will alert if average temperature is above this
$minutes = 3 # Duration in minutes to test for
start-process "$env:SystemRoot\TEMP\OpenHardwareMonitor\openhardwaremonitor.exe"
$counter = 0
$sum = 0
while($counter -lt $minutes) {
$cpuArray = Get-WmiObject -Namespace "Root\OpenHardwareMonitor" -Query "SELECT * FROM Sensor WHERE Sensortype='Temperature'"
if($cpuArray | where name -like 'cpu package*') {
$cpuTempArray = $cpuArray | where name -like 'cpu package*'
} else {
$cpuTempArray = $cpuArray | where name -like 'cpu core*'
}
foreach($cpuTemp in $cpuTempArray){
$sum += $cpuTemp.value
}
$counter++
if($counter -ne $minutes) {
Start-Sleep -Seconds 60
}
}
$average = $sum/($counter*$cpuTempArray.Count)
# Alert if average above threshold
if($average -gt $tempThreshold) {
Write-Host "Alert function here"
}发布于 2021-03-28 14:45:09
正如我提到的另一个答案,另一个脚本运行一次,IMHO不能被视为“监控”-就像“每当达到阈值时发出警报”-就像每分钟一样。
下面的脚本在无限循环中运行。它收集设定的数据量($minutes),平均每分钟。您可能希望将其更改为存储$valueArray中所有cpus的最大值,然后取该值的平均值。
$tempThreshold = 80 # Will alert if average temperature is above this
$minutes = 3 # Duration in minutes to test for
Start-Process -FilePath "$env:SystemRoot\TEMP\OpenHardwareMonitor\openhardwaremonitor.exe"
$valueArray = [System.Collections.ArrayList]@()
while($true) {
# keep only #no of values in array specified in $minutes
if($valueArray.Count -gt $minutes) {
# remove oldest entry from array
$valueArray.RemoveAt(0)
}
if($valueArray.Count -eq $minutes) {
$average = ($valueArray | Measure-Object -Sum).Sum/$minutes
# Alert if average above threshold
if($average -gt $tempThreshold) {
Write-Host "Alert function here"
}
}
$sensorArray = Get-WmiObject -Namespace "Root\OpenHardwareMonitor" -Query "SELECT * FROM Sensor WHERE Sensortype='Temperature'"
if($sensorArray | where name -like 'asdf cpu package*') {
$cpuTempArray = $sensorArray | where name -like 'cpu package*'
} else {
$cpuTempArray = $sensorArray | where name -like 'cpu core*'
}
$counter = 0
$sum = 0
foreach($cpuTemp in $cpuTempArray) {
$counter++
$sum += $cpuTemp.value
}
$currentAverageTemp = $sum/$counter
$valueArray.Add($currentAverageTemp)
Start-Sleep -Seconds 60
}https://stackoverflow.com/questions/66645892
复制相似问题