Skip to content

Instantly share code, notes, and snippets.

@garlandkr
Created August 8, 2013 14:52
Show Gist options
  • Save garlandkr/6185278 to your computer and use it in GitHub Desktop.
Save garlandkr/6185278 to your computer and use it in GitHub Desktop.
Using inotify to monitor system file changed in a directory and take action.
#!/usr/bin/env python
import re
import os
import logging
import argparse
import pyinotify
import subprocess
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--directory", dest="dirname", required=True,
help="Give the directory to monitor, ex: /tmp")
return parser.parse_args()
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CLOSE_WRITE(self, event):
try:
p = re.compile('.*\.tmp$')
if not p.search(event.pathname):
logging.info("Opta write event: %s" % event.pathname)
{%- for hostname in pillar['opta_remote_hosts'] %}
cmd = ['/usr/bin/rsync', '-tdz', '--delete-after', '/home/opta/stats/', '{{ hostname }}:/home/opta/stats/']
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_value, stderr_value = p.communicate()
if stderr_value:
logging.error(stderr_value)
elif stdout_value:
logging.info(stdout_value)
{%- endfor %}
except sh.ErrorReturnCode_12, err:
logging.debug(err)
except Exception, err:
logging.debug(err)
def process_IN_DELETE(self, event):
try:
p = re.compile('.*\.tmp$')
if not p.search(event.pathname):
logging.info("Opta delete event: %s" % event.pathname)
{%- for hostname in pillar['opta_remote_hosts'] %}
cmd = ['/usr/bin/rsync', '-tdz', '--delete-after', '/home/opta/stats/', '{{ hostname }}:/home/opta/stats/']
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_value, stderr_value = p.communicate()
if stderr_value:
logging.error(stderr_value)
elif stdout_value:
logging.info(stdout_value)
{%- endfor %}
except sh.ErrorReturnCode_12, err:
logging.debug(err)
except Exception, err:
logging.debug(err)
def run():
logging.basicConfig(filename='/var/log/optawatch.log',level=logging.DEBUG)
args = get_args()
pid_file = "/tmp/optawatch.pid"
try:
open(pid_file, 'r')
os.remove(pid_file)
except IOError:
if args.dirname:
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE # watched events
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(args.dirname, mask, rec=True)
try:
notifier.loop(daemonize=True,
pid_file=pid_file, stdout='/tmp/stdout.txt')
except pyinotify.NotifierError, err:
logging.debug(err)
else:
logging.warning("You must specify an existing directory to monitor.")
except Exception, err:
logging.debug("Unexplained error - %s" % err)
if __name__ == "__main__":
run()
@garlandkr
Copy link
Author

This process is no longer being used as rsync was taking up too many resources. This started out as a 1:1 copy of an existing script in perl. The next incarnation uses paraminko, the ssh module, to directly handle the transfers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment