Created
July 2, 2015 11:53
-
-
Save ysasaki/4c90d31100d0dda8277e to your computer and use it in GitHub Desktop.
EC2 インスタンス上の httpd の server-status を CloudWatch に追加する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apache_dir = '/etc/httpd' | |
cookbook_file "#{apache_dir}/conf.d/server-status.conf" do | |
source 'server-status.conf' | |
owner 'root' | |
group 'root' | |
mode '0644' | |
action :create | |
end | |
cookbook_file '/usr/local/bin/cw-server-status.sh' do | |
source 'cw-server-status.sh' | |
owner 'root' | |
group 'root' | |
mode '0755' | |
action :create | |
end | |
execute "ExtendedStatus On" do | |
not_if "grep -e '^ExtendedStatus On' #{apache_dir}/conf/httpd.conf" | |
command "echo 'ExtendedStatus On' >> #{apache_dir}/conf/httpd.conf" | |
user "root" | |
action :run | |
end | |
execute "Install crontab" do | |
not_if "grep cw-server-status.sh /etc/crontab" | |
command "echo '* * * * * root /usr/local/bin/cw-server-status.sh' >> /etc/crontab" | |
user 'root' | |
action :run | |
end | |
service 'httpd' do | |
restart_command '/sbin/service httpd restart && sleep 1' | |
supports [:restart] | |
action :restart | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# IAM user:cloudwatch | |
AWS_DEFAULT_REGION="ap-northeast-1" | |
AWS_ACCESS_KEY_ID="YOUR ACCESS KEY" | |
AWS_SECRET_ACCESS_KEY="YOUR SECRET ACCESS KEY" | |
INSTANCE_ID_URL="http://169.254.169.254/latest/meta-data/instance-id" | |
INSTANCE_ID=$(curl -s ${INSTANCE_ID_URL}) | |
SERVER_STATUS_URL="http://localhost/server-status?auto" | |
SERVER_STATUS_HOSTNAME="server-status.localhost" | |
NAMESPACE="httpd" | |
TIMESTAMP=$(date -u '+%Y-%m-%dT%H:%M:%SZ') | |
PUT_METRIC_CMD="/bin/env \ | |
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} \ | |
AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} \ | |
AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} \ | |
/usr/bin/aws cloudwatch put-metric-data \ | |
--region ${AWS_DEFAULT_REGION} \ | |
--dimensions InstanceId=${INSTANCE_ID} \ | |
--namespace ${NAMESPACE} \ | |
--timestamp ${TIMESTAMP}" | |
TMP_FILE="/tmp/server-status" | |
# GET /server-status | |
curl -H"Host: ${SERVER_STATUS_HOSTNAME}" \ | |
-s -L ${SERVER_STATUS_URL} > ${TMP_FILE} | |
# put-metric-data | |
${PUT_METRIC_CMD} --metric-name ReqPerSec --unit Count \ | |
--value $(grep ReqPerSec ${TMP_FILE} | awk '{print $2}') | |
${PUT_METRIC_CMD} --metric-name BusyWorkers --unit Count \ | |
--value $(grep BusyWorkers ${TMP_FILE} | awk '{print $2}') | |
${PUT_METRIC_CMD} --metric-name IdleWorkers --unit Count \ | |
--value $(grep IdleWorkers ${TMP_FILE} | awk '{print $2}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<VirtualHost *:80> | |
ServerName server-status.localhost | |
DocumentRoot /var/www/html | |
ErrorLog /var/log/httpd/server-status.localhost-error_log | |
CustomLog /var/log/httpd/server-status.localhost-access_log combined | |
DirectoryIndex index.html | |
<Location "/server-status"> | |
SetHandler server-status | |
Order deny,allow | |
Deny from all | |
Allow from 127.0.0.1 | |
</Location> | |
</VirtualHost> | |
# vim:set ts=4 sw=4 expandtab ft=apache: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment