022014

Log File Change Script Andrew Mallett | Updated February 2014

This versatile script tests for increases in a file's size. I wrote it as a way to monitor an Apache web server's access log, which grows when a web page is viewed. It can be used to monitor any log file which increases in size when its application is accessed.

The script runs every five minutes as a cron job (hence full paths are used) and utilises a nifty utility called sendEmail to send an administrative alert..

cd /usr/local/apache/logs

/usr/bin/stat -s access_log | cut -d" " -f8 | cut -c9-20 > new_size   

 if [ $(more new_size) -gt $(more prev_size) ]; then
 /sc/apache/emailapacheused.sh
 else
 echo
 fi

rm prev_size

mv new_size prev_size

The second line uses the stat command and cuts out all characters except the file size of the apache access_log file and then outputs this to a new file called new_size

On the third line, the script compares a newly-created new_size file to the previous prev_size file, to see if it is bigger.

If the newer file is larger in size, then line four sends an administrative alert email using a second script, emailapacheused.sh

If the file size hasn't changed then it simply echoes nothing.

The older prev_size file is deleted and finally the script renames (moves) the new_size file to prev_size and is ready to run again.

Note that before the script runs for the first time, be sure to create an original prev_size file for new_size to be compared to. This can be achieved, by running the second line of the script manually and outputting it to /usr/local/apache/logs/prev_size.

The code can easily be modified to monitor anything that a command like stat can tell you about a file.

Back

Comments (1)

---
Log file change size = somethings happened . I like it! :-O
#1 - ECummings - 12/13/2014 - 05:40
Name
E-mail (Will not appear online)
Homepage
Title
Comment
;-) :-) :-D :-( :-o :-O B-) :oops: :-[] :-P
To prevent automated Bots form spamming, please enter the text you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
»
This comment form is powered by GentleSource Comment Script. It can be included in PHP or HTML files and allows visitors to leave comments on the website.

PHP Script   Throwaway Email   Temporary Email