My iptabbles rules:
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto bittorrent -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto bittorrent -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto ftp -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto ftp -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto ssh -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto ssh -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto pop3 -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto pop3 -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto smtp -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto smtp -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto imap -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto imap -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto directconnect -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto directconnect -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto http -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto http -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto rdp -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto rdp -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto smb -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto smb -j MARK --set-mark 3
iptables -t mangle -A POSTROUTING -o vlan1 -m layer7 --l7proto ventrilo -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -i vlan1 -m layer7 --l7proto ventrilo -j MARK --set-mark 3
#All unmarked
iptables -t mangle -A POSTROUTING -o $WAN -m mark --mark 0 -j MARK --set-mark 100
iptables -t mangle -A PREROUTING -i $WAN -m mark --mark 0 -j MARK --set-mark 101
The sscripts on the webserver:
#!/bin/sh
iptables -L -v -t mangle --exact
Create the rrd database (really ugly code, shame on me):
#!/usr/bin/perl -w
#
use RRDs;
# define location of rrdtool databases
my $rrd = '/var/lib/rrd';
system("rm siptables.sh");
system("rm snet_in.sh");
system("rm snet_out.sh");
system("wget -q http://192.168.1.1:3000/cgi-bin/iptables.sh -O siptables.sh");
system("wget -q http://192.168.1.1:3000/cgi-bin/net_in.sh -O snet_in.sh");
system("wget -q http://192.168.1.1:3000/cgi-bin/net_out.sh -O snet_out.sh");
if (! -e "$rrd/service_graphs.rrd")
{
print "creating rrd database for network...\n";
RRDs::create "$rrd/service_graphs.rrd",
"-s 300",
"DS:in_total:COUNTER:600:0:12500000",
"DS:out_total:COUNTER:600:0:12500000",
"DS:ftp_in:COUNTER:600:0:12500000",
"DS:ftp_out:COUNTER:600:0:12500000",
"DS:ssh_in:COUNTER:600:0:12500000",
"DS:ssh_out:COUNTER:600:0:12500000",
"DS:http_in:COUNTER:600:0:12500000",
"DS:http_out:COUNTER:600:0:12500000",
"DS:mail_in:COUNTER:600:0:12500000",
"DS:mail_out:COUNTER:600:0:12500000",
"DS:ventrilo_in:COUNTER:600:0:12500000",
"DS:ventrilo_out:COUNTER:600:0:12500000",
#"DS:hlstats_in:COUNTER:600:0:12500000",
#"DS:hlstats_out:COUNTER:600:0:12500000",
"DS:warez_in:COUNTER:600:0:12500000",
"DS:warez_out:COUNTER:600:0:12500000",
"DS:games_in:COUNTER:600:0:12500000",
"DS:games_out:COUNTER:600:0:12500000",
"DS:other_in:COUNTER:600:0:12500000",
"DS:other_out:COUNTER:600:0:12500000",
"RRA:AVERAGE:0.5:1:576",
"RRA:AVERAGE:0.5:6:672",
"RRA:AVERAGE:0.5:24:732",
"RRA:AVERAGE:0.5:144:1460";
if ($ERROR = RRDs::error) { print "unable to generate database: $ERROR\n"; }
}
sleep 2;
my $in_total = `cat snet_in.sh` *1;
my $out_total = `cat snet_out.sh` *1;
#my $ftp_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'spts:20:21'|gawk '{print \$2}'` * 1;
#my $ftp_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpts:20:21'|gawk '{print \$2}'` * 1;
#my $ftp_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'spts:20:21'|gawk '{print \$2}'` * 1;
#my $ftp_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpts:20:21'|gawk '{print \$2}'` * 1;
#my $ftp_in = $ftp_in1 + $ftp_in2;
#my $ftp_out = $ftp_out1 + $ftp_out2;
my $ftp_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto ftp'|gawk '{print \$2}'` * 1;
my $ftp_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto ftp'|gawk '{print \$2}'` * 1;
#my $ssh_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpt:22 M'|gawk '{print \$2}'` * 1;
#my $ssh_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spt:22 M'|gawk '{print \$2}'` * 1;
#my $ssh_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpt:22 M'|gawk '{print \$2}'` * 1;
#my $ssh_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spt:22 M'|gawk '{print \$2}'` * 1;
#my $ssh_in = $ssh_in1 + $ssh_in2;
#my $ssh_out = $ssh_out1 + $ssh_out2;
my $ssh_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto ssh'|gawk '{print \$2}'` * 1;
my $ssh_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto ssh'|gawk '{print \$2}'` * 1;
#my $http_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpts:80:8080'|gawk '{print \$2}'` * 1;
#my $http_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spts:80:8080'|gawk '{print \$2}'` * 1;
#my $http_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpts:80:8080'|gawk '{print \$2}'` * 1;
#my $http_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spts:80:8080'|gawk '{print \$2}'` * 1;
#my $https_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpt:443'|gawk '{print \$2}'` * 1;
#my $https_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spt:443'|gawk '{print \$2}'` * 1;
#my $https_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpt:443'|gawk '{print \$2}'` * 1;
#my $https_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spt:443'|gawk '{print \$2}'` * 1;
#my $http_in = $http_in1 + $http_in2 + $https_in1 + $https_in2;
#my $http_out = $http_out1 + $http_out2 + $https_out1 + $https_out2;
my $http_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto http'|gawk '{print \$2}'` * 1;
my $http_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto http'|gawk '{print \$2}'` * 1;
#my $smtp_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpts:25:465'|gawk '{print \$2}'` * 1;
#my $smtp_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spts:25:465'|gawk '{print \$2}'` * 1;
#my $smtp_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpts:25:465'|gawk '{print \$2}'` * 1;
#my $smtp_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spts:25:465'|gawk '{print \$2}'` * 1;
#my $pop3_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpts:110:995'|gawk '{print \$2}'` * 1;
#my $pop3_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spts:110:995'|gawk '{print \$2}'` * 1;
#my $pop3_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpts:110:995'|gawk '{print \$2}'` * 1;
#my $pop3_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spts:110:995'|gawk '{print \$2}'` * 1;
#my $imap_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpts:143:993'|gawk '{print \$2}'` * 1;
#my $imap_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spts:143:993'|gawk '{print \$2}'` * 1;
#my $imap_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpts:143:993'|gawk '{print \$2}'` * 1;
#my $imap_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spts:143:993'|gawk '{print \$2}'` * 1;
#my $mail_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpts:389:689'|gawk '{print \$2}'` * 1;
#my $mail_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spts:389:689'|gawk '{print \$2}'` * 1;
#my $mail_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpts:389:689'|gawk '{print \$2}'` * 1;
#my $mail_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spts:389:689'|gawk '{print \$2}'` * 1;
#my $mail_in = $smtp_in1 + $smtp_in1 + $pop3_in1 + $pop3_in2 + $imap_in1 + $imap_in2 + $mail_in1 + $mail_in2;
#my $mail_out = $smtp_out1 + $smtp_out2 + $pop3_out1 + $pop3_out2 + $imap_out1 + $imap_out2 + $mail_out1 + $mail_out2;
my $smtp_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto smtp'|gawk '{print \$2}'` * 1;
my $smtp_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto smtp'|gawk '{print \$2}'` * 1;
my $pop3_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto pop3'|gawk '{print \$2}'` * 1;
my $pop3_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto pop3'|gawk '{print \$2}'` * 1;
my $imap_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto imap'|gawk '{print \$2}'` * 1;
my $imap_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto imap'|gawk '{print \$2}'` * 1;
my $mail_in = $smtp_in + $pop3_in + $imap_in;
my $mail_out = $smtp_out + $pop3_out + $imap_out;
#my $ventrilo_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpt:3784'|gawk '{print \$2}'` * 1;
#my $ventrilo_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spt:3784'|gawk '{print \$2}'` * 1;
#my $ventrilo_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpt:3784'|gawk '{print \$2}'` * 1;
#my $ventrilo_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spt:3784'|gawk '{print \$2}'` * 1;
#my $ventrilo_in = $ventrilo_in1 + $ventrilo_in2;
#my $ventrilo_out = $ventrilo_out1 + $ventrilo_out2;
my $ventrilo_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto ventrilo'|gawk '{print \$2}'` * 1;
my $ventrilo_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto ventrilo'|gawk '{print \$2}'` * 1;
#my $hlstats_in1 = `cat siptables.sh|grep 'vlan1 any'|grep 'dpt:27500'|gawk '{print \$2}'` * 1;
#my $hlstats_in2 = `cat siptables.sh|grep 'vlan1 any'|grep 'spt:27500'|gawk '{print \$2}'` * 1;
#my $hlstats_out1 = `cat siptables.sh|grep 'ny vlan1'|grep 'dpt:27500'|gawk '{print \$2}'` * 1;
#my $hlstats_out2 = `cat siptables.sh|grep 'ny vlan1'|grep 'spt:27500'|gawk '{print \$2}'` * 1;
#my $hlstats_in = $hlstats_in1 + $hlstats_in2;
#my $hlstats_out = $hlstats_out1 + $hlstats_out2;
my $directconnect_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto directconnect'|gawk '{print \$2}'` * 1;
my $directconnect_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto directconnect'|gawk '{print \$2}'` * 1;
my $bittorrent_in = `cat siptables.sh|grep 'vlan1 any'|grep 'l7proto bittorrent'|gawk '{print \$2}'` * 1;
my $bittorrent_out = `cat siptables.sh|grep 'ny vlan1'|grep 'l7proto bittorrent'|gawk '{print \$2}'` * 1;
my $warez_in = $directconnect_in + $bittorrent_in;
my $warez_out = $directconnect_out + $bittorrent_out;
my $games_in = 0 * 1;
my $games_out = 0 * 1;
my $other_in = `cat siptables.sh|grep 'MARK match 0x0 MARK set 0x65'|gawk '{print \$2}'` * 1;
my $other_out = `cat siptables.sh|grep 'MARK match 0x0 MARK set 0x64'|gawk '{print \$2}'` * 1;
my $uptime = `cut -d" " -f1 /proc/uptime` *1;
if ($uptime > 600) {
# insert values into rrd
RRDs::update "$rrd/service_graphs.rrd",
"-t",
"in_total:out_total:ftp_in:ftp_out:http_in:http_out:mail_in:mail_out:ventrilo_in:ventrilo_out:warez_in:warez_out:games_in:games_out:other_in:other_out",
"N:$in_total:$out_total:$ftp_in:$ftp_out:$http_in:$http_out:$mail_in:$mail_out:$ventrilo_in:$ventrilo_out:$warez_in:$warez_out:$games_in:$games_out:$other_in:$other_out";
if ($ERROR = RRDs::error) { print "unable to update: $ERROR\n"; }
}
Generate graphs:
#!/bin/bash
# Full path to your rrdtool binary
RRDTOOL=/usr/bin/rrdtool
# Full path to your rrd datase
RRD=/var/lib/rrd/service_graphs.rrd
# The title of your graph
TITLE='traffic on vlan0 :: Mokey (Internet Gateway) Port Specific'
# Output path where your graphs will end up
OPATH=/home/magnus/www/graphs/
# Date command, add date formating if you would like so (man date)
DATECMD='/bin/date'
#
# End of configuration
#
for i in 1 2 3 4 5;
do
if [ "$i" == "1" ]; then
START="12h"
FILENAME="network-12h.png"
fi
if [ "$i" == "2" ]; then
START="24h"
FILENAME="network-day.png"
fi
if [ "$i" == "3" ]; then
START=604800
FILENAME="network-week.png"
fi
if [ "$i" == "4" ]; then
START=2419200
FILENAME="network-month.png"
fi
if [ "$i" == "5" ]; then
START=31536000
FILENAME="network-year.png"
fi
DATE=`$DATECMD`
$RRDTOOL graph $OPATH/$FILENAME \
--start -$START \
--title "$TITLE" \
--vertical-label 'Bytes per second' \
-w 800 \
-h 200 \
-l 0 \
-Y \
-M \
-E \
-i \
DEF:in_total=$RRD:in_total:AVERAGE \
DEF:out_total=$RRD:out_total:AVERAGE \
DEF:ftp_in=$RRD:ftp_in:AVERAGE \
DEF:ftp_out=$RRD:ftp_out:AVERAGE \
DEF:http_in=$RRD:http_in:AVERAGE \
DEF:http_out=$RRD:http_out:AVERAGE \
DEF:ssh_in=$RRD:ssh_in:AVERAGE \
DEF:ssh_out=$RRD:ssh_out:AVERAGE \
DEF:mail_in=$RRD:mail_in:AVERAGE \
DEF:mail_out=$RRD:mail_out:AVERAGE \
DEF:ventrilo_in=$RRD:ventrilo_in:AVERAGE \
DEF:ventrilo_out=$RRD:ventrilo_out:AVERAGE \
DEF:warez_in=$RRD:warez_in:AVERAGE \
DEF:warez_out=$RRD:warez_out:AVERAGE \
DEF:games_in=$RRD:games_in:AVERAGE \
DEF:games_out=$RRD:games_out:AVERAGE \
DEF:other_in=$RRD:other_in:AVERAGE \
DEF:other_out=$RRD:other_out:AVERAGE \
"CDEF:neg_out_total=out_total,-1,*" \
"CDEF:neg_ftp_out=ftp_out,-1,*" \
"CDEF:neg_ssh_out=ssh_out,-1,*" \
"CDEF:neg_http_out=http_out,-1,*" \
"CDEF:neg_mail_out=mail_out,-1,*" \
"CDEF:neg_ventrilo_out=ventrilo_out,-1,*" \
"CDEF:neg_warez_out=warez_out,-1,*" \
"CDEF:neg_games_out=games_out,-1,*" \
"CDEF:neg_other_out=other_out,-1,*" \
"COMMENT: IN OUT\N" \
"COMMENT: MAX AVERAGE CURRENT MAX AVERAGE CURRENT\n" \
LINE1:in_total#FF0000:'TOTAL ' \
"GPRINT:in_total:MAX:%5.1lf %sB/s", \
"GPRINT:in_total:AVERAGE:%5.1lf %sB/s", \
"GPRINT:in_total:LAST:%5.1lf %sB/s " \
"GPRINT:out_total:MAX:%5.1lf %sB/s", \
"GPRINT:out_total:AVERAGE:%5.1lf %sB/s", \
"GPRINT:out_total:LAST:%5.1lf %sB/s\n" \
LINE1:ftp_in#ff00ea:'FTP ' \
"GPRINT:ftp_in:MAX:%5.1lf %sB/s", \
"GPRINT:ftp_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:ftp_in:LAST:%5.1lf %sB/s " \
"GPRINT:ftp_out:MAX:%5.1lf %sB/s", \
"GPRINT:ftp_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:ftp_out:LAST:%5.1lf %sB/s\n" \
LINE1:ssh_in#ff00ea:'SSH ' \
"GPRINT:ssh_in:MAX:%5.1lf %sB/s", \
"GPRINT:ssh_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:ssh_in:LAST:%5.1lf %sB/s " \
"GPRINT:ssh_out:MAX:%5.1lf %sB/s", \
"GPRINT:ssh_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:ssh_out:LAST:%5.1lf %sB/s\n" \
LINE1:http_in#a200ff:'HTTP ' \
"GPRINT:http_in:MAX:%5.1lf %sB/s", \
"GPRINT:http_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:http_in:LAST:%5.1lf %sB/s " \
"GPRINT:http_out:MAX:%5.1lf %sB/s", \
"GPRINT:http_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:http_out:LAST:%5.1lf %sB/s\n" \
LINE1:mail_in#3c00ff:'MAIL ' \
"GPRINT:mail_in:MAX:%5.1lf %sB/s", \
"GPRINT:mail_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:mail_in:LAST:%5.1lf %sB/s " \
"GPRINT:mail_out:MAX:%5.1lf %sB/s", \
"GPRINT:mail_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:mail_out:LAST:%5.1lf %sB/s\n" \
LINE1:ventrilo_in#0072ff:'VENTRILO' \
"GPRINT:ventrilo_in:MAX:%5.1lf %sB/s", \
"GPRINT:ventrilo_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:ventrilo_in:LAST:%5.1lf %sB/s " \
"GPRINT:ventrilo_out:MAX:%5.1lf %sB/s", \
"GPRINT:ventrilo_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:ventrilo_out:LAST:%5.1lf %sB/s\n" \
LINE1:warez_in#00ff84:'WAREZ ' \
"GPRINT:warez_in:MAX:%5.1lf %sB/s", \
"GPRINT:warez_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:warez_in:LAST:%5.1lf %sB/s " \
"GPRINT:warez_out:MAX:%5.1lf %sB/s", \
"GPRINT:warez_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:warez_out:LAST:%5.1lf %sB/s\n" \
LINE1:games_in#00ff18:'GAMES ' \
"GPRINT:games_in:MAX:%5.1lf %sB/s", \
"GPRINT:games_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:games_in:LAST:%5.1lf %sB/s " \
"GPRINT:games_out:MAX:%5.1lf %sB/s", \
"GPRINT:games_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:games_out:LAST:%5.1lf %sB/s\n" \
LINE1:other_in#a8ff00:'OTHER ' \
"GPRINT:other_in:MAX:%5.1lf %sB/s", \
"GPRINT:other_in:AVERAGE:%5.1lf %sB/s", \
"GPRINT:other_in:LAST:%5.1lf %sB/s " \
"GPRINT:other_out:MAX:%5.1lf %sB/s", \
"GPRINT:other_out:AVERAGE:%5.1lf %sB/s", \
"GPRINT:other_out:LAST:%5.1lf %sB/s\n" \
LINE1:neg_ftp_out#ff00ea \
LINE1:neg_ssh_out#a200ff \
LINE1:neg_http_out#3c00ff \
LINE1:neg_mail_out#0072ff \
LINE1:neg_ventrilo_out#00fffc \
LINE1:neg_warez_out#00ff18 \
LINE1:neg_games_out#a8ff00 \
LINE1:neg_other_out#fcff00 \
COMMENT:"!\r" #date
done
And there it is....