这篇博客的任务是如何获得一组虚拟机服务器的通信矩阵,及任意两台虚拟机在一段时间的通信量。如标题所示,是基于openvswitch+sflow环境的,关于openvswitch和sflow在前几篇博客已经介绍了,这里不再赘述。
sflowtool提供了一个awk脚本,这个脚本的任务是获得任意两台虚拟机在一个小时内,被采集到报文数(注意只是报文数),详细脚本如下。
#!/usr/bin/awk -f
#
# Copyright (c) 2001 InMon Corp. Licensed under the terms of the InMon sFlow licence:
# http://www.inmon.com/technology/sflowlicense.txt
BEGIN{ lastInt = 0; }
/unixSecondsUTC/{
currentInt = $2 - ($2 % 3600);
if(currentInt != lastInt) {
for(key in count) {
print strftime("%R", lastInt) " " key " " count[key];
}
lastInt = currentInt;
delete count;
}
}
/srcIP/{ srcIP = $2; }
/dstIP/{
key = srcIP " " $2;
count[key] = count[key] + 1; }
END{}
方法很简单,就是统计一个小时内,对任意两对虚拟机采样报文数,我们结合flow采样的数据对上述代码稍微改一下,让其输出采样报文的总字节数(并将统计时间改为60秒)。如下,引入totalPacketSize数组,将脚本命名ipTrafficMatrix_my 。
#!/usr/bin/awk -f
#
# Copyright (c) 2001 InMon Corp. Licensed under the terms of the InMon sFlow licence:
# http://www.inmon.com/technology/sflowlicense.txt
BEGIN{ lastInt = 0;}
/unixSecondsUTC/{
currentInt = $2 - ($2 % 60);
if(currentInt != lastInt) {
for(key in count) {
print strftime("%R", lastInt) " " key " " count[key] " " countPacketSize[key];
}
lastInt = currentInt;
delete count;
delete countPacketSize;
}
}
/srcIP/{ srcIP = $2; }
/sampledPacketSize/{ packetSize = $2;}
/dstIP/{
key = srcIP " " $2;
count[key] = count[key] + 1;
countPacketSize[key] = countPacketSize[key]+packetSize;
}
END{}
我们从192.168.1.56发送一个ubuntu iso镜像( 591M 大小 )到192.168.1.57来验证程序是否达到了功能。
使用scp复制文件
在这之前在sflow的监控端运行sflowtool命令:
sflowtool | ./ipTrafficMatrix_my
程序输出如下:
从上面可以看到 从192.168.1.56 统计了两次,两次统计的报文字节数分别为 5268042 、4415592字节。根据flow采样的采样率(meanSkipCount 为64,认为每64个报文采样一次),我们估测一下检查到的通信量,计算如下:
不得不说还是相当精确的。
扫描二维码关注公众号,回复:
2553426 查看本文章