如何优化 munin fw_conntrack 脚本

如何优化 munin fw_conntrack 脚本

我有一个服务器,ip_conntrack 中有大约 65k 个连接。我该如何优化统计各种连接类型的 munin 脚本(默认fw_conntrack脚本):

cat /proc/net/ip_conntrack | awk '
BEGIN {
    STATE["ESTABLISHED"]=STATE["FIN_WAIT"]=STATE["TIME_WAIT"]=0;
    TOTAL=ASSURED=NOREPLY=STATE["SYN_SENT"]=STATE["UDP"]=0; 
}
/^tcp/ { STATE[$4]++; }
/^udp/ { STATE["UDP"]++; }
/ASSURED/ { ASSURED++; }
{
  TOTAL++;
}
END {
    print "established.value " STATE["ESTABLISHED"];
    print "fin_wait.value " STATE["FIN_WAIT"];
    print "time_wait.value " STATE["TIME_WAIT"];
    print "syn_sent.value " STATE["SYN_SENT"];
    print "udp.value " STATE["UDP"];
    print "assured.value " ASSURED;
    print "total.value " TOTAL;
}'

目前执行大约需要 30 秒。

答案1

替换cat /proc/net/ip_conntrackconntrack -L。对于大量连接,其效率更高。

例子:

root@utemp:~# time conntrack -L | wc -l
conntrack v0.9.14 (conntrack-tools): 16855 flow entries have been shown.
16855

real    0m0.099s
user    0m0.068s
sys     0m0.036s

root@utemp:~# time cat /proc/net/ip_conntrack | wc -l
16634

real    0m0.270s
user    0m0.008s
sys     0m0.264s

相关内容