2016-05-02 8 views
0

問題文:Python:リモートサーバで複数のgzipファイルを読み込んで処理する

リモートサーバに複数の(1000+)* .gzファイルがあります。私はこれらのファイルを読んで、特定の文字列をチェックする必要があります。文字列が一致する場合は、ファイル名を返す必要があります。私は次のコードを試しました。次のプログラムは動作していますが、膨大なIOが含まれているので効率的ではありません。これを行うための効率的な方法をお勧めしますか?

マイコード:

import gzip 
import os 
import paramiko 
import multiprocessing 
from bisect import insort 
synchObj=multiprocessing.Manager() 
hostname = '192.168.1.2' 
port = 22 
username='may' 
password='Apa$sW0rd' 

def miniAnalyze(): 
    ifile_list=synchObj.list([]) # A synchronized list to Store the File names containing the matched String. 

    def analyze_the_file(file_single): 
     strings = ("error 72","error 81",) # Hard Coded the Strings that needs to be searched. 
     try: 
      ssh=paramiko.SSHClient() 
      #Code to FTP the file to local system from the remote machine. 
      ..... 
      ........ 
      path_f='/home/user/may/'+filename 

      #Read the Gzip file in local system after FTP is done 

      with gzip.open(path_f, 'rb') as f: 
      contents = f.read() 
      if any(s in contents for s in strings): 
       print "File " + str(path_f) + " is a hit." 
       insort(ifile_list, filename) # Push the file into the list if there is a match. 
       os.remove(path_f) 
      else: 
       os.remove(path_f) 
     except Exception, ae: 
      print "Error while Analyzing file "+ str(ae) 

     finally: 
      if ifile_list: 
      print "The Error is at "+ ifile_list 
      ftp.close() 
      ssh.close() 


    def assign_to_proc(): 
     # Code to glob files matching a pattern and pass to another function via multiprocess . 
     apath = '/home/remotemachine/log/' 
     apattern = '"*.gz"' 
     first_command = 'find {path} -name {pattern}' 
     command = first_command.format(path=apath, pattern=apattern) 

     try: 
      ssh=paramiko.SSHClient() 
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
      ssh.connect(hostname,username=username,password=password) 
      stdin, stdout, stderr = ssh.exec_command(command) 
      while not stdout.channel.exit_status_ready(): 
       time.sleep(2) 
      filelist = stdout.read().splitlines() 

      jobs = [] 

      for ifle in filelist: 
       p = multiprocessing.Process(target=analyze_the_file,args=(ifle,)) 
       jobs.append(p) 
       p.start() 

      for job in jobs: 
       job.join() 


     except Exception, fe: 
      print "Error while getting file names "+ str(fe) 

     finally: 
      ssh.close() 


if __name__ == '__main__': 
    miniAnalyze() 

上記のコードは遅いです。 GZファイルをローカルシステムに取得する際には、多くのIOがあります。親切にも、より良い方法を見つけるのを手伝ってください。

答えて

0

zgrepなどのリモートOSコマンドを実行し、コマンド結果をローカルで処理します。この方法では、ローカルマシン上のファイルの内容全体を転送する必要はありません。

関連する問題