Python implementation of System State monitoring and failover instance method _python

Source: Internet
Author: User
Tags failover instance method

Copy Code code as follows:

#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

Copy Code code as follows:

#!/bin/bash

vip=8.8.8.8

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

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.