Tuesday, 20 August 2013

Using a log file to check application is running on schedule

Sometimes it's overlooked during the writing of a application that sys guys might need to monitor it. A lot of the time this leads to sys guys having to write a few lines of bash to get something that resembles monitoring in place while dev go back and improve the built-in support for monitoring.

I've removed a few senstive bits, but he is a really easy way to check if:

A) A log file has been touched in the last 10 minutes
B) That in the last 30 lines of the log file some expected text has been logged

It's not great, but it's better than nothing:

LOG_FILE=$(find /shared/logs -name "*.log" -mmin -30)
LOG_FILE_CHECK=$(find /shared/logs -name "*.log" -mmin -10 | wc -l)
LOG_FILE_RET=$(tail -30 $LOG_FILE | grep "retrieved" | wc -l)
LOG_FILE_FINISH=$(tail -30 $LOG_FILE | grep "job finished" | wc -l)

if [ $LOG_FILE_CHECK -ne 0 ] && [ $LOG_FILE_RET -ne 0 ] && [ $LOG_FILE_FINISH -ne 0 ] ; then
  echo "OK - running"
  exit 0
else echo "CRIT - not running"
  exit 2

No comments:

Post a Comment