Using Python socket to realize LVS automatic weighing adjustment and monitoring management

Source: Internet
Author: User
Tags connect readline socket split

With friends. This application ~ was written in Shell before, the method is two, and the performance is not good, because the company has a better solution, so this method can be shared out >

A python to monitor the operation of LVS, according to the back-end server load situation, to achieve LVS weighing load

Because everyone's environment is not the same, it is best to look at the code, many tests and then use ~

This program needs to set a threshold to run server.py in Realserver running client.py at the LVs end.

The backend server will send the current load situation, using the socket way to the LVS end, with the LVs end will check Ipvsadm-l-n situation.

If the check reaches the warning threshold, the corresponding balance, if not, nothing happens. When the load returns to normal, the weights are set back to their original values.

I will be in the middle, the code to explain the comments ~

Communication with XMLRPC, not suitable for large-scale applications, to deal with this simple report, it is still possible.

For a small example, you can see it.

XML RPC is an RPC mechanism that uses the HTTP protocol as a transport protocol to transmit commands and data in the form of XML text. An RPC system, which must consist of 2 parts: 1.rpc client, is used to invoke the method to RPC server and receive the return data of the method; 2.rpc server, which responds to RPC client requests, executes the method, and echoes the method execution results.

Rpcserver

Import Simplexmlrpcserver     
         
class HelloWorld:     
    def Say (self): return     
        "Hello, world!" 
         
