Amazon S3 ã使ã£ããã°ä¿åã®ä»æ¹
å¤åãLinuxã®ã³ãã³ãã«æ £ãã¦ãªãã¨ä¸æãåºæ¥ãªãã¨æãã®ã§ãæ¸ãã¦ããã¾ãã
Log rotation
ããã«ã¤ãã¦ã¯ããã¾ãæ¸ããã¨ããªãã§ããlogrotate ã§æ¤ç´¢ãã¦ã¿ã¦ä¸ãã
ã¾ããLog rotationã¯ããã°ãã¡ã¤ã«ããï¼é±éãªããï¼ã¶æãªãã§ããã¼ãã¼ã·ã§ã³ããããã®ã§ãã
ãããã¡ã¤ã«access.logããã£ãã¨ããããï¼é±éã§access.log ãä¸æ¦åé¤ãããã¯ã¢ãã(ãªããã¯ä¸¡æ¹)ããaccess.log ã«ã¯ã¾ãæ°ããªãã°ãæ¸ãè¾¼ã¾ãã¦ããã¾ãã
ãªããããããã¨ããã¨ãaccess.log ã¯ç¡éã«å¤§ãããªãããã¡ã¤ã«æä½ããä¸ã§é害ã«ãªã£ãããããã¨ã¨ã
AWSã«ããã¦ã¯ãEC2ã«å·¨å¤§ãªãã°ãä¿ç®¡ãããããS3ã«å·¨å¤§ãªãã°ãä¿ç®¡ããã®ããéç¨ä¸æ£ããããã§ã(EC2ã¯æ¶ããåæã®ãã¼ã¿ã§ãå¤æ®µçã«ãEC2ã¯å®ããEC2ã§ãªãã¨ãããã ã¹ãã¬ã¼ã¸ã«ç¹åããS3ã®ãããªãã®ã«ã大ããªãã¼ã¿ã横æµãããã®ãè¯ãã¨æã)ã
ã¨ããããã¨ã§é©å½ãªã¿ã¤ãã³ã°ã§log ãrotate ãããè¨å®ããã¦ãã ããã
ï¼æ¥ï¼ã¤ããããããããããªããã¨æãã¾ãããEC2ã®ã¤ã³ã¹ã¿ã³ã¹ãä¸åè½ã¡ãã¨ãããã°ãæ¶ãã¦ãæ³£ããªãã¬ãã«ãã®é »åº¦ã§ãªãã¨æ³£ãã¡ããã®ã§ã
ã¡ãªã¿ã«1000ãªã¯ã¨ã¹ãã§1åã¨ãã®ã¬ãã«ãªã®ã§ããã®ããããæ°ã«ããªãã§è¯ãã¨æãã¾ãã
S3 ã使ã
ç´°ãããã¨ã¯ã¯ã¶ãã¾ãããhttp://timkay.com/aws/ã«ã¦å
¬éããã¦ããã¹ã¯ãªããã使ãã¾ã(Amazonããããªã³ã¯ããã¦ã¾ã)ã
ãã®ãµã¤ãã«æ¸ãã¦ããè¨å®(1ã5ã£ã¦æ¸ãã¦ããã¨ã)ãå®äºããã
s3mkdir BUCKET_NAME
ãå®è¡ããã¨ãs3ä¸ã«bucketãä¸åçæããã¾ãã1ãµã¼ãã¹ãã¨ã«Bucketãä½ãã®ãããã®ã§ã¯ãªããã¨ã
S3ã¸ã®ããã¯ã¢ãã
Log rotateããã¿ã¤ãã³ã°ã§ãS3ã«ããã¯ã¢ãããåãããã§ãããããã¦ãEC2ä¸ã«ã¯ãã°ãã¼ã¿ãæ®ããªãããã«ãããããã§ãã
捨てられるログを外部保管させるãåèã«ãã¦ã¾ãã
logrotateãããåã«ããã¹ã¯ãªãããå®è¡ããããã§ããã以ä¸ã®ã¹ã¯ãªããã使ãã¾ããsave_to_s3.shçãé©å½ãªãã¡ã¤ã«åã§ä¿åãã¦ä¸ããã
ã·ã§ã«ã¹ã¯ãªããæãã§ãçå群ãªã®ã§ãã«ã¼ãã¨ãè¯ãæãã«æ¸ãæãã¦ãããã¨å¬ããã®ã§ããâ¦ã
#!/bin/env bash DIR_NAME=$1 # ex: rails, apache, nginx LOG_PATH=$2 # ex: /path/to/log/file.log LOG_NAME=`echo $2 | sed -e "s/\//\n/g" | tail -n 1` # ex: file.log DATE=`date -d '9 hours' +%F` # ex: 2012-08-30 AMI_ID=`wget -q -O - http://169.254.169.254/latest/meta-data/ami-id` #ami-a1b2c3 s3put BUCKET_NAME/${DIR_NAME}/${LOG_NAME}/${DATE}-${AMI_ID}-${LOG_NAME} ${LOG_PATH}
BUCKET_NAMEã¯é©å½ã«æ¸ãæãã¦ä¸ãããããã§ãä¸ã§ä½æããS3ã®Bucketå
ã«ã
rails/production.log/2012-08-30-ami****-production.logã¨ãããã¡ã¤ã«ãä½æããã¾ã(ä¾)ã
ãã¦ãä»åº¦ã¯log rotationã®è¨å®ãã¡ã¤ã«(ä¾ï¼/etc/logrotate.d/httpd.conf)ãéãã¾ããããã¦ãã¯root権éã§éããã»ããè¯ãã§ãã
ãã®è¨å®ãã¡ã¤ã«ã«ãprerotateã以ä¸ã®ããã«æ¸ãã¾ãã
/var/log/httpd/*log { (ä¸ç¥) postrotate /path/to/save_to_s3.sh apache /var/log/httpd/access_log /path/to/save_to_s3.sh apache /var/log/httpd/error_log endscript }
åãã¡ã¤ã«ãã¹ãªã©ã¯ãé©å®æ¸ãæãã¦ä¸ããã