Python to emulate remote tail -f?

前端 未结 6 1657
攒了一身酷
攒了一身酷 2021-01-04 20:28

We have several application servers, and a central monitoring server.

We are currently running ssh with \"tail -f\" from the monitoring server to stream several text

6条回答
  •  无人及你
    2021-01-04 20:46

    I think the screen idea is the best idea, but if you're not wanting to ssh and you want a python script to do it. Here is a simple pythonic XMLRPC way of getting the info. It will only update when something has been appended to the file in question.

    This is the client file. You tell this which file you want to read from and what computer its on.

    #!/usr/bin/python
    # This should be run on the computer you want to output the files
    # You must pass a filename and a location
    # filename must be the full path from the root directory, or relative path
    # from the directory the server is running
    # location must be in the form of http://location:port (i.e. http:localhost:8000)
    
    import xmlrpclib, time, sys, os
    
    def tail(filename, location):
       # connect to server
       s = xmlrpclib.ServerProxy(location)
    
       # get starting length of file
       curSeek = s.GetSize(filename)
    
       # constantly check
       while 1:
          time.sleep(1) # make sure to sleep
    
          # get a new length of file and check for changes
          prevSeek = curSeek
    
          # some times it fails if the file is being writter to,
          # we'll wait another second for it to finish
          try:
             curSeek = s.GetSize(filename)
          except:
             pass
    
          # if file length has changed print it
          if prevSeek != curSeek:
             print s.tail(filename, prevSeek),
    
    
    def main():
       # check that we got a file passed to us
       if len(sys.argv) != 3 or not os.path.isfile(sys.argv[1]):
          print 'Must give a valid filename.'
          return
    
       # run tail function
       tail(sys.argv[1], sys.argv[2])
    
    main()
    

    This is the server you will run this on each computer that has a file you want to look at. Its nothing fancy. You can daemonize it if you want. You just run it, and you client should connect to it if you tell the client where it is and you have the right ports open.

    #!/usr/bin/python
    # This runs on the computer(s) you want to read the file from
    # Make sure to change out the HOST and PORT variables
    HOST = 'localhost'
    PORT = 8000
    
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
    
    import time, os
    
    def GetSize(filename):
       # get file size
       return os.stat(filename)[6]
    
    def tail(filename, seek):
       #Set the filename and open the file
       f = open(filename,'r')
    
       #Find the size of the file and move to the end
       f.seek(seek)
       return f.read()
    
    def CreateServer():
       # Create server
       server = SimpleXMLRPCServer((HOST, PORT),
                                   requestHandler=SimpleXMLRPCRequestHandler)
    
    # register functions
       server.register_function(tail, 'tail')
       server.register_function(GetSize, 'GetSize')
    
       # Run the server's main loop
       server.serve_forever()
    
    # start server
    CreateServer()
    

    Ideally you run the server once, then from the client run "python client.py sample.log http://somehost:8000" and it should start going. Hope that helps.

提交回复
热议问题