obj = HelloWorld ()     
server = Simplexmlrpcserver.simplexmlrpcserver (("localhost")     
server.register_ Instance (obj)     
print "Listening on port ..." 
Server.serve_forever ()

Rpcclient

Import xmlrpclib     
server = Xmlrpclib. Serverproxy ("http://localhost:80")     
print "Result:" + Server.say ()

server.py

#!/usr/bin/env python #-*-Coding:utf-8-*-# binds its own IP and an available port ServerIP = ' 172.17.0.100 ' ServerPort = 12219 # Set the server's load threshold loadavg_crit_threshold = ' 1.00 ' loadavg_warn_threshold = ' 0.60 ' # when, will set a tradeoff such as setting 5 is 20% C Hangeweightwarn = 4 # 100%/4 = -25% of original Changeweightcrit = 2 # 100%/2 = -50% of original from Simp Lexmlrpcserver Import simplexmlrpcserver from simplexmlrpcserver import simplexmlrpcrequesthandler import string,s Ocket # convert Hex2dec def hex2dec (s): Return int (S.) # simple routine to read from a fi  Le, line by line def readlines (filename): f = open (filename, "r") lines = F.readlines () Lines # or Realserver IP in/proc/net/ip_vs connection condition # This ACL should is built with the following format: # {RI  P = [VIP, WEIGHT, Weightcurrent, Vport, Rport]} print "Building ACL from LVS table ..." accesslist={ ReadLines ('/PROC/NET/IP_vs '): If Line.split () [0] = = ' TCP ' or line.split () [0] = = ' UDP ': VIP = Line.split () [1].split (': ') [0] VIPVIPFST = Vip[0:2] vipvipscd = vip[2:4] vipviptrd = vip[4:6] Vipvipfth = V Ip[6:8] Vipport = Hex2dec (Line.split () [1].split (': ') [1]) Virtualip = str (HEX2DEC (VIPFST)) + "." +    
        STR (HEX2DEC (VIPSCD)) + "." + str (HEX2DEC (VIPTRD)) + "." + str (HEX2DEC (vipfth)) if Line.split () [0] = = '-> ': If Len (str (Line.split () [1])) = = 13:rsip = Line.split () [1].split (': ') [0] Rsiprsipf st = Rsip[0:2] # octet RSIPRSIPSCD = rsip[2:4] # second octet RSIPRSIPTRD = Rsip[4:6 ] # third Octet rsiprsipfth = Rsip[6:8] # fourth Octet Rsipport = Hex2dec (Line.split () [1].    
            Split (': ') [1]) # Get routing Type (Nat/direct/tun) Rsroutetype = Line.split () [2] if Rsroutetype = = "Route": Rsroute = "g" elif Rsroutetype = = "MASQ": Rsroute = "M" elif Rsroutetype = = "Tunnel": Rsroute = "I" # compile RIP Realserv    
            Erip = str (HEX2DEC (RSIPFST)) + "." + str (HEX2DEC (RSIPSCD)) + "." + str (HEX2DEC (RSIPTRD)) + "." + str (HEX2DEC (rsipfth))  # build Realserver List realserverlist = [Virtualip] # Add original weight to List Realserverlist.append (Line.split () [3]) # Add current weight to list (same as Origina    
            L weight) Realserverlist.append (Line.split () [3]) # Add virtual port to list    
            Realserverlist.append (vipport) # Add real port to list realserverlist.append (Rsipport) # Add routing type to list Realserverlist.append (rsroute) # Add KeyPair (IP    
   : weight) to ACL         Accesslist[realserverip] = realserverlist print "Adding%s:%s:%s:%s:%s:%s to ACL as RIP:VIP:weigh TOrig:weightCurrent:vPort:rPort:rType "% (str (realserverip), str (virtualip), str (accesslist[realserverip][1)), str ( Vipport), str (rsipport), str (rsroutetype)) # or Load condition and pushload def pushload_function (Loadavg, clientip): I  mport os LoadAvgLoadAvg1 = Loadavg.split () [0] #print "debug:client reports IP:%s"% ClientIP # Determine original weight rsweightoriginal = accesslist[clientip][1] #print "debug:original weight of Rs :%s% rsweightoriginal # determine changed weight rsweightcurrent = accesslist[clientip][2] #pr int "Debug:current weight of Rs:%s"% rsweightcurrent # determine if CRITICAL threshold is reached if f Loat (LOADAVG1) >= float (loadavg_crit_threshold): print "Critical:load Average (1min) threshold (%s) Reache D:%s '% (str (loadavg_crit_threshoLD), LOADAVG1) rsweighthalf = Int (rsweightoriginal)-INT (rsweightoriginal)/2 # Only change weight if n OT already changed if not int (rsweightcurrent) = = Int (rsweighthalf): print "Changing Realserve R ' s weight to 50% of original "print" running/sbin/ipvsadm-e-T "+ str (accesslist[clientip][0]) +": "+ str (Accesslist[clientip][3]) + "R" + str (CLIENTIP) + ":" + str (accesslist[clientip][4)) + "-W" + str (rsweighthalf) + "- "+ str (accesslist[clientip][5]) cmd ="/sbin/ipvsadm-e-T "+ str (accesslist[clientip][0)) +": "+ str (a CCESSLIST[CLIENTIP][3]) + "R" + str (CLIENTIP) + ":" + str (accesslist[clientip][4)) + "-W" + str (rsweighthalf) + "-"    
            + str (accesslist[clientip][5]) Os.system (CMD) # Setting new weight in list ACCESSLIST[CLIENTIP][2] = rsweighthalf else:print "Weight already changed.
    Doing nothing. " # Determine if WarninG threshold is reached elif float (LoadAvg1) >= float (loadavg_warn_threshold): print "Warning:load Average (1min) threshold (%s) reached:%s% (str (loadavg_warn_threshold), LoadAvg1) rsweightquart = Int (rsweightor iginal)-Int (rsweightoriginal)/4 # Only change weight if not already changed if not int (rsweightcurrent) = = Int (rsweightquart): print "changing realserver ' weight to 75% of original ' print ' Running  /SBIN/IPVSADM-E-T "+ str (accesslist[clientip][0]) +": "+ str (accesslist[clientip][3]) +"-r "+ str (CLIENTIP) +": "+ STR (accesslist[clientip][4]) + "-W" + str (rsweightquart) + "-" + str (accesslist[clientip][5)) cmd = "/ SBIN/IPVSADM-E-T "+ str (accesslist[clientip][0]) +": "+ str (accesslist[clientip][3]) +"-r "+ str (CLIENTIP) +": "+ STR (accesslist[clientip][4]) + "-W" + str (rsweightquart) + "-" + str (accesslist[clientip][5]) os.system (cmd ) # Setting new weight in list accesslist[clientip][2] = Rsweightquart Else:print "We Ight already changed.

    Doing nothing. " Else:print "Ok:load Average (1min) threshold normal:%s"% LOADAVG1 # Setting weight back to Origina  L value if not already set if not int (rsweightcurrent) = = Int (rsweightoriginal): print "Setting weight Back to original value of%s "% str (rsweightoriginal) print" running/sbin/ipvsadm-e-T "+ STR (accesslist[c  Lientip][0]) + ":" + str (accesslist[clientip][3]) + "R" + str (CLIENTIP) + ":" + str (ACCESSLIST[CLIENTIP][4) + "W" + STR (rsweightoriginal) cmd = "/sbin/ipvsadm-e-T" + str (accesslist[clientip][0)) + ":" + str (accesslist[clie NTIP][3]) + "-r" + str (CLIENTIP) + ":" + str (accesslist[clientip][4)) + "-W" + str (rsweightoriginal) os.sy Stem (cmd) accesslist[clientip][2] = rsweightoriginal return LOADAVG1 # Subclass simplexmlrpcserver to grab Client_address class Server (simplexmlrpcserver): def __init__ (Self,*args): Simplexmlrpcserver.__init__ (self, (args[0],args[1)) def server_bind (self): Self.socket.setsockop T (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1) simplexmlrpcserver.server_bind (self) def verify_request (Self,request, client_address) : print "\ n" clientip = client_address[0] If Accesslist.has_key (clientip): Print  Client (%s) in LVs table. '% clientip return 1 else:print ' client (%s) not in LVs table. " % clientip return 0 if __name__ = = "__main__": print "Starting up ..." server = Server (serverip,serv Erport) server.register_function (pushload_function, ' pushload ') server.logrequests = 0 Server.serve_fo Rever ()

client.py

#!/usr/bin/env python import xmlrpclib import Socket # Ip:port settings of Director Dir Ectorip = ' 172.17.0.100 ' directorport = ' 12219 ' # Connect to server s = xmlrpclib. Serverproxy ("http://" + Directorip + ":" + directorport) # fixme:implement timeout (else RPC request'll ha    
    ng indefinately) #socket. Setdefaulttimeout def readLine (filename): f = open (filename, "R") lines = F.readline () return lines loadavg = ReadLine ('/proc/loadavg ') # fugly Way To grab our IP. A better way would is to take we initial # Connect and get it from there.    
I don ' t know how (yet). From socket import socket, sock_dgram, af_inet ugly = socket (af_inet, Sock_dgram) ugly.connect ((Directorip, int (di Rectorport)) MyIP = Ugly.getsockname () [0] myload = s.pushload (loadavg,myip) print "Debug:p Ushed Current 1m load avg of "+ STR (myload)

This article comes from "Feng Yun, just her." "Blog, please be sure to keep this source http://rfyiamcool.blog.51cto.com/1030776/1141785

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.