Question

There is /var/log/httpd/error_log file which contains errors from the whole httpd.

I want to be able to send those new log via email message if any appears - with for example 15 minutes interval. I would like to use CRON to call bash script that will send new content.

Is there any efficience way to obtain, which lines were appended since last check ?

Was it helpful?

Solution

When sending a mail, you can store the line count of the file somewhere (e.g., a file), and then use tail +n to print only from the n-th line on, like

last_linecount=`cat .last_linecount` # or check if file does not exist
wc -l /var/log/httpd/error_log >.last_linecount
tail -n +$((last_linecount + 1)) /var/log/httpd/error_log | whatever

You should also check if the current number of lines is lower than last_linecount, as then the logfile might have been rotated (if applicable) and you have to combine both the tail on the old logfile and everything from the new logfile.

OTHER TIPS

You can use the error_log as a marker and don't need store line numbers in the external file.

the next script:

#!/bin/bash

STRING="last_log_check_point_string"
LOGFILE="/opt/local/apache2/logs/error_log"
URL="http://localhost/$STRING"

linenum=$(grep -n "$STRING" $LOGFILE | tail -1 | sed 's/:.*//')
curl "${URL}" >/dev/null 2>&1
[[ ! -z "$linenum" ]] && sed -n "$linenum,\$p" < $LOGFILE | mail -s "error log at $(date)" "your@email.com" 
  • will find the line number of the last occurence of last_log_check_point_string
  • will make an request to your web with nonexistent url what contain your string (so, you get an new notice into the error_log - the new marker_point)
  • every line after the above line number will mail to you.

The last line of the error_log will be the new marker point, so you have a control how the script works. If you don't have curl, use wget - or is possible make a request with a pure bash too.

or the variant - you will get email only when some errors occured

#!/bin/bash

STRING="last_log_check_point_string"
LOGFILE="/opt/local/apache2/logs/error_log"
URL="http://localhost/$STRING"
TEMPFILE="/tmp/hterror.$$"

linenum=$(grep -n "$STRING" $LOGFILE | tail -1 | sed 's/:.*//')
curl "${URL}" >/dev/null 2>&1
[[ ! -z "$linenum" ]] && sed -n "$linenum,\$p" < $LOGFILE | grep -v "$STRING" >$TEMPFILE
[[ -s "$TEMPFILE" ]] && mail -s "error log at $(date)" "your@email.com"  < $TEMPFILE
rm -f "$TEMPFILE"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top