简体   繁体   English

监视日志文件中的特定模式

[英]monitoring a log file for a specific pattern

I am trying to write a batch file to monitor a log file for the word 'rdy' on a line and alert if the value against rdy is less than 200 我正在尝试编写一个批处理文件,以监视一行中的单词“ rdy”的日志文件,并警告是否针对rdy的值小于200

extract from my log file as below: 从我的日志文件中提取如下:

[Sun Jun 23 11:00:00 2013] [notice] mpmstats: rdy 249 bsy 1 rd 0 wr 1 ka 0 log 0 dns 0 cls 0
[Sun Jun 23 11:00:02 2013] [error] [client 10.25.134.1] File does not exist: E:/htdocs/default/KeepAlive.html

I have written a basic script ( Still on my L's ) which monitors the error.log file in a particular directory. 我编写了一个基本脚本(仍在我的L上),该脚本监视特定目录中的error.log文件。 The issue is there are error logs from multiple days and I want to monitor the current error log. 问题是有几天以来的错误日志,我想监视当前的错误日志。

@echo off

set log=E:\scripts\busycheckalert.log
set Time=%time:~0,5%
set Today=%date:~4,2%
set Month=%date:~7,2%
set Year=%date:~12,2%
set file=E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%

echo Polling %file% at %Time% >> %log%
for /f "usebackq delims=;" %%a in (`dir /b   E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%`) do (
    echo Checking now >> %log%
    for /f "tokens=8,9 delims= " %%a in     (E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%) do (
        echo Doing Checks >> %log%
        if %%j LEQ 200 echo %Today%-%Month%-%Year% at %Time% Error - Ready threshold exceeded >> %log% in %%a ))

I manage to get till the first checkpoint " Checking now". 我设法到达第一个检查站“ Checking now”。 However, it seems it doe not enter that 2nd loop. 但是,似乎没有进入第二个循环。

This is the extract from the resultant log file: 这是从结果日志文件中提取的内容:

Polling E:\logs\ihs\Default\error.log.06.23.13 at 22:48 
Checking now 
Polling E:\logs\ihs\Default\error.log.06.23.13 at 22:49 
Checking now 
Polling E:\logs\ihs\Default\error.log.06.23.13 at 22:50 
Checking now 

Could you please advise where I am going wrong? 您能告诉我我要去哪里了吗? Any help would be great. 任何帮助都会很棒。

Thanks 谢谢

The issue is there are error logs from multiple days and I want to monitor the current error log.

for /f "delims=" %%a in (' dir /b /a-d /od *.log ') do set "latest_file=%%a"

I gather now that it's not your aim. 我收集到的不是您的目标。

You seem to be using the same log file name as the file you are processing. 您似乎正在使用与正在处理的文件相同的日志文件名。

You have delims=; 你有delims =; where there are no ; 没有的地方; in your log snippet. 在您的日志片段中。

You are reusing %%a in both loops. 您正在两个循环中重用%% a。

set file=E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%

echo Polling %file% at %Time% >> %log%
for /f "usebackq delims=;" %%a in (`dir /b   E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%`) do (
    echo Checking now >> %log%
    for /f "tokens=8,9 delims= " %%a in     (E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%) do (
for /f "tokens=8,9 delims= " %%a in     (E:\logs\ihs\Default error.log.%Month%.%Today%.%Year%) do (

Hmmm- now I wonder what would happen if you were to change the %%a to, say, %%i ? 嗯,现在我想知道如果您将%%a更改为%%i会发生什么?

You appear not to be checking that %%i ==rdy either. 您似乎也没有检查%%i == rdy。 If you don't do that, you may land up with some rather odd results. 如果不这样做,您可能会得到一些相当奇怪的结果。

Suggestion with code for GNUWin32 : 建议使用GNUWin32 代码:

@echo off & setlocal
set "cTime=%time:~0,5%"
set "Today=%date:~3,2%"
set "Month=%date:~6,2%"
set "Year=%date:~11,2%"
set "file=E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%"
SET "log=resultant.log"

echo Polling %file% at %Time% >> "%log%"
for /f "delims=" %%a in ('dir /b /a-d "%file%') do (
     echo Checking now >> "%log%"
     for /f "tokens=3" %%b IN ('grep -o "mpmstats: rdy [0-9]\+" "%file%"') do SET "rdy=%%b"
     echo Doing Checks >> "%log%"
     SETLOCAL ENABLEDELAYEDEXPANSION
     if !rdy! LEQ 200 echo %Today%-%Month%-%Year% at %cTime% Error - Ready threshold exceeded IN %%a >> "%log%"
     endlocal
)

You are using a specific file in your FOR loops, so there is no need for two of them. 您在FOR循环中使用的是特定文件,因此不需要两个。 The first one merely confirms that the file exists. 第一个仅确认文件存在。 That is easier and more efficiently done with: 使用以下方法可以更轻松,更有效地完成此任务:

if exist "E:\logs\ihs\Default\error.log.%Month%.%Today%.%Year%" ( ... )

Others have said you have a problem using %%a for both loops. 其他人则说您在两个循环中都使用%%a遇到了问题。 Actually, there is nothing wrong with reusing a character within nested loops. 实际上,在嵌套循环中重用字符没有错。 But then your inner loop cannot access the outer loop value. 但是,您的内部循环无法访问外部循环值。 Given that your inner loop DO references %%j , I suspect you intended for your inner loop to use %%i instead of %%a . 鉴于您的内部循环DO引用了%%j ,我怀疑您打算让您的内部循环使用%%i而不是%%a

Your logic is wrong in that your loop is processing all lines, when it should only process lines that contain " rdy ". 您的逻辑是错误的,因为您的循环仅处理包含“ rdy”的行,因此正在处理所有行。 FIND or FINDSTR can be used to efficiently filter out unwanted lines. FIND或FINDSTR可用于有效过滤掉不需要的行。

You should never assign your own value to a variable named TIME (note that variable names are case insensitive). 永远不要将自己的值分配给名为TIME的变量(请注意,变量名不区分大小写)。 Doing so prevents you from later accessing the dynamic time value. 这样做会阻止您以后访问动态时间值。

I haven't figured out what would prevent entry to your inner loop when the outer loop works. 我还没有弄清楚当外循环工作时什么会阻止您进入内循环。 But I would restructure your entire code. 但我会重组您的整个代码。

Instead of deriving the name of the log file from the current date, I would list all log files in date/time order and use FOR /F to capture the last one found. 与其从当前日期获取日志文件的名称,不如按日期/时间顺序列出所有日志文件,并使用FOR / F捕获找到的最后一个文件。

Then I would use another FOR /F to parse the output of a FINDSTR search for " rdy " 然后,我将使用另一个FOR / F来解析FINDSTR搜索“ rdy”的输出

@echo off
setlocal
set "log=E:\scripts\busycheckalert.log"
set "checkTime=%time:~0,5%"
pushd "E:\logs\ihs\Default"

set "currentLog="
for /f "delims=" %%F in ('dir /b /a-d /od "error.log.*"') do set "currentLog=%%F"
if defined currentLog (
  >>"%log%" echo Polling %currentLog% at %checkTime%
  for /f "tokens=9" %%A in ('findstr /c:" rdy " "%currentLog%"') do (
    if %%A leq 200 >>"%log%" echo Error at %time%: Ready threshold exceeded in %currentLog% 
  )
) else  >>"%log%" echo No log found at %checkTime%"

popd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM