Skip to content

Instantly share code, notes, and snippets.

@kawahiro311
Created October 6, 2016 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawahiro311/935d85661b9314255b5873a338c92f35 to your computer and use it in GitHub Desktop.
Save kawahiro311/935d85661b9314255b5873a338c92f35 to your computer and use it in GitHub Desktop.
プロセス監視用のcloudwatchカスタムメトリックス
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