0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Elastic Beanstalkでメモリとディスク残量の監視を行う

Last updated at Posted at 2020-11-04

(事前にSNS側で通知を受けられるような準備はしておくこと)

.ebextensionにて以下を追加

packages:
  yum:
    perl-DateTime: []
    perl-Sys-Syslog: []
    perl-LWP-Protocol-https: []
    perl-Switch: []
    perl-URI: []
    perl-Bundle-LWP: []
files:
  /root/setup_alarm.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash
      INSTANCE_ID="`wget -qO- http://169.254.169.254/latest/meta-data/instance-id`"
      REGION="`curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region`"
      echo '*/5 * * * * root perl /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl `{"Fn::GetOptionSetting" : { "OptionName" : "CloudWatchMetrics", "DefaultValue" : "--mem-util --disk-space-util --disk-path=/" }}` >> /var/log/cwpump.log 2>&1' > /etc/cron.d/cwpump
      chmod 644 /etc/cron.d/cwpump
      chmod u+x /opt/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl
      aws cloudwatch put-metric-alarm --region=$REGION --alarm-name WebDiskSpaceUtilization --alarm-description "Alarm when DiskSpaceUtilization exceeds 90 percent" --metric-name DiskSpaceUtilization --namespace System/Linux --statistic Average --period 300 --threshold 90 --comparison-operator GreaterThanThreshold  --dimensions Name=Filesystem,Value=/dev/nvme0n1p1 Name=MountPath,Value=/ Name=InstanceId,Value=$INSTANCE_ID --evaluation-periods 2 --alarm-actions #{SNSのARN} --unit Percent
      aws cloudwatch put-metric-alarm --region=$REGION --alarm-name WebMemoryUtilization --alarm-description "Alarm when MemoryUtilization exceeds 95 percent" --metric-name MemoryUtilization --namespace System/Linux --statistic Average --period 300 --threshold 95 --comparison-operator GreaterThanThreshold  --dimensions Name=InstanceId,Value=$INSTANCE_ID --evaluation-periods 2 --alarm-actions #{SNSのARN} --unit Percent
sources:
  /opt/cloudwatch: https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip
container_commands:
  add-swap-space:
    command: "/bin/bash /root/setup_alarm.sh"
    ignoreErrors: true
option_settings:
  "aws:autoscaling:launchconfiguration" :
    IamInstanceProfile : "aws-elasticbeanstalk-ec2-role"
  "aws:elasticbeanstalk:customoption" :
    CloudWatchMetrics : "--mem-util --mem-used --mem-avail --disk-space-util --disk-space-used --disk-space-avail --disk-path=/ --auto-scaling"

IAMでRoles > aws-elasticbeanstalk-ec2-role の「Add inline policy」にて、下記のように設定して追加

  • Service: CloudWatch
  • Actions: PutMetricData と PutMetricAlarm を追加
  • Resources: Specific Add ARN にて例えば下記
    • Region => ana
    • Account => デフォルトのまま
    • Alarm Name => any
  • Name: CloudWatchMetricAlarm など

これでデプロイしてCloudWatchの

  • System/Linux > InstanceID でメモリ使用量などが確認できる
  • System/Linux > AutoScalingGroupName, Filesystem, MountPath でディスク残量などが確認できる

Alarmで監視されていることを確認

参考: https://aws.amazon.com/jp/premiumsupport/knowledge-center/elastic-beanstalk-memory-monitoring/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?