2012年12月12日水曜日

ディレクトリの監視!

どもども。内山です。
今日はレーシングバイクのすばらしさについ書きます

ラッキーストライクカラーのバイクとライダーは、
SUZUKI RGV-Γ / ケビン・シュワンツ
という、1988年から1995年までの8シーズンでGP通算25勝を記録した伝説的なライダーです。
そして、青いバイクは、ケビン・シュワンツに憧れた内山という小僧が乗っている、RGV250Γという、RGV-Γのレプリカバイクです。
マルボロカラーのバイクは、
YAMAHA YZR500 / ウェイン・レイニー
 ロスマンズカラーのバイクは
HONDA NSR500 / ワイン・ガードナー  です。とにかくかっこいいですね!!!

はい、本題に入ります。
Linuxでのディレクトリ、ファイルの監視 についてです。
筆者はCentOS6.2及び6.3を使用してます。

キーワードは、「inotify」と、「fsniper」!


この2つの説明を見てみると、
  • inotify API はファイルシステムイベントを監視するための機構を提供する。
  • fsniperを使用すると、監視対象のディレクトリを指定し、
    それらのディレクトリで新しいファイルが作成されたときにスクリプトを実行することができる。
とあります。
inotifyは、ファイルやディレクトリに変化があると、
どんな操作があったのかを教えてくれるみたいですね。
一方、fsniperはinotifyを利用し、変化があるとスクリプトを実行してくれるみたいですね。
まずはinotify-toolsをインストールしてみます。 
# yum install inotify-tools      (RedHat 系の場合)
# apt-get install inotify-tools  (Debian 系の場合)
はい。インストール完了です。
inotifywait と inotifywatch というコマンドが使えるようになります。
inotifywait は、指定したファイルやディレクトリに変化があると、 その旨出力してくれるコマンドらしいです。
inotifywatch は、指定したファイルやディレクトリに対する操作内容をまとめ、 統計結果を出力してくれるコマンドらしいです。
えーっと、waitさんは何か起きたらすぐに教えてくれる人で、
watchさんは終了の合図があるまで記録をとり続ける人みたいですね。はい。
まずはhogetestディレクトリを作り、inotifywaitで監視させてみます。
引数に-mを指定することで、一度で終了せずに監視を続けてくれます。 
# mkdir /hogetest
# inotifywait -m /hogetest
Setting up watches.  
Watches established.
フォアグラウンドで動くので、別の端末からhogetestにtest.txtを作ってみます。
# touch /hogetest/test.txt
すると、inotifywaitを実行中の端末に 
/testdir/ CREATE test.txt
/testdir/ OPEN test.txt
/testdir/ ATTRIB test.txt
/testdir/ CLOSE_WRITE,CLOSE test.txt
と表示されました! 
touchコマンドだけでも、ファイルを作成し、ファイルを開いて属性を変更していることがわかりますね。
このように、inotifywaitコマンドは何か操作が行われた際のイベント情報を出力してくれます。
起動スクリプトを書いて、重要なディレクトリを監視させてlogファイルに記録を残すようにすれば、 トラブル時には役にたつかもしれませんね。
この辺でinotifyさんは終わりにします。
 
次に、fsniperです。
yumで拾ってこれないので、この辺からtar.gzファイルをダウンロードして、
いつも通りにインストールします。
# tar -xzf fsniper-1.3.1.tar.gz 解凍
# cd fsniper
# ./configure
...
checking for a BSD-compatible install... /usr/bin/install -c
checking for magic_open in -lmagic... no
configure: error: libmagic not found
はい。なんか怒られますね。libmagicがねーよ!って怒られているのでインストールします。
# yum install file-devel      (RedHat 系の場合)
# apt-get install libmagic-dev  (Debian 系の場合)
今度こそconfigureするぞー(棒
# ./configure
...
checking for magic_open in -lmagic... yes
checking for pcre_version in -lpcre... no
configure: error: libpcre not found
(^ω^#)ピキピキ
# yum install pcre-devel
気を取り直して、
# make
# make install
たぶんインストールできましたね。
--version でバージョンを確認できます
今、実行しても、設定ファイルがみつからないよ!と怒られます。
# fsniper --help
        --help
                Prints this help text.
        --version
                Prints version information.
        --daemon
                Run as a daemon.
        --verbose
                Turns on debug text.
        --sync
                Sync mode (for debugging).
        --log-to-stdout
                Log to stdout alongside the usual log file.
# fsniper --version
fsniper v1.3.1
# fsniper --verbose
error: could not open config file: /root/.config/fsniper/config
というわけで、configファイルを作ります。 
watch {
 /hogetest {
  */* {
   handler = cp %% /home/hoge/Desktop/
  }
 }
}
上記の設定は、先ほど作成したhogetestディレクトリに何かファイルが作られたら
それをhogeさんのデスクトップにコピーするように書きました。 
fsniper --daemon で走らせてみます。 
# fsniper --daemon
#touch //hogetest/test2.txt
#ls //home/hoge/Desktop/
test.txt test2.txt
 
こんな感じでディレクトリやファイル監視して、
任意のスクリプトを実行させることができました。
アップローダーにファイルがアップロードされたら、
ファイル一覧を転送したりと、いろいろ活用できそうですね! 
 
凄いね!やったね!
おしまい。まる。 

4 件のコメント:

  1. The aspiration of every student is to acheive top grades in college. This acheivement stands beyond a plethora of challeneges, of which exams are the biggest. While most of the students spend hours of time and tons of elbow grease to cram down entire textbooks, smart students use test banks online to breeze through their exams. Find awesome learning aids including Prehospital Emergency Care 11th Edition Test Bank today and ace your class.

    返信削除
  2. Great Platform. Are you also searching for nursing essay writers ? we are the best solution for you. We are best known for offering the best nursing essay writing services

    返信削除
  3. I want to always read your blogs. I love them Are you also searching for Nursing case study writing services? we are the best solution for you. We are best known for delivering Nursing case study writing services to students without having to break the bank

    返信削除
  4. Here you will find all the related information about Wendy’s lunch hours, updated Wendy’s lunch menu and their prices, Hours of operations, and much more in this post.

    Visit My Post: Wendy’s Lunch Hours

    返信削除