I'm working on creating a tripwire script, so that I'm alerted to certain filetypes being created in a folder. It works as expected for the most, although I had intended to receive an e-mail with ALL files changes occurring in a 5 minute period (to batch them, rather than an e-mail each time).
However, it seems to e-mail be batches of 8 files at a time and spaces sending the e-mail every 5 minutes. For example, if I triggered 80 file alerts in 1 minute, it would alert me to 8 files changing each 5 minutes, taking 10 e-mails to notify me of all changes, rather than just 1.
Can you see anything in this script that would cause this problem?
However, it seems to e-mail be batches of 8 files at a time and spaces sending the e-mail every 5 minutes. For example, if I triggered 80 file alerts in 1 minute, it would alert me to 8 files changing each 5 minutes, taking 10 e-mails to notify me of all changes, rather than just 1.
Can you see anything in this script that would cause this problem?
Code:
### SERVER NAME
$serverName = "TestServer"
### SET PHP FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$phpwatcher = New-Object System.IO.FileSystemWatcher
$phpwatcher.Path = "C:\www\vhosts"
$phpwatcher.Filter = "*.php"
$phpwatcher.IncludeSubdirectories = $true
$phpwatcher.EnableRaisingEvents = $true
### SET SQL FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$sqlwatcher = New-Object System.IO.FileSystemWatcher
$sqlwatcher.Path = "C:\www\vhosts"
$sqlwatcher.Filter = "*.sql"
$sqlwatcher.IncludeSubdirectories = $true
$sqlwatcher.EnableRaisingEvents = $true
### SET ZIP FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$zipwatcher = New-Object System.IO.FileSystemWatcher
$zipwatcher.Path = "C:\www\vhosts"
$zipwatcher.Filter = "*.zip"
$zipwatcher.IncludeSubdirectories = $true
$zipwatcher.EnableRaisingEvents = $true
### SET RAR FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$rarwatcher = New-Object System.IO.FileSystemWatcher
$rarwatcher.Path = "C:\www\vhosts"
$rarwatcher.Filter = "*.rar"
$rarwatcher.IncludeSubdirectories = $true
$rarwatcher.EnableRaisingEvents = $true
### SET EMAIL SETTINGS
$From = "[email protected]"
$To = "[email protected]"
$SMTPServer = "smtp.example.com"
$SMTPPort = "587"
$Username = "[email protected]"
$Password = "password"
$subject = "File Change Log - $serverName"
$global:body = @()
### DEFINE PHP ACTIONS AFTER A EVENT IS DETECTED
$phpaction = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), ${changeType}: $path"
Add-content "$PSScriptRoot\Logs\PHP\$(get-date -f yyyy-MM-dd).txt" -value $logline
Write-Output $logline
$global:changes = "true"
$global:body += $logline
$global:body += "`r`n"
}
### DEFINE SQL ACTIONS AFTER A EVENT IS DETECTED
$sqlaction = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), ${changeType}: $path"
Add-content "$PSScriptRoot\Logs\SQL\$(get-date -f yyyy-MM-dd).txt" -value $logline
Write-Output $logline
$global:changes = "true"
$global:body += $logline
$global:body += "`r`n"
}
### DEFINE ZIP ACTIONS AFTER A EVENT IS DETECTED
$zipaction = {
if ($Event.SourceEventArgs.Name -notlike "*.log.zip") {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), ${changeType}: $path"
Add-content "$PSScriptRoot\Logs\ZIP\$(get-date -f yyyy-MM-dd).txt" -value $logline
Write-Output $logline
$global:changes = "true"
$global:body += $logline
$global:body += "`r`n"
}
}
### DEFINE RAR ACTIONS AFTER A EVENT IS DETECTED
$raraction = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), ${changeType}: $path"
Add-content "$PSScriptRoot\Logs\RAR\$(get-date -f yyyy-MM-dd).txt" -value $logline
Write-Output $logline
$global:changes = "true"
$global:body += $logline
$global:body += "`r`n"
}
### DECIDE WHICH PHP EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$phpcreated = Register-ObjectEvent $phpwatcher "Created" -Action $phpaction
$phpchanged = Register-ObjectEvent $phpwatcher "Changed" -Action $phpaction
$phpdeleted = Register-ObjectEvent $phpwatcher "Deleted" -Action $phpaction
$phprenamed = Register-ObjectEvent $phpwatcher "Renamed" -Action $phpaction
### DECIDE WHICH SQL EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$sqlcreated = Register-ObjectEvent $sqlwatcher "Created" -Action $sqlaction
$sqlchanged = Register-ObjectEvent $sqlwatcher "Changed" -Action $sqlaction
$sqldeleted = Register-ObjectEvent $sqlwatcher "Deleted" -Action $sqlaction
$sqlrenamed = Register-ObjectEvent $sqlwatcher "Renamed" -Action $sqlaction
### DECIDE WHICH ZIP EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$zipcreated = Register-ObjectEvent $zipwatcher "Created" -Action $zipaction
$zipchanged = Register-ObjectEvent $zipwatcher "Changed" -Action $zipaction
$zipdeleted = Register-ObjectEvent $zipwatcher "Deleted" -Action $zipaction
$ziprenamed = Register-ObjectEvent $zipwatcher "Renamed" -Action $zipaction
### DECIDE WHICH RAR EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$rarcreated = Register-ObjectEvent $rarwatcher "Created" -Action $raraction
$rarchanged = Register-ObjectEvent $rarwatcher "Changed" -Action $raraction
$rardeleted = Register-ObjectEvent $rarwatcher "Deleted" -Action $raraction
$rarrenamed = Register-ObjectEvent $rarwatcher "Renamed" -Action $raraction
while ($true) {
sleep 300
if ($global:changes -eq "true") {
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($From, $To, $subject, $body);
Write-Output "E-Mail Sent"
$global:changes = "false"
$global:body = @()
$global:body += "PHP Change Log - $serverName"
$global:body += "`r`n"
}
}