Last active
December 25, 2015 11:48
-
-
Save mikeda/6971270 to your computer and use it in GitHub Desktop.
EC2のメモリ、ディスクの使用率をCloudWatchのカスタムメトリクスに追加するcronスクリプト
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
require 'aws-sdk' | |
$config_file = File.join(File.dirname(__FILE__), "../../config/aws/aws.yml") | |
$config = YAML.load(File.read($config_file)) | |
AWS.config( | |
access_key_id: $config['access_key_id'], | |
secret_access_key: $config['secret_access_key'], | |
region: $config['region'] | |
) |
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
#!/usr/bin/env ruby | |
# OSの監視情報をCloudWatchのカスタムメトリクスに追加するスクリプト | |
# | |
# 追加する項目 | |
# - メモリ使用率 | |
# - ディスク利用率 | |
# - / : 標準EBSボリューム | |
# - /data : 追加EBSボリューム | |
# 各EC2インスタンス上のcronで5分ごとに実行 | |
# */5 * * * * ec2-user /home/ec2-user/bin/mon_put_data_ec2.rb | |
require File.expand_path(File.dirname(__FILE__) + '/config') | |
require 'net/http' | |
NAMESPACE = 'Custom/EC2' | |
def mem_usage | |
meminfo = {} | |
File.open('/proc/meminfo').each do |line| | |
line =~ /^(.*?):\s+(\d+)/ or next; | |
meminfo[$1] = $2.to_i * 1024 | |
end | |
total = meminfo['MemTotal'] | |
avail = meminfo['MemFree'] + meminfo['Cached'] + meminfo['Buffers'] | |
100.0 * ( total - avail ) / total | |
end | |
def disk_usage(path) | |
df = "/bin/df -k -l -P #{path} | tail -n 1" | |
total, used = `#{df}`.chomp.split(/\s+/).values_at(1, 2) | |
100 * ( used.to_f / total.to_f ) | |
end | |
instance_id = Net::HTTP.get('169.254.169.254', '/latest/meta-data/instance-id') | |
dimensions = [ { name: 'InstanceId', value: instance_id } ] | |
metric_data = [ | |
{ metric_name: 'MemoryUsage', value: mem_usage }, | |
{ metric_name: 'DiskUsage_root', value: disk_usage('/') }, | |
{ metric_name: 'DiskUsage_data', value: disk_usage('/data') }, | |
] | |
metric_data.each do |m| | |
m[:dimensions] = dimensions | |
m[:unit] = 'Percent' | |
end | |
cw = AWS::CloudWatch.new | |
cw.put_metric_data( namespace: NAMESPACE, metric_data: metric_data ) |
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
#!/usr/bin/env ruby | |
# EC2インスタンスにアラームを設定するスクリプト | |
# ※事前にアラート送信先となるSNSトピックを作成しておくこと | |
require File.expand_path(File.dirname(__FILE__) + '/config') | |
if ARGV.size != 1 | |
puts "usage: #{$0} <hostname>" | |
exit 1 | |
end | |
hostname = ARGV.shift | |
ec2 = AWS::EC2.new | |
instance_id = ec2.tags.filter('value', hostname).first.resource.id | |
cw_config = [ | |
{ | |
namespace: 'AWS/EC2', | |
metric_name: 'StatusCheckFailed', | |
threshold: 0, | |
comparison_operator: 'GreaterThanThreshold' | |
}, | |
{ | |
namespace: 'AWS/EC2', | |
metric_name: 'CPUUtilization', | |
threshold: 80, | |
comparison_operator: 'GreaterThanThreshold' | |
}, | |
{ | |
namespace: 'Custom/EC2', | |
metric_name: 'MemoryUsage', | |
threshold: 80, | |
comparison_operator: 'GreaterThanThreshold' | |
}, | |
{ | |
namespace: 'Custom/EC2', | |
metric_name: 'DiskUsage_root', | |
threshold: 80, | |
comparison_operator: 'GreaterThanThreshold' | |
}, | |
{ | |
namespace: 'Custom/EC2', | |
metric_name: 'DiskUsage_data', | |
threshold: 80, | |
comparison_operator: 'GreaterThanThreshold' | |
}, | |
] | |
cw = AWS::CloudWatch.new | |
cw_config.each do |config| | |
alert_name = [ hostname, config[:namespace], config[:metric_name] ].join('-') | |
cw.alarms.create( alert_name, | |
namespace: config[:namespace], | |
metric_name: config[:metric_name], | |
threshold: config[:threshold], | |
comparison_operator: config[:comparison_operator], | |
statistic: 'Average', | |
period: 300, | |
dimensions: [ { name: 'InstanceId', value: instance_id } ], | |
evaluation_periods: 1, | |
ok_actions: $config['cloudwatch']['actions'], | |
alarm_actions: $config['cloudwatch']['actions'], | |
insufficient_data_actions: $config['cloudwatch']['actions'], | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
aws.ymlの中身が知りたいです。actionの記載もあるのですか?