I've to intercept a pattern in a log file and, when this pattern is matched for 5 times in 5 minutes, send an alert...
I've wrote this lines, but I'm stuck... I don't know how to handle the time frame...
Can you help me please?
Roberto.
$count = get-content test.txt -Tail 1 -Wait | select-string -pattern "auth" -simplematch
$count.length
I've tried other approach, but nothing to do...
$file = "test.txt"
$mymatch = "auth"
#$FirstEventTime = Get-Date
#$LastEventTime = Get-Date
$Err = 0
$Range =@(1,2,3,4)
$Tailfile = Get-Content $file -Tail 1 -Wait | select-string -pattern $mymatch -simplematch | foreach ($mymatch) {
if($Err -lt 1){
New-Event -SourceIdentifier StartEvent -Sender windows.timer
$Err++
}
#elseif ($Err -eq [1-4])
elseif ($Range -contains $Err){
$Err++ }
else {
New-Event -SourceIdentifier LastEvent -Sender windows.timer
$FisrtEvent = (get-event StartEvent).TimeGenerated
$LastEvent = (get-event LastEvent).TimeGenerated
$Elapsed = $LastEvent-$FisrtEvent
$realElasped = ($elapsed).TotalMinutes
[math]::Round($realElasped)
if ($realElasped -lt 5) {
echo "sendmail......"
$Err = 0}
else {
$Err = 0
}
}
}