riemann-tools 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +11 -0
  3. data/.github/workflows/ci.yml +15 -0
  4. data/.github/workflows/codeql-analysis.yml +72 -0
  5. data/.gitignore +2 -0
  6. data/.rubocop.yml +40 -0
  7. data/.ruby-version +1 -0
  8. data/CHANGELOG.md +62 -2
  9. data/README.markdown +8 -24
  10. data/Rakefile +14 -5
  11. data/SECURITY.md +42 -0
  12. data/bin/riemann-apache-status +3 -94
  13. data/bin/riemann-bench +4 -67
  14. data/bin/riemann-cloudant +3 -54
  15. data/bin/riemann-consul +3 -102
  16. data/bin/riemann-dir-files-count +3 -51
  17. data/bin/riemann-dir-space +3 -51
  18. data/bin/riemann-diskstats +3 -91
  19. data/bin/riemann-fd +4 -63
  20. data/bin/riemann-freeswitch +4 -116
  21. data/bin/riemann-haproxy +3 -54
  22. data/bin/riemann-health +3 -344
  23. data/bin/riemann-kvminstance +4 -19
  24. data/bin/riemann-memcached +3 -33
  25. data/bin/riemann-net +3 -105
  26. data/bin/riemann-nginx-status +3 -80
  27. data/bin/riemann-ntp +3 -34
  28. data/bin/riemann-portcheck +3 -37
  29. data/bin/riemann-proc +3 -104
  30. data/bin/riemann-varnish +3 -50
  31. data/bin/riemann-wrapper +75 -0
  32. data/bin/riemann-zookeeper +3 -37
  33. data/lib/riemann/tools/apache_status.rb +107 -0
  34. data/lib/riemann/tools/bench.rb +72 -0
  35. data/lib/riemann/tools/cloudant.rb +57 -0
  36. data/lib/riemann/tools/consul_health.rb +107 -0
  37. data/lib/riemann/tools/dir_files_count.rb +56 -0
  38. data/lib/riemann/tools/dir_space.rb +56 -0
  39. data/lib/riemann/tools/diskstats.rb +94 -0
  40. data/lib/riemann/tools/fd.rb +81 -0
  41. data/lib/riemann/tools/freeswitch.rb +119 -0
  42. data/lib/riemann/tools/haproxy.rb +59 -0
  43. data/lib/riemann/tools/health.rb +478 -0
  44. data/lib/riemann/tools/kvm.rb +23 -0
  45. data/lib/riemann/tools/memcached.rb +38 -0
  46. data/lib/riemann/tools/net.rb +105 -0
  47. data/lib/riemann/tools/nginx_status.rb +86 -0
  48. data/lib/riemann/tools/ntp.rb +42 -0
  49. data/lib/riemann/tools/portcheck.rb +45 -0
  50. data/lib/riemann/tools/proc.rb +109 -0
  51. data/lib/riemann/tools/riemann_client_wrapper.rb +43 -0
  52. data/lib/riemann/tools/uptime_parser.tab.rb +323 -0
  53. data/lib/riemann/tools/varnish.rb +55 -0
  54. data/lib/riemann/tools/version.rb +1 -1
  55. data/lib/riemann/tools/zookeeper.rb +40 -0
  56. data/lib/riemann/tools.rb +31 -52
  57. data/riemann-tools.gemspec +8 -2
  58. data/tools/riemann-aws/{Rakefile.rb → Rakefile} +8 -9
  59. data/tools/riemann-aws/bin/riemann-aws-billing +4 -83
  60. data/tools/riemann-aws/bin/riemann-aws-rds-status +4 -50
  61. data/tools/riemann-aws/bin/riemann-aws-sqs-status +4 -40
  62. data/tools/riemann-aws/bin/riemann-aws-status +4 -67
  63. data/tools/riemann-aws/bin/riemann-elb-metrics +4 -163
  64. data/tools/riemann-aws/bin/riemann-s3-list +4 -78
  65. data/tools/riemann-aws/bin/riemann-s3-status +4 -95
  66. data/tools/riemann-aws/lib/riemann/tools/aws/billing.rb +87 -0
  67. data/tools/riemann-aws/lib/riemann/tools/aws/elb_metrics.rb +163 -0
  68. data/tools/riemann-aws/lib/riemann/tools/aws/rds_status.rb +63 -0
  69. data/tools/riemann-aws/lib/riemann/tools/aws/s3_list.rb +82 -0
  70. data/tools/riemann-aws/lib/riemann/tools/aws/s3_status.rb +97 -0
  71. data/tools/riemann-aws/lib/riemann/tools/aws/sqs_status.rb +45 -0
  72. data/tools/riemann-aws/lib/riemann/tools/aws/status.rb +74 -0
  73. data/tools/riemann-chronos/{Rakefile.rb → Rakefile} +8 -9
  74. data/tools/riemann-chronos/bin/riemann-chronos +3 -139
  75. data/tools/riemann-chronos/lib/riemann/tools/chronos.rb +157 -0
  76. data/tools/riemann-docker/{Rakefile.rb → Rakefile} +7 -8
  77. data/tools/riemann-docker/bin/riemann-docker +4 -213
  78. data/tools/riemann-docker/lib/riemann/tools/docker.rb +200 -0
  79. data/tools/riemann-elasticsearch/{Rakefile.rb → Rakefile} +8 -9
  80. data/tools/riemann-elasticsearch/bin/riemann-elasticsearch +3 -161
  81. data/tools/riemann-elasticsearch/lib/riemann/tools/elasticsearch.rb +170 -0
  82. data/tools/riemann-marathon/{Rakefile.rb → Rakefile} +8 -9
  83. data/tools/riemann-marathon/bin/riemann-marathon +3 -142
  84. data/tools/riemann-marathon/lib/riemann/tools/marathon.rb +159 -0
  85. data/tools/riemann-mesos/{Rakefile.rb → Rakefile} +8 -9
  86. data/tools/riemann-mesos/bin/riemann-mesos +3 -126
  87. data/tools/riemann-mesos/lib/riemann/tools/mesos.rb +142 -0
  88. data/tools/riemann-munin/{Rakefile.rb → Rakefile} +7 -8
  89. data/tools/riemann-munin/bin/riemann-munin +3 -32
  90. data/tools/riemann-munin/lib/riemann/tools/munin.rb +37 -0
  91. data/tools/riemann-rabbitmq/{Rakefile.rb → Rakefile} +8 -9
  92. data/tools/riemann-rabbitmq/bin/riemann-rabbitmq +3 -264
  93. data/tools/riemann-rabbitmq/lib/riemann/tools/rabbitmq.rb +269 -0
  94. data/tools/riemann-riak/{Rakefile.rb → Rakefile} +7 -8
  95. data/tools/riemann-riak/bin/riemann-riak +3 -326
  96. data/tools/riemann-riak/bin/riemann-riak-keys +0 -1
  97. data/tools/riemann-riak/bin/riemann-riak-ring +0 -1
  98. data/tools/riemann-riak/lib/riemann/tools/riak.rb +317 -0
  99. metadata +112 -16
  100. data/.travis.yml +0 -31
  101. data/tools/riemann-riak/riak_status/key_count.erl +0 -13
  102. data/tools/riemann-riak/riak_status/riak_status.rb +0 -152
  103. data/tools/riemann-riak/riak_status/ringready.erl +0 -9
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Riemann
4
4
  module Tools # :nodoc:
5
- VERSION = '1.0.0'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'riemann/tools'
4
+
5
+ # Gathers zookeeper STATS and submits them to Riemann.
6
+ module Riemann
7
+ module Tools
8
+ class Zookeeper
9
+ include Riemann::Tools
10
+ require 'socket'
11
+
12
+ opt :zookeeper_host, 'Zookeeper hostname', default: 'localhost'
13
+ opt :zookeeper_port, 'Zookeeper port', default: 2181
14
+
15
+ def tick
16
+ sock = TCPSocket.new(opts[:zookeeper_host], opts[:zookeeper_port])
17
+ sock.sync = true
18
+ sock.print('mntr')
19
+ sock.flush
20
+
21
+ loop do
22
+ stats = sock.gets
23
+
24
+ break if stats.nil?
25
+
26
+ m = stats.match(/^(\w+)\t+(.*)/)
27
+
28
+ report(
29
+ host: opts[:zookeeper_host].dup,
30
+ service: "zookeeper #{m[1]}",
31
+ metric: m[2].to_f,
32
+ state: 'ok',
33
+ tags: ['zookeeper'],
34
+ )
35
+ end
36
+ sock.close
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/riemann/tools.rb CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Riemann
2
4
  module Tools
3
5
  require 'optimist'
4
- require 'riemann/client'
6
+ require 'riemann/tools/riemann_client_wrapper'
5
7
 
6
8
  def self.included(base)
7
9
  base.instance_eval do
@@ -9,36 +11,36 @@ module Riemann
9
11
  new.run
10
12
  end
11
13
 
12
- def opt(*a)
13
- a.unshift :opt
14
+ def opt(*args)
15
+ args.unshift :opt
14
16
  @opts ||= []
15
- @opts << a
17
+ @opts << args
16
18
  end
17
19
 
18
20
  def options
19
21
  p = Optimist::Parser.new
20
22
  @opts.each do |o|
21
- p.send *o
23
+ p.send(*o)
22
24
  end
23
- Optimist::with_standard_exception_handling(p) do
25
+ Optimist.with_standard_exception_handling(p) do
24
26
  p.parse ARGV
25
27
  end
26
28
  end
27
29
 
28
- opt :host, "Riemann host", :default => '127.0.0.1'
29
- opt :port, "Riemann port", :default => 5555
30
- opt :event_host, "Event hostname", :type => String
31
- opt :interval, "Seconds between updates", :default => 5
32
- opt :tag, "Tag to add to events", :type => String, :multi => true
33
- opt :ttl, "TTL for events", :type => Integer
34
- opt :attribute, "Attribute to add to the event", :type => String, :multi => true
35
- opt :timeout, "Timeout (in seconds) when waiting for acknowledgements", :default => 30
36
- opt :tcp, "Use TCP transport instead of UDP (improves reliability, slight overhead.", :default => true
37
- opt :tls, "Use TLS for securing traffic", :default => false
38
- opt :tls_key, "TLS Key to use when using TLS", :type => String
39
- opt :tls_cert, "TLS Certificate to use when using TLS", :type => String
40
- opt :tls_ca_cert, "Trusted CA Certificate when using TLS", :type => String
41
- opt :tls_verify, "Verify TLS peer when using TLS", :default => true
30
+ opt :host, 'Riemann host', default: '127.0.0.1'
31
+ opt :port, 'Riemann port', default: 5555
32
+ opt :event_host, 'Event hostname', type: String
33
+ opt :interval, 'Seconds between updates', default: 5
34
+ opt :tag, 'Tag to add to events', type: String, multi: true
35
+ opt :ttl, 'TTL for events', type: Integer
36
+ opt :attribute, 'Attribute to add to the event', type: String, multi: true
37
+ opt :timeout, 'Timeout (in seconds) when waiting for acknowledgements', default: 30
38
+ opt :tcp, 'Use TCP transport instead of UDP (improves reliability, slight overhead.', default: true
39
+ opt :tls, 'Use TLS for securing traffic', default: false
40
+ opt :tls_key, 'TLS Key to use when using TLS', type: String
41
+ opt :tls_cert, 'TLS Certificate to use when using TLS', type: String
42
+ opt :tls_ca_cert, 'Trusted CA Certificate when using TLS', type: String
43
+ opt :tls_verify, 'Verify TLS peer when using TLS', default: true
42
44
  end
43
45
  end
44
46
 
