riemann-tools 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +2 -0
  3. data/.gitignore +2 -0
  4. data/.rubocop.yml +8 -0
  5. data/.ruby-version +1 -0
  6. data/CHANGELOG.md +25 -2
  7. data/Rakefile +10 -3
  8. data/bin/riemann-apache-status +1 -106
  9. data/bin/riemann-bench +2 -70
  10. data/bin/riemann-cloudant +1 -56
  11. data/bin/riemann-consul +1 -106
  12. data/bin/riemann-dir-files-count +1 -55
  13. data/bin/riemann-dir-space +1 -55
  14. data/bin/riemann-diskstats +1 -92
  15. data/bin/riemann-fd +2 -81
  16. data/bin/riemann-freeswitch +2 -119
  17. data/bin/riemann-haproxy +1 -58
  18. data/bin/riemann-health +0 -2
  19. data/bin/riemann-kvminstance +2 -22
  20. data/bin/riemann-memcached +1 -37
  21. data/bin/riemann-net +0 -2
  22. data/bin/riemann-nginx-status +1 -85
  23. data/bin/riemann-ntp +0 -2
  24. data/bin/riemann-portcheck +1 -44
  25. data/bin/riemann-proc +1 -108
  26. data/bin/riemann-varnish +1 -54
  27. data/bin/riemann-wrapper +75 -0
  28. data/bin/riemann-zookeeper +1 -39
  29. data/lib/riemann/tools/apache_status.rb +107 -0
  30. data/lib/riemann/tools/bench.rb +72 -0
  31. data/lib/riemann/tools/cloudant.rb +57 -0
  32. data/lib/riemann/tools/consul_health.rb +107 -0
  33. data/lib/riemann/tools/dir_files_count.rb +56 -0
  34. data/lib/riemann/tools/dir_space.rb +56 -0
  35. data/lib/riemann/tools/diskstats.rb +94 -0
  36. data/lib/riemann/tools/fd.rb +81 -0
  37. data/lib/riemann/tools/freeswitch.rb +119 -0
  38. data/lib/riemann/tools/haproxy.rb +59 -0
  39. data/lib/riemann/tools/health.rb +150 -19
  40. data/lib/riemann/tools/kvm.rb +23 -0
  41. data/lib/riemann/tools/memcached.rb +38 -0
  42. data/lib/riemann/tools/net.rb +2 -1
  43. data/lib/riemann/tools/nginx_status.rb +86 -0
  44. data/lib/riemann/tools/ntp.rb +1 -0
  45. data/lib/riemann/tools/portcheck.rb +45 -0
  46. data/lib/riemann/tools/proc.rb +109 -0
  47. data/lib/riemann/tools/riemann_client_wrapper.rb +43 -0
  48. data/lib/riemann/tools/uptime_parser.tab.rb +323 -0
  49. data/lib/riemann/tools/varnish.rb +55 -0
  50. data/lib/riemann/tools/version.rb +1 -1
  51. data/lib/riemann/tools/zookeeper.rb +40 -0
  52. data/lib/riemann/tools.rb +2 -20
  53. data/riemann-tools.gemspec +4 -1
  54. data/tools/riemann-aws/Rakefile +6 -9
  55. data/tools/riemann-aws/bin/riemann-aws-billing +2 -87
  56. data/tools/riemann-aws/bin/riemann-aws-rds-status +2 -62
  57. data/tools/riemann-aws/bin/riemann-aws-sqs-status +2 -44
  58. data/tools/riemann-aws/bin/riemann-aws-status +2 -77
  59. data/tools/riemann-aws/bin/riemann-elb-metrics +2 -162
  60. data/tools/riemann-aws/bin/riemann-s3-list +2 -81
  61. data/tools/riemann-aws/bin/riemann-s3-status +2 -96
  62. data/tools/riemann-aws/lib/riemann/tools/aws/billing.rb +87 -0
  63. data/tools/riemann-aws/lib/riemann/tools/aws/elb_metrics.rb +163 -0
  64. data/tools/riemann-aws/lib/riemann/tools/aws/rds_status.rb +63 -0
  65. data/tools/riemann-aws/lib/riemann/tools/aws/s3_list.rb +82 -0
  66. data/tools/riemann-aws/lib/riemann/tools/aws/s3_status.rb +97 -0
  67. data/tools/riemann-aws/lib/riemann/tools/aws/sqs_status.rb +45 -0
  68. data/tools/riemann-aws/lib/riemann/tools/aws/status.rb +74 -0
  69. data/tools/riemann-chronos/Rakefile +6 -9
  70. data/tools/riemann-chronos/bin/riemann-chronos +1 -154
  71. data/tools/riemann-chronos/lib/riemann/tools/chronos.rb +157 -0
  72. data/tools/riemann-docker/Rakefile +5 -8
  73. data/tools/riemann-docker/bin/riemann-docker +2 -200
  74. data/tools/riemann-docker/lib/riemann/tools/docker.rb +200 -0
  75. data/tools/riemann-elasticsearch/Rakefile +6 -9
  76. data/tools/riemann-elasticsearch/bin/riemann-elasticsearch +1 -167
  77. data/tools/riemann-elasticsearch/lib/riemann/tools/elasticsearch.rb +170 -0
  78. data/tools/riemann-marathon/Rakefile +6 -9
  79. data/tools/riemann-marathon/bin/riemann-marathon +1 -156
  80. data/tools/riemann-marathon/lib/riemann/tools/marathon.rb +159 -0
  81. data/tools/riemann-mesos/Rakefile +6 -9
  82. data/tools/riemann-mesos/bin/riemann-mesos +1 -139
  83. data/tools/riemann-mesos/lib/riemann/tools/mesos.rb +142 -0
  84. data/tools/riemann-munin/Rakefile +5 -8
  85. data/tools/riemann-munin/bin/riemann-munin +1 -36
  86. data/tools/riemann-munin/lib/riemann/tools/munin.rb +37 -0
  87. data/tools/riemann-rabbitmq/Rakefile +6 -9
  88. data/tools/riemann-rabbitmq/bin/riemann-rabbitmq +1 -266
  89. data/tools/riemann-rabbitmq/lib/riemann/tools/rabbitmq.rb +269 -0
  90. data/tools/riemann-riak/Rakefile +5 -8
  91. data/tools/riemann-riak/bin/riemann-riak +1 -316
  92. data/tools/riemann-riak/bin/riemann-riak-keys +0 -1
  93. data/tools/riemann-riak/bin/riemann-riak-ring +0 -1
  94. data/tools/riemann-riak/lib/riemann/tools/riak.rb +317 -0
  95. metadata +57 -10
  96. data/.travis.yml +0 -31
  97. data/tools/riemann-riak/riak_status/key_count.erl +0 -13
  98. data/tools/riemann-riak/riak_status/riak_status.rb +0 -152
  99. data/tools/riemann-riak/riak_status/ringready.erl +0 -9
@@ -3,48 +3,6 @@
3
3
 
4
4
  Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- require 'riemann/tools'
6
+ require 'riemann/tools/aws/sqs_status'
7
7
 
8
- $0 = __FILE__ # Let's not expose our AWS keys in the process list
9
-
10
- module Riemann
11
- module Tools
12
- class AWS
13
- include Riemann::Tools
14
- require 'fog'
15
-
16
- opt :access_key, 'AWS access key', type: String
17
- opt :secret_key, 'Secret access key', type: String
18
- opt :region, 'AWS region', type: String, default: 'us-east-1'
19
- opt :queue, 'SQS Queue name', type: String
20
- def initialize
21
- creds = if opts.key?('access_key') && opts.key?('secret_key')
22
- {
23
- aws_access_key_id: opts[:access_key],
24
- aws_secret_access_key: opts[:secret_key],
25
- }
26
- else
27
- { use_iam_profile: true }
28
- end
29
- creds['region'] = opts[:region]
30
- @sqs = Fog::AWS::SQS.new(creds)
31
- response = @sqs.list_queues({ 'QueueNamePrefix' => opts[:queue] })
32
- @queue_url = response[:body]['QueueUrls'].first
33
- end
34
-
35
- def tick
36
- response = @sqs.get_queue_attributes(@queue_url, 'All')
37
- %w[ApproximateNumberOfMessages ApproximateNumberOfMessagesNotVisible].each do |attr|
38
- msg = {
39
- metric: response[:body]['Attributes'][attr],
40
- service: "#{opts[:queue]} #{attr}",
41
- state: 'ok',
42
- }
43
- report msg
44
- end
45
- end
46
- end
47
- end
48
- end
49
-
50
- Riemann::Tools::AWS.run
8
+ Riemann::Tools::Aws::SqsStatus.run
@@ -3,81 +3,6 @@
3
3
 
4
4
  Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- require 'riemann/tools'
