AWS

CloudWatch Agent Laravelログ出力 AutoScalingGroup環境

AWS

関連

CloudWatch Agent Supervisordのプロセス監視

 

ENAが有効になっていないとネットワーク系のメトリクスは取得できません

AWS EC2 ENAの設定確認と有効化

 

公式資料

 

 

環境

  • AWS EC2
  • Ubuntu

 

Ubuntu + Laravel

AWS Ubuntu18 + Laravelのログ集約

 

IAM

IAM
  ロールの作成 ->
  ポリシー -> CloudWatchAgentAdminPolicyにチェック

 

 

EC2にIAMロールを紐付け

 

CloudWatch Agent

 

$ wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
$ sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
$ sudo amazon-cloudwatch-agent-ctl -a start

$ sudo apt-get update
$ sudo apt-get install collectd

 

初期コンフィグ

$ cat /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/default

{
	"agent": {
		"run_as_user": "cwagent"
	},
	"metrics": {
		"metrics_collected": {
			"mem": {
				"measurement": [
					"mem_used_percent"
				]
			},
			"disk": {
				"measurement": [
					"used_percent"
				],
				"resources": [
					"*"
				]
			}
		},
		"append_dimensions": {
			"ImageId": "${aws:ImageId}",
			"InstanceId": "${aws:InstanceId}",
			"InstanceType": "${aws:InstanceType}",
			"AutoScalingGroupName": "${aws:AutoScalingGroupName}"
		}
	}
}

 

Laravel用に改造

$ vi /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/default


{
        "agent": {
                "run_as_user": "cwagent"
        },
        "metrics": {
                "metrics_collected": {
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ]
                        },
                        "disk": {
                                "measurement": [
                                        "used_percent"
                                ],
                                "resources": [
                                        "*"
                                ]
                        }
                },
                "append_dimensions": {
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}",
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}"
                }
        }
}

 

# vi /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/default


{
    "agent": {
        "metrics_collection_interval": 60,
        "run_as_user": "root"
    },
    "metrics": {
        "aggregation_dimensions": [
            [
                "InstanceId"
            ],
            [
                "AutoScalingGroupName"
            ]
        ],
        "append_dimensions": {
            "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
            "ImageId": "${aws:ImageId}",
            "InstanceId": "${aws:InstanceId}",
            "InstanceType": "${aws:InstanceType}"
        },
        "metrics_collected": {
            "collectd": {
                "metrics_aggregation_interval": 60
            },
            "ethtool": {
              "interface_include": [
                "eth0"
              ],
              "metrics_include": [
                "rx_packets",
                "tx_packets",
                "bw_in_allowance_exceeded",
                "bw_out_allowance_exceeded",
                "conntrack_allowance_exceeded",
                "linklocal_allowance_exceeded",
                "pps_allowance_exceeded"
               ]
            },
            "net": {
                "measurement": [
                    "bytes_sent",
                    "bytes_recv",
                    "drop_in",
                    "drop_out",
                    "err_in",
                    "err_out",
                    "packets_sent",
                    "packets_recv"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "cpu": {
                "measurement": [
                    "cpu_usage_idle",
                    "cpu_usage_iowait",
                    "cpu_usage_user",
                    "cpu_usage_system"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ],
                "totalcpu": false
            },
            "disk": {
                "measurement": [
                    "used_percent",
                    "inodes_free",
                    "inodes_used",
                    "inodes_total"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "diskio": {
                "measurement": [
                    "io_time",
                    "write_bytes",
                    "read_bytes",
                    "writes",
                    "reads"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 60
            },
            "swap": {
                "measurement": [
                    "swap_used_percent",
                    "swap_used"
                ],
                "metrics_collection_interval": 60
            },
            "netstat": {
                "measurement": [
                    "tcp_established",
                    "tcp_time_wait"
                ],
                "metrics_collection_interval": 60
            },
            "statsd": {
                "metrics_aggregation_interval": 60,
                "metrics_collection_interval": 10,
                "service_address": ":8125"
            }
        }
    },
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "/var/www/project/storage/logs/laravel*log",
                        "log_group_name": "/ec2/develop/console/laravel",
                        "log_stream_name": "ec2-{instance_id}-laravel"
                    },
                    {
                        "file_path": "/var/www/project/storage/logs/request*log",
                        "log_group_name": "/ec2/develop/console/laravel/request",
                        "log_stream_name": "ec2-{instance_id}-laravel-request"
                    },
                    {
                        "file_path": "/var/log/nginx/*.log*",
                        "log_group_name": "/ec2/develop/console/nginx",
                        "log_stream_name": "ec2-{instance_id}-nginx"
                    },
                    {
                        "file_path": "/var/log/nginx/error.log",
                        "log_group_name": "/ec2/develop/console/nginx/error",
                        "log_stream_name": "ec2-{instance_id}-nginx-error"
                    },
                    {
                         "file_path": "/var/log/php7.2-fpm.log",
                         "log_group_name": "/ec2/develop/console/php-fpm",
                         "log_stream_name": "ec2-{instance_id}-php-fpm"
                    },
                    {
                        "file_path": "/var/log/php7.2-fpm-slow.log",
                        "log_group_name": "/ec2/develop/console/php-fpm/slow",
                        "log_stream_name": "ec2-{instance_id}-php-fpm-slow"
                    }
                ]
            }
        }
    }
}

 

再起動

$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop
$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start

$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
{
  "status": "running",
  "starttime": "2020-10-01T03:19:47+00:00",
  "version": "1.247345.34b249191"
}

# tail -n 1000 /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log

再起動してCloudWatch Agentが自動起動していたら寝て良し!

CWAgent > AutoScalingGroupName, ImageId, InstanceId, InstanceType
1
CWAgent > AutoScalingGroupName, ImageId, InstanceId, InstanceType, device, fstype, path
7
CWAgent > InstanceId
2

 

にてヒットした
AWSコンソールでCloudWatch -> ログからロググループ”yuu3-stg”を確認してログが出力されている🐱

 

関連

 

AWS CloudWatch Logs MetricsにLoadAverageのメトリクスを追加する

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)