summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2005-08-04 07:15:38 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2005-08-04 07:15:38 (GMT)
commit07bcede1f741ed4fac48427890f7fac912e6960b (patch) (unidiff)
treedabc11e89640e84ee3742c4c5fd75019184c9fc7
parent557cc6a018232026c40e92cf1920431deb738b27 (diff)
downloadipacct-07bcede1f741ed4fac48427890f7fac912e6960b.tar.gz
ipacct-07bcede1f741ed4fac48427890f7fac912e6960b.tar.bz2
The stuff was initially written somewhere around April-May 1999. The
subsequent modifications weren't considerable, and they were not documented anyway. Somewhere I still keep the old RCS repository of the project but it does not seem to be of any interest.
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--trafsum/README20
-rw-r--r--trafsum/traflog.awk45
-rwxr-xr-xtrafsum/trafsum160
-rw-r--r--trafsum/trafsum.conf11
-rw-r--r--trafsum/trafsum.local50
-rw-r--r--trafsum/translate7
6 files changed, 293 insertions, 0 deletions
diff --git a/trafsum/README b/trafsum/README
new file mode 100644
index 0000000..cf7f56a
--- a/dev/null
+++ b/trafsum/README
@@ -0,0 +1,20 @@
1This is trafsum: a traffic data summator. It processes the information
2from one or several log files and stores the data in MySQL database. The
3format of log files is as follows:
4
5# <---- Slice time ----> IP-Address Inbytes Outbytes
6Mon Jun 4 03:30:02 2001 213.130.7.7 4012128 7627102
7
8To install:
9
10 1. Copy the entire directory somewhere where you'd like it to be installed;
11 2. Copy the file trafsum.conf to /etc and edit it to your liking. It
12 should have access permissions of 600.
13 3. If necessary, you may wish to edit <instdir>/translate. The necessary
14 instructons are found in the comment at the start of the file;
15 4. Add the script to your crontab:
16
17 20 0 * * * /usr/local/trafsum/trafsum >/var/log/trafsum.err 2>&1
18
19 5. Take cover and wait for the script to work.
20
diff --git a/trafsum/traflog.awk b/trafsum/traflog.awk
new file mode 100644
index 0000000..7792fb9
--- a/dev/null
+++ b/trafsum/traflog.awk
@@ -0,0 +1,45 @@
1# $Id: traflog.awk,v 1.1 2005/08/04 07:15:38 gray Exp $
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
19#Field assignment
20# 1 2 3 4 5 6 7 8
21 #Sun Jun 20 00:04:06 1999 208.244.48.32 301736
22
23BEGIN {
24 divisor = 1048576
25 maxtraf = 4294960000
26}
27
28NF==8 {
29 # Sanity check
30 if (0+$7 < 0 || 0+$8 < 0 || 0+$7 > maxtraf || 0+$8 > maxtraf) {
31 print NR ": Skipped (" $7 " " $8 ")" >"/dev/stderr"
32 next
33 } else {
34 inbytes[$6] += 0+$7
35 outbytes[$6] += 0+$8
36 }
37}
38
39END {
40 for (ip in inbytes) {
41 print ip " " inbytes[ip] / divisor " " outbytes[ip] / divisor
42 }
43}
44
45
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
diff --git a/trafsum/trafsum.conf b/trafsum/trafsum.conf
new file mode 100644
index 0000000..dd1df10
--- a/dev/null
+++ b/trafsum/trafsum.conf
@@ -0,0 +1,11 @@
1# Config file for traffic accounter
2
3SERVER=#<MYSQL server name>
4PORT=#<MYSQL port>
5DATABASE=#<Database name>
6TABLE=#<Table name>
7MYSQL_USER=
8MYSQL_PASSWORD=
9SOURCE_FILES="" # Ws-delimited list of files to be analyzed
10ADDRESSLIST="" # Ws-delimited list of email addresses the report should be sent
11 # to
diff --git a/trafsum/trafsum.local b/trafsum/trafsum.local
new file mode 100644
index 0000000..d88f979
--- a/dev/null
+++ b/trafsum/trafsum.local
@@ -0,0 +1,50 @@
1#! /bin/sh
2# This is an example of trafsum.local script.
3# Copyright 2000 Sergey Poznyakoff
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2, or (at your option)
8# any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18# MA 02110-1301, USA.
19
20FILE=$1
21DATE=`date --date '1 day ago'`
22(
23 cat - <<EOF
24From: Trafsum <noreturn@some.dom.ain>
25Subject: Your statistics for $DATE
26
27*********************************************************************
28* THE RETURN ADDRESSES ON THIS LETTER HAVE BEEN SET TO PREVENT MAIL *
29* LOOPS IN THE EVENT YOU ARE RUNNING SOFTWARE WHICH AUTO-REPLIES TO *
30* INBOUND MAIL. WE WILL NOT SEE ANY REPLY SENT TO THIS LETTER *
31*********************************************************************
32
33Dear customer,
34
35Please find below the statistics of your traffic consumption for $DATE
36
37---------------------------------------------------------------------
38EOF
39
40 grep 213.130.3.56 $FILE
41
42 cat - <<EOF
43---------------------------------------------------------------------
44
45Kind regards,
46Trafsum,
47Statistics robot
48EOF
49 ) | sendmail some.addr@some.domain
50
diff --git a/trafsum/translate b/trafsum/translate
new file mode 100644
index 0000000..a163d1d
--- a/dev/null
+++ b/trafsum/translate
@@ -0,0 +1,7 @@
1# This file is used to translate IP addresses for those customers who
2# have more than one IP assigned
3# Syntax is:
4# <from-ip-addr>:<to-ip-addr>
5# In the example below all traffic for 213.130.4.43 will be summed up
6# with that for 213.130.4.40:
7 #213.130.4.43:213.130.4.40Comment

Return to:

Send suggestions and report system problems to the System administrator.