LoginSignup
2
0

More than 1 year has passed since last update.

サクッとApacheのerrorlogをJSON化してBigQueryにLoadします

Last updated at Posted at 2021-12-21

このエントリーは、GMOアドマーケティング Advent Calendar 2021【12/22】 の記事です。

Overview

apacheのerrorlogを簡単にBig QueryにUploadする方法になります。

Reference

ErrorLogFormat
errorlogのformat変更はapache2.4から対応しています

Environment

apache2.4

Sample Format

ErrorLogFormat "{\"message\":\"%M\",\"error_time\":\"%{cu}t\",\"loglevel\":\"%-m:%l\",\"serverip\":\"%A\",\"host\":\"%a\",\"referer\":\"%-{Referer}i\""}"

Sample Log Export

{"message":"error message","error_time":"2021-12-25 12:11:05.763873","loglevel":"php7:notice","serverip":"192.168.1.121:80","host":"xx.xxx.xxx.xxx:40812","referer":"http://hogehoge.jp/","log_type":"apache_error"}

Schema

[
  {
    "name": "error_time",
    "type": "TIMESTAMP",
    "mode": "REQUIRED"
  },
  {
    "description": "ログレベル",
    "name": "loglevel",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "serverip",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "host",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "message",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "referer",
    "type": "STRING",
    "mode": "NULLABLE"
  }
]

Big Query Upload Shell

#!/bin/bash

/bin/echo "S: `date +'%Y-%m-%d %H:%M:%S'`"

v_year="2021"
v_month="12"
v_day="25"
v_ymd="${v_year}${v_month}${v_day}"

v_gcp_project="GCPPROJECT"
v_gcp_bq_dataset="file"
v_gcspath="gs://DATASET/file/2021/12/25"
v_table="error"

v_errorfilename=error_log.${v_ymd}
v_errorfilepath=/var/log/httpd/${v_errorfilename}

#Compression
if [ ! -f ${v_errorfilepath}.gz ]; then
  nice -n 19 grep -v "^{-" ${v_errorfilepath} > ${v_errorfilepath}_tmp && \
  nice -n 19 sed -i -e "s/'/\"/g" ${v_errorfilepath}_tmp && \
  nice -n 19 gzip --fast -c ${v_errorfilepath}_tmp > ${v_errorfilepath}.gz && \
  nice -n 19 rm -f ${v_errorfilepath}_tmp
fi

#GCS Copy
trickle -s -u 10000 gsutil cp ${v_errorfilepath}.gz ${v_gcspath}/

#Big Query Load
bq load \
--project_id=${v_gcp_project} \
--dataset_id=${v_gcp_bq_dataset} \
--ignore_unknown_values \
--max_bad_records=100 \
--schema=/path/schema/${v_table}.json \
--source_format=NEWLINE_DELIMITED_JSON \
${v_table}_${v_ymd} \
${v_gcspath}/${v_errorfilename}.gz

/bin/echo "E: `date +'%Y-%m-%d %H:%M:%S'`"

Result

スクリーンショット 2021-12-06 12.28.43.png

次回のAdvent Calendar 2021

明日は、【@watashinohakumai】さんの【AWS SAPを取得したら視野が広がった話】についてのお話です。
お楽しみに。

クリスマスまで続くGMOアドマーケティング Advent Calendar 2021
ぜひ今後も投稿をウォッチしてください!

2
0
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
2
0