6
+ require 'riemann/tools/aws/status'
7
7
 
8
- $0 = __FILE__ # Let's not expose our AWS keys in the process list
9
-
10
- module Riemann
11
- module Tools
12
- class AWS
13
- include Riemann::Tools
14
- require 'fog'
15
- require 'date'
16
-
17
- opt :access_key, 'AWS access key', type: String
18
- opt :secret_key, 'Secret access key', type: String
19
- opt :region, 'AWS region', type: String, default: 'eu-west-1'
20
-
21
- opt :retirement_critical, 'Number of days before retirement. Defaults to 2', default: 2
22
- opt :event_warning, 'Number of days before event. Defaults to nil (i.e. when the event appears)', default: nil
23
-
24
- def initialize
25
- creds = if opts.key?('secret_key') && opts.key?('access_key')
26
- {
27
- aws_secret_access_key: opts[:secret_key],
28
- aws_access_key_id: opts[:access_key],
29
- }
30
- else
31
- { use_iam_profile: true }
32
- end
33
- creds['region'] = opts[:region]
34
- creds['provider'] = 'AWS'
35
- @compute = Fog::Compute.new(creds)
36
- end
37
-
38
- def tick
39
- # XXX: needs work:
40
- # * instance_status and status seems unused
41
- # * where is inject comming from
42
- # It looks line the only needed line for the 2 next paragraph is the one that assigns hosts.
43
- instance_status = @compute.describe_instance_status.body['instanceStatusSet']
44
- status = instance_status.each_with_object({}) do |i, acc|
45
- acc[i.delete('instanceId')] = i
46
- end
47
-
48
- hosts = @compute.servers.select { |s| s.state == 'running' }
49
- inject([status, {}]) do |(astatus, acc), host|
50
- acc[host.private_dns_name] = astatus.delete(host.id)
51
- [astatus, acc]
52
- end[1]
53
-
54
- hosts.each do |host, host_status|
55
- host_status['eventsSet'].each do |event|
56
- before, _after = %w[notBefore notAfter].map { |k| Date.parse event[k].to_s if event[k] }
57
-
58
- ev = {
59
- host: host,
60
- service: 'aws_instance_status',
61
- description: "#{event['code']}\n\nstart #{event['notBefore']}\nend #{event['notAfter']}\n\n#{event['description']}",
62
- state: 'ok',
63
- ttl: 300,
64
- }
65
-
66
- ev2 = if (event['code'] == 'instance-retirement') &&
67
- (Date.today >= before - opts[:retirement_critical])
68
- { state: 'critical' }
69
- elsif opts[:event_warning] && (Date.today >= before - opts[:event_warning])
70
- { state: 'warning' }
71
- else
72
- { state: 'warning' }
73
- end
74
-
75
- report ev.merge(ev2)
76
- end
77
- end
78
- end
79
- end
80
- end
81
- end
82
-
83
- Riemann::Tools::AWS.run
8
+ Riemann::Tools::Aws::Status.run
@@ -3,166 +3,6 @@
3
3
 
4
4
  Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- require 'riemann/tools'
6
+ require 'riemann/tools/aws/elb_metrics'
7
7
 
8
- $0 = __FILE__
9
-
10
- module Riemann
11
- module Tools
12
- class ELBMetrics
13
- include Riemann::Tools
14
- require 'fog'
15
- require 'time'
16
-
17
- opt :fog_credentials_file, 'Fog credentials file', type: String
18
- opt :fog_credential, 'Fog credentials to use', type: String
19
- opt :aws_access, 'AWS Access Key', type: String
20
- opt :aws_secret, 'AWS Secret Key', type: String
21
- opt :aws_region, 'AWS Region', type: String, default: 'eu-west-1'
22
- opt :aws_azs, 'List of AZs to aggregate against', type: :strings, default: ['all_az']
23
- opt :elbs, 'List of ELBs to pull metrics from', type: :strings, required: true
24
-
25
- def standard_metrics
26
- # ELB metric types, from:
27
- # http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html#elb-metricscollected
28
- {
29
- 'Latency' => {
30
- 'Unit' => 'Seconds',
31
- 'Statistics' => %w[Maximum Minimum Average],
32
- },
33
- 'RequestCount' => {
34
- 'Unit' => 'Count',
35
- 'Statistics' => ['Sum'],
36
- },
37
- 'HealthyHostCount' => {
38
- 'Units' => 'Count',
39
- 'Statistics' => %w[Minimum Maximum Average],
40
- },
41
- 'UnHealthyHostCount' => {
42
- 'Units' => 'Count',
43
- 'Statistics' => %w[Minimum Maximum Average],
44
- },
45
- 'HTTPCode_ELB_4XX' => {
46
- 'Units' => 'Count',
47
- 'Statistics' => ['Sum'],
48
- },
49
- 'HTTPCode_ELB_5XX' => {
50
- 'Units' => 'Count',
51
- 'Statistics' => ['Sum'],
52
- },
53
- 'HTTPCode_Backend_2XX' => {
54
- 'Units' => 'Count',
55
- 'Statistics' => ['Sum'],
56
- },
57
- 'HTTPCode_Backend_3XX' => {
58
- 'Units' => 'Count',
59
- 'Statistics' => ['Sum'],
60
- },
61
- 'HTTPCode_Backend_4XX' => {
62
- 'Units' => 'Count',
63
- 'Statistics' => ['Sum'],
64
- },
65
- 'HTTPCode_Backend_5XX' => {
66
- 'Units' => 'Count',
67
- 'Statistics' => ['Sum'],
68
- },
69
- }
70
- end
71
-
72
- def base_metrics
73
- # get last 60 seconds
74
- start_time = (Time.now.utc - 60).iso8601
75
- end_time = Time.now.utc.iso8601
76
-
77
- # The base query that all metrics would get
78
- {
79
- 'Namespace' => 'AWS/ELB',
80
- 'StartTime' => start_time,
81
- 'EndTime' => end_time,
82
- 'Period' => 60,
83
- }
84
- end
85
-
86
- def tick
87
- if options[:fog_credentials_file]
88
- Fog.credentials_path = options[:fog_credentials_file]
89
- Fog.credential = options[:fog_credential].to_sym
90
- connection = Fog::AWS::CloudWatch.new
91
- else
92
- connection = if options[:aws_access] && options[:aws_secret]
93
- Fog::AWS::CloudWatch.new({
94
- aws_access_key_id: options[:aws_access],
95
- aws_secret_access_key: options[:aws_secret],
96
- region: options[:aws_region],
97
- })
98
- else
99
- Fog::AWS::CloudWatch.new({
100
- use_iam_profile: true,
101
- region: options[:aws_region],
102
- })
103
- end
104
- end
105
-
106
- options[:elbs].each do |lb|
107
- metric_options = standard_metrics
108
- metric_base_options = base_metrics
109
-
110
- options[:aws_azs].each do |az|
111
- metric_options.keys.sort.each do |metric_type|
112
- merged_options = metric_base_options.merge(metric_options[metric_type])
113
- merged_options['MetricName'] = metric_type
114
- merged_options['Dimensions'] = if az == 'all_az'
115
- [{ 'Name' => 'LoadBalancerName', 'Value' => lb }]
116
- else
117
- [
118
- { 'Name' => 'LoadBalancerName', 'Value' => lb },
119
- { 'Name' => 'AvailabilityZone', 'Value' => az },
120
- ]
121
- end
122
-
123
- result = connection.get_metric_statistics(merged_options)
124
-
125
- # "If no response codes in the category 2XX-5XX range are sent to clients within
126
- # the given time period, values for these metrics will not be recorded in CloudWatch"
127
- # next if result.body["GetMetricStatisticsResult"]["Datapoints"].empty? && metric_type =~ /[2345]XX/
128
- #
129
- if result.body['GetMetricStatisticsResult']['Datapoints'].empty?
130
- standard_metrics[metric_type]['Statistics'].each do |stat_type|
131
- event = event(lb, az, metric_type, stat_type, 0.0)
132
- report(event)
133
- end
134
- next
135
- end
136
-
137
- # We should only ever have a single data point
138
- result.body['GetMetricStatisticsResult']['Datapoints'][0].keys.sort.each do |stat_type|
139
- next if stat_type == 'Unit'
140
- next if stat_type == 'Timestamp'
141
-
142
- unit = result.body['GetMetricStatisticsResult']['Datapoints'][0]['Unit']
143
- metric = result.body['GetMetricStatisticsResult']['Datapoints'][0][stat_type]
144
- event = event(lb, az, metric_type, stat_type, metric, unit)
145
- report(event)
146
- end
147
- end
148
- end
149
- end
150
- end
151
-
152
- private
153
-
154
- def event(lb, az, metric_type, stat_type, metric, unit = nil)
155
- {
156
- host: lb,
157
- service: "elb.#{az}.#{metric_type}.#{stat_type}",
158
- ttl: 60,
159
- description: "#{lb} #{metric_type} #{stat_type} (#{unit})",
160
- tags: ['elb_metrics'],
161
- metric: metric,
162
- }
163
- end
164
- end
165
- end
166
- end
167
-
168
- Riemann::Tools::ELBMetrics.run
8
+ Riemann::Tools::Aws::ElbMetrics.run
@@ -3,85 +3,6 @@
3
3
 
