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.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 日志使用 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 毫秒后再次检查。当写入时间确实发生变化时,找出写入文件的内容并仅解析写入文件的新信息。

关于tomcat - 使用 Powershell 从日志文件发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15765188/

相关文章:

java - hibernate 中打开的 session 是否一定意味着我已连接到数据库?

android - 未找到证书路径异常的信任 anchor

java - 需要在 Windows 2008 R2 上的压缩文件中按天导出 IIS 日志和 Tomcat 日志

powershell - Powershell中的值(value)没有增长

Powershell:$^和$$的实际用法?

powershell - 如何真正了解robocopy返回码2

tomcat - 如何登录jar库的静态 block

tomcat - 名为 [create_subscription] 和 [servlet.create] 的 servlet 都映射到不允许的 url-pattern [/create]

Java - Hibernate 可以在 persistence.xml 中的两个数据库配置之间进行选择吗?

powershell - 无法从 Windows PowerShell 命令提示符运行 Java