@@ -46,14 +48,12 @@ module Riemann
46
48
  def options
47
49
  @options ||= self.class.options
48
50
  end
49
- alias :opts :options
51
+ alias opts options
50
52
 
51
53
  def attributes
52
54
  @attributes ||= Hash[options[:attribute].map do |attr|
53
- k,v = attr.split(/=/)
54
- if k and v
55
- [k,v]
56
- end
55
+ k, v = attr.split(/=/)
56
+ [k, v] if k && v
57
57
  end]
58
58
  end
59
59
 
@@ -65,45 +65,25 @@ module Riemann
65
65
 
66
66
  event[:ttl] ||= (options[:ttl] || (options[:interval] * 2))
67
67
 
68
- if options[:event_host]
69
- event[:host] = options[:event_host].dup
70
- end
68
+ event[:host] = options[:event_host].dup if options[:event_host]
71
69
 
72
70
  event = event.merge(attributes)
73
71
 
74
72
  riemann << event
75
73
  end
76
74
 
77
- def new_riemann_client
78
- r = Riemann::Client.new(
79
- :host => options[:host],
80
- :port => options[:port],
81
- :timeout => options[:timeout],
82
- :ssl => options[:tls],
83
- :key_file => options[:tls_key],
84
- :cert_file => options[:tls_cert],
85
- :ca_file => options[:tls_ca_cert],
86
- :ssl_verify => options[:tls_verify],
87
- )
88
- if options[:tcp] || options[:tls]
89
- r.tcp
90
- else
91
- r
92
- end
93
- end
94
-
95
75
  def riemann
96
- @riemann ||= new_riemann_client
76
+ @riemann ||= RiemannClientWrapper.instance.configure(options)
97
77
  end
98
- alias :r :riemann
78
+ alias r riemann
99
79
 
100
80
  def run
101
81
  t0 = Time.now
102
82
  loop do
103
83
  begin
104
84
  tick
105
- rescue => e
106
- $stderr.puts "#{e.class} #{e}\n#{e.backtrace.join "\n"}"
85
+ rescue StandardError => e
86
+ warn "#{e.class} #{e}\n#{e.backtrace.join "\n"}"
107
87
  end
108
88
 
109
89
  # Sleep.
@@ -111,7 +91,6 @@ module Riemann
111
91
  end
112
92
  end
113
93
 
114
- def tick
115
- end
94
+ def tick; end
116
95
  end
117
96
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = 'Collection of utilities which submit events to Riemann,'
13
13
  spec.homepage = 'https://github.com/aphyr/riemann-tools'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
16
16
 
17
17
  spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
18
18
 
@@ -23,7 +23,9 @@ Gem::Specification.new do |spec|
23
23
  # Specify which files should be added to the gem when it is released.
24
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
25
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } -
27
+ ['lib/riemann/tools/uptime_parser.y'] +
28
+ ['lib/riemann/tools/uptime_parser.tab.rb']
27
29
  end
28
30
  spec.bindir = 'bin'
29
31
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -34,6 +36,10 @@ Gem::Specification.new do |spec|
34
36
  spec.add_runtime_dependency 'riemann-client', '~> 1.0'
35
37
 
36
38
  spec.add_development_dependency 'github_changelog_generator'
39
+ spec.add_development_dependency 'racc'
37
40
  spec.add_development_dependency 'rake'
38
41
  spec.add_development_dependency 'rspec'
42
+ spec.add_development_dependency 'rubocop'
43
+ spec.add_development_dependency 'rubocop-rake'
44
+ spec.add_development_dependency 'rubocop-rspec'
39
45
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
  require 'rubygems/package_task'
3
5
  require 'rdoc/task'
@@ -9,10 +11,8 @@ ENV['COPYFILE_DISABLE'] = 'true'
9
11
 
10
12
  # Gemspec
11
13
  gemspec = Gem::Specification.new do |s|
12
- s.rubyforge_project = 'riemann-aws'
13
-
14
14
  s.name = 'riemann-aws'
