vnstat - 将 WiFi 和以太网的使用结合在一起

vnstat - 将 WiFi 和以太网的使用结合在一起

我的笔记本电脑有两个网络接口enp59s0wlp60s0。从历史上看,我一直使用以太网卡(enp59s0),因此使用它vnstat来获取今天、昨天、本周和本月的网络消耗对我来说从来都不是问题。

最近我搬家了,现在要使用 WiFi(wlp60s0)。我不得不修改我的conky代码:

${color}${goto 5}Today ${goto 100}Yesterday ${goto 225}Week ${goto 325}Month ${color green}
# vnstatd updates database every five minutes
${execi 300 vnstat -i wlp60s0 | grep "today" | awk '{print $8" "substr ($9, 1, 1)}'} ${goto 110}${execi 300 vnstat -i wlp60s0 | grep "yesterday" | awk '{print $8" "substr ($9, 1, 1)}'} ${goto 220}${execi 300 vnstat -i wlp60s0 -w | grep "current week" | awk '{print $9" "substr ($10, 1, 1)}'} ${goto 315}${execi 300 vnstat -i wlp60s0 -m | grep "`date +"%b '%y"`" | awk '{print $9" "substr ($10, 1, 1)}'}

对我来说,将 conky 调用改为 很麻烦,vntstat所以我想将两者结合起来wlp60s0,并enp59s0使用 bash 脚本。我更喜欢 bash,因为它对我来说更容易维护。该脚本每 5 分钟调用一次,因此性能影响很小。

脚本不仅可以让我免于根据接口更改 Conky 代码,而且通过将两个接口加在一起,还可以准确跟踪 ISP 每月配额。

我更喜欢用 bash 脚本来解决这个问题但其他读者可能对 C、Java 或 Python 解决方案感兴趣。

答案1

vnstat此 bash 脚本将把多个接口的总数加起来:

#!/bin/bash

# NAME: vnall
# DESC: Add up multiple interfaces of vnstat into totals for today,
#       yesterday, current week and current month.
# PARM: "today", "yesterday", "week" (current), "month" (current)
# DATE: September 2, 2019. Modified September 4, 2019.

# To discover interfaces `lshw -c network` then place names below
# aInterfaces=( "enp59s0" "wlp60s0" )
# Use automatic discovery if you are unsure of names or don't want to look up
Interfaces=( $(lshw -c network 2>/dev/null | grep name: | cut -d':' -f2) )

Tally () {
    Ktot=0     # Totals in KiB, MiB and GiB
    Mtot=0
    Gtot=0
    # Tally all interfaces
    local i Interface Line Raw ThisNum
    for (( i=0; i<${#aInterfaces[@]}; i++ )) ; do

        Interface="${aInterfaces[i]}"
        if [[ "$vnstatParm" == "" ]] ; then
            Line=$(vnstat -i "$Interface" | grep "$GrepString")
        else
            Line=$(vnstat -i "$Interface" "$vnstatParm" | grep "$GrepString")
        fi

        [[ $Line == "" ]] && continue   # No data collected yet
        if [[ $vnstatParm == "" ]] ; then
            Raw=$(echo "$Line" | awk '{print $8 substr ($9, 1, 1)}')
        else
            Raw=$(echo "$Line" | awk '{print $9 substr ($10, 1, 1)}')
        fi

        ThisNum="${Raw::-1}"    # Number without suffix
        case ${Raw: -1} in      # Decide on last character K, M or G
            K)
                Ktot=$(echo "$Ktot + $ThisNum " | bc) ;;
            M)
                Mtot=$(echo "$Mtot + $ThisNum " | bc) ;;
            G)
                Gtot=$(echo "$Gtot + $ThisNum " | bc) ;;
            *)
                echo "Unknown Unit: ${Raw: -1}" ;;
        esac
    done

    [[ $Gtot != "0" ]] && { echo "$Gtot G" ; return ; }
    [[ $Mtot != "0" ]] && { echo "$Mtot M" ; return ; }
    [[ $Ktot != "0" ]] && { echo "$Ktot K" ; return ; }
    echo "N/A"
} # Tally

Init () {
    GrepString="$1"     # Create defaults for "today" and "yesterday"
    vnstatParm=""
    if [[ $1 == week ]] ; then
        GrepString="current $GrepString"
        vnstatParm="-w"
    fi
    if [[ $1 == month ]] ; then
        GrepString=$(date +"%b '%y")
        vnstatParm="-m"
    fi
} # Init

Init "$@"
Tally

自动与手动接口名称

注意这部分代码:

# To discover interfaces `lshw -c network` then place names below
# aInterfaces=( "enp59s0" "wlp60s0" )
# Use automatic discovery if you are unsure of names or don't want to look up
Interfaces=( $(lshw -c network 2>/dev/null | grep name: | cut -d':' -f2) )

第四行自动发现接口名称(在我的情况下为enp59s0wlp60s0)并构建接口数组(aInterfaces)。如果您希望在脚本中手动指定名称,请#在行首添加注释( ),并通过删除行前缀取消注释第二行#

Conky 代码大幅缩减

因为新vnall脚本会根据需要自动创建参数,所以 conky 代码比以前小了很多:

${color}${goto 5}Today ${goto 100}Yesterday ${goto 225}Week ${goto 325}Month ${color green}
# vnstatd updates database every five minutes
${execi 300 vnall "today"} ${goto 110}${execi 300 vnall "yesterday"} ${goto 220}${execi 300 vnall "week"} ${goto 315}${execi 300 vnall "month"}

Conky 图像

韋爾斯.png

答案2

vnstat命令支持使用+手册页中说明的语法在内部合并接口数据:

-i, --iface interface
       Select one specific interface and apply actions to only it. For queries, it is possible to merge the information of two or
       more interfaces using the interface1+interface2+...  syntax.

-i wlp60s0对于您来说,用替换 就足够了-i wlp60s0+enp59s0

相关内容