こんにちは、中の人です。
Tableauは非常に強力なBIツールでエリア情報を地図にマッピングしてくれる機能などもあります。
IPアドレスのままではTableauに表示することが出来ないので、Amazon KinesisでIPアドレスを緯度経度に変換して、Redshiftに取り込んでTableauで表示させます。
まず、Kinesisのセットアップですが以下を参考にStreamを立ち上げます。
Kinesis編~Kinesisを試してみる①~ /archives/5629
セットアップが完了したら、アクセスログをKinesisにPutするスクリプトを作成します。
putrecords.pyというスクリプトをpythonで作成します。botoが必要となりますので、未インストールの場合は以下のいずれかの方法でインストールして下さい。
1sudo easy_install boto1sudo pip install botoputrecords.pyの作成
1$ vim putrecords.py23#!/usr/bin/python45# -*- coding: utf-8 -*-67import boto.kinesis,datetime,time8import threading, Queue, subprocess, sys9tailq = Queue.Queue(maxsize=10)1011def tail_forever(fn):12 p = subprocess.Popen(["tail", "-f", fn], stdout=subprocess.PIPE)13 while 1:14 line = p.stdout.readline()15 tailq.put(line)16 if not line:17 break1819def main():20 fn = sys.argv[1]21 threading.Thread(target=tail_forever, args=(fn,)).start()2223 connection = boto.kinesis.connect_to_region('us-east-1')24 stream_name = 'demo-knowcom'25 partition_key = 'partition_1'2627 while True:28 print connection.put_record(stream_name,tailq.get(),partition_key)2930if __name__ == '__main__':31 main()では、早速アクセスログをkinesisにputしてみましょう
1$ sudo python putrecords.py /var/log/httpd/access_log23{u'ShardId': u'shardId-000000000000', u'SequenceNumber': u'49539048558044286732641341533280647198527365833732128769'}4{u'ShardId': u'shardId-000000000000', u'SequenceNumber': u'49539048558044286732641341502046860148002378622592614401'}5{u'ShardId': u'shardId-000000000000', u'SequenceNumber': u'49539048558044286732641341575579715712851433767774978049'}6{u'ShardId': u'shardId-000000000000', u'SequenceNumber': u'49539048558044286732641343882624362641073213350684393473'}上記のようなログが出れば確認完了です。
プログラムのprint部分を変更することで、表示を止めることも可能です。
1- print connection.put_record(stream_name,tailq.get(),partition_key)2+ connection.put_record(stream_name,tailq.get(),partition_key)1$ sudo python putrecords.py /var/log/httpd/access_log &でバックグラウンド実行させます。
次回はKinesisにputしたデータをgetして、IPアドレスを緯度経度情報に変換します。
お楽しみに!!