15
- s.version = '0.1.4'
15
+ s.version = '0.1.5'
16
16
  s.author = 'Kyle Kingsbury'
17
17
  s.email = 'aphyr@aphyr.com'
18
18
  s.homepage = 'https://github.com/riemann/riemann-tools'
@@ -20,15 +20,14 @@ gemspec = Gem::Specification.new do |s|
20
20
  s.summary = 'Submits AWS stats to riemann.'
21
21
  s.license = 'MIT'
22
22
 
23
- s.add_dependency 'riemann-tools', '>= 0.2.13'
24
- s.add_dependency 'fog', '>= 1.4.0'
25
- s.add_dependency 'json'
23
+ s.add_runtime_dependency 'riemann-tools', '~> 1.0', '>= 1.1.1'
24
+ s.add_runtime_dependency 'fog-aws', '~> 3.14', '>= 3.14.0'
25
+ s.add_runtime_dependency 'json', '~> 2.6', '>=2.6.2'
26
26
 
27
- s.files = FileList['bin/*', 'LICENSE', 'README.md'].to_a
27
+ s.files = FileList['bin/*', 'lib/**/*.rb', 'LICENSE', 'README.md'].to_a
28
28
  s.executables |= Dir.entries('bin/')
29
- s.has_rdoc = false
30
29
 
31
- s.required_ruby_version = '>= 1.8.7'
30
+ s.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
32
31
  end
33
32
 
34
33
  Gem::PackageTask.new gemspec do |p|
@@ -1,87 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
3
 
4
- require 'riemann/tools'
4
+ Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- $0 = __FILE__
6
+ require 'riemann/tools/aws/billing'
7
7
 
8
- class Riemann::Tools::AWSBilling
9
- include Riemann::Tools
10
- require 'fog'
11
-
12
- opt :fog_credentials_file, "Fog credentials file", :type => String
13
- opt :fog_credential, "Fog credentials to use", :type => String
14
-
15
- opt :access_key, "AWS access key", :type => String
16
- opt :secret_key, "Secret access key", :type => String
17
- opt :services, "AWS services: AmazonEC2 AmazonS3 AWSDataTransfer", :type => :strings, :multi => true, :default => ["AmazonEC2", "AmazonS3", "AWSDataTransfer"]
18
-
19
- opt :time_start, "Start time in seconds of the metrics period (2hrs ago default)", :type => Integer, :default => 7200
20
- opt :time_end, "End time in seconds of the metrics period ", :type => Integer, :default => 60
21
-
22
-
23
- def initialize
24
- if options[:fog_credentials_file]
25
- Fog.credentials_path = opts[:fog_credentials_file]
26
- Fog.credential = opts[:fog_credential].to_sym
27
- @cloudwatch = Fog::AWS::CloudWatch.new
28
- else
29
- if opts.has_key?('secret_key') and opts.has_key?('access_key')
30
- creds = {
31
- :aws_secret_access_key => opts[:secret_key],
32
- :aws_access_key_id => opts[:access_key]
33
- }
34
- else
35
- creds = { :use_iam_profile => true }
36
- end
37
- @cloudwatch = Fog::AWS::CloudWatch.new(creds)
38
- end
39
- @start_time = (Time.now.utc - opts[:time_start]).iso8601
40
- @end_time = (Time.now.utc - opts[:time_end]).iso8601
41
- end
42
-
43
- def tick
44
- opts[:services].each do |service|
45
- data = @cloudwatch.get_metric_statistics({
46
- 'Statistics' => ["Maximum"],
47
- 'StartTime' => @start_time,
48
- 'EndTime' => @end_time,
49
- 'Period' => 3600,
50
- 'Unit' => "None",
51
- 'MetricName' => "EstimatedCharges",
52
- 'Namespace' => "AWS/Billing",
53
- 'Dimensions' => [
54
- {
55
- 'Name' => "ServiceName",
56
- 'Value' => service
57
- },
58
- {
59
- 'Name' => "Currency",
60
- 'Value' => "USD"
61
- }
62
- ]
63
- }).body['GetMetricStatisticsResult']['Datapoints']
64
-
65
-
66
- data.each do |metrics|
67
- name = "AWScloudwatch.Billing." + service
68
- value = metrics["Maximum"]
69
- timestamp = metrics["Timestamp"].to_i
70
-
71
- event = {
72
- host: nil,
73
- service: name,
74
- time: timestamp,
75
- description: "AWS Estimate Charges for #{service}",
76
- tags: ["aws_billing"],
77
- state: "ok",
78
- metric: value
79
- }
80
-
81
- report event
82
- end
83
- end
84
- end
85
- end
86
-
87
- Riemann::Tools::AWSBilling.run
8
+ Riemann::Tools::Aws::Billing.run
@@ -1,54 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
3
 
