Skip to content

Instantly share code, notes, and snippets.

@blog2i2j
Forked from andy-g/usage_json.sh
Created January 15, 2016 04:22
Show Gist options
  • Save blog2i2j/fe7c86847eca32e683b6 to your computer and use it in GitHub Desktop.
Save blog2i2j/fe7c86847eca32e683b6 to your computer and use it in GitHub Desktop.
Per-user traffic monitoring script for OpenWRT
#!/bin/sh
echo 'Content-Type: application/json'
/usr/bin/wrtbwmon setup br-lan
/usr/bin/wrtbwmon update /tmp/user-usage.db
echo
echo { \"date\": $(date +%s), \"devices\": [
while IFS=, read MAC IN OUT LASTSEEN IP
do
devices=$devices"{\"mac\": \"${MAC}\", \"ip\": \"${IP}\", \"in\": \"${IN}000\", \"out\": \"${OUT}000\"},"
done < /tmp/user-usage.db
echo ${devices%?}
echo ]}
case $QUERY_STRING in
reset) iptables -L RRDIPT -vnxZ -t filter &> /dev/null; rm /tmp/user-usage.db
esac
#!/bin/sh
#
# Traffic logging tool for OpenWRT-based routers
#
# Created by Emmanuel Brucy (e.brucy AT qut.edu.au)
#
# Based on work from Fredrik Erlandsson (erlis AT linux.nu)
# Based on traff_graph script by twist - http://wiki.openwrt.org/RrdTrafficWatch
#
# 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
# of the License, 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.
# Originally from: http://www.kallisti.net.nz/~robin/wrtbwmon
LAN_IFACE=${2} || $(nvram get lan_ifname)
lock()
{
while [ -f /tmp/wrtbwmon.lock ]; do
if [ ! -d /proc/$(cat /tmp/wrtbwmon.lock) ]; then
echo "WARNING : Lockfile detected but process $(cat /tmp/wrtbwmon.lock) does not exist !"
rm -f /tmp/wrtbwmon.lock
fi
sleep 1
done
echo $$ > /tmp/wrtbwmon.lock
}
unlock()
{
rm -f /tmp/wrtbwmon.lock
}
case ${1} in
"setup" )
#Create the RRDIPT CHAIN (it doesn't matter if it already exists).
iptables -N RRDIPT 2> /dev/null
#Add the RRDIPT CHAIN to the FORWARD chain (if non existing).
iptables -L FORWARD --line-numbers -n | grep "RRDIPT" | grep "1" > /dev/null
if [ $? -ne 0 ]; then
iptables -L FORWARD -n | grep "RRDIPT" > /dev/null
if [ $? -eq 0 ]; then
echo "DEBUG : iptables chain misplaced, recreating it..."
iptables -D FORWARD -j RRDIPT
fi
iptables -I FORWARD -j RRDIPT
fi
#For each host in the ARP table
grep ${LAN_IFACE} /proc/net/arp | while read IP TYPE FLAGS MAC MASK IFACE ;
do
#Add iptable rules (if non existing).
iptables -nL RRDIPT | grep "${IP} " > /dev/null
if [ $? -ne 0 ]; then
iptables -I RRDIPT -d ${IP} -j RETURN
iptables -I RRDIPT -s ${IP} -j RETURN
fi
done
;;
"update" )
[ -z "${2}" ] && echo "ERROR : Missing argument 2" && exit 1
lock
#Read counters
iptables -L RRDIPT -vnx -t filter > /tmp/traffic_$$.tmp
#grep -v "0x0" /proc/net/arp | grep -v 'IP address' | grep -v eth0.1 | while read IP TYPE FLAGS MAC MASK IFACE
#grep -v "00:00:00:00:00:00" /proc/net/arp | grep -v 'IP address' | grep -v eth0.1 | while read IP TYPE FLAGS MAC MASK IFACE
grep -v 'IP address' /proc/net/arp | grep -v eth0.1 | while read IP TYPE FLAGS MAC MASK IFACE
do
#Add new data to the graph. Count in Kbs to deal with 16 bits signed values (up to 2G only)
#Have to use temporary files because of crappy busybox shell
grep ${IP} /tmp/traffic_$$.tmp | while read PKTS BYTES TARGET PROT OPT IFIN IFOUT SRC DST
do
[ "${DST}" = "${IP}" ] && echo $((${BYTES}/1000)) > /tmp/in_$$.tmp
[ "${SRC}" = "${IP}" ] && echo $((${BYTES}/1000)) > /tmp/out_$$.tmp
done
IN=$(cat /tmp/in_$$.tmp)
OUT=$(cat /tmp/out_$$.tmp)
rm -f /tmp/in_$$.tmp
rm -f /tmp/out_$$.tmp
if [ ${IN} -gt 0 -o ${OUT} -gt 0 ]; then
grep -v "${MAC}" ${2} > /tmp/db_$$.tmp
mv /tmp/db_$$.tmp ${2}
echo ${MAC},${IN},${OUT},$(date "+%d-%m-%Y %H:%M"),${IP} >> ${2}
fi
done
#Free some memory
rm -f /tmp/*_$$.tmp
unlock
;;
*)
echo "Usage : $0 {setup|update} [options...]"
echo "Options : "
echo " $0 setup"
echo " $0 update database_file"
echo "Examples : "
echo " $0 setup"
echo " $0 update /tmp/usage.db"
exit
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment