1 #!/bin/sh 2 3 TOTAL_IN=0 4 TOTAL_OUT=0 5 EOL=' 6 ' 7 8 show_human_size() 9 { 10 if [ $1 -lt 10000 ]; then 11 echo "$1 Bytes" 12 return 0 13 fi 14 15 if [ $1 -lt 10000000 ]; then 16 echo "$(($1 / 1024)) KiB" 17 return 0 18 fi 19 20 if [ $1 -lt 10000000000 ]; then 21 echo "$(($1 / (1024*1024))) MiB" 22 return 0 23 fi 24 25 echo "$(($1 / (1024*1024*1024))) GiB" 26 return 0 27 } 28 29 # Program entry point 30 31 if [ -n "$1" ]; then 32 LOG_FILE="$1" 33 else 34 LOG_FILE="/var/log/traffic-accounting.log" 35 fi 36 37 echo "Bytes input:" 38 echo "-------------" 39 IFS=$EOL 40 for LINE in `cat "$LOG_FILE" |sort -n --key=3 --reverse`; do 41 hostname="$(echo "$LINE" |cut -s -d' ' -f1)" 42 ip="$(echo "$LINE" |cut -s -d' ' -f2)" 43 size="$(echo "$LINE" |cut -s -d' ' -f3)" 44 45 if [ "$hostname" = "0/0" ]; then 46 hostname="Other traffic" 47 elif [ "$hostname" = "0.0.0.0/0" ]; then 48 hostname="Other IPv4 traffic" 49 elif [ "$hostname" = "::/0" ]; then 50 hostname="Other IPv6 traffic" 51 fi 52 53 echo "$hostname ($ip): $(show_human_size $size)" 54 55 TOTAL_IN=$(($TOTAL_IN + $size)) 56 done 57 58 echo "" 59 echo "Total input traffic: $(show_human_size $TOTAL_IN)" 60 61 echo "" 62 echo "Bytes output:" 63 echo "-------------" 64 IFS=$EOL 65 for LINE in `cat "$LOG_FILE" |sort -n --key=4 --reverse`; do 66 hostname="$(echo "$LINE" |cut -s -d' ' -f1)" 67 ip="$(echo "$LINE" |cut -s -d' ' -f2)" 68 size="$(echo "$LINE" |cut -s -d' ' -f4)" 69 70 if [ "$hostname" = "0/0" ]; then 71 hostname="Other traffic" 72 elif [ "$hostname" = "0.0.0.0/0" ]; then 73 hostname="Other IPv4 traffic" 74 elif [ "$hostname" = "::/0" ]; then 75 hostname="Other IPv6 traffic" 76 fi 77 78 echo "$hostname ($ip): $(show_human_size $size)" 79 80 TOTAL_OUT=$(($TOTAL_OUT + $size)) 81 done 82 83 echo "" 84 echo "Total output traffic: $(show_human_size $TOTAL_OUT)" 85 86