ban2fail/ban2fail.sh

55 lines
1.2 KiB
Bash
Raw Normal View History

#!/bin/bash -e
2019-11-23 15:50:13 +00:00
#
# JDR Wed 27 Nov 2019 01:30:29 PM EST
# The purpose of this script is to be run from a systemd service
# file, or sysvinit script.
2019-11-23 15:50:13 +00:00
#
2019-11-25 14:12:19 +00:00
BAN2FAIL=/usr/local/bin/ban2fail
BAN2FAIL_CFG=/etc/ban2fail/ban2fail.cfg
2019-11-23 15:50:13 +00:00
# Uncomment this if you wish to see output from the time command
#TIME=time
2019-11-23 15:50:13 +00:00
# Always do initial check
echo "Initial run for $BAN2FAIL"
$TIME $BAN2FAIL
2019-11-23 15:50:13 +00:00
while true; do
echo "Starting main loop"
LOG_NAMES=$($BAN2FAIL --print-lfn | tr $'\n' ' ')
LOG_NAMES="$LOG_NAMES $BAN2FAIL_CFG"
2019-11-23 15:50:13 +00:00
echo "Monitoring: $LOG_NAMES"
2019-11-23 15:50:13 +00:00
while read; do
# if a file gets renamed, logrotate is doing it's thing.
[[ "$REPLY" =~ MOVE_SELF ]] && break
[[ "$REPLY" == $BAN2FAIL_CFG\ MODIFY ]] && break
2019-11-23 15:50:13 +00:00
[[ "$REPLY" =~ MODIFY ]] || continue
2019-11-23 15:50:13 +00:00
# Uncomment this to see the inotifywait output which triggered this cycle
#echo "REPLY= '$REPLY'"
2019-11-23 15:50:13 +00:00
2019-11-27 20:06:13 +00:00
# Avoid running ban2fail multiple times if possible
while read -t 0; do
read
done
echo "Running $BAN2FAIL"
# Check for offenses
# If ban2fail failed, then pause to avoid DOS on CPU
$TIME $BAN2FAIL || sleep 1
2019-11-23 15:50:13 +00:00
2019-11-27 20:06:13 +00:00
done < <(exec /usr/bin/inotifywait -m $LOG_NAMES)
2019-11-23 15:50:13 +00:00
date | tr -d $'\n'
echo ' Exiting main loop'
2019-11-23 15:50:13 +00:00
sleep 1
2019-11-23 15:50:13 +00:00
done
exit 0