2017-09-13 18 views
-1

リモートディレクトリとファイルを監視しようとしています。私はファイルやディレクトリの変更、つまり(アクセス、書き込み、オープン、クローズのイベント)を保存またはログに記録する必要があります。リモートディレクトリとファイルの変更を監視する方法は?

これらのイベントの監視と記録にpyinotifyを使用してみました。私はローカルシステムファイルのためにそれを達成しましたが、私の問題はリモートファイルとディレクトリを監視する方法です。

私はsshの助けを借りてこれを達成することができますか、または他の方法でリモートファイルやディレクトリにイベントが記録される可能性はありますか?

ローカルシステムファイルの監視用に私のコードを与えました。

import pyinotify 
import asyncore 
from .models import AccessEvents 
import threading 

class MyEventHandler(pyinotify.ProcessEvent): 
    def process_IN_ACCESS(self, event): 
     access=AccessEvents(mode_id=1,path=event.pathname) 
     access.save() 
    def process_IN_ATTRIB(self, event): 
     attrib = AccessEvents(mode_id=2, path=event.pathname) 
     attrib.save() 
    def process_IN_CLOSE_NOWRITE(self, event): 
     nwrite = AccessEvents(mode_id=3, path=event.pathname) 
     nwrite.save() 
    def process_IN_CLOSE_WRITE(self, event): 
     write = AccessEvents(mode_id=4, path=event.pathname) 
     write.save() 
    def process_IN_CREATE(self, event): 
     create = AccessEvents(mode_id=5, path=event.pathname) 
     create.save() 
    def process_IN_DELETE(self, event): 
     delete = AccessEvents(mode_id=6, path=event.pathname) 
     delete.save() 
    def process_IN_MODIFY(self, event): 
     modify = AccessEvents(mode_id=7, path=event.pathname) 
     modify.save() 
    def process_IN_OPEN(self, event): 
     open = AccessEvents(mode_id=8, path=event.pathname) 
     open.save() 

def startmonitor(file_or_dir): 
    # watch manager 
    wm = pyinotify.WatchManager() 
    try: 
     test=wm.add_watch(file_or_dir, pyinotify.ALL_EVENTS, rec=True) 
     if test[file_or_dir]==-1: 
      return 'no_such_file_or_dir' 
     else: 
      # event handler 
      eh = MyEventHandler() 
      # notifier 
      notifier = pyinotify.AsyncNotifier(wm, eh) 
      thread = threading.Thread(target=asyncore.loop(), args=()) 
      thread.daemon = True # Daemonize thread 
      thread.start() # Start the execution 
      return 'file_monitoring_started' 
    except Exception as e: 
     print 'error',e 

startmonitor('/tmp/test') 

リモートシステムファイルの監視について知っている人は、私にあなたの提案をしてください。前もって感謝します!!!

答えて

1

単純なクライアント - サーバーモデル(http)で行うことができます。

最初の手順では、監視するリモートシステム上でファイルウォッチャーコードを実行する必要があります。構造化された形式で変更を保存します。たとえば、次のようなものがあります。 -

class ChangeEvent: 

def __init__(self, event_name) 

def files_changed(self, list_files) 

これらのChangeEventsのリストをキューとして(バッファーとして)格納します。クライアントが変更イベントのリストを取得できるように、単純なGET APIを作成します。あなたが送信したキューからChangeEventsを削除します。

クライアントサイドのアプリ(モバイルや多分、問題ではないかもしれません)では、上記のように定期的にapiを押して変更を取得してください。

これらのChangeEventをjsonまたはcsvとしてリモートサーバーに保存することもできます。

関連する問題