keepalived split brain script PY-- concurrent ping packet, waits for the command is not used

vim keepalived_nginx.py
i
# coding=utf-8
import os,time,subprocess,threading
def ping(Sip,Dip):
Ip='ping -c 1 -I %s %s'%(Sip,Dip)
p = subprocess.Popen([Ip],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
out = p.stdout.read().decode('utf-8').split(',')
for i in out:
if i.find('received')>0:
return i.split()[0]

class MyThread(threading.Thread):
def __init__(self, func, args):
super(MyThread, self).__init__()
self.func = func
self.args = args
def run(self):
self.result = self.func(*self.args)

def get_result(self):
try:
return self.result
except Exception:
return None

if __name__ == "__main__":
Sip1 = '192.168.3.224'
Sip2 = '172.16.0.224'
Gip1 = '192.168.3.1'
Dip1 = '192.168.3.225'
Dip2 = '172.16.0.225'
Gip2 = '172.16.0.1'
i = 0
while True:
print()
time.sleep(3)
t1 = MyThread(ping, args=(Sip1, Gip1))
t1.start()
t2 = MyThread(ping, args=(Sip1, Dip1))
t2.start()
t3 = MyThread(ping, args=(Sip2, Dip2))
t3.start()
the MyThread = T4 (of ping, args = (Sip2, Gip2))
t4.start ()
t1.join ()
t2.join ()
t3.join ()
t4.join ()
I1 = int (t1.get_result ())
I2 int = (t2.get_result ())
I3 = int (t3.get_result ())
I4 = int (t4.get_result ())
IF I1 and I4:
the os.system ( 'Start systemctl keepalived Nginx')
I = 0
IF I2 and I3 0 == 0 ==:
Print ( 'warning, Keepalived the master device outside the network are broken \ n the device master device \ n')
elif I2 and I3 0 ==> 0:
Print ( 'warning, the main Keepalived disconnecting the external network \ n the device master device \ n ')
elif I3 == 0 and I2> 0:
print ( 'warning, disconnect the main network Keepalived \ n the device master device \ n')
the else:
print ( 'normal network, the device is a backup')
the else:
IF I1 == 0:
print ( 'warning, outer network disconnected \ the n-')
the else:
Print (' warning, the network is disconnected \ the n-')
os.system (' systemctl STOP keepalived nginx ')
Print (' nginx and keepalived service has been suspended for \ the n-')
i = 1
the Continue

Guess you like

Origin www.cnblogs.com/Leaders543/p/12528959.html