如何使用简单的 shell 脚本解析一小时的日志文件?

如何使用简单的 shell 脚本解析一小时的日志文件?

如何使用解析日志文件仅有的一个 shell 脚本,持续一小时?

每个小时,脚本都应该执行,解析前一小时的日志,并且已经警告或列出的错误不应该重复。

错误日志示例如下:

120622 13:06:36 mysqld_safe Starting mysqld daemon with databases from <path>
120622 13:06:36 [Note] Plugin'FEDERATED' is disabled.
120622 13:06:36  InnoDB: Initializing buffer pool, size = \78.0M
120622 13:06:36  InnoDB: Completed initialization of buffer pool
InnoDB: No valid checkpoint found.
InnoDB: If this error appears when you are creating an InnoDB database,
InnoDB: the problem may be that during an earlier attempt you managed
InnoDB: to create the InnoDB data files, but log file creation failed.
InnoDB: If that is the case, please refer to
InnoDB: http:<path>error-creating-innodb.html
120622 13:06:36 [ERROR] Plugin 'InnoDB' init function returned error.
120622 13:06:36 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
120622 13:06:36 [Note] Event Scheduler: Loaded 0 events
120622 13:06:36 [Note] <path>mysqld: ready for connections.

我只想警告 ERROR d 行。

答案1

查看logtail。这是一个用于读取日志中尚未读取的行的工具。

http://manpages.ubuntu.com/manpages/hardy/man8/logtail2.8.html

答案2

像这样编写脚本并每小时从 cron 调用它:

#!/bin/bash

logfile=/var/lib/mysql/error.log
oldlogfile=/var/lib/mysql/error.log.old
[email protected]

if [ -f $oldlogfile ]; then
  diff $oldlogfile $logfile | grep ERROR > /dev/null
  if [ $? -eq 0 ]; then
    diff $oldlogfile $logfile | grep ERROR | mail -s 'last hour mysql errors' $email
  fi
fi
cp -p $logfile $oldlogfile

相关内容