使用Powershell从日志文件中进行警报

时间:2013-04-02 13:19:31

标签: tomcat powershell

我希望通过电子邮件收到通知,因为服务页面的时间超过10秒。我无法使用外接显示器,因为这是一个封闭的系统。

以下是我想要观看的日志文件的片段。

02-Apr-2013 10:19:50 com.domain.service.core.actions.aaa.GetInfo handleAction
SEVERE: Post call to Widget Service Request WS
02-Apr-2013 10:19:50 com.domain.service.core.actions.aaa.GetInfo handleAction
SEVERE: ERROR: cannot attach result to XML doc
02-Apr-2013 10:19:50 org.apache.jsp.run _jspService
INFO: 35 |  |  | ffffHM6Ly8qLmhhcnJvdy5nb3YudWsv | page1 - time = 7905 ms
02-Apr-2013 10:19:55 org.apache.jsp.run _jspService
INFO: 35 |  |  | ODeeeeeeM6Ly8qLmhh45y4y55YudWsv | summary - time = 7800 ms
02-Apr-2013 10:19:56 org.apache.jsp.run _jspService
INFO: 35 |  |  | ODeeeeeeM6Ly8qLmhh45y4y55YudWsv | page2 - time = 3430 ms
02-Apr-2013 10:19:56 org.apache.jsp.run _jspService
INFO: 35 |  |  | ODeeeeeeM6Ly8qLmhh45y4y55YudWsv | page1 - time = 4210 ms
02-Apr-2013 10:19:56 org.apache.jsp.run _jspService
INFO: 35 |  |  | ODeeeeeeM6Ly8qLmhh45y4y55YudWsv | page3 - time = 4370 ms
02-Apr-2013 10:19:56 org.apache.jsp.run _jspService
INFO: 35 |  |  | ODeeeeeeM6Ly8qLmhh45y4y55YudWsv | page1 - time = 5708 ms
02-Apr-2013 10:19:56 com.domain.service.netServ.netServSTUFF createSession
INFO: creating session. token: xxxxxx
02-Apr-2013 10:19:56 com.domain.service.netServ.netServSTUFF createSession
INFO: creating session. encrypted token: xxxxxx
02-Apr-2013 10:19:56 com.domain.service.core.actions.user.CreateNetsolSession handleAction
INFO: netServ Token: GisLCpQwnMrMEWa5bHuQQw++
02-Apr-2013 10:19:57 org.apache.jsp.run _jspService
INFO: 35 |  |  | ODeeeeeeM6Ly8qLmhh45y4y55YudWsv | home - time = 14000 ms

我希望通过电子邮件发送给我的是达到阈值的日期和时间以及加载页面的时间。

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果我更正确,你的Tomcat日志会使用2行purr事件。

以下是PowerShell的一些行,它将扫描整个日志文件一次,并为home - time大于10秒的每个实例发送一封电子邮件。

$file = Get-Content tomcatlog.txt

$lines = $file.Count
for($i=0;$i-lt$lines;$i++){

#scaning the file for strings like  "home - time = #### ms"
if($file[$i] -like '*home - time = * ms'){

        #The date will be on the previous line
        $date = $file[$i-1].Substring(0,20)

        #Get the Milliseconds
        $time_index = $file[$i].LastIndexOf('=') + 2
        $time = [int]($file[$i].Substring($time_index).Replace(' ms',''))

        #Calculate the seconds
        $seconds = $time/1000

        "Line $i`: On $date the time is took to load was $sec seconds"

        if($seconds -ge 10){
            Send-MailMessage `
            -To 'Michael Bluth <Michale.Bluth@BluthCompany.com>' `
            -From 'Bob Loblaw <Bob.Loblaw@BobLoblaw.com>' `
            -Subject 'Tomcat' `
            -Body "Line $i`: On $date the time is took to load was $sec seconds" `
            -SmtpServer 'smtp.bobloblaw.com'
        }
    }
}

现在,从您的问题来看,您可能希望对此日志文件进行实时监控。因此,只要将新事件添加到日志中,并且home - time = 11000 ms大于10秒,就发送电子邮件。如果您只是运行此脚本一天,您将收到前一天的重复电子邮件,因为日志文件是相同的。

现在,如果您在每天开始时创建了一个新的日志文件,并且在一天结束时运行了该脚本,那将会改变一些事情。因此,在脚本结束时,您可以添加一行来重命名日志文件Rename-Item -Path C:\tomcatlog.txt -NewName "$(Get-Date -UFormat %y%m%d)_tomcatlog.txt",并假设您的服务器将重新创建Tomcat日志。这样每天你只会收到那天的电子邮件。

您可以将重复次数加速到每12小时,或每6小时或每1小时一次。你会积累很多文件,但它会接近实时。

尝试2

$new_log_path = 'tomcatlog.txt'
$old_log_path = 'copy_tomcatlog.txt'

$lastwritetime = (Get-Item $new_log_path).LastWriteTime
while($lastwritetime -eq (Get-Item $new_log_path).LastWriteTime){
    Start-Sleep -Milliseconds 100
}

$newfile = Get-Content $new_log_path
$oldfile = Get-Content $old_log_path #Just a copy of the old original log file

#You calculate the difference of what was just written to the file
$updates = Compare-Object -ReferenceObject $newfile -DifferenceObject $oldfile

#Copy the file so we can as reference of what already was parsed
Copy-Item -Path $new_log_path -Destination $old_log_path -Force

$len = $updates.count
for($i=0;$i-lt$len;$i++){
    #it would be $updates[$i].InputObject instead of $file[$i]
    ...
}

虽然文件的写入时间不会改变,只是睡眠并在100毫秒内再次检查。当写入时间确实改变时,找出写入文件的内容并仅解析写入其中的新信息。