Tomcat 来自Powershell日志文件的警报

Tomcat 来自Powershell日志文件的警报,tomcat,powershell,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.a

我想通过电子邮件提醒我,当它所花的时间来服务一个页面超过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
我希望通过电子邮件发送给我的是达到阈值的日期和时间以及加载页面所需的时间


提前谢谢

如果我得到正确答案,您的Tomcat日志将使用两行purr事件

下面是PowerShell的一些行,它们将扫描整个日志文件一次,并针对
起始时间大于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'
        }
    }
}

虽然文件的写入时间没有改变,但在100毫秒内只需休眠并再次检查。当写入时间发生变化时,找出写入文件的内容,并仅解析写入文件的新信息

我不知道如何读取日志文件。您现在看到的是出现在某些行开头的
02-Apr-2013 10:19:50
?嘿,谢谢您的评论。是的,有点乱。我只想知道从2013年4月2日10:19:57开始到结束的几行…| home-time=14000 ms。这是一个Tomcat日志,它似乎把事情分成了两行。时间=xxxx是我需要监控的。这看起来很棒,非常适合更好地了解正在发生的事情。但要使其实时化,您的代码是否可以包含在这样的内容中:?我认为实现实时性的另一种方法是在日志中的行上标记“Checked”。然后在代码中只检查不带“Checked”的行,然后在选中时添加单词。等等,不。。废话,这是Windows服务器,Tomcat将锁定该文件。。该死。您可以尝试获取“checked”行值所在的行号,然后将其写入另一个文件或XML,只需从“checked line”文件的最后一个条目开始解析文件即可。@ChristopherRanney这听起来像是实现“实时”解决方案的更简洁的方法。我该怎么做呢?我会用你提供的相同日志文件给你答复。它很可能需要在服务器上安排作业,可以吗?嗨@ChristopherRanney。你有机会调查一下吗?
$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]
    ...
}