Created
March 12, 2012 17:40
-
-
Save donjohnson/2023585 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#TODO: actual callouts | |
### BEGIN LOCKING CODE | |
### The following non-reinvented wheel is from http://wiki.bash-hackers.org/howto/mutex. | |
# lock dirs/files | |
LOCKDIR="/tmp/statsgen-lock" | |
PIDFILE="${LOCKDIR}/PID" | |
# exit codes and text for them - additional features nobody needs :-) | |
ENO_SUCCESS=0; ETXT[0]="ENO_SUCCESS" | |
ENO_GENERAL=1; ETXT[1]="ENO_GENERAL" | |
ENO_LOCKFAIL=2; ETXT[2]="ENO_LOCKFAIL" | |
ENO_RECVSIG=3; ETXT[3]="ENO_RECVSIG" | |
### | |
### start locking attempt | |
### | |
trap 'ECODE=$?; echo "[statsgen] Exit: ${ETXT[ECODE]}($ECODE)" >&2' 0 | |
echo -n "[statsgen] Locking: " >&2 | |
if mkdir "${LOCKDIR}" &>/dev/null; then | |
# lock succeeded, install signal handlers before storing the PID just in case | |
# storing the PID fails | |
trap 'ECODE=$?; | |
echo "[statsgen] Removing lock. Exit: ${ETXT[ECODE]}($ECODE)" >&2 | |
rm -rf "${LOCKDIR}"' 0 | |
echo "$$" >"${PIDFILE}" | |
# the following handler will exit the script on receiving these signals | |
# the trap on "0" (EXIT) from above will be triggered by this trap's "exit" command! | |
trap 'echo "[statsgen] Killed by a signal." >&2 | |
exit ${ENO_RECVSIG}' 1 2 3 15 | |
echo "success, installed signal handlers" | |
else | |
# lock failed, now check if the other PID is alive | |
OTHERPID="$(cat "${PIDFILE}")" | |
# if cat wasn't able to read the file anymore, another instance probably is | |
# about to remove the lock -- exit, we're *still* locked | |
# Thanks to Grzegorz Wierzowiecki for pointing this race condition out on | |
# http://wiki.grzegorz.wierzowiecki.pl/code:mutex-in-bash | |
if [ $? != 0 ]; then | |
echo "lock failed, PID ${OTHERPID} is active" >&2 | |
exit ${ENO_LOCKFAIL} | |
fi | |
if ! kill -0 $OTHERPID &>/dev/null; then | |
# lock is stale, remove it and restart | |
echo "removing stale lock of nonexistant PID ${OTHERPID}" >&2 | |
rm -rf "${LOCKDIR}" | |
echo "[statsgen] restarting myself" >&2 | |
exec "$0" "$@" | |
else | |
# lock is valid and OTHERPID is active - exit, we're locked! | |
echo "lock failed, PID ${OTHERPID} is active" >&2 | |
exit ${ENO_LOCKFAIL} | |
fi | |
fi | |
### END LOCKING CODE | |
function spamhaus { | |
VAR1=`echo $1|sed -e "s/.*\[//" -e "s/\].*//" -e "s/\./ /g"|awk '{print $4"."$3"."$2"."$1".sbl.spamhaus.org"}'` | |
RETVAL=`nslookup $VAR1|grep -c "127.0.0."` | |
echo $RETVAL | |
} | |
function spamcop { | |
VAR1=`echo $1|sed -e "s/.*\[//" -e "s/\].*//" -e "s/\./ /g"|awk '{print $4"."$3"."$2"."$1".bl.spamcop.net"}'` | |
RETVAL=`nslookup $VAR1|grep -v NXDOMAIN|grep -c Name` | |
echo $RETVAL | |
} | |
UNIQUE=`date +\%s`.$$ | |
SRCFILE=log.$UNIQUE.tmp | |
wget http://entropymonkey.org/test/log.txt -O $SRCFILE | |
COUNTLOGENTRIES=`grep -vc '$^\|$#' $SRCFILE` | |
FILE1=$SRCFILE.stage1 | |
cat $SRCFILE |sed -e "s/.*\[//g" -e "s/\].*//g"|grep -v "$^\|$#"|sort|uniq -c|sort -n|awk '{print $2","$1}' > $FILE1 | |
COUNTUNIQUEIPS=`grep -c . $FILE1` | |
SC_COUNT=0 | |
SH_COUNT=0 | |
LOOPER=0 | |
for i in `tac $FILE1` | |
do | |
IP=`/bin/echo "$i"|/bin/cut -f 1 -d","` | |
CNT=`/bin/echo "$i"|/bin/cut -f 2 -d","` | |
if [ $(spamhaus $IP) == "1" ]; then | |
SH_COUNT=`expr $SH_COUNT + $CNT` | |
fi | |
if [ $(spamcop $IP) == "1" ]; then | |
SC_COUNT=`expr $SC_COUNT + $CNT` | |
fi | |
LOOPER=`expr $LOOPER + 1` | |
if [ $(($LOOPER % 50)) == 0 ]; then | |
echo "$LOOPER" | |
fi | |
done | |
echo | |
echo "1. Number of Unique IPs: $COUNTUNIQUEIPS" | |
echo "2a. Number of Hits on SpamHaus RBL: $SH_COUNT" | |
echo "2b. Number of Hits on Spamcop RBL: $SC_COUNT" | |
echo -n "3a. Percentage of Log Entries to hit SpamHaus SBL: " | |
echo "scale=2; $SH_COUNT/$COUNTLOGENTRIES" | bc -l|sed -e "s/^\.//" -e "s/$/\%/" | |
echo -n "3b. Percentage of Log Entries to hit Spamcop RBL: " | |
echo "scale=2; $SC_COUNT/$COUNTLOGENTRIES" | bc -l|sed -e "s/^\.//" -e "s/$/\%/" | |
echo | |
#cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment