summaryrefslogtreecommitdiffabout
path: root/trafsum/trafsum
Unidiff
Diffstat (limited to 'trafsum/trafsum') (more/less context) (ignore 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 @@
1#! /bin/sh
2# Copyright 2000, 2005 Sergey Poznyakoff
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17# MA 02110-1301, USA.
18
19PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
20export PATH
21
22BASEDIR=`dirname $0`
23SED=/tmp/trafsum.$$.sed
24TEMP=/tmp/trafsum.$$
25TERR=/tmp/trafsum.err.$$
26CONF=/etc/trafsum.conf
27SUMMARY=/tmp/trafsum.summary
28LOG=/var/log/ipstat/trafsum.log
29SQLLOG=/var/log/ipstat/trafsum.sql
30DATE=`date +%y%m%d%n -d yesterday`
31STDERR=/var/log/ipstat/trafsum.err
32HISTORY=/var/log/ipstat/history
33
34log() {
35 echo "$*" | tee -a $TEMPLOG >> $LOG
36}
37
38## Determine yesterday's date
39
40yesterday() {
41 datestr=`date +%Y-%m-%d -d "-1 day"`
42}
43
44lookup() {
45 NAME=`nslookup $1 2>/dev/null | sed -ne 's/Name: *\(.*\)/\1/p'`
46 if [ "$NAME" != "" ]; then
47 cp $BASEDIR/namelist $BASEDIR/namelist.b
48 if fgrep "$1:" $BASEDIR/namelist 2>/dev/null ; then
49 sed -e "s/$1:.*$/$1: $NAME/" $BASEDIR/namelist.b > $BASEDIR/namelist
50 else
51 echo "$1: $NAME" >> $BASEDIR/namelist
52 fi
53 log "Cached DNS entry $1: $NAME"
54 else
55 NAME=$1
56 fi
57}
58
59###########################################
60### Main
61#
62
63. $CONF
64
65trap 'rm -f $SED $TEMP $TEMPLOG $SUMMARY' 0 1 2 3 15
66
67if [ x"$SOURCE_FILES" = x"" ]; then
68 log "Configuration error: no source files specified"
69 exit 1
70fi
71cat /dev/null > $SUMMARY
72cat /dev/null > $STDERR
73
74if [ ! -f $LOG ] ; then
75 cat /dev/null > $LOG
76fi
77
78cat $SOURCE_FILES > $SUMMARY
79
80# Get yesterday's date into $datestr
81yesterday
82
83echo -e "\ndate: $datestr"
84
85## Process statistics data
86#
87if [ -r $BASEDIR/translate ]; then
88 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
89else
90 rm -f $SED # Make sure it does not exist
91fi
92
93MYSQL_OPTIONS="-A "
94if [ "$SERVER" ]; then
95 MYSQL_OPTIONS="$MYSQL_OPTIONS -h$SERVER"
96fi
97
98if [ "$PORT" ]; then
99 MYSQL_OPTIONS="$MYSQL_OPTIONS -P$PORT"
100fi
101
102if [ "$MYSQL_USER" ]; then
103 MYSQL_OPTIONS="$MYSQL_OPTIONS -u$MYSQL_USER"
104fi
105
106if [ "$MYSQL_PASSWORD" ]; then
107 MYSQL_OPTIONS="$MYSQL_OPTIONS -p$MYSQL_PASSWORD"
108fi
109MYSQL_OPTIONS="$MYSQL_OPTIONS $DATABASE"
110
111cat $SUMMARY |
112(
113 if [ -r $SED ]; then
114 sed -f $SED
115 else
116 cat -
117 fi | \
118 awk -f $BASEDIR/traflog.awk 2>$TERR |
119 while read IPADDR INBYTES OUTBYTES
120 do
121 NAME=`fgrep "$IPADDR:" $BASEDIR/namelist | sed -e 's/.*: *\(.*\)/\1/' 2>/dev/null`
122 [ "$NAME" = "" ] && lookup $IPADDR
123 echo -e "$IPADDR\t$NAME\t$INBYTES\t\t$OUTBYTES" >> $TEMP
124 echo "INSERT INTO $TABLE VALUES ('$IPADDR','$datestr',$INBYTES,$OUTBYTES);"
125 done
126) |
127 tee $SQLLOG |
128 mysql $MYSQL_OPTIONS
129
130[ x"$ADDRESSLIST" != x"" ] && \
131(
132 if [ -s $TERR ]; then
133 echo "WARNING: traflog.awk reported errors. They are:"
134 echo "==============================================="
135 cat $TERR
136 echo "==============================================="
137 fi
138
139 sort -t'.' -n +0 -1 +1 -2 +2 -3 +3 -4 $TEMP | \
140 awk -v DATE=$datestr 'BEGIN {
141 print "I/O traffic summary for " DATE
142 print
143 printf "%-17.17s%24.24s%15s%15s\n", "IP", "Name/address", "Incoming", "Outgoing"
144 }
145 { printf "%-17.17s%24.24s%15s%15s\n", $1, $2, $3, $4 }' \
146) | mail -s "traffic statistics" $ADDRESSLIST
147
148cat $SUMMARY | gzip - > $HISTORY/${DATE}.gz
149
150if [ -x $BASEDIR/trafsum.local ]; then
151 sh $BASEDIR/trafsum.local $SUMMARY
152fi
153
154rm -f $SED $TEMP $TEMPLOG $SUMMARY $TERR
155for i in $SOURCE_FILES
156do
157 cat /dev/null > $i
158done
159
160

Return to:

Send suggestions and report system problems to the System administrator.