I have modified not.you's code, cleaning it up a bit and adding the ability to just text you when your miner has stopped or text you and automatically reboot. It has been working for about 2 weeks without false alarms and 1 needed reboot.
Name the code below with file name MinitorMining.ps1
$LogFile = "MiningMonitor.log"
$ProcessName = "openhardwaremonitor"
$Date = Get-Date
$TooLow = 40 # This is the Celsius temperature you wish to "flag" as an offline GPU
$TooHigh = 70 # This temperature triggers a warning text
$lowestReportedTemp = 1000 # Remember to check that HardwareMonitor is reporting in Celsius
$numIterations = 9 # Set the number of iterations to test before considering a "failed" miner process.
$rebootIfLow = $true # Set to true to send text and reboot (production) when low temps are found. Set to false to only send text.
# Textual elements
$textBodyReboot = "At $Date, Miner rebooted - GPU temperature dropped to:"
$textBodyWarning = "At $Date, GPU temperature dropped to:"
# Constants for email message
$senderEmail = 'youremail@yourprovider.com'
$senderPassword = 'emailpassword'
# you must set this for your phone number and carrier. The list is here: http://www.emailtextmessages.com/
$textNumberAndCarrier = '1234567899@txt.yourcarrier.net'
$textSubject = 'Miner Message';
$smtpServer = 'smtp.mail.yourprovider.com'
$port = '587'
$useSsl = $true
if ($rebootIfLow) {
Write-Host "Monitor will send Message and Reboot if Needed"
$textBody = $textBodyReboot
}
else {
Write-Host "Monitor is in Message Only Mode"
$textBody = $textBodyWarning
}
# [console]::TreatControlCAsInput = $true
try
{
#Test if openhardwaremonitor is running and if not, start it
if((get-process $ProcessName -ErrorAction SilentlyContinue) -eq $Null)
{ Start-Process -FilePath ".\OpenHardwareMonitor.exe" -WorkingDirectory ".\"} # -WindowStyle Minimized; echo "Starting OpenHardwareMonitor..." }
else
{ echo "OpenHardwareMonitor is running" }
$countOfIterationsLow = 0
#if the computer just started or OpenHardwareMonitor is slow starting we will have problems, so insert wait here
Start-Sleep 120
For ($i=1; $i -le $numIterations; $i++) {
# Query GPU temperature from OpenHardwareMonitor
$GPUTempObj = Get-WmiObject -namespace root\openhardwaremonitor -class sensor | Where-Object {$_.SensorType -Match "temperature" -and $_.Identifier -like "*gpu*"}
$GPU_Num = 0;
ForEach($GPU In $GPUTempObj)
{
$GPUTemp = $GPU.value
if($GPUTemp -lt $TooLow)
{
Write-Host "GPU$GPU_Num is $GPUTemp, below cutoff of $TooLow"
"$Date - GPU$GPU_Num is $GPUTemp, below cutoff of $TooLow" | Out-File $LogFile -Append
$countOfIterationsLow = $countOfIterationsLow + 1
if ($GPUTemp -lt $lowestReportedTemp) { $lowestReportedTemp = $GPUTemp }
}
else
{
Write-Host "GPU$GPU_Num is OK at $GPUTemp degrees (above $TooLow)"
}
$GPU_Num++
}
if ($Host.UI.RawUI.KeyAvailable -and (3 -eq [int]$Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho").Character))
{
Write-Host "You pressed CTRL-C. Do you want to continue Mining Monitor (Y/N)?"
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
if ($key.Character -eq "N") { break; }
}
#if we have bad timing on a driver crash (and recovery) or work restarts we may get low results, so we wait between tests
if ($i -lt $numIterations) {
Write-Host "- - - - - - - - - - -"
Start-Sleep 20
}
}
# All tests (determined by &numIterations) have to get a low result restart
if($countOfIterationsLow -ge $numIterations)
{"$Date - $textBody $lowestReportedTemp on $countOfIterationsLow occasions with cutoff of $TooLow - restarting" | Out-File $LogFile -Append
$MailArgs = @{
From = $senderEmail
To = $textNumberAndCarrier
Subject = $textSubject
Body = "$textBody $lowestReportedTemp on $countOfIterationsLow occasions with cutoff of $TooLow"
SmtpServer = $smtpServer
Port = $port
UseSsl = $useSsl
Credential = New-Object pscredential $senderEmail,$($senderPassword |ConvertTo-SecureString -AsPlainText -Force)
}
Send-MailMessage @MailArgs
if ($rebootIfLow) {
Restart-Computer -force
}
}
elseif ($lowestReportedTemp -lt $TooLow)
{"$Date - $countOfIterationsLow results below cutoff, lowest was $lowestReportedTemp with report cutoff of $TooLow" | Out-File $LogFile -Append}
else
{"$Date - No concerning results, cutoff temperature currently: $TooLow" | Out-File $LogFile -Append}
}
finally
{
"$Date - Mining Monitor Stopped" | Out-File $LogFile -Append
}
Below are the contents of batch file to start it. I named mine "StartMiningMonitor.bat", but it can be named anything you want
@echo off
set counter=0
:start
cls
set /a counter=counter+1
echo Mining Monitor loop %counter%
powershell.exe .\MonitorMining.ps1
TIMEOUT /T 30
goto start
NOTE: This script has to be placed in the same folder as OpenHardwareMonitor. If you want to place it somewhere else, the following line has to be modified:
{ Start-Process -FilePath ".\OpenHardwareMonitor.exe" -WorkingDirectory ".\"} # -WindowStyle Minimized; echo "Starting OpenHardwareMonitor..." }
You are free to use and adapt to your needs. I would appreciate improvement suggestions and feedback.
othgeek