Touch Genie Remote Log Module

Source: Internet
Author: User

First, the function

The Lua log method automatically discovers log servers under the same network segment

The Lua log method can proactively send logs to the server

LUA Client process Restart service side no impact

Second, the realization

The server is written using Python:

Start a thread, listen to a specific port with UDP, accept the client's scan, and the port of the feedback log

Starts a thread, listens on a specific port, accepts a TCP connection, dispatches a new log thread to process log

The client uses LUA to write:

Before printing log, use the UDP socket to scan the server's log port and create a TCP socket send log

Third, the source code:

Server:

  

Import Socketimport timeimport Reimport threadudp_listen_port = 31500tcp_listen_port = 31501stop_udp_server = Falsedef ge  Tlocalip (): MyName = Socket.getfqdn (Socket.gethostname ()) myaddr = Socket.gethostbyname (myname) return myaddr;def GetTime (): Return Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())) def Log (content): Print ("[%s]:%s" % (GetTime (), content)) def startudpserver (): Address = (Getlocalip (), udp_listen_port) s = Socket.socket (socket.af_i NET, Socket.    SOCK_DGRAM) S.bind (address) print (' Listen broadcast%s:%s '% address) Log (' Listen broadcast%s:%s '% address)             While not stop_udp_server:data, addr = S.recvfrom (2048) If not Data:log ("Client not existed!") Continue Log (' received%s from%s '% (data, addr)) Client_ip_port = ' {0}:{1} '. Format (addr[0],add R[1]) Pat = Re.compile ("^ (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+) $") for Ip,port in Pat.findall (client_ip_        Port):    Log (' Got iphone addr:port =%s%s '% (IP, port)) Local_addr_port = (Getlocalip (), Tcp_listen_port)            Log (' Rsp server Addr:port =%s%s '% local_addr_port) Rsp_data = ' {0} '. Format (local_addr_port[1])    S.sendto (rsp_data,addr) break; S.close () def starttcplogserver (sock,client): while (True): Try:data = SOCK.RECV (4096) if D Ata:pat = Re.compile (' \[(. *?)        \] ') for log in Pat.findall (data): Log (log) Else:break;    Except Socket.error:sock.close () Break Log (' Client (%s,%s) close '% client); Sock.close () def starttcplistenserver (): #create tcp Socket sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM) #bind local Ip-port server_addr = (Getlocalip (), Tcp_listen_port) Log (' Start TCP server on%s port% S '% server_addr) sock.bind (SERVER_ADDR) Sock.listen (5) #print recv data   while (True): Ss,addr = sock.accept () log (' Got log client from (%s,%s) '% addr) thread.start_new_th Read (Starttcplogserver, (ss,addr,)) Sock.close () if __name__ = = ' __main__ ': t_udp = Thread.start_new_thread (startUDPSe RVer, ()) t_tcp = Thread.start_new_thread (Starttcplistenserver, ()) while (True): Time.sleep (1)

Client:

 

Udp_listen_port =31500tcp_server_ip=""Tcp_server_port=0functionGetlocalip ()LocalSocket =require("Socket")  LocalIP, v =Socket.dns.toip (Socket.dns.gethostname ()) forKvinch Pairs(V.IP) Do      returnvEnd  return NilEndfunctionTryfindlogserver ()LocalIP =Getlocalip (); LocalIptable = {}  LocalSocket =require("Socket")  LocalUDP =socket.udp ()ifIP ~=Nil  Then     forIinch String.gmatch(IP,"%d+") Do      Print(i)Table.insert(iptable,i)End  End  LocalLength =table.getn (iptable)LocalFindip =Nil   LocalFindport =Nil  ifLength >0  Then     fori = -,255  Do       LocalDesip =String.Format("%s.%s.%s.%s", iptable[1], iptable[2], iptable[3],ToString(i))Print(DESIP) udp:setpeername (Desip, Udp_listen_port) udp:settimeout (0.5) Udp:send ("Hello") Data=udp:receive ()ifData ~=Nil  Then          Print(String.Format("Find Server%s:%s", Desip,data)) Tcp_server_ip=Desip Tcp_server_port=Tonumber(data)return true; End    End  End  return falseEndTCP=NilfunctionLog (content)--Body    ifTCP = =Nil  Then    LocalSocket =require("Socket") TCP=socket.tcp () tcp:connect (Tcp_server_ip,tcp_server_port)EndTcp:send (String.Format("[%s]",ToString(content)))EndifTryfindlogserver () Then     fori =1, -  DoLog ("Find Server")  End  End

Four, the way to load Luasocket in client Lua is as follows:

LocalSZ =require("sz")LocalHTTP =require("szocket.http")LocalRes, code = http.request ("http://www.baidu.com");ifCode = = $  ThenLog ("Start") Log (RES) dialog (res),0); Log ("End")End

Touch Genie Remote Log Module

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.