为了 Google 员工的利益回答一个老问题。
我们遇到了类似的问题,并使用 iptables 计数器“解决”了这个问题,这让我们认识到,所有私有的传出流量都将位于 10.0.0.0/8 IP 地址上,其余的是公共流量。您还可以出于其他目的跟踪输入;当然,只对传出的公共交通收费。
所以,创建一些计数器:
iptables -A INPUT -s 0.0.0.0/0 --> Total incoming traffic
iptables -A INPUT -s 10.0.0.0/8 --> private incoming traffic
iptables -A OUTPUT -d 0.0.0.0/0 --> Total outgoing traffic
iptables -A OUTPUT -d 10.0.0.0/8 --> private outgoing traffic
检查计数器:
iptables -nv -L INPUT --> counters about incoming traffic
iptables -nv -L OUTPUT --> counters about outgoing traffic
注意:当您使用这些值时,您会得到私有和 TOTAL:因此要公开,请先从 Total 中减去私有,然后再将其用于任何事情。
如果您不想报告累积带宽,也可以将计数器清零:
iptables --zero INPUT --> clear counter
iptables --zero OUTPUT --> clear counter
假设您已经创建了计数器,以下是一个(丑陋的)bash 脚本,它将将此信息推送到 Ganglia:
#!/bin/bash
OUTPUT_PUBLIC=`sudo iptables -nvx -L OUTPUT | head -3 | tail -1 | tr -s [:blank:] |cut -d' ' -f3`
OUTPUT_PRIVATE=`sudo iptables -nvx -L OUTPUT | tail -1 | tr -s [:blank:] |cut -d' ' -f3`
let OUTPUT_PUBLIC=$OUTPUT_PUBLIC-$OUTPUT_PRIVATE
sudo iptables --zero INPUT
sudo iptables --zero OUTPUT
gmetric -n "public_outbound_traffic" -v $OUTPUT_PUBLIC -t uint32 -u "bytes"
gmetric -n "private_outbound_traffic" -v $OUTPUT_PRIVATE -t uint32 -u "bytes"
在 cronjob 中运行它,只需确保 cronjob 频率与您的神经节报告频率匹配(或以其他方式处理可能的不匹配)。
希望这对某人有所帮助。