4
- require 'riemann/tools'
4
+ Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- $0 = __FILE__ # Let's not expose our AWS keys in the process list
6
+ require 'riemann/tools/aws/rds_status'
7
7
 
8
- class Riemann::Tools::AWS
9
- include Riemann::Tools
10
- require 'fog'
11
- require 'date'
12
- require 'time'
13
- require 'json'
14
-
15
- opt :access_key, "AWS access key", :type => String
16
- opt :secret_key, "Secret access key", :type => String
17
- opt :region, "AWS region", :type => String, :default => 'eu-west-1'
18
- opt :dbinstance_identifier, "DBInstanceIdentifier", :type => String
19
- def initialize
20
- abort "FATAL: specify a DB instance name, see --help for usage" unless opts[:dbinstance_identifier]
21
- if opts[:access_key] and opts[:secret_key]
22
- creds = {
23
- :aws_access_key_id => opts[:access_key],
24
- :aws_secret_access_key => opts[:secret_key]
25
- }
26
- else
27
- creds = { :use_iam_profile => true }
28
- end
29
- creds['region'] = opts[:region]
30
- @cloudwatch = Fog::AWS::CloudWatch.new(creds)
31
- end
32
-
33
- def tick
34
- time = Time.new
35
- ['DatabaseConnections', 'FreeableMemory', 'FreeStorageSpace', 'NetworkReceiveThroughput', 'NetworkTransmitThroughput', 'ReadThroughput', 'CPUUtilization'].each do |metric|
36
- result = @cloudwatch.get_metric_statistics({"Namespace" => 'AWS/RDS', "MetricName" => "#{metric}", "Statistics" => 'Average', "Dimensions" => [{"Name" => "DBInstanceIdentifier", "Value" => "#{opts[:dbinstance_identifier]}"}], "StartTime" => (time-120).to_time.iso8601, "EndTime" => time.to_time.iso8601, "Period" => 60})
37
- metricsResult = result.data[:body]['GetMetricStatisticsResult']
38
- if (metricsResult['Datapoints'].length>0)
39
- datapoint = metricsResult['Datapoints'][0]
40
- ev = {:metric => datapoint['Average'],
41
- :service => "#{opts[:dbinstance_identifier]}.#{metric} (#{datapoint['Unit']})",
42
- :description => JSON.dump(metricsResult),
43
- :state => "ok",
44
- :ttl => 300}
45
-
46
-
47
- report ev
48
- end
49
-
50
- end
51
- end
52
- end
53
-
54
- Riemann::Tools::AWS.run
8
+ Riemann::Tools::Aws::RdsStatus.run
@@ -1,44 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
3
 
4
- require 'riemann/tools'
4
+ Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- $0 = __FILE__ # Let's not expose our AWS keys in the process list
6
+ require 'riemann/tools/aws/sqs_status'
7
7
 
