#!/bin/bash

basepath=$(cd `dirname $0`; pwd)
filename=`echo $0|awk -F '/' '{print $NF}'`
tmpfile="$basepath/tmp/${filename}.tmp"
tmpdir=`dirname $tmpfile`
[ ! -d $tmpdir ] && mkdir $tmpdir

number=0
for netcard in `ls /sys/class/net/| egrep '^eth.$|^nic.$|^net.$'`
do
    cat /sys/class/net/$netcard/operstate |grep -q up
    if [ $? -eq 0 ];then
        active_netcard[number]=$netcard
    fi
    let number=number+1
done

addr=${active_netcard[@]}

collect() {
        ifname=$1
	#查看是否有历史文件
	if [ ! -f "$tmpfile.$ifname" ];then
        	value=`ethtool -S "$ifname"`
		echo "$value" > $tmpfile.$ifname
	else
		oldts=`stat -c %Z $tmpfile.$ifname`
		now=$(date +%s)
		#删除老值插入新值
		if [[ $(($now - $oldts)) -ge 10 ]];then
        		value=`ethtool -S "$ifname"`
			echo "$value" > $tmpfile.$ifname
		fi
	fi
}

for i in $addr
do	
	collect $i
done