4
4
  Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- require 'riemann/tools'
6
+ require 'riemann/tools/aws/s3_list'
7
7
 
8
- $0 = __FILE__
9
-
10
- module Riemann
11
- module Tools
12
- class S3Metrics
13
- include Riemann::Tools
14
- require 'fog'
15
- require 'time'
16
-
17
- opt :fog_credentials_file, 'Fog credentials file', type: String
18
- opt :fog_credential, 'Fog credentials to use', type: String
19
- opt :aws_access, 'AWS Access Key', type: String
20
- opt :aws_secret, 'AWS Secret Key', type: String
21
- opt :aws_region, 'AWS Region', type: String, default: 'eu-west-1'
22
- opt :buckets, 'Buckets to pull metrics from, multi=true, can have a prefix like mybucket/prefix', type: String,
23
- multi: true, required: true
24
- opt :max_objects, 'Max number of objects to list before stopping to save bandwidth', default: -1
25
-
26
- def tick
27
- if options[:fog_credentials_file]
28
- Fog.credentials_path = options[:fog_credentials_file]
29
- Fog.credential = options[:fog_credential].to_sym
30
- connection = Fog::Storage.new
31
- else
32
- connection = if options[:aws_access] && options[:aws_secret]
33
- Fog::Storage.new({
34
- provider: 'AWS',
35
- aws_access_key_id: options[:aws_access],
36
- aws_secret_access_key: options[:aws_secret],
37
- region: options[:aws_region],
38
- })
39
- else
40
- Fog::Storage.new({
41
- provider: 'AWS',
42
- use_iam_profile: true,
43
- region: options[:aws_region],
44
- })
45
- end
46
- end
47
-
48
- options[:buckets].each do |url|
49
- split = url.split('/')
50
- bucket = split[0]
51
- prefix = ''
52
- prefix = url[(split[0].length + 1)..] if split[1]
53
- count = 0
54
- connection.directories.get(bucket, prefix: prefix).files.map do |_file|
55
- count += 1
56
- break if options[:max_objects].positive? && count > options[:max_objects]
57
- end
58
- event = if options[:max_objects].positive? && count > options[:max_objects]
59
- event(
60
- url, 'objectCount', count, "count was bigger than threshold #{options[:max_objects]}",
61
- 'warning',
62
- )
63
- else
64
- event(url, 'objectCount', count, "All objects counted, threshold=#{options[:max_objects]}", 'ok')
65
- end
66
- report(event)
67
- end
68
- end
69
-
70
- private
71
-
72
- def event(bucket, label, metric, description, severity)
73
- {
74
- host: "bucket_#{bucket}",
75
- service: "s3.#{label}",
76
- ttl: 300,
77
- description: "#{bucket} #{description}",
78
- tags: ['s3_metrics'],
79
- metric: metric,
80
- state: severity,
81
- }
82
- end
83
- end
84
- end
85
- end
86
-
87
- Riemann::Tools::S3Metrics.run
8
+ Riemann::Tools::Aws::S3List.run
@@ -3,100 +3,6 @@
3
3
 
4
4
  Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- require 'riemann/tools'
6
+ require 'riemann/tools/aws/s3_status'
7
7
 
