#!/bin/ash

[ -z "`which tcpdump`" ] && exit 0

[ -z "$ROOT" ] && ROOT=/www
SCRIPT=$ROOT/script

pid=`ps | grep "tcpdump" | grep "port 67 or port 68" | awk '{print $1}'`
[ -z "$pid" ] || kill -9 $pid

tcpdump -ni any -v -s1500 '((port 67 or port 68) and (udp[8:1] = 0x1))' 2>/dev/null \
  | awk -f $SCRIPT/collector/dhcp-dump.awk 2>/dev/null >/dev/null &

clean() {
  touch /tmp/now.dhcp
  sleep 120
  find /var/stats/arp -type f ! -newer /tmp/now.dhcp -exec rm -rf {} \;
  find /var/stats/rarp -type f ! -newer /tmp/now.dhcp -exec rm -rf {} \;
}

while true ; do
  clean
done
