Created
October 6, 2016 09:47
-
-
Save kawahiro311/935d85661b9314255b5873a338c92f35 to your computer and use it in GitHub Desktop.
プロセス監視用のcloudwatchカスタムメトリックス
This file contains 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 'optparse' | |
require 'net/http' | |
require 'aws-sdk-resources' | |
# bundle exec ruby cloudwatch_put_process.rb --process nginx | |
opts = ARGV.getopts('', 'process:') | |
process = opts['process'] | |
METADATA_ENDPOINT = 'http://169.254.169.254/latest/meta-data' | |
instance_id = Net::HTTP.get(URI.parse("#{METADATA_ENDPOINT}/instance-id")) | |
process_count = `ps awux | grep -w #{process} | grep -v grep | grep -v "cloudwatch_put_process.rb --process" | wc -l` | |
cloudwatch = Aws::CloudWatch::Client.new(region: 'ap-northeast-1') | |
cloudwatch.put_metric_data( | |
namespace: 'Custom Metrix', | |
metric_data: [ | |
{ | |
metric_name: "#{process}Processes", | |
dimensions: [ | |
{ | |
name: 'InstanceId', | |
value: instance_id, | |
} | |
], | |
value: process_count, | |
unit: 'Count', | |
} | |
] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment