软件环境
操作系统 CentOS
运行环境:python 2.7
工具:/sbin/ipvsadm
参数说明
[root@gd02-zabbix-db-research python_api]# ./lvs.py
usage: ./lvs.py --help or --example
[root@gd02-zabbix-db-research python_api]# ./lvs.py --help
usage: lvs.py [-h] [-A] [-D] [-F] [-L] [-t] [-u] [-s SCHEDULER]
[-p PERSISTENT] [-r REALSERVER] [-f FLOAPINGIP] [-g] [-m]
[-P SRCPORT] [-R DESTPORT] [--example]
optional arguments:
-h, --help show this help message and exit -A, --add add LVS rules -D, --delete delete LVS rules -F, --fresh clean all LVS rules -L, --show show LVS status -t, --tcp tcp service -u, --udp udp service -s SCHEDULER, --scheduler SCHEDULER one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,the
default scheduler is wlc
-p PERSISTENT, --persistent PERSISTENT persistent service, default:1500
-r REALSERVER, --realserver REALSERVER server-address is host (and port) Example: -r '1.1.1.1
2.2.2.2 3.3.3.3'
-f FLOAPINGIP, --floapingip FLOAPINGIP vip address of lvs
-g, --gatewaying gatewaying (direct routing) -m, --masquerading masquerading (NAT) -P SRCPORT, --srcport SRCPORT listen on floapip port
-R DESTPORT, --destport DESTPORT listen on realserver port
--example show create LVS rule example
python 脚本
#!/apps/svr/python/python
#coding:utf-8
# Terry zeng <[email protected]>
#
# 2014 04 10
#
# lvs 调用接口
#
# 脚本主要功能:
# 1. 参数输入判定
# 2. 查询, 添加, 删除, 清空 lvs 规则
# 3. 帮助语法: ./lvs.py --help
# 4. 添加规则语法:
#
# 添加 udp 53 端口调度例子
# ./lvs.py --add --udp --scheduler wlc --persistent 1500 --floapingip 61.1.1.1 --srcport 53 --realserver "192.168.1.1 172.16.14.1 10.1.1.1" --destport 53 --gatewaying
# ./lvs.py -A -u -s wlc -p 1500 -f 61.1.1.1 -P 53 -r "192.168.1.1 172.16.14.1 10.1.1.1" -R 53 -g
#
#
# 添加 tcp 80 端口调度例子
# ./lvs.py --add --tcp --scheduler rr --persistent 1500 --floapingip 202.1.1.1 --srcport 80 --realserver "192.168.1.1 172.16.14.1 10.1.1.1 10.3.4.1" --destport 8080 --masquerading
# ./lvs.py -A -t -s rr -p 1500 -f 202.1.1.1 -P 80 -r "192.168.1.1 172.16.14.1 10.1.1.1 10.3.4.1" -R 8080 -m
#
#
# 删除某个规则方法
# ./lvs.py --delete --tcp --scheduler rr --persistent 1500 --floapingip 202.1.1.1 --srcport 80 --realserver "192.168.1.1 172.16.14.1 10.1.1.1 10.3.4.1" --destport 8080 --masquerading
#
# 清空所有规则
# ./lvs.py --fresh
# 显示规则方法
# ./lvs.py --show
#
import argparse
import sys
import socket
import os
import re
### 判断 ipvsadm 是否可用
if os.path.exists('/sbin/ipvsadm') == False:
print "install ipvsadm pckage first"
print "contact administrator or rum command: yum install -y ipvsadm"
exit()
## ip 地址可用性判断
def valid_ip(address):
try:
socket.inet_aton(address)
return True
except:
return False
def usage():
print 'usage: ' + __file__ + ' --help or --example'
sys.exit(2)
#### 参数调用说明
parser = argparse.ArgumentParser()
parser.add_argument('-A','--add', help='add LVS rules', action='store_true')
parser.add_argument('-D','--delete', help='delete LVS rules', action='store_true')
parser.add_argument('-F','--fresh', help='clean all LVS rules', action='store_true')
parser.add_argument('-L','--show', help='show LVS status', action='store_true')
parser.add_argument('-t','--tcp', help='tcp service',action='store_true')
parser.add_argument('-u', '--udp', help='udp service', action='store_true')
parser.add_argument('-s', '--scheduler', help='one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,the default scheduler is wlc')
parser.add_argument('-p', '--persistent', help='persistent service, default:1500', type = int)
parser.add_argument('-r', '--realserver', help="server-address is host (and port) Example: -r '1.1.1.1 2.2.2.2 3.3.3.3'", type = str )
parser.add_argument('-f', '--floapingip', help='vip address of lvs' )
parser.add_argument('-g', '--gatewaying', help='gatewaying (direct routing)',action='store_true')
parser.add_argument('-m','--masquerading', help='masquerading (NAT)', action='store_true')
parser.add_argument('-P','--srcport', help='listen on floapip port', type = int )
parser.add_argument('-R','--destport', help='listen on realserver port', type = int )
parser.add_argument('--example', help='show create LVS rule example', action='store_true' )
args = parser.parse_args()
## 对参数进行空值判断, add delete fresh show example 重复,可用性判断
if args.example == True:
print __file__ + ' --add --tcp --scheduler wlc --persistent 1500 --floapingip 61.1.1.1 --srcport 80 --realserver "192.168.1.1 172.16.14.1 10.1.1.1" --destport 8080 --gatewaying'
print __file__ + ' --delete --tcp --scheduler wlc --persistent 1500 --floapingip 61.1.1.1 --srcport 80 --realserver "192.168.1.1 172.16.14.1 10.1.1.1" --destport 8080 --gatewaying'
print __file__ + ' --fresh'
print __file__ + ' --show'
sys.exit()
rule = False
if args.add == True:
if True in [ args.delete, args.fresh, args.show ]:
usage()
else:
rule = "add"
if args.delete == True:
if True in [ args.add, args.fresh, args.show ]:
usage()
else:
rule = "delete"
if args.fresh == True:
if True in [ args.add, args.delete, args.show ]:
usage()
else:
os.system('/sbin/ipvsadm -C')
os.system('/etc/init.d/ipvsadm save')
exit()
if args.show == True:
if True in [ args.add, args.delete, args.add ]:
usage()
else:
os.system('/sbin/ipvsadm -L -n')
exit()
if rule == False:
usage()
## 对参数进行空值判断, tcp, udp || DR, NAT 重复判断
if args.tcp == args.udp:
usage()
if args.gatewaying == args.masquerading:
usage()
### 判断 persistent 值是否正确 start
if args.persistent == None:
persistent = "1500"
else:
persistent = str(args.persistent)
### 判断 persistent 值是否正确 end
### 判断 scheduler start
if args.scheduler == None:
scheduler = 'wlc'
else:
scheduler = args.scheduler
if scheduler not in ('rr', 'wrr', 'lc', 'wlc', 'lblc', 'lblcr', 'dh', 'sh', 'sed', 'nq'):
print 'scheduler must one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,the default scheduler is wlc'
usage()
### 判断 scheduler end
### 判断 realserver 及 ip 地址 start
### 解决参数中包含多个空格问题
### 解决 IP 地址验证问题
if args.realserver == None:
print "--realserver is needed, example: -r '1.1.1.1 2.2.2.2 3.3.3.3'"
usage()
else:
realip = re.sub(r'\s+', ' ',args.realserver)
realserver = realip.split(' ')
for address in realserver:
if valid_ip(address) == False:
print "--realserver ip error, example: -r '1.1.1.1 2.2.2.2 3.3.3.3'"
usage()
### 判断 realserver 及 ip 地址 end
### 判断 floading ip start
if args.floapingip == None:
print "--floapingip is needed, example: -f '1.1.1.1'"
usage()
else:
floapingip = args.floapingip
if valid_ip(floapingip) == False:
print "--realserver ip error, example: -f '1.1.1.1'"
usage()
### 判断 floading ip end
### 判断 vip port start
if args.srcport == None:
print "--srcport is needed, example: -S '80'"
usage()
else:
srcport = str(args.srcport)
### 判断 vip port start
### 判断 realserver port start, 切记类型转换
if args.destport == None:
print "--destport is needed, example: -R '80'"
usage()
else:
destport = str(args.destport)
### 判断 realserver port end
###lvs 语法
if args.tcp == False:
protocal = "-u"
else:
protocal = "-t"
if args.gatewaying == None:
gateway = "-m"
else:
gateway = "-g"
if rule == "add":
main_rule = "/sbin/ipvsadm -A " + protocal + " " + floapingip + ":" + srcport + " -s " + scheduler + " -p " + persistent
os.system(main_rule)
for ip in realserver:
sub_rule = "/sbin/ipvsadm -a " + protocal + " " + floapingip + ":" + srcport + " -r " + ip + ":" + srcport + " " + gateway
os.system(sub_rule)
elif rule == "delete":
main_rule = "/sbin/ipvsadm -D " + protocal + " " + floapingip + ":" + srcport
os.system(main_rule)
os.system('/etc/init.d/ipvsadm save')