On VPS systems with QoS limits you frequently hit the iptables rules limit. Especially if you run portsentry and BFD, the limit is reached rather quickly. The first thing to do is ask support to raise the default limit because its too low (i've asked to set it to 1000, which fills up within a week or so).
STEP 1 - the script
Copy the following code into a text file, name it something like "ipbanreset" and place it in your "/root/bin" directory.
Code:#!/bin/bash # Make sure we are running under a VPS system with QoS if [ ! -f "/proc/user_beancounters" ] then echo "File \"/proc/user_beancounters\" does not exist. Make sure this script runs on a VPS system with QoS enabled." exit $E_NOFILE fi # Get the iptables hard limit iplimit=` tail /proc/user_beancounters | grep numiptent | awk '{print$5}' ` # Get the 90% of the limit, convert to integer via scale iplimit_soft=` echo "scale=0; $iplimit * 0.90 / 1" | bc -l ` # Get the iptables current count ipcurrent=` tail /proc/user_beancounters | grep numiptent | awk '{print$2}' ` # If the current count not greater or equal (less than) to the soft limit, exit if [ "$ipcurrent" -lt "$iplimit_soft" ] then exit 0 fi hostname=`hostname` echo "Warning: iptables rule limit has been reached (over 90% full) on $hostname" echo "Hard limit: $iplimit" echo "Soft limit (90%): $iplimit_soft" echo "Current number of rules: $ipcurrent" # Get all currently banned IP addresses from iptables declare -a bannedips bannedips=( `/sbin/iptables --list -n | grep "DROP" | grep "all" | awk '{print$4}'` ) # Exit if no banned IP addresses are found if [ "${#bannedips[*]}" -eq "0" ] then echo "No banned IP addresses found, other types of rules have filled the rule limit." exit 0 fi echo "Removing old banned IP addresses..." # Count them and get half of them (the oldest half) bannedips_count=`expr ${#bannedips[*]} / 2` # Remove them from iptables for element in $(seq $bannedips_count $((${#bannedips[@]} - 1))) do `/sbin/iptables -D INPUT -s ${bannedips[$element]} -j DROP` done echo "Removed $bannedips_count banned IP addresses" ipcurrent=` tail /proc/user_beancounters | grep numiptent | awk '{print$2}' ` echo "New current number of rules: $ipcurrent"
STEP 2 - the cron job
Now create a cron job so that the above script runs once a day, for example, run "crontab -e" as root and add (runs once a day, at midnight):
Code:0 0 * * * /root/bin/ipbanreset


LinkBack URL
About LinkBacks



Reply With Quote


Bookmarks