summaryrefslogtreecommitdiffabout
path: root/trafsum/trafsum
blob: 10437bde74dc768d5f66b994ed8a9cb9fdb35802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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.