0

How do I parse a log file using only a shell script, for last one hour?

Each hour, the script should execute, parse the previous hour's logs, and the already alerted or listed errors should not repeat.

Error log example given below:

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.

I want to alert only the ERROR d line.

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
ara
  • 11
  • 1
  • 2

2 Answers2

2

Check out logtail. It's a tool for reading lines in logs that have not been read.

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

pkhamre
  • 6,120
  • 3
  • 17
  • 27
  • I am looking for a shell script only. – ara Jul 19 '12 at 07:26
  • Since you need to know what's already read, you need to keep data about that in a state-file. I don't think it will be much overhead to use the logtail2-package, as implementing a shell script for this might give you more overhead. – pkhamre Jul 19 '12 at 07:59
0

Write the script like this and call it from cron every hour:

#!/bin/bash

logfile=/var/lib/mysql/error.log
oldlogfile=/var/lib/mysql/error.log.old
email=your@mail.box.here

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
avz2012
  • 49
  • 1