summaryrefslogtreecommitdiffabout
path: root/trafsum/trafsum
Side-by-side diff
Diffstat (limited to 'trafsum/trafsum') (more/less context) (show whitespace changes)
-rwxr-xr-xtrafsum/trafsum160
1 files changed, 160 insertions, 0 deletions
diff --git a/trafsum/trafsum b/trafsum/trafsum
new file mode 100755
index 0000000..10437bd
--- a/dev/null
+++ b/trafsum/trafsum
@@ -0,0 +1,160 @@
+#! /bin/sh
+# Copyright 2000, 2005 Sergey Poznyakoff
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
+export PATH
+
+BASEDIR=`dirname $0`
+SED=/tmp/trafsum.$$.sed
+TEMP=/tmp/trafsum.$$
+TERR=/tmp/trafsum.err.$$
+CONF=/etc/trafsum.conf
+SUMMARY=/tmp/trafsum.summary
+LOG=/var/log/ipstat/trafsum.log
+SQLLOG=/var/log/ipstat/trafsum.sql
+DATE=`date +%y%m%d%n -d yesterday`
+STDERR=/var/log/ipstat/trafsum.err
+HISTORY=/var/log/ipstat/history
+
+log() {
+ echo "$*" | tee -a $TEMPLOG >> $LOG
+}
+
+## Determine yesterday's date
+
+yesterday() {
+ datestr=`date +%Y-%m-%d -d "-1 day"`
+}
+
+lookup() {
+ NAME=`nslookup $1 2>/dev/null | sed -ne 's/Name: *\(.*\)/\1/p'`
+ if [ "$NAME" != "" ]; then
+ cp $BASEDIR/namelist $BASEDIR/namelist.b
+ if fgrep "$1:" $BASEDIR/namelist 2>/dev/null ; then
+ sed -e "s/$1:.*$/$1: $NAME/" $BASEDIR/namelist.b > $BASEDIR/namelist
+ else
+ echo "$1: $NAME" >> $BASEDIR/namelist
+ fi
+ log "Cached DNS entry $1: $NAME"
+ else
+ NAME=$1
+ fi
+}
+
+###########################################
+### Main
+#
+
+. $CONF
+
+trap 'rm -f $SED $TEMP $TEMPLOG $SUMMARY' 0 1 2 3 15
+
+if [ x"$SOURCE_FILES" = x"" ]; then
+ log "Configuration error: no source files specified"
+ exit 1
+fi
+cat /dev/null > $SUMMARY
+cat /dev/null > $STDERR
+
+if [ ! -f $LOG ] ; then
+ cat /dev/null > $LOG
+fi
+
+cat $SOURCE_FILES > $SUMMARY
+
+# Get yesterday's date into $datestr
+yesterday
+
+echo -e "\ndate: $datestr"
+
+## Process statistics data
+#
+if [ -r $BASEDIR/translate ]; then
+ sed -ne 's%\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\):[^0-9]*\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\).*%s/\1\\.\2\\.\3\\.\4/\5\\.\6\\.\7\\.\8/%p' $BASEDIR/translate > $SED
+else
+ rm -f $SED # Make sure it does not exist
+fi
+
+MYSQL_OPTIONS="-A "
+if [ "$SERVER" ]; then
+ MYSQL_OPTIONS="$MYSQL_OPTIONS -h$SERVER"
+fi
+
+if [ "$PORT" ]; then
+ MYSQL_OPTIONS="$MYSQL_OPTIONS -P$PORT"
+fi
+
+if [ "$MYSQL_USER" ]; then
+ MYSQL_OPTIONS="$MYSQL_OPTIONS -u$MYSQL_USER"
+fi
+
+if [ "$MYSQL_PASSWORD" ]; then
+ MYSQL_OPTIONS="$MYSQL_OPTIONS -p$MYSQL_PASSWORD"
+fi
+MYSQL_OPTIONS="$MYSQL_OPTIONS $DATABASE"
+
+cat $SUMMARY |
+(
+ if [ -r $SED ]; then
+ sed -f $SED
+ else
+ cat -
+ fi | \
+ awk -f $BASEDIR/traflog.awk 2>$TERR |
+ while read IPADDR INBYTES OUTBYTES
+ do
+ NAME=`fgrep "$IPADDR:" $BASEDIR/namelist | sed -e 's/.*: *\(.*\)/\1/' 2>/dev/null`
+ [ "$NAME" = "" ] && lookup $IPADDR
+ echo -e "$IPADDR\t$NAME\t$INBYTES\t\t$OUTBYTES" >> $TEMP
+ echo "INSERT INTO $TABLE VALUES ('$IPADDR','$datestr',$INBYTES,$OUTBYTES);"
+ done
+) |
+ tee $SQLLOG |
+ mysql $MYSQL_OPTIONS
+
+[ x"$ADDRESSLIST" != x"" ] && \
+(
+ if [ -s $TERR ]; then
+ echo "WARNING: traflog.awk reported errors. They are:"
+ echo "==============================================="
+ cat $TERR
+ echo "==============================================="
+ fi
+
+ sort -t'.' -n +0 -1 +1 -2 +2 -3 +3 -4 $TEMP | \
+ awk -v DATE=$datestr 'BEGIN {
+ print "I/O traffic summary for " DATE
+ print
+ printf "%-17.17s%24.24s%15s%15s\n", "IP", "Name/address", "Incoming", "Outgoing"
+ }
+ { printf "%-17.17s%24.24s%15s%15s\n", $1, $2, $3, $4 }' \
+) | mail -s "traffic statistics" $ADDRESSLIST
+
+cat $SUMMARY | gzip - > $HISTORY/${DATE}.gz
+
+if [ -x $BASEDIR/trafsum.local ]; then
+ sh $BASEDIR/trafsum.local $SUMMARY
+fi
+
+rm -f $SED $TEMP $TEMPLOG $SUMMARY $TERR
+for i in $SOURCE_FILES
+do
+ cat /dev/null > $i
+done
+
+

Return to:

Send suggestions and report system problems to the System administrator.