Bandwidth monitoring on KVM
Solution 1:
I recommend using libvirt for managing your KVM virtual machines. It makes the whole thing much easier than with just plain QEMU/KVM and provides also nice CPU, IO and network statistics. You will like tools such as virt-top and virt-manager which work on top of the libvirt framework.
To collect the virtual machine interface statistics from a script, you could use virsh command line interface to libvirt or some of the language APIs. The format of the virsh command is as follows:
virsh domifstat VMNAME IFNAME
..where VMNAME is the name of your virtual machine and IFNAME is the name of a (virtual) network interface. To find out a name of a network interface you can do:
virsh dumpxml VMNAME
In the resulting XML output, look for your network interface definition(s). Look for a line such as follows:
<target dev='vnet0'/>
In this case your IFNAME would be "vnet0".
Alternatively you could use a statistics collection daemon, such as collectd which has support for monitoring libvirt statistics, but a simple shell script which some times stores the output of "virsh domifstat" is maybe enough in this case.