8
- $0 = __FILE__
9
-
10
- module Riemann
11
- module Tools
12
- class S3Metrics
13
- include Riemann::Tools
14
- require 'fog'
15
- require 'time'
16
-
17
- opt :fog_credentials_file, 'Fog credentials file', type: String
18
- opt :fog_credential, 'Fog credentials to use', type: String
19
- opt :aws_access, 'AWS Access Key', type: String
20
- opt :aws_secret, 'AWS Secret Key', type: String
21
- opt :aws_region, 'AWS Region', type: String, default: 'eu-west-1'
22
- opt :buckets, 'Buckets to pull metrics from, multi=true', type: String, multi: true, required: true
23
- opt :statistic, 'Statistic to retrieve, multi=true, e.g. --statistic=Average --statistic=Maximum', type: String,
24
- multi: true, required: true
25
-
26
- def base_metrics
27
- # get last 60 seconds
28
- start_time = (Time.now.utc - 3600 * 24 * 1).iso8601
29
- end_time = Time.now.utc.iso8601
30
-
31
- # The base query that all metrics would get
32
- {
33
- 'Namespace' => 'AWS/S3',
34
- 'StartTime' => start_time,
35
- 'EndTime' => end_time,
36
- 'Period' => 3600,
37
- 'MetricName' => 'NumberOfObjects',
38
- }
39
- end
40
-
41
- def tick
42
- if options[:fog_credentials_file]
43
- Fog.credentials_path = options[:fog_credentials_file]
44
- Fog.credential = options[:fog_credential].to_sym
45
- connection = Fog::AWS::CloudWatch.new
46
- else
47
- connection = if options[:aws_access] && options[:aws_secret]
48
- Fog::AWS::CloudWatch.new({
49
- aws_access_key_id: options[:aws_access],
50
- aws_secret_access_key: options[:aws_secret],
51
- region: options[:aws_region],
52
- })
53
- else
54
- Fog::AWS::CloudWatch.new({
55
- use_iam_profile: true,
56
- region: options[:aws_region],
57
- })
58
- end
59
- end
60
-
61
- options[:statistic].each do |statistic|
62
- options[:buckets].each do |bucket|
63
- metric_base_options = base_metrics
64
- metric_base_options['Statistics'] = statistic
65
- metric_base_options['Dimensions'] = [
66
- { 'Name' => 'BucketName', 'Value' => bucket },
67
- { 'Name' => 'StorageType', 'Value' => 'AllStorageTypes' },
68
- ]
69
-
70
- result = connection.get_metric_statistics(metric_base_options)
71
- next if result.body['GetMetricStatisticsResult']['Datapoints'].empty?
72
-
73
- result.body['GetMetricStatisticsResult']['Datapoints'][0].keys.sort.each do |stat_type|
74
- next if stat_type == 'Unit'
75
- next if stat_type == 'Timestamp'
76
-
77
- unit = result.body['GetMetricStatisticsResult']['Datapoints'][0]['Unit']
78
- metric = result.body['GetMetricStatisticsResult']['Datapoints'][0][stat_type]
79
- event = event(bucket, result.body['GetMetricStatisticsResult']['Label'], stat_type, unit, metric)
80
- report(event)
81
- end
82
- end
83
- end
84
- end
85
-
86
- private
87
-
88
- def event(bucket, label, metric_type, stat_type, metric, unit = nil)
89
- {
90
- host: "bucket_#{bucket}",
91
- service: "s3.#{label}.#{metric_type}.#{stat_type}",
92
- ttl: 300,
93
- description: "#{bucket} #{metric_type} #{stat_type} (#{unit})",
94
- tags: ['s3_metrics'],
95
- metric: metric,
96
- }
97
- end
98
- end
99
- end
100
- end
101
-
102
- Riemann::Tools::S3Metrics.run
8
+ Riemann::Tools::Aws::S3Status.run
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'riemann/tools'
4
+
5
+ module Riemann
6
+ module Tools
7
+ module Aws
8
+ class Billing
9
+ include Riemann::Tools
10
+ require 'fog/aws'
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,
18
+ default: %w[AmazonEC2 AmazonS3 AWSDataTransfer]
19
+
20
+ opt :time_start, 'Start time in seconds of the metrics period (2hrs ago default)', type: Integer, default: 7200
21
+ opt :time_end, 'End time in seconds of the metrics period ', type: Integer, default: 60
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
+ creds = if opts.key?('secret_key') && opts.key?('access_key')
30
+ {
31
+ aws_secret_access_key: opts[:secret_key],
32
+ aws_access_key_id: opts[:access_key],
33
+ }
34
+ else
35
+ { 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
+ data.each do |metrics|
66
+ name = "AWScloudwatch.Billing.#{service}"
67
+ value = metrics['Maximum']
68
+ timestamp = metrics['Timestamp'].to_i
69
+
70
+ event = {
71
+ host: nil,
72
+ service: name,
73
+ time: timestamp,
74
+ description: "AWS Estimate Charges for #{service}",
75
+ tags: ['aws_billing'],
76
+ state: 'ok',
77
+ metric: value,
78
+ }
79
+
80
+ report event
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end