目前在杭州与上海间有两条带宽各为1G的线路,当其中一条线路断开后,流量会自动切换到另一条线路。MASTER线路在正常情况跑非spark间跨机房流量,平均流量在250Mb,峰值500M。SPARK线路正常情况跑跨机房spark与spark间的流量,平均流量小,峰值为跑满1G。
当前MASTER线路单IP限流250Mb,当spark线路断开后,流量切到master线路,自动进入400M上限限速,保证MASTER正常业务最小有600M可用带宽。因为master线路间的流量涉及IP广,无法提前标记流量,导致无法对MASTER流量做预置的QOS,同时要保证SPARK线路在两条线路都正常时,spark线路可以尽最大带宽跑流量。因当前网络设备不支持联动QOS策略,需要借助其它工具实现。跨机房QOS自动化是解决当前MASTER线路断开后流量切到SPARK线路,刚好SPARK线路流量正在满带宽在跑,MASTER流量无法得到保障问题。
设计思路:
在杭州端的设备上开启SNMP-TRAP,并在杭州机房布署,SNMP接收服务器。当接收到MASTER线路断开的NQA报警后,服务器自动登陆到设备启用SPARK线路上的限速策略。当接收到MASTER线路恢复正常报警后,服务器自动登陆到设备取消限速策略。
具体实施:
网络设备端:开启SNMP-TRAP,并放行到接收服务器的安全策略。
服务器端:
1、环境准备,需要用到的包:pysnmp,paramiko, twisted,time
2、服务端代码:
from twisted.internet import reactor
from pysnmp.entity import engine, config
from pysnmp.carrier.twisted.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
import paramiko
import time
#device ip username and passwd
username = "*****"
passwd = "******"
ip = '192.168.45.36'
#comands
cmdup = ['sys','traffic-policy','rule name spark_hz>sh','disable','y',' rule name spark_sh>hz','disable','y']
cmddown = ['sys','traffic-policy','rule name spark_hz>sh','enable','rule name spark_sh>hz','enable']
#ssh device
def ssh2(ip,username,passwd,cmd):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,passwd,timeout=5)
ssh_shell = ssh.invoke_shell()
print ssh_shell.recv(1024)
for m in cmd:
res = ssh_shell.sendall(m+'\n')
time.sleep(float(1))
print ssh_shell.recv(1024)
ssh.close()
except :
print '%s\tError\n'%(ip)
# Create SNMP engine with autogenernated engineID and pre-bound
# to socket transport dispatcher
snmpEngine = engine.SnmpEngine()
# Transport setup
# UDP over IPv4, first listening interface/port
config.addTransport(
snmpEngine,
udp.domainName + (1,),
udp.UdpTwistedTransport().openServerMode(('0.0.0.0', 162))
)
# SNMPv1/2c setup
# SecurityName <-> CommunityName mapping
config.addV1System(snmpEngine, 'my-area', 'public')
# Callback function for receiving notifications
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def cbFun(snmpEngine, stateReference, contextEngineId, contextName,
varBinds, cbCtx):
#Do what you want to do
for name, val in varBinds:
if val.prettyPrint()=='hz_sh_master':
for name, val in varBinds:
if val.prettyPrint()=='down':
print "down"
cmd=cmddown
ssh2(ip,username,passwd,cmd)
elif val.prettyPrint()=='up':
print "up"
cmd=cmdup
ssh2(ip,username,passwd,cmd)
else:
pass
else:
pass
# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(snmpEngine, cbFun)
# Run Twisted main loop
reactor.run()
# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(snmpEngine, cbFun)
# Run Twisted main loop
reactor.run()
测试结果:
[root@192-168-56-48 snmptrap]# python snmptrap.py
down
*************************************************************************
* Copyright (C) 2013-2017 Huawei Technologies Co., Ltd. *
* All rights reserved. *
* Without the owner's prior written consent, *
* no decompiling or reverse-engineering shall be allowed. *
*************************************************************************
Info: The max number of VTY users is 10, and the number
of current VTY users on line is 2.
The current login time is 2018-04-18 00:15:13+08:00.
HRP_M<XS-FW_ZX_Master>sys
Enter system view, return user view with Ctrl+Z.
HRP_M[XS-FW_ZX_Master]traffic-policy (+B)
HRP_M[XS-FW_ZX_Master-policy-traffic]rule name spark_hz>sh (+B)
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_hz>sh]enable (+B)
Info: The policy is enabled successfully.
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_hz>sh]rule name spark_sh>hz (+B)
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_sh>hz]enable (+B)
Info: The policy is enabled successfully.
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_sh>hz]
up
*************************************************************************
* Copyright (C) 2013-2017 Huawei Technologies Co., Ltd. *
* All rights reserved. *
* Without the owner's prior written consent, *
* no decompiling or reverse-engineering shall be allowed. *
*************************************************************************
Info: The max number of VTY users is 10, and the number
of current VTY users on line is 2.
The current login time is 2018-04-18 00:16:29+08:00.
HRP_M<XS-FW_ZX_Master>sys
Enter system view, return user view with Ctrl+Z.
HRP_M[XS-FW_ZX_Master]traffic-policy (+B)
HRP_M[XS-FW_ZX_Master-policy-traffic]rule name spark_hz>sh (+B)
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_hz>sh]disable (+B)
Warning: This command will invalidate the rule. Continue?[Y/N]y
Info: The policy is disabled successfully.
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_hz>sh] rule name spark_sh>hz (+B)
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_sh>hz]disable (+B)
Warning: This command will invalidate the rule. Continue?[Y/N]y
Info: The policy is disabled successfully.
HRP_M[XS-FW_ZX_Master-policy-traffic-rule-spark_sh>hz]