8
- class Riemann::Tools::AWS
9
- include Riemann::Tools
10
- require 'fog'
11
-
12
- opt :access_key, "AWS access key", :type => String
13
- opt :secret_key, "Secret access key", :type => String
14
- opt :region, "AWS region", :type => String, :default => 'us-east-1'
15
- opt :queue, "SQS Queue name", :type => String
16
- def initialize
17
- if opts.has_key?('access_key') and opts.has_key?('secret_key')
18
- creds = {
19
- :aws_access_key_id => opts[:access_key],
20
- :aws_secret_access_key => opts[:secret_key]
21
- }
22
- else
23
- creds = { :use_iam_profile => true }
24
- end
25
- creds['region'] = opts[:region]
26
- @sqs = Fog::AWS::SQS.new(creds)
27
- response = @sqs.list_queues({'QueueNamePrefix' => opts[:queue]})
28
- @queue_url = response[:body]['QueueUrls'].first
29
- end
30
-
31
- def tick
32
- response = @sqs.get_queue_attributes(@queue_url, 'All')
33
- ['ApproximateNumberOfMessages', 'ApproximateNumberOfMessagesNotVisible'].each do |attr|
34
- msg = {
35
- metric: response[:body]['Attributes'][attr],
36
- service: "#{opts[:queue]} #{attr}",
37
- state: 'ok'
38
- }
39
- report msg
40
- end
41
- end
42
- end
43
-
44
- Riemann::Tools::AWS.run
8
+ Riemann::Tools::Aws::SqsStatus.run
@@ -1,71 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
3
 
4
- require 'riemann/tools'
4
+ Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- $0 = __FILE__ # Let's not expose our AWS keys in the process list
6
+ require 'riemann/tools/aws/status'
7
7
 
8
- class Riemann::Tools::AWS
9
- include Riemann::Tools
10
- require 'fog'
11
- require 'date'
12
-
13
- opt :access_key, "AWS access key", :type => String
14
- opt :secret_key, "Secret access key", :type => String
15
- opt :region, "AWS region", :type => String, :default => 'eu-west-1'
16
-
17
- opt :retirement_critical, "Number of days before retirement. Defaults to 2", :default => 2
18
- opt :event_warning, "Number of days before event. Defaults to nil (i.e. when the event appears)", :default => nil
19
-
20
- def initialize
21
- if opts.has_key?('secret_key') and opts.has_key?('access_key')
22
- creds = {
23
- :aws_secret_access_key => opts[:secret_key],
24
- :aws_access_key_id => opts[:access_key]
25
- }
26
- else
27
- creds = { :use_iam_profile => true }
28
- end
29
- creds['region'] = opts[:region]
30
- creds['provider'] = 'AWS'
31
- @compute = Fog::Compute.new(creds)
32
- end
33
-
34
- def tick
35
- instance_status = @compute.describe_instance_status.body["instanceStatusSet"]
36
- status = instance_status.inject({}) do |acc,i|
37
- acc[i.delete("instanceId")] = i
38
- acc
39
- end
40
-
41
- hosts = @compute.servers.select { |s| s.state == "running" }
42
- inject([status, {}]) do |(status, acc), host|
43
- acc[host.private_dns_name] = status.delete(host.id); [status, acc]
44
- end[1]
45
-
46
- hosts.each do |host, status|
47
- status['eventsSet'].each do |event|
48
- before, after = ['notBefore', 'notAfter'].map { |k| Date.parse event[k].to_s if event[k] }
49
-
50
- ev = {:host => host,
51
- :service => "aws_instance_status",
52
- :description => "#{event['code']}\n\nstart #{event['notBefore']}\nend #{event['notAfter']}\n\n#{event['description']}",
53
- :state => "ok",
54
- :ttl => 300}
55
-
56
- ev2 = if (event['code'] == 'instance-retirement') and
57
- Date.today >= before-opts[:retirement_critical]
58
- {:state => "critical"}
59
- elsif opts[:event_warning] and Date.today >= before-opts[:event_warning]
60
- {:state => "warning"}
61
- else
62
- {:state => "warning"}
63
- end
64
-
65
- report ev.merge(ev2)
66
- end
67
- end
68
- end
69
- end
70
-
71
- Riemann::Tools::AWS.run
8
+ Riemann::Tools::Aws::Status.run