Last active
September 6, 2023 16:59
-
-
Save brookst/90d6c062c61f6c67f709ddb9c84dedb0 to your computer and use it in GitHub Desktop.
No data alarm for PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check data is being written | |
# Tim Brooks Sept 2023 <[email protected]> | |
# | |
# Specify a hashmap of Contacts with WhatsApp numbers and APIKeys | |
# E.g. > $Contacts=@{'+271234567890'=123456}; .\nodata_alarm.ps1 | |
# | |
# Complete the enrolment described below to get an APIKey for each number: | |
# https://www.callmebot.com/blog/free-api-whatsapp-messages/ | |
$FileName = "foo.txt" #File that should grow in size | |
$PollTime = 5 #Check interval in seconds | |
$ReminderTime = 1800 #Send a reminder message after alerting for interval in seconds | |
if ( $Contacts -eq $Null ) { | |
Write-Host "No Contacts provided!" | |
Write-Host "Enroll on callmebot.com and provide WhatsApp and APIKey e.g.:" | |
Write-Host "> `$Contacts=@{'+271234567890'=123456}; .\nodata_alarm.ps1" | |
exit | |
} | |
function Send-Message { | |
param ( | |
$message | |
) | |
$message = [uri]::EscapeDataString($message) | |
foreach ( $number in $Contacts.Keys ) { | |
$APIKey=$Contacts[$number] | |
Invoke-WebRequest "https://api.callmebot.com/whatsapp.php?phone=$number&text=$message&apikey=$APIKey" | |
} | |
} | |
function Set-Alert { | |
$message = "Warning, no increase in data size of $FileName in past $PollTime seconds." | |
Write-Host "$message" | |
Send-Message $message | |
} | |
function Clear-Alert { | |
$message = "All clear, $FileName now increasing in size." | |
Write-Host "$message" | |
Send-Message $message | |
} | |
function Remind-Alert { | |
param ( | |
$StartTime | |
) | |
$message = "Warning, still no increase in data size of $FileName since $StartTime." | |
Write-Host "$message" | |
Send-Message $message | |
} | |
$LastLength = -1 | |
$AlertSet = $false | |
while ( $true ) { | |
$NewLength = ( Get-Item $FileName ).length | |
if ( $NewLength -eq $LastLength ) { | |
if ( $AlertSet -eq $false ) { | |
$AlertSet = $true | |
Set-Alert | |
$AlertTime = ( Get-Date ) | |
} else { | |
$delta = ( Get-Date ) - $AlertTime | |
Write-Host $delta.Seconds | |
if ( $delta.TotalSeconds % $ReminderTime -lt $PollTime ) { | |
Remind-Alert $AlertTime | |
} | |
} | |
} else { | |
if ($AlertSet -eq $true ) { | |
$AlertSet = $false | |
Clear-Alert | |
} | |
} | |
$LastLength = $NewLength | |
Start-Sleep -Seconds $PollTime | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment