ban2fail/ban2fail.sh

57 lines
1.3 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-27 22:31:15 +00:00
INOTIFYWAIT=/usr/bin/inotifywait
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
while ! $TIME $BAN2FAIL; do
sleep 1
done
2019-11-23 15:50:13 +00:00
2019-11-27 22:31:15 +00:00
done < <(exec $INOTIFYWAIT -m $LOG_NAMES)
2019-11-23 15:50:13 +00:00
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