ruboty-sqs_monitor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b8712835ce504b363df97f53e95882c263ed923
4
+ data.tar.gz: aa82ed7daeb17fe59fe6660221c0a77ba08dfe84
5
+ SHA512:
6
+ metadata.gz: 9787a39f917347fcb66c99fc7008a8e06388b03862b1a0f0e373b0acb59237fda587d04ea143dd3b9d19a395c36ff883f94ecd909474c4411fe3ff595b6f2fb1
7
+ data.tar.gz: cc944c1128c0a8d4178fa915d03bfaed6ca3c5ed4545a08fab5c50a68ea1b3e6db5ccbc13bacf47f4d0e5de60b8ac94325cee8ccf72f45cb900105d12abd7235
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-elb.gemspec
4
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruboty-elb (0.1.0)
5
+ aws-sdk
6
+ ruboty
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (4.2.6)
12
+ i18n (~> 0.7)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.3, >= 0.3.4)
16
+ tzinfo (~> 1.1)
17
+ aws-sdk (2.2.28)
18
+ aws-sdk-resources (= 2.2.28)
19
+ aws-sdk-core (2.2.28)
20
+ jmespath (~> 1.0)
21
+ aws-sdk-resources (2.2.28)
22
+ aws-sdk-core (= 2.2.28)
23
+ dotenv (2.1.0)
24
+ i18n (0.7.0)
25
+ jmespath (1.1.3)
26
+ json (1.8.3)
27
+ mem (0.1.5)
28
+ minitest (5.8.4)
29
+ rake (10.5.0)
30
+ ruboty (1.2.2)
31
+ activesupport
32
+ bundler
33
+ dotenv
34
+ mem
35
+ slop
36
+ slop (4.3.0)
37
+ thread_safe (0.3.5)
38
+ tzinfo (1.2.2)
39
+ thread_safe (~> 0.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ bundler (~> 1.10)
46
+ rake (~> 10.0)
47
+ ruboty-elb!
48
+
49
+ BUNDLED WITH
50
+ 1.10.6
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 arukoh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,102 @@
1
+ # Ruboty::SqsMonitor
2
+
3
+ [Ruboty](https://github.com/r7kamura/ruboty) plug-in to monitor AWS SQS.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruboty-sqs_monitor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruboty-sqs_monitor
20
+
21
+ ## ENV
22
+ ```
23
+ SQS_REGION - AWS region for SQS (default: ENV["AWS_REGION"])
24
+ SQS_ACCESS_KEY_ID - AWS access key for SQS (default: ENV["AWS_ACCESS_KEY_ID"])
25
+ SQS_SECRET_ACCESS_KEY - AWS secret key for SQS (default: ENV["AWS_SECRET_ACCESS_KEY"])
26
+ SQS_METRICS - Metric list to monitor (default: "ApproximateNumberOfMessagesDelayed,ApproximateNumberOfMessagesNotVisible,ApproximateNumberOfMessagesVisible,NumberOfEmptyReceives,NumberOfMessagesDeleted,NumberOfMessagesReceived,NumberOfMessagesSent,SentMessageSize")
27
+ SQS_TREND_SEPARATOR - Trend separator (default: ' ')
28
+ ```
29
+
30
+ ## Usage
31
+ ```
32
+ $ bundle exec ruboty
33
+ Type `exit` or `quit` to end the session.
34
+ > @ruboty help sqs
35
+ ruboty /add sqs (?<name>.+) (?<real_name>.+)/ - Add a new alias name of sqs queue to monitor.
36
+ ruboty /delete sqs (?<name>.+)/ - Delete a alias name of sqs queue to monitor.
37
+ ruboty /list sqs queues\z/ - List all sqs queue names.
38
+ ruboty /show sqs state (?<from>(?:(?!last).)+) (?<to>.+)/ - Show all sqs queue status of the specified period.
39
+ ruboty /show sqs state last (?<time>\d+)(?<format>(w|d|h|m)) trend\z/ - Show all sqs queue status with trend of the last [w]eeks, [d]ays, [h]ours or [m]inutes.
40
+ ruboty /show sqs state last (?<time>\d+)(?<format>(w|d|h|m))\z/ - Show all sqs queue status of the last [w]eeks, [d]ays, [h]ours or [m]inutes.
41
+ ```
42
+
43
+ ### Example
44
+ ```
45
+ $ bundle exec ruboty
46
+ Type `exit` or `quit` to end the session.
47
+ > @ruboty add sqs foo sqs-name-xxx
48
+ SQS foo registerd.
49
+ > @ruboty list sqs queues
50
+ foo: sqs-name-xxx
51
+ > @ruboty show sqs state 20160320 20160323
52
+ 2016-03-20T00:00:00Z - 2016-03-23T00:00:00Z
53
+ *** foo(sqs-name-xxx) ***
54
+ - ApproximateNumberOfMessagesDelayed(Maximum) 0
55
+ - ApproximateNumberOfMessagesNotVisible(Maximum) 1
56
+ - ApproximateNumberOfMessagesVisible(Maximum) 0
57
+ - NumberOfEmptyReceives(Sum) 12929
58
+ - NumberOfMessagesDeleted(Sum) 18
59
+ - NumberOfMessagesReceived(Sum) 30
60
+ - NumberOfMessagesSent(Sum) 19
61
+ - SentMessageSize(Average) 813
62
+ > @ruboty show sqs state last 3d
63
+ 2016-03-20T00:00:00Z - 2016-03-23T00:00:00Z
64
+ *** foo(sqs-name-xxx) ***
65
+ - ApproximateNumberOfMessagesDelayed(Maximum) 0
66
+ - ApproximateNumberOfMessagesNotVisible(Maximum) 1
67
+ - ApproximateNumberOfMessagesVisible(Maximum) 0
68
+ - NumberOfEmptyReceives(Sum) 12929
69
+ - NumberOfMessagesDeleted(Sum) 18
70
+ - NumberOfMessagesReceived(Sum) 30
71
+ - NumberOfMessagesSent(Sum) 19
72
+ - SentMessageSize(Average) 813
73
+ > @ruboty show sqs state last 3d trend
74
+ 2016-03-20T00:00:00Z - 2016-03-23T00:00:00Z
75
+ *** foo(sqs-name-xxx) ***
76
+ - ApproximateNumberOfMessagesDelayed(Maximum) 0 0 0
77
+ - ApproximateNumberOfMessagesNotVisible(Maximum) 0 1 0
78
+ - ApproximateNumberOfMessagesVisible(Maximum) 0 0 0
79
+ - NumberOfEmptyReceives(Sum) 4288 4297 4344
80
+ - NumberOfMessagesDeleted(Sum) 0 1 17
81
+ - NumberOfMessagesReceived(Sum) 0 13 17
82
+ - NumberOfMessagesSent(Sum) 0 2 17
83
+ - SentMessageSize(Average) 0 529 846
84
+ > @ruboty delete sqs foo
85
+ SQS foo deleted
86
+ ```
87
+
88
+ ## Development
89
+
90
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
91
+
92
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruboty-sqs_monitor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
97
+
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
102
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty"
5
+ require "ruboty/sqs_monitor"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,44 @@
1
+ require "ruboty/sqs_monitor/actions/add"
2
+ require "ruboty/sqs_monitor/actions/delete"
3
+ require "ruboty/sqs_monitor/actions/list"
4
+ require "ruboty/sqs_monitor/actions/state"
5
+ require "ruboty/sqs_monitor/actions/state_trend"
6
+
7
+ module Ruboty
8
+ module Handlers
9
+ # Monitor AWS SQS via Ruboty.
10
+ class SqsMonitor < Base
11
+ on /add sqs (?<name>.+) (?<real_name>.+)/, name: 'add', description: 'Add a new alias name of sqs queue to monitor.'
12
+ on /delete sqs (?<name>.+)/, name: 'delete', description: 'Delete a alias name of sqs queue to monitor.'
13
+ on /list sqs queues\z/, name: 'list', description: 'List all sqs queue names.'
14
+ on /show sqs state last (?<time>\d+)(?<format>(w|d|h|m))\z/, name: 'state', description: 'Show all sqs queue status of the last [w]eeks, [d]ays, [h]ours or [m]inutes.'
15
+ on /show sqs state last (?<time>\d+)(?<format>(w|d|h|m)) trend\z/, name: 'state_trend', description: 'Show all sqs queue status with trend of the last [w]eeks, [d]ays, [h]ours or [m]inutes.'
16
+ on /show sqs state (?<from>(?:(?!last).)+) (?<to>.+)/, name: 'state', description: 'Show all sqs queue status of the specified period.'
17
+ env :SQS_REGION, "AWS region for SQS.", optional: true
18
+ env :SQS_ACCESS_KEY_ID, "AWS access key for SQS.", optional: true
19
+ env :SQS_SECRET_ACCESS_KEY, "AWS secret key for SQS.", optional: true
20
+ env :SQS_METRICS, "Metric list to ask.", optional: true
21
+ env :SQS_TREND_SEPARATOR, "Trend separator.", optional: true
22
+
23
+ def add(message)
24
+ Ruboty::SqsMonitor::Actions::Add.new(message).call
25
+ end
26
+
27
+ def delete(message)
28
+ Ruboty::SqsMonitor::Actions::Delete.new(message).call
29
+ end
30
+
31
+ def list(message)
32
+ Ruboty::SqsMonitor::Actions::List.new(message).call
33
+ end
34
+
35
+ def state(message)
36
+ Ruboty::SqsMonitor::Actions::State.new(message).call
37
+ end
38
+
39
+ def state_trend(message)
40
+ Ruboty::SqsMonitor::Actions::StateTrend.new(message).call
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+ require "ruboty/sqs_monitor/version"
2
+ require "ruboty/sqs_monitor/aws"
3
+ require "ruboty/handlers/sqs_monitor"
4
+
5
+ module Ruboty
6
+ module SqsMonitor
7
+ NAMESPACE = "sqs_monitor"
8
+
9
+ class << self
10
+ def sqs(name)
11
+ Aws::Sqs.new(name, sdk_options)
12
+ end
13
+
14
+ def cloudwatch(sqs_name, start_time=nil, end_time=nil)
15
+ options = sdk_options.merge(
16
+ start_time: start_time,
17
+ end_time: end_time,
18
+ )
19
+ Aws::CloudWatch.new(sqs_name, options)
20
+ end
21
+
22
+ def metrics
23
+ (ENV["SQS_METRICS"] || DEFAULT_METRICS).split(/,/)
24
+ end
25
+
26
+ def trend_separator
27
+ ENV["SQS_TREND_SEPARATOR"] || " "
28
+ end
29
+
30
+ private
31
+ DEFAULT_METRICS = %w{
32
+ ApproximateNumberOfMessagesDelayed
33
+ ApproximateNumberOfMessagesNotVisible
34
+ ApproximateNumberOfMessagesVisible
35
+ NumberOfEmptyReceives
36
+ NumberOfMessagesDeleted
37
+ NumberOfMessagesReceived
38
+ NumberOfMessagesSent
39
+ SentMessageSize
40
+ }.join(",")
41
+
42
+ def sdk_options
43
+ options = {
44
+ http_proxy: ENV["HTTPS_PROXY"] || ENV["https_proxy"] || ENV["HTTP_PROXY"] || ENV["http_proxy"]
45
+ }
46
+ options[:region] = ENV["SQS_REGION"] if ENV["SQS_REGION"]
47
+ options[:access_key_id] = ENV["SQS_ACCESS_KEY_ID"] if ENV["SQS_ACCESS_KEY_ID"]
48
+ options[:secret_access_key] = ENV["SQS_SECRET_ACCESS_KEY"] if ENV["SQS_SECRET_ACCESS_KEY"]
49
+ options
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,27 @@
1
+ require_relative "base"
2
+
3
+ module Ruboty
4
+ module SqsMonitor
5
+ module Actions
6
+ class Add < Base
7
+ def call
8
+ message.reply(add)
9
+ rescue => e
10
+ message.reply(e.message)
11
+ end
12
+
13
+ private
14
+ def add
15
+ name = message[:name]
16
+ real = message[:real_name]
17
+
18
+ sqs = Ruboty::SqsMonitor.sqs(real)
19
+ raise "SQS #{name} not found." unless sqs.exists?
20
+ queues[name] = real
21
+
22
+ "SQS #{name} registerd."
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ module Ruboty
2
+ module SqsMonitor
3
+ module Actions
4
+ class Base < Ruboty::Actions::Base
5
+ private
6
+ def queues
7
+ message.robot.brain.data[Ruboty::SqsMonitor::NAMESPACE] ||= {}
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "base"
2
+
3
+ module Ruboty
4
+ module SqsMonitor
5
+ module Actions
6
+ class Delete < Base
7
+ def call
8
+ message.reply(delete)
9
+ rescue => e
10
+ message.reply(e.message)
11
+ end
12
+
13
+ private
14
+ def delete
15
+ name = message[:name]
16
+ if queues.has_key?(name)
17
+ queues.delete(name)
18
+ "SQS #{name} deleted"
19
+ else
20
+ "SQS #{name} does not exist"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "base"
2
+
3
+ module Ruboty
4
+ module SqsMonitor
5
+ module Actions
6
+ class List < Base
7
+ def call
8
+ message.reply(list)
9
+ rescue => e
10
+ message.reply(e.message)
11
+ end
12
+
13
+ private
14
+ def list
15
+ if queues.empty?
16
+ "SQS not found"
17
+ else
18
+ queues.map{|k, v| "#{k}: #{v}"}.join("\n")
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,79 @@
1
+ require_relative "base"
2
+
3
+ module Ruboty
4
+ module SqsMonitor
5
+ module Actions
6
+ class State < Base
7
+ def call
8
+ message.reply(state)
9
+ rescue => e
10
+ message.reply(e.message)
11
+ end
12
+
13
+ private
14
+ def state(trend=false)
15
+ return "SQS not found" if queues.empty?
16
+
17
+ start_time, end_time, period = get_time(trend)
18
+
19
+ msg = []
20
+ msg << "#{start_time.iso8601} - #{end_time.iso8601}"
21
+ queues.each do |name, real|
22
+ msg << "*** #{name}(#{real}) ***"
23
+
24
+ resp = get_state(real, start_time, end_time, period)
25
+ label_max = resp.map(&:label).map(&:size).max
26
+ resp.each do |st|
27
+ value = get_value(st)
28
+ msg << " - #{st.label.ljust(label_max)}\t#{value}"
29
+ end
30
+ end
31
+ msg.join("\n")
32
+ end
33
+
34
+ def get_state(name, start_time, end_time, period)
35
+ cloudwatch = Ruboty::SqsMonitor.cloudwatch(name, start_time, end_time)
36
+ Ruboty::SqsMonitor.metrics.map do |metric|
37
+ cloudwatch.get_statistics(metric, period: period)
38
+ end
39
+ end
40
+
41
+ def get_time(trend)
42
+ from = message[:from] rescue nil
43
+ to = message[:to] rescue nil
44
+ time = message[:time].to_i rescue nil
45
+ format = message[:format] rescue nil
46
+ if from && to
47
+ last = Time.parse(from)
48
+ now = Time.parse(to)
49
+ [ last, now, now - last ]
50
+ else
51
+ now = Time.now
52
+ last = case format.to_s
53
+ when "w"
54
+ now - (time * 7 * 24 * 60 * 60)
55
+ when "d"
56
+ now - (time * 24 * 60 * 60)
57
+ when "h"
58
+ now - (time * 60 * 60)
59
+ when "m"
60
+ now - (time * 60)
61
+ else
62
+ now
63
+ end
64
+ period = trend ? (now - last) / time : (now - last)
65
+ [ last, now, period.to_i ]
66
+ end
67
+ end
68
+
69
+ def get_value(st)
70
+ values = []
71
+ st.datapoints.each do |dp|
72
+ values << (dp.unit.to_s.downcase == "seconds" ? "%0.2f"%dp.value : "%d"%dp.value)
73
+ end
74
+ values.empty? ? 0 : values.join(Ruboty::SqsMonitor.trend_separator)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "state"
2
+
3
+ module Ruboty
4
+ module SqsMonitor
5
+ module Actions
6
+ class StateTrend < State
7
+ def call
8
+ message.reply(state(true))
9
+ rescue => e
10
+ message.reply(e.message)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ require "aws-sdk"
2
+ require "ruboty/sqs_monitor/aws/sqs"
3
+ require "ruboty/sqs_monitor/aws/cloudwatch"
@@ -0,0 +1,110 @@
1
+ module Ruboty
2
+ module SqsMonitor
3
+ module Aws
4
+ class CloudWatch
5
+ attr_reader :queue_name, :start_time, :end_time
6
+
7
+ def initialize(queue_name, options)
8
+ @queue_name = queue_name
9
+ @start_time = options[:start_time]
10
+ @end_time = options[:end_time]
11
+
12
+ opts = options.select {|k, v| CLIENT_OPTIONS.include? k.to_sym }
13
+ @client = ::Aws::CloudWatch::Client.new(opts)
14
+ end
15
+
16
+ def get_statistics(metric_name,
17
+ stats: nil,
18
+ start_time: nil,
19
+ end_time: nil,
20
+ period: nil)
21
+ stats ||= default_stats(metric_name)
22
+ end_time ||= @end_time || Time.now
23
+ start_time ||= @start_time || (end_time - 3600)
24
+ period ||= end_time - start_time
25
+
26
+ opts = {
27
+ namespace: "AWS/SQS",
28
+ metric_name: metric_name,
29
+ dimensions: [ {name: "QueueName", value: @queue_name} ],
30
+ start_time: start_time,
31
+ end_time: end_time,
32
+ period: period.to_i,
33
+ statistics: [stats]
34
+ }
35
+ resp = @client.get_metric_statistics(opts)
36
+
37
+ datapoints = default_datapoints(start_time, end_time, period)
38
+ resp.datapoints.each do |datapoint|
39
+ ts = datapoint.timestamp
40
+ dp = datapoints[ts.to_i]
41
+ dp.value = datapoint_to_value(datapoint)
42
+ dp.unit = datapoint.unit
43
+ dp.freeze
44
+ end
45
+
46
+ OpenStruct.new(
47
+ queue_name: @queue_name,
48
+ start_time: start_time,
49
+ end_time: end_time,
50
+ metric: resp.label,
51
+ stats: stats,
52
+ label: "#{resp.label}(#{stats})",
53
+ datapoints: datapoints.sort_by{|k,v| k}.map(&:last)
54
+ ).freeze
55
+ end
56
+
57
+ private
58
+ CLIENT_OPTIONS = [:region, :access_key_id, :secret_access_key ]
59
+
60
+ STATS_MAXIMUM_GROUP = %w{
61
+ ApproximateNumberOfMessagesDelayed
62
+ ApproximateNumberOfMessagesNotVisible
63
+ ApproximateNumberOfMessagesVisible
64
+ }
65
+
66
+ STATS_AVERAGE_GROUP = %w{
67
+ SentMessageSize
68
+ }
69
+
70
+ STATS_SUM_GROUP = %w{
71
+ NumberOfEmptyReceives
72
+ NumberOfMessagesDeleted
73
+ NumberOfMessagesReceived
74
+ NumberOfMessagesSent
75
+ }
76
+
77
+ def default_stats(metric)
78
+ case metric
79
+ when *STATS_MAXIMUM_GROUP
80
+ "Maximum"
81
+ when *STATS_AVERAGE_GROUP
82
+ "Average"
83
+ when *STATS_SUM_GROUP
84
+ "Sum"
85
+ else
86
+ raise ArgumentError, "#{metric} is unknown metric name"
87
+ end
88
+ end
89
+
90
+ def datapoint_to_value(dp)
91
+ dp.sample_count || dp.average || dp.sum || dp.minimum || dp.maximum
92
+ end
93
+
94
+ def sec_truncate(t)
95
+ Time.new(t.year, t.mon, t.day, t.hour, t.min, nil, t.utc_offset)
96
+ end
97
+
98
+ def default_datapoints(start_time, end_time, period)
99
+ stime = sec_truncate(start_time)
100
+ datapoints = {}
101
+ ((end_time - start_time) / period).to_i.times do |i|
102
+ ts = stime + i * period
103
+ datapoints[ts.to_i] = OpenStruct.new(timestamp: ts, value: 0)
104
+ end
105
+ datapoints
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,24 @@
1
+ module Ruboty
2
+ module SqsMonitor
3
+ module Aws
4
+ class Sqs
5
+ def initialize(name, options)
6
+ @name = name
7
+
8
+ opts = options.select {|k, v| CLIENT_OPTIONS.include? k.to_sym }
9
+ @client = ::Aws::SQS::Client.new(opts)
10
+ end
11
+
12
+ def exists?
13
+ result = @client.list_queues(queue_name_prefix: @name)
14
+ !!result.queue_urls.find {|url| url.to_s.end_with? @name }
15
+ rescue
16
+ false
17
+ end
18
+
19
+ private
20
+ CLIENT_OPTIONS = [:region, :access_key_id, :secret_access_key ]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module SqsMonitor
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruboty/sqs_monitor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruboty-sqs_monitor"
8
+ spec.version = Ruboty::SqsMonitor::VERSION
9
+ spec.authors = ["arukoh"]
10
+ spec.email = ["arukoh10@gmail.com"]
11
+
12
+ spec.summary = %q{Monitor AWS SQS via Ruboty.}
13
+ spec.description = %q{Monitor AWS SQS via Ruboty.}
14
+ spec.homepage = "https://github.com/arukoh/ruboty-sqs_monitor"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_dependency "ruboty"
26
+ spec.add_dependency "aws-sdk"
27
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-sqs_monitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - arukoh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruboty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Monitor AWS SQS via Ruboty.
70
+ email:
71
+ - arukoh10@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/ruboty/handlers/sqs_monitor.rb
87
+ - lib/ruboty/sqs_monitor.rb
88
+ - lib/ruboty/sqs_monitor/actions/add.rb
89
+ - lib/ruboty/sqs_monitor/actions/base.rb
90
+ - lib/ruboty/sqs_monitor/actions/delete.rb
91
+ - lib/ruboty/sqs_monitor/actions/list.rb
92
+ - lib/ruboty/sqs_monitor/actions/state.rb
93
+ - lib/ruboty/sqs_monitor/actions/state_trend.rb
94
+ - lib/ruboty/sqs_monitor/aws.rb
95
+ - lib/ruboty/sqs_monitor/aws/cloudwatch.rb
96
+ - lib/ruboty/sqs_monitor/aws/sqs.rb
97
+ - lib/ruboty/sqs_monitor/version.rb
98
+ - ruboty-sqs_monitor.gemspec
99
+ homepage: https://github.com/arukoh/ruboty-sqs_monitor
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.4.5.1
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Monitor AWS SQS via Ruboty.
123
+ test_files: []