如何确定当前的网络利用率?


9

我想显示网站上Debian框的一个界面的当前网络利用率(带宽利用率)。它不应该是非常详尽或精确的,只是一个简单的数字,例如“ 52 Mbit / s”。

诸如此类的典型网络带宽监视器iftop使我无法简单地提取此类值。

如何最好地找回它?

例如,我想我可能/proc/net/dev每隔几分钟就解析一次。虽然不确定这是否真的是最好的方法。

Answers:


10

我认为ifstat将帮助您:

[root @ localhost〜]#ifstat -i eth0 -q 1 1
       eth0
 KB / s进出KB / s
 3390.26 69.69

7

做到这一点的最佳方法可能就是解析/proc/net/dev(警告它/proc不可移植)。这是bash我快速整理的脚本,应该可以计算出来:

#!/bin/bash

_die() {
    printf '%s\n' "$@"
    exit 1
}

_interface=$1

[[ ${_interface} ]] || _die 'Usage: ifspeed [interface]'
grep -q "^ *${_interface}:" /proc/net/dev || _die "Interface ${_interface} not found in /proc/net/dev"

_interface_bytes_in_old=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { sub(/^.*:/, "") ; print $1 } else { print $2 } }' /proc/net/dev)
_interface_bytes_out_old=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { print $9 } else { print $10 } }' /proc/net/dev)

while sleep 1; do
    _interface_bytes_in_new=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { sub(/^.*:/, "") ; print $1 } else { print $2 } }' /proc/net/dev)
    _interface_bytes_out_new=$(awk "/^ *${_interface}:/"' { if ($1 ~ /.*:[0-9][0-9]*/) { print $9 } else { print $10 } }' /proc/net/dev)

    printf '%s: %s\n' 'Bytes in/sec'  "$(( _interface_bytes_in_new - _interface_bytes_in_old ))" \
                      'Bytes out/sec' "$(( _interface_bytes_out_new - _interface_bytes_out_old ))"

    # printf '%s: %s\n' 'Kilobytes in/sec'  "$(( ( _interface_bytes_in_new - _interface_bytes_in_old ) / 1024 ))" \
    #                   'Kilobytes out/sec' "$(( ( _interface_bytes_out_new - _interface_bytes_out_old ) / 1024 ))"

    # printf '%s: %s\n' 'Megabits in/sec'  "$(( ( _interface_bytes_in_new - _interface_bytes_in_old ) / 131072 ))" \
    #                   'Megabits out/sec' "$(( ( _interface_bytes_out_new - _interface_bytes_out_old ) / 131072 ))"

    _interface_bytes_in_old=${_interface_bytes_in_new}
    _interface_bytes_out_old=${_interface_bytes_out_new}
done

请记住,sleep没有考虑在while循环中执行操作所花费的时间,因此(非常轻微)这是不准确的。在我的600mhz铜矿上,循环需要0.011秒-对于大多数用途而言,这种误差可以忽略不计。当使用(注释)千字节/兆位输出时,请记住,bash仅理解整数算术。


我认为这应该是选择的答案。其他所有解决方案都在解析后在后台进行中继,/proc/net/dev而没有真正了解这种魔术的作用和发生方式。
伊兰

此解决方案在路由器/ busybox上对我有用。
克隆人

使用date +%s.%N获得UNIX时间戳每次迭代和除以时间戳差异字节的差异。这样就避免了循环迭代超过1s的问题。
阿纳维翁

3

有一些网络流量监控器,例如vnstat 可以每月记录一次您的流量,也可以使用slurm直接从存储在内核中的值中获取其值。在大多数发行版存储库中都可用。

这是我跑步时看到的slurm -i ra0

在此处输入图片说明


1

这是一个非常简单的shell脚本来计算:

#!/bin/sh

dev=$1

grep -q "^$dev:" /proc/net/dev || exec echo "$dev: no such device"

read rx <"/sys/class/net/$dev/statistics/rx_bytes"
read tx <"/sys/class/net/$dev/statistics/tx_bytes"

while sleep 1; do
    read newrx <"/sys/class/net/$dev/statistics/rx_bytes"
    read newtx <"/sys/class/net/$dev/statistics/tx_bytes"

    # convert bytes to kbit/s: bytes * 8 / 1000 => bytes / 125
    echo "$dev  {rx: $(((newrx-rx) / 125)), tx: $(((newtx-tx) / 125))}"

    rx=$newrx
    tx=$newtx
done

只需启动传递接口名称的脚本即可,例如。 ./shtraf eth1


1
你能解释一下吗?该参数究竟应该是什么?的意义是125什么?请不要在评论中回复;编辑您的答案,使其更清晰,更完整。
斯科特
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.