LoginSignup
19
22

More than 5 years have passed since last update.

CloudWatch Logsのインストール・設定

Posted at

CloudWatch logsでやれること

  • EC2の任意のログをリアルタイムで取得することができる
  • サーバにログインする必要がなく、AWSのコンソール画面でログを確認することができる
  • 取得したログの保持期間は、最短1日から失効なしまで選ぶことができる
  • ログの出力された期間を指定し、エクスポートすることができる

インストール前提

  • EC2へログインしていること
  • rootに切り替えていること
  • IAMで以下の権限付与されていること
      ・CloudWatch logsFulllAccess
  • EC2にアクセスキーとシークレットキーが登録されていること
  • 今回は、 /var/log/httpd/access_log を取得する

インストール手順

1. インストールコマンドの実行

インストールスクリプトのダウンロード
# curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O

パーミッションの変更
# chmod +x ./awslogs-agent-setup.py

インストールスクリプトの実行 ★S3を使用しないときは、下記(■設定内容のセットアップ)を参照
# ./awslogs-agent-setup.py -n -r ap-northeast-1 -c s3://使用するS3のBucket名/ 
■設定内容のセットアップ
# python ./awslogs-agent-setup.py --region ap-northeast-1 --only-generate-config

Launching interactive setup of CloudWatch Logs agent ...
Skipping downloading and installation of agent bits.Step 3 of 5: Configuring AWS CLI ...
AWS Access Key ID [****************MAJQ]: <シークレットキー> ★エンター
AWS Secret Access Key [****************fPn1]: <シークレットアクセスキー> ★エンター
Default region name [ap-northeast-1]: <リージョン> ★エンター
Default output format [json]: <出力形式(基本jsonでOK)> ★エンター

Step 4 of 5: Configuring the CloudWatch Logs Agent ...
Path of log file to upload [/var/log/messages]: <取得したいログのパス> ★エンター
Destination Log Group name [/var/log/messages]: <AWSコンソールで表示させる名前> ★エンター

Choose Log Stream name:
  1. Use EC2 instance id.
  2. Use hostname.
  3. Custom.
Enter choice [1]: <取得したサーバーの情報を何で表示させるか> ★エンター

Choose Log Event timestamp format:
  1. %b %d %H:%M:%S    (Dec 31 23:59:59)
  2. %d/%b/%Y:%H:%M:%S (10/Oct/2000:13:55:36)
  3. %Y-%m-%d %H:%M:%S (2008-09-08 11:52:54)
  4. Custom
Enter choice [1]: <ログのタイムスタンプを選択> ★エンター

Choose initial position of upload:
  1. From start of file.
  2. From end of file.
Enter choice [1]: ★エンター
More log files to configure? [Y]:  <複数のログを取得するなら[Y]、終了するなら[N]>

Step 5 of 5: Setting up agent as a daemon ...DONE


------------------------------------------------------
- Configuration file successfully saved at: /var/awslogs/etc/awslogs.conf
- You can begin accessing new log events after a few moments at https://console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#logs:
- You can use 'sudo service awslogs start|stop|status|restart' to control the daemon.
- To see diagnostic information for the CloudWatch Logs Agent, see /var/log/awslogs.log
- You can rerun interactive setup using 'sudo python ./awslogs-agent-setup.py --region ap-northeast-1 --only-generate-config'
------------------------------------------------------


2.起動確認コマンドの実行

# systemctl status awslogs
● awslogs.service - LSB: Daemon for AWSLogs agent.
   Loaded: loaded (/etc/rc.d/init.d/awslogs; bad; vendor preset: disabled)
   Active: active (running) since 月 2017-08-21 16:58:05 JST; 1 weeks 4 days ago
     Docs: man:systemd-sysv-generator(8)
  Process: 820 ExecStop=/etc/rc.d/init.d/awslogs stop (code=exited, status=0/SUCCESS)
  Process: 840 ExecStart=/etc/rc.d/init.d/awslogs start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/awslogs.service
           tq846 /bin/sh /var/awslogs/bin/awslogs-agent-launcher.sh
           mq849 /var/awslogs/bin/python /var/awslogs/bin/aws logs push --con...

取得ログの追加

設定ファイルの変更
# vi /var/awslogs/etc/awslogs.conf ← CloudWatch logsの設定ファイル

設定ファイルに以下の内容を追記
[/var/log/httpd/access_log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /var/log/httpd/access_log ← 取得したいログファイル
buffer_duration = 5000
log_stream_name = {ip_address} ← ログストリーム名を入力
initial_position = start_of_file
log_group_name = /var/log/http/access_log ← ロググループ名を入力

3.AWSコンソールで確認する
AWSコンソールにログインする⇒[サービス]⇒[CloudWatch]⇒[ログ]⇒手順1で設定した(Destination Log Group name)押下⇒CloudWatch Logsをインストールしたサーバーが出てればOK

参考URL

19
22
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
19
22