Resetting ifconfig counters

Every Linux user is probably familiar with the ifconfig command. This allows us to see the current network settings on a system and to make various changes to the network interfaces (change IP, bring up, down, etc.). A typical run of ifconfig (or ifconfig -a) without any parameters will show the status of all the network interfaces, while using a network interface name (like ifconfig eth0) will output the status of only that interface.

Let’s see how a sample output looks like:

ifconfig eth0
eth0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4233700943 errors:2 dropped:0 overruns:3 frame:5
TX packets:1917219659 errors:1 dropped:0 overruns:0 carrier:348

collisions:5753 txqueuelen:3000
RX bytes:1467520026 (1.3 GiB) TX bytes:2299240337 (2.1 GiB)

As we can see here ifconfig is keeping a couple of counters (RX/TX packets, RX/TX bytes, errors, dropped, overruns, frame, carrier collisions). We can quickly spot if there is a problem by just looking at the ifconfig counters. It is not the scope of this post how to solve these issues, but let’s consider that the problem is solved and then we normally don’t want to see the errors there anymore. Of course if there are new errors that show the same problem or a new one that would be ok to see them. So the normal thought is how to clear those counters and start fresh. How to reset them? If someone has tried to do this before, they might tell us that restarting the network interface will not help. This would have been the most logical choice, but how can we do this without rebooting the system?

The idea is that those counters are kept by the kernel (more exactly by the network card driver). By the way we can look at the same counters directly on the proc system using:

cat /proc/net/dev

Considering this, we have 2 possible choices: the network card driver is build either as a kernel module or build-in statically inside the kernel. These relate to our issue like this:

  • kernel module: in this case we can solve this issue and reset the counters by unloading the module from the kernel and then loading it back again. This will clear the counters.
    Assuming that I have a NIC using the e100 module this can be done by:
    modprobe -r e100; modprobe e100; ifup eth0

    or something similar to bring the interface back up (depending on your particular Linux distribution the script to bring up the network interface might be different).

    Note: this will bring down the network interface, and if you are doing this over a ssh connection, use extra care (like maybe create a small script, add some extra measures to ensure you will not lock yourself out of the system).

  • module built-in the kernel: in this case there is no way to reset the counters (of course without rebooting the system) if the particular network module driver has not a way to reset them using some code written inside the driver. From what I have seen in the source code of the modules I had used, none of them had such a software hook. But there might be other modules that offer this functionality… just that I have not personally seen it on the common NIC drivers I have used.

By the way a quick way to find out what particular driver is using a network interface is to use ethtool (a very powerfool tool that has many great usages):

ethtool -i eth0
driver: e100
version: 3.4.14-k4-NAPI
firmware-version: N/A
bus-info: 0000:01:07.0

I hope that you found this post useful and please feel free to use the comment box bellow to let me know if you have any additions/corrections to this.

转自http://www.ducea.com/2006/09/08/resetting-ifconfig-counters/

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12759501/viewspace-1042439/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12759501/viewspace-1042439/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值