trafcron.sh
Код: Выделить всё
#!/bin/sh
tmp_file="/tmp/trafd.traffic.$$.tmp"
#out_file="/usr/local/var/trafd/tmp_data.txt"
#external_IP="`/sbin/ifconfig xl0 | grep inet | awk '{print $2}'`"
#internal_IP="`/sbin/ifconfig ep0 | grep inet | awk '{print $2}'`"
#wifi_IP="`/sbin/ifconfig ath0 | grep inet | awk '{print $2}'`"
day="`date +%Y-%m-%d`"
year="`date +%Y`"
month="`date +%m`"
out_file="/usr/local/var/trafd/${year}_${month}.txt"
curr_time="`date +%H:%M:00`"
NewDir="/var/traffic/${year}/${month}"
mkdir -p ${NewDir}
cd ${NewDir}
. /etc/rc.conf
for iface in ${trafd_ifaces}
do
/usr/local/bin/trafsave ${iface}
/usr/local/bin/traflog -i ${iface} -a -n -s > /tmp/summary.${iface} 2>/dev/null
cat /dev/null > /usr/local/var/trafd/trafd.${iface}
cat /tmp/summary.${iface} >> ${NewDir}/summary.${iface}
done
echo "traf for this monf:" > ${out_file}
echo "interface In Out" >> ${out_file}
for iface in ${trafd_ifaces}
do
IP="`/sbin/ifconfig ${iface} | grep inet | awk '{print $2}' | head -1`"
cat ${NewDir}/summary.${iface} | grep "^[0-9]" | grep "^${IP}"| awk '
BEGIN { d=0 }
{
d=$6+d
}
END {
print d/1048576
} ' > ${tmp_file}
read bytes_out < ${tmp_file}
cat ${NewDir}/summary.${iface} | grep "^[0-9]" | grep -v "^${IP}"| awk '
BEGIN { d=0 }
{
d=$6+d
}
END {
print d/1048576
} ' > ${tmp_file}
read bytes_in < ${tmp_file}
if [ "${bytes_in}" = "0" ]
then
echo "no traffic"
else
echo " ${iface} ${bytes_in} Mb ${bytes_out} Mb" >> ${out_file}
fi
rm ${tmp_file}
done
echo "counted `date +%Y-%m-%d` `date +%H:%M:%S` MSK" >> ${out_file}
crontab
Код: Выделить всё
29,59 * * * * root /usr/local/etc/trafcron.sh
rc.conf
Код: Выделить всё
trafd_enable="YES"
trafd_ifaces="rl0"
trafd_log="/var/log/traffic.log"