LoginSignup
2

More than 5 years have passed since last update.

MacからNginxのログをS3に保存してAmazonAthenaで可視化する

Posted at

NginxのログをLTSV形式で出力

Nginxは既にインストール済みとします。
自分の場合は↓を参考にnginxのログをLTSV形式で出力するようにしました。

nginxのログをLTSV形式で出力して、fluentd経由でMongoDBに保存する

nginx.conf (抜粋)

http {
    include /usr/local/etc/nginx/mime.types;

    log_format ltsv 'time:$time_iso8601\t'
                'remote_addr:$remote_addr\t'
                'request_method:$request_method\t'
                'request_length:$request_length\t'
                'request_uri:$request_uri\t'
                'https:$https\t'
                'uri:$uri\t'
                'query_string:$query_string\t'
                'status:$status\t'
                'bytes_sent:$bytes_sent\t'
                'body_bytes_sent:$body_bytes_sent\t'
                'referer:$http_referer\t'
                'useragent:$http_user_agent\t'
                'forwardedfor:$http_x_forwarded_for\t'
                'request_time:$request_time\t'
                'upstream_response_time:$upstream_response_time';

    server {
      listen 80;
      access_log /usr/local/var/log/nginx/access.log ltsv;
      error_log /usr/local/var/log/nginx/error.log;

MacにFluentdをインストール

rubyのバージョンは2.4.1。
td-agentはインストーラ形式でも配布されていますが、Macのlaunchctlが慣れないのでgemでインストール。

$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
gem install fluentd --no-ri --no-rdoc
fluentd --setup ./fluent

Amazon S3 Output Plugin をインストール

fluent-gem install fluent-plugin-s3

fluent.conf

<source>
  @type tail
  format ltsv
  pos_file /usr/local/var/log/td-agent/access.log.pos
  path /usr/local/var/log/nginx/access.log
  tag nginx.access
</source>
<match nginx.access>
 @type copy
 <store>
   @type s3
   aws_key_id <S3へのアクセスキー>
   aws_sec_key <S3へのシークレットキー>
   s3_region ap-northeast-1
   s3_bucket yutoogi.logs

   s3_object_key_format %{path}/nginx/%{time_slice}_%{index}.%{file_extension}
   path logs
   time_slice_format %Y%m%d-%H%M%S
   buffer_path /usr/local/var/log/td-agent/buffer/nginx

   format json
   retry_wait 30s
   retry_limit 5
   flush_at_shutdown true
 </store>
 <store>
   @type stdout
 </store>
</match>

fluentd.confを設定後、td-agentを起動。
stdoutでも出力しているので

cd fluentd
fluentd -c ./fluent/fluent.conf -vv

flush_at_shutdownがtrueになっているので、flush_intervalの時間がくるか、fluentdのプロセスを停止するとS3へログが保存される。

S3_Management_Console.png

Amazon Athenaの設定

Athena.png

Athena 2.png

Athena 3.png

1カラムずつ手入力しても良いですが、数が多いので"Bulk add columns"から一括で登録します。
入力内容は以下の通りです。

remote_addr string, request_method string, request_length int, request_uri string, https string, uri string, query_string string, status int, bytes_sent int, body_bytes_sent int, referer string, useragent string, request_time string, upstream_response_time string

Athena 4.png

最終的には以下のようなSQLが実行されます。

CREATE EXTERNAL TABLE IF NOT EXISTS yutoogi.nginx_access (
  `remote_addr` string,
  `request_method` string,
  `request_length` int,
  `request_uri` string,
  `https` string,
  `uri` string,
  `query_string` string,
  `status` int,
  `bytes_sent` int,
  `body_bytes_sent` int,
  `referer` string,
  `useragent` string,
  `request_time` string,
  `upstream_response_time` string
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES (
  'serialization.format' = '1'
) LOCATION 's3://yutoogi.logs/logs/nginx/'
TBLPROPERTIES ('has_encrypted_data'='false')

テーブルの作成が完了しました。
あとは中央にあるエディタでSQLを記述していけばOKですね。

Athena 6.png

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
2