Skip to content

Instantly share code, notes, and snippets.

@cupracer
Last active June 10, 2022 08:28
Show Gist options
  • Save cupracer/5bc0fa93cb225fe6a6c02beb8ba3906d to your computer and use it in GitHub Desktop.
Save cupracer/5bc0fa93cb225fe6a6c02beb8ba3906d to your computer and use it in GitHub Desktop.
Simple logfile monitor which counts occurrences of a string and reacts after n hits
#!/bin/bash
USE_SYSTEMD_JOURNAL=0
LOGFILE=/var/log/snapper.log
WAIT_FOR_HITS=3
SEARCH_TERM="test 123"
################
if [ ${USE_SYSTEMD_JOURNAL} -lt 1 ] && ! [ -r ${LOGFILE} ]; then
echo "couldn't read ${LOGFILE}"
exit 1
fi
COUNTER=0
while [ ${COUNTER} -lt ${WAIT_FOR_HITS} ];
do
if [ ${USE_SYSTEMD_JOURNAL} -ne 0 ]; then
( journalctl -f -n0 & ) | grep -q "${SEARCH_TERM}"
else
( tail -F -n0 ${LOGFILE} & ) | grep -q "${SEARCH_TERM}"
fi
let COUNTER=${COUNTER}+1
echo "HIT ${COUNTER} - $(date +'%Y-%m-%d %H:%M:%S')"
done
################
echo "COUNTER REACHED"
date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment