Created
April 28, 2012 08:55
-
-
Save mikeda/2517305 to your computer and use it in GitHub Desktop.
inotifyで指定ディレクトリ下の操作を監視するfluentdプラグイン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#指定ディレクトリ下のファイル操作を監視する | |
# | |
#config | |
#<source> | |
# type inotify | |
# tag inotify.log | |
# dir /tmp/inotify_test_d/ | |
#</source> | |
# | |
#emit | |
#{"name":"/tmp/inotify_test_d/dir7","mask":1073742080} | |
# | |
# TODO:対象フラグを設定できるように | |
# TODO:監視対象のフィルタリングを設定できるように | |
# TODO:設定ミス時の例外処理 | |
# TODO:emit項目増やす | |
class Inotify < Fluent::Input | |
Fluent::Plugin.register_input('inotify', self) | |
def initialize | |
super | |
require 'inotify' | |
require 'find' | |
@flag = Inotify::CREATE | Inotify::DELETE | Inotify::MOVE | Inotify::MODIFY | |
@wds = {} | |
@i = Inotify.new | |
@t = Thread.new do | |
@i.each_event do |ev| | |
time = Time.now.to_i | |
record = { | |
"name" => fullname(ev), | |
"mask" => ev.mask | |
} | |
Fluent::Engine.emit(@tag, time, record) | |
if ev.mask & Inotify::CREATE != 0 && ev.mask & Inotify::ISDIR != 0 | |
dir = fullname(ev) | |
add_watch_dir(dir) | |
end | |
end | |
end | |
end | |
def configure(conf) | |
super | |
@tag = conf['tag'] | |
@dir = conf['dir'] | |
# @flag = conf['flag'] | |
end | |
def fullname(ev) | |
File.join(@wds[ev.wd], ev.name) | |
end | |
def add_watch_dir(dir) | |
$log.debug "### inotify add: #{@dir} #{@flag}" | |
wd = @i.add_watch(dir, @flag) | |
@wds[wd] = dir | |
end | |
def start | |
super | |
Find.find(@dir) do |e| | |
if !File.directory? e | |
Find.prune | |
else | |
begin | |
add_watch_dir(e) | |
rescue | |
$log.fatal "inotify skipping add: #{e}: #{$!}" | |
end | |
end | |
end | |
@t.join | |
end | |
def shutdown | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment