欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现系统状态监测和故障转移实例方法

程序员文章站 2023-11-11 12:58:22
复制代码 代码如下:#coding: utf-8import socketimport selectimport timeimport osimport threading...

复制代码 代码如下:

#coding: utf-8
import socket
import select
import time
import os
import threading

def ser():
    s = socket.socket(socket.af_inet,socket.sock_dgram)
    s.bind(("",43244))
    while 1:
        infds,outfds,errfds = select.select([s],[],[],5)
        if infds:
            sms = s.recv(1024)
            if sms=="alived":
                print "peer is alived"
        else:
            print "can't hear peer!"
            os.system("./failover.sh")

def clt():   
    while 1:
       sock = socket.socket(socket.af_inet, socket.sock_dgram) 
       sock.connect(('192.168.10.1', 43244))
       sock.send("alived")
       time.sleep(2)

if __name__=="__main__":
    ser=threading.thread(target=ser)
    clt=threading.thread(target=clt)
    ser.start()
    clt.start()
    ser.join()
    clt.join()

failover.sh

复制代码 代码如下:

#!/bin/bash

vip=8.8.8.8

vip_local=`ifconfig |grep -a 1 "eth0:0" |awk '/inet addr/{print $2}'|cut -d ":" -f2`

if [ ! $vip_local ];then ifconfig eth0:0 $vip netmask 255.255.255.0 up;fi