0

I want to get RX and TX from each network adapter, so to get RX and TX from one network adapter, i used this commands :

cat /proc/net/dev | grep 'wlan0' |awk  '{if ($2) print $2}' |  awk -v var="$(date +"%Y-%m-%d %r")" '{print  $1"&",var";"}' >> /opt/baran/log/baran_RX_usage_$(date +"%Y-%m-%d");
cat /proc/net/dev | grep 'wlan0' |awk  '{if ($10) print $10}' |  awk -v var="$(date +"%Y-%m-%d %r")" '{print  $1"&",var";"}' >> /opt/baran/log/baran_TX_usage_$(date +"%Y-%m-%d");

As you see, i put result into a file with date (daily). I want this in a while loop to get every network adapter instead of just wlan0, so we must have some log files like :

opt/baran/log/baran_TX_usage_wlan0_2014-01-01
opt/baran/log/baran_RX_usage_wlan0_2014-01-01
opt/baran/log/baran_TX_usage_eth0_2014-01-01
opt/baran/log/baran_RX_usage_eth0_2014-01-01
opt/baran/log/baran_TX_usage_eth1_2014-01-01
opt/baran/log/baran_RX_usage_eth1_2014-01-01
opt/baran/log/baran_TX_usage_eth2_2014-01-01
opt/baran/log/baran_RX_usage_eth2_2014-01-01
opt/baran/log/baran_TX_usage_eth3_2014-01-01
opt/baran/log/baran_RX_usage_eth3_2014-01-01

As you can see, i have one wireless adapter and 4 eth, and i want log file for each of them daily.

Thanks

1 Answer 1

2

How about using easier kernel APIs ?

for iface in /sys/class/net/*; do
     # might want to filter out the 'lo' interface

     read rx_bytes < "$iface"/statistics/rx_bytes
     read tx_bytes < "$iface"/statistics/tx_bytes

     day="$(date +"%Y-%m-%d")"
     time="$(date +"%r")"

     printf "%s& %s %s;"  "$rx_bytes" "$day" "$time" >> /opt/baran/log/baran_RX_usage_"$day"
     printf "%s& %s %s;"  "$tx_bytes" "$day" "$time" >> /opt/baran/log/baran_TX_usage_"$day"
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .