如何在最后一个小时内仅使用shell脚本parsing日志文件?
每个小时,脚本应该执行,parsing前一小时的日志,并且已经提醒或列出的错误不应该重复。
下面给出的错误日志示例:
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行。
检查logtail 。 这是读取日志中尚未读取的行的工具。
http://manpages.ubuntu.com/manpages/hardy/man8/logtail2.8.html
写这样的脚本,每小时从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