
条件
サーバに権限をつける
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricData"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances"
],
"Resource": "*"
}
]
}
Roleの検証はしていませんので、失敗する場合は他サイトをご参考にお願いします。
sudo apt-get update sudo apt-get install collectd sudo apt-get install awscli
vi /root/loadaverage.sh
#!/bin/bash
AWS_CLI_REGION="ap-northeast-1"
metric_name="LoadAverage"
name_space="EC2/${metric_name}"
unit="Percent"
load_average=`cut -f 1 -d " " /proc/loadavg`
# get ec2 name value
export INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
TMP_NAME=$(aws ec2 describe-instances --region ${AWS_CLI_REGION} --instance-ids "${INSTANCE_ID}" --query 'Reservations[0].Instances[0].Tags[?Key==`Name`]|[0].Value')
NAME=`echo ${TMP_NAME} | tr -d '"'`
# put metrics
aws cloudwatch --region ${AWS_CLI_REGION} put-metric-data --namespace ${name_space} --metric-name ${metric_name} --dimensions InstanceId=${INSTANCE_ID},InstanceName=${NAME} --value ${load_average} --unit ${unit}
# vi /etc/crontab # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) # LoadAverage * * * * * root /root/loadaverage.sh
# systemctl restart cron
ubuntuでcronログを見る
# sudo journalctl -f -u cron




