摘要:先简单的介绍一下Xen环境下bridge的创建过程,然后给出多网卡创建bridge的方法以及监控流量的脚本。
一. Xen单网卡网桥创建的过程:
1. the script creates a new bridge named xenbr0
2. "real" ethernet interface eth0 is brought down
3. the IP and MAC addresses of eth0 are copied to virtual network interface veth0
4. real interface eth0 is renamed peth0
5. virtual interface veth0 is renamed eth0
6. peth0 and vif0.0 are attached to bridge xenbr0 as bridge ports
7. the bridge, peth0, eth0 and vif0.0 are brought up
二. 多网卡创建bridge
如果是双网卡则需要添加网卡创建的脚本,使得上面的步骤能够同时进行,添加的过程如下:
修改/etc/xen/xend-config-sxp
修改成如下内容:
#(network-script network-bridge)
(network-script network-xen-multi-bridge)
在/etc/xen/scripts
执行:
mv network-bridge network-bridge.xen
vi network-xen-multi-bridge
内容如下:
- #!/bin/sh
- # network-xen-multi-bridge
- # Exit if anything goes wrong.
- set -e
- # First arg is the operation.
- OP=$1
- shift
- script=/etc/xen/scripts/network-bridge.xen
- case ${OP} in
- start)
- $script start vifnum=1 bridge=xenbr1 netdev=eth1
- $script start vifnum=0 bridge=xenbr0 netdev=eth0
- ;;
- stop)
- $script stop vifnum=1 bridge=xenbr1 netdev=eth1
- $script stop vifnum=0 bridge=xenbr0 netdev=eth0
- ;;
- status)
- $script status vifnum=1 bridge=xenbr1 netdev=eth1
- $script status vifnum=0 bridge=xenbr0 netdev=eth0
- ;;
- *)
- echo 'Unknown command: ' ${OP}
- echo 'Valid commands are: start, stop, status'
- exit 1
- esac
最后执行:
chmod 755 network-xen-multi-bridge
重启xend或者服务器
重启xend
/etc/init.d/xend restart
至此网桥创建完毕。
三. 网络监控
监控VPS物理主机总的流量,主要是监控peth0和peth1
- #!/usr/bin/env python
- from __future__ import division
- import os
- import sys
- import time
- import string
- def getnetdev(netstreamlist):
- fnetdev = open('/proc/net/dev', 'r')
- netdevfile = fnetdev.readlines()
- fnetdev.close()
- for i in range(3, len(netdevfile)):
- nidlist = []
- if netdevfile[i].split()[8] != '0':
- nidlist.append(netdevfile[i].split()[0])
- nidlist.append(netdevfile[i].split()[8])
- netstreamlist.append(nidlist)
- def calculate_bandwidth(timesetting):
- netstreamlist1 = []
- netstreamlist2 = []
- getnetdev(netstreamlist1)
- print ""
- print ""
- print "The average speed in %d :" % timesetting
- time.sleep(timesetting)
- getnetdev(netstreamlist2)
- for i in range(len(netstreamlist1)):
- name = netstreamlist1[i][0].split(":", 1)[0]
- revdata1 = string.atoi(netstreamlist1[i][0].split(":", 1)[1])
- revdata2 = string.atoi(netstreamlist2[i][0].split(":", 1)[1])
- trxdata1 = string.atoi(netstreamlist1[i][1])
- trxdata2 = string.atoi(netstreamlist2[i][1])
- revspeed = (((revdata2 - revdata1))/timesetting)/1024
- trxspeed = (((trxdata2 - trxdata1))/timesetting)/1024
- print "The %s rev speed is %d Kb/s, trx speed is %d Kb/s" % (name, revspeed, trxspeed)
- def main():
- if (len(sys.argv) < 2):
- print "Please enter the time "
- sys.exit()
- while 1:
- calculate_bandwidth(string.atoi(sys.argv[1]))
- if __name__ == '__main__':
- main()
四. 参考资源:
阅读(848) | 评论(0) | 转发(0) |