LoginSignup
1
1

More than 5 years have passed since last update.

GNU parallel で複数台のサーバ群から、手動でまとめてログ収集を並列処理する

Last updated at Posted at 2015-10-16

昨今は Logstash や fluentd で、ログサーバへ収集するのが流行りですが、
そんな流行りに逆行して、手動(scp と gnu parallel)でサーバ群からログをダウンロードするための手順です。

必要なコマンド

GNU parallel
scp
sshpass(初回接続のみ)
ssh-copy-id(初回接続のみ)

GNU parallel は、チュートリアルが大変参考になりますので、一度は目を通しておきましょう。

OSX に sshpass をインストール

brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

サーバ名一覧のテキストファイルを準備

echo -e "example01\nexample02\nexample03" > hosts.txt

こんな感じのファイルを用意

hosts.txt
example01
example02
example03

サーバへ公開鍵認証での接続設定

各サーバへの接続をパスワード入力したくないので、公開鍵認証にする。
初回は sshpass で通常のパスワード入力でログインして ssh-copy-id で、
各サーバへ公開鍵をコピーする

pararell --will-cite sshpass -p パスワード ssh-copy-id < hosts.txt

ログのダウンロードを実行する

重要なのが sudo su - などしなくても、ログファイルへのアクセス読み込み権限があることですが、sudo が必要な場合は、下記のように事前に ssh -tsudo -S でパスワードを直接コマンドにして、アクセス権限を外しておく

ssh -t user@example.com 'echo パスワード | sudo -S chmod o+r /var/log/httpd/access.log'

{} で hosts.txt の内容を展開してくれます。

parallel --will-cite mkdir -p logs/{} < hosts.txt
parallel --will-cite scp {}:/var/log/httpd/access.log logs/{}/ < hosts.txt

あとは grep -i 検索文字列 logs/*/access.log とかで使用します。

圧縮されたログなら

find ./ -name *.gz -exec gzip -d {} \; で、解凍できる

ssh 経由でログから grep コマンドを実行する

ログを手元に落とすのも手間がかかるので、ssh で直接 grep する場合は、下記のようにできる

parallel --will-cite "ssh {} 'grep -e hogehoge /var/log/httpd/access.log'" < hosts.txt
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1