问题
I have a server with OpenVZ installed and virtual machines with a public IPv4 for every machine. I need to get the bandwidth usage of each separate virtual machine (mbps or kbps) from PHP. My idea is to install iftop on the host machine and run it from PHP function exec(), the command "iftop -n -F AA.BB.CC.DD/32". The problem is that is an application iftop "top" style, and is made to run on a real terminal and is updated with new data. I can not install anything on the VMs, should be clean. Nor has a network interface each VM and I can not change it. I wonder what other options I have or how to carry out my idea. Sorry for my english
回答1:
If the guests are also running Linux or they have separate interfaces on the host, you can cat /sys/class/net/$interface_name/statistics/rx_bytes and tx_bytes, just compare the difference and divide by time to get an estimate of current throughput.
来源:https://stackoverflow.com/questions/4066029/get-the-current-bandwidth-use-in